blob: 12a4399fab2e4579d9625a52665a7a4fbc0df7e9 [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
17package com.android.systemui.statusbar.notification;
18
19import static com.android.systemui.statusbar.notification.NotificationInflater.FLAG_REINFLATE_ALL;
20
21import static org.mockito.Mockito.spy;
22import static org.mockito.Mockito.times;
23import static org.mockito.Mockito.verify;
24
25import android.app.Notification;
26import android.content.Context;
Selim Cinekd246bed2017-06-19 16:58:35 -070027import android.os.CancellationSignal;
28import android.os.Handler;
29import android.os.Looper;
Selim Cinek2630dc72017-04-20 15:16:10 -070030import android.service.notification.StatusBarNotification;
Selim Cinek01d3da62017-04-28 15:03:48 -070031import android.support.test.annotation.UiThreadTest;
Geoffrey Pitsch70950de2017-05-23 17:13:38 -040032import android.support.test.filters.FlakyTest;
Selim Cinek2630dc72017-04-20 15:16:10 -070033import android.support.test.filters.SmallTest;
34import android.support.test.runner.AndroidJUnit4;
Selim Cinekd246bed2017-06-19 16:58:35 -070035import android.view.View;
36import android.view.ViewGroup;
Selim Cinek2630dc72017-04-20 15:16:10 -070037import android.widget.RemoteViews;
38
39import com.android.systemui.R;
Jason Monkfba8faf2017-05-23 10:42:59 -040040import com.android.systemui.SysuiTestCase;
Selim Cinek2630dc72017-04-20 15:16:10 -070041import com.android.systemui.statusbar.ExpandableNotificationRow;
Jason Monkb05395f2017-07-11 10:05:03 -040042import com.android.systemui.statusbar.InflationTask;
Selim Cinek2630dc72017-04-20 15:16:10 -070043import com.android.systemui.statusbar.NotificationData;
44import com.android.systemui.statusbar.NotificationTestHelper;
45
46import org.junit.Assert;
47import org.junit.Before;
Geoffrey Pitsch351a3212017-05-22 15:20:20 -040048import org.junit.Ignore;
Selim Cinek2630dc72017-04-20 15:16:10 -070049import org.junit.Test;
50import org.junit.runner.RunWith;
51
Selim Cinekd246bed2017-06-19 16:58:35 -070052import java.util.HashMap;
Selim Cinek2630dc72017-04-20 15:16:10 -070053import java.util.concurrent.CountDownLatch;
Selim Cinekd246bed2017-06-19 16:58:35 -070054import java.util.concurrent.Executor;
Selim Cinek2630dc72017-04-20 15:16:10 -070055
56@SmallTest
57@RunWith(AndroidJUnit4.class)
Geoffrey Pitsch70950de2017-05-23 17:13:38 -040058@FlakyTest
Jason Monkfba8faf2017-05-23 10:42:59 -040059public class NotificationInflaterTest extends SysuiTestCase {
Selim Cinek2630dc72017-04-20 15:16:10 -070060
Selim Cinek2630dc72017-04-20 15:16:10 -070061 private NotificationInflater mNotificationInflater;
62 private Notification.Builder mBuilder;
63 private ExpandableNotificationRow mRow;
64
65 @Before
66 public void setUp() throws Exception {
Selim Cinek2630dc72017-04-20 15:16:10 -070067 mBuilder = new Notification.Builder(mContext).setSmallIcon(
68 R.drawable.ic_person)
69 .setContentTitle("Title")
70 .setContentText("Text")
71 .setStyle(new Notification.BigTextStyle().bigText("big text"));
72 ExpandableNotificationRow row = new NotificationTestHelper(mContext).createRow(
73 mBuilder.build());
74 mRow = spy(row);
75 mNotificationInflater = new NotificationInflater(mRow);
76 mNotificationInflater.setInflationCallback(new NotificationInflater.InflationCallback() {
77 @Override
78 public void handleInflationException(StatusBarNotification notification,
Selim Cinek01d3da62017-04-28 15:03:48 -070079 Exception e) {
Selim Cinek2630dc72017-04-20 15:16:10 -070080 }
81
82 @Override
83 public void onAsyncInflationFinished(NotificationData.Entry entry) {
84 }
85 });
86 }
87
88 @Test
Selim Cinek01d3da62017-04-28 15:03:48 -070089 @UiThreadTest
Selim Cinek2630dc72017-04-20 15:16:10 -070090 public void testIncreasedHeadsUpBeingUsed() {
91 mNotificationInflater.setUsesIncreasedHeadsUpHeight(true);
92 Notification.Builder builder = spy(mBuilder);
93 mNotificationInflater.inflateNotificationViews(FLAG_REINFLATE_ALL, builder, mContext);
94 verify(builder).createHeadsUpContentView(true);
95 }
96
97 @Test
Selim Cinek01d3da62017-04-28 15:03:48 -070098 @UiThreadTest
Selim Cinek2630dc72017-04-20 15:16:10 -070099 public void testIncreasedHeightBeingUsed() {
100 mNotificationInflater.setUsesIncreasedHeight(true);
101 Notification.Builder builder = spy(mBuilder);
102 mNotificationInflater.inflateNotificationViews(FLAG_REINFLATE_ALL, builder, mContext);
103 verify(builder).createContentView(true);
104 }
105
106 @Test
107 public void testInflationCallsUpdated() throws Exception {
108 runThenWaitForInflation(() -> mNotificationInflater.inflateNotificationViews(),
109 mNotificationInflater);
110 verify(mRow).onNotificationUpdated();
111 }
112
113 @Test
114 public void testInflationCallsOnlyRightMethod() throws Exception {
115 mRow.getPrivateLayout().removeAllViews();
116 mRow.getEntry().cachedBigContentView = null;
117 runThenWaitForInflation(() -> mNotificationInflater.inflateNotificationViews(
118 NotificationInflater.FLAG_REINFLATE_EXPANDED_VIEW), mNotificationInflater);
119 Assert.assertTrue(mRow.getPrivateLayout().getChildCount() == 1);
120 Assert.assertTrue(mRow.getPrivateLayout().getChildAt(0)
121 == mRow.getPrivateLayout().getExpandedChild());
122 verify(mRow).onNotificationUpdated();
123 }
124
125 @Test
126 public void testInflationThrowsErrorDoesntCallUpdated() throws Exception {
127 mRow.getPrivateLayout().removeAllViews();
128 mRow.getStatusBarNotification().getNotification().contentView
Selim Cinekdc1231c2017-04-27 17:30:50 -0700129 = new RemoteViews(mContext.getPackageName(), R.layout.status_bar);
Selim Cinek2630dc72017-04-20 15:16:10 -0700130 runThenWaitForInflation(() -> mNotificationInflater.inflateNotificationViews(),
131 true /* expectingException */, mNotificationInflater);
132 Assert.assertTrue(mRow.getPrivateLayout().getChildCount() == 0);
133 verify(mRow, times(0)).onNotificationUpdated();
134 }
135
Selim Cinekdc1231c2017-04-27 17:30:50 -0700136 @Test
137 public void testAsyncTaskRemoved() throws Exception {
Selim Cinek0f66a4c2017-04-28 19:26:28 -0700138 mRow.getEntry().abortTask();
Selim Cinekdc1231c2017-04-27 17:30:50 -0700139 runThenWaitForInflation(() -> mNotificationInflater.inflateNotificationViews(),
140 mNotificationInflater);
Selim Cinek67ff2482017-05-25 10:27:28 -0700141 verify(mRow).onNotificationUpdated();
142 }
143
144 @Test
145 public void testRemovedNotInflated() throws Exception {
146 mRow.setRemoved();
147 mNotificationInflater.inflateNotificationViews();
148 Assert.assertNull(mRow.getEntry().getRunningTask());
149 }
150
Selim Cinekd246bed2017-06-19 16:58:35 -0700151 @Test
Jason Monkb05395f2017-07-11 10:05:03 -0400152 @Ignore
Selim Cinekd246bed2017-06-19 16:58:35 -0700153 public void testInflationIsRetriedIfAsyncFails() throws Exception {
154 NotificationInflater.InflationProgress result =
155 new NotificationInflater.InflationProgress();
156 result.packageContext = mContext;
157 CountDownLatch countDownLatch = new CountDownLatch(1);
158 NotificationInflater.applyRemoteView(result,
159 NotificationInflater.FLAG_REINFLATE_EXPANDED_VIEW, 0, mRow,
160 false /* redactAmbient */, true /* isNewView */, new RemoteViews.OnClickHandler(),
161 new NotificationInflater.InflationCallback() {
162 @Override
163 public void handleInflationException(StatusBarNotification notification,
164 Exception e) {
165 countDownLatch.countDown();
166 throw new RuntimeException("No Exception expected");
167 }
168
169 @Override
170 public void onAsyncInflationFinished(NotificationData.Entry entry) {
171 countDownLatch.countDown();
172 }
173 }, mRow.getEntry(), mRow.getPrivateLayout(), null, null, new HashMap<>(),
174 new NotificationInflater.ApplyCallback() {
175 @Override
176 public void setResultView(View v) {
177 }
178
179 @Override
180 public RemoteViews getRemoteView() {
181 return new AsyncFailRemoteView(mContext.getPackageName(),
182 R.layout.custom_view_dark);
183 }
184 });
185 countDownLatch.await();
186 }
Selim Cinek67ff2482017-05-25 10:27:28 -0700187
188 @Test
189 public void testSupersedesExistingTask() throws Exception {
190 mNotificationInflater.inflateNotificationViews();
191 mNotificationInflater.setIsLowPriority(true);
192 mNotificationInflater.setIsChildInGroup(true);
193 InflationTask runningTask = mRow.getEntry().getRunningTask();
194 NotificationInflater.AsyncInflationTask asyncInflationTask =
195 (NotificationInflater.AsyncInflationTask) runningTask;
196 Assert.assertSame("Successive inflations don't inherit the previous flags!",
197 asyncInflationTask.getReInflateFlags(),
198 NotificationInflater.FLAG_REINFLATE_ALL);
199 runningTask.abort();
Selim Cinekdc1231c2017-04-27 17:30:50 -0700200 }
201
Selim Cinekfc8073c2017-08-16 17:50:20 -0700202 @Test
203 public void doesntReapplyDisallowedRemoteView() throws Exception {
204 mBuilder.setStyle(new Notification.MediaStyle());
205 RemoteViews mediaView = mBuilder.createContentView();
206 mBuilder.setStyle(new Notification.DecoratedCustomViewStyle());
207 mBuilder.setCustomContentView(new RemoteViews(getContext().getPackageName(),
208 R.layout.custom_view_dark));
209 RemoteViews decoratedMediaView = mBuilder.createContentView();
210 Assert.assertFalse("The decorated media style doesn't allow a view to be reapplied!",
211 NotificationInflater.canReapplyRemoteView(mediaView, decoratedMediaView));
212 }
213
Selim Cinek2630dc72017-04-20 15:16:10 -0700214 public static void runThenWaitForInflation(Runnable block,
215 NotificationInflater inflater) throws Exception {
216 runThenWaitForInflation(block, false /* expectingException */, inflater);
217 }
218
219 private static void runThenWaitForInflation(Runnable block, boolean expectingException,
220 NotificationInflater inflater) throws Exception {
221 com.android.systemui.util.Assert.isNotMainThread();
222 CountDownLatch countDownLatch = new CountDownLatch(1);
223 final ExceptionHolder exceptionHolder = new ExceptionHolder();
224 inflater.setInflationCallback(new NotificationInflater.InflationCallback() {
225 @Override
226 public void handleInflationException(StatusBarNotification notification,
Selim Cinek01d3da62017-04-28 15:03:48 -0700227 Exception e) {
Selim Cinek2630dc72017-04-20 15:16:10 -0700228 if (!expectingException) {
229 exceptionHolder.setException(e);
230 }
231 countDownLatch.countDown();
232 }
233
234 @Override
235 public void onAsyncInflationFinished(NotificationData.Entry entry) {
236 if (expectingException) {
237 exceptionHolder.setException(new RuntimeException(
238 "Inflation finished even though there should be an error"));
239 }
240 countDownLatch.countDown();
241 }
242 });
243 block.run();
Selim Cinek454a5fc42017-05-11 16:56:55 -0700244 countDownLatch.await();
Selim Cinek2630dc72017-04-20 15:16:10 -0700245 if (exceptionHolder.mException != null) {
246 throw exceptionHolder.mException;
247 }
248 }
249
250 private static class ExceptionHolder {
251 private Exception mException;
252
253 public void setException(Exception exception) {
254 mException = exception;
255 }
256 }
Selim Cinekd246bed2017-06-19 16:58:35 -0700257
258 private class AsyncFailRemoteView extends RemoteViews {
259 Handler mHandler = new Handler(Looper.getMainLooper());
260
261 public AsyncFailRemoteView(String packageName, int layoutId) {
262 super(packageName, layoutId);
263 }
264
265 @Override
266 public View apply(Context context, ViewGroup parent) {
267 return super.apply(context, parent);
268 }
269
270 @Override
271 public CancellationSignal applyAsync(Context context, ViewGroup parent, Executor executor,
272 OnViewAppliedListener listener, OnClickHandler handler) {
273 mHandler.post(() -> listener.onError(new RuntimeException("Failed to inflate async")));
274 return new CancellationSignal();
275 }
276
277 @Override
278 public CancellationSignal applyAsync(Context context, ViewGroup parent, Executor executor,
279 OnViewAppliedListener listener) {
280 return applyAsync(context, parent, executor, listener, null);
281 }
282 }
Selim Cinek2630dc72017-04-20 15:16:10 -0700283}