1 /*
2  * Copyright (C) 2024 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.NotifPipeline
24 import com.android.systemui.statusbar.notification.collection.NotificationEntryBuilder
25 import com.android.systemui.statusbar.notification.collection.listbuilder.OnAfterRenderEntryListener
26 import com.android.systemui.statusbar.notification.collection.listbuilder.OnBeforeFinalizeFilterListener
27 import com.android.systemui.statusbar.notification.collection.render.NotifRowController
28 import com.android.systemui.util.mockito.mock
29 import com.android.systemui.util.mockito.withArgCaptor
30 import com.google.common.truth.Truth.assertThat
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.verify
36 import org.mockito.Mockito.verifyNoMoreInteractions
37 import org.mockito.MockitoAnnotations.initMocks
38 
39 @SmallTest
40 @RunWith(AndroidJUnit4::class)
41 @RunWithLooper
42 class RowAlertTimeCoordinatorTest : SysuiTestCase() {
43     private lateinit var coordinator: RowAlertTimeCoordinator
44     private lateinit var beforeFinalizeFilterListener: OnBeforeFinalizeFilterListener
45     private lateinit var afterRenderEntryListener: OnAfterRenderEntryListener
46 
47     @Mock private lateinit var pipeline: NotifPipeline
48 
49     @Before
setUpnull50     fun setUp() {
51         initMocks(this)
52         coordinator = RowAlertTimeCoordinator()
53         coordinator.attach(pipeline)
54         beforeFinalizeFilterListener = withArgCaptor {
55             verify(pipeline).addOnBeforeFinalizeFilterListener(capture())
56         }
57         afterRenderEntryListener = withArgCaptor {
58             verify(pipeline).addOnAfterRenderEntryListener(capture())
59         }
60     }
61 
62     @Test
testSetLastAudiblyAlertednull63     fun testSetLastAudiblyAlerted() {
64         val entry1 = NotificationEntryBuilder().setLastAudiblyAlertedMs(10).build()
65         val entry2 = NotificationEntryBuilder().setLastAudiblyAlertedMs(20).build()
66         val summary = NotificationEntryBuilder().setLastAudiblyAlertedMs(5).build()
67         val child1 = NotificationEntryBuilder().setLastAudiblyAlertedMs(0).build()
68         val child2 = NotificationEntryBuilder().setLastAudiblyAlertedMs(8).build()
69         val group =
70             GroupEntryBuilder()
71                 .setKey("group")
72                 .setSummary(summary)
73                 .addChild(child1)
74                 .addChild(child2)
75                 .build()
76 
77         val entries = listOf(entry1, summary, child1, child2, entry2)
78 
79         beforeFinalizeFilterListener.onBeforeFinalizeFilter(listOf(entry1, group, entry2))
80         val actualTimesSet =
81             entries.associateWith {
82                 val rowController = mock<NotifRowController>()
83                 afterRenderEntryListener.onAfterRenderEntry(it, rowController)
84                 withArgCaptor<Long> {
85                     verify(rowController).setLastAudibleMs(capture())
86                     verifyNoMoreInteractions(rowController)
87                 }
88             }
89         val expectedTimesSet =
90             mapOf(
91                 entry1 to 10L,
92                 entry2 to 20L,
93                 summary to 8L,
94                 child1 to 0L,
95                 child2 to 8L,
96             )
97         assertThat(actualTimesSet).containsExactlyEntriesIn(expectedTimesSet)
98     }
99 }
100