40
40
41
41
import com .onesignal .BuildConfig ;
42
42
import com .onesignal .GcmBroadcastReceiver ;
43
+ import com .onesignal .GcmIntentService ;
43
44
import com .onesignal .NotificationExtenderService ;
44
45
import com .onesignal .NotificationOpenedProcessor ;
45
46
import com .onesignal .OSNotificationPayload ;
52
53
import com .onesignal .ShadowRoboNotificationManager .PostedNotification ;
53
54
import com .onesignal .example .BlankActivity ;
54
55
import com .onesignal .OneSignalPackagePrivateHelper .NotificationTable ;
56
+ import com .onesignal .OneSignalPackagePrivateHelper .NotificationRestorer ;
55
57
56
58
import junit .framework .Assert ;
57
59
@@ -169,6 +171,7 @@ public void shouldHandleBasicNotifications() throws Exception {
169
171
long currentTime = System .currentTimeMillis () / 1000 ;
170
172
cursor .moveToFirst ();
171
173
Assert .assertTrue (cursor .getLong (0 ) > currentTime - 2 && cursor .getLong (0 ) <= currentTime );
174
+ cursor .close ();
172
175
173
176
// Should get marked as opened.
174
177
NotificationOpenedProcessor .processFromActivity (blankActivity , createOpenIntent (bundle ));
@@ -177,44 +180,55 @@ public void shouldHandleBasicNotifications() throws Exception {
177
180
Assert .assertEquals (1 , cursor .getInt (0 ));
178
181
Assert .assertEquals (0 , ShadowBadgeCountUpdater .lastCount );
179
182
int firstNotifId = cursor .getInt (1 );
183
+ cursor .close ();
180
184
181
185
// Should not display a duplicate notification, count should still be 1
182
186
NotificationBundleProcessor_ProcessFromGCMIntentService (blankActivity , bundle , null );
183
187
cursor = readableDb .query (NotificationTable .TABLE_NAME , null , null , null , null , null , null );
184
188
Assert .assertEquals (1 , cursor .getCount ());
185
189
Assert .assertEquals (0 , ShadowBadgeCountUpdater .lastCount );
190
+ cursor .close ();
186
191
187
192
// Display a second notification
188
193
bundle = getBaseNotifBundle ("UUID2" );
189
194
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 );
193
195
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 );
196
199
197
200
// Display a 3rd notification
198
201
// Should of been added for a total of 2 records now.
199
202
// First opened should of been cleaned up, 1 week old non opened notification should stay, and one new record.
200
203
bundle = getBaseNotifBundle ("UUID3" );
201
204
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 );
205
209
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 );
215
210
cursor .close ();
216
211
}
217
212
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
+
218
232
@ Test
219
233
public void shouldGenerate2BasicGroupNotifications () throws Exception {
220
234
// Make sure the notification got posted and the content is correct.
0 commit comments