Skip to content

Commit 5c8a029

Browse files
authored
Merge pull request #1345 from OneSignal/fix/flaky_tests_2021_05_22
Fix flaky tests 2021-05-22
2 parents cdb9e15 + b696903 commit 5c8a029

File tree

7 files changed

+70
-23
lines changed

7 files changed

+70
-23
lines changed

OneSignalSDK/onesignal/src/main/java/com/onesignal/OneSignal.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -404,15 +404,15 @@ public static String getSdkVersionRaw() {
404404
}
405405

406406
private static OSLogger logger = new OSLogWrapper();
407-
private static FocusTimeController focusTimeController = new FocusTimeController(new OSFocusTimeProcessorFactory(), logger);
407+
private static FocusTimeController focusTimeController;
408408
private static OSSessionManager.SessionListener sessionListener = new OSSessionManager.SessionListener() {
409409
@Override
410410
public void onSessionEnding(@NonNull List<OSInfluence> lastInfluences) {
411411
if (outcomeEventsController == null)
412412
OneSignal.Log(LOG_LEVEL.WARN, "OneSignal onSessionEnding called before init!");
413413
if (outcomeEventsController != null)
414414
outcomeEventsController.cleanOutcomes();
415-
focusTimeController.onSessionEnded(lastInfluences);
415+
getFocusTimeController().onSessionEnded(lastInfluences);
416416
}
417417
};
418418

