From b15ea8aa20ead06e750f62dd8e1aef011aece4e9 Mon Sep 17 00:00:00 2001 From: Victor Babenko Date: Wed, 1 May 2019 18:18:02 -0700 Subject: [PATCH] Remove PowerMock from most tests --- .../iterableapi/IterablePushRegistration.java | 49 +++++++++++++++---- .../iterableapi/DeferredDeepLinkTest.java | 2 +- .../IterableActivityMonitorTest.java | 2 +- .../IterableInAppHTMLNotificationTest.java | 2 +- .../IterablePushRegistrationTest.java | 33 ++++++------- 5 files changed, 59 insertions(+), 29 deletions(-) diff --git a/iterableapi/src/main/java/com/iterable/iterableapi/IterablePushRegistration.java b/iterableapi/src/main/java/com/iterable/iterableapi/IterablePushRegistration.java index 29d06cba7..a6ebe989b 100644 --- a/iterableapi/src/main/java/com/iterable/iterableapi/IterablePushRegistration.java +++ b/iterableapi/src/main/java/com/iterable/iterableapi/IterablePushRegistration.java @@ -8,6 +8,8 @@ import org.json.JSONObject; +import java.io.IOException; + /** * Created by David Truong dt@iterable.com */ @@ -64,8 +66,7 @@ PushRegistrationObject getDeviceToken() { return null; } - FirebaseInstanceId instanceID = FirebaseInstanceId.getInstance(); - return new PushRegistrationObject(instanceID.getToken()); + return new PushRegistrationObject(Util.getFirebaseToken()); } catch (Exception e) { IterableLogger.e(TAG, "Exception while retrieving the device token: check that firebase is added to the build dependencies", e); @@ -85,7 +86,7 @@ private void disableOldDeviceIfNeeded() { final SharedPreferences sharedPref = applicationContext.getSharedPreferences(IterableConstants.PUSH_APP_ID, Context.MODE_PRIVATE); boolean migrationDone = sharedPref.getBoolean(IterableConstants.SHARED_PREFS_FCM_MIGRATION_DONE_KEY, false); if (!migrationDone) { - String oldToken = FirebaseInstanceId.getInstance().getToken(gcmSenderId, IterableConstants.MESSAGING_PLATFORM_GOOGLE); + String oldToken = Util.getFirebaseToken(gcmSenderId, IterableConstants.MESSAGING_PLATFORM_GOOGLE); // We disable the device on Iterable but keep the token if (oldToken != null) { @@ -104,16 +105,46 @@ public void onSuccess(JSONObject data) { } static class Util { + static UtilImpl instance = new UtilImpl(); + static int getFirebaseResouceId(Context applicationContext) { - return applicationContext.getResources().getIdentifier(IterableConstants.FIREBASE_RESOURCE_ID, IterableConstants.ANDROID_STRING, applicationContext.getPackageName()); + return instance.getFirebaseResouceId(applicationContext); + } + + static String getFirebaseToken() { + return instance.getFirebaseToken(); + } + + static String getFirebaseToken(String senderId, String platform) throws IOException { + return instance.getFirebaseToken(senderId, platform); } static String getSenderId(Context applicationContext) { - int resId = applicationContext.getResources().getIdentifier(IterableConstants.FIREBASE_SENDER_ID, IterableConstants.ANDROID_STRING, applicationContext.getPackageName()); - if (resId != 0) { - return applicationContext.getResources().getString(resId); - } else { - return null; + return instance.getSenderId(applicationContext); + } + + static class UtilImpl { + int getFirebaseResouceId(Context applicationContext) { + return applicationContext.getResources().getIdentifier(IterableConstants.FIREBASE_RESOURCE_ID, IterableConstants.ANDROID_STRING, applicationContext.getPackageName()); + } + + String getFirebaseToken() { + FirebaseInstanceId instanceID = FirebaseInstanceId.getInstance(); + return instanceID.getToken(); + } + + String getFirebaseToken(String senderId, String platform) throws IOException { + FirebaseInstanceId instanceId = FirebaseInstanceId.getInstance(); + return instanceId.getToken(senderId, platform); + } + + String getSenderId(Context applicationContext) { + int resId = applicationContext.getResources().getIdentifier(IterableConstants.FIREBASE_SENDER_ID, IterableConstants.ANDROID_STRING, applicationContext.getPackageName()); + if (resId != 0) { + return applicationContext.getResources().getString(resId); + } else { + return null; + } } } } diff --git a/iterableapi/src/test/java/com/iterable/iterableapi/DeferredDeepLinkTest.java b/iterableapi/src/test/java/com/iterable/iterableapi/DeferredDeepLinkTest.java index 6594612e4..69afd9ac6 100644 --- a/iterableapi/src/test/java/com/iterable/iterableapi/DeferredDeepLinkTest.java +++ b/iterableapi/src/test/java/com/iterable/iterableapi/DeferredDeepLinkTest.java @@ -23,7 +23,7 @@ import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; -public class DeferredDeepLinkTest extends BasePowerMockTest { +public class DeferredDeepLinkTest extends BaseTest { private MockWebServer server; diff --git a/iterableapi/src/test/java/com/iterable/iterableapi/IterableActivityMonitorTest.java b/iterableapi/src/test/java/com/iterable/iterableapi/IterableActivityMonitorTest.java index 5ec487fb8..e19f40dfa 100644 --- a/iterableapi/src/test/java/com/iterable/iterableapi/IterableActivityMonitorTest.java +++ b/iterableapi/src/test/java/com/iterable/iterableapi/IterableActivityMonitorTest.java @@ -16,7 +16,7 @@ import static org.mockito.Mockito.reset; import static org.mockito.Mockito.verify; -public class IterableActivityMonitorTest extends BasePowerMockTest { +public class IterableActivityMonitorTest extends BaseTest { @Before public void setUp() { diff --git a/iterableapi/src/test/java/com/iterable/iterableapi/IterableInAppHTMLNotificationTest.java b/iterableapi/src/test/java/com/iterable/iterableapi/IterableInAppHTMLNotificationTest.java index 74dac4fbe..290360cbd 100644 --- a/iterableapi/src/test/java/com/iterable/iterableapi/IterableInAppHTMLNotificationTest.java +++ b/iterableapi/src/test/java/com/iterable/iterableapi/IterableInAppHTMLNotificationTest.java @@ -7,7 +7,7 @@ import org.robolectric.Robolectric; import org.robolectric.android.controller.ActivityController; -public class IterableInAppHTMLNotificationTest extends BasePowerMockTest { +public class IterableInAppHTMLNotificationTest extends BaseTest { @Test public void testDoNotCrashOnResizeAfterDismiss() { diff --git a/iterableapi/src/test/java/com/iterable/iterableapi/IterablePushRegistrationTest.java b/iterableapi/src/test/java/com/iterable/iterableapi/IterablePushRegistrationTest.java index 3bb994a11..57c41e4ac 100644 --- a/iterableapi/src/test/java/com/iterable/iterableapi/IterablePushRegistrationTest.java +++ b/iterableapi/src/test/java/com/iterable/iterableapi/IterablePushRegistrationTest.java @@ -1,15 +1,13 @@ package com.iterable.iterableapi; -import com.google.firebase.iid.FirebaseInstanceId; +import android.content.Context; import org.json.JSONObject; import org.junit.After; import org.junit.Before; import org.junit.Test; import org.mockito.ArgumentCaptor; -import org.powermock.api.mockito.PowerMockito; -import org.powermock.core.MockRepository; -import org.powermock.core.classloader.annotations.PrepareForTest; +import org.robolectric.Robolectric; import org.robolectric.RuntimeEnvironment; import org.robolectric.shadows.ShadowApplication; @@ -28,8 +26,7 @@ import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; -@PrepareForTest({IterablePushRegistration.Util.class, FirebaseInstanceId.class}) -public class IterablePushRegistrationTest extends BasePowerMockTest { +public class IterablePushRegistrationTest extends BaseTest { private static final String TEST_TOKEN = "testToken"; private static final String NEW_TOKEN = "newToken"; @@ -40,7 +37,8 @@ public class IterablePushRegistrationTest extends BasePowerMockTest { private MockWebServer server; private IterableApi originalApi; private IterableApi apiMock; - private FirebaseInstanceId mockInstanceId; + private IterablePushRegistration.Util.UtilImpl originalPushRegistrationUtil; + private IterablePushRegistration.Util.UtilImpl pushRegistrationUtilMock; @Before public void setUp() throws Exception { @@ -52,16 +50,17 @@ public void setUp() throws Exception { apiMock = spy(IterableApi.sharedInstance); IterableApi.sharedInstance = apiMock; - mockInstanceId = mock(FirebaseInstanceId.class); - PowerMockito.stub(PowerMockito.method(FirebaseInstanceId.class, "getInstance")).toReturn(mockInstanceId); - PowerMockito.stub(PowerMockito.method(IterablePushRegistration.Util.class, "getFirebaseResouceId")).toReturn(1); + originalPushRegistrationUtil = IterablePushRegistration.Util.instance; + pushRegistrationUtilMock = mock(IterablePushRegistration.Util.UtilImpl.class); + IterablePushRegistration.Util.instance = pushRegistrationUtilMock; + + when(pushRegistrationUtilMock.getFirebaseResouceId(any(Context.class))).thenReturn(1); } @After public void tearDown() throws Exception { + IterablePushRegistration.Util.instance = originalPushRegistrationUtil; IterableApi.sharedInstance = originalApi; - MockRepository.remove(FirebaseInstanceId.class); - MockRepository.remove(IterablePushRegistration.Util.class); server.shutdown(); server = null; @@ -69,7 +68,7 @@ public void tearDown() throws Exception { @Test public void testEnableDevice() throws Exception { - when(mockInstanceId.getToken()).thenReturn(TEST_TOKEN); + when(pushRegistrationUtilMock.getFirebaseToken()).thenReturn(TEST_TOKEN); IterablePushRegistrationData data = new IterablePushRegistrationData(IterableTestUtils.userEmail, null, INTEGRATION_NAME, IterablePushRegistrationData.PushRegistrationAction.ENABLE); new IterablePushRegistration().execute(data); @@ -80,7 +79,7 @@ public void testEnableDevice() throws Exception { @Test public void testDisableDevice() throws Exception { - when(mockInstanceId.getToken()).thenReturn("testToken"); + when(pushRegistrationUtilMock.getFirebaseToken()).thenReturn("testToken"); IterablePushRegistrationData data = new IterablePushRegistrationData(IterableTestUtils.userEmail, null, INTEGRATION_NAME, IterablePushRegistrationData.PushRegistrationAction.DISABLE); new IterablePushRegistration().execute(data); @@ -93,12 +92,11 @@ public void testDisableDevice() throws Exception { public void testDisableOldGcmToken() throws Exception { IterableApi.initialize(RuntimeEnvironment.application, "apiKey", new IterableConfig.Builder().setLegacyGCMSenderId(GCM_SENDER_ID).build()); - when(mockInstanceId.getToken()).thenReturn(NEW_TOKEN); - when(mockInstanceId.getToken(eq(GCM_SENDER_ID), eq(IterableConstants.MESSAGING_PLATFORM_GOOGLE))).thenReturn(OLD_TOKEN); + when(pushRegistrationUtilMock.getFirebaseToken()).thenReturn(NEW_TOKEN); + when(pushRegistrationUtilMock.getFirebaseToken(eq(GCM_SENDER_ID), eq(IterableConstants.MESSAGING_PLATFORM_GOOGLE))).thenReturn(OLD_TOKEN); IterablePushRegistrationData data = new IterablePushRegistrationData(IterableTestUtils.userEmail, null, INTEGRATION_NAME, IterablePushRegistrationData.PushRegistrationAction.ENABLE); new IterablePushRegistration().execute(data); - ShadowApplication.runBackgroundTasks(); ArgumentCaptor successHandlerCaptor = ArgumentCaptor.forClass(IterableHelper.SuccessHandler.class); 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 { reset(apiMock); new IterablePushRegistration().execute(data); + verify(apiMock).registerDeviceToken(eq(IterableTestUtils.userEmail), isNull(String.class), eq(INTEGRATION_NAME), eq(NEW_TOKEN), eq(IterableConstants.MESSAGING_PLATFORM_FIREBASE)); verify(apiMock, never()).disableToken(eq(IterableTestUtils.userEmail), isNull(String.class), any(String.class), nullable(IterableHelper.SuccessHandler.class), nullable(IterableHelper.FailureHandler.class)); }