• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package org.robolectric.shadows;
2 
3 import static android.os.Build.VERSION_CODES.JELLY_BEAN_MR1;
4 import static android.os.Build.VERSION_CODES.JELLY_BEAN_MR2;
5 import static android.os.Build.VERSION_CODES.M;
6 import static org.assertj.core.api.Assertions.assertThat;
7 import static org.robolectric.Shadows.shadowOf;
8 
9 import android.app.Notification;
10 import android.app.PendingIntent;
11 import android.graphics.Bitmap;
12 import android.graphics.BitmapFactory;
13 import android.graphics.drawable.Icon;
14 import org.junit.Test;
15 import org.junit.runner.RunWith;
16 import org.robolectric.R;
17 import org.robolectric.RobolectricTestRunner;
18 import org.robolectric.RuntimeEnvironment;
19 import org.robolectric.annotation.Config;
20 
21 @RunWith(RobolectricTestRunner.class)
22 public class ShadowNotificationBuilderTest {
23   private final Notification.Builder builder = new Notification.Builder(RuntimeEnvironment.application);
24 
25   @Test
build_setsContentTitleOnNotification()26   public void build_setsContentTitleOnNotification() throws Exception {
27     Notification notification = builder.setContentTitle("Hello").build();
28     assertThat(shadowOf(notification).getContentTitle().toString()).isEqualTo("Hello");
29   }
30 
31   @Test
build_whenSetOngoingNotSet_leavesSetOngoingAsFalse()32   public void build_whenSetOngoingNotSet_leavesSetOngoingAsFalse() {
33     Notification notification = builder.build();
34     assertThat(shadowOf(notification).isOngoing()).isFalse();
35   }
36 
37   @Test
build_whenSetOngoing_setsOngoingToTrue()38   public void build_whenSetOngoing_setsOngoingToTrue() {
39     Notification notification = builder.setOngoing(true).build();
40     assertThat(shadowOf(notification).isOngoing()).isTrue();
41   }
42 
43   @Test
44   @Config(minSdk = JELLY_BEAN_MR1)
build_whenShowWhenNotSet_setsShowWhenOnNotificationToTrue()45   public void build_whenShowWhenNotSet_setsShowWhenOnNotificationToTrue() {
46     Notification notification = builder.setWhen(100).setShowWhen(true).build();
47 
48     assertThat(shadowOf(notification).isWhenShown()).isTrue();
49   }
50 
51   @Test
52   @Config(minSdk = JELLY_BEAN_MR1)
build_setShowWhenOnNotification()53   public void build_setShowWhenOnNotification() {
54     Notification notification = builder.setShowWhen(false).build();
55 
56     assertThat(shadowOf(notification).isWhenShown()).isFalse();
57   }
58 
59   @Test
build_setsContentTextOnNotification()60   public void build_setsContentTextOnNotification() throws Exception {
61     Notification notification = builder.setContentText("Hello Text").build();
62 
63     assertThat(shadowOf(notification).getContentText().toString()).isEqualTo("Hello Text");
64   }
65 
66   @Test
build_setsTickerOnNotification()67   public void build_setsTickerOnNotification() throws Exception {
68     Notification notification = builder.setTicker("My ticker").build();
69 
70     assertThat(notification.tickerText).isEqualTo("My ticker");
71   }
72 
73   @Test
build_setsContentInfoOnNotification()74   public void build_setsContentInfoOnNotification() throws Exception {
75     builder.setContentInfo("11");
76     Notification notification = builder.build();
77     assertThat(shadowOf(notification).getContentInfo().toString()).isEqualTo("11");
78   }
79 
80   @Test
81   @Config(minSdk = M)
build_setsIconOnNotification()82   public void build_setsIconOnNotification() throws Exception {
83     Notification notification = builder.setSmallIcon(R.drawable.an_image).build();
84 
85     assertThat(notification.getSmallIcon().getResId()).isEqualTo(R.drawable.an_image);
86   }
87 
88   @Test
build_setsWhenOnNotification()89   public void build_setsWhenOnNotification() throws Exception {
90     Notification notification = builder.setWhen(11L).build();
91 
92     assertThat(notification.when).isEqualTo(11L);
93   }
94 
95   @Test
build_setsProgressOnNotification_true()96   public void build_setsProgressOnNotification_true() throws Exception {
97     Notification notification = builder.setProgress(36, 57, true).build();
98     // If indeterminate then max and progress values are ignored.
99     assertThat(shadowOf(notification).isIndeterminate()).isTrue();
100   }
101 
102   @Test
build_setsProgressOnNotification_false()103   public void build_setsProgressOnNotification_false() throws Exception {
104     Notification notification = builder.setProgress(50, 10, false).build();
105 
106     assertThat(shadowOf(notification).getMax()).isEqualTo(50);
107     assertThat(shadowOf(notification).getProgress()).isEqualTo(10);
108     assertThat(shadowOf(notification).isIndeterminate()).isFalse();
109   }
110 
111   @Test
112   @Config(minSdk = JELLY_BEAN_MR1)
build_setsUsesChronometerOnNotification_true()113   public void build_setsUsesChronometerOnNotification_true() throws Exception {
114     Notification notification = builder.setUsesChronometer(true).setWhen(10).setShowWhen(true).build();
115 
116     assertThat(shadowOf(notification).usesChronometer()).isTrue();
117   }
118 
119   @Test
120   @Config(minSdk = JELLY_BEAN_MR1)
build_setsUsesChronometerOnNotification_false()121   public void build_setsUsesChronometerOnNotification_false() throws Exception {
122     Notification notification = builder.setUsesChronometer(false).setWhen(10).setShowWhen(true).build();
123 
124     assertThat(shadowOf(notification).usesChronometer()).isFalse();
125   }
126 
127   @Test
build_handlesNullContentTitle()128   public void build_handlesNullContentTitle() {
129     Notification notification = builder.setContentTitle(null).build();
130 
131     assertThat(shadowOf(notification).getContentTitle()).isNullOrEmpty();
132   }
133 
134   @Test
build_handlesNullContentText()135   public void build_handlesNullContentText() {
136     Notification notification = builder.setContentText(null).build();
137 
138     assertThat(shadowOf(notification).getContentText()).isNullOrEmpty();
139   }
140 
141   @Test
build_handlesNullTicker()142   public void build_handlesNullTicker() {
143     Notification notification = builder.setTicker(null).build();
144 
145     assertThat(notification.tickerText).isNull();
146   }
147 
148   @Test
build_handlesNullContentInfo()149   public void build_handlesNullContentInfo() {
150     Notification notification = builder.setContentInfo(null).build();
151 
152     assertThat(shadowOf(notification).getContentInfo()).isNullOrEmpty();
153   }
154 
155   @Test
156   @Config(minSdk = JELLY_BEAN_MR2)
build_addsActionToNotification()157   public void build_addsActionToNotification() throws Exception {
158     PendingIntent action = PendingIntent.getBroadcast(RuntimeEnvironment.application, 0, null, 0);
159     Notification notification = builder.addAction(0, "Action", action).build();
160 
161     assertThat(notification.actions[0].actionIntent).isEqualToComparingFieldByField(action);
162   }
163 
164   @Test
withBigTextStyle()165   public void withBigTextStyle() {
166     Notification notification = builder.setStyle(new Notification.BigTextStyle(builder)
167         .bigText("BigText")
168         .setBigContentTitle("Title")
169         .setSummaryText("Summary"))
170         .build();
171 
172     assertThat(shadowOf(notification).getBigText()).isEqualTo("BigText");
173     assertThat(shadowOf(notification).getBigContentTitle()).isEqualTo("Title");
174     assertThat(shadowOf(notification).getBigContentText()).isEqualTo("Summary");
175     assertThat(shadowOf(notification).getBigPicture()).isNull();
176   }
177 
178   @Test
179   @Config(minSdk = M)
withBigPictureStyle()180   public void withBigPictureStyle() {
181     Bitmap bigPicture = BitmapFactory.decodeResource(RuntimeEnvironment.application.getResources(), R.drawable.an_image);
182 
183     Icon bigLargeIcon = Icon.createWithBitmap(bigPicture);
184     Notification notification = builder.setStyle(new Notification.BigPictureStyle(builder)
185         .bigPicture(bigPicture)
186         .bigLargeIcon(bigLargeIcon))
187         .build();
188 
189     assertThat(shadowOf(notification).getBigPicture()).isEqualTo(bigPicture);
190   }
191 }
192