xref: /aosp_15_r20/cts/hostsidetests/statsdatom/src/android/cts/statsdatom/bluetooth/BluetoothStatsTests.java (revision b7c941bb3fa97aba169d73cee0bed2de8ac964bf)
1 /*
2  * Copyright (C) 2020 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.cts.statsdatom.bluetooth;
18 
19 import com.android.tradefed.util.RunUtil;
20 import static com.google.common.truth.Truth.assertThat;
21 
22 import android.cts.statsdatom.lib.AtomTestUtils;
23 import android.cts.statsdatom.lib.ConfigUtils;
24 import android.cts.statsdatom.lib.DeviceUtils;
25 import android.cts.statsdatom.lib.ReportUtils;
26 
27 import com.android.internal.os.StatsdConfigProto;
28 import com.android.os.AtomsProto;
29 import com.android.os.StatsLog;
30 import com.android.tradefed.build.IBuildInfo;
31 import com.android.tradefed.testtype.DeviceTestCase;
32 import com.android.tradefed.testtype.IBuildReceiver;
33 
34 import java.util.Arrays;
35 import java.util.Collections;
36 import java.util.HashSet;
37 import java.util.List;
38 import java.util.Set;
39 
40 public class BluetoothStatsTests extends DeviceTestCase implements IBuildReceiver {
41     private static final String FEATURE_BLUETOOTH_LE = "android.hardware.bluetooth_le";
42 
43     private IBuildInfo mCtsBuild;
44 
45     @Override
setUp()46     protected void setUp() throws Exception {
47         super.setUp();
48         assertThat(mCtsBuild).isNotNull();
49         ConfigUtils.removeConfig(getDevice());
50         ReportUtils.clearReports(getDevice());
51         DeviceUtils.installStatsdTestApp(getDevice(), mCtsBuild);
52         RunUtil.getDefault().sleep(AtomTestUtils.WAIT_TIME_LONG);
53     }
54 
55     @Override
tearDown()56     protected void tearDown() throws Exception {
57         ConfigUtils.removeConfig(getDevice());
58         ReportUtils.clearReports(getDevice());
59         DeviceUtils.uninstallStatsdTestApp(getDevice());
60         super.tearDown();
61     }
62 
63     @Override
setBuild(IBuildInfo buildInfo)64     public void setBuild(IBuildInfo buildInfo) {
65         mCtsBuild = buildInfo;
66     }
67 
testBleScan()68     public void testBleScan() throws Exception {
69         if (!DeviceUtils.hasFeature(getDevice(), FEATURE_BLUETOOTH_LE)) return;
70 
71         final int atomTag = AtomsProto.Atom.BLE_SCAN_STATE_CHANGED_FIELD_NUMBER;
72         Set<Integer> onState = new HashSet<>(
73                 Collections.singletonList(AtomsProto.BleScanStateChanged.State.ON_VALUE));
74         Set<Integer> offState = new HashSet<>(
75                 Collections.singletonList(AtomsProto.BleScanStateChanged.State.OFF_VALUE));
76         final int expectedWait = 3_000;
77         // Add state sets to the list in order.
78         List<Set<Integer>> stateSet = Arrays.asList(onState, offState);
79         ConfigUtils.uploadConfigForPushedAtomWithUid(getDevice(), DeviceUtils.STATSD_ATOM_TEST_PKG,
80                 atomTag, /*useAttributionChain=*/ true);
81 
82         DeviceUtils.runDeviceTestsOnStatsdApp(getDevice(), ".AtomTests", "testBleScanUnoptimized");
83         RunUtil.getDefault().sleep(AtomTestUtils.WAIT_TIME_SHORT);
84 
85         List<StatsLog.EventMetricData> data = ReportUtils.getEventMetricDataList(getDevice());
86         AtomTestUtils.assertStatesOccurredInOrder(stateSet, data, expectedWait,
87                 atom -> atom.getBleScanStateChanged().getState().getNumber());
88     }
89 
testBleUnoptimizedScan()90     public void testBleUnoptimizedScan() throws Exception {
91         if (!DeviceUtils.hasFeature(getDevice(), FEATURE_BLUETOOTH_LE)) return;
92 
93         final int atomTag = AtomsProto.Atom.BLE_SCAN_STATE_CHANGED_FIELD_NUMBER;
94         Set<Integer> onState = new HashSet<>(
95                 Collections.singletonList(AtomsProto.BleScanStateChanged.State.ON_VALUE));
96         Set<Integer> offState = new HashSet<>(
97                 Collections.singletonList(AtomsProto.BleScanStateChanged.State.OFF_VALUE));
98         final int minTimeDiffMillis = 1_500;
99         final int maxTimeDiffMillis = 3_000;
100         // Add state sets to the list in order.
101         List<Set<Integer>> stateSet = Arrays.asList(onState, offState);
102         ConfigUtils.uploadConfigForPushedAtomWithUid(getDevice(), DeviceUtils.STATSD_ATOM_TEST_PKG,
103                 atomTag, /*useAttributionChain=*/ true);
104 
105         DeviceUtils.runDeviceTestsOnStatsdApp(getDevice(), ".AtomTests", "testBleScanUnoptimized");
106         RunUtil.getDefault().sleep(AtomTestUtils.WAIT_TIME_LONG);
107 
108         List<StatsLog.EventMetricData> data = ReportUtils.getEventMetricDataList(getDevice());
109         AtomTestUtils.assertTimeDiffBetween(data.get(0), data.get(1), minTimeDiffMillis,
110                 maxTimeDiffMillis);
111         AtomsProto.BleScanStateChanged a0 = data.get(0).getAtom().getBleScanStateChanged();
112         assertThat(a0.getState().getNumber()).isEqualTo(
113                 AtomsProto.BleScanStateChanged.State.ON_VALUE);
114         assertThat(a0.getIsFiltered()).isFalse();
115         assertThat(a0.getIsFirstMatch()).isFalse();
116         assertThat(a0.getIsOpportunistic()).isFalse();
117         AtomsProto.BleScanStateChanged a1 = data.get(1).getAtom().getBleScanStateChanged();
118         assertThat(a1.getState().getNumber()).isEqualTo(
119                 AtomsProto.BleScanStateChanged.State.OFF_VALUE);
120         assertThat(a1.getIsFiltered()).isFalse();
121         assertThat(a1.getIsFirstMatch()).isFalse();
122         assertThat(a1.getIsOpportunistic()).isFalse();
123     }
124 
testBleOpportunisticScan()125     public void testBleOpportunisticScan() throws Exception {
126         if (!DeviceUtils.hasFeature(getDevice(), FEATURE_BLUETOOTH_LE)) return;
127 
128         final int atomTag = AtomsProto.Atom.BLE_SCAN_STATE_CHANGED_FIELD_NUMBER;
129         Set<Integer> onState = new HashSet<>(
130                 Collections.singletonList(AtomsProto.BleScanStateChanged.State.ON_VALUE));
131         Set<Integer> offState = new HashSet<>(
132                 Collections.singletonList(AtomsProto.BleScanStateChanged.State.OFF_VALUE));
133         final int minTimeDiffMillis = 1_500;
134         final int maxTimeDiffMillis = 3_000;
135         // Add state sets to the list in order.
136         List<Set<Integer>> stateSet = Arrays.asList(onState, offState);
137         ConfigUtils.uploadConfigForPushedAtomWithUid(getDevice(), DeviceUtils.STATSD_ATOM_TEST_PKG,
138                 atomTag, /*useAttributionChain=*/ true);
139 
140         DeviceUtils.runDeviceTestsOnStatsdApp(getDevice(), ".AtomTests",
141                 "testBleScanOpportunistic");
142 
143         List<StatsLog.EventMetricData> data = ReportUtils.getEventMetricDataList(getDevice());
144         AtomTestUtils.assertTimeDiffBetween(data.get(0), data.get(1), minTimeDiffMillis,
145                 maxTimeDiffMillis);
146         AtomsProto.BleScanStateChanged a0 = data.get(0).getAtom().getBleScanStateChanged();
147         assertThat(a0.getState().getNumber()).isEqualTo(
148                 AtomsProto.BleScanStateChanged.State.ON_VALUE);
149         assertThat(a0.getIsFiltered()).isFalse();
150         assertThat(a0.getIsFirstMatch()).isFalse();
151         assertThat(a0.getIsOpportunistic()).isTrue();  // This scan is opportunistic.
152         AtomsProto.BleScanStateChanged a1 = data.get(1).getAtom().getBleScanStateChanged();
153         assertThat(a1.getState().getNumber()).isEqualTo(
154                 AtomsProto.BleScanStateChanged.State.OFF_VALUE);
155         assertThat(a1.getIsFiltered()).isFalse();
156         assertThat(a1.getIsFirstMatch()).isFalse();
157         assertThat(a1.getIsOpportunistic()).isTrue();
158     }
159 }
160