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.assertNull; 6 import static org.junit.Assert.assertSame; 7 import static org.mockito.Matchers.any; 8 import static org.mockito.Mockito.when; 9 10 import android.app.Activity; 11 import android.content.Context; 12 import com.google.android.gms.common.ConnectionResult; 13 import com.google.android.gms.common.GooglePlayServicesUtil; 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.ShadowGooglePlayServicesUtil.GooglePlayServicesUtilImpl; 25 26 @RunWith(RobolectricTestRunner.class) 27 @Config(manifest = Config.NONE, shadows = {ShadowGooglePlayServicesUtil.class}) 28 public class ShadowGooglePlayServicesUtilTest { 29 30 @Mock 31 private GooglePlayServicesUtilImpl mockGooglePlayServicesUtil; 32 33 @Rule 34 public ExpectedException thrown = ExpectedException.none(); 35 36 @Before 37 public void setup() { 38 MockitoAnnotations.initMocks(this); 39 } 40 41 @Test 42 public void getImplementation_defaultNotNull() { 43 assertNotNull(ShadowGooglePlayServicesUtil.getImpl()); 44 } 45 46 @Test 47 public void provideImplementation_nullValueNotAllowed() { 48 thrown.expect(NullPointerException.class); 49 ShadowGooglePlayServicesUtil.provideImpl(null); 50 } 51 52 @Test 53 public void getImplementation_shouldGetSetted() { 54 ShadowGooglePlayServicesUtil.provideImpl(mockGooglePlayServicesUtil); 55 ShadowGooglePlayServicesUtil.GooglePlayServicesUtilImpl googlePlayServicesUtil = 56 ShadowGooglePlayServicesUtil.getImpl(); 57 assertSame(googlePlayServicesUtil, mockGooglePlayServicesUtil); 58 } 59 60 @Test 61 public void canRedirectStaticMethodToImplementation() { 62 ShadowGooglePlayServicesUtil.provideImpl(mockGooglePlayServicesUtil); 63 when(mockGooglePlayServicesUtil.isGooglePlayServicesAvailable( 64 any(Context.class))).thenReturn(ConnectionResult.INTERNAL_ERROR); 65 assertEquals(ConnectionResult.INTERNAL_ERROR, 66 GooglePlayServicesUtil.isGooglePlayServicesAvailable(RuntimeEnvironment.application)); 67 } 68 69 @Test 70 public void getErrorString_goesToRealImpl() { 71 assertEquals("SUCCESS", GooglePlayServicesUtil.getErrorString(ConnectionResult.SUCCESS)); 72 assertEquals("SERVICE_MISSING", GooglePlayServicesUtil 73 .getErrorString(ConnectionResult.SERVICE_MISSING)); 74 } 75 76 @Test 77 public void getRemoteContext_defaultNotNull() { 78 assertNotNull(GooglePlayServicesUtil.getRemoteContext(RuntimeEnvironment.application)); 79 } 80 81 @Test 82 public void getRemoteResource_defaultNotNull() { 83 assertNotNull(GooglePlayServicesUtil.getRemoteResource(RuntimeEnvironment.application)); 84 } 85 86 @Test 87 public void getErrorDialog() { 88 assertNotNull(GooglePlayServicesUtil.getErrorDialog( 89 ConnectionResult.SERVICE_MISSING, new Activity(), 0)); 90 assertNull(GooglePlayServicesUtil.getErrorDialog( 91 ConnectionResult.SUCCESS, new Activity(), 0)); 92 assertNotNull(GooglePlayServicesUtil.getErrorDialog( 93 ConnectionResult.SERVICE_MISSING, new Activity(), 0, null)); 94 assertNull(GooglePlayServicesUtil.getErrorDialog( 95 ConnectionResult.SUCCESS, new Activity(), 0, null)); 96 } 97 98 @Test 99 public void getErrorPendingIntent() { 100 assertNotNull(GooglePlayServicesUtil.getErrorPendingIntent( 101 ConnectionResult.SERVICE_MISSING, RuntimeEnvironment.application, 0)); 102 assertNull(GooglePlayServicesUtil.getErrorPendingIntent( 103 ConnectionResult.SUCCESS, RuntimeEnvironment.application, 0)); 104 } 105 106 @Test 107 public void getOpenSourceSoftwareLicenseInfo_defaultNotNull() { 108 assertNotNull(GooglePlayServicesUtil.getOpenSourceSoftwareLicenseInfo( 109 RuntimeEnvironment.application)); 110 } 111 112 @Test 113 public void isGooglePlayServicesAvailable_defaultServiceMissing() { 114 assertEquals(ConnectionResult.SERVICE_MISSING, 115 GooglePlayServicesUtil.isGooglePlayServicesAvailable(RuntimeEnvironment.application)); 116 } 117 } 118