1 /*
2  * Copyright (C) 2018 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.statsd.metadata;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import android.cts.statsd.atom.BufferDebug;
22 import android.cts.statsd.metric.MetricsUtils;
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.StatsdConfig;
28 import com.android.os.AtomsProto.Atom;
29 import com.android.os.StatsLog.StatsdStatsReport;
30 import com.android.tradefed.build.IBuildInfo;
31 import com.android.tradefed.device.CollectingByteOutputReceiver;
32 import com.android.tradefed.device.DeviceNotAvailableException;
33 import com.android.tradefed.log.LogUtil;
34 import com.android.tradefed.testtype.DeviceTestCase;
35 import com.android.tradefed.testtype.IBuildReceiver;
36 import com.android.tradefed.util.RunUtil;
37 
38 import com.google.protobuf.InvalidProtocolBufferException;
39 import com.google.protobuf.MessageLite;
40 import com.google.protobuf.Parser;
41 
42 public class MetadataTestCase extends DeviceTestCase implements IBuildReceiver {
43     public static final String DUMP_METADATA_CMD = "cmd stats print-stats";
44 
45     protected IBuildInfo mCtsBuild;
46 
getStatsdStatsReport()47     protected StatsdStatsReport getStatsdStatsReport() throws Exception {
48         try {
49             StatsdStatsReport report = MetricsUtils.getDump(getDevice(), StatsdStatsReport.parser(),
50                     String.join(" ", DUMP_METADATA_CMD, "--proto"));
51             return report;
52         } catch (com.google.protobuf.InvalidProtocolBufferException e) {
53             LogUtil.CLog.e("Failed to fetch and parse the statsdstats output report.");
54             throw (e);
55         }
56     }
57 
getBaseConfig()58     protected final StatsdConfig.Builder getBaseConfig() throws Exception {
59         StatsdConfig.Builder builder = ConfigUtils.createConfigBuilder(
60                 MetricsUtils.DEVICE_SIDE_TEST_PACKAGE);
61         ConfigUtils.addEventMetric(builder, Atom.APP_BREADCRUMB_REPORTED_FIELD_NUMBER);
62         return builder;
63     }
64 
65     @Override
setUp()66     protected void setUp() throws Exception {
67         super.setUp();
68         assertThat(mCtsBuild).isNotNull();
69         ConfigUtils.removeConfig(getDevice());
70         ReportUtils.clearReports(getDevice());
71         DeviceUtils.installTestApp(getDevice(), MetricsUtils.DEVICE_SIDE_TEST_APK,
72                 MetricsUtils.DEVICE_SIDE_TEST_PACKAGE, mCtsBuild);
73         RunUtil.getDefault().sleep(1000);
74     }
75 
76     @Override
tearDown()77     protected void tearDown() throws Exception {
78         ConfigUtils.removeConfig(getDevice());
79         ReportUtils.clearReports(getDevice());
80         DeviceUtils.uninstallTestApp(getDevice(), MetricsUtils.DEVICE_SIDE_TEST_PACKAGE);
81         super.tearDown();
82     }
83 
84     @Override
setBuild(IBuildInfo buildInfo)85     public void setBuild(IBuildInfo buildInfo) {
86         mCtsBuild = buildInfo;
87     }
88 }
89