Skip to content

Commit d70fbc0

Browse files
mikechochjkasten2
authored andcommitted
Fixed all unit tests to use advanceSystemTimeBy() method
* Previously using SystemClock.setCurrentTimeMillis() or ShadowSystemClock.setCurrentTimeMillis(), which did not append time. This caused issues with advancing on_sessions and on_focus events and other timing events * advanceSystemTimeBy() simply appends time in millis on top of the current time * Also added a resetSystemClock() method for before each test
1 parent 84662bd commit d70fbc0

File tree

6 files changed

+72
-80
lines changed

6 files changed

+72
-80
lines changed

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

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@
106106
import static com.onesignal.OneSignalPackagePrivateHelper.NotificationOpenedProcessor_processFromContext;
107107
import static com.onesignal.OneSignalPackagePrivateHelper.NotificationSummaryManager_updateSummaryNotificationAfterChildRemoved;
108108
import static com.onesignal.OneSignalPackagePrivateHelper.createInternalPayloadBundle;
109-
import static com.test.onesignal.TestHelpers.advanceTimeByMs;
109+
import static com.test.onesignal.TestHelpers.advanceSystemTimeBy;
110110
import static com.test.onesignal.TestHelpers.threadAndTaskWait;
111111
import static junit.framework.Assert.assertEquals;
112112
import static junit.framework.Assert.assertFalse;
@@ -147,9 +147,6 @@ public static void setUpClass() throws Exception {
147147

148148
@Before // Before each test
149149
public void beforeEachTest() throws Exception {
150-
// Robolectric mocks System.currentTimeMillis() to 0, we need the current real time to match our SQL records.
151-
SystemClock.setCurrentTimeMillis(System.currentTimeMillis());
152-
153150
blankActivityController = Robolectric.buildActivity(BlankActivity.class).create();
154151
blankActivity = blankActivityController.get();
155152
blankActivity.getApplicationInfo().name = "UnitTestApp";
@@ -164,7 +161,7 @@ public void beforeEachTest() throws Exception {
164161
notificationManager.cancelAll();
165162
NotificationRestorer.restored = false;
166163
}
167-
164+
168165
@AfterClass
169166
public static void afterEverything() throws Exception {
170167
StaticResetHelper.restSetStaticFields();
@@ -259,7 +256,7 @@ public void notificationOpened(OSNotificationOpenResult result) {
259256
NotificationBundleProcessor_ProcessFromGCMIntentService(blankActivity, bundle, null);
260257

261258
// Go forward 4 weeks
262-
SystemClock.setCurrentTimeMillis(System.currentTimeMillis() + 2419202L * 1000L);
259+
advanceSystemTimeBy(2_419_202);
263260

264261
// Display a 3 normal notification.
265262
NotificationBundleProcessor_ProcessFromGCMIntentService(blankActivity, getBaseNotifBundle("UUID3"), null);
@@ -805,7 +802,7 @@ public void shouldHandleBasicNotifications() {
805802

806803
// Go forward 4 weeks
807804
// Note: Does not effect the SQL function strftime
808-
SystemClock.setCurrentTimeMillis(System.currentTimeMillis() + 2419201L * 1000L);
805+
advanceSystemTimeBy(2_419_202);
809806

810807
// Display a 3rd notification
811808
// Should of been added for a total of 2 records now.
@@ -832,7 +829,7 @@ public void shouldRestoreNotifications() {
832829

833830
// Go forward 1 week
834831
// Note: Does not effect the SQL function strftime
835-
SystemClock.setCurrentTimeMillis(System.currentTimeMillis() + 604801L * 1000L);
832+
advanceSystemTimeBy(604_801);
836833

837834
// Restorer should not fire service since the notification is over 1 week old.
838835
NotificationRestorer.restore(blankActivity); NotificationRestorer.restored = false;
@@ -863,7 +860,7 @@ private void helperShouldRestoreNotificationsPastExpireTime(boolean should) {
863860
assertRestoreRan();
864861

865862
// Go forward just past the TTL of the notification
866-
advanceTimeByMs((ttl + 1) * 1_000L);
863+
advanceSystemTimeBy(ttl + 1);
867864
restoreNotifications();
868865
if (should)
869866
assertRestoreRan();
@@ -887,7 +884,7 @@ public void badgeCountShouldNotIncludeOldNotifications() {
887884
NotificationBundleProcessor_ProcessFromGCMIntentService(blankActivity, getBaseNotifBundle(), null);
888885

889886
// Go forward 1 week
890-
SystemClock.setCurrentTimeMillis(System.currentTimeMillis() + 604801 * 1000);
887+
advanceSystemTimeBy(604_801);
891888

892889
// Should not count as a badge
893890
SQLiteDatabase readableDb = OneSignalDbHelper.getInstance(blankActivity).getReadableDatabase();

0 commit comments

Comments
 (0)