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; |
Ned Burns | 1a5e22f | 2019-02-14 15:11:52 -0500 | [diff] [blame] | 20 | import static com.android.systemui.statusbar.notification.row.NotificationContentInflater.FLAG_CONTENT_VIEW_EXPANDED; |
| 21 | import static com.android.systemui.statusbar.notification.row.NotificationContentInflater.FLAG_CONTENT_VIEW_HEADS_UP; |
| 22 | 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] | 23 | |
Kevin | d4660b2 | 2018-09-27 10:57:35 -0700 | [diff] [blame] | 24 | import static org.junit.Assert.assertEquals; |
| 25 | import static org.junit.Assert.assertNotNull; |
| 26 | import static org.junit.Assert.assertNull; |
Jason Monk | 6dceace | 2018-05-15 20:24:07 -0400 | [diff] [blame] | 27 | import static org.junit.Assert.assertTrue; |
Selim Cinek | 2630dc7 | 2017-04-20 15:16:10 -0700 | [diff] [blame] | 28 | import static org.mockito.Mockito.spy; |
| 29 | import static org.mockito.Mockito.times; |
| 30 | import static org.mockito.Mockito.verify; |
| 31 | |
| 32 | import android.app.Notification; |
| 33 | import android.content.Context; |
Selim Cinek | d246bed | 2017-06-19 16:58:35 -0700 | [diff] [blame] | 34 | import android.os.CancellationSignal; |
| 35 | import android.os.Handler; |
| 36 | import android.os.Looper; |
Selim Cinek | 2630dc7 | 2017-04-20 15:16:10 -0700 | [diff] [blame] | 37 | import android.service.notification.StatusBarNotification; |
Jason Monk | 6dceace | 2018-05-15 20:24:07 -0400 | [diff] [blame] | 38 | import android.testing.AndroidTestingRunner; |
| 39 | import android.testing.TestableLooper.RunWithLooper; |
Kevin | d4660b2 | 2018-09-27 10:57:35 -0700 | [diff] [blame] | 40 | import android.util.ArrayMap; |
Selim Cinek | d246bed | 2017-06-19 16:58:35 -0700 | [diff] [blame] | 41 | import android.view.View; |
| 42 | import android.view.ViewGroup; |
Selim Cinek | 2630dc7 | 2017-04-20 15:16:10 -0700 | [diff] [blame] | 43 | import android.widget.RemoteViews; |
| 44 | |
Brett Chabot | 84151d9 | 2019-02-27 15:37:59 -0800 | [diff] [blame] | 45 | import androidx.test.filters.SmallTest; |
| 46 | |
Selim Cinek | 2630dc7 | 2017-04-20 15:16:10 -0700 | [diff] [blame] | 47 | import com.android.systemui.R; |
Jason Monk | fba8faf | 2017-05-23 10:42:59 -0400 | [diff] [blame] | 48 | import com.android.systemui.SysuiTestCase; |
Jason Monk | b05395f | 2017-07-11 10:05:03 -0400 | [diff] [blame] | 49 | import com.android.systemui.statusbar.InflationTask; |
Selim Cinek | 2630dc7 | 2017-04-20 15:16:10 -0700 | [diff] [blame] | 50 | import com.android.systemui.statusbar.NotificationTestHelper; |
Ned Burns | f81c4c4 | 2019-01-07 14:10:43 -0500 | [diff] [blame] | 51 | import com.android.systemui.statusbar.notification.collection.NotificationEntry; |
Ned Burns | 1a5e22f | 2019-02-14 15:11:52 -0500 | [diff] [blame] | 52 | import com.android.systemui.statusbar.notification.row.NotificationContentInflater.InflationCallback; |
Selim Cinek | 2630dc7 | 2017-04-20 15:16:10 -0700 | [diff] [blame] | 53 | |
| 54 | import org.junit.Assert; |
| 55 | import org.junit.Before; |
Geoffrey Pitsch | 351a321 | 2017-05-22 15:20:20 -0400 | [diff] [blame] | 56 | import org.junit.Ignore; |
Selim Cinek | 2630dc7 | 2017-04-20 15:16:10 -0700 | [diff] [blame] | 57 | import org.junit.Test; |
| 58 | import org.junit.runner.RunWith; |
| 59 | |
Selim Cinek | d246bed | 2017-06-19 16:58:35 -0700 | [diff] [blame] | 60 | import java.util.HashMap; |
Selim Cinek | 2630dc7 | 2017-04-20 15:16:10 -0700 | [diff] [blame] | 61 | import java.util.concurrent.CountDownLatch; |
Selim Cinek | d246bed | 2017-06-19 16:58:35 -0700 | [diff] [blame] | 62 | import java.util.concurrent.Executor; |
Jason Monk | 6dceace | 2018-05-15 20:24:07 -0400 | [diff] [blame] | 63 | import java.util.concurrent.TimeUnit; |
Selim Cinek | 2630dc7 | 2017-04-20 15:16:10 -0700 | [diff] [blame] | 64 | |
| 65 | @SmallTest |
Jason Monk | 6dceace | 2018-05-15 20:24:07 -0400 | [diff] [blame] | 66 | @RunWith(AndroidTestingRunner.class) |
| 67 | @RunWithLooper(setAsMainLooper = true) |
Ned Burns | 1a5e22f | 2019-02-14 15:11:52 -0500 | [diff] [blame] | 68 | public class NotificationContentInflaterTest extends SysuiTestCase { |
Selim Cinek | 2630dc7 | 2017-04-20 15:16:10 -0700 | [diff] [blame] | 69 | |
Ned Burns | 1a5e22f | 2019-02-14 15:11:52 -0500 | [diff] [blame] | 70 | private NotificationContentInflater mNotificationInflater; |
Selim Cinek | 2630dc7 | 2017-04-20 15:16:10 -0700 | [diff] [blame] | 71 | private Notification.Builder mBuilder; |
| 72 | private ExpandableNotificationRow mRow; |
| 73 | |
| 74 | @Before |
| 75 | public void setUp() throws Exception { |
Selim Cinek | 2630dc7 | 2017-04-20 15:16:10 -0700 | [diff] [blame] | 76 | 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 Burns | 1a5e22f | 2019-02-14 15:11:52 -0500 | [diff] [blame] | 84 | mNotificationInflater = new NotificationContentInflater(mRow); |
| 85 | mNotificationInflater.setInflationCallback(new InflationCallback() { |
Selim Cinek | 2630dc7 | 2017-04-20 15:16:10 -0700 | [diff] [blame] | 86 | @Override |
| 87 | public void handleInflationException(StatusBarNotification notification, |
Selim Cinek | 01d3da6 | 2017-04-28 15:03:48 -0700 | [diff] [blame] | 88 | Exception e) { |
Selim Cinek | 2630dc7 | 2017-04-20 15:16:10 -0700 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | @Override |
Ned Burns | f81c4c4 | 2019-01-07 14:10:43 -0500 | [diff] [blame] | 92 | public void onAsyncInflationFinished(NotificationEntry entry, |
Ned Burns | 1a5e22f | 2019-02-14 15:11:52 -0500 | [diff] [blame] | 93 | @NotificationContentInflater.InflationFlag int inflatedFlags) { |
Selim Cinek | 2630dc7 | 2017-04-20 15:16:10 -0700 | [diff] [blame] | 94 | } |
| 95 | }); |
| 96 | } |
| 97 | |
| 98 | @Test |
| 99 | public void testIncreasedHeadsUpBeingUsed() { |
| 100 | mNotificationInflater.setUsesIncreasedHeadsUpHeight(true); |
| 101 | Notification.Builder builder = spy(mBuilder); |
Ned Burns | 342c3a0 | 2019-02-15 18:08:39 -0500 | [diff] [blame] | 102 | mNotificationInflater.inflateNotificationViews( |
Selim Cinek | 69d11c2 | 2019-06-10 23:52:32 -0700 | [diff] [blame] | 103 | true /* inflateSynchronously */, |
Ned Burns | 342c3a0 | 2019-02-15 18:08:39 -0500 | [diff] [blame] | 104 | FLAG_CONTENT_VIEW_ALL, |
| 105 | builder, |
| 106 | mContext); |
Selim Cinek | 2630dc7 | 2017-04-20 15:16:10 -0700 | [diff] [blame] | 107 | verify(builder).createHeadsUpContentView(true); |
| 108 | } |
| 109 | |
| 110 | @Test |
| 111 | public void testIncreasedHeightBeingUsed() { |
| 112 | mNotificationInflater.setUsesIncreasedHeight(true); |
| 113 | Notification.Builder builder = spy(mBuilder); |
Ned Burns | 342c3a0 | 2019-02-15 18:08:39 -0500 | [diff] [blame] | 114 | mNotificationInflater.inflateNotificationViews( |
Selim Cinek | 69d11c2 | 2019-06-10 23:52:32 -0700 | [diff] [blame] | 115 | true /* inflateSynchronously */, |
Ned Burns | 342c3a0 | 2019-02-15 18:08:39 -0500 | [diff] [blame] | 116 | FLAG_CONTENT_VIEW_ALL, |
| 117 | builder, |
| 118 | mContext); |
Selim Cinek | 2630dc7 | 2017-04-20 15:16:10 -0700 | [diff] [blame] | 119 | 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 |
Kevin | d4660b2 | 2018-09-27 10:57:35 -0700 | [diff] [blame] | 130 | public void testInflationOnlyInflatesSetFlags() throws Exception { |
Kevin | d5022f9 | 2018-10-08 18:30:26 -0700 | [diff] [blame] | 131 | mNotificationInflater.updateInflationFlag(FLAG_CONTENT_VIEW_HEADS_UP, |
Kevin | d4660b2 | 2018-09-27 10:57:35 -0700 | [diff] [blame] | 132 | true /* shouldInflate */); |
| 133 | runThenWaitForInflation(() -> mNotificationInflater.inflateNotificationViews(), |
| 134 | mNotificationInflater); |
| 135 | |
| 136 | assertNotNull(mRow.getPrivateLayout().getHeadsUpChild()); |
Selim Cinek | 2630dc7 | 2017-04-20 15:16:10 -0700 | [diff] [blame] | 137 | verify(mRow).onNotificationUpdated(); |
| 138 | } |
| 139 | |
| 140 | @Test |
| 141 | public void testInflationThrowsErrorDoesntCallUpdated() throws Exception { |
| 142 | mRow.getPrivateLayout().removeAllViews(); |
| 143 | mRow.getStatusBarNotification().getNotification().contentView |
Selim Cinek | dc1231c | 2017-04-27 17:30:50 -0700 | [diff] [blame] | 144 | = new RemoteViews(mContext.getPackageName(), R.layout.status_bar); |
Selim Cinek | 2630dc7 | 2017-04-20 15:16:10 -0700 | [diff] [blame] | 145 | runThenWaitForInflation(() -> mNotificationInflater.inflateNotificationViews(), |
| 146 | true /* expectingException */, mNotificationInflater); |
Jason Monk | 6dceace | 2018-05-15 20:24:07 -0400 | [diff] [blame] | 147 | assertTrue(mRow.getPrivateLayout().getChildCount() == 0); |
Selim Cinek | 2630dc7 | 2017-04-20 15:16:10 -0700 | [diff] [blame] | 148 | verify(mRow, times(0)).onNotificationUpdated(); |
| 149 | } |
| 150 | |
Selim Cinek | dc1231c | 2017-04-27 17:30:50 -0700 | [diff] [blame] | 151 | @Test |
| 152 | public void testAsyncTaskRemoved() throws Exception { |
Selim Cinek | 0f66a4c | 2017-04-28 19:26:28 -0700 | [diff] [blame] | 153 | mRow.getEntry().abortTask(); |
Selim Cinek | dc1231c | 2017-04-27 17:30:50 -0700 | [diff] [blame] | 154 | runThenWaitForInflation(() -> mNotificationInflater.inflateNotificationViews(), |
| 155 | mNotificationInflater); |
Selim Cinek | 67ff248 | 2017-05-25 10:27:28 -0700 | [diff] [blame] | 156 | verify(mRow).onNotificationUpdated(); |
| 157 | } |
| 158 | |
| 159 | @Test |
| 160 | public void testRemovedNotInflated() throws Exception { |
| 161 | mRow.setRemoved(); |
Selim Cinek | 69d11c2 | 2019-06-10 23:52:32 -0700 | [diff] [blame] | 162 | mNotificationInflater.setInflateSynchronously(true); |
Selim Cinek | 67ff248 | 2017-05-25 10:27:28 -0700 | [diff] [blame] | 163 | mNotificationInflater.inflateNotificationViews(); |
| 164 | Assert.assertNull(mRow.getEntry().getRunningTask()); |
| 165 | } |
| 166 | |
Selim Cinek | d246bed | 2017-06-19 16:58:35 -0700 | [diff] [blame] | 167 | @Test |
Jason Monk | b05395f | 2017-07-11 10:05:03 -0400 | [diff] [blame] | 168 | @Ignore |
Selim Cinek | d246bed | 2017-06-19 16:58:35 -0700 | [diff] [blame] | 169 | public void testInflationIsRetriedIfAsyncFails() throws Exception { |
Ned Burns | 1a5e22f | 2019-02-14 15:11:52 -0500 | [diff] [blame] | 170 | NotificationContentInflater.InflationProgress result = |
| 171 | new NotificationContentInflater.InflationProgress(); |
Selim Cinek | d246bed | 2017-06-19 16:58:35 -0700 | [diff] [blame] | 172 | result.packageContext = mContext; |
| 173 | CountDownLatch countDownLatch = new CountDownLatch(1); |
Ned Burns | 342c3a0 | 2019-02-15 18:08:39 -0500 | [diff] [blame] | 174 | NotificationContentInflater.applyRemoteView( |
| 175 | false /* inflateSynchronously */, |
| 176 | result, |
| 177 | FLAG_CONTENT_VIEW_EXPANDED, |
| 178 | 0, |
Selim Cinek | c3fec68 | 2019-06-06 18:11:07 -0700 | [diff] [blame] | 179 | new ArrayMap() /* cachedContentViews */, mRow, |
Sunny Goyal | 43c9704 | 2018-08-23 15:21:26 -0700 | [diff] [blame] | 180 | true /* isNewView */, (v, p, r) -> true, |
Ned Burns | 1a5e22f | 2019-02-14 15:11:52 -0500 | [diff] [blame] | 181 | new InflationCallback() { |
Selim Cinek | d246bed | 2017-06-19 16:58:35 -0700 | [diff] [blame] | 182 | @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 Burns | f81c4c4 | 2019-01-07 14:10:43 -0500 | [diff] [blame] | 190 | public void onAsyncInflationFinished(NotificationEntry entry, |
Ned Burns | 1a5e22f | 2019-02-14 15:11:52 -0500 | [diff] [blame] | 191 | @NotificationContentInflater.InflationFlag int inflatedFlags) { |
Selim Cinek | d246bed | 2017-06-19 16:58:35 -0700 | [diff] [blame] | 192 | countDownLatch.countDown(); |
| 193 | } |
Kevin | d4660b2 | 2018-09-27 10:57:35 -0700 | [diff] [blame] | 194 | }, mRow.getPrivateLayout(), null, null, new HashMap<>(), |
Ned Burns | 1a5e22f | 2019-02-14 15:11:52 -0500 | [diff] [blame] | 195 | new NotificationContentInflater.ApplyCallback() { |
Selim Cinek | d246bed | 2017-06-19 16:58:35 -0700 | [diff] [blame] | 196 | @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 Monk | 6dceace | 2018-05-15 20:24:07 -0400 | [diff] [blame] | 206 | assertTrue(countDownLatch.await(500, TimeUnit.MILLISECONDS)); |
Selim Cinek | d246bed | 2017-06-19 16:58:35 -0700 | [diff] [blame] | 207 | } |
Selim Cinek | 67ff248 | 2017-05-25 10:27:28 -0700 | [diff] [blame] | 208 | |
Kevin | 38ce6fa | 2018-10-17 16:00:14 -0700 | [diff] [blame] | 209 | @Test |
| 210 | public void testUpdateNeedsRedactionReinflatesChangedContentViews() { |
Kevin | 38ce6fa | 2018-10-17 16:00:14 -0700 | [diff] [blame] | 211 | mNotificationInflater.updateInflationFlag(FLAG_CONTENT_VIEW_PUBLIC, true); |
| 212 | mNotificationInflater.updateNeedsRedaction(true); |
| 213 | |
Ned Burns | 1a5e22f | 2019-02-14 15:11:52 -0500 | [diff] [blame] | 214 | NotificationContentInflater.AsyncInflationTask asyncInflationTask = |
| 215 | (NotificationContentInflater.AsyncInflationTask) mRow.getEntry().getRunningTask(); |
Selim Cinek | c3fec68 | 2019-06-06 18:11:07 -0700 | [diff] [blame] | 216 | assertEquals(FLAG_CONTENT_VIEW_PUBLIC, asyncInflationTask.getReInflateFlags()); |
Kevin | 38ce6fa | 2018-10-17 16:00:14 -0700 | [diff] [blame] | 217 | asyncInflationTask.abort(); |
| 218 | } |
| 219 | |
Selim Cinek | 21f3366 | 2017-09-08 13:24:21 -0700 | [diff] [blame] | 220 | /* 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] | 221 | @Test |
Kevin | d4660b2 | 2018-09-27 10:57:35 -0700 | [diff] [blame] | 222 | public void testSupersedesExistingTask() { |
Kevin | d5022f9 | 2018-10-08 18:30:26 -0700 | [diff] [blame] | 223 | mNotificationInflater.addInflationFlags(FLAG_CONTENT_VIEW_ALL); |
Selim Cinek | 67ff248 | 2017-05-25 10:27:28 -0700 | [diff] [blame] | 224 | mNotificationInflater.inflateNotificationViews(); |
Kevin | d4660b2 | 2018-09-27 10:57:35 -0700 | [diff] [blame] | 225 | |
| 226 | // Trigger inflation of content and expanded only. |
Selim Cinek | 67ff248 | 2017-05-25 10:27:28 -0700 | [diff] [blame] | 227 | mNotificationInflater.setIsLowPriority(true); |
| 228 | mNotificationInflater.setIsChildInGroup(true); |
Kevin | d4660b2 | 2018-09-27 10:57:35 -0700 | [diff] [blame] | 229 | |
Selim Cinek | 67ff248 | 2017-05-25 10:27:28 -0700 | [diff] [blame] | 230 | InflationTask runningTask = mRow.getEntry().getRunningTask(); |
Ned Burns | 1a5e22f | 2019-02-14 15:11:52 -0500 | [diff] [blame] | 231 | NotificationContentInflater.AsyncInflationTask asyncInflationTask = |
| 232 | (NotificationContentInflater.AsyncInflationTask) runningTask; |
Kevin | d4660b2 | 2018-09-27 10:57:35 -0700 | [diff] [blame] | 233 | assertEquals("Successive inflations don't inherit the previous flags!", |
Kevin | 38ce6fa | 2018-10-17 16:00:14 -0700 | [diff] [blame] | 234 | FLAG_CONTENT_VIEW_ALL, asyncInflationTask.getReInflateFlags()); |
Selim Cinek | 67ff248 | 2017-05-25 10:27:28 -0700 | [diff] [blame] | 235 | runningTask.abort(); |
Selim Cinek | dc1231c | 2017-04-27 17:30:50 -0700 | [diff] [blame] | 236 | } |
| 237 | |
Selim Cinek | fc8073c | 2017-08-16 17:50:20 -0700 | [diff] [blame] | 238 | @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 Burns | 1a5e22f | 2019-02-14 15:11:52 -0500 | [diff] [blame] | 247 | NotificationContentInflater.canReapplyRemoteView(mediaView, decoratedMediaView)); |
Selim Cinek | fc8073c | 2017-08-16 17:50:20 -0700 | [diff] [blame] | 248 | } |
| 249 | |
Selim Cinek | 2630dc7 | 2017-04-20 15:16:10 -0700 | [diff] [blame] | 250 | public static void runThenWaitForInflation(Runnable block, |
Ned Burns | 1a5e22f | 2019-02-14 15:11:52 -0500 | [diff] [blame] | 251 | NotificationContentInflater inflater) throws Exception { |
Selim Cinek | 2630dc7 | 2017-04-20 15:16:10 -0700 | [diff] [blame] | 252 | runThenWaitForInflation(block, false /* expectingException */, inflater); |
| 253 | } |
| 254 | |
| 255 | private static void runThenWaitForInflation(Runnable block, boolean expectingException, |
Ned Burns | 1a5e22f | 2019-02-14 15:11:52 -0500 | [diff] [blame] | 256 | NotificationContentInflater inflater) throws Exception { |
Selim Cinek | 2630dc7 | 2017-04-20 15:16:10 -0700 | [diff] [blame] | 257 | CountDownLatch countDownLatch = new CountDownLatch(1); |
| 258 | final ExceptionHolder exceptionHolder = new ExceptionHolder(); |
Ned Burns | 342c3a0 | 2019-02-15 18:08:39 -0500 | [diff] [blame] | 259 | inflater.setInflateSynchronously(true); |
Ned Burns | 1a5e22f | 2019-02-14 15:11:52 -0500 | [diff] [blame] | 260 | inflater.setInflationCallback(new InflationCallback() { |
Selim Cinek | 2630dc7 | 2017-04-20 15:16:10 -0700 | [diff] [blame] | 261 | @Override |
| 262 | public void handleInflationException(StatusBarNotification notification, |
Selim Cinek | 01d3da6 | 2017-04-28 15:03:48 -0700 | [diff] [blame] | 263 | Exception e) { |
Selim Cinek | 2630dc7 | 2017-04-20 15:16:10 -0700 | [diff] [blame] | 264 | if (!expectingException) { |
| 265 | exceptionHolder.setException(e); |
| 266 | } |
| 267 | countDownLatch.countDown(); |
| 268 | } |
| 269 | |
| 270 | @Override |
Ned Burns | f81c4c4 | 2019-01-07 14:10:43 -0500 | [diff] [blame] | 271 | public void onAsyncInflationFinished(NotificationEntry entry, |
Ned Burns | 1a5e22f | 2019-02-14 15:11:52 -0500 | [diff] [blame] | 272 | @NotificationContentInflater.InflationFlag int inflatedFlags) { |
Selim Cinek | 2630dc7 | 2017-04-20 15:16:10 -0700 | [diff] [blame] | 273 | 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 Monk | 6dceace | 2018-05-15 20:24:07 -0400 | [diff] [blame] | 281 | assertTrue(countDownLatch.await(500, TimeUnit.MILLISECONDS)); |
Selim Cinek | 2630dc7 | 2017-04-20 15:16:10 -0700 | [diff] [blame] | 282 | 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 Cinek | d246bed | 2017-06-19 16:58:35 -0700 | [diff] [blame] | 294 | |
| 295 | private class AsyncFailRemoteView extends RemoteViews { |
Jason Monk | 6dceace | 2018-05-15 20:24:07 -0400 | [diff] [blame] | 296 | Handler mHandler = Handler.createAsync(Looper.getMainLooper()); |
Selim Cinek | d246bed | 2017-06-19 16:58:35 -0700 | [diff] [blame] | 297 | |
| 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 Cinek | 2630dc7 | 2017-04-20 15:16:10 -0700 | [diff] [blame] | 320 | } |