Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
23410 tejbeer 1
/*
2
 * Copyright (C) 2015 The Android Open Source Project
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 * you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at
7
 *
8
 *      http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
15
 */
16
 
17
package com.android.volley.toolbox;
18
 
19
import android.accounts.Account;
20
import android.accounts.AccountManager;
21
import android.accounts.AccountManagerFuture;
22
import android.accounts.AuthenticatorException;
23
import android.content.Context;
24
import android.content.Intent;
25
import android.os.Bundle;
26
 
27
import com.android.volley.AuthFailureError;
28
 
29
import org.junit.Assert;
30
import org.junit.Before;
31
import org.junit.Test;
32
import org.junit.runner.RunWith;
33
import org.robolectric.RobolectricTestRunner;
34
 
35
import static org.mockito.Mockito.mock;
36
import static org.mockito.Mockito.verify;
37
import static org.mockito.Mockito.when;
38
 
39
@RunWith(RobolectricTestRunner.class)
40
public class AndroidAuthenticatorTest {
41
    private AccountManager mAccountManager;
42
    private Account mAccount;
43
    private AccountManagerFuture<Bundle> mFuture;
44
    private AndroidAuthenticator mAuthenticator;
45
 
46
    @Before
47
    public void setUp() {
48
        mAccountManager = mock(AccountManager.class);
49
        mFuture = mock(AccountManagerFuture.class);
50
        mAccount = new Account("coolperson", "cooltype");
51
        mAuthenticator = new AndroidAuthenticator(mAccountManager, mAccount, "cooltype", false);
52
    }
53
 
54
    @Test(expected = AuthFailureError.class)
55
    public void failedGetAuthToken() throws Exception {
56
        when(mAccountManager.getAuthToken(mAccount, "cooltype", false, null, null)).thenReturn(mFuture);
57
        when(mFuture.getResult()).thenThrow(new AuthenticatorException("sadness!"));
58
        mAuthenticator.getAuthToken();
59
    }
60
 
61
    @Test(expected = AuthFailureError.class)
62
    public void resultContainsIntent() throws Exception {
63
        Intent intent = new Intent();
64
        Bundle bundle = new Bundle();
65
        bundle.putParcelable(AccountManager.KEY_INTENT, intent);
66
        when(mAccountManager.getAuthToken(mAccount, "cooltype", false, null, null)).thenReturn(mFuture);
67
        when(mFuture.getResult()).thenReturn(bundle);
68
        when(mFuture.isDone()).thenReturn(true);
69
        when(mFuture.isCancelled()).thenReturn(false);
70
        mAuthenticator.getAuthToken();
71
    }
72
 
73
    @Test(expected = AuthFailureError.class)
74
    public void missingAuthToken() throws Exception {
75
        Bundle bundle = new Bundle();
76
        when(mAccountManager.getAuthToken(mAccount, "cooltype", false, null, null)).thenReturn(mFuture);
77
        when(mFuture.getResult()).thenReturn(bundle);
78
        when(mFuture.isDone()).thenReturn(true);
79
        when(mFuture.isCancelled()).thenReturn(false);
80
        mAuthenticator.getAuthToken();
81
    }
82
 
83
    @Test
84
    public void invalidateAuthToken() throws Exception {
85
        mAuthenticator.invalidateAuthToken("monkey");
86
        verify(mAccountManager).invalidateAuthToken("cooltype", "monkey");
87
    }
88
 
89
    @Test
90
    public void goodToken() throws Exception {
91
        Bundle bundle = new Bundle();
92
        bundle.putString(AccountManager.KEY_AUTHTOKEN, "monkey");
93
        when(mAccountManager.getAuthToken(mAccount, "cooltype", false, null, null)).thenReturn(mFuture);
94
        when(mFuture.getResult()).thenReturn(bundle);
95
        when(mFuture.isDone()).thenReturn(true);
96
        when(mFuture.isCancelled()).thenReturn(false);
97
        Assert.assertEquals("monkey", mAuthenticator.getAuthToken());
98
    }
99
 
100
    @Test
101
    public void publicMethods() throws Exception {
102
        // Catch-all test to find API-breaking changes.
103
        Context context = mock(Context.class);
104
        new AndroidAuthenticator(context, mAccount, "cooltype");
105
        new AndroidAuthenticator(context, mAccount, "cooltype", true);
106
        Assert.assertSame(mAccount, mAuthenticator.getAccount());
107
    }
108
}