summaryrefslogtreecommitdiff
path: root/packages/SystemUI/tests/src/com/android/systemui/statusbar/notification/row/NotificationEntryManagerInflationTest.java
blob: 2d9397e2dd7ebe2fc29aadcb1f92826ae43484bb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
/*
 * Copyright (C) 2020 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.systemui.statusbar.notification.row;

import static android.app.NotificationManager.IMPORTANCE_DEFAULT;

import static com.android.systemui.statusbar.notification.row.NotificationRowContentBinder.FLAG_CONTENT_VIEW_HEADS_UP;

import static junit.framework.Assert.assertNotNull;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import android.app.Notification;
import android.content.Context;
import android.content.pm.LauncherApps;
import android.os.Handler;
import android.service.notification.NotificationListenerService;
import android.service.notification.NotificationListenerService.Ranking;
import android.service.notification.StatusBarNotification;
import android.testing.AndroidTestingRunner;
import android.testing.TestableLooper;

import androidx.asynclayoutinflater.view.AsyncLayoutInflater;
import androidx.test.filters.SmallTest;

import com.android.internal.logging.MetricsLogger;
import com.android.internal.statusbar.IStatusBarService;
import com.android.internal.util.NotificationMessagingUtil;
import com.android.systemui.R;
import com.android.systemui.SysuiTestCase;
import com.android.systemui.classifier.FalsingCollectorFake;
import com.android.systemui.classifier.FalsingManagerFake;
import com.android.systemui.dump.DumpManager;
import com.android.systemui.flags.FeatureFlags;
import com.android.systemui.media.MediaFeatureFlag;
import com.android.systemui.media.dialog.MediaOutputDialogFactory;
import com.android.systemui.plugins.statusbar.StatusBarStateController;
import com.android.systemui.shared.plugins.PluginManager;
import com.android.systemui.statusbar.NotificationListener;
import com.android.systemui.statusbar.NotificationLockscreenUserManager;
import com.android.systemui.statusbar.NotificationMediaManager;
import com.android.systemui.statusbar.NotificationPresenter;
import com.android.systemui.statusbar.NotificationRemoteInputManager;
import com.android.systemui.statusbar.SbnBuilder;
import com.android.systemui.statusbar.SmartReplyController;
import com.android.systemui.statusbar.notification.ConversationNotificationProcessor;
import com.android.systemui.statusbar.notification.NotifPipelineFlags;
import com.android.systemui.statusbar.notification.NotificationClicker;
import com.android.systemui.statusbar.notification.NotificationEntryListener;
import com.android.systemui.statusbar.notification.NotificationEntryManager;
import com.android.systemui.statusbar.notification.NotificationEntryManagerLogger;
import com.android.systemui.statusbar.notification.NotificationFilter;
import com.android.systemui.statusbar.notification.NotificationSectionsFeatureManager;
import com.android.systemui.statusbar.notification.collection.NotifLiveDataStoreMocksKt;
import com.android.systemui.statusbar.notification.collection.NotificationEntry;
import com.android.systemui.statusbar.notification.collection.NotificationRankingManager;
import com.android.systemui.statusbar.notification.collection.inflation.NotificationRowBinderImpl;
import com.android.systemui.statusbar.notification.collection.legacy.LowPriorityInflationHelper;
import com.android.systemui.statusbar.notification.collection.legacy.NotificationGroupManagerLegacy;
import com.android.systemui.statusbar.notification.collection.provider.HighPriorityProvider;
import com.android.systemui.statusbar.notification.icon.IconBuilder;
import com.android.systemui.statusbar.notification.icon.IconManager;
import com.android.systemui.statusbar.notification.interruption.NotificationInterruptStateProvider;
import com.android.systemui.statusbar.notification.logging.NotificationLogger;
import com.android.systemui.statusbar.notification.people.PeopleNotificationIdentifier;
import com.android.systemui.statusbar.notification.row.dagger.ExpandableNotificationRowComponent;
import com.android.systemui.statusbar.notification.row.dagger.NotificationRowComponent;
import com.android.systemui.statusbar.notification.stack.NotificationListContainer;
import com.android.systemui.statusbar.phone.KeyguardBypassController;
import com.android.systemui.statusbar.policy.HeadsUpManager;
import com.android.systemui.statusbar.policy.InflatedSmartReplyState;
import com.android.systemui.statusbar.policy.InflatedSmartReplyViewHolder;
import com.android.systemui.statusbar.policy.SmartReplyConstants;
import com.android.systemui.statusbar.policy.SmartReplyStateInflater;
import com.android.systemui.statusbar.policy.dagger.RemoteInputViewSubcomponent;
import com.android.systemui.util.concurrency.FakeExecutor;
import com.android.systemui.util.leak.LeakDetector;
import com.android.systemui.util.time.FakeSystemClock;
import com.android.systemui.wmshell.BubblesManager;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Answers;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.mockito.stubbing.Answer;

import java.util.Optional;
import java.util.concurrent.CountDownLatch;

/**
 * Functional tests for notification inflation from {@link NotificationEntryManager}.
 */
