1 package org.robolectric.shadows.gms.common; 2 3 import static org.assertj.core.api.Assertions.assertThat; 4 import static org.mockito.Mockito.mock; 5 6 import android.app.Activity; 7 import android.app.Dialog; 8 import android.content.Context; 9 import android.content.DialogInterface; 10 import com.google.android.gms.common.ConnectionResult; 11 import com.google.android.gms.common.GoogleApiAvailability; 12 import org.junit.After; 13 import org.junit.Before; 14 import org.junit.Test; 15 import org.junit.runner.RunWith; 16 import org.robolectric.RobolectricTestRunner; 17 import org.robolectric.RuntimeEnvironment; 18 import org.robolectric.annotation.Config; 19 import org.robolectric.shadows.gms.Shadows; 20 21 /** 22 * Created by diegotori on 2/14/16. 23 */ 24 @RunWith(RobolectricTestRunner.class) 25 @Config(manifest = Config.NONE, shadows = {ShadowGoogleApiAvailability.class}) 26 public class ShadowGoogleApiAvailabilityTest { 27 28 private Context roboContext; 29 30 @Before 31 public void setUp() { 32 roboContext = RuntimeEnvironment.application; 33 } 34 35 @After 36 public void tearDown() { 37 roboContext = null; 38 } 39 40 @Test 41 public void getInstance() { 42 //Given the expected GoogleApiAvailability instance 43 final GoogleApiAvailability expected = GoogleApiAvailability.getInstance(); 44 45 //When getting the actual one from the shadow 46 final GoogleApiAvailability actual = ShadowGoogleApiAvailability.getInstance(); 47 48 //Then verify that the expected is a not null and equal to the actual one 49 assertThat(expected).isNotNull().isEqualTo(actual); 50 } 51 52 @Test 53 public void shadowOf() { 54 final ShadowGoogleApiAvailability shadowGoogleApiAvailability 55 = Shadows.shadowOf(GoogleApiAvailability.getInstance()); 56 assertThat(shadowGoogleApiAvailability).isNotNull(); 57 } 58 59 @Test 60 public void setIsGooglePlayServicesAvailable() { 61 //Given an expected and injected ConnectionResult code 62 final ShadowGoogleApiAvailability shadowGoogleApiAvailability 63 = Shadows.shadowOf(GoogleApiAvailability.getInstance()); 64 final int expectedCode = ConnectionResult.SUCCESS; 65 shadowGoogleApiAvailability.setIsGooglePlayServicesAvailable(expectedCode); 66 67 //When getting the actual ConnectionResult code 68 final int actualCode = GoogleApiAvailability.getInstance() 69 .isGooglePlayServicesAvailable(roboContext); 70 71 //Then verify that we got back our expected code and not the default one. 72 assertThat(actualCode) 73 .isNotEqualTo(ConnectionResult.SERVICE_MISSING) 74 .isEqualTo(expectedCode); 75 } 76 77 @Test 78 public void setIsUserResolvableError() { 79 //Given an injected user resolvable error flag 80 final ShadowGoogleApiAvailability shadowGoogleApiAvailability 81 = Shadows.shadowOf(GoogleApiAvailability.getInstance()); 82 shadowGoogleApiAvailability.setIsUserResolvableError(true); 83 84 //When getting the actual flag value 85 final boolean actual = GoogleApiAvailability.getInstance() 86 .isUserResolvableError(ConnectionResult.API_UNAVAILABLE); 87 88 //Then verify that its equal to true 89 assertThat(actual).isTrue(); 90 } 91 92 @Test 93 public void setOpenSourceSoftwareLicenseInfo() { 94 //Given mock open source license info 95 final String expected = "Mock open source license info"; 96 final ShadowGoogleApiAvailability shadowGoogleApiAvailability 97 = Shadows.shadowOf(GoogleApiAvailability.getInstance()); 98 shadowGoogleApiAvailability.setOpenSourceSoftwareLicenseInfo(expected); 99 100 //When getting the actual value 101 final String actual = GoogleApiAvailability.getInstance() 102 .getOpenSourceSoftwareLicenseInfo(roboContext); 103 104 //Then verify that its not null, not empty, and equal to the expected value 105 assertThat(actual) 106 .isNotNull() 107 .isNotEmpty() 108 .isEqualTo(expected); 109 } 110 111 @Test 112 public void setErrorDialog(){ 113 final ShadowGoogleApiAvailability shadowGoogleApiAvailability 114 = Shadows.shadowOf(GoogleApiAvailability.getInstance()); 115 final Dialog expectedDialog = mock(Dialog.class); 116 final Activity mockActivity = mock(Activity.class); 117 final int mockErrorCode = ConnectionResult.API_UNAVAILABLE; 118 final int mockRequestCode = 1234; 119 shadowGoogleApiAvailability.setErrorDialog(expectedDialog); 120 121 final Dialog actualDialog = GoogleApiAvailability.getInstance() 122 .getErrorDialog(mockActivity, mockErrorCode, mockRequestCode); 123 124 assertThat(actualDialog) 125 .isNotNull() 126 .isEqualTo(expectedDialog); 127 } 128 129 @Test 130 public void setErrorDialog__OnCancelListenerMethod(){ 131 final ShadowGoogleApiAvailability shadowGoogleApiAvailability 132 = Shadows.shadowOf(GoogleApiAvailability.getInstance()); 133 final Dialog expectedDialog = mock(Dialog.class); 134 final Activity mockActivity = mock(Activity.class); 135 final DialogInterface.OnCancelListener mockOnCancelListener = 136 mock(DialogInterface.OnCancelListener.class); 137 final int mockErrorCode = ConnectionResult.API_UNAVAILABLE; 138 final int mockRequestCode = 1234; 139 shadowGoogleApiAvailability.setErrorDialog(expectedDialog); 140 141 final Dialog actualDialog = GoogleApiAvailability.getInstance() 142 .getErrorDialog(mockActivity, mockErrorCode, mockRequestCode, mockOnCancelListener); 143 144 assertThat(actualDialog) 145 .isNotNull() 146 .isEqualTo(expectedDialog); 147 } 148 } 149