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