Skip to content

Commit 5b082ed

Browse files
emawbyJeasmine
authored andcommitted
unit tests for IAM end_time logic
1 parent 5de720f commit 5b082ed

File tree

2 files changed

+57
-1
lines changed

2 files changed

+57
-1
lines changed

OneSignalSDK/unittest/src/test/java/com/onesignal/InAppMessagingHelpers.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,17 @@ public static OSTestInAppMessage buildTestMessage(final JSONArray triggerJson) t
108108
return new OSTestInAppMessage(basicIAMJSONObject(triggerJson));
109109
}
110110

111+
public static OSTestInAppMessage buildTestMessageWithEndTime(final OSTriggerKind kind, final String key, final String operator, final Object value, final boolean pastEndTime) throws JSONException {
112+
JSONArray triggerJson = basicTrigger(kind, key, operator, value);
113+
JSONObject json = basicIAMJSONObject(triggerJson);
114+
if (pastEndTime) {
115+
json.put("end_time", "1960-01-01T00:00:00.000Z");
116+
} else {
117+
json.put("end_time", "2200-01-01T00:00:00.000Z");
118+
}
119+
return new OSTestInAppMessage(json);
120+
}
121+
111122
public static OSTestInAppMessage buildTestMessageWithMultipleTriggers(ArrayList<ArrayList<OSTestTrigger>> triggers) throws JSONException {
112123
JSONArray ors = buildTriggers(triggers);
113124
return buildTestMessage(ors);

OneSignalSDK/unittest/src/test/java/com/test/onesignal/InAppMessageIntegrationTests.java

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,46 @@ public void testDisableInAppMessagingPreventsMessageDisplay() throws Exception {
171171
assertFalse(OneSignalPackagePrivateHelper.isInAppMessageShowing());
172172
}
173173

174+
@Test
175+
public void testMessagesDoNotDisplayPastEndTime() throws Exception {
176+
final OSTestInAppMessage message = InAppMessagingHelpers.buildTestMessageWithEndTime(OSTriggerKind.CUSTOM, "test_key", OSTestTrigger.OSTriggerOperator.EQUAL_TO.toString(), 3, true);
177+
setMockRegistrationResponseWithMessages(new ArrayList<OSTestInAppMessage>() {{
178+
add(message);
179+
}});
180+
181+
// the SDK should read the message from registration JSON, set up a timer, and once
182+
// the timer fires the message should get shown.
183+
OneSignalInit();
184+
threadAndTaskWait();
185+
186+
// We will set the trigger. However, since the end time is in the past the message should not be shown
187+
OneSignal.addTrigger("test_key", 3);
188+
// Make no IAMs are in the display queue
189+
assertEquals(0, OneSignalPackagePrivateHelper.getInAppMessageDisplayQueue().size());
190+
// Make sure no IAM is showing
191+
assertFalse(OneSignalPackagePrivateHelper.isInAppMessageShowing());
192+
}
193+
194+
@Test
195+
public void testMessagesDoDisplayBeforeEndTime() throws Exception {
196+
final OSTestInAppMessage message = InAppMessagingHelpers.buildTestMessageWithEndTime(OSTriggerKind.CUSTOM, "test_key", OSTestTrigger.OSTriggerOperator.EQUAL_TO.toString(), 3, false);
197+
setMockRegistrationResponseWithMessages(new ArrayList<OSTestInAppMessage>() {{
198+
add(message);
199+
}});
200+
201+
// the SDK should read the message from registration JSON, set up a timer, and once
202+
// the timer fires the message should get shown.
203+
OneSignalInit();
204+
threadAndTaskWait();
205+
206+
// We will set the trigger. However, since the end time is in the past the message should not be shown
207+
OneSignal.addTrigger("test_key", 3);
208+
// Make no IAMs are in the display queue
209+
assertEquals(1, OneSignalPackagePrivateHelper.getInAppMessageDisplayQueue().size());
210+
// Make sure no IAM is showing
211+
assertTrue(OneSignalPackagePrivateHelper.isInAppMessageShowing());
212+
}
213+
174214
/**
175215
* Since it is possible for multiple in-app messages to be valid at the same time, we've implemented
176216
* a queue so that the SDK does not try to display both messages at the same time.
@@ -307,7 +347,12 @@ public void testTimedMessageIsDisplayedOncePerSession() throws Exception {
307347
Awaitility.await()
308348
.atMost(new Duration(150, TimeUnit.MILLISECONDS))
309349
.pollInterval(new Duration(10, TimeUnit.MILLISECONDS))
310-
.until(() -> OneSignalPackagePrivateHelper.getInAppMessageDisplayQueue().size() == 1);
350+
.until(new Callable<Boolean>() {
351+
@Override
352+
public Boolean call() throws Exception {
353+
return OneSignalPackagePrivateHelper.getInAppMessageDisplayQueue().size() == 1;
354+
}
355+
});
311356

312357
OneSignalPackagePrivateHelper.dismissCurrentMessage();
313358

0 commit comments

Comments
 (0)