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 
17 package com.android.systemui.qs.external
18 
19 import android.app.StatusBarManager
20 import android.testing.AndroidTestingRunner
21 import android.testing.TestableLooper
22 import androidx.test.filters.SmallTest
23 import com.android.internal.logging.InstanceId
24 import com.android.internal.logging.UiEventLogger
25 import com.android.internal.logging.testing.UiEventLoggerFake
26 import com.android.systemui.InstanceIdSequenceFake
27 import com.android.systemui.SysuiTestCase
28 import com.google.common.truth.Truth.assertThat
29 import org.junit.Before
30 import org.junit.Test
31 import org.junit.runner.RunWith
32 
33 @SmallTest
34 @RunWith(AndroidTestingRunner::class)
35 @TestableLooper.RunWithLooper
36 class TileRequestDialogEventLoggerTest : SysuiTestCase() {
37 
38     companion object {
39         private const val PACKAGE_NAME = "package"
40     }
41 
42     private lateinit var uiEventLogger: UiEventLoggerFake
43     private val instanceIdSequence =
44             InstanceIdSequenceFake(TileRequestDialogEventLogger.MAX_INSTANCE_ID)
45     private lateinit var logger: TileRequestDialogEventLogger
46 
47     @Before
setUpnull48     fun setUp() {
49         uiEventLogger = UiEventLoggerFake()
50 
51         logger = TileRequestDialogEventLogger(uiEventLogger, instanceIdSequence)
52     }
53 
54     @Test
testInstanceIdsFromSequencenull55     fun testInstanceIdsFromSequence() {
56         (1..10).forEach {
57             assertThat(logger.newInstanceId().id).isEqualTo(instanceIdSequence.lastInstanceId)
58         }
59     }
60 
61     @Test
testLogTileAlreadyAddednull62     fun testLogTileAlreadyAdded() {
63         val instanceId = instanceIdSequence.newInstanceId()
64         logger.logTileAlreadyAdded(PACKAGE_NAME, instanceId)
65 
66         assertThat(uiEventLogger.numLogs()).isEqualTo(1)
67         uiEventLogger[0].match(
68                 TileRequestDialogEvent.TILE_REQUEST_DIALOG_TILE_ALREADY_ADDED,
69                 instanceId
70         )
71     }
72 
73     @Test
testLogDialogShownnull74     fun testLogDialogShown() {
75         val instanceId = instanceIdSequence.newInstanceId()
76         logger.logDialogShown(PACKAGE_NAME, instanceId)
77 
78         assertThat(uiEventLogger.numLogs()).isEqualTo(1)
79         uiEventLogger[0].match(TileRequestDialogEvent.TILE_REQUEST_DIALOG_SHOWN, instanceId)
80     }
81 
82     @Test
testLogDialogDismissednull83     fun testLogDialogDismissed() {
84         val instanceId = instanceIdSequence.newInstanceId()
85         logger.logUserResponse(
86                 StatusBarManager.TILE_ADD_REQUEST_RESULT_DIALOG_DISMISSED,
87                 PACKAGE_NAME,
88                 instanceId
89         )
90 
91         assertThat(uiEventLogger.numLogs()).isEqualTo(1)
92         uiEventLogger[0].match(TileRequestDialogEvent.TILE_REQUEST_DIALOG_DISMISSED, instanceId)
93     }
94 
95     @Test
testLogDialogTileNotAddednull96     fun testLogDialogTileNotAdded() {
97         val instanceId = instanceIdSequence.newInstanceId()
98         logger.logUserResponse(
99                 StatusBarManager.TILE_ADD_REQUEST_RESULT_TILE_NOT_ADDED,
100                 PACKAGE_NAME,
101                 instanceId
102         )
103 
104         assertThat(uiEventLogger.numLogs()).isEqualTo(1)
105         uiEventLogger[0]
106                 .match(TileRequestDialogEvent.TILE_REQUEST_DIALOG_TILE_NOT_ADDED, instanceId)
107     }
108 
109     @Test
testLogDialogTileAddednull110     fun testLogDialogTileAdded() {
111         val instanceId = instanceIdSequence.newInstanceId()
112         logger.logUserResponse(
113                 StatusBarManager.TILE_ADD_REQUEST_RESULT_TILE_ADDED,
114                 PACKAGE_NAME,
115                 instanceId
116         )
117 
118         assertThat(uiEventLogger.numLogs()).isEqualTo(1)
119         uiEventLogger[0].match(TileRequestDialogEvent.TILE_REQUEST_DIALOG_TILE_ADDED, instanceId)
120     }
121 
122     @Test(expected = IllegalArgumentException::class)
testLogResponseInvalid_throwsnull123     fun testLogResponseInvalid_throws() {
124         val instanceId = instanceIdSequence.newInstanceId()
125         logger.logUserResponse(
126                 -1,
127                 PACKAGE_NAME,
128                 instanceId
129         )
130     }
131 
matchnull132     private fun UiEventLoggerFake.FakeUiEvent.match(
133         event: UiEventLogger.UiEventEnum,
134         instanceId: InstanceId
135     ) {
136         assertThat(eventId).isEqualTo(event.id)
137         assertThat(uid).isEqualTo(0)
138         assertThat(packageName).isEqualTo(PACKAGE_NAME)
139         assertThat(this.instanceId).isEqualTo(instanceId)
140     }
141 }