Home | History | Annotate | Download | only in shadows
      1 package com.xtremelabs.robolectric.shadows;
      2 
      3 import static com.xtremelabs.robolectric.Robolectric.shadowOf;
      4 import static org.hamcrest.CoreMatchers.equalTo;
      5 import static org.junit.Assert.assertArrayEquals;
      6 import static org.junit.Assert.assertNull;
      7 import static org.junit.Assert.assertThat;
      8 import static org.junit.Assert.assertTrue;
      9 
     10 import com.xtremelabs.robolectric.WithTestDefaultsRunner;
     11 
     12 import org.junit.Before;
     13 import org.junit.Test;
     14 import org.junit.runner.RunWith;
     15 
     16 import android.accounts.Account;
     17 import android.accounts.AccountManager;
     18 import android.accounts.AccountManagerFuture;
     19 import android.app.Activity;
     20 import android.os.Bundle;
     21 
     22 @RunWith(WithTestDefaultsRunner.class)
     23 public class AccountManagerTest {
     24 
     25     private static final Account ACCOUNT = new Account("name", "type");
     26 
     27     private AccountManager accountManager;
     28     private Activity activity;
     29     private String accountType;
     30     private String authTokenType;
     31     private String[] features;
     32 
     33     @Before
     34     public void setUp() {
     35         activity = new Activity();
     36         accountManager = AccountManager.get(activity);
     37         accountManager.invalidateAuthToken(null, null);
     38         accountType = "accountType";
     39         authTokenType = "authTokenType";
     40         features = new String[]{};
     41     }
     42 
     43     @Test
     44     public void testGetAuthTokenByFeatures_isCancelled() throws Exception {
     45         AccountManagerFuture<Bundle> future =
     46                 accountManager.getAuthTokenByFeatures(accountType, authTokenType, features, activity, null, null, null, null);
     47 
     48         assertThat(future.isCancelled(), equalTo(false));
     49         future.cancel(true);
     50         assertThat(future.isCancelled(), equalTo(true));
     51     }
     52 
     53     @Test
     54     public void testGetAuthTokenByFeatures_isDoneWithCancel() throws Exception {
     55         AccountManagerFuture<Bundle> future =
     56                 accountManager.getAuthTokenByFeatures(accountType, authTokenType, features, activity, null, null, null, null);
     57 
     58         assertThat(future.isDone(), equalTo(false));
     59         future.cancel(true);
     60         assertThat(future.isDone(), equalTo(true));
     61     }
     62 
     63     @Test
     64     public void testGetAuthTokenByFeatures_isDoneWithGetResult() throws Exception {
     65         AccountManagerFuture<Bundle> future =
     66                 accountManager.getAuthTokenByFeatures(accountType, authTokenType, features, activity, null, null, null, null);
     67 
     68         assertThat(future.isDone(), equalTo(false));
     69         future.getResult();
     70         assertThat(future.isDone(), equalTo(true));
     71     }
     72 
     73     @Test
     74     public void testInvalidateAuthToken() throws Exception {
     75         // Check that it doesn't crash
     76         accountManager.invalidateAuthToken(accountType, null);
     77     }
     78 
     79     @Test
     80     public void testGetAuthTokenByFeatures_getResult() throws Exception {
     81         AccountManagerFuture<Bundle> future =
     82                 accountManager.getAuthTokenByFeatures(accountType, authTokenType, features, activity, null, null, null, null);
     83 
     84         Bundle result = future.getResult();
     85         assertThat(result.containsKey(AccountManager.KEY_AUTHTOKEN), equalTo(true));
     86         assertThat(result.containsKey(AccountManager.KEY_ACCOUNT_NAME), equalTo(true));
     87         assertThat(result.containsKey(AccountManager.KEY_ACCOUNT_TYPE), equalTo(true));
     88     }
     89 
     90     @Test
     91     public void testGetAuthToken_getResult() throws Exception {
     92         AccountManagerFuture<Bundle> future =
     93                 accountManager.getAuthToken(ACCOUNT, authTokenType, null, activity, null, null);
     94 
     95         Bundle result = future.getResult();
     96         assertThat(ACCOUNT.name, equalTo(result.getString(AccountManager.KEY_ACCOUNT_NAME)));
     97         assertThat(ACCOUNT.type, equalTo(result.getString(AccountManager.KEY_ACCOUNT_TYPE)));
     98     }
     99 
    100     @Test
    101     public void testAccountManagerIsSingleton() throws Exception {
    102         AccountManager ref = AccountManager.get(activity);
    103 
    104         assertTrue(ref == accountManager);
    105         assertThat(ref, equalTo(accountManager));
    106     }
    107 
    108     @Test
    109     public void testGetAccounts() throws Exception {
    110         Account[] origAccounts = new Account[] { ACCOUNT, ACCOUNT };
    111         shadowOf(accountManager).setAccounts(origAccounts);
    112 
    113         Account[] accounts = accountManager.getAccounts();
    114 
    115         assertArrayEquals(origAccounts, accounts);
    116     }
    117 
    118     @Test
    119     public void testGetAccountsByType() throws Exception {
    120         Account diffAccount = new Account("myName", "myType");
    121         Account[] origAccounts = new Account[] { ACCOUNT, diffAccount };
    122         shadowOf(accountManager).setAccounts(origAccounts);
    123 
    124         Account[] accounts = accountManager.getAccountsByType("myType");
    125 
    126         assertThat(accounts.length, equalTo(1));
    127         assertThat(accounts[0], equalTo(diffAccount));
    128     }
    129 
    130     @Test
    131     public void testPeek() throws Exception {
    132         shadowOf(accountManager).setCachedAuthToken(ACCOUNT, authTokenType, "myToken");
    133 
    134         String authToken = accountManager.peekAuthToken(ACCOUNT, authTokenType);
    135 
    136         assertThat(authToken, equalTo("myToken"));
    137     }
    138 
    139     @Test
    140     public void testPeek_differentAccount() throws Exception {
    141         shadowOf(accountManager).setCachedAuthToken(ACCOUNT, authTokenType, "myToken");
    142 
    143         String authToken =
    144                 accountManager.peekAuthToken(new Account("other", "type"), authTokenType);
    145 
    146         assertNull(authToken);
    147     }
    148 
    149     @Test
    150     public void testPeek_differentAuthTokenType() throws Exception {
    151         shadowOf(accountManager).setCachedAuthToken(ACCOUNT, authTokenType, "myToken");
    152 
    153         String authToken = accountManager.peekAuthToken(ACCOUNT, "other");
    154 
    155         assertNull(authToken);
    156     }
    157 }
    158