blob: 025296d14da4c407b87e834bfb0e19d4aff98f74 [file] [log] [blame]
Selim Cinek2630dc72017-04-20 15:16:10 -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
Rohan Shah20790b82018-07-02 17:21:04 -070017package com.android.systemui.statusbar.notification.row;
Selim Cinek2630dc72017-04-20 15:16:10 -070018
Ned Burns1a5e22f2019-02-14 15:11:52 -050019import static com.android.systemui.statusbar.notification.row.NotificationContentInflater.FLAG_CONTENT_VIEW_ALL;
20import static com.android.systemui.statusbar.notification.row.NotificationContentInflater.FLAG_CONTENT_VIEW_AMBIENT;
21import static com.android.systemui.statusbar.notification.row.NotificationContentInflater.FLAG_CONTENT_VIEW_EXPANDED;
22import static com.android.systemui.statusbar.notification.row.NotificationContentInflater.FLAG_CONTENT_VIEW_HEADS_UP;
23import static com.android.systemui.statusbar.notification.row.NotificationContentInflater.FLAG_CONTENT_VIEW_PUBLIC;
Selim Cinek2630dc72017-04-20 15:16:10 -070024
Kevind4660b22018-09-27 10:57:35 -070025import static org.junit.Assert.assertEquals;
26import static org.junit.Assert.assertNotNull;
27import static org.junit.Assert.assertNull;
Jason Monk6dceace2018-05-15 20:24:07 -040028import static org.junit.Assert.assertTrue;
Selim Cinek2630dc72017-04-20 15:16:10 -070029import static org.mockito.Mockito.spy;
30import static org.mockito.Mockito.times;
31import static org.mockito.Mockito.verify;
32
33import android.app.Notification;
34import android.content.Context;
Selim Cinekd246bed2017-06-19 16:58:35 -070035import android.os.CancellationSignal;
36import android.os.Handler;
37import android.os.Looper;
Selim Cinek2630dc72017-04-20 15:16:10 -070038import android.service.notification.StatusBarNotification;
Jason Monk6dceace2018-05-15 20:24:07 -040039import android.testing.AndroidTestingRunner;
40import android.testing.TestableLooper.RunWithLooper;
Kevind4660b22018-09-27 10:57:35 -070041import android.util.ArrayMap;
Selim Cinekd246bed2017-06-19 16:58:35 -070042import android.view.View;
43import android.view.ViewGroup;
Selim Cinek2630dc72017-04-20 15:16:10 -070044import android.widget.RemoteViews;
45
Brett Chabot84151d92019-02-27 15:37:59 -080046import androidx.test.filters.SmallTest;
47
Selim Cinek2630dc72017-04-20 15:16:10 -070048import com.android.systemui.R;
Jason Monkfba8faf2017-05-23 10:42:59 -040049import com.android.systemui.SysuiTestCase;
Jason Monkb05395f2017-07-11 10:05:03 -040050import com.android.systemui.statusbar.InflationTask;
Selim Cinek2630dc72017-04-20 15:16:10 -070051import com.android.systemui.statusbar.NotificationTestHelper;
Ned Burnsf81c4c42019-01-07 14:10:43 -050052import com.android.systemui.statusbar.notification.collection.NotificationEntry;
Ned Burns1a5e22f2019-02-14 15:11:52 -050053import com.android.systemui.statusbar.notification.row.NotificationContentInflater.InflationCallback;
Selim Cinek2630dc72017-04-20 15:16:10 -070054
55import org.junit.Assert;
56import org.junit.Before;
Geoffrey Pitsch351a3212017-05-22 15:20:20 -040057import org.junit.Ignore;
Selim Cinek2630dc72017-04-20 15:16:10 -070058import org.junit.Test;
59import org.junit.runner.RunWith;
60
Selim Cinekd246bed2017-06-19 16:58:35 -070061import java.util.HashMap;
Selim Cinek2630dc72017-04-20 15:16:10 -070062import java.util.concurrent.CountDownLatch;
Selim Cinekd246bed2017-06-19 16:58:35 -070063import java.util.concurrent.Executor;
Jason Monk6dceace2018-05-15 20:24:07 -040064import java.util.concurrent.TimeUnit;
Selim Cinek2630dc72017-04-20 15:16:10 -070065
66@SmallTest
Jason Monk6dceace2018-05-15 20:24:07 -040067@RunWith(AndroidTestingRunner.class)
68@RunWithLooper(setAsMainLooper = true)
Ned Burns1a5e22f2019-02-14 15:11:52 -050069public class NotificationContentInflaterTest extends SysuiTestCase {
Selim Cinek2630dc72017-04-20 15:16:10 -070070
Ned Burns1a5e22f2019-02-14 15:11:52 -050071 private NotificationContentInflater mNotificationInflater;
Selim Cinek2630dc72017-04-20 15:16:10 -070072 private Notification.Builder mBuilder;
73 private ExpandableNotificationRow mRow;
74
75 @Before
76 public void setUp() throws Exception {
Selim Cinek2630dc72017-04-20 15:16:10 -070077 mBuilder = new Notification.Builder(mContext).setSmallIcon(
78 R.drawable.ic_person)
79 .setContentTitle("Title")
80 .setContentText("Text")
81 .setStyle(new Notification.BigTextStyle().bigText("big text"));
82 ExpandableNotificationRow row = new NotificationTestHelper(mContext).createRow(
83 mBuilder.build());
84 mRow = spy(row);
Ned Burns1a5e22f2019-02-14 15:11:52 -050085 mNotificationInflater = new NotificationContentInflater(mRow);
86 mNotificationInflater.setInflationCallback(new InflationCallback() {
Selim Cinek2630dc72017-04-20 15:16:10 -070087 @Override
88 public void handleInflationException(StatusBarNotification notification,
Selim Cinek01d3da62017-04-28 15:03:48 -070089 Exception e) {
Selim Cinek2630dc72017-04-20 15:16:10 -070090 }
91
92 @Override
Ned Burnsf81c4c42019-01-07 14:10:43 -050093 public void onAsyncInflationFinished(NotificationEntry entry,
Ned Burns1a5e22f2019-02-14 15:11:52 -050094 @NotificationContentInflater.InflationFlag int inflatedFlags) {
Selim Cinek2630dc72017-04-20 15:16:10 -070095 }
96 });
97 }
98
99 @Test
100 public void testIncreasedHeadsUpBeingUsed() {
101 mNotificationInflater.setUsesIncreasedHeadsUpHeight(true);
102 Notification.Builder builder = spy(mBuilder);
Ned Burns342c3a02019-02-15 18:08:39 -0500103 mNotificationInflater.inflateNotificationViews(
104 false /* inflateSynchronously */,
105 FLAG_CONTENT_VIEW_ALL,
106 builder,
107 mContext);
Selim Cinek2630dc72017-04-20 15:16:10 -0700108 verify(builder).createHeadsUpContentView(true);
109 }
110
111 @Test
112 public void testIncreasedHeightBeingUsed() {
113 mNotificationInflater.setUsesIncreasedHeight(true);
114 Notification.Builder builder = spy(mBuilder);
Ned Burns342c3a02019-02-15 18:08:39 -0500115 mNotificationInflater.inflateNotificationViews(
116 false /* inflateSynchronously */,
117 FLAG_CONTENT_VIEW_ALL,
118 builder,
119 mContext);
Selim Cinek2630dc72017-04-20 15:16:10 -0700120 verify(builder).createContentView(true);
121 }
122
123 @Test
124 public void testInflationCallsUpdated() throws Exception {
125 runThenWaitForInflation(() -> mNotificationInflater.inflateNotificationViews(),
126 mNotificationInflater);
127 verify(mRow).onNotificationUpdated();
128 }
129
130 @Test
Kevind4660b22018-09-27 10:57:35 -0700131 public void testInflationOnlyInflatesSetFlags() throws Exception {
Kevind5022f92018-10-08 18:30:26 -0700132 mNotificationInflater.updateInflationFlag(FLAG_CONTENT_VIEW_HEADS_UP,
Kevind4660b22018-09-27 10:57:35 -0700133 true /* shouldInflate */);
134 runThenWaitForInflation(() -> mNotificationInflater.inflateNotificationViews(),
135 mNotificationInflater);
136
137 assertNotNull(mRow.getPrivateLayout().getHeadsUpChild());
138 assertNull(mRow.getShowingLayout().getAmbientChild());
Selim Cinek2630dc72017-04-20 15:16:10 -0700139 verify(mRow).onNotificationUpdated();
140 }
141
142 @Test
143 public void testInflationThrowsErrorDoesntCallUpdated() throws Exception {
144 mRow.getPrivateLayout().removeAllViews();
145 mRow.getStatusBarNotification().getNotification().contentView
Selim Cinekdc1231c2017-04-27 17:30:50 -0700146 = new RemoteViews(mContext.getPackageName(), R.layout.status_bar);
Selim Cinek2630dc72017-04-20 15:16:10 -0700147 runThenWaitForInflation(() -> mNotificationInflater.inflateNotificationViews(),
148 true /* expectingException */, mNotificationInflater);
Jason Monk6dceace2018-05-15 20:24:07 -0400149 assertTrue(mRow.getPrivateLayout().getChildCount() == 0);
Selim Cinek2630dc72017-04-20 15:16:10 -0700150 verify(mRow, times(0)).onNotificationUpdated();
151 }
152
Selim Cinekdc1231c2017-04-27 17:30:50 -0700153 @Test
154 public void testAsyncTaskRemoved() throws Exception {
Selim Cinek0f66a4c2017-04-28 19:26:28 -0700155 mRow.getEntry().abortTask();
Selim Cinekdc1231c2017-04-27 17:30:50 -0700156 runThenWaitForInflation(() -> mNotificationInflater.inflateNotificationViews(),
157 mNotificationInflater);
Selim Cinek67ff2482017-05-25 10:27:28 -0700158 verify(mRow).onNotificationUpdated();
159 }
160
161 @Test
162 public void testRemovedNotInflated() throws Exception {
163 mRow.setRemoved();
164 mNotificationInflater.inflateNotificationViews();
165 Assert.assertNull(mRow.getEntry().getRunningTask());
166 }
167
Selim Cinekd246bed2017-06-19 16:58:35 -0700168 @Test
Jason Monkb05395f2017-07-11 10:05:03 -0400169 @Ignore
Selim Cinekd246bed2017-06-19 16:58:35 -0700170 public void testInflationIsRetriedIfAsyncFails() throws Exception {
Ned Burns1a5e22f2019-02-14 15:11:52 -0500171 NotificationContentInflater.InflationProgress result =
172 new NotificationContentInflater.InflationProgress();
Selim Cinekd246bed2017-06-19 16:58:35 -0700173 result.packageContext = mContext;
174 CountDownLatch countDownLatch = new CountDownLatch(1);
Ned Burns342c3a02019-02-15 18:08:39 -0500175 NotificationContentInflater.applyRemoteView(
176 false /* inflateSynchronously */,
177 result,
178 FLAG_CONTENT_VIEW_EXPANDED,
179 0,
Kevind4660b22018-09-27 10:57:35 -0700180 new ArrayMap() /* cachedContentViews */, mRow, false /* redactAmbient */,
Sunny Goyal43c97042018-08-23 15:21:26 -0700181 true /* isNewView */, (v, p, r) -> true,
Ned Burns1a5e22f2019-02-14 15:11:52 -0500182 new InflationCallback() {
Selim Cinekd246bed2017-06-19 16:58:35 -0700183 @Override
184 public void handleInflationException(StatusBarNotification notification,
185 Exception e) {
186 countDownLatch.countDown();
187 throw new RuntimeException("No Exception expected");
188 }
189
190 @Override
Ned Burnsf81c4c42019-01-07 14:10:43 -0500191 public void onAsyncInflationFinished(NotificationEntry entry,
Ned Burns1a5e22f2019-02-14 15:11:52 -0500192 @NotificationContentInflater.InflationFlag int inflatedFlags) {
Selim Cinekd246bed2017-06-19 16:58:35 -0700193 countDownLatch.countDown();
194 }
Kevind4660b22018-09-27 10:57:35 -0700195 }, mRow.getPrivateLayout(), null, null, new HashMap<>(),
Ned Burns1a5e22f2019-02-14 15:11:52 -0500196 new NotificationContentInflater.ApplyCallback() {
Selim Cinekd246bed2017-06-19 16:58:35 -0700197 @Override
198 public void setResultView(View v) {
199 }
200
201 @Override
202 public RemoteViews getRemoteView() {
203 return new AsyncFailRemoteView(mContext.getPackageName(),
204 R.layout.custom_view_dark);
205 }
206 });
Jason Monk6dceace2018-05-15 20:24:07 -0400207 assertTrue(countDownLatch.await(500, TimeUnit.MILLISECONDS));
Selim Cinekd246bed2017-06-19 16:58:35 -0700208 }
Selim Cinek67ff2482017-05-25 10:27:28 -0700209
Kevin38ce6fa2018-10-17 16:00:14 -0700210 @Test
211 public void testUpdateNeedsRedactionReinflatesChangedContentViews() {
212 mNotificationInflater.updateInflationFlag(FLAG_CONTENT_VIEW_AMBIENT, true);
213 mNotificationInflater.updateInflationFlag(FLAG_CONTENT_VIEW_PUBLIC, true);
214 mNotificationInflater.updateNeedsRedaction(true);
215
Ned Burns1a5e22f2019-02-14 15:11:52 -0500216 NotificationContentInflater.AsyncInflationTask asyncInflationTask =
217 (NotificationContentInflater.AsyncInflationTask) mRow.getEntry().getRunningTask();
Kevin38ce6fa2018-10-17 16:00:14 -0700218 assertEquals(FLAG_CONTENT_VIEW_AMBIENT | FLAG_CONTENT_VIEW_PUBLIC,
219 asyncInflationTask.getReInflateFlags());
220 asyncInflationTask.abort();
221 }
222
Selim Cinek21f33662017-09-08 13:24:21 -0700223 /* Cancelling requires us to be on the UI thread otherwise we might have a race */
Selim Cinek67ff2482017-05-25 10:27:28 -0700224 @Test
Kevind4660b22018-09-27 10:57:35 -0700225 public void testSupersedesExistingTask() {
Kevind5022f92018-10-08 18:30:26 -0700226 mNotificationInflater.addInflationFlags(FLAG_CONTENT_VIEW_ALL);
Selim Cinek67ff2482017-05-25 10:27:28 -0700227 mNotificationInflater.inflateNotificationViews();
Kevind4660b22018-09-27 10:57:35 -0700228
229 // Trigger inflation of content and expanded only.
Selim Cinek67ff2482017-05-25 10:27:28 -0700230 mNotificationInflater.setIsLowPriority(true);
231 mNotificationInflater.setIsChildInGroup(true);
Kevind4660b22018-09-27 10:57:35 -0700232
Selim Cinek67ff2482017-05-25 10:27:28 -0700233 InflationTask runningTask = mRow.getEntry().getRunningTask();
Ned Burns1a5e22f2019-02-14 15:11:52 -0500234 NotificationContentInflater.AsyncInflationTask asyncInflationTask =
235 (NotificationContentInflater.AsyncInflationTask) runningTask;
Kevind4660b22018-09-27 10:57:35 -0700236 assertEquals("Successive inflations don't inherit the previous flags!",
Kevin38ce6fa2018-10-17 16:00:14 -0700237 FLAG_CONTENT_VIEW_ALL, asyncInflationTask.getReInflateFlags());
Selim Cinek67ff2482017-05-25 10:27:28 -0700238 runningTask.abort();
Selim Cinekdc1231c2017-04-27 17:30:50 -0700239 }
240
Selim Cinekfc8073c2017-08-16 17:50:20 -0700241 @Test
242 public void doesntReapplyDisallowedRemoteView() throws Exception {
243 mBuilder.setStyle(new Notification.MediaStyle());
244 RemoteViews mediaView = mBuilder.createContentView();
245 mBuilder.setStyle(new Notification.DecoratedCustomViewStyle());
246 mBuilder.setCustomContentView(new RemoteViews(getContext().getPackageName(),
247 R.layout.custom_view_dark));
248 RemoteViews decoratedMediaView = mBuilder.createContentView();
249 Assert.assertFalse("The decorated media style doesn't allow a view to be reapplied!",
Ned Burns1a5e22f2019-02-14 15:11:52 -0500250 NotificationContentInflater.canReapplyRemoteView(mediaView, decoratedMediaView));
Selim Cinekfc8073c2017-08-16 17:50:20 -0700251 }
252
Selim Cinek2630dc72017-04-20 15:16:10 -0700253 public static void runThenWaitForInflation(Runnable block,
Ned Burns1a5e22f2019-02-14 15:11:52 -0500254 NotificationContentInflater inflater) throws Exception {
Selim Cinek2630dc72017-04-20 15:16:10 -0700255 runThenWaitForInflation(block, false /* expectingException */, inflater);
256 }
257
258 private static void runThenWaitForInflation(Runnable block, boolean expectingException,
Ned Burns1a5e22f2019-02-14 15:11:52 -0500259 NotificationContentInflater inflater) throws Exception {
Selim Cinek2630dc72017-04-20 15:16:10 -0700260 CountDownLatch countDownLatch = new CountDownLatch(1);
261 final ExceptionHolder exceptionHolder = new ExceptionHolder();
Ned Burns342c3a02019-02-15 18:08:39 -0500262 inflater.setInflateSynchronously(true);
Ned Burns1a5e22f2019-02-14 15:11:52 -0500263 inflater.setInflationCallback(new InflationCallback() {
Selim Cinek2630dc72017-04-20 15:16:10 -0700264 @Override
265 public void handleInflationException(StatusBarNotification notification,
Selim Cinek01d3da62017-04-28 15:03:48 -0700266 Exception e) {
Selim Cinek2630dc72017-04-20 15:16:10 -0700267 if (!expectingException) {
268 exceptionHolder.setException(e);
269 }
270 countDownLatch.countDown();
271 }
272
273 @Override
Ned Burnsf81c4c42019-01-07 14:10:43 -0500274 public void onAsyncInflationFinished(NotificationEntry entry,
Ned Burns1a5e22f2019-02-14 15:11:52 -0500275 @NotificationContentInflater.InflationFlag int inflatedFlags) {
Selim Cinek2630dc72017-04-20 15:16:10 -0700276 if (expectingException) {
277 exceptionHolder.setException(new RuntimeException(
278 "Inflation finished even though there should be an error"));
279 }
280 countDownLatch.countDown();
281 }
282 });
283 block.run();
Jason Monk6dceace2018-05-15 20:24:07 -0400284 assertTrue(countDownLatch.await(500, TimeUnit.MILLISECONDS));
Selim Cinek2630dc72017-04-20 15:16:10 -0700285 if (exceptionHolder.mException != null) {
286 throw exceptionHolder.mException;
287 }
288 }
289
290 private static class ExceptionHolder {
291 private Exception mException;
292
293 public void setException(Exception exception) {
294 mException = exception;
295 }
296 }
Selim Cinekd246bed2017-06-19 16:58:35 -0700297
298 private class AsyncFailRemoteView extends RemoteViews {
Jason Monk6dceace2018-05-15 20:24:07 -0400299 Handler mHandler = Handler.createAsync(Looper.getMainLooper());
Selim Cinekd246bed2017-06-19 16:58:35 -0700300
301 public AsyncFailRemoteView(String packageName, int layoutId) {
302 super(packageName, layoutId);
303 }
304
305 @Override
306 public View apply(Context context, ViewGroup parent) {
307 return super.apply(context, parent);
308 }
309
310 @Override
311 public CancellationSignal applyAsync(Context context, ViewGroup parent, Executor executor,
312 OnViewAppliedListener listener, OnClickHandler handler) {
313 mHandler.post(() -> listener.onError(new RuntimeException("Failed to inflate async")));
314 return new CancellationSignal();
315 }
316
317 @Override
318 public CancellationSignal applyAsync(Context context, ViewGroup parent, Executor executor,
319 OnViewAppliedListener listener) {
320 return applyAsync(context, parent, executor, listener, null);
321 }
322 }
Selim Cinek2630dc72017-04-20 15:16:10 -0700323}