private Notification buildDefaultNotification(Context context) { String title = context.getString(R.string.default_filedownloader_notification_title); String content = context.getString(R.string.default_filedownloader_notification_content); Notification.Builder builder = new Notification.Builder(context, notificationChannelId); builder.setContentTitle(title).setContentText(content) .setSmallIcon(DEFAULT_NOTIFICATION_ID); return builder.build(); }
@Test @Config(minSdk = N) public void build_handlesNonStringContentTitle_atLeastN() { Notification notification = builder.setContentTitle(new SpannableString("My title")).build(); assertThat(shadowOf(notification).getContentTitle().toString()).isEqualTo("My title"); }
@Test @Config(maxSdk = M) public void build_handlesNonStringContentText() { Notification notification = builder.setContentText(new SpannableString("Hello")).build(); assertThat(shadowOf(notification).getContentText().toString()).isEqualTo("Hello"); }
@Test @Config(maxSdk = M) public void build_handlesNullContentTitle() { Notification notification = builder.setContentTitle(null).build(); assertThat(shadowOf(notification).getContentTitle().toString()).isEmpty(); }
@Test @Config(maxSdk = M) public void build_handlesNonStringContentTitle() { Notification notification = builder.setContentTitle(new SpannableString("My title")).build(); assertThat(shadowOf(notification).getContentTitle().toString()).isEqualTo("My title"); }
@Test @Config(maxSdk = M) public void build_handlesNullContentText() { Notification notification = builder.setContentText(null).build(); assertThat(shadowOf(notification).getContentText().toString()).isEmpty(); }
@Test @Config(minSdk = M) public void build_setsIconOnNotification() throws Exception { Notification notification = builder.setSmallIcon(R.drawable.an_image).build(); assertThat(notification.getSmallIcon().getResId()).isEqualTo(R.drawable.an_image); }
@Test @Config(minSdk = JELLY_BEAN_MR1) public void build_setShowWhenOnNotification() { Notification notification = builder.setShowWhen(false).build(); assertThat(shadowOf(notification).isWhenShown()).isFalse(); }
@Test public void stopForegroundRemovesNotificationIfAsked() { service.startForeground(21, notBuilder.build()); service.stopForeground(true); assertThat(shadowOf(nm2).getNotification(21)).isNull(); }
@Test public void startForeground() { Notification n = notBuilder.build(); service.startForeground(23, n); assertThat(shadowOf(service).getLastForegroundNotification()).isSameAs(n); assertThat(shadowOf(service).getLastForegroundNotificationId()).isEqualTo(23); assertThat(shadowOf(nm2).getNotification(23)).isSameAs(n); assertThat(n.flags & Notification.FLAG_FOREGROUND_SERVICE).isNotEqualTo(0); }
@Test @Config(maxSdk = M) public void build_handlesNullContentInfo() { Notification notification = builder.setContentInfo(null).build(); assertThat(shadowOf(notification).getContentInfo().toString()).isEmpty(); }
@Test public void build_setsContentTextOnNotification() throws Exception { Notification notification = builder.setContentText("Hello Text").build(); assertThat(shadowOf(notification).getContentText().toString()).isEqualTo("Hello Text"); }
@Test @Config(minSdk = JELLY_BEAN_MR1) public void build_whenShowWhenNotSet_setsShowWhenOnNotificationToTrue() { Notification notification = builder.setWhen(100).setShowWhen(true).build(); assertThat(shadowOf(notification).isWhenShown()).isTrue(); }
/** * According to spec, if the foreground notification is not removed earlier, * then it will be removed when the service is destroyed. */ @Test public void stopForegroundDoesntRemoveNotificationUnlessAsked() { Notification n = notBuilder.build(); service.startForeground(21, n); service.stopForeground(false); assertThat(shadowOf(nm2).getNotification(21)).isSameAs(n); }
@Test @Config(minSdk = JELLY_BEAN_MR1) public void build_setsUsesChronometerOnNotification_false() throws Exception { Notification notification = builder.setUsesChronometer(false).setWhen(10).setShowWhen(true).build(); assertThat(shadowOf(notification).usesChronometer()).isFalse(); }
@Test @Config(minSdk = JELLY_BEAN_MR1) public void build_setsUsesChronometerOnNotification_true() throws Exception { Notification notification = builder.setUsesChronometer(true).setWhen(10).setShowWhen(true).build(); assertThat(shadowOf(notification).usesChronometer()).isTrue(); }
@Test public void build_setsProgressOnNotification_true() throws Exception { Notification notification = builder.setProgress(36, 57, true).build(); // If indeterminate then max and progress values are ignored. assertThat(shadowOf(notification).isIndeterminate()).isTrue(); }
@Test public void build_setsContentInfoOnNotification() throws Exception { builder.setContentInfo("11"); Notification notification = builder.build(); assertThat(shadowOf(notification).getContentInfo().toString()).isEqualTo("11"); }
@Test public void build_whenSetOngoing_setsOngoingToTrue() { Notification notification = builder.setOngoing(true).build(); assertThat(shadowOf(notification).isOngoing()).isTrue(); }
/** * According to spec, if the foreground notification is not removed earlier, * then it will be removed when the service is destroyed. */ @Test public void onDestroyRemovesNotification() { Notification n = notBuilder.build(); service.startForeground(21, n); service.onDestroy(); assertThat(shadowOf(nm2).getNotification(21)).isNull(); }