Skip to content

Commit

Permalink
Merge pull request #100 from Iterable/chore/MOB-372-remove-powermock
Browse files Browse the repository at this point in the history
[MOB-372] Remove PowerMock from most tests
  • Loading branch information
vbabenkoru authored May 6, 2019
2 parents 0375226 + b15ea8a commit 4e8effc
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

import org.json.JSONObject;

import java.io.IOException;

/**
* Created by David Truong [email protected]
*/
Expand Down Expand Up @@ -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);
Expand All @@ -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) {
Expand All @@ -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;
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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";
Expand All @@ -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 {
Expand All @@ -52,24 +50,25 @@ 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;
}

@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);
Expand All @@ -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);
Expand All @@ -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<IterableHelper.SuccessHandler> 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));
Expand All @@ -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));
}
Expand Down

0 comments on commit 4e8effc

Please sign in to comment.