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 android.bluetooth
18 
19 import android.cts.statsdatom.lib.ConfigUtils
20 import android.cts.statsdatom.lib.DeviceUtils
21 import android.cts.statsdatom.lib.ReportUtils
22 import com.android.os.AtomsProto
23 import com.android.os.AtomsProto.BluetoothEnabledStateChanged
24 import com.android.os.StatsLog
25 import com.android.tradefed.testtype.DeviceJUnit4ClassRunner
26 import com.android.tradefed.testtype.junit4.BaseHostJUnit4Test
27 import com.google.common.truth.Truth.assertThat
28 import java.time.Duration
29 import org.junit.Before
30 import org.junit.Test
31 import org.junit.runner.RunWith
32 
33 @RunWith(DeviceJUnit4ClassRunner::class)
34 class MetricsTest : BaseHostJUnit4Test() {
35 
36     companion object {
37         private const val TAG = "BluetoothMetricsTests"
38         private const val TEST_APP_PKG_NAME = "android.bluetooth"
39         private const val TEST_APP_CLASS_NAME = ".BluetoothMetricsHelperTest"
40     }
41 
42     @Before
setUpnull43     fun setUp() {
44         ConfigUtils.removeConfig(getDevice())
45         ReportUtils.clearReports(getDevice())
46     }
47 
48     @Test
testBluetoothDisableEnable_shouldProduceEnabledStateChangednull49     fun testBluetoothDisableEnable_shouldProduceEnabledStateChanged() {
50         val data =
51             uploadAtomConfigAndTriggerTest(
52                 "testBluetoothDisableEnable",
53                 intArrayOf(AtomsProto.Atom.BLUETOOTH_ENABLED_STATE_CHANGED_FIELD_NUMBER)
54             )
55         // First atom might be the setup one.
56         val offset =
57             data[0].atom.bluetoothEnabledStateChanged.let {
58                 if (it.state == BluetoothEnabledStateChanged.State.ENABLED) {
59                     1
60                 } else {
61                     0
62                 }
63             }
64         data[offset].atom.bluetoothEnabledStateChanged.apply {
65             assertThat(state).isEqualTo(BluetoothEnabledStateChanged.State.DISABLED)
66             assertThat(previousState).isEqualTo(BluetoothEnabledStateChanged.State.ENABLED)
67             assertThat(timeSinceLastChangedMillis).isGreaterThan(Duration.ofMillis(1).toMillis())
68         }
69         data[offset + 1].atom.bluetoothEnabledStateChanged.apply {
70             assertThat(state).isEqualTo(BluetoothEnabledStateChanged.State.ENABLED)
71             assertThat(previousState).isEqualTo(BluetoothEnabledStateChanged.State.DISABLED)
72             assertThat(timeSinceLastChangedMillis).isGreaterThan(Duration.ofMillis(1).toMillis())
73         }
74     }
75 
uploadAtomConfigAndTriggerTestnull76     private fun uploadAtomConfigAndTriggerTest(
77         testName: String,
78         atoms: IntArray
79     ): List<StatsLog.EventMetricData> {
80         ConfigUtils.uploadConfigForPushedAtoms(device, TEST_APP_PKG_NAME, atoms)
81 
82         DeviceUtils.runDeviceTests(device, TEST_APP_PKG_NAME, TEST_APP_CLASS_NAME, testName)
83 
84         return ReportUtils.getEventMetricDataList(device)
85     }
86 }
87