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.server.bluetooth.test
17 
18 import android.bluetooth.BluetoothProtoEnums.ENABLE_DISABLE_REASON_AIRPLANE_MODE
19 import android.bluetooth.BluetoothProtoEnums.ENABLE_DISABLE_REASON_APPLICATION_REQUEST
20 import android.bluetooth.BluetoothProtoEnums.ENABLE_DISABLE_REASON_CRASH
21 import android.bluetooth.BluetoothProtoEnums.ENABLE_DISABLE_REASON_DISALLOWED
22 import android.bluetooth.BluetoothProtoEnums.ENABLE_DISABLE_REASON_FACTORY_RESET
23 import android.bluetooth.BluetoothProtoEnums.ENABLE_DISABLE_REASON_RESTARTED
24 import android.bluetooth.BluetoothProtoEnums.ENABLE_DISABLE_REASON_RESTORE_USER_SETTING
25 import android.bluetooth.BluetoothProtoEnums.ENABLE_DISABLE_REASON_SATELLITE_MODE
26 import android.bluetooth.BluetoothProtoEnums.ENABLE_DISABLE_REASON_START_ERROR
27 import android.bluetooth.BluetoothProtoEnums.ENABLE_DISABLE_REASON_SYSTEM_BOOT
28 import android.bluetooth.BluetoothProtoEnums.ENABLE_DISABLE_REASON_USER_SWITCH
29 import android.util.proto.ProtoOutputStream
30 import com.android.server.bluetooth.ActiveLogs
31 import com.android.server.bluetooth.Log
32 import com.google.common.truth.Truth.assertThat
33 import java.io.PrintWriter
34 import java.io.StringWriter
35 import org.junit.Before
36 import org.junit.Rule
37 import org.junit.Test
38 import org.junit.rules.TestName
39 import org.junit.runner.RunWith
40 import org.robolectric.RobolectricTestRunner
41 
42 @RunWith(RobolectricTestRunner::class)
43 @kotlinx.coroutines.ExperimentalCoroutinesApi
44 class ActiveLogTest {
45     @get:Rule val testName = TestName()
46 
47     @Before
setUpnull48     fun setUp() {
49         Log.i("ActiveLogTest", "\t--> setup of " + testName.getMethodName())
50         ActiveLogs.activeLogs.clear()
51     }
52 
53     @Test
dump_whenNoActiveLog_indicateNeverEnablednull54     fun dump_whenNoActiveLog_indicateNeverEnabled() {
55         val stringWriter = StringWriter()
56         val writer = PrintWriter(stringWriter)
57 
58         ActiveLogs.dump(writer)
59 
60         assertThat(stringWriter.toString()).isEqualTo("Bluetooth never enabled!\n")
61     }
62 
63     @Test
dump_whenActiveLog_indicateAllnull64     fun dump_whenActiveLog_indicateAll() {
65         val numberOfLogEntry = 3
66         for (i in 1..numberOfLogEntry) {
67             ActiveLogs.add(ENABLE_DISABLE_REASON_START_ERROR, false)
68         }
69         val stringWriter = StringWriter()
70         val writer = PrintWriter(stringWriter)
71 
72         ActiveLogs.dump(writer)
73 
74         assertThat(stringWriter.toString()).matches("Enable log:\n(.*\n){$numberOfLogEntry}")
75     }
76 
77     @Test
dump_overflowQueue_indicateFirstEntriesnull78     fun dump_overflowQueue_indicateFirstEntries() {
79         for (i in 1..ActiveLogs.MAX_ENTRIES_STORED * 2) {
80             ActiveLogs.add(ENABLE_DISABLE_REASON_START_ERROR, false)
81         }
82         val stringWriter = StringWriter()
83         val writer = PrintWriter(stringWriter)
84 
85         ActiveLogs.dump(writer)
86 
87         assertThat(stringWriter.toString())
88             .matches("Enable log:\n(.*\n){${ActiveLogs.MAX_ENTRIES_STORED}}")
89     }
90 
91     @Test
dump_differentState_logsVariationnull92     fun dump_differentState_logsVariation() {
93         ActiveLogs.add(ENABLE_DISABLE_REASON_START_ERROR, false)
94         ActiveLogs.add(ENABLE_DISABLE_REASON_START_ERROR, true, "Foo", true)
95         ActiveLogs.add(ENABLE_DISABLE_REASON_START_ERROR, true)
96         val stringWriter = StringWriter()
97         val writer = PrintWriter(stringWriter)
98 
99         ActiveLogs.dump(writer)
100 
101         assertThat(stringWriter.toString())
102             .matches("Enable log:\n.*Disable.*\n.*EnableBle.*\n.*Enable.*\n")
103     }
104 
105     @Test
dump_allReason_stringIsKnownnull106     fun dump_allReason_stringIsKnown() {
107         ActiveLogs.add(ENABLE_DISABLE_REASON_APPLICATION_REQUEST, false)
108         ActiveLogs.add(ENABLE_DISABLE_REASON_AIRPLANE_MODE, false)
109         ActiveLogs.add(ENABLE_DISABLE_REASON_DISALLOWED, false)
110         ActiveLogs.add(ENABLE_DISABLE_REASON_RESTARTED, false)
111         ActiveLogs.add(ENABLE_DISABLE_REASON_START_ERROR, false)
112         ActiveLogs.add(ENABLE_DISABLE_REASON_SYSTEM_BOOT, false)
113         ActiveLogs.add(ENABLE_DISABLE_REASON_CRASH, false)
114         ActiveLogs.add(ENABLE_DISABLE_REASON_USER_SWITCH, false)
115         ActiveLogs.add(ENABLE_DISABLE_REASON_RESTORE_USER_SETTING, false)
116         ActiveLogs.add(ENABLE_DISABLE_REASON_FACTORY_RESET, false)
117         ActiveLogs.add(ENABLE_DISABLE_REASON_SATELLITE_MODE, false)
118         ActiveLogs.add(42, false)
119         val stringWriter = StringWriter()
120         val writer = PrintWriter(stringWriter)
121 
122         ActiveLogs.dump(writer)
123         assertThat(stringWriter.toString())
124             .matches(
125                 "Enable log:\n" +
126                     ".*APPLICATION_REQUEST\n" +
127                     ".*AIRPLANE_MODE\n" +
128                     ".*DISALLOWED\n" +
129                     ".*RESTARTED\n" +
130                     ".*START_ERROR\n" +
131                     ".*SYSTEM_BOOT\n" +
132                     ".*CRASH\n" +
133                     ".*USER_SWITCH\n" +
134                     ".*RESTORE_USER_SETTING\n" +
135                     ".*FACTORY_RESET\n" +
136                     ".*SATELLITE MODE\n" +
137                     ".*UNKNOWN\\[\\d+\\]\n"
138             )
139     }
140 
141     @Test
protoDumpnull142     fun protoDump() {
143         ActiveLogs.add(ENABLE_DISABLE_REASON_APPLICATION_REQUEST, false)
144 
145         val proto = ProtoOutputStream()
146         ActiveLogs.dumpProto(proto)
147 
148         assertThat(proto.getRawSize()).isEqualTo(48)
149     }
150 }
151