4040
4141import com .onesignal .BuildConfig ;
4242import com .onesignal .GcmBroadcastReceiver ;
43+ import com .onesignal .GcmIntentService ;
4344import com .onesignal .NotificationExtenderService ;
4445import com .onesignal .NotificationOpenedProcessor ;
4546import com .onesignal .OSNotificationPayload ;
5253import com .onesignal .ShadowRoboNotificationManager .PostedNotification ;
5354import com .onesignal .example .BlankActivity ;
5455import com .onesignal .OneSignalPackagePrivateHelper .NotificationTable ;
56+ import com .onesignal .OneSignalPackagePrivateHelper .NotificationRestorer ;
5557
5658import 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