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.testing.TestableLooper.RunWithLooper
19 import androidx.test.ext.junit.runners.AndroidJUnit4
20 import androidx.test.filters.SmallTest
21 import com.android.systemui.SysuiTestCase
22 import com.android.systemui.statusbar.notification.collection.GroupEntryBuilder
23 import com.android.systemui.statusbar.notification.collection.NotifLiveDataStoreImpl
24 import com.android.systemui.statusbar.notification.collection.NotifPipeline
25 import com.android.systemui.statusbar.notification.collection.NotificationEntry
26 import com.android.systemui.statusbar.notification.collection.NotificationEntryBuilder
27 import com.android.systemui.statusbar.notification.collection.listbuilder.NotifSection
28 import com.android.systemui.statusbar.notification.collection.listbuilder.OnAfterRenderListListener
29 import com.android.systemui.statusbar.notification.collection.render.NotifStackController
30 import com.android.systemui.util.mockito.withArgCaptor
31 import com.google.common.truth.Truth.assertThat
32 import org.junit.Before
33 import org.junit.Test
34 import org.junit.runner.RunWith
35 import org.mockito.kotlin.eq
36 import org.mockito.kotlin.mock
37 import org.mockito.kotlin.verify
38 import org.mockito.kotlin.verifyNoMoreInteractions
39 
40 @SmallTest
41 @RunWith(AndroidJUnit4::class)
42 @RunWithLooper
43 class DataStoreCoordinatorTest : SysuiTestCase() {
44     private lateinit var coordinator: DataStoreCoordinator
45     private lateinit var afterRenderListListener: OnAfterRenderListListener
46 
47     private lateinit var entry: NotificationEntry
48 
49     private val pipeline: NotifPipeline = mock()
50     private val notifLiveDataStoreImpl: NotifLiveDataStoreImpl = mock()
51     private val stackController: NotifStackController = mock()
52     private val section: NotifSection = mock()
53 
54     @Before
setUpnull55     fun setUp() {
56         entry = NotificationEntryBuilder().setSection(section).build()
57         coordinator = DataStoreCoordinator(notifLiveDataStoreImpl)
58         coordinator.attach(pipeline)
59         afterRenderListListener = withArgCaptor {
60             verify(pipeline).addOnAfterRenderListListener(capture())
61         }
62     }
63 
64     @Test
testUpdateDataStore_withOneEntrynull65     fun testUpdateDataStore_withOneEntry() {
66         afterRenderListListener.onAfterRenderList(listOf(entry), stackController)
67         verify(notifLiveDataStoreImpl).setActiveNotifList(eq(listOf(entry)))
68         verifyNoMoreInteractions(notifLiveDataStoreImpl)
69     }
70 
71     @Test
testUpdateDataStore_withGroupsnull72     fun testUpdateDataStore_withGroups() {
73         afterRenderListListener.onAfterRenderList(
74             listOf(
75                 notificationEntry("foo", 1),
76                 notificationEntry("foo", 2),
77                 GroupEntryBuilder()
78                     .setSummary(notificationEntry("bar", 1))
79                     .setChildren(
80                         listOf(
81                             notificationEntry("bar", 2),
82                             notificationEntry("bar", 3),
83                             notificationEntry("bar", 4),
84                         )
85                     )
86                     .setSection(section)
87                     .build(),
88                 notificationEntry("baz", 1),
89             ),
90             stackController,
91         )
92         val list: List<NotificationEntry> = withArgCaptor {
93             verify(notifLiveDataStoreImpl).setActiveNotifList(capture())
94         }
95         assertThat(list.map { it.key })
96             .containsExactly(
97                 "0|foo|1|null|0",
98                 "0|foo|2|null|0",
99                 "0|bar|1|null|0",
100                 "0|bar|2|null|0",
101                 "0|bar|3|null|0",
102                 "0|bar|4|null|0",
103                 "0|baz|1|null|0",
104             )
105             .inOrder()
106         verifyNoMoreInteractions(notifLiveDataStoreImpl)
107     }
108 
notificationEntrynull109     private fun notificationEntry(pkg: String, id: Int) =
110         NotificationEntryBuilder().setPkg(pkg).setId(id).setSection(section).build()
111 
112     @Test
113     fun testUpdateDataStore_withZeroEntries_whenNewPipelineEnabled() {
114         afterRenderListListener.onAfterRenderList(listOf(), stackController)
115         verify(notifLiveDataStoreImpl).setActiveNotifList(eq(listOf()))
116         verifyNoMoreInteractions(notifLiveDataStoreImpl)
117     }
118 }
119