@SmallTest
@RunWith(AndroidTestingRunner.class)
@TestableLooper.RunWithLooper(setAsMainLooper = true)
public class NotificationEntryManagerInflationTest extends SysuiTestCase {

    private static final String TEST_TITLE = "Title";
    private static final String TEST_TEXT = "Text";
    private static final long TIMEOUT_TIME = 10000;
    private static final Runnable TIMEOUT_RUNNABLE = () -> {
        throw new RuntimeException("Timed out waiting to inflate");
    };

    @Mock private NotificationListener mNotificationListener;
    @Mock private NotificationPresenter mPresenter;
    @Mock private NotificationEntryManager.KeyguardEnvironment mEnvironment;
    @Mock private NotificationListContainer mListContainer;
    @Mock private NotificationEntryListener mEntryListener;
    @Mock private NotificationRowBinderImpl.BindRowCallback mBindCallback;
    @Mock private HeadsUpManager mHeadsUpManager;
    @Mock private NotificationInterruptStateProvider mNotificationInterruptionStateProvider;
    @Mock private NotificationLockscreenUserManager mLockscreenUserManager;
    @Mock private NotificationGutsManager mGutsManager;
    @Mock private NotificationRemoteInputManager mRemoteInputManager;
    @Mock private NotificationMediaManager mNotificationMediaManager;
    @Mock(answer = Answers.RETURNS_SELF)
    private ExpandableNotificationRowComponent.Builder mExpandableNotificationRowComponentBuilder;
    @Mock private ExpandableNotificationRowComponent mExpandableNotificationRowComponent;
    @Mock private KeyguardBypassController mKeyguardBypassController;
    @Mock private StatusBarStateController mStatusBarStateController;

    @Mock private NotificationGroupManagerLegacy mGroupMembershipManager;
    @Mock private NotificationGroupManagerLegacy mGroupExpansionManager;
    @Mock private NotifPipelineFlags mNotifPipelineFlags;
    @Mock private LeakDetector mLeakDetector;

    @Mock private ActivatableNotificationViewController mActivatableNotificationViewController;
    @Mock private NotificationRowComponent.Builder mNotificationRowComponentBuilder;
    @Mock private PeopleNotificationIdentifier mPeopleNotificationIdentifier;
    @Mock private InflatedSmartReplyState mInflatedSmartReplyState;
    @Mock private InflatedSmartReplyViewHolder mInflatedSmartReplies;

    private StatusBarNotification mSbn;
    private NotificationListenerService.RankingMap mRankingMap;
    private NotificationEntryManager mEntryManager;
    private NotificationRowBinderImpl mRowBinder;
    private Handler mHandler;
    private FakeExecutor mBgExecutor;
    private RowContentBindStage mRowContentBindStage;

