MapboxOfflineDownloaderService extends Service implements OfflineRegionObserver, OnDownloadMapListener { @Override public void onStatusChanged(@NonNull OfflineRegionStatus status, @NonNull OfflineRegion offlineRegion) { double percentageDownload = (status.getRequiredResourceCount() >= 0) ? 100.0 * status.getCompletedResourceCount() / status.getRequiredResourceCount() : 0.0; sendBroadcast(SERVICE_ACTION_RESULT.SUCCESSFUL, currentMapDownloadName, currentServiceAction, String.valueOf(percentageDownload)); if (status.isComplete()) { stopDownloadProgressUpdater(); showDownloadCompleteNotification(String.format(getString(R.string.notification_download_complete_title), currentMapDownloadName), String.format(getString(R.string.notification_download_complete_content), getFriendlyFileSize(status.getCompletedResourceSize()))); realmDatabase.persistCompletedStatus(currentMapBoxTask); releaseQueueToPerformOtherJobs(); performNextTask(); } else { queueDownloadProgressUpdate(currentMapDownloadName, percentageDownload); } } MapboxOfflineDownloaderService(); @Override int onStartCommand(@Nullable Intent intent, int flags, int startId); @Nullable @Override IBinder onBind(Intent intent); @Override void onStatusChanged(@NonNull OfflineRegionStatus status, @NonNull OfflineRegion offlineRegion); @Override void onError(String error); @Override void onError(@NonNull String reason, @Nullable String message); @Override void mapboxTileCountLimitExceeded(long limit); @Override void onDestroy(); static final String KEY_RESULT_STATUS; static final String KEY_RESULT_MESSAGE; static final String KEY_RESULTS_PARENT_ACTION; static final String MY_PREFERENCES; static final String PREFERENCE_MAPBOX_ACCESS_TOKEN; static final int PROGRESS_NOTIFICATION_ID; static final int REQUEST_ID_STOP_MAP_DOWNLOAD; static int LAST_DOWNLOAD_COMPLETE_NOTIFICATION_ID; static int LAST_DOWNLOAD_ERROR_NOTIFICATION_ID; @RestrictTo(RestrictTo.Scope.TESTS)
    public boolean onStartCommandCalled; @RestrictTo(RestrictTo.Scope.TESTS)
    public boolean persistOfflineMapTaskCalled; @RestrictTo(RestrictTo.Scope.TESTS)
    public boolean performNextTaskCalled; @RestrictTo(RestrictTo.Scope.TESTS)
    public boolean observeOfflineRegionCalled; }
@Test public void onStatusChangedShouldShowProgressNotificationWhenGivenIncompleteOfflineRegionStatus() throws NoSuchFieldException, IllegalAccessException, InterruptedException, NoSuchMethodException, InvocationTargetException { latch = new CountDownLatch(1); OfflineRegionStatus incompleteOfflineRegionStatus = createOfflineRegion(OfflineRegion.STATE_ACTIVE, 200, 98923, 898, 230909, 300, true, false); setMapNameAndDownloadAction(mapName, MapboxOfflineDownloaderService.SERVICE_ACTION.DOWNLOAD_MAP); registerLocalBroadcastReceiverForDownloadServiceUpdates(); insertValueInPrivateField(mapboxOfflineDownloaderService, "serviceHandler", new Handler(mapboxOfflineDownloaderService.getApplication().getMainLooper())); Method method = mapboxOfflineDownloaderService.getClass().getDeclaredMethod("startDownloadProgressUpdater"); method.setAccessible(true); method.invoke(mapboxOfflineDownloaderService); mapboxOfflineDownloaderService.onStatusChanged(incompleteOfflineRegionStatus, null); latch.await(); double percentage = 100.0 * 200l/300l; String contentTitle = "Offline Map Download Progress: " + mapName; String contentText = "Downloading: " + NumberFormatter.formatDecimal(percentage) + " %"; Intent intent = (Intent) resultsToCheck.get(0); assertBroadcastResults(intent, MapboxOfflineDownloaderService.SERVICE_ACTION_RESULT.SUCCESSFUL, mapName, String.valueOf(percentage), MapboxOfflineDownloaderService.SERVICE_ACTION.DOWNLOAD_MAP ); ShadowNotificationManager shadowNotificationManager = Shadows.shadowOf((NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE)); Notification notification = shadowNotificationManager.getNotification(mapboxOfflineDownloaderService.PROGRESS_NOTIFICATION_ID); assertEquals(null, notification); Thread.sleep((Long) getValueInPrivateField(mapboxOfflineDownloaderService, "timeBetweenUpdates") * 2); ShadowLooper.runUiThreadTasks(); notification = shadowNotificationManager.getNotification(mapboxOfflineDownloaderService.PROGRESS_NOTIFICATION_ID); ShadowNotification shadowNotification = Shadows.shadowOf(notification); assertEquals(contentTitle, shadowNotification.getContentTitle()); assertEquals(contentText, shadowNotification.getContentText()); method = mapboxOfflineDownloaderService.getClass().getDeclaredMethod("stopDownloadProgressUpdater"); method.setAccessible(true); method.invoke(mapboxOfflineDownloaderService); }