11package com .iterable .iterableapi ;
22
3- import com . google . firebase . iid . FirebaseInstanceId ;
3+ import android . content . Context ;
44
55import org .json .JSONObject ;
66import org .junit .After ;
77import org .junit .Before ;
88import org .junit .Test ;
99import org .mockito .ArgumentCaptor ;
10- import org .powermock .api .mockito .PowerMockito ;
11- import org .powermock .core .MockRepository ;
12- import org .powermock .core .classloader .annotations .PrepareForTest ;
10+ import org .robolectric .Robolectric ;
1311import org .robolectric .RuntimeEnvironment ;
1412import org .robolectric .shadows .ShadowApplication ;
1513
2826import static org .mockito .Mockito .verify ;
2927import static org .mockito .Mockito .when ;
3028
31- @ PrepareForTest ({IterablePushRegistration .Util .class , FirebaseInstanceId .class })
32- public class IterablePushRegistrationTest extends BasePowerMockTest {
29+ public class IterablePushRegistrationTest extends BaseTest {
3330
3431 private static final String TEST_TOKEN = "testToken" ;
3532 private static final String NEW_TOKEN = "newToken" ;
@@ -40,7 +37,8 @@ public class IterablePushRegistrationTest extends BasePowerMockTest {
4037 private MockWebServer server ;
4138 private IterableApi originalApi ;
4239 private IterableApi apiMock ;
43- private FirebaseInstanceId mockInstanceId ;
40+ private IterablePushRegistration .Util .UtilImpl originalPushRegistrationUtil ;
41+ private IterablePushRegistration .Util .UtilImpl pushRegistrationUtilMock ;
4442
4543 @ Before
4644 public void setUp () throws Exception {
@@ -52,24 +50,25 @@ public void setUp() throws Exception {
5250 apiMock = spy (IterableApi .sharedInstance );
5351 IterableApi .sharedInstance = apiMock ;
5452
55- mockInstanceId = mock (FirebaseInstanceId .class );
56- PowerMockito .stub (PowerMockito .method (FirebaseInstanceId .class , "getInstance" )).toReturn (mockInstanceId );
57- PowerMockito .stub (PowerMockito .method (IterablePushRegistration .Util .class , "getFirebaseResouceId" )).toReturn (1 );
53+ originalPushRegistrationUtil = IterablePushRegistration .Util .instance ;
54+ pushRegistrationUtilMock = mock (IterablePushRegistration .Util .UtilImpl .class );
55+ IterablePushRegistration .Util .instance = pushRegistrationUtilMock ;
56+
57+ when (pushRegistrationUtilMock .getFirebaseResouceId (any (Context .class ))).thenReturn (1 );
5858 }
5959
6060 @ After
6161 public void tearDown () throws Exception {
62+ IterablePushRegistration .Util .instance = originalPushRegistrationUtil ;
6263 IterableApi .sharedInstance = originalApi ;
63- MockRepository .remove (FirebaseInstanceId .class );
64- MockRepository .remove (IterablePushRegistration .Util .class );
6564
6665 server .shutdown ();
6766 server = null ;
6867 }
6968
7069 @ Test
7170 public void testEnableDevice () throws Exception {
72- when (mockInstanceId . getToken ()).thenReturn (TEST_TOKEN );
71+ when (pushRegistrationUtilMock . getFirebaseToken ()).thenReturn (TEST_TOKEN );
7372
7473 IterablePushRegistrationData data = new IterablePushRegistrationData (IterableTestUtils .userEmail , null , INTEGRATION_NAME , IterablePushRegistrationData .PushRegistrationAction .ENABLE );
7574 new IterablePushRegistration ().execute (data );
@@ -80,7 +79,7 @@ public void testEnableDevice() throws Exception {
8079
8180 @ Test
8281 public void testDisableDevice () throws Exception {
83- when (mockInstanceId . getToken ()).thenReturn ("testToken" );
82+ when (pushRegistrationUtilMock . getFirebaseToken ()).thenReturn ("testToken" );
8483
8584 IterablePushRegistrationData data = new IterablePushRegistrationData (IterableTestUtils .userEmail , null , INTEGRATION_NAME , IterablePushRegistrationData .PushRegistrationAction .DISABLE );
8685 new IterablePushRegistration ().execute (data );
@@ -93,12 +92,11 @@ public void testDisableDevice() throws Exception {
9392 public void testDisableOldGcmToken () throws Exception {
9493 IterableApi .initialize (RuntimeEnvironment .application , "apiKey" , new IterableConfig .Builder ().setLegacyGCMSenderId (GCM_SENDER_ID ).build ());
9594
96- when (mockInstanceId . getToken ()).thenReturn (NEW_TOKEN );
97- when (mockInstanceId . getToken (eq (GCM_SENDER_ID ), eq (IterableConstants .MESSAGING_PLATFORM_GOOGLE ))).thenReturn (OLD_TOKEN );
95+ when (pushRegistrationUtilMock . getFirebaseToken ()).thenReturn (NEW_TOKEN );
96+ when (pushRegistrationUtilMock . getFirebaseToken (eq (GCM_SENDER_ID ), eq (IterableConstants .MESSAGING_PLATFORM_GOOGLE ))).thenReturn (OLD_TOKEN );
9897
9998 IterablePushRegistrationData data = new IterablePushRegistrationData (IterableTestUtils .userEmail , null , INTEGRATION_NAME , IterablePushRegistrationData .PushRegistrationAction .ENABLE );
10099 new IterablePushRegistration ().execute (data );
101- ShadowApplication .runBackgroundTasks ();
102100
103101 ArgumentCaptor <IterableHelper .SuccessHandler > successHandlerCaptor = ArgumentCaptor .forClass (IterableHelper .SuccessHandler .class );
104102 verify (apiMock ).registerDeviceToken (eq (IterableTestUtils .userEmail ), isNull (String .class ), eq (INTEGRATION_NAME ), eq (NEW_TOKEN ), eq (IterableConstants .MESSAGING_PLATFORM_FIREBASE ));
@@ -108,6 +106,7 @@ public void testDisableOldGcmToken() throws Exception {
108106 reset (apiMock );
109107
110108 new IterablePushRegistration ().execute (data );
109+
111110 verify (apiMock ).registerDeviceToken (eq (IterableTestUtils .userEmail ), isNull (String .class ), eq (INTEGRATION_NAME ), eq (NEW_TOKEN ), eq (IterableConstants .MESSAGING_PLATFORM_FIREBASE ));
112111 verify (apiMock , never ()).disableToken (eq (IterableTestUtils .userEmail ), isNull (String .class ), any (String .class ), nullable (IterableHelper .SuccessHandler .class ), nullable (IterableHelper .FailureHandler .class ));
113112 }
0 commit comments