RemoteNotifications { public void updateNotificationsFromServer(UpdatePolicy updatePolicy, final NotificationLoaderFinishListener listener) { if (updatePolicy == null) { throw new IllegalArgumentException("updatePolicy must not be null"); } Date date = preferenceStore.getLastServerUpdate(); boolean shouldUpdate = updatePolicy.shouldUpdate(date); Log.v(TAG, "UpdatePolicy: " + updatePolicy.toString() + ", shouldUpdate: " + shouldUpdate); if (!shouldUpdate) { return; } boolean updateRunning = notificationUpdateRunning.getAndSet(true); if (updateRunning) { Log.w(TAG, "Notification update already running, skipping this update"); return; } NotificationLoaderFinishListener internalListener = new NotificationLoaderFinishListener() { @Override public void onDownloadFinished(Set<UserNotification> notifications) { Log.v(TAG, "Received " + notifications.size() + " notifications"); handleNotificationsFromServer(notifications); notificationUpdateRunning.set(false); preferenceStore.saveLastServerUpdate(new Date()); if (scheduledShowEvent.getAndSet(false)) { Log.v(TAG, "executing scheduled show event"); showPendingNotificationsToUser(scheduledShowEventParameter); } if (listener != null) { listener.onDownloadFinished(notifications); } } }; NotificationLoaderTask notificationLoaderTask = new NotificationLoaderTask(serverUrl, internalListener); notificationLoaderTask.execute(); } RemoteNotifications(Context context, URL serverUrl);  RemoteNotifications(Context context, URL serverUrl, String sharedPreferenceName); void showPendingNotificationsToUser(); void showPendingNotificationsToUser(boolean showAll); void updateNotificationsFromServer(UpdatePolicy updatePolicy, final NotificationLoaderFinishListener listener); void updateNotificationsFromServer(UpdatePolicy updatePolicy); NotificationStore getPreferenceStore(); static void start(Context context, URL url, UpdatePolicy updatePolicy); static final String TAG; }
@Test(expected = IllegalArgumentException.class) public void testMissingServerFile() { URL serverUrl = RemoteNotificationsTest.class.getResource("/not-existing.json"); RemoteNotifications remoteNotifications = new RemoteNotifications(RuntimeEnvironment.application.getApplicationContext(), serverUrl); remoteNotifications.updateNotificationsFromServer(UpdatePolicy.NOW); }