blob: 0c5bdeacbd4e7fb82123954386e51a37ad3af4ea [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;
27import android.service.notification.StatusBarNotification;
28import android.support.test.InstrumentationRegistry;
Selim Cinek01d3da62017-04-28 15:03:48 -070029import android.support.test.annotation.UiThreadTest;
Selim Cinek2630dc72017-04-20 15:16:10 -070030import android.support.test.filters.SmallTest;
31import android.support.test.runner.AndroidJUnit4;
32import android.widget.RemoteViews;
33
34import com.android.systemui.R;
35import com.android.systemui.statusbar.ExpandableNotificationRow;
36import com.android.systemui.statusbar.NotificationData;
37import com.android.systemui.statusbar.NotificationTestHelper;
38
39import org.junit.Assert;
40import org.junit.Before;
41import org.junit.Test;
42import org.junit.runner.RunWith;
43
44import java.util.concurrent.CountDownLatch;
Selim Cinek2630dc72017-04-20 15:16:10 -070045
46@SmallTest
47@RunWith(AndroidJUnit4.class)
48public class NotificationInflaterTest {
49
50 private Context mContext;
51 private NotificationInflater mNotificationInflater;
52 private Notification.Builder mBuilder;
53 private ExpandableNotificationRow mRow;
54
55 @Before
56 public void setUp() throws Exception {
57 mContext = InstrumentationRegistry.getTargetContext();
58 mBuilder = new Notification.Builder(mContext).setSmallIcon(
59 R.drawable.ic_person)
60 .setContentTitle("Title")
61 .setContentText("Text")
62 .setStyle(new Notification.BigTextStyle().bigText("big text"));
63 ExpandableNotificationRow row = new NotificationTestHelper(mContext).createRow(
64 mBuilder.build());
65 mRow = spy(row);
66 mNotificationInflater = new NotificationInflater(mRow);
67 mNotificationInflater.setInflationCallback(new NotificationInflater.InflationCallback() {
68 @Override
69 public void handleInflationException(StatusBarNotification notification,
Selim Cinek01d3da62017-04-28 15:03:48 -070070 Exception e) {
Selim Cinek2630dc72017-04-20 15:16:10 -070071 }
72
73 @Override
74 public void onAsyncInflationFinished(NotificationData.Entry entry) {
75 }
76 });
77 }
78
79 @Test
Selim Cinek01d3da62017-04-28 15:03:48 -070080 @UiThreadTest
Selim Cinek2630dc72017-04-20 15:16:10 -070081 public void testIncreasedHeadsUpBeingUsed() {
82 mNotificationInflater.setUsesIncreasedHeadsUpHeight(true);
83 Notification.Builder builder = spy(mBuilder);
84 mNotificationInflater.inflateNotificationViews(FLAG_REINFLATE_ALL, builder, mContext);
85 verify(builder).createHeadsUpContentView(true);
86 }
87
88 @Test
Selim Cinek01d3da62017-04-28 15:03:48 -070089 @UiThreadTest
Selim Cinek2630dc72017-04-20 15:16:10 -070090 public void testIncreasedHeightBeingUsed() {
91 mNotificationInflater.setUsesIncreasedHeight(true);
92 Notification.Builder builder = spy(mBuilder);
93 mNotificationInflater.inflateNotificationViews(FLAG_REINFLATE_ALL, builder, mContext);
94 verify(builder).createContentView(true);
95 }
96
97 @Test
98 public void testInflationCallsUpdated() throws Exception {
99 runThenWaitForInflation(() -> mNotificationInflater.inflateNotificationViews(),
100 mNotificationInflater);
101 verify(mRow).onNotificationUpdated();
102 }
103
104 @Test
105 public void testInflationCallsOnlyRightMethod() throws Exception {
106 mRow.getPrivateLayout().removeAllViews();
107 mRow.getEntry().cachedBigContentView = null;
108 runThenWaitForInflation(() -> mNotificationInflater.inflateNotificationViews(
109 NotificationInflater.FLAG_REINFLATE_EXPANDED_VIEW), mNotificationInflater);
110 Assert.assertTrue(mRow.getPrivateLayout().getChildCount() == 1);
111 Assert.assertTrue(mRow.getPrivateLayout().getChildAt(0)
112 == mRow.getPrivateLayout().getExpandedChild());
113 verify(mRow).onNotificationUpdated();
114 }
115
116 @Test
117 public void testInflationThrowsErrorDoesntCallUpdated() throws Exception {
118 mRow.getPrivateLayout().removeAllViews();
119 mRow.getStatusBarNotification().getNotification().contentView
Selim Cinekdc1231c2017-04-27 17:30:50 -0700120 = new RemoteViews(mContext.getPackageName(), R.layout.status_bar);
Selim Cinek2630dc72017-04-20 15:16:10 -0700121 runThenWaitForInflation(() -> mNotificationInflater.inflateNotificationViews(),
122 true /* expectingException */, mNotificationInflater);
123 Assert.assertTrue(mRow.getPrivateLayout().getChildCount() == 0);
124 verify(mRow, times(0)).onNotificationUpdated();
125 }
126
Selim Cinekdc1231c2017-04-27 17:30:50 -0700127 @Test
128 public void testAsyncTaskRemoved() throws Exception {
Selim Cinek0f66a4c2017-04-28 19:26:28 -0700129 mRow.getEntry().abortTask();
Selim Cinekdc1231c2017-04-27 17:30:50 -0700130 runThenWaitForInflation(() -> mNotificationInflater.inflateNotificationViews(),
131 mNotificationInflater);
Selim Cinek01d3da62017-04-28 15:03:48 -0700132 Assert.assertNull(mRow.getEntry().getRunningTask() );
Selim Cinekdc1231c2017-04-27 17:30:50 -0700133 }
134
Selim Cinek2630dc72017-04-20 15:16:10 -0700135 public static void runThenWaitForInflation(Runnable block,
136 NotificationInflater inflater) throws Exception {
137 runThenWaitForInflation(block, false /* expectingException */, inflater);
138 }
139
140 private static void runThenWaitForInflation(Runnable block, boolean expectingException,
141 NotificationInflater inflater) throws Exception {
142 com.android.systemui.util.Assert.isNotMainThread();
143 CountDownLatch countDownLatch = new CountDownLatch(1);
144 final ExceptionHolder exceptionHolder = new ExceptionHolder();
145 inflater.setInflationCallback(new NotificationInflater.InflationCallback() {
146 @Override
147 public void handleInflationException(StatusBarNotification notification,
Selim Cinek01d3da62017-04-28 15:03:48 -0700148 Exception e) {
Selim Cinek2630dc72017-04-20 15:16:10 -0700149 if (!expectingException) {
150 exceptionHolder.setException(e);
151 }
152 countDownLatch.countDown();
153 }
154
155 @Override
156 public void onAsyncInflationFinished(NotificationData.Entry entry) {
157 if (expectingException) {
158 exceptionHolder.setException(new RuntimeException(
159 "Inflation finished even though there should be an error"));
160 }
161 countDownLatch.countDown();
162 }
163 });
164 block.run();
Selim Cinek454a5fc42017-05-11 16:56:55 -0700165 countDownLatch.await();
Selim Cinek2630dc72017-04-20 15:16:10 -0700166 if (exceptionHolder.mException != null) {
167 throw exceptionHolder.mException;
168 }
169 }
170
171 private static class ExceptionHolder {
172 private Exception mException;
173
174 public void setException(Exception exception) {
175 mException = exception;
176 }
177 }
178}