Skip to content

Commit 049f712

Browse files
committed
Updated tests for restore notification change
1 parent 1b8eb37 commit 049f712

File tree

2 files changed

+32
-17
lines changed

2 files changed

+32
-17
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,5 @@ public static boolean GcmBroadcastReceiver_processBundle(Context context, Bundle
6565

6666

6767
public class NotificationTable extends OneSignalDbContract.NotificationTable { }
68+
public class NotificationRestorer extends com.onesignal.NotificationRestorer { }
6869
}

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

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040

4141
import com.onesignal.BuildConfig;
4242
import com.onesignal.GcmBroadcastReceiver;
43+
import com.onesignal.GcmIntentService;
4344
import com.onesignal.NotificationExtenderService;
4445
import com.onesignal.NotificationOpenedProcessor;
4546
import com.onesignal.OSNotificationPayload;
@@ -52,6 +53,7 @@
5253
import com.onesignal.ShadowRoboNotificationManager.PostedNotification;
5354
import com.onesignal.example.BlankActivity;
5455
import com.onesignal.OneSignalPackagePrivateHelper.NotificationTable;
56+
import com.onesignal.OneSignalPackagePrivateHelper.NotificationRestorer;
5557

5658
import junit.framework.Assert;
5759

@@ -169,6 +171,7 @@ public void shouldHandleBasicNotifications() throws Exception {
169171
long currentTime = System.currentTimeMillis() / 1000;
170172
cursor.moveToFirst();
171173
Assert.assertTrue(cursor.getLong(0) > currentTime - 2 && cursor.getLong(0) <= currentTime);
174+
cursor.close();
172175

173176
// Should get marked as opened.
174177
NotificationOpenedProcessor.processFromActivity(blankActivity, createOpenIntent(bundle));
@@ -177,44 +180,55 @@ public void shouldHandleBasicNotifications() throws Exception {
177180
Assert.assertEquals(1, cursor.getInt(0));
178181
Assert.assertEquals(0, ShadowBadgeCountUpdater.lastCount);
179182
int firstNotifId = cursor.getInt(1);
183+
cursor.close();
180184

181185
// Should not display a duplicate notification, count should still be 1
182186
NotificationBundleProcessor_ProcessFromGCMIntentService(blankActivity, bundle, null);
183187
cursor = readableDb.query(NotificationTable.TABLE_NAME, null, null, null, null, null, null);
184188
Assert.assertEquals(1, cursor.getCount());
185189
Assert.assertEquals(0, ShadowBadgeCountUpdater.lastCount);
190+
cursor.close();
186191

187192
// Display a second notification
188193
bundle = getBaseNotifBundle("UUID2");
189194
NotificationBundleProcessor_ProcessFromGCMIntentService(blankActivity, bundle, null);
190-
cursor = readableDb.query(NotificationTable.TABLE_NAME, new String[] { "android_notification_id" }, "android_notification_id <> " + firstNotifId, null, null, null, null);
191-
cursor.moveToFirst();
192-
int secondNotifId = cursor.getInt(0);
193195

194-
// Go forward 1 week.
195-
ShadowSystemClock.setCurrentTimeMillis(System.currentTimeMillis() + 604801 * 1000);
196+
// Go forward 4 weeks
197+
// Note: Does not effect the SQL function strftime
198+
ShadowSystemClock.setCurrentTimeMillis(System.currentTimeMillis() + 2419201L * 1000L);
196199

197200
// Display a 3rd notification
198201
// Should of been added for a total of 2 records now.
199202
// First opened should of been cleaned up, 1 week old non opened notification should stay, and one new record.
200203
bundle = getBaseNotifBundle("UUID3");
201204
NotificationBundleProcessor_ProcessFromGCMIntentService(blankActivity, bundle, null);
202-
cursor = readableDb.query(NotificationTable.TABLE_NAME, new String[] { "android_notification_id" }, null, null, null, null, null);
203-
Assert.assertEquals(2, cursor.getCount());
204-
Assert.assertEquals(2, ShadowBadgeCountUpdater.lastCount);
205+
cursor = readableDb.query(NotificationTable.TABLE_NAME, new String[] { "android_notification_id", "created_time" }, null, null, null, null, null);
206+
207+
Assert.assertEquals(1, cursor.getCount());
208+
Assert.assertEquals(1, ShadowBadgeCountUpdater.lastCount);
205209

206-
cursor.moveToFirst();
207-
boolean foundSecond = false;
208-
do {
209-
Assert.assertTrue(cursor.getInt(0) != firstNotifId);
210-
if (cursor.getInt(0) == secondNotifId)
211-
foundSecond = true;
212-
} while (cursor.moveToNext());
213-
214-
Assert.assertTrue(foundSecond);
215210
cursor.close();
216211
}
217212

213+
@Test
214+
public void shouldRestoreNotifications() throws Exception {
215+
NotificationRestorer.restore(blankActivity); NotificationRestorer.restored = false;
216+
217+
NotificationBundleProcessor_ProcessFromGCMIntentService(blankActivity, getBaseNotifBundle(), null);
218+
219+
NotificationRestorer.restore(blankActivity); NotificationRestorer.restored = false;
220+
Intent intent = Shadows.shadowOf(blankActivity).getNextStartedService();
221+
Assert.assertEquals(GcmIntentService.class.getName(), intent.getComponent().getClassName());
222+
223+
// Go forward 1 week
224+
// Note: Does not effect the SQL function strftime
225+
ShadowSystemClock.setCurrentTimeMillis(System.currentTimeMillis() + 604801L * 1000L);
226+
227+
// Restorer should not fire service since the notification is over 1 week old.
228+
NotificationRestorer.restore(blankActivity); NotificationRestorer.restored = false;
229+
Assert.assertNull(Shadows.shadowOf(blankActivity).getNextStartedService());
230+
}
231+
218232
@Test
219233
public void shouldGenerate2BasicGroupNotifications() throws Exception {
220234
// Make sure the notification got posted and the content is correct.

0 commit comments

Comments
 (0)