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.vts.istats;
17 
18 import static com.google.common.truth.Truth.assertThat;
19 
20 import android.cts.statsdatom.lib.AtomTestUtils;
21 import android.cts.statsdatom.lib.ConfigUtils;
22 import android.cts.statsdatom.lib.DeviceUtils;
23 import android.cts.statsdatom.lib.ReportUtils;
24 import android.hardware.istats.TestVendorAtom;
25 import com.android.compatibility.common.util.NonApiTest;
26 import com.android.os.StatsLog.EventMetricData;
27 import com.android.tradefed.build.IBuildInfo;
28 import com.android.tradefed.device.DeviceNotAvailableException;
29 import com.android.tradefed.device.ITestDevice;
30 import com.android.tradefed.log.LogUtil.CLog;
31 import com.android.tradefed.result.TestRunResult;
32 import com.android.tradefed.testtype.DeviceTestCase;
33 import com.android.tradefed.testtype.IBuildReceiver;
34 import com.google.protobuf.CodedInputStream;
35 import com.google.protobuf.CodedOutputStream;
36 import java.io.ByteArrayInputStream;
37 import java.io.ByteArrayOutputStream;
38 import java.util.Arrays;
39 import java.util.List;
40 import javax.annotation.Nonnull;
41 import javax.annotation.Nullable;
42 
43 /* VTS test to verify IStats reportVendorAtom APIs. */
44 @NonApiTest(exemptionReasons = {}, justification = "METRIC")
45 public class VendorAtomTests extends DeviceTestCase implements IBuildReceiver {
46     private static final String ISTATS_TEST_PKG = "com.android.vts.istats.vendoratom";
47 
48     private IBuildInfo mVtsBuild;
49 
50     @Override
setUp()51     protected void setUp() throws Exception {
52         super.setUp();
53         assertThat(mVtsBuild).isNotNull();
54         ConfigUtils.removeConfig(getDevice());
55         ReportUtils.clearReports(getDevice());
56         Thread.sleep(AtomTestUtils.WAIT_TIME_LONG);
57     }
58 
59     @Override
tearDown()60     protected void tearDown() throws Exception {
61         ConfigUtils.removeConfig(getDevice());
62         ReportUtils.clearReports(getDevice());
63         super.tearDown();
64     }
65 
66     @Override
setBuild(IBuildInfo buildInfo)67     public void setBuild(IBuildInfo buildInfo) {
68         mVtsBuild = buildInfo;
69     }
70 
testReportVendorAtomWrongId()71     public void testReportVendorAtomWrongId() throws Exception {
72         assertThat(isIStatsPresentOnDevice()).isTrue();
73         ConfigUtils.uploadConfigForPushedAtom(getDevice(), ISTATS_TEST_PKG, 1000);
74         List<EventMetricData> data = runVendorAtomDeviceTests("testReportVendorAtomWrongId");
75         assertThat(data).hasSize(0);
76     }
77 
testReportVendorAtomInt()78     public void testReportVendorAtomInt() throws Exception {
79         assertThat(isIStatsPresentOnDevice()).isTrue();
80         ConfigUtils.uploadConfigForPushedAtom(getDevice(), ISTATS_TEST_PKG,
81             TestVendorAtom.Atom.TEST_VENDOR_ATOM_REPORTED_FIELD_NUMBER);
82 
83         List<EventMetricData> data = runVendorAtomDeviceTests("testReportVendorAtomInt");
84         final TestVendorAtom.TestVendorAtomReported vendorAtom = getVendorAtom(data);
85         assertThat(vendorAtom.getReverseDomainName()).isEqualTo("com.test.domain");
86         assertThat(vendorAtom.getIntValue()).isEqualTo(7);
87     }
88 
testReportVendorAtomRepeated()89     public void testReportVendorAtomRepeated() throws Exception {
90         assertThat(isIStatsPresentOnDevice()).isTrue();
91         ConfigUtils.uploadConfigForPushedAtom(getDevice(), ISTATS_TEST_PKG,
92             TestVendorAtom.Atom.TEST_VENDOR_ATOM_REPORTED_FIELD_NUMBER);
93 
94         List<EventMetricData> data = runVendorAtomDeviceTests("testReportVendorAtomRepeated");
95         final TestVendorAtom.TestVendorAtomReported vendorAtom = getVendorAtom(data);
96         assertThat(vendorAtom.getReverseDomainName()).isEqualTo("com.test.domain");
97         assertThat(vendorAtom.getIntValue()).isEqualTo(7);
98         assertThat(vendorAtom.getLongValue()).isEqualTo(70000L);
99         assertThat(vendorAtom.getFloatValue()).isEqualTo(8.5f);
100         assertThat(vendorAtom.getStringValue()).isEqualTo("testString");
101         assertThat(vendorAtom.getBoolValue()).isEqualTo(true);
102 
103         assertThat(vendorAtom.getRepeatedIntValueList()).isEqualTo(Arrays.asList(11, 12, 13));
104         assertThat(vendorAtom.getRepeatedLongValueList())
105             .isEqualTo(Arrays.asList(11000L, 12000L, 13000L));
106         assertThat(vendorAtom.getRepeatedFloatValueList())
107             .isEqualTo(Arrays.asList(0.1f, 0.2f, 0.3f));
108         assertThat(vendorAtom.getRepeatedStringValueList())
109             .isEqualTo(Arrays.asList("abc", "def", "xyz"));
110         assertThat(vendorAtom.getRepeatedBoolValueList())
111             .isEqualTo(Arrays.asList(true, false, false, true));
112     }
113 
isIStatsPresentOnDevice()114     private boolean isIStatsPresentOnDevice() throws Exception {
115         return checkDeviceFor("testIStatsPresent");
116     }
117 
checkDeviceFor(String methodName)118     private boolean checkDeviceFor(String methodName) throws Exception {
119         try {
120             runDeviceTestsOnVendorAtom(getDevice(), methodName);
121             // Test passes, meaning that the answer is true.
122             CLog.d(methodName + "() indicates true.");
123             return true;
124         } catch (AssertionError e) {
125             // Method is designed to fail if the answer is false.
126             CLog.d(methodName + "() indicates false.");
127             return false;
128         }
129     }
130 
getVendorAtom(@onnull List<EventMetricData> data)131     private TestVendorAtom.TestVendorAtomReported getVendorAtom(@Nonnull List<EventMetricData> data)
132         throws Exception {
133         assertThat(data).hasSize(1);
134         ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
135         CodedOutputStream codedos = CodedOutputStream.newInstance(outputStream);
136         data.get(0).getAtom().writeTo(codedos);
137         codedos.flush();
138 
139         ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray());
140         CodedInputStream codedis = CodedInputStream.newInstance(inputStream);
141         final TestVendorAtom.Atom atom = TestVendorAtom.Atom.parseFrom(codedis);
142         assertThat(atom.hasTestVendorAtomReported()).isTrue();
143 
144         return atom.getTestVendorAtomReported();
145     }
146 
runVendorAtomDeviceTests(String testMethodName)147     private List<EventMetricData> runVendorAtomDeviceTests(String testMethodName) throws Exception {
148         runDeviceTestsOnVendorAtom(getDevice(), testMethodName);
149         Thread.sleep(AtomTestUtils.WAIT_TIME_LONG);
150         // Sorted list of events in order in which they occurred.
151         return ReportUtils.getEventMetricDataList(getDevice());
152     }
153 
154     /** Runs device side tests from the com.android.vts.istats.vendoratom package. */
runDeviceTestsOnVendorAtom( ITestDevice device, @Nullable String testMethodName)155     private static @Nonnull TestRunResult runDeviceTestsOnVendorAtom(
156         ITestDevice device, @Nullable String testMethodName) throws DeviceNotAvailableException {
157         return DeviceUtils.runDeviceTests(
158             device, ISTATS_TEST_PKG, ".VtsVendorAtomJavaTest", testMethodName);
159     }
160 }
161