Skip to content

Commit 9450fdd

Browse files
committed
[:iterableapi] Replace usages of RuntimeEnvironment.application with ApplicationProvider.getApplicationContext()
1 parent bfabf45 commit 9450fdd

File tree

2 files changed

+15
-11
lines changed

2 files changed

+15
-11
lines changed

iterableapi/src/test/java/com/iterable/iterableapi/IterablePushActionReceiverTest.java

+11-8
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
package com.iterable.iterableapi;
22

3+
import android.app.Application;
34
import android.content.ClipData;
45
import android.content.Context;
56
import android.content.Intent;
67
import android.os.Bundle;
78
import androidx.core.app.RemoteInput;
9+
import androidx.test.core.app.ApplicationProvider;
810

911
import org.json.JSONObject;
1012
import org.junit.After;
1113
import org.junit.Before;
1214
import org.junit.Test;
1315
import org.mockito.ArgumentCaptor;
14-
import org.robolectric.RuntimeEnvironment;
1516

1617
import java.util.concurrent.TimeUnit;
1718

@@ -63,11 +64,12 @@ public void testPushOpenWithNonInitializedSDK() throws Exception {
6364
intent.putExtra(IterableConstants.ITERABLE_DATA_KEY, IterableTestUtils.getResourceString("push_payload_silent_action.json"));
6465

6566
// This must not crash
66-
iterablePushActionReceiver.onReceive(RuntimeEnvironment.application, intent);
67+
iterablePushActionReceiver.onReceive(ApplicationProvider.getApplicationContext(), intent);
6768
}
6869

6970
@Test
7071
public void testTrackPushOpenWithCustomAction() throws Exception {
72+
Application application = ApplicationProvider.getApplicationContext();
7173
final JSONObject responseData = new JSONObject("{\"key\":\"value\"}");
7274
stubAnyRequestReturningStatusCode(server, 200, responseData);
7375

@@ -76,15 +78,15 @@ public void testTrackPushOpenWithCustomAction() throws Exception {
7678
intent.putExtra(IterableConstants.ITERABLE_DATA_ACTION_IDENTIFIER, IterableConstants.ITERABLE_ACTION_DEFAULT);
7779
intent.putExtra(IterableConstants.ITERABLE_DATA_KEY, IterableTestUtils.getResourceString("push_payload_custom_action.json"));
7880

79-
iterablePushActionReceiver.onReceive(RuntimeEnvironment.application, intent);
81+
iterablePushActionReceiver.onReceive(application, intent);
8082

8183
// Verify that IterableActionRunner was called with the proper action
8284
ArgumentCaptor<IterableAction> capturedAction = ArgumentCaptor.forClass(IterableAction.class);
8385
verify(actionRunnerMock).executeAction(any(Context.class), capturedAction.capture(), eq(IterableActionSource.PUSH));
8486
assertEquals("customAction", capturedAction.getValue().getType());
8587

8688
// Verify that the main app activity was launched
87-
Intent activityIntent = shadowOf(RuntimeEnvironment.application).peekNextStartedActivity();
89+
Intent activityIntent = shadowOf(application).peekNextStartedActivity();
8890
assertNotNull(activityIntent);
8991
assertEquals(Intent.ACTION_MAIN, activityIntent.getAction());
9092

@@ -102,16 +104,17 @@ public void testTrackPushOpenWithCustomAction() throws Exception {
102104

103105
@Test
104106
public void testPushActionWithSilentAction() throws Exception {
107+
Application application = ApplicationProvider.getApplicationContext();
105108
stubAnyRequestReturningStatusCode(server, 200, "{}");
106109
IterablePushActionReceiver iterablePushActionReceiver = new IterablePushActionReceiver();
107110
Intent intent = new Intent(IterableConstants.ACTION_PUSH_ACTION);
108111
intent.putExtra(IterableConstants.ITERABLE_DATA_ACTION_IDENTIFIER, "silentButton");
109112
intent.putExtra(IterableConstants.ITERABLE_DATA_KEY, IterableTestUtils.getResourceString("push_payload_silent_action.json"));
110113

111-
iterablePushActionReceiver.onReceive(RuntimeEnvironment.application, intent);
114+
iterablePushActionReceiver.onReceive(application, intent);
112115

113116
// Verify that the main app activity was NOT launched
114-
Intent activityIntent = shadowOf(RuntimeEnvironment.application).peekNextStartedActivity();
117+
Intent activityIntent = shadowOf(application).peekNextStartedActivity();
115118
assertNull(activityIntent);
116119
}
117120

@@ -129,7 +132,7 @@ public void testPushActionWithTextInput() throws Exception {
129132
clipDataIntent.putExtra(RemoteInput.EXTRA_RESULTS_DATA, resultsBundle);
130133
intent.setClipData(ClipData.newIntent(RESULTS_CLIP_LABEL, clipDataIntent));
131134

132-
iterablePushActionReceiver.onReceive(RuntimeEnvironment.application, intent);
135+
iterablePushActionReceiver.onReceive(ApplicationProvider.getApplicationContext(), intent);
133136

134137
// Verify that IterableActionRunner was called with the proper action
135138
ArgumentCaptor<IterableAction> actionCaptor = ArgumentCaptor.forClass(IterableAction.class);
@@ -147,7 +150,7 @@ public void testLegacyDeepLinkPayload() throws Exception {
147150
intent.putExtras(IterableTestUtils.getBundleFromJsonResource("push_payload_legacy_deep_link.json"));
148151
intent.putExtra(IterableConstants.ITERABLE_DATA_ACTION_IDENTIFIER, IterableConstants.ITERABLE_ACTION_DEFAULT);
149152

150-
iterablePushActionReceiver.onReceive(RuntimeEnvironment.application, intent);
153+
iterablePushActionReceiver.onReceive(ApplicationProvider.getApplicationContext(), intent);
151154

152155
// Verify that IterableActionRunner was called with openUrl action
153156
ArgumentCaptor<IterableAction> capturedAction = ArgumentCaptor.forClass(IterableAction.class);

iterableapi/src/test/java/com/iterable/iterableapi/IterableTestUtils.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22

33
import android.os.Bundle;
44

5+
import androidx.test.core.app.ApplicationProvider;
6+
57
import org.json.JSONException;
68
import org.json.JSONObject;
7-
import org.robolectric.RuntimeEnvironment;
89

910
import java.io.BufferedReader;
1011
import java.io.IOException;
@@ -48,7 +49,7 @@ public static void createIterableApiNew(ConfigBuilderExtender extender, String e
4849
builder = extender.run(builder);
4950
}
5051

51-
IterableApi.initialize(RuntimeEnvironment.application, apiKey, builder.build());
52+
IterableApi.initialize(ApplicationProvider.getApplicationContext(), apiKey, builder.build());
5253
IterableApi.getInstance().setEmail(email);
5354
}
5455

@@ -60,7 +61,7 @@ public static String getResourceString(String fileName) throws IOException {
6061
InputStream inputStream = IterableTestUtils.class.getClassLoader().getResourceAsStream(fileName);
6162
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
6263
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
63-
String receiveString = "";
64+
String receiveString;
6465
StringBuilder stringBuilder = new StringBuilder();
6566

6667
while ((receiveString = bufferedReader.readLine()) != null) {

0 commit comments

Comments
 (0)