use of org.robolectric.shadows.ShadowNotificationManager in project materialistic by hidroh.
the class ItemSyncAdapterTest method testNotification.
@Test
public void testNotification() throws IOException {
Call<HackerNewsItem> call = mock(Call.class);
when(call.execute()).thenReturn(Response.success(new TestHnItem(1L) {
@Override
public boolean isStoryType() {
return true;
}
@Override
public String getRawUrl() {
return "http://example.com";
}
@Override
public long[] getKids() {
return new long[] { 2L, 3L };
}
}));
when(TestRestServiceFactory.hnRestService.cachedItem(eq("1"))).thenReturn(call);
Call<HackerNewsItem> kid1Call = mock(Call.class);
when(kid1Call.execute()).thenReturn(Response.success(new TestHnItem(2L) {
@Override
public boolean isStoryType() {
return false;
}
}));
when(TestRestServiceFactory.hnRestService.cachedItem(eq("2"))).thenReturn(kid1Call);
Call<HackerNewsItem> kid2Call = mock(Call.class);
when(kid2Call.execute()).thenThrow(IOException.class);
when(TestRestServiceFactory.hnRestService.cachedItem(eq("3"))).thenReturn(kid2Call);
when(TestRestServiceFactory.hnRestService.networkItem(eq("3"))).thenReturn(kid2Call);
PreferenceManager.getDefaultSharedPreferences(service).edit().putBoolean(service.getString(R.string.pref_offline_notification), true).apply();
SyncDelegate.scheduleSync(service, new SyncDelegate.JobBuilder(RuntimeEnvironment.application, "1").build());
adapter.onPerformSync(mock(Account.class), getLastSyncExtras(), null, null, null);
verify(readabilityClient).parse(any(), eq("http://example.com"), readabilityCallbackCaptor.capture());
readabilityCallbackCaptor.getValue().onResponse("");
ShadowNotificationManager notificationManager = shadowOf((NotificationManager) service.getSystemService(Context.NOTIFICATION_SERVICE));
ProgressBar progress = shadowOf(notificationManager.getNotification(1)).getProgressBar();
// self + kid 1 + readability
assertThat(progress.getProgress()).isEqualTo(3);
// self + 2 kids + readability + web
assertThat(progress.getMax()).isEqualTo(104);
shadowOf(adapter.syncDelegate.mWebView).getWebChromeClient().onProgressChanged(adapter.syncDelegate.mWebView, 100);
verify(kid2Call).enqueue(callbackCapture.capture());
callbackCapture.getValue().onFailure(null, null);
assertThat(notificationManager.getAllNotifications()).isEmpty();
}
Aggregations