Home | History | Annotate | Download | only in toolbox
      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 import com.android.volley.AuthFailureError;
     27 import org.junit.Assert;
     28 import org.junit.Before;
     29 import org.junit.Ignore;
     30 import org.junit.Test;
     31 import org.junit.runner.RunWith;
     32 import org.mockito.Mock;
     33 import org.mockito.MockitoAnnotations;
     34 import org.robolectric.RobolectricTestRunner;
     35 
     36 import static org.mockito.Mockito.*;
     37 
     38 @RunWith(RobolectricTestRunner.class)
     39 public class AndroidAuthenticatorTest {
     40     private AccountManager mAccountManager;
     41     private Account mAccount;
     42     private AccountManagerFuture<Bundle> mFuture;
     43     private AndroidAuthenticator mAuthenticator;
     44 
     45     @Before
     46     public void setUp() {
     47         mAccountManager = mock(AccountManager.class);
     48         mFuture = mock(AccountManagerFuture.class);
     49         mAccount = new Account("coolperson", "cooltype");
     50         mAuthenticator = new AndroidAuthenticator(mAccountManager, mAccount, "cooltype", false);
     51     }
     52 
     53     @Test(expected = AuthFailureError.class)
     54     public void failedGetAuthToken() throws Exception {
     55         when(mAccountManager.getAuthToken(mAccount, "cooltype", false, null, null)).thenReturn(mFuture);
     56         when(mFuture.getResult()).thenThrow(new AuthenticatorException("sadness!"));
     57         mAuthenticator.getAuthToken();
     58     }
     59 
     60     @Test(expected = AuthFailureError.class)
     61     public void resultContainsIntent() throws Exception {
     62         Intent intent = new Intent();
     63         Bundle bundle = new Bundle();
     64         bundle.putParcelable(AccountManager.KEY_INTENT, intent);
     65         when(mAccountManager.getAuthToken(mAccount, "cooltype", false, null, null)).thenReturn(mFuture);
     66         when(mFuture.getResult()).thenReturn(bundle);
     67         when(mFuture.isDone()).thenReturn(true);
     68         when(mFuture.isCancelled()).thenReturn(false);
     69         mAuthenticator.getAuthToken();
     70     }
     71 
     72     @Test(expected = AuthFailureError.class)
     73     public void missingAuthToken() throws Exception {
     74         Bundle bundle = new Bundle();
     75         when(mAccountManager.getAuthToken(mAccount, "cooltype", false, null, null)).thenReturn(mFuture);
     76         when(mFuture.getResult()).thenReturn(bundle);
     77         when(mFuture.isDone()).thenReturn(true);
     78         when(mFuture.isCancelled()).thenReturn(false);
     79         mAuthenticator.getAuthToken();
     80     }
     81 
     82     @Test
     83     public void invalidateAuthToken() throws Exception {
     84         mAuthenticator.invalidateAuthToken("monkey");
     85         verify(mAccountManager).invalidateAuthToken("cooltype", "monkey");
     86     }
     87 
     88     @Test
     89     public void goodToken() throws Exception {
     90         Bundle bundle = new Bundle();
     91         bundle.putString(AccountManager.KEY_AUTHTOKEN, "monkey");
     92         when(mAccountManager.getAuthToken(mAccount, "cooltype", false, null, null)).thenReturn(mFuture);
     93         when(mFuture.getResult()).thenReturn(bundle);
     94         when(mFuture.isDone()).thenReturn(true);
     95         when(mFuture.isCancelled()).thenReturn(false);
     96         Assert.assertEquals("monkey", mAuthenticator.getAuthToken());
     97     }
     98 
     99     @Test
    100     public void publicMethods() throws Exception {
    101         // Catch-all test to find API-breaking changes.
    102         Context context = mock(Context.class);
    103         new AndroidAuthenticator(context, mAccount, "cooltype");
    104         new AndroidAuthenticator(context, mAccount, "cooltype", true);
    105         Assert.assertSame(mAccount, mAuthenticator.getAccount());
    106     }
    107 }
    108