blob: 718393410d3b2b27e02b84bac5291a3b21454132 [file] [log] [blame]
Selim Cinek389edcd2017-05-11 19:16:44 -07001/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License
15 */
16
17package android.app;
18
19import static com.android.internal.util.NotificationColorUtil.satisfiesTextContrast;
20
Julia Reynoldsd71c5a92017-06-30 13:34:01 -040021import static org.junit.Assert.assertFalse;
Robin Leead7e72a2017-12-04 15:45:46 +010022import static org.junit.Assert.assertNotSame;
23import static org.junit.Assert.assertSame;
Selim Cinek389edcd2017-05-11 19:16:44 -070024import static org.junit.Assert.assertTrue;
25
26import android.content.Context;
Adrian Roosfb921842017-10-26 14:49:56 +020027import android.content.Intent;
Robin Leead7e72a2017-12-04 15:45:46 +010028import android.graphics.BitmapFactory;
29import android.graphics.drawable.Icon;
Selim Cinek389edcd2017-05-11 19:16:44 -070030import android.media.session.MediaSession;
Adrian Roosfb921842017-10-26 14:49:56 +020031import android.os.Parcel;
Robin Leead7e72a2017-12-04 15:45:46 +010032import android.os.Parcelable;
Selim Cinek389edcd2017-05-11 19:16:44 -070033import android.support.test.InstrumentationRegistry;
34import android.support.test.filters.SmallTest;
35import android.support.test.runner.AndroidJUnit4;
Adrian Roosfb921842017-10-26 14:49:56 +020036import android.widget.RemoteViews;
Selim Cinek389edcd2017-05-11 19:16:44 -070037
Selim Cinek389edcd2017-05-11 19:16:44 -070038import org.junit.Before;
39import org.junit.Test;
40import org.junit.runner.RunWith;
41
42@RunWith(AndroidJUnit4.class)
43@SmallTest
44public class NotificationTest {
45
46 private Context mContext;
47
48 @Before
49 public void setUp() {
50 mContext = InstrumentationRegistry.getContext();
51 }
52
53 @Test
Julia Reynoldsd71c5a92017-06-30 13:34:01 -040054 public void testColorizedByPermission() {
55 Notification n = new Notification.Builder(mContext, "test")
56 .setFlag(Notification.FLAG_CAN_COLORIZE, true)
57 .setColorized(true)
58 .build();
59 assertTrue(n.isColorized());
60
61 n = new Notification.Builder(mContext, "test")
62 .setFlag(Notification.FLAG_CAN_COLORIZE, true)
63 .build();
64 assertFalse(n.isColorized());
65
66 n = new Notification.Builder(mContext, "test")
67 .setFlag(Notification.FLAG_CAN_COLORIZE, false)
68 .setColorized(true)
69 .build();
70 assertFalse(n.isColorized());
71 }
72
73 @Test
74 public void testColorizedByForeground() {
75 Notification n = new Notification.Builder(mContext, "test")
76 .setFlag(Notification.FLAG_FOREGROUND_SERVICE, true)
77 .setColorized(true)
78 .build();
79 assertTrue(n.isColorized());
80
81 n = new Notification.Builder(mContext, "test")
82 .setFlag(Notification.FLAG_FOREGROUND_SERVICE, true)
83 .build();
84 assertFalse(n.isColorized());
85
86 n = new Notification.Builder(mContext, "test")
87 .setFlag(Notification.FLAG_FOREGROUND_SERVICE, false)
88 .setColorized(true)
89 .build();
90 assertFalse(n.isColorized());
91 }
92
93 @Test
Selim Cinek389edcd2017-05-11 19:16:44 -070094 public void testColorSatisfiedWhenBgDarkTextDarker() {
95 Notification.Builder builder = getMediaNotification();
Julia Reynoldsd71c5a92017-06-30 13:34:01 -040096 Notification n = builder.build();
97
98 assertTrue(n.isColorized());
Selim Cinek389edcd2017-05-11 19:16:44 -070099
100 // An initial guess where the foreground color is actually darker than an already dark bg
101 int backgroundColor = 0xff585868;
102 int initialForegroundColor = 0xff505868;
103 builder.setColorPalette(backgroundColor, initialForegroundColor);
104 int primaryTextColor = builder.getPrimaryTextColor();
105 assertTrue(satisfiesTextContrast(primaryTextColor, backgroundColor));
106 int secondaryTextColor = builder.getSecondaryTextColor();
107 assertTrue(satisfiesTextContrast(secondaryTextColor, backgroundColor));
108 }
109
Julia Reynolds6ad0aec2017-07-05 08:47:03 -0400110 @Test
111 public void testHasCompletedProgress_noProgress() {
112 Notification n = new Notification.Builder(mContext).build();
113
114 assertFalse(n.hasCompletedProgress());
115 }
116
117 @Test
118 public void testHasCompletedProgress_complete() {
119 Notification n = new Notification.Builder(mContext)
120 .setProgress(100, 100, true)
121 .build();
122 Notification n2 = new Notification.Builder(mContext)
123 .setProgress(10, 10, false)
124 .build();
125 assertTrue(n.hasCompletedProgress());
126 assertTrue(n2.hasCompletedProgress());
127 }
128
129 @Test
130 public void testHasCompletedProgress_notComplete() {
131 Notification n = new Notification.Builder(mContext)
132 .setProgress(100, 99, true)
133 .build();
134 Notification n2 = new Notification.Builder(mContext)
135 .setProgress(10, 4, false)
136 .build();
137 assertFalse(n.hasCompletedProgress());
138 assertFalse(n2.hasCompletedProgress());
139 }
140
141 @Test
142 public void testHasCompletedProgress_zeroMax() {
143 Notification n = new Notification.Builder(mContext)
144 .setProgress(0, 0, true)
145 .build();
146 assertFalse(n.hasCompletedProgress());
147 }
148
Adrian Roosfb921842017-10-26 14:49:56 +0200149 @Test
Robin Leead7e72a2017-12-04 15:45:46 +0100150 public void largeIconMultipleReferences_keptAfterParcelling() {
151 Icon originalIcon = Icon.createWithBitmap(BitmapFactory.decodeResource(
152 mContext.getResources(), com.android.frameworks.coretests.R.drawable.test128x96));
153
154 Notification n = new Notification.Builder(mContext).setLargeIcon(originalIcon).build();
155 assertSame(n.getLargeIcon(), originalIcon);
156
157 Notification q = writeAndReadParcelable(n);
158 assertNotSame(q.getLargeIcon(), n.getLargeIcon());
159
160 assertTrue(q.getLargeIcon().getBitmap().sameAs(n.getLargeIcon().getBitmap()));
161 assertSame(q.getLargeIcon(), q.extras.getParcelable(Notification.EXTRA_LARGE_ICON));
162 }
163
164 @Test
165 public void largeIconReferenceInExtrasOnly_keptAfterParcelling() {
166 Icon originalIcon = Icon.createWithBitmap(BitmapFactory.decodeResource(
167 mContext.getResources(), com.android.frameworks.coretests.R.drawable.test128x96));
168
169 Notification n = new Notification.Builder(mContext).build();
170 n.extras.putParcelable(Notification.EXTRA_LARGE_ICON, originalIcon);
171 assertSame(n.getLargeIcon(), null);
172
173 Notification q = writeAndReadParcelable(n);
174 assertSame(q.getLargeIcon(), null);
175 assertTrue(((Icon) q.extras.getParcelable(Notification.EXTRA_LARGE_ICON)).getBitmap()
176 .sameAs(originalIcon.getBitmap()));
177 }
178
179 @Test
Adrian Roosfb921842017-10-26 14:49:56 +0200180 public void allPendingIntents_recollectedAfterReusingBuilder() {
181 PendingIntent intent1 = PendingIntent.getActivity(mContext, 0, new Intent("test1"), 0);
182 PendingIntent intent2 = PendingIntent.getActivity(mContext, 0, new Intent("test2"), 0);
183
184 Notification.Builder builder = new Notification.Builder(mContext, "channel");
185 builder.setContentIntent(intent1);
186
187 Parcel p = Parcel.obtain();
188
189 Notification n1 = builder.build();
190 n1.writeToParcel(p, 0);
191
192 builder.setContentIntent(intent2);
193 Notification n2 = builder.build();
194 n2.writeToParcel(p, 0);
195
196 assertTrue(n2.allPendingIntents.contains(intent2));
197 }
198
199 @Test
200 public void allPendingIntents_containsCustomRemoteViews() {
201 PendingIntent intent = PendingIntent.getActivity(mContext, 0, new Intent("test"), 0);
202
203 RemoteViews contentView = new RemoteViews(mContext.getPackageName(), 0 /* layoutId */);
204 contentView.setOnClickPendingIntent(1 /* id */, intent);
205
206 Notification.Builder builder = new Notification.Builder(mContext, "channel");
207 builder.setCustomContentView(contentView);
208
209 Parcel p = Parcel.obtain();
210
211 Notification n = builder.build();
212 n.writeToParcel(p, 0);
213
214 assertTrue(n.allPendingIntents.contains(intent));
215 }
216
Kodlee Yin9ac617c2017-12-19 11:20:50 -0800217 @Test
218 public void testMessagingStyle_isGroupConversation() {
219 Notification.MessagingStyle messagingStyle = new Notification.MessagingStyle("self name")
220 .setGroupConversation(true);
221 Notification notification = new Notification.Builder(mContext, "test id")
222 .setSmallIcon(1)
223 .setContentTitle("test title")
224 .setStyle(messagingStyle)
225 .build();
226
227 assertTrue(messagingStyle.isGroupConversation());
228 assertTrue(notification.extras.getBoolean(Notification.EXTRA_IS_GROUP_CONVERSATION));
229 }
230
Selim Cinek389edcd2017-05-11 19:16:44 -0700231 private Notification.Builder getMediaNotification() {
232 MediaSession session = new MediaSession(mContext, "test");
233 return new Notification.Builder(mContext, "color")
234 .setSmallIcon(com.android.internal.R.drawable.emergency_icon)
235 .setContentTitle("Title")
236 .setContentText("Text")
237 .setStyle(new Notification.MediaStyle().setMediaSession(session.getSessionToken()));
238 }
Robin Leead7e72a2017-12-04 15:45:46 +0100239
240 /**
241 * Writes an arbitrary {@link Parcelable} into a {@link Parcel} using its writeToParcel
242 * method before reading it out again to check that it was sent properly.
243 */
244 private static <T extends Parcelable> T writeAndReadParcelable(T original) {
245 Parcel p = Parcel.obtain();
246 p.writeParcelable(original, /* flags */ 0);
247 p.setDataPosition(0);
248 return p.readParcelable(/* classLoader */ null);
249 }
Selim Cinek389edcd2017-05-11 19:16:44 -0700250}