Selim Cinek | 2630dc7 | 2017-04-20 15:16:10 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.systemui.statusbar.notification; |
| 18 | |
| 19 | import static com.android.systemui.statusbar.notification.NotificationInflater.FLAG_REINFLATE_ALL; |
| 20 | |
| 21 | import static org.mockito.Mockito.spy; |
| 22 | import static org.mockito.Mockito.times; |
| 23 | import static org.mockito.Mockito.verify; |
| 24 | |
| 25 | import android.app.Notification; |
| 26 | import android.content.Context; |
Selim Cinek | d246bed | 2017-06-19 16:58:35 -0700 | [diff] [blame] | 27 | import android.os.CancellationSignal; |
| 28 | import android.os.Handler; |
| 29 | import android.os.Looper; |
Selim Cinek | 2630dc7 | 2017-04-20 15:16:10 -0700 | [diff] [blame] | 30 | import android.service.notification.StatusBarNotification; |
Selim Cinek | 01d3da6 | 2017-04-28 15:03:48 -0700 | [diff] [blame] | 31 | import android.support.test.annotation.UiThreadTest; |
Geoffrey Pitsch | 70950de | 2017-05-23 17:13:38 -0400 | [diff] [blame] | 32 | import android.support.test.filters.FlakyTest; |
Selim Cinek | 2630dc7 | 2017-04-20 15:16:10 -0700 | [diff] [blame] | 33 | import android.support.test.filters.SmallTest; |
| 34 | import android.support.test.runner.AndroidJUnit4; |
Selim Cinek | d246bed | 2017-06-19 16:58:35 -0700 | [diff] [blame] | 35 | import android.view.View; |
| 36 | import android.view.ViewGroup; |
Selim Cinek | 2630dc7 | 2017-04-20 15:16:10 -0700 | [diff] [blame] | 37 | import android.widget.RemoteViews; |
| 38 | |
| 39 | import com.android.systemui.R; |
Jason Monk | fba8faf | 2017-05-23 10:42:59 -0400 | [diff] [blame] | 40 | import com.android.systemui.SysuiTestCase; |
Selim Cinek | 2630dc7 | 2017-04-20 15:16:10 -0700 | [diff] [blame] | 41 | import com.android.systemui.statusbar.ExpandableNotificationRow; |
Jason Monk | b05395f | 2017-07-11 10:05:03 -0400 | [diff] [blame] | 42 | import com.android.systemui.statusbar.InflationTask; |
Selim Cinek | 2630dc7 | 2017-04-20 15:16:10 -0700 | [diff] [blame] | 43 | import com.android.systemui.statusbar.NotificationData; |
| 44 | import com.android.systemui.statusbar.NotificationTestHelper; |
| 45 | |
| 46 | import org.junit.Assert; |
| 47 | import org.junit.Before; |
Geoffrey Pitsch | 351a321 | 2017-05-22 15:20:20 -0400 | [diff] [blame] | 48 | import org.junit.Ignore; |
Selim Cinek | 2630dc7 | 2017-04-20 15:16:10 -0700 | [diff] [blame] | 49 | import org.junit.Test; |
| 50 | import org.junit.runner.RunWith; |
| 51 | |
Selim Cinek | d246bed | 2017-06-19 16:58:35 -0700 | [diff] [blame] | 52 | import java.util.HashMap; |
Selim Cinek | 2630dc7 | 2017-04-20 15:16:10 -0700 | [diff] [blame] | 53 | import java.util.concurrent.CountDownLatch; |
Selim Cinek | d246bed | 2017-06-19 16:58:35 -0700 | [diff] [blame] | 54 | import java.util.concurrent.Executor; |
Selim Cinek | 2630dc7 | 2017-04-20 15:16:10 -0700 | [diff] [blame] | 55 | |
| 56 | @SmallTest |
| 57 | @RunWith(AndroidJUnit4.class) |
Geoffrey Pitsch | 70950de | 2017-05-23 17:13:38 -0400 | [diff] [blame] | 58 | @FlakyTest |
Jason Monk | fba8faf | 2017-05-23 10:42:59 -0400 | [diff] [blame] | 59 | public class NotificationInflaterTest extends SysuiTestCase { |
Selim Cinek | 2630dc7 | 2017-04-20 15:16:10 -0700 | [diff] [blame] | 60 | |
Selim Cinek | 2630dc7 | 2017-04-20 15:16:10 -0700 | [diff] [blame] | 61 | private NotificationInflater mNotificationInflater; |
| 62 | private Notification.Builder mBuilder; |
| 63 | private ExpandableNotificationRow mRow; |
| 64 | |
| 65 | @Before |
| 66 | public void setUp() throws Exception { |
Selim Cinek | 2630dc7 | 2017-04-20 15:16:10 -0700 | [diff] [blame] | 67 | 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 Cinek | 01d3da6 | 2017-04-28 15:03:48 -0700 | [diff] [blame] | 79 | Exception e) { |
Selim Cinek | 2630dc7 | 2017-04-20 15:16:10 -0700 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | @Override |
| 83 | public void onAsyncInflationFinished(NotificationData.Entry entry) { |
| 84 | } |
| 85 | }); |
| 86 | } |
| 87 | |
| 88 | @Test |
Selim Cinek | 01d3da6 | 2017-04-28 15:03:48 -0700 | [diff] [blame] | 89 | @UiThreadTest |
Selim Cinek | 2630dc7 | 2017-04-20 15:16:10 -0700 | [diff] [blame] | 90 | 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 Cinek | 01d3da6 | 2017-04-28 15:03:48 -0700 | [diff] [blame] | 98 | @UiThreadTest |
Selim Cinek | 2630dc7 | 2017-04-20 15:16:10 -0700 | [diff] [blame] | 99 | 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 Cinek | dc1231c | 2017-04-27 17:30:50 -0700 | [diff] [blame] | 129 | = new RemoteViews(mContext.getPackageName(), R.layout.status_bar); |
Selim Cinek | 2630dc7 | 2017-04-20 15:16:10 -0700 | [diff] [blame] | 130 | runThenWaitForInflation(() -> mNotificationInflater.inflateNotificationViews(), |
| 131 | true /* expectingException */, mNotificationInflater); |
| 132 | Assert.assertTrue(mRow.getPrivateLayout().getChildCount() == 0); |
| 133 | verify(mRow, times(0)).onNotificationUpdated(); |
| 134 | } |
| 135 | |
Selim Cinek | dc1231c | 2017-04-27 17:30:50 -0700 | [diff] [blame] | 136 | @Test |
| 137 | public void testAsyncTaskRemoved() throws Exception { |
Selim Cinek | 0f66a4c | 2017-04-28 19:26:28 -0700 | [diff] [blame] | 138 | mRow.getEntry().abortTask(); |
Selim Cinek | dc1231c | 2017-04-27 17:30:50 -0700 | [diff] [blame] | 139 | runThenWaitForInflation(() -> mNotificationInflater.inflateNotificationViews(), |
| 140 | mNotificationInflater); |
Selim Cinek | 67ff248 | 2017-05-25 10:27:28 -0700 | [diff] [blame] | 141 | 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 Cinek | d246bed | 2017-06-19 16:58:35 -0700 | [diff] [blame] | 151 | @Test |
Jason Monk | b05395f | 2017-07-11 10:05:03 -0400 | [diff] [blame] | 152 | @Ignore |
Selim Cinek | d246bed | 2017-06-19 16:58:35 -0700 | [diff] [blame] | 153 | 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 Cinek | 67ff248 | 2017-05-25 10:27:28 -0700 | [diff] [blame] | 187 | |
| 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 Cinek | dc1231c | 2017-04-27 17:30:50 -0700 | [diff] [blame] | 200 | } |
| 201 | |
Selim Cinek | fc8073c | 2017-08-16 17:50:20 -0700 | [diff] [blame] | 202 | @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 Cinek | 2630dc7 | 2017-04-20 15:16:10 -0700 | [diff] [blame] | 214 | 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 Cinek | 01d3da6 | 2017-04-28 15:03:48 -0700 | [diff] [blame] | 227 | Exception e) { |
Selim Cinek | 2630dc7 | 2017-04-20 15:16:10 -0700 | [diff] [blame] | 228 | 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 Cinek | 454a5fc4 | 2017-05-11 16:56:55 -0700 | [diff] [blame] | 244 | countDownLatch.await(); |
Selim Cinek | 2630dc7 | 2017-04-20 15:16:10 -0700 | [diff] [blame] | 245 | 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 Cinek | d246bed | 2017-06-19 16:58:35 -0700 | [diff] [blame] | 257 | |
| 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 Cinek | 2630dc7 | 2017-04-20 15:16:10 -0700 | [diff] [blame] | 283 | } |