    @Before
    public void setUp() {
        MockitoAnnotations.initMocks(this);
        mDependency.injectMockDependency(SmartReplyController.class);
        mDependency.injectMockDependency(MediaOutputDialogFactory.class);

        mHandler = Handler.createAsync(TestableLooper.get(this).getLooper());

        // Add an action so heads up content views are made
        Notification.Action action = new Notification.Action.Builder(null, null, null).build();
        Notification notification = new Notification.Builder(mContext)
                .setSmallIcon(R.drawable.ic_person)
                .setContentTitle(TEST_TITLE)
                .setContentText(TEST_TEXT)
                .setActions(action)
                .build();
        mSbn = new SbnBuilder()
                .setNotification(notification)
                .build();

        when(mNotifPipelineFlags.checkLegacyPipelineEnabled()).thenReturn(true);
        when(mNotifPipelineFlags.isNewPipelineEnabled()).thenReturn(false);

        mEntryManager = new NotificationEntryManager(
                mock(NotificationEntryManagerLogger.class),
                mGroupMembershipManager,
                mNotifPipelineFlags,
                () -> mRowBinder,
                () -> mRemoteInputManager,
                mLeakDetector,
                mock(IStatusBarService.class),
                NotifLiveDataStoreMocksKt.createNotifLiveDataStoreImplMock(),
                mock(DumpManager.class)
        );
        mEntryManager.initialize(
                mNotificationListener,
                new NotificationRankingManager(
                        () -> mock(NotificationMediaManager.class),
                        mGroupMembershipManager,
                        mHeadsUpManager,
                        mock(NotificationFilter.class),
                        mock(NotificationEntryManagerLogger.class),
                        mock(NotificationSectionsFeatureManager.class),
                        mock(PeopleNotificationIdentifier.class),
                        mock(HighPriorityProvider.class),
                        mEnvironment));

        NotifRemoteViewCache cache = new NotifRemoteViewCacheImpl(mEntryManager);
        NotifBindPipeline pipeline = new NotifBindPipeline(
                mEntryManager,
                mock(NotifBindPipelineLogger.class),
                TestableLooper.get(this).getLooper());
        mBgExecutor = new FakeExecutor(new FakeSystemClock());
        NotificationContentInflater binder = new NotificationContentInflater(
                cache,
                mRemoteInputManager,
                mock(ConversationNotificationProcessor.class),
                mock(MediaFeatureFlag.class),
                mBgExecutor,
                new SmartReplyStateInflater() {
                    @Override
                    public InflatedSmartReplyState inflateSmartReplyState(NotificationEntry entry) {
                        return mInflatedSmartReplyState;
                    }

                    @Override
                    public InflatedSmartReplyViewHolder inflateSmartReplyViewHolder(
                            Context sysuiContext, Context notifPackageContext,
                            NotificationEntry entry,
                            InflatedSmartReplyState existingSmartReplyState,
                            InflatedSmartReplyState newSmartReplyState) {
                        return mInflatedSmartReplies;
                    }
                });
        mRowContentBindStage = new RowContentBindStage(
                binder,
                mock(NotifInflationErrorManager.class),
                mock(RowContentBindStageLogger.class));
        pipeline.setStage(mRowContentBindStage);

        ArgumentCaptor<ExpandableNotificationRow> viewCaptor =
                ArgumentCaptor.forClass(ExpandableNotificationRow.class);
        when(mExpandableNotificationRowComponentBuilder
                .expandableNotificationRow(viewCaptor.capture()))
                .thenReturn(mExpandableNotificationRowComponentBuilder);
        when(mExpandableNotificationRowComponentBuilder.build())
                .thenReturn(mExpandableNotificationRowComponent);

        when(mExpandableNotificationRowComponent.getExpandableNotificationRowController())
                .thenAnswer((Answer<ExpandableNotificationRowController>) invocation ->
                        new ExpandableNotificationRowController(
                                viewCaptor.getValue(),
                                mock(ActivatableNotificationViewController.class),
                                mock(RemoteInputViewSubcomponent.Factory.class),
                                mock(MetricsLogger.class),
                                mListContainer,
                                mNotificationMediaManager,
                                mock(SmartReplyConstants.class),
                                mock(SmartReplyController.class),
                                mock(PluginManager.class),
                                new FakeSystemClock(),
                                "FOOBAR",
                                "FOOBAR",
                                mKeyguardBypassController,
                                mGroupMembershipManager,
                                mGroupExpansionManager,
                                mRowContentBindStage,
                                mock(NotificationLogger.class),
                                mHeadsUpManager,
                                mPresenter,
                                mStatusBarStateController,
                                mGutsManager,
                                true,
                                null,
                                new FalsingManagerFake(),
                                new FalsingCollectorFake(),
                                mock(FeatureFlags.class),
                                mPeopleNotificationIdentifier,
                                Optional.of(mock(BubblesManager.class)),
                                mock(ExpandableNotificationRowDragController.class),
                                mock(IStatusBarService.class)));

        when(mNotificationRowComponentBuilder.activatableNotificationView(any()))
                .thenReturn(mNotificationRowComponentBuilder);
        when(mNotificationRowComponentBuilder.build()).thenReturn(
                () -> mActivatableNotificationViewController);

        mRowBinder = new NotificationRowBinderImpl(
                mContext,
                new NotificationMessagingUtil(mContext),
                mRemoteInputManager,
                mLockscreenUserManager,
                pipeline,
                mRowContentBindStage,
                RowInflaterTask::new,
                mExpandableNotificationRowComponentBuilder,
                new IconManager(
                        mEntryManager,
                        mock(LauncherApps.class),
                        new IconBuilder(mContext)),
                mock(LowPriorityInflationHelper.class),
                mNotifPipelineFlags);

        mEntryManager.setUpWithPresenter(mPresenter);
        mEntryManager.addNotificationEntryListener(mEntryListener);

        mRowBinder.setUpWithPresenter(mPresenter, mListContainer, mBindCallback);
        mRowBinder.setNotificationClicker(mock(NotificationClicker.class));

        Ranking ranking = new Ranking();
        ranking.populate(
                mSbn.getKey(),
                0,
                false,
                0,
                0,
                IMPORTANCE_DEFAULT,
                null,
                null,
                null,
                null,
                null,
                true,
                Ranking.USER_SENTIMENT_NEUTRAL,
                false,
                -1,
                false,
                null,
                null,
                false,
                false,
                false,
                null,
                0,
                false
            );
        mRankingMap = new NotificationListenerService.RankingMap(new Ranking[] {ranking});

        TestableLooper.get(this).processAllMessages();
    }

