Home | History | Annotate | Download | only in gms
      1 package org.robolectric.shadows.gms;
      2 
      3 import static org.junit.Assert.assertEquals;
      4 import static org.junit.Assert.assertNotNull;
      5 import static org.junit.Assert.assertSame;
      6 import static org.mockito.Mockito.times;
      7 import static org.mockito.Mockito.verify;
      8 
      9 import android.accounts.Account;
     10 import android.content.Intent;
     11 import com.google.android.gms.auth.AccountChangeEvent;
     12 import com.google.android.gms.auth.GoogleAuthUtil;
     13 import java.util.List;
     14 import org.junit.Before;
     15 import org.junit.Rule;
     16 import org.junit.Test;
     17 import org.junit.rules.ExpectedException;
     18 import org.junit.runner.RunWith;
     19 import org.mockito.Mock;
     20 import org.mockito.MockitoAnnotations;
     21 import org.robolectric.RobolectricTestRunner;
     22 import org.robolectric.RuntimeEnvironment;
     23 import org.robolectric.annotation.Config;
     24 import org.robolectric.shadows.gms.ShadowGoogleAuthUtil.GoogleAuthUtilImpl;
     25 
     26 /**
     27  * Unit test for {@link ShadowGoogleAuthUtil}.
     28  */
     29 @RunWith(RobolectricTestRunner.class)
     30 @Config(manifest = Config.NONE, shadows = {ShadowGoogleAuthUtil.class})
     31 public class ShadowGoogleAuthUtilTest {
     32 
     33   @Mock private GoogleAuthUtilImpl mockGoogleAuthUtil;
     34 
     35   @Rule
     36   public ExpectedException thrown = ExpectedException.none();
     37 
     38   @Before
     39   public void setup() {
     40     MockitoAnnotations.initMocks(this);
     41     ShadowGoogleAuthUtil.reset();
     42   }
     43 
     44   @Test
     45   public void getImplementation_defaultNotNull() {
     46     assertNotNull(ShadowGoogleAuthUtil.getImpl());
     47   }
     48 
     49   @Test
     50   public void provideImplementation_nullValueNotAllowed() {
     51     thrown.expect(NullPointerException.class);
     52     ShadowGoogleAuthUtil.provideImpl(null);
     53   }
     54 
     55   @Test
     56   public void getImplementation_shouldGetSetted() {
     57     ShadowGoogleAuthUtil.provideImpl(mockGoogleAuthUtil);
     58     GoogleAuthUtilImpl googleAuthUtil = ShadowGoogleAuthUtil.getImpl();
     59     assertSame(googleAuthUtil, mockGoogleAuthUtil);
     60   }
     61 
     62   @Test
     63   public void canRedirectStaticMethodToImplementation() throws Exception {
     64     ShadowGoogleAuthUtil.provideImpl(mockGoogleAuthUtil);
     65     GoogleAuthUtil.clearToken(RuntimeEnvironment.application, "token");
     66     verify(mockGoogleAuthUtil, times(1)).clearToken(RuntimeEnvironment.application, "token");
     67   }
     68 
     69   @Test
     70   public void getAccountChangeEvents_defaultReturnEmptyList() throws Exception {
     71     List<AccountChangeEvent> list = GoogleAuthUtil.getAccountChangeEvents(
     72         RuntimeEnvironment.application, 0, "name");
     73     assertNotNull(list);
     74     assertEquals(0, list.size());
     75   }
     76 
     77   @Test
     78   public void getAccountId_defaultNotNull() throws Exception {
     79     assertNotNull(GoogleAuthUtil.getAccountId(RuntimeEnvironment.application, "name"));
     80   }
     81 
     82   @Test
     83   public void getToken_defaultNotNull() throws Exception {
     84     assertNotNull(GoogleAuthUtil.getToken(RuntimeEnvironment.application, "name", "scope"));
     85     assertNotNull(GoogleAuthUtil.getToken(RuntimeEnvironment.application, "name", "scope",
     86         null));
     87     assertNotNull(GoogleAuthUtil.getToken(RuntimeEnvironment.application, new Account("name",
     88         "robo"), "scope"));
     89     assertNotNull(GoogleAuthUtil.getToken(RuntimeEnvironment.application, new Account("name",
     90         "robo"), "scope", null));
     91     assertNotNull(GoogleAuthUtil.getTokenWithNotification(RuntimeEnvironment.application,
     92         "name", "scope", null));
     93     assertNotNull(GoogleAuthUtil.getTokenWithNotification(RuntimeEnvironment.application,
     94         "name", "scope", null, new Intent()));
     95     assertNotNull(GoogleAuthUtil.getTokenWithNotification(RuntimeEnvironment.application,
     96         "name", "scope", null, "authority", null));
     97     assertNotNull(GoogleAuthUtil.getTokenWithNotification(RuntimeEnvironment.application,
     98         new Account("name", "robo"), "scope", null));
     99     assertNotNull(GoogleAuthUtil.getTokenWithNotification(RuntimeEnvironment.application,
    100         new Account("name", "robo"), "scope", null, new Intent()));
    101     assertNotNull(GoogleAuthUtil.getTokenWithNotification(RuntimeEnvironment.application,
    102         new Account("name", "robo"), "scope", null, "authority", null));
    103   }
    104 
    105   @Test
    106   public void getTokenWithNotification_nullCallBackThrowIllegalArgumentException()
    107       throws Exception {
    108     thrown.expect(IllegalArgumentException.class);
    109     GoogleAuthUtil.getTokenWithNotification(RuntimeEnvironment.application, "name", "scope",
    110         null, null);
    111   }
    112 
    113   @Test
    114   public void getTokenWithNotification_nullAuthorityThrowIllegalArgumentException()
    115       throws Exception {
    116     thrown.expect(IllegalArgumentException.class);
    117     assertNotNull(GoogleAuthUtil.getTokenWithNotification(RuntimeEnvironment.application,
    118         "name", "scope", null, null, null));
    119   }
    120 }
    121