blob: 2ec125e8a43b0cd966fded4e52c4683539e5bfe2 [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;
Ned Burns1a5e22f2019-02-14 15:11:52 -050020import static com.android.systemui.statusbar.notification.row.NotificationContentInflater.FLAG_CONTENT_VIEW_EXPANDED;
21import static com.android.systemui.statusbar.notification.row.NotificationContentInflater.FLAG_CONTENT_VIEW_HEADS_UP;
22import static com.android.systemui.statusbar.notification.row.NotificationContentInflater.FLAG_CONTENT_VIEW_PUBLIC;
Selim Cinek2630dc72017-04-20 15:16:10 -070023
Kevind4660b22018-09-27 10:57:35 -070024import static org.junit.Assert.assertEquals;
25import static org.junit.Assert.assertNotNull;
26import static org.junit.Assert.assertNull;
Jason Monk6dceace2018-05-15 20:24:07 -040027import static org.junit.Assert.assertTrue;
Selim Cinek2630dc72017-04-20 15:16:10 -070028import static org.mockito.Mockito.spy;
29import static org.mockito.Mockito.times;
30import static org.mockito.Mockito.verify;
31
32import android.app.Notification;
33import android.content.Context;
Selim Cinekd246bed2017-06-19 16:58:35 -070034import android.os.CancellationSignal;
35import android.os.Handler;
36import android.os.Looper;
Selim Cinek2630dc72017-04-20 15:16:10 -070037import android.service.notification.StatusBarNotification;
Jason Monk6dceace2018-05-15 20:24:07 -040038import android.testing.AndroidTestingRunner;
39import android.testing.TestableLooper.RunWithLooper;
Kevind4660b22018-09-27 10:57:35 -070040import android.util.ArrayMap;
Selim Cinekd246bed2017-06-19 16:58:35 -070041import android.view.View;
42import android.view.ViewGroup;
Selim Cinek2630dc72017-04-20 15:16:10 -070043import android.widget.RemoteViews;
44
Brett Chabot84151d92019-02-27 15:37:59 -080045import androidx.test.filters.SmallTest;
46
Selim Cinek2630dc72017-04-20 15:16:10 -070047import 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(
Selim Cinek69d11c22019-06-10 23:52:32 -0700103 true /* inflateSynchronously */,
Ned Burns342c3a02019-02-15 18:08:39 -0500104 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(
Selim Cinek69d11c22019-06-10 23:52:32 -0700115 true /* inflateSynchronously */,
Ned Burns342c3a02019-02-15 18:08:39 -0500116 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());
Selim Cinek2630dc72017-04-20 15:16:10 -0700137 verify(mRow).onNotificationUpdated();
138 }
139
140 @Test
141 public void testInflationThrowsErrorDoesntCallUpdated() throws Exception {
142 mRow.getPrivateLayout().removeAllViews();
143 mRow.getStatusBarNotification().getNotification().contentView
Selim Cinekdc1231c2017-04-27 17:30:50 -0700144 = new RemoteViews(mContext.getPackageName(), R.layout.status_bar);
Selim Cinek2630dc72017-04-20 15:16:10 -0700145 runThenWaitForInflation(() -> mNotificationInflater.inflateNotificationViews(),
146 true /* expectingException */, mNotificationInflater);
Jason Monk6dceace2018-05-15 20:24:07 -0400147 assertTrue(mRow.getPrivateLayout().getChildCount() == 0);
Selim Cinek2630dc72017-04-20 15:16:10 -0700148 verify(mRow, times(0)).onNotificationUpdated();
149 }
150
Selim Cinekdc1231c2017-04-27 17:30:50 -0700151 @Test
152 public void testAsyncTaskRemoved() throws Exception {
Selim Cinek0f66a4c2017-04-28 19:26:28 -0700153 mRow.getEntry().abortTask();
Selim Cinekdc1231c2017-04-27 17:30:50 -0700154 runThenWaitForInflation(() -> mNotificationInflater.inflateNotificationViews(),
155 mNotificationInflater);
Selim Cinek67ff2482017-05-25 10:27:28 -0700156 verify(mRow).onNotificationUpdated();
157 }
158
159 @Test
160 public void testRemovedNotInflated() throws Exception {
161 mRow.setRemoved();
Selim Cinek69d11c22019-06-10 23:52:32 -0700162 mNotificationInflater.setInflateSynchronously(true);
Selim Cinek67ff2482017-05-25 10:27:28 -0700163 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,
Selim Cinekc3fec682019-06-06 18:11:07 -0700179 new ArrayMap() /* cachedContentViews */, mRow,
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() {
Kevin38ce6fa2018-10-17 16:00:14 -0700211 mNotificationInflater.updateInflationFlag(FLAG_CONTENT_VIEW_PUBLIC, true);
212 mNotificationInflater.updateNeedsRedaction(true);
213
Ned Burns1a5e22f2019-02-14 15:11:52 -0500214 NotificationContentInflater.AsyncInflationTask asyncInflationTask =
215 (NotificationContentInflater.AsyncInflationTask) mRow.getEntry().getRunningTask();
Selim Cinekc3fec682019-06-06 18:11:07 -0700216 assertEquals(FLAG_CONTENT_VIEW_PUBLIC, asyncInflationTask.getReInflateFlags());
Kevin38ce6fa2018-10-17 16:00:14 -0700217 asyncInflationTask.abort();
218 }
219
Selim Cinek21f33662017-09-08 13:24:21 -0700220 /* Cancelling requires us to be on the UI thread otherwise we might have a race */
Selim Cinek67ff2482017-05-25 10:27:28 -0700221 @Test
Kevind4660b22018-09-27 10:57:35 -0700222 public void testSupersedesExistingTask() {
Kevind5022f92018-10-08 18:30:26 -0700223 mNotificationInflater.addInflationFlags(FLAG_CONTENT_VIEW_ALL);
Selim Cinek67ff2482017-05-25 10:27:28 -0700224 mNotificationInflater.inflateNotificationViews();
Kevind4660b22018-09-27 10:57:35 -0700225
226 // Trigger inflation of content and expanded only.
Selim Cinek67ff2482017-05-25 10:27:28 -0700227 mNotificationInflater.setIsLowPriority(true);
228 mNotificationInflater.setIsChildInGroup(true);
Kevind4660b22018-09-27 10:57:35 -0700229
Selim Cinek67ff2482017-05-25 10:27:28 -0700230 InflationTask runningTask = mRow.getEntry().getRunningTask();
Ned Burns1a5e22f2019-02-14 15:11:52 -0500231 NotificationContentInflater.AsyncInflationTask asyncInflationTask =
232 (NotificationContentInflater.AsyncInflationTask) runningTask;
Kevind4660b22018-09-27 10:57:35 -0700233 assertEquals("Successive inflations don't inherit the previous flags!",
Kevin38ce6fa2018-10-17 16:00:14 -0700234 FLAG_CONTENT_VIEW_ALL, asyncInflationTask.getReInflateFlags());
Selim Cinek67ff2482017-05-25 10:27:28 -0700235 runningTask.abort();
Selim Cinekdc1231c2017-04-27 17:30:50 -0700236 }
237
Selim Cinekfc8073c2017-08-16 17:50:20 -0700238 @Test
239 public void doesntReapplyDisallowedRemoteView() throws Exception {
240 mBuilder.setStyle(new Notification.MediaStyle());
241 RemoteViews mediaView = mBuilder.createContentView();
242 mBuilder.setStyle(new Notification.DecoratedCustomViewStyle());
243 mBuilder.setCustomContentView(new RemoteViews(getContext().getPackageName(),
244 R.layout.custom_view_dark));
245 RemoteViews decoratedMediaView = mBuilder.createContentView();
246 Assert.assertFalse("The decorated media style doesn't allow a view to be reapplied!",
Ned Burns1a5e22f2019-02-14 15:11:52 -0500247 NotificationContentInflater.canReapplyRemoteView(mediaView, decoratedMediaView));
Selim Cinekfc8073c2017-08-16 17:50:20 -0700248 }
249
Selim Cinek2630dc72017-04-20 15:16:10 -0700250 public static void runThenWaitForInflation(Runnable block,
Ned Burns1a5e22f2019-02-14 15:11:52 -0500251 NotificationContentInflater inflater) throws Exception {
Selim Cinek2630dc72017-04-20 15:16:10 -0700252 runThenWaitForInflation(block, false /* expectingException */, inflater);
253 }
254
255 private static void runThenWaitForInflation(Runnable block, boolean expectingException,
Ned Burns1a5e22f2019-02-14 15:11:52 -0500256 NotificationContentInflater inflater) throws Exception {
Selim Cinek2630dc72017-04-20 15:16:10 -0700257 CountDownLatch countDownLatch = new CountDownLatch(1);
258 final ExceptionHolder exceptionHolder = new ExceptionHolder();
Ned Burns342c3a02019-02-15 18:08:39 -0500259 inflater.setInflateSynchronously(true);
Ned Burns1a5e22f2019-02-14 15:11:52 -0500260 inflater.setInflationCallback(new InflationCallback() {
Selim Cinek2630dc72017-04-20 15:16:10 -0700261 @Override
262 public void handleInflationException(StatusBarNotification notification,
Selim Cinek01d3da62017-04-28 15:03:48 -0700263 Exception e) {
Selim Cinek2630dc72017-04-20 15:16:10 -0700264 if (!expectingException) {
265 exceptionHolder.setException(e);
266 }
267 countDownLatch.countDown();
268 }
269
270 @Override
Ned Burnsf81c4c42019-01-07 14:10:43 -0500271 public void onAsyncInflationFinished(NotificationEntry entry,
Ned Burns1a5e22f2019-02-14 15:11:52 -0500272 @NotificationContentInflater.InflationFlag int inflatedFlags) {
Selim Cinek2630dc72017-04-20 15:16:10 -0700273 if (expectingException) {
274 exceptionHolder.setException(new RuntimeException(
275 "Inflation finished even though there should be an error"));
276 }
277 countDownLatch.countDown();
278 }
279 });
280 block.run();
Jason Monk6dceace2018-05-15 20:24:07 -0400281 assertTrue(countDownLatch.await(500, TimeUnit.MILLISECONDS));
Selim Cinek2630dc72017-04-20 15:16:10 -0700282 if (exceptionHolder.mException != null) {
283 throw exceptionHolder.mException;
284 }
285 }
286
287 private static class ExceptionHolder {
288 private Exception mException;
289
290 public void setException(Exception exception) {
291 mException = exception;
292 }
293 }
Selim Cinekd246bed2017-06-19 16:58:35 -0700294
295 private class AsyncFailRemoteView extends RemoteViews {
Jason Monk6dceace2018-05-15 20:24:07 -0400296 Handler mHandler = Handler.createAsync(Looper.getMainLooper());
Selim Cinekd246bed2017-06-19 16:58:35 -0700297
298 public AsyncFailRemoteView(String packageName, int layoutId) {
299 super(packageName, layoutId);
300 }
301
302 @Override
303 public View apply(Context context, ViewGroup parent) {
304 return super.apply(context, parent);
305 }
306
307 @Override
308 public CancellationSignal applyAsync(Context context, ViewGroup parent, Executor executor,
309 OnViewAppliedListener listener, OnClickHandler handler) {
310 mHandler.post(() -> listener.onError(new RuntimeException("Failed to inflate async")));
311 return new CancellationSignal();
312 }
313
314 @Override
315 public CancellationSignal applyAsync(Context context, ViewGroup parent, Executor executor,
316 OnViewAppliedListener listener) {
317 return applyAsync(context, parent, executor, listener, null);
318 }
319 }
Selim Cinek2630dc72017-04-20 15:16:10 -0700320}