    @After
    public void cleanUp() {
        // Don't leave anything on main thread
        TestableLooper.get(this).processAllMessages();
    }

    @Test
    public void testAddNotification() {
        // WHEN a notification is added
        mEntryManager.addNotification(mSbn, mRankingMap);
        ArgumentCaptor<NotificationEntry> entryCaptor = ArgumentCaptor.forClass(
                NotificationEntry.class);
        verify(mEntryListener).onPendingEntryAdded(entryCaptor.capture());
        NotificationEntry entry = entryCaptor.getValue();

        waitForInflation();

        // THEN the notification has its row inflated
        assertNotNull(entry.getRow());
        assertNotNull(entry.getRow().getPrivateLayout().getContractedChild());

        // THEN inflation callbacks are called
        verify(mBindCallback).onBindRow(entry.getRow());
        verify(mEntryListener, never()).onInflationError(any(), any());
        verify(mEntryListener).onEntryInflated(entry);
        verify(mEntryListener).onNotificationAdded(entry);

        // THEN the notification is active
        assertNotNull(mEntryManager.getActiveNotificationUnfiltered(mSbn.getKey()));

        // THEN we update the presenter
        verify(mPresenter).updateNotificationViews(any());
    }

    @Test
    public void testUpdateNotification() {
        // GIVEN a notification already added
        mEntryManager.addNotification(mSbn, mRankingMap);
        ArgumentCaptor<NotificationEntry> entryCaptor = ArgumentCaptor.forClass(
                NotificationEntry.class);
        verify(mEntryListener).onPendingEntryAdded(entryCaptor.capture());
        NotificationEntry entry = entryCaptor.getValue();
        waitForInflation();

        Mockito.reset(mEntryListener);
        Mockito.reset(mPresenter);

        // WHEN the notification is updated
        mEntryManager.updateNotification(mSbn, mRankingMap);

        waitForInflation();

        // THEN the notification has its row and inflated
        assertNotNull(entry.getRow());

        // THEN inflation callbacks are called
        verify(mEntryListener, never()).onInflationError(any(), any());
        verify(mEntryListener).onEntryReinflated(entry);

        // THEN we update the presenter
        verify(mPresenter).updateNotificationViews(any());
    }

    @Test
    public void testContentViewInflationDuringRowInflationInflatesCorrectViews() {
        // GIVEN a notification is added and the row is inflating
        mEntryManager.addNotification(mSbn, mRankingMap);
        ArgumentCaptor<NotificationEntry> entryCaptor = ArgumentCaptor.forClass(
                NotificationEntry.class);
        verify(mEntryListener).onPendingEntryAdded(entryCaptor.capture());
        NotificationEntry entry = entryCaptor.getValue();

        // WHEN we try to bind a content view
        mRowContentBindStage.getStageParams(entry).requireContentViews(FLAG_CONTENT_VIEW_HEADS_UP);
        mRowContentBindStage.requestRebind(entry, null);

        waitForInflation();

        // THEN the notification has its row and all relevant content views inflated
        assertNotNull(entry.getRow());
        assertNotNull(entry.getRow().getPrivateLayout().getContractedChild());
        assertNotNull(entry.getRow().getPrivateLayout().getHeadsUpChild());
    }

    /**
     * Wait for inflation to finish.
     *
     * A few things to note
     * 1) Row inflation is done via {@link AsyncLayoutInflater} on its own background thread that
     * calls back to main thread which is why we wait on main thread.
     * 2) Row *content* inflation is done on the {@link FakeExecutor} we pass in in this test class
     * so we control when that work is done. The callback is still always on the main thread.
     */
    private void waitForInflation() {
        mHandler.postDelayed(TIMEOUT_RUNNABLE, TIMEOUT_TIME);
        final CountDownLatch latch = new CountDownLatch(1);
        NotificationEntryListener inflationListener = new NotificationEntryListener() {
            @Override
            public void onEntryInflated(NotificationEntry entry) {
                latch.countDown();
            }

            @Override
            public void onEntryReinflated(NotificationEntry entry) {
                latch.countDown();
            }

            @Override
            public void onInflationError(StatusBarNotification notification, Exception exception) {
                latch.countDown();
            }
        };
        mEntryManager.addNotificationEntryListener(inflationListener);
        while (latch.getCount() != 0) {
            mBgExecutor.runAllReady();
            TestableLooper.get(this).processMessages(1);
        }
        mHandler.removeCallbacks(TIMEOUT_RUNNABLE);
        mEntryManager.removeNotificationEntryListener(inflationListener);
    }

}