blob: 99dc9f87520f702d9c5754c1c066f16a9cd05e8f [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;
Selim Cinek2630dc72017-04-20 15:16:10 -070039import android.support.test.filters.SmallTest;
Jason Monk6dceace2018-05-15 20:24:07 -040040import android.testing.AndroidTestingRunner;
41import android.testing.TestableLooper.RunWithLooper;
Kevind4660b22018-09-27 10:57:35 -070042import android.util.ArrayMap;
Selim Cinekd246bed2017-06-19 16:58:35 -070043import android.view.View;
44import android.view.ViewGroup;
Selim Cinek2630dc72017-04-20 15:16:10 -070045import android.widget.RemoteViews;
46
47import com.android.systemui.R;
Jason Monkfba8faf2017-05-23 10:42:59 -040048import com.android.systemui.SysuiTestCase;
Jason Monkb05395f2017-07-11 10:05:03 -040049import com.android.systemui.statusbar.InflationTask;
Selim Cinek2630dc72017-04-20 15:16:10 -070050import com.android.systemui.statusbar.NotificationTestHelper;
Ned Burnsf81c4c42019-01-07 14:10:43 -050051import com.android.systemui.statusbar.notification.collection.NotificationEntry;
Ned Burns1a5e22f2019-02-14 15:11:52 -050052import com.android.systemui.statusbar.notification.row.NotificationContentInflater.InflationCallback;
Selim Cinek2630dc72017-04-20 15:16:10 -070053
54import org.junit.Assert;
55import org.junit.Before;
Geoffrey Pitsch351a3212017-05-22 15:20:20 -040056import org.junit.Ignore;
Selim Cinek2630dc72017-04-20 15:16:10 -070057import org.junit.Test;
58import org.junit.runner.RunWith;
59
Selim Cinekd246bed2017-06-19 16:58:35 -070060import java.util.HashMap;
Selim Cinek2630dc72017-04-20 15:16:10 -070061import java.util.concurrent.CountDownLatch;
Selim Cinekd246bed2017-06-19 16:58:35 -070062import java.util.concurrent.Executor;
Jason Monk6dceace2018-05-15 20:24:07 -040063import java.util.concurrent.TimeUnit;
Selim Cinek2630dc72017-04-20 15:16:10 -070064
65@SmallTest
Jason Monk6dceace2018-05-15 20:24:07 -040066@RunWith(AndroidTestingRunner.class)
67@RunWithLooper(setAsMainLooper = true)
Ned Burns1a5e22f2019-02-14 15:11:52 -050068public class NotificationContentInflaterTest extends SysuiTestCase {
Selim Cinek2630dc72017-04-20 15:16:10 -070069
Ned Burns1a5e22f2019-02-14 15:11:52 -050070 private NotificationContentInflater mNotificationInflater;
Selim Cinek2630dc72017-04-20 15:16:10 -070071 private Notification.Builder mBuilder;
72 private ExpandableNotificationRow mRow;
73
74 @Before
75 public void setUp() throws Exception {
Selim Cinek2630dc72017-04-20 15:16:10 -070076 mBuilder = new Notification.Builder(mContext).setSmallIcon(
77 R.drawable.ic_person)
78 .setContentTitle("Title")
79 .setContentText("Text")
80 .setStyle(new Notification.BigTextStyle().bigText("big text"));
81 ExpandableNotificationRow row = new NotificationTestHelper(mContext).createRow(
82 mBuilder.build());
83 mRow = spy(row);
Ned Burns1a5e22f2019-02-14 15:11:52 -050084 mNotificationInflater = new NotificationContentInflater(mRow);
85 mNotificationInflater.setInflationCallback(new InflationCallback() {
Selim Cinek2630dc72017-04-20 15:16:10 -070086 @Override
87 public void handleInflationException(StatusBarNotification notification,
Selim Cinek01d3da62017-04-28 15:03:48 -070088 Exception e) {
Selim Cinek2630dc72017-04-20 15:16:10 -070089 }
90
91 @Override
Ned Burnsf81c4c42019-01-07 14:10:43 -050092 public void onAsyncInflationFinished(NotificationEntry entry,
Ned Burns1a5e22f2019-02-14 15:11:52 -050093 @NotificationContentInflater.InflationFlag int inflatedFlags) {
Selim Cinek2630dc72017-04-20 15:16:10 -070094 }
95 });
96 }
97
98 @Test
99 public void testIncreasedHeadsUpBeingUsed() {
100 mNotificationInflater.setUsesIncreasedHeadsUpHeight(true);
101 Notification.Builder builder = spy(mBuilder);
Ned Burns342c3a02019-02-15 18:08:39 -0500102 mNotificationInflater.inflateNotificationViews(
103 false /* inflateSynchronously */,
104 FLAG_CONTENT_VIEW_ALL,
105 builder,
106 mContext);
Selim Cinek2630dc72017-04-20 15:16:10 -0700107 verify(builder).createHeadsUpContentView(true);
108 }
109
110 @Test
111 public void testIncreasedHeightBeingUsed() {
112 mNotificationInflater.setUsesIncreasedHeight(true);
113 Notification.Builder builder = spy(mBuilder);
Ned Burns342c3a02019-02-15 18:08:39 -0500114 mNotificationInflater.inflateNotificationViews(
115 false /* inflateSynchronously */,
116 FLAG_CONTENT_VIEW_ALL,
117 builder,
118 mContext);
Selim Cinek2630dc72017-04-20 15:16:10 -0700119 verify(builder).createContentView(true);
120 }
121
122 @Test
123 public void testInflationCallsUpdated() throws Exception {
124 runThenWaitForInflation(() -> mNotificationInflater.inflateNotificationViews(),
125 mNotificationInflater);
126 verify(mRow).onNotificationUpdated();
127 }
128
129 @Test
Kevind4660b22018-09-27 10:57:35 -0700130 public void testInflationOnlyInflatesSetFlags() throws Exception {
Kevind5022f92018-10-08 18:30:26 -0700131 mNotificationInflater.updateInflationFlag(FLAG_CONTENT_VIEW_HEADS_UP,
Kevind4660b22018-09-27 10:57:35 -0700132 true /* shouldInflate */);
133 runThenWaitForInflation(() -> mNotificationInflater.inflateNotificationViews(),
134 mNotificationInflater);
135
136 assertNotNull(mRow.getPrivateLayout().getHeadsUpChild());
137 assertNull(mRow.getShowingLayout().getAmbientChild());
Selim Cinek2630dc72017-04-20 15:16:10 -0700138 verify(mRow).onNotificationUpdated();
139 }
140
141 @Test
142 public void testInflationThrowsErrorDoesntCallUpdated() throws Exception {
143 mRow.getPrivateLayout().removeAllViews();
144 mRow.getStatusBarNotification().getNotification().contentView
Selim Cinekdc1231c2017-04-27 17:30:50 -0700145 = new RemoteViews(mContext.getPackageName(), R.layout.status_bar);
Selim Cinek2630dc72017-04-20 15:16:10 -0700146 runThenWaitForInflation(() -> mNotificationInflater.inflateNotificationViews(),
147 true /* expectingException */, mNotificationInflater);
Jason Monk6dceace2018-05-15 20:24:07 -0400148 assertTrue(mRow.getPrivateLayout().getChildCount() == 0);
Selim Cinek2630dc72017-04-20 15:16:10 -0700149 verify(mRow, times(0)).onNotificationUpdated();
150 }
151
Selim Cinekdc1231c2017-04-27 17:30:50 -0700152 @Test
153 public void testAsyncTaskRemoved() throws Exception {
Selim Cinek0f66a4c2017-04-28 19:26:28 -0700154 mRow.getEntry().abortTask();
Selim Cinekdc1231c2017-04-27 17:30:50 -0700155 runThenWaitForInflation(() -> mNotificationInflater.inflateNotificationViews(),
156 mNotificationInflater);
Selim Cinek67ff2482017-05-25 10:27:28 -0700157 verify(mRow).onNotificationUpdated();
158 }
159
160 @Test
161 public void testRemovedNotInflated() throws Exception {
162 mRow.setRemoved();
163 mNotificationInflater.inflateNotificationViews();
164 Assert.assertNull(mRow.getEntry().getRunningTask());
165 }
166
Selim Cinekd246bed2017-06-19 16:58:35 -0700167 @Test
Jason Monkb05395f2017-07-11 10:05:03 -0400168 @Ignore
Selim Cinekd246bed2017-06-19 16:58:35 -0700169 public void testInflationIsRetriedIfAsyncFails() throws Exception {
Ned Burns1a5e22f2019-02-14 15:11:52 -0500170 NotificationContentInflater.InflationProgress result =
171 new NotificationContentInflater.InflationProgress();
Selim Cinekd246bed2017-06-19 16:58:35 -0700172 result.packageContext = mContext;
173 CountDownLatch countDownLatch = new CountDownLatch(1);
Ned Burns342c3a02019-02-15 18:08:39 -0500174 NotificationContentInflater.applyRemoteView(
175 false /* inflateSynchronously */,
176 result,
177 FLAG_CONTENT_VIEW_EXPANDED,
178 0,
Kevind4660b22018-09-27 10:57:35 -0700179 new ArrayMap() /* cachedContentViews */, mRow, false /* redactAmbient */,
Sunny Goyal43c97042018-08-23 15:21:26 -0700180 true /* isNewView */, (v, p, r) -> true,
Ned Burns1a5e22f2019-02-14 15:11:52 -0500181 new InflationCallback() {
Selim Cinekd246bed2017-06-19 16:58:35 -0700182 @Override
183 public void handleInflationException(StatusBarNotification notification,
184 Exception e) {
185 countDownLatch.countDown();
186 throw new RuntimeException("No Exception expected");
187 }
188
189 @Override
Ned Burnsf81c4c42019-01-07 14:10:43 -0500190 public void onAsyncInflationFinished(NotificationEntry entry,
Ned Burns1a5e22f2019-02-14 15:11:52 -0500191 @NotificationContentInflater.InflationFlag int inflatedFlags) {
Selim Cinekd246bed2017-06-19 16:58:35 -0700192 countDownLatch.countDown();
193 }
Kevind4660b22018-09-27 10:57:35 -0700194 }, mRow.getPrivateLayout(), null, null, new HashMap<>(),
Ned Burns1a5e22f2019-02-14 15:11:52 -0500195 new NotificationContentInflater.ApplyCallback() {
Selim Cinekd246bed2017-06-19 16:58:35 -0700196 @Override
197 public void setResultView(View v) {
198 }
199
200 @Override
201 public RemoteViews getRemoteView() {
202 return new AsyncFailRemoteView(mContext.getPackageName(),
203 R.layout.custom_view_dark);
204 }
205 });
Jason Monk6dceace2018-05-15 20:24:07 -0400206 assertTrue(countDownLatch.await(500, TimeUnit.MILLISECONDS));
Selim Cinekd246bed2017-06-19 16:58:35 -0700207 }
Selim Cinek67ff2482017-05-25 10:27:28 -0700208
Kevin38ce6fa2018-10-17 16:00:14 -0700209 @Test
210 public void testUpdateNeedsRedactionReinflatesChangedContentViews() {
211 mNotificationInflater.updateInflationFlag(FLAG_CONTENT_VIEW_AMBIENT, true);
212 mNotificationInflater.updateInflationFlag(FLAG_CONTENT_VIEW_PUBLIC, true);
213 mNotificationInflater.updateNeedsRedaction(true);
214
Ned Burns1a5e22f2019-02-14 15:11:52 -0500215 NotificationContentInflater.AsyncInflationTask asyncInflationTask =
216 (NotificationContentInflater.AsyncInflationTask) mRow.getEntry().getRunningTask();
Kevin38ce6fa2018-10-17 16:00:14 -0700217 assertEquals(FLAG_CONTENT_VIEW_AMBIENT | FLAG_CONTENT_VIEW_PUBLIC,
218 asyncInflationTask.getReInflateFlags());
219 asyncInflationTask.abort();
220 }
221
Selim Cinek21f33662017-09-08 13:24:21 -0700222 /* Cancelling requires us to be on the UI thread otherwise we might have a race */
Selim Cinek67ff2482017-05-25 10:27:28 -0700223 @Test
Kevind4660b22018-09-27 10:57:35 -0700224 public void testSupersedesExistingTask() {
Kevind5022f92018-10-08 18:30:26 -0700225 mNotificationInflater.addInflationFlags(FLAG_CONTENT_VIEW_ALL);
Selim Cinek67ff2482017-05-25 10:27:28 -0700226 mNotificationInflater.inflateNotificationViews();
Kevind4660b22018-09-27 10:57:35 -0700227
228 // Trigger inflation of content and expanded only.
Selim Cinek67ff2482017-05-25 10:27:28 -0700229 mNotificationInflater.setIsLowPriority(true);
230 mNotificationInflater.setIsChildInGroup(true);
Kevind4660b22018-09-27 10:57:35 -0700231
Selim Cinek67ff2482017-05-25 10:27:28 -0700232 InflationTask runningTask = mRow.getEntry().getRunningTask();
Ned Burns1a5e22f2019-02-14 15:11:52 -0500233 NotificationContentInflater.AsyncInflationTask asyncInflationTask =
234 (NotificationContentInflater.AsyncInflationTask) runningTask;
Kevind4660b22018-09-27 10:57:35 -0700235 assertEquals("Successive inflations don't inherit the previous flags!",
Kevin38ce6fa2018-10-17 16:00:14 -0700236 FLAG_CONTENT_VIEW_ALL, asyncInflationTask.getReInflateFlags());
Selim Cinek67ff2482017-05-25 10:27:28 -0700237 runningTask.abort();
Selim Cinekdc1231c2017-04-27 17:30:50 -0700238 }
239
Selim Cinekfc8073c2017-08-16 17:50:20 -0700240 @Test
241 public void doesntReapplyDisallowedRemoteView() throws Exception {
242 mBuilder.setStyle(new Notification.MediaStyle());
243 RemoteViews mediaView = mBuilder.createContentView();
244 mBuilder.setStyle(new Notification.DecoratedCustomViewStyle());
245 mBuilder.setCustomContentView(new RemoteViews(getContext().getPackageName(),
246 R.layout.custom_view_dark));
247 RemoteViews decoratedMediaView = mBuilder.createContentView();
248 Assert.assertFalse("The decorated media style doesn't allow a view to be reapplied!",
Ned Burns1a5e22f2019-02-14 15:11:52 -0500249 NotificationContentInflater.canReapplyRemoteView(mediaView, decoratedMediaView));
Selim Cinekfc8073c2017-08-16 17:50:20 -0700250 }
251
Selim Cinek2630dc72017-04-20 15:16:10 -0700252 public static void runThenWaitForInflation(Runnable block,
Ned Burns1a5e22f2019-02-14 15:11:52 -0500253 NotificationContentInflater inflater) throws Exception {
Selim Cinek2630dc72017-04-20 15:16:10 -0700254 runThenWaitForInflation(block, false /* expectingException */, inflater);
255 }
256
257 private static void runThenWaitForInflation(Runnable block, boolean expectingException,
Ned Burns1a5e22f2019-02-14 15:11:52 -0500258 NotificationContentInflater inflater) throws Exception {
Selim Cinek2630dc72017-04-20 15:16:10 -0700259 CountDownLatch countDownLatch = new CountDownLatch(1);
260 final ExceptionHolder exceptionHolder = new ExceptionHolder();
Ned Burns342c3a02019-02-15 18:08:39 -0500261 inflater.setInflateSynchronously(true);
Ned Burns1a5e22f2019-02-14 15:11:52 -0500262 inflater.setInflationCallback(new InflationCallback() {
Selim Cinek2630dc72017-04-20 15:16:10 -0700263 @Override
264 public void handleInflationException(StatusBarNotification notification,
Selim Cinek01d3da62017-04-28 15:03:48 -0700265 Exception e) {
Selim Cinek2630dc72017-04-20 15:16:10 -0700266 if (!expectingException) {
267 exceptionHolder.setException(e);
268 }
269 countDownLatch.countDown();
270 }
271
272 @Override
Ned Burnsf81c4c42019-01-07 14:10:43 -0500273 public void onAsyncInflationFinished(NotificationEntry entry,
Ned Burns1a5e22f2019-02-14 15:11:52 -0500274 @NotificationContentInflater.InflationFlag int inflatedFlags) {
Selim Cinek2630dc72017-04-20 15:16:10 -0700275 if (expectingException) {
276 exceptionHolder.setException(new RuntimeException(
277 "Inflation finished even though there should be an error"));
278 }
279 countDownLatch.countDown();
280 }
281 });
282 block.run();
Jason Monk6dceace2018-05-15 20:24:07 -0400283 assertTrue(countDownLatch.await(500, TimeUnit.MILLISECONDS));
Selim Cinek2630dc72017-04-20 15:16:10 -0700284 if (exceptionHolder.mException != null) {
285 throw exceptionHolder.mException;
286 }
287 }
288
289 private static class ExceptionHolder {
290 private Exception mException;
291
292 public void setException(Exception exception) {
293 mException = exception;
294 }
295 }
Selim Cinekd246bed2017-06-19 16:58:35 -0700296
297 private class AsyncFailRemoteView extends RemoteViews {
Jason Monk6dceace2018-05-15 20:24:07 -0400298 Handler mHandler = Handler.createAsync(Looper.getMainLooper());
Selim Cinekd246bed2017-06-19 16:58:35 -0700299
300 public AsyncFailRemoteView(String packageName, int layoutId) {
301 super(packageName, layoutId);
302 }
303
304 @Override
305 public View apply(Context context, ViewGroup parent) {
306 return super.apply(context, parent);
307 }
308
309 @Override
310 public CancellationSignal applyAsync(Context context, ViewGroup parent, Executor executor,
311 OnViewAppliedListener listener, OnClickHandler handler) {
312 mHandler.post(() -> listener.onError(new RuntimeException("Failed to inflate async")));
313 return new CancellationSignal();
314 }
315
316 @Override
317 public CancellationSignal applyAsync(Context context, ViewGroup parent, Executor executor,
318 OnViewAppliedListener listener) {
319 return applyAsync(context, parent, executor, listener, null);
320 }
321 }
Selim Cinek2630dc72017-04-20 15:16:10 -0700322}