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 17 package com.android.systemui.qs.tiles.base.logging 18 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.common.shared.model.ContentDescription 23 import com.android.systemui.common.shared.model.Icon 24 import com.android.systemui.log.LogBuffer 25 import com.android.systemui.log.LogBufferFactory 26 import com.android.systemui.log.LogcatEchoTrackerAlways 27 import com.android.systemui.plugins.statusbar.StatusBarStateController 28 import com.android.systemui.qs.pipeline.shared.TileSpec 29 import com.android.systemui.qs.tiles.viewmodel.QSTileState 30 import com.android.systemui.qs.tiles.viewmodel.QSTileUserAction 31 import com.android.systemui.util.mockito.any 32 import com.android.systemui.util.mockito.whenever 33 import com.google.common.truth.Truth.assertThat 34 import java.io.PrintWriter 35 import java.io.StringWriter 36 import org.junit.Before 37 import org.junit.Test 38 import org.junit.runner.RunWith 39 import org.mockito.Mock 40 import org.mockito.MockitoAnnotations 41 42 @SmallTest 43 @RunWith(AndroidJUnit4::class) 44 class QSTileLoggerTest : SysuiTestCase() { 45 46 @Mock private lateinit var statusBarController: StatusBarStateController 47 @Mock private lateinit var logBufferFactory: LogBufferFactory 48 49 private val chattyLogBuffer = LogBuffer("TestChatty", 5, LogcatEchoTrackerAlways()) 50 private val logBuffer = LogBuffer("Test", 1, LogcatEchoTrackerAlways()) 51 52 private lateinit var underTest: QSTileLogger 53 54 @Before setupnull55 fun setup() { 56 MockitoAnnotations.initMocks(this) 57 whenever(logBufferFactory.create(any(), any(), any(), any())).thenReturn(logBuffer) 58 val tileSpec: TileSpec = TileSpec.create("chatty_tile") 59 underTest = 60 QSTileLogger(mapOf(tileSpec to chattyLogBuffer), logBufferFactory, statusBarController) 61 } 62 63 @Test testChattyLognull64 fun testChattyLog() { 65 underTest.logUserActionRejectedByFalsing( 66 QSTileUserAction.Click(null), 67 TileSpec.create("chatty_tile"), 68 ) 69 underTest.logUserActionRejectedByFalsing( 70 QSTileUserAction.Click(null), 71 TileSpec.create("chatty_tile"), 72 ) 73 74 val logs = chattyLogBuffer.getStringBuffer().lines().filter { it.isNotBlank() } 75 assertThat(logs).hasSize(2) 76 logs.forEach { assertThat(it).contains("tile click: rejected by falsing") } 77 } 78 79 @Test testLogUserActionnull80 fun testLogUserAction() { 81 underTest.logUserAction( 82 QSTileUserAction.Click(null), 83 TileSpec.create("test_spec"), 84 hasData = false, 85 hasTileState = false, 86 ) 87 88 assertThat(logBuffer.getStringBuffer()) 89 .contains("tile click: statusBarState=SHADE, hasState=false, hasData=false") 90 } 91 92 @Test testLogUserActionRejectedByFalsingnull93 fun testLogUserActionRejectedByFalsing() { 94 underTest.logUserActionRejectedByFalsing( 95 QSTileUserAction.Click(null), 96 TileSpec.create("test_spec"), 97 ) 98 99 assertThat(logBuffer.getStringBuffer()).contains("tile click: rejected by falsing") 100 } 101 102 @Test testLogUserActionRejectedByPolicynull103 fun testLogUserActionRejectedByPolicy() { 104 underTest.logUserActionRejectedByPolicy( 105 QSTileUserAction.Click(null), 106 TileSpec.create("test_spec"), 107 "test_restriction", 108 ) 109 110 assertThat(logBuffer.getStringBuffer()).contains("tile click: rejected by policy") 111 } 112 113 @Test testLogUserActionPipelinenull114 fun testLogUserActionPipeline() { 115 underTest.logUserActionPipeline( 116 TileSpec.create("test_spec"), 117 QSTileUserAction.Click(null), 118 QSTileState.build(Icon.Resource(0, ContentDescription.Resource(0)), "") {}, 119 "test_data", 120 ) 121 122 assertThat(logBuffer.getStringBuffer()) 123 .contains( 124 "tile click pipeline: " + 125 "statusBarState=SHADE, " + 126 "state=[" + 127 "label=, " + 128 "state=INACTIVE, " + 129 "s_label=null, " + 130 "cd=null, " + 131 "sd=null, " + 132 "svi=None, " + 133 "enabled=ENABLED, " + 134 "a11y=android.widget.Switch" + 135 "], " + 136 "data=test_data" 137 ) 138 } 139 140 @Test testLogStateUpdatenull141 fun testLogStateUpdate() { 142 underTest.logStateUpdate( 143 TileSpec.create("test_spec"), 144 QSTileState.build(Icon.Resource(0, ContentDescription.Resource(0)), "") {}, 145 "test_data", 146 ) 147 148 assertThat(logBuffer.getStringBuffer()) 149 .contains( 150 "tile state update: " + 151 "state=[label=, " + 152 "state=INACTIVE, " + 153 "s_label=null, " + 154 "cd=null, " + 155 "sd=null, " + 156 "svi=None, " + 157 "enabled=ENABLED, " + 158 "a11y=android.widget.Switch], " + 159 "data=test_data" 160 ) 161 } 162 163 @Test testLogForceUpdatenull164 fun testLogForceUpdate() { 165 underTest.logForceUpdate(TileSpec.create("test_spec")) 166 167 assertThat(logBuffer.getStringBuffer()).contains("tile data force update") 168 } 169 170 @Test testLogInitialUpdatenull171 fun testLogInitialUpdate() { 172 underTest.logInitialRequest(TileSpec.create("test_spec")) 173 174 assertThat(logBuffer.getStringBuffer()).contains("tile data initial update") 175 } 176 LogBuffernull177 private fun LogBuffer.getStringBuffer(): String { 178 val stringWriter = StringWriter() 179 dump(PrintWriter(stringWriter), 0) 180 return stringWriter.buffer.toString() 181 } 182 } 183