1 /*
2  * Copyright (C) 2021 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 package com.android.systemui.statusbar.notification.collection.coordinator
17 
18 import android.service.notification.NotificationListenerService.REASON_APP_CANCEL
19 import android.service.notification.NotificationListenerService.REASON_CANCEL
20 import android.testing.TestableLooper.RunWithLooper
21 import androidx.test.ext.junit.runners.AndroidJUnit4
22 import androidx.test.filters.SmallTest
23 import com.android.systemui.SysuiTestCase
24 import com.android.systemui.log.logcatLogBuffer
25 import com.android.systemui.statusbar.notification.collection.NotifPipeline
26 import com.android.systemui.statusbar.notification.collection.NotificationEntry
27 import com.android.systemui.statusbar.notification.collection.NotificationEntryBuilder
28 import com.android.systemui.statusbar.notification.collection.listbuilder.OnBeforeRenderListListener
29 import com.android.systemui.statusbar.notification.collection.notifcollection.NotifCollectionListener
30 import com.android.systemui.util.mockito.withArgCaptor
31 import org.junit.Before
32 import org.junit.Test
33 import org.junit.runner.RunWith
34 import org.mockito.Mock
35 import org.mockito.Mockito.any
36 import org.mockito.Mockito.never
37 import org.mockito.Mockito.verify
38 import org.mockito.MockitoAnnotations.initMocks
39 import java.util.concurrent.Executor
40 import org.mockito.Mockito.`when` as whenever
41 
42 @SmallTest
43 @RunWith(AndroidJUnit4::class)
44 @RunWithLooper
45 class ShadeEventCoordinatorTest : SysuiTestCase() {
46     private lateinit var coordinator: ShadeEventCoordinator
47     private lateinit var notifCollectionListener: NotifCollectionListener
48     private lateinit var onBeforeRenderListListener: OnBeforeRenderListListener
49 
50     private lateinit var entry1: NotificationEntry
51     private lateinit var entry2: NotificationEntry
52 
53     @Mock private lateinit var pipeline: NotifPipeline
54     private val logger = ShadeEventCoordinatorLogger(logcatLogBuffer())
55     @Mock private lateinit var executor: Executor
56     @Mock private lateinit var notifRemovedByUserCallback: Runnable
57     @Mock private lateinit var shadeEmptiedCallback: Runnable
58 
59     @Before
setUpnull60     fun setUp() {
61         initMocks(this)
62         whenever(executor.execute(any())).then {
63             (it.arguments[0] as Runnable).run()
64             true
65         }
66         coordinator = ShadeEventCoordinator(executor, logger)
67         coordinator.attach(pipeline)
68         notifCollectionListener = withArgCaptor {
69             verify(pipeline).addCollectionListener(capture())
70         }
71         onBeforeRenderListListener = withArgCaptor {
72             verify(pipeline).addOnBeforeRenderListListener(capture())
73         }
74         coordinator.setNotifRemovedByUserCallback(notifRemovedByUserCallback)
75         coordinator.setShadeEmptiedCallback(shadeEmptiedCallback)
76         entry1 = NotificationEntryBuilder().setId(1).build()
77         entry2 = NotificationEntryBuilder().setId(2).build()
78     }
79 
80     @Test
testUserCancelLastNotificationnull81     fun testUserCancelLastNotification() {
82         notifCollectionListener.onEntryRemoved(entry1, REASON_CANCEL)
83         verify(shadeEmptiedCallback, never()).run()
84         verify(notifRemovedByUserCallback, never()).run()
85         onBeforeRenderListListener.onBeforeRenderList(listOf())
86         verify(shadeEmptiedCallback).run()
87         verify(notifRemovedByUserCallback).run()
88     }
89 
90     @Test
testAppCancelLastNotificationnull91     fun testAppCancelLastNotification() {
92         notifCollectionListener.onEntryRemoved(entry1, REASON_APP_CANCEL)
93         onBeforeRenderListListener.onBeforeRenderList(listOf())
94         verify(shadeEmptiedCallback).run()
95         verify(notifRemovedByUserCallback, never()).run()
96     }
97 
98     @Test
testUserCancelOneOfTwoNotificationsnull99     fun testUserCancelOneOfTwoNotifications() {
100         notifCollectionListener.onEntryRemoved(entry1, REASON_CANCEL)
101         onBeforeRenderListListener.onBeforeRenderList(listOf(entry2))
102         verify(shadeEmptiedCallback, never()).run()
103         verify(notifRemovedByUserCallback).run()
104     }
105 
106     @Test
testAppCancelOneOfTwoNotificationsnull107     fun testAppCancelOneOfTwoNotifications() {
108         notifCollectionListener.onEntryRemoved(entry1, REASON_APP_CANCEL)
109         onBeforeRenderListListener.onBeforeRenderList(listOf(entry2))
110         verify(shadeEmptiedCallback, never()).run()
111         verify(notifRemovedByUserCallback, never()).run()
112     }
113 }
114