1 /*
2  * Copyright (C) 2023 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.notetask
17 
18 import android.os.UserHandle
19 import android.test.suitebuilder.annotation.SmallTest
20 import androidx.test.runner.AndroidJUnit4
21 import com.android.internal.logging.UiEventLogger
22 import com.android.systemui.SysuiTestCase
23 import com.android.systemui.notetask.NoteTaskEventLogger.NoteTaskUiEvent.NOTE_CLOSED_VIA_STYLUS_TAIL_BUTTON
24 import com.android.systemui.notetask.NoteTaskEventLogger.NoteTaskUiEvent.NOTE_CLOSED_VIA_STYLUS_TAIL_BUTTON_LOCKED
25 import com.android.systemui.notetask.NoteTaskEventLogger.NoteTaskUiEvent.NOTE_OPENED_VIA_KEYGUARD_QUICK_AFFORDANCE
26 import com.android.systemui.notetask.NoteTaskEventLogger.NoteTaskUiEvent.NOTE_OPENED_VIA_SHORTCUT
27 import com.android.systemui.notetask.NoteTaskEventLogger.NoteTaskUiEvent.NOTE_OPENED_VIA_STYLUS_TAIL_BUTTON
28 import com.android.systemui.notetask.NoteTaskEventLogger.NoteTaskUiEvent.NOTE_OPENED_VIA_STYLUS_TAIL_BUTTON_LOCKED
29 import org.junit.Before
30 import org.junit.Test
31 import org.junit.runner.RunWith
32 import org.mockito.Mock
33 import org.mockito.Mockito.verify
34 import org.mockito.Mockito.verifyNoMoreInteractions
35 import org.mockito.MockitoAnnotations
36 
37 /** atest SystemUITests:MonitoringNoteTaskEventListenerTest */
38 @SmallTest
39 @RunWith(AndroidJUnit4::class)
40 internal class NoteTaskEventLoggerTest : SysuiTestCase() {
41 
42     @Mock lateinit var uiEventLogger: UiEventLogger
43 
44     private fun createNoteTaskEventLogger(): NoteTaskEventLogger =
45         NoteTaskEventLogger(uiEventLogger)
46 
47     private fun createNoteTaskInfo(): NoteTaskInfo =
48         NoteTaskInfo(packageName = NOTES_PACKAGE_NAME, uid = NOTES_UID, UserHandle.of(0))
49 
50     @Before
51     fun setUp() {
52         MockitoAnnotations.initMocks(this)
53     }
54 
55     // region logNoteTaskOpened
56     @Test
57     fun logNoteTaskOpened_entryPointWidgetPickerShortcut_noteOpenedViaShortcut() {
58         val info = createNoteTaskInfo().copy(entryPoint = NoteTaskEntryPoint.WIDGET_PICKER_SHORTCUT)
59 
60         createNoteTaskEventLogger().logNoteTaskOpened(info)
61 
62         val expected = NOTE_OPENED_VIA_SHORTCUT
63         verify(uiEventLogger).log(expected, info.uid, info.packageName)
64     }
65 
66     @Test
67     fun onNoteTaskBubbleExpanded_entryPointQuickAffordance_noteOpenedViaQuickAffordance() {
68         val info = createNoteTaskInfo().copy(entryPoint = NoteTaskEntryPoint.QUICK_AFFORDANCE)
69 
70         createNoteTaskEventLogger().logNoteTaskOpened(info)
71 
72         val expected = NOTE_OPENED_VIA_KEYGUARD_QUICK_AFFORDANCE
73         verify(uiEventLogger).log(expected, info.uid, info.packageName)
74     }
75 
76     @Test
77     fun onNoteTaskBubbleExpanded_entryPointTailButtonAndIsKeyguardUnlocked_noteOpenedViaTailButtonUnlocked() { // ktlint-disable max-line-length
78         val info =
79             createNoteTaskInfo()
80                 .copy(
81                     entryPoint = NoteTaskEntryPoint.TAIL_BUTTON,
82                     isKeyguardLocked = false,
83                 )
84 
85         createNoteTaskEventLogger().logNoteTaskOpened(info)
86 
87         val expected = NOTE_OPENED_VIA_STYLUS_TAIL_BUTTON
88         verify(uiEventLogger).log(expected, info.uid, info.packageName)
89     }
90 
91     @Test
92     fun onNoteTaskBubbleExpanded_entryPointTailButtonAndIsKeyguardLocked_noteOpenedViaTailButtonLocked() { // ktlint-disable max-line-length
93         val info =
94             createNoteTaskInfo()
95                 .copy(
96                     entryPoint = NoteTaskEntryPoint.TAIL_BUTTON,
97                     isKeyguardLocked = true,
98                 )
99 
100         createNoteTaskEventLogger().logNoteTaskOpened(info)
101 
102         val expected = NOTE_OPENED_VIA_STYLUS_TAIL_BUTTON_LOCKED
103         verify(uiEventLogger).log(expected, info.uid, info.packageName)
104     }
105 
106     @Test
107     fun onNoteTaskBubbleExpanded_noEntryPoint_noLog() {
108         val info = createNoteTaskInfo().copy(entryPoint = null)
109 
110         createNoteTaskEventLogger().logNoteTaskOpened(info)
111 
112         verifyNoMoreInteractions(uiEventLogger)
113     }
114     // endregion
115 
116     // region logNoteTaskClosed
117     @Test
118     fun logNoteTaskClosed_entryPointTailButton_noteClosedViaTailButtonUnlocked() {
119         val info =
120             createNoteTaskInfo()
121                 .copy(
122                     entryPoint = NoteTaskEntryPoint.TAIL_BUTTON,
123                     isKeyguardLocked = false,
124                 )
125 
126         createNoteTaskEventLogger().logNoteTaskClosed(info)
127 
128         val expected = NOTE_CLOSED_VIA_STYLUS_TAIL_BUTTON
129         verify(uiEventLogger).log(expected, info.uid, info.packageName)
130     }
131 
132     @Test
133     fun logNoteTaskClosed_entryPointTailButtonAndKeyguardLocked_noteClosedViaTailButtonLocked() {
134         val info =
135             createNoteTaskInfo()
136                 .copy(
137                     entryPoint = NoteTaskEntryPoint.TAIL_BUTTON,
138                     isKeyguardLocked = true,
139                 )
140 
141         createNoteTaskEventLogger().logNoteTaskClosed(info)
142 
143         val expected = NOTE_CLOSED_VIA_STYLUS_TAIL_BUTTON_LOCKED
144         verify(uiEventLogger).log(expected, info.uid, info.packageName)
145     }
146 
147     @Test
148     fun logNoteTaskClosed_noEntryPoint_noLog() {
149         val info = createNoteTaskInfo().copy(entryPoint = null)
150 
151         createNoteTaskEventLogger().logNoteTaskOpened(info)
152 
153         verifyNoMoreInteractions(uiEventLogger)
154     }
155     // endregion
156 
157     private companion object {
158         const val NOTES_PACKAGE_NAME = "com.android.note.app"
159         const val NOTES_UID = 123456
160     }
161 }
162