@@ -891,7 +891,7 @@ private static void handleActivityLifecycleHandler(Context context) {
891891
activityLifecycleHandler.setNextResumeIsFirstActivity(true);
892892
}
893893
OSNotificationRestoreWorkManager.beginEnqueueingWork(context, false);
894-
focusTimeController.appForegrounded();
894+
getFocusTimeController().appForegrounded();
895895
} else if (activityLifecycleHandler != null) {
896896
activityLifecycleHandler.setNextResumeIsFirstActivity(true);
897897
}
@@ -1289,7 +1289,7 @@ static void backgroundSyncLogic() {
12891289
if (trackAmazonPurchase != null)
12901290
trackAmazonPurchase.checkListener();
12911291

1292-
focusTimeController.appBackgrounded();
1292+
getFocusTimeController().appBackgrounded();
12931293

12941294
scheduleSyncService();
12951295
}
@@ -1337,7 +1337,7 @@ private static void onAppFocusLogic() {
13371337
if (shouldLogUserPrivacyConsentErrorMessageForMethodName("onAppFocus"))
13381338
return;
13391339

1340-
focusTimeController.appForegrounded();
1340+
getFocusTimeController().appForegrounded();
13411341

13421342
doSessionInit();
13431343

@@ -3172,6 +3172,10 @@ static OSTaskController getTaskController() {
31723172
}
31733173

31743174
static FocusTimeController getFocusTimeController() {
3175+
if (focusTimeController == null) {
3176+
focusTimeController = new FocusTimeController(new OSFocusTimeProcessorFactory(), logger);
3177+
}
3178+
31753179
return focusTimeController;
31763180
}
31773181
/*

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import android.content.Context;
55
import android.content.Intent;
66
import android.os.Bundle;
7+
import android.util.Log;
78

89
import androidx.annotation.NonNull;
910
import androidx.annotation.Nullable;
@@ -30,6 +31,8 @@ public class OneSignalPackagePrivateHelper {
3031

3132
public static final long MIN_ON_SESSION_TIME_MILLIS = com.onesignal.OneSignal.MIN_ON_SESSION_TIME_MILLIS;
3233

34+
private static final String LOGCAT_TAG = "OS_PACKAGE_HELPER";
35+
3336
private static abstract class RunnableArg<T> {
3437
abstract void run(T object) throws Exception;
3538
}
@@ -513,8 +516,11 @@ public OSTestInAppMessageAction(JSONObject json) throws JSONException {
513516

514517
public static void dismissCurrentMessage() {
515518
com.onesignal.OSInAppMessage message = OneSignal.getInAppMessageController().getCurrentDisplayedInAppMessage();
516-
if (message != null)
519+
if (message != null) {
517520
OneSignal.getInAppMessageController().messageWasDismissed(message);
521+
} else {
522+
Log.e(LOGCAT_TAG, "No currently displaying IAM to dismiss!");
523+
}
518524
}
519525

520526
public static boolean isInAppMessageShowing() {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ private static void mockPost(String url, JSONObject jsonBody, OneSignalRestClien
311311

312312
String retJson;
313313

314-
if (nextSuccessfulRegistrationResponse != null) {
314+
if (nextSuccessfulRegistrationResponse != null && url.contains("players")) {
315315
retJson = nextSuccessfulRegistrationResponse;
316316
nextSuccessfulRegistrationResponse = null;
317317
}

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ public static void load() {
5959
}
6060
return false;
6161
}));
62-
classes.add(new ClassState(FocusTimeController.class, null));
6362
classes.add(new ClassState(OSSessionManager.class, null));
6463
classes.add(new ClassState(MockSessionManager.class, null));
6564
classes.add(new ClassState(OSNotificationWorkManager.class, field -> {

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -719,9 +719,10 @@ private void runImplicitServices() throws Exception {
719719

720720
@Test
721721
@Config(shadows = { ShadowGenerateNotification.class })
722-
public void shouldUpdateBadgesWhenDismissingNotification() {
722+
public void shouldUpdateBadgesWhenDismissingNotification() throws Exception {
723723
Bundle bundle = getBaseNotifBundle();
724724
NotificationBundleProcessor_ProcessFromFCMIntentService(blankActivity, bundle);
725+
threadAndTaskWait();
725726
assertEquals(notifMessage, ShadowRoboNotificationManager.getLastShadowNotif().getContentText());
726727
assertEquals(1, ShadowBadgeCountUpdater.lastCount);
727728

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

Lines changed: 51 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
import static junit.framework.Assert.assertEquals;
7676
import static junit.framework.Assert.assertFalse;
7777
import static junit.framework.Assert.assertTrue;
78+
import static org.junit.Assert.fail;
7879

7980
@Config(packageName = "com.onesignal.example",
8081
shadows = {
@@ -149,9 +150,8 @@ public void afterEachTest() throws Exception {
149150
ShadowDynamicTimer.shouldScheduleTimers = true;
150151
ShadowDynamicTimer.hasScheduledTimer = false;
151152
OneSignal.setInAppMessageClickHandler(null);
152-
TestHelpers.afterTestCleanup();
153-
154153
InAppMessagingHelpers.clearTestState();
154+
TestHelpers.afterTestCleanup();
155155
}
156156

157157
@Test
@@ -363,14 +363,28 @@ public void testTimedMessageIsDisplayedOncePerSession() throws Exception {
363363
// for the correct amount of time, so all we are doing here is checking to
364364
// make sure the message actually gets displayed once the timer fires
365365
Awaitility.await()
366-
.atMost(new Duration(150, TimeUnit.MILLISECONDS))
366+
.atMost(new Duration(1_000, TimeUnit.MILLISECONDS))
367367
.pollInterval(new Duration(10, TimeUnit.MILLISECONDS))
368368
.until(() -> OneSignalPackagePrivateHelper.getInAppMessageDisplayQueue().size() == 1);
369369

370+
// After IAM is added to display queue we now need to wait until it is shown
371+
Awaitility.await()
372+
.atMost(new Duration(1_000, TimeUnit.MILLISECONDS))
373+
.pollInterval(new Duration(10, TimeUnit.MILLISECONDS))
374+
.untilAsserted(new ThrowingRunnable() {
375+
@Override
376+
public void run() {
377+
assertTrue(OneSignalPackagePrivateHelper.isInAppMessageShowing());
378+
}
379+
});
380+
370381
OneSignalPackagePrivateHelper.dismissCurrentMessage();
371382

372383
// Check that the IAM is not displayed again
373-
assertEquals(0, OneSignalPackagePrivateHelper.getInAppMessageDisplayQueue().size());
384+
Awaitility.await()
385+
.atMost(new Duration(1_000, TimeUnit.MILLISECONDS))
386+
.pollInterval(new Duration(10, TimeUnit.MILLISECONDS))
387+
.until(() -> OneSignalPackagePrivateHelper.getInAppMessageDisplayQueue().size() == 0);
374388
}
375389

376390
@Test
@@ -410,7 +424,12 @@ public void testAfterLastInAppTimeIsDisplayedOncePerSession() throws Exception {
410424
.pollInterval(new Duration(10, TimeUnit.MILLISECONDS))
411425
.untilAsserted(() -> {
412426
assertEquals(1, OneSignalPackagePrivateHelper.getInAppMessageDisplayQueue().size());
413-
assertEquals(message1.messageId, OneSignalPackagePrivateHelper.getShowingInAppMessageId());
427+
try {
428+
assertEquals(message1.messageId, OneSignalPackagePrivateHelper.getShowingInAppMessageId());
429+
} catch (NullPointerException e) {
430+
// Awaitility won't retry if something is thrown, but will if an assert fails.
431+
fail("Should not throw");
432+
}
414433
});
415434

416435
OneSignalPackagePrivateHelper.dismissCurrentMessage();
@@ -421,7 +440,12 @@ public void testAfterLastInAppTimeIsDisplayedOncePerSession() throws Exception {
421440
.pollInterval(new Duration(100, TimeUnit.MILLISECONDS))
422441
.untilAsserted(() -> {
423442
assertEquals(1, OneSignalPackagePrivateHelper.getInAppMessageDisplayQueue().size());
424-
assertEquals(message2.messageId, OneSignalPackagePrivateHelper.getShowingInAppMessageId());
443+
try {
444+
assertEquals(message2.messageId, OneSignalPackagePrivateHelper.getShowingInAppMessageId());
445+
} catch (NullPointerException e) {
446+
// Awaitility won't retry if something is thrown, but will if an assert fails.
447+
fail("Should not throw");
448+
}
425449
});
426450

427451

@@ -1330,7 +1354,26 @@ public void testInAppMessageDisplayMultipleTimes_sessionDurationTrigger() throws
13301354
threadAndTaskWait();
13311355

13321356
// No schedule should happen, IAM should evaluate to true
1333-
assertEquals(1, OneSignalPackagePrivateHelper.getInAppMessageDisplayQueue().size());
1357+
Awaitility.await()
1358+
.atMost(new Duration(1_000, TimeUnit.MILLISECONDS))
1359+
.pollInterval(new Duration(10, TimeUnit.MILLISECONDS))
1360+
.untilAsserted(new ThrowingRunnable() {
1361+
@Override
1362+
public void run() throws Exception {
1363+
assertEquals(1, OneSignalPackagePrivateHelper.getInAppMessageDisplayQueue().size());
1364+
}
1365+
});
1366+
1367+
// After IAM is added to display queue we now need to wait until it is shown
1368+
Awaitility.await()
1369+
.atMost(new Duration(1_000, TimeUnit.MILLISECONDS))
1370+
.pollInterval(new Duration(10, TimeUnit.MILLISECONDS))
1371+
.untilAsserted(new ThrowingRunnable() {
1372+
@Override
1373+
public void run() throws Exception {
1374+
assertTrue(OneSignalPackagePrivateHelper.isInAppMessageShowing());
1375+
}
1376+
});
13341377

13351378
// Dismiss IAM will make display quantity increase and last display time to change
13361379
OneSignalPackagePrivateHelper.dismissCurrentMessage();
@@ -1357,7 +1400,7 @@ public void testInAppMessageDisplayMultipleTimes_sessionDurationTrigger() throws
13571400
// No schedule should happen since session time period is very small, should evaluate to true on first run
13581401
// Wait for redisplay logic
13591402
Awaitility.await()
1360-
.atMost(new Duration(150, TimeUnit.MILLISECONDS))
1403+
.atMost(new Duration(1_000, TimeUnit.MILLISECONDS))
13611404
.pollInterval(new Duration(10, TimeUnit.MILLISECONDS))
13621405
.untilAsserted(new ThrowingRunnable() {
13631406
@Override

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

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,6 @@ public void testLocationSchedule() throws Exception {
233233
assertEquals(3.3f, ShadowOneSignalRestClient.lastPost.opt("loc_acc"));
234234

235235
assertEquals(false, ShadowOneSignalRestClient.lastPost.opt("loc_bg"));
236-
assertEquals("11111111-2222-3333-4444-555555555555", ShadowOneSignalRestClient.lastPost.opt("ad_id"));
237236

238237
// Testing loc_bg
239238
blankActivityController.pause();
@@ -249,7 +248,6 @@ public void testLocationSchedule() throws Exception {
249248
assertEquals(3.3f, ShadowOneSignalRestClient.lastPost.opt("loc_acc"));
250249
assertEquals(true, ShadowOneSignalRestClient.lastPost.opt("loc_bg"));
251250
assertEquals(1, ShadowOneSignalRestClient.lastPost.optInt("loc_type"));
252-
assertEquals("11111111-2222-3333-4444-555555555555", ShadowOneSignalRestClient.lastPost.opt("ad_id"));
253251
}
254252

255253
@Test
@@ -449,8 +447,6 @@ public void testLocationSchedule_Huawei() throws Exception {
449447
assertEquals(3.3f, ShadowOneSignalRestClient.lastPost.opt("loc_acc"));
450448

451449
assertEquals(false, ShadowOneSignalRestClient.lastPost.opt("loc_bg"));
452-
// Currently not getting ad_id for HMS devices
453-
assertNull(ShadowOneSignalRestClient.lastPost.opt("ad_id"));
454450

455451
// Testing loc_bg
456452
blankActivityController.pause();
@@ -467,8 +463,6 @@ public void testLocationSchedule_Huawei() throws Exception {
467463
assertEquals(3.3f, ShadowOneSignalRestClient.lastPost.opt("loc_acc"));
468464
assertEquals(true, ShadowOneSignalRestClient.lastPost.opt("loc_bg"));
469465
assertEquals(1, ShadowOneSignalRestClient.lastPost.optInt("loc_type"));
470-
// Currently not getting ad_id for HMS devices
471-
assertNull(ShadowOneSignalRestClient.lastPost.opt("ad_id"));
472466
}
473467

474468
@Test

0 commit comments

Comments
 (0)