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 | |
Rohan Shah | 20790b8 | 2018-07-02 17:21:04 -0700 | [diff] [blame] | 17 | package com.android.systemui.statusbar.notification.row; |
Selim Cinek | 2630dc7 | 2017-04-20 15:16:10 -0700 | [diff] [blame] | 18 | |
Ned Burns | 1a5e22f | 2019-02-14 15:11:52 -0500 | [diff] [blame] | 19 | import static com.android.systemui.statusbar.notification.row.NotificationContentInflater.FLAG_CONTENT_VIEW_ALL; |
| 20 | import static com.android.systemui.statusbar.notification.row.NotificationContentInflater.FLAG_CONTENT_VIEW_AMBIENT; |
| 21 | import static com.android.systemui.statusbar.notification.row.NotificationContentInflater.FLAG_CONTENT_VIEW_EXPANDED; |
| 22 | import static com.android.systemui.statusbar.notification.row.NotificationContentInflater.FLAG_CONTENT_VIEW_HEADS_UP; |
| 23 | import static com.android.systemui.statusbar.notification.row.NotificationContentInflater.FLAG_CONTENT_VIEW_PUBLIC; |
Selim Cinek | 2630dc7 | 2017-04-20 15:16:10 -0700 | [diff] [blame] | 24 | |
Kevin | d4660b2 | 2018-09-27 10:57:35 -0700 | [diff] [blame] | 25 | import static org.junit.Assert.assertEquals; |
| 26 | import static org.junit.Assert.assertNotNull; |
| 27 | import static org.junit.Assert.assertNull; |
Jason Monk | 6dceace | 2018-05-15 20:24:07 -0400 | [diff] [blame] | 28 | import static org.junit.Assert.assertTrue; |
Selim Cinek | 2630dc7 | 2017-04-20 15:16:10 -0700 | [diff] [blame] | 29 | import static org.mockito.Mockito.spy; |
| 30 | import static org.mockito.Mockito.times; |
| 31 | import static org.mockito.Mockito.verify; |
| 32 | |
| 33 | import android.app.Notification; |
| 34 | import android.content.Context; |
Selim Cinek | d246bed | 2017-06-19 16:58:35 -0700 | [diff] [blame] | 35 | import android.os.CancellationSignal; |
| 36 | import android.os.Handler; |
| 37 | import android.os.Looper; |
Selim Cinek | 2630dc7 | 2017-04-20 15:16:10 -0700 | [diff] [blame] | 38 | import android.service.notification.StatusBarNotification; |
Jason Monk | 6dceace | 2018-05-15 20:24:07 -0400 | [diff] [blame] | 39 | import android.testing.AndroidTestingRunner; |
| 40 | import android.testing.TestableLooper.RunWithLooper; |
Kevin | d4660b2 | 2018-09-27 10:57:35 -0700 | [diff] [blame] | 41 | import android.util.ArrayMap; |
Selim Cinek | d246bed | 2017-06-19 16:58:35 -0700 | [diff] [blame] | 42 | import android.view.View; |
| 43 | import android.view.ViewGroup; |
Selim Cinek | 2630dc7 | 2017-04-20 15:16:10 -0700 | [diff] [blame] | 44 | import android.widget.RemoteViews; |
| 45 | |
Brett Chabot | 84151d9 | 2019-02-27 15:37:59 -0800 | [diff] [blame] | 46 | import androidx.test.filters.SmallTest; |
| 47 | |
Selim Cinek | 2630dc7 | 2017-04-20 15:16:10 -0700 | [diff] [blame] | 48 | import com.android.systemui.R; |
Jason Monk | fba8faf | 2017-05-23 10:42:59 -0400 | [diff] [blame] | 49 | import com.android.systemui.SysuiTestCase; |
Jason Monk | b05395f | 2017-07-11 10:05:03 -0400 | [diff] [blame] | 50 | import com.android.systemui.statusbar.InflationTask; |
Selim Cinek | 2630dc7 | 2017-04-20 15:16:10 -0700 | [diff] [blame] | 51 | import com.android.systemui.statusbar.NotificationTestHelper; |
Ned Burns | f81c4c4 | 2019-01-07 14:10:43 -0500 | [diff] [blame] | 52 | import com.android.systemui.statusbar.notification.collection.NotificationEntry; |
Ned Burns | 1a5e22f | 2019-02-14 15:11:52 -0500 | [diff] [blame] | 53 | import com.android.systemui.statusbar.notification.row.NotificationContentInflater.InflationCallback; |
Selim Cinek | 2630dc7 | 2017-04-20 15:16:10 -0700 | [diff] [blame] | 54 | |
| 55 | import org.junit.Assert; |
| 56 | import org.junit.Before; |
Geoffrey Pitsch | 351a321 | 2017-05-22 15:20:20 -0400 | [diff] [blame] | 57 | import org.junit.Ignore; |
Selim Cinek | 2630dc7 | 2017-04-20 15:16:10 -0700 | [diff] [blame] | 58 | import org.junit.Test; |
| 59 | import org.junit.runner.RunWith; |
| 60 | |
Selim Cinek | d246bed | 2017-06-19 16:58:35 -0700 | [diff] [blame] | 61 | import java.util.HashMap; |
Selim Cinek | 2630dc7 | 2017-04-20 15:16:10 -0700 | [diff] [blame] | 62 | import java.util.concurrent.CountDownLatch; |
Selim Cinek | d246bed | 2017-06-19 16:58:35 -0700 | [diff] [blame] | 63 | import java.util.concurrent.Executor; |
Jason Monk | 6dceace | 2018-05-15 20:24:07 -0400 | [diff] [blame] | 64 | import java.util.concurrent.TimeUnit; |
Selim Cinek | 2630dc7 | 2017-04-20 15:16:10 -0700 | [diff] [blame] | 65 | |
| 66 | @SmallTest |
Jason Monk | 6dceace | 2018-05-15 20:24:07 -0400 | [diff] [blame] | 67 | @RunWith(AndroidTestingRunner.class) |
| 68 | @RunWithLooper(setAsMainLooper = true) |
Ned Burns | 1a5e22f | 2019-02-14 15:11:52 -0500 | [diff] [blame] | 69 | public class NotificationContentInflaterTest extends SysuiTestCase { |
Selim Cinek | 2630dc7 | 2017-04-20 15:16:10 -0700 | [diff] [blame] | 70 | |
Ned Burns | 1a5e22f | 2019-02-14 15:11:52 -0500 | [diff] [blame] | 71 | private NotificationContentInflater mNotificationInflater; |
Selim Cinek | 2630dc7 | 2017-04-20 15:16:10 -0700 | [diff] [blame] | 72 | private Notification.Builder mBuilder; |
| 73 | private ExpandableNotificationRow mRow; |
| 74 | |
| 75 | @Before |
| 76 | public void setUp() throws Exception { |
Selim Cinek | 2630dc7 | 2017-04-20 15:16:10 -0700 | [diff] [blame] | 77 | mBuilder = new Notification.Builder(mContext).setSmallIcon( |
| 78 | R.drawable.ic_person) |
| 79 | .setContentTitle("Title") |
| 80 | .setContentText("Text") |
| 81 | .setStyle(new Notification.BigTextStyle().bigText("big text")); |
| 82 | ExpandableNotificationRow row = new NotificationTestHelper(mContext).createRow( |
| 83 | mBuilder.build()); |
| 84 | mRow = spy(row); |
Ned Burns | 1a5e22f | 2019-02-14 15:11:52 -0500 | [diff] [blame] | 85 | mNotificationInflater = new NotificationContentInflater(mRow); |
| 86 | mNotificationInflater.setInflationCallback(new InflationCallback() { |
Selim Cinek | 2630dc7 | 2017-04-20 15:16:10 -0700 | [diff] [blame] | 87 | @Override |
| 88 | public void handleInflationException(StatusBarNotification notification, |
Selim Cinek | 01d3da6 | 2017-04-28 15:03:48 -0700 | [diff] [blame] | 89 | Exception e) { |
Selim Cinek | 2630dc7 | 2017-04-20 15:16:10 -0700 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | @Override |
Ned Burns | f81c4c4 | 2019-01-07 14:10:43 -0500 | [diff] [blame] | 93 | public void onAsyncInflationFinished(NotificationEntry entry, |
Ned Burns | 1a5e22f | 2019-02-14 15:11:52 -0500 | [diff] [blame] | 94 | @NotificationContentInflater.InflationFlag int inflatedFlags) { |
Selim Cinek | 2630dc7 | 2017-04-20 15:16:10 -0700 | [diff] [blame] | 95 | } |
| 96 | }); |
| 97 | } |
| 98 | |
| 99 | @Test |
| 100 | public void testIncreasedHeadsUpBeingUsed() { |
| 101 | mNotificationInflater.setUsesIncreasedHeadsUpHeight(true); |
| 102 | Notification.Builder builder = spy(mBuilder); |
Ned Burns | 342c3a0 | 2019-02-15 18:08:39 -0500 | [diff] [blame] | 103 | mNotificationInflater.inflateNotificationViews( |
| 104 | false /* inflateSynchronously */, |
| 105 | FLAG_CONTENT_VIEW_ALL, |
| 106 | builder, |
| 107 | mContext); |
Selim Cinek | 2630dc7 | 2017-04-20 15:16:10 -0700 | [diff] [blame] | 108 | verify(builder).createHeadsUpContentView(true); |
| 109 | } |
| 110 | |
| 111 | @Test |
| 112 | public void testIncreasedHeightBeingUsed() { |
| 113 | mNotificationInflater.setUsesIncreasedHeight(true); |
| 114 | Notification.Builder builder = spy(mBuilder); |
Ned Burns | 342c3a0 | 2019-02-15 18:08:39 -0500 | [diff] [blame] | 115 | mNotificationInflater.inflateNotificationViews( |
| 116 | false /* inflateSynchronously */, |
| 117 | FLAG_CONTENT_VIEW_ALL, |
| 118 | builder, |
| 119 | mContext); |
Selim Cinek | 2630dc7 | 2017-04-20 15:16:10 -0700 | [diff] [blame] | 120 | verify(builder).createContentView(true); |
| 121 | } |
| 122 | |
| 123 | @Test |
| 124 | public void testInflationCallsUpdated() throws Exception { |
| 125 | runThenWaitForInflation(() -> mNotificationInflater.inflateNotificationViews(), |
| 126 | mNotificationInflater); |
| 127 | verify(mRow).onNotificationUpdated(); |
| 128 | } |
| 129 | |
| 130 | @Test |
Kevin | d4660b2 | 2018-09-27 10:57:35 -0700 | [diff] [blame] | 131 | public void testInflationOnlyInflatesSetFlags() throws Exception { |
Kevin | d5022f9 | 2018-10-08 18:30:26 -0700 | [diff] [blame] | 132 | mNotificationInflater.updateInflationFlag(FLAG_CONTENT_VIEW_HEADS_UP, |
Kevin | d4660b2 | 2018-09-27 10:57:35 -0700 | [diff] [blame] | 133 | true /* shouldInflate */); |
| 134 | runThenWaitForInflation(() -> mNotificationInflater.inflateNotificationViews(), |
| 135 | mNotificationInflater); |
| 136 | |
| 137 | assertNotNull(mRow.getPrivateLayout().getHeadsUpChild()); |
| 138 | assertNull(mRow.getShowingLayout().getAmbientChild()); |
Selim Cinek | 2630dc7 | 2017-04-20 15:16:10 -0700 | [diff] [blame] | 139 | verify(mRow).onNotificationUpdated(); |
| 140 | } |
| 141 | |
| 142 | @Test |
| 143 | public void testInflationThrowsErrorDoesntCallUpdated() throws Exception { |
| 144 | mRow.getPrivateLayout().removeAllViews(); |
| 145 | mRow.getStatusBarNotification().getNotification().contentView |
Selim Cinek | dc1231c | 2017-04-27 17:30:50 -0700 | [diff] [blame] | 146 | = new RemoteViews(mContext.getPackageName(), R.layout.status_bar); |
Selim Cinek | 2630dc7 | 2017-04-20 15:16:10 -0700 | [diff] [blame] | 147 | runThenWaitForInflation(() -> mNotificationInflater.inflateNotificationViews(), |
| 148 | true /* expectingException */, mNotificationInflater); |
Jason Monk | 6dceace | 2018-05-15 20:24:07 -0400 | [diff] [blame] | 149 | assertTrue(mRow.getPrivateLayout().getChildCount() == 0); |
Selim Cinek | 2630dc7 | 2017-04-20 15:16:10 -0700 | [diff] [blame] | 150 | verify(mRow, times(0)).onNotificationUpdated(); |
| 151 | } |
| 152 | |
Selim Cinek | dc1231c | 2017-04-27 17:30:50 -0700 | [diff] [blame] | 153 | @Test |
| 154 | public void testAsyncTaskRemoved() throws Exception { |
Selim Cinek | 0f66a4c | 2017-04-28 19:26:28 -0700 | [diff] [blame] | 155 | mRow.getEntry().abortTask(); |
Selim Cinek | dc1231c | 2017-04-27 17:30:50 -0700 | [diff] [blame] | 156 | runThenWaitForInflation(() -> mNotificationInflater.inflateNotificationViews(), |
| 157 | mNotificationInflater); |
Selim Cinek | 67ff248 | 2017-05-25 10:27:28 -0700 | [diff] [blame] | 158 | verify(mRow).onNotificationUpdated(); |
| 159 | } |
| 160 | |
| 161 | @Test |
| 162 | public void testRemovedNotInflated() throws Exception { |
| 163 | mRow.setRemoved(); |
| 164 | mNotificationInflater.inflateNotificationViews(); |
| 165 | Assert.assertNull(mRow.getEntry().getRunningTask()); |
| 166 | } |
| 167 | |
Selim Cinek | d246bed | 2017-06-19 16:58:35 -0700 | [diff] [blame] | 168 | @Test |
Jason Monk | b05395f | 2017-07-11 10:05:03 -0400 | [diff] [blame] | 169 | @Ignore |
Selim Cinek | d246bed | 2017-06-19 16:58:35 -0700 | [diff] [blame] | 170 | public void testInflationIsRetriedIfAsyncFails() throws Exception { |
Ned Burns | 1a5e22f | 2019-02-14 15:11:52 -0500 | [diff] [blame] | 171 | NotificationContentInflater.InflationProgress result = |
| 172 | new NotificationContentInflater.InflationProgress(); |
Selim Cinek | d246bed | 2017-06-19 16:58:35 -0700 | [diff] [blame] | 173 | result.packageContext = mContext; |
| 174 | CountDownLatch countDownLatch = new CountDownLatch(1); |
Ned Burns | 342c3a0 | 2019-02-15 18:08:39 -0500 | [diff] [blame] | 175 | NotificationContentInflater.applyRemoteView( |
| 176 | false /* inflateSynchronously */, |
| 177 | result, |
| 178 | FLAG_CONTENT_VIEW_EXPANDED, |
| 179 | 0, |
Kevin | d4660b2 | 2018-09-27 10:57:35 -0700 | [diff] [blame] | 180 | new ArrayMap() /* cachedContentViews */, mRow, false /* redactAmbient */, |
Sunny Goyal | 43c9704 | 2018-08-23 15:21:26 -0700 | [diff] [blame] | 181 | true /* isNewView */, (v, p, r) -> true, |
Ned Burns | 1a5e22f | 2019-02-14 15:11:52 -0500 | [diff] [blame] | 182 | new InflationCallback() { |
Selim Cinek | d246bed | 2017-06-19 16:58:35 -0700 | [diff] [blame] | 183 | @Override |
| 184 | public void handleInflationException(StatusBarNotification notification, |
| 185 | Exception e) { |
| 186 | countDownLatch.countDown(); |
| 187 | throw new RuntimeException("No Exception expected"); |
| 188 | } |
| 189 | |
| 190 | @Override |
Ned Burns | f81c4c4 | 2019-01-07 14:10:43 -0500 | [diff] [blame] | 191 | public void onAsyncInflationFinished(NotificationEntry entry, |
Ned Burns | 1a5e22f | 2019-02-14 15:11:52 -0500 | [diff] [blame] | 192 | @NotificationContentInflater.InflationFlag int inflatedFlags) { |
Selim Cinek | d246bed | 2017-06-19 16:58:35 -0700 | [diff] [blame] | 193 | countDownLatch.countDown(); |
| 194 | } |
Kevin | d4660b2 | 2018-09-27 10:57:35 -0700 | [diff] [blame] | 195 | }, mRow.getPrivateLayout(), null, null, new HashMap<>(), |
Ned Burns | 1a5e22f | 2019-02-14 15:11:52 -0500 | [diff] [blame] | 196 | new NotificationContentInflater.ApplyCallback() { |
Selim Cinek | d246bed | 2017-06-19 16:58:35 -0700 | [diff] [blame] | 197 | @Override |
| 198 | public void setResultView(View v) { |
| 199 | } |
| 200 | |
| 201 | @Override |
| 202 | public RemoteViews getRemoteView() { |
| 203 | return new AsyncFailRemoteView(mContext.getPackageName(), |
| 204 | R.layout.custom_view_dark); |
| 205 | } |
| 206 | }); |
Jason Monk | 6dceace | 2018-05-15 20:24:07 -0400 | [diff] [blame] | 207 | assertTrue(countDownLatch.await(500, TimeUnit.MILLISECONDS)); |
Selim Cinek | d246bed | 2017-06-19 16:58:35 -0700 | [diff] [blame] | 208 | } |
Selim Cinek | 67ff248 | 2017-05-25 10:27:28 -0700 | [diff] [blame] | 209 | |
Kevin | 38ce6fa | 2018-10-17 16:00:14 -0700 | [diff] [blame] | 210 | @Test |
| 211 | public void testUpdateNeedsRedactionReinflatesChangedContentViews() { |
| 212 | mNotificationInflater.updateInflationFlag(FLAG_CONTENT_VIEW_AMBIENT, true); |
| 213 | mNotificationInflater.updateInflationFlag(FLAG_CONTENT_VIEW_PUBLIC, true); |
| 214 | mNotificationInflater.updateNeedsRedaction(true); |
| 215 | |
Ned Burns | 1a5e22f | 2019-02-14 15:11:52 -0500 | [diff] [blame] | 216 | NotificationContentInflater.AsyncInflationTask asyncInflationTask = |
| 217 | (NotificationContentInflater.AsyncInflationTask) mRow.getEntry().getRunningTask(); |
Kevin | 38ce6fa | 2018-10-17 16:00:14 -0700 | [diff] [blame] | 218 | assertEquals(FLAG_CONTENT_VIEW_AMBIENT | FLAG_CONTENT_VIEW_PUBLIC, |
| 219 | asyncInflationTask.getReInflateFlags()); |
| 220 | asyncInflationTask.abort(); |
| 221 | } |
| 222 | |
Selim Cinek | 21f3366 | 2017-09-08 13:24:21 -0700 | [diff] [blame] | 223 | /* Cancelling requires us to be on the UI thread otherwise we might have a race */ |
Selim Cinek | 67ff248 | 2017-05-25 10:27:28 -0700 | [diff] [blame] | 224 | @Test |
Kevin | d4660b2 | 2018-09-27 10:57:35 -0700 | [diff] [blame] | 225 | public void testSupersedesExistingTask() { |
Kevin | d5022f9 | 2018-10-08 18:30:26 -0700 | [diff] [blame] | 226 | mNotificationInflater.addInflationFlags(FLAG_CONTENT_VIEW_ALL); |
Selim Cinek | 67ff248 | 2017-05-25 10:27:28 -0700 | [diff] [blame] | 227 | mNotificationInflater.inflateNotificationViews(); |
Kevin | d4660b2 | 2018-09-27 10:57:35 -0700 | [diff] [blame] | 228 | |
| 229 | // Trigger inflation of content and expanded only. |
Selim Cinek | 67ff248 | 2017-05-25 10:27:28 -0700 | [diff] [blame] | 230 | mNotificationInflater.setIsLowPriority(true); |
| 231 | mNotificationInflater.setIsChildInGroup(true); |
Kevin | d4660b2 | 2018-09-27 10:57:35 -0700 | [diff] [blame] | 232 | |
Selim Cinek | 67ff248 | 2017-05-25 10:27:28 -0700 | [diff] [blame] | 233 | InflationTask runningTask = mRow.getEntry().getRunningTask(); |
Ned Burns | 1a5e22f | 2019-02-14 15:11:52 -0500 | [diff] [blame] | 234 | NotificationContentInflater.AsyncInflationTask asyncInflationTask = |
| 235 | (NotificationContentInflater.AsyncInflationTask) runningTask; |
Kevin | d4660b2 | 2018-09-27 10:57:35 -0700 | [diff] [blame] | 236 | assertEquals("Successive inflations don't inherit the previous flags!", |
Kevin | 38ce6fa | 2018-10-17 16:00:14 -0700 | [diff] [blame] | 237 | FLAG_CONTENT_VIEW_ALL, asyncInflationTask.getReInflateFlags()); |
Selim Cinek | 67ff248 | 2017-05-25 10:27:28 -0700 | [diff] [blame] | 238 | runningTask.abort(); |
Selim Cinek | dc1231c | 2017-04-27 17:30:50 -0700 | [diff] [blame] | 239 | } |
| 240 | |
Selim Cinek | fc8073c | 2017-08-16 17:50:20 -0700 | [diff] [blame] | 241 | @Test |
| 242 | public void doesntReapplyDisallowedRemoteView() throws Exception { |
| 243 | mBuilder.setStyle(new Notification.MediaStyle()); |
| 244 | RemoteViews mediaView = mBuilder.createContentView(); |
| 245 | mBuilder.setStyle(new Notification.DecoratedCustomViewStyle()); |
| 246 | mBuilder.setCustomContentView(new RemoteViews(getContext().getPackageName(), |
| 247 | R.layout.custom_view_dark)); |
| 248 | RemoteViews decoratedMediaView = mBuilder.createContentView(); |
| 249 | Assert.assertFalse("The decorated media style doesn't allow a view to be reapplied!", |
Ned Burns | 1a5e22f | 2019-02-14 15:11:52 -0500 | [diff] [blame] | 250 | NotificationContentInflater.canReapplyRemoteView(mediaView, decoratedMediaView)); |
Selim Cinek | fc8073c | 2017-08-16 17:50:20 -0700 | [diff] [blame] | 251 | } |
| 252 | |
Selim Cinek | 2630dc7 | 2017-04-20 15:16:10 -0700 | [diff] [blame] | 253 | public static void runThenWaitForInflation(Runnable block, |
Ned Burns | 1a5e22f | 2019-02-14 15:11:52 -0500 | [diff] [blame] | 254 | NotificationContentInflater inflater) throws Exception { |
Selim Cinek | 2630dc7 | 2017-04-20 15:16:10 -0700 | [diff] [blame] | 255 | runThenWaitForInflation(block, false /* expectingException */, inflater); |
| 256 | } |
| 257 | |
| 258 | private static void runThenWaitForInflation(Runnable block, boolean expectingException, |
Ned Burns | 1a5e22f | 2019-02-14 15:11:52 -0500 | [diff] [blame] | 259 | NotificationContentInflater inflater) throws Exception { |
Selim Cinek | 2630dc7 | 2017-04-20 15:16:10 -0700 | [diff] [blame] | 260 | CountDownLatch countDownLatch = new CountDownLatch(1); |
| 261 | final ExceptionHolder exceptionHolder = new ExceptionHolder(); |
Ned Burns | 342c3a0 | 2019-02-15 18:08:39 -0500 | [diff] [blame] | 262 | inflater.setInflateSynchronously(true); |
Ned Burns | 1a5e22f | 2019-02-14 15:11:52 -0500 | [diff] [blame] | 263 | inflater.setInflationCallback(new InflationCallback() { |
Selim Cinek | 2630dc7 | 2017-04-20 15:16:10 -0700 | [diff] [blame] | 264 | @Override |
| 265 | public void handleInflationException(StatusBarNotification notification, |
Selim Cinek | 01d3da6 | 2017-04-28 15:03:48 -0700 | [diff] [blame] | 266 | Exception e) { |
Selim Cinek | 2630dc7 | 2017-04-20 15:16:10 -0700 | [diff] [blame] | 267 | if (!expectingException) { |
| 268 | exceptionHolder.setException(e); |
| 269 | } |
| 270 | countDownLatch.countDown(); |
| 271 | } |
| 272 | |
| 273 | @Override |
Ned Burns | f81c4c4 | 2019-01-07 14:10:43 -0500 | [diff] [blame] | 274 | public void onAsyncInflationFinished(NotificationEntry entry, |
Ned Burns | 1a5e22f | 2019-02-14 15:11:52 -0500 | [diff] [blame] | 275 | @NotificationContentInflater.InflationFlag int inflatedFlags) { |
Selim Cinek | 2630dc7 | 2017-04-20 15:16:10 -0700 | [diff] [blame] | 276 | if (expectingException) { |
| 277 | exceptionHolder.setException(new RuntimeException( |
| 278 | "Inflation finished even though there should be an error")); |
| 279 | } |
| 280 | countDownLatch.countDown(); |
| 281 | } |
| 282 | }); |
| 283 | block.run(); |
Jason Monk | 6dceace | 2018-05-15 20:24:07 -0400 | [diff] [blame] | 284 | assertTrue(countDownLatch.await(500, TimeUnit.MILLISECONDS)); |
Selim Cinek | 2630dc7 | 2017-04-20 15:16:10 -0700 | [diff] [blame] | 285 | if (exceptionHolder.mException != null) { |
| 286 | throw exceptionHolder.mException; |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | private static class ExceptionHolder { |
| 291 | private Exception mException; |
| 292 | |
| 293 | public void setException(Exception exception) { |
| 294 | mException = exception; |
| 295 | } |
| 296 | } |
Selim Cinek | d246bed | 2017-06-19 16:58:35 -0700 | [diff] [blame] | 297 | |
| 298 | private class AsyncFailRemoteView extends RemoteViews { |
Jason Monk | 6dceace | 2018-05-15 20:24:07 -0400 | [diff] [blame] | 299 | Handler mHandler = Handler.createAsync(Looper.getMainLooper()); |
Selim Cinek | d246bed | 2017-06-19 16:58:35 -0700 | [diff] [blame] | 300 | |
| 301 | public AsyncFailRemoteView(String packageName, int layoutId) { |
| 302 | super(packageName, layoutId); |
| 303 | } |
| 304 | |
| 305 | @Override |
| 306 | public View apply(Context context, ViewGroup parent) { |
| 307 | return super.apply(context, parent); |
| 308 | } |
| 309 | |
| 310 | @Override |
| 311 | public CancellationSignal applyAsync(Context context, ViewGroup parent, Executor executor, |
| 312 | OnViewAppliedListener listener, OnClickHandler handler) { |
| 313 | mHandler.post(() -> listener.onError(new RuntimeException("Failed to inflate async"))); |
| 314 | return new CancellationSignal(); |
| 315 | } |
| 316 | |
| 317 | @Override |
| 318 | public CancellationSignal applyAsync(Context context, ViewGroup parent, Executor executor, |
| 319 | OnViewAppliedListener listener) { |
| 320 | return applyAsync(context, parent, executor, listener, null); |
| 321 | } |
| 322 | } |
Selim Cinek | 2630dc7 | 2017-04-20 15:16:10 -0700 | [diff] [blame] | 323 | } |