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 package android.cts.statsd.alarm;
17 
18 import static com.google.common.truth.Truth.assertThat;
19 
20 import android.cts.statsd.metric.MetricsUtils;
21 import android.cts.statsdatom.lib.AtomTestUtils;
22 import android.cts.statsdatom.lib.ConfigUtils;
23 import android.cts.statsdatom.lib.DeviceUtils;
24 import android.cts.statsdatom.lib.ReportUtils;
25 
26 import com.android.internal.os.StatsdConfigProto.Alarm;
27 import com.android.internal.os.StatsdConfigProto.IncidentdDetails;
28 import com.android.internal.os.StatsdConfigProto.StatsdConfig;
29 import com.android.internal.os.StatsdConfigProto.Subscription;
30 import com.android.tradefed.build.IBuildInfo;
31 import com.android.tradefed.log.LogUtil.CLog;
32 import com.android.tradefed.testtype.DeviceTestCase;
33 import com.android.tradefed.testtype.IBuildReceiver;
34 import com.android.tradefed.util.RunUtil;
35 
36 import java.text.SimpleDateFormat;
37 import java.util.Date;
38 
39 /**
40  * Statsd Anomaly Detection tests.
41  */
42 public class AlarmTests extends DeviceTestCase implements IBuildReceiver {
43 
44     private static final String TAG = "Statsd.AnomalyDetectionTests";
45 
46     private static final boolean INCIDENTD_TESTS_ENABLED = false;
47 
48     // Config constants
49     private static final int ALARM_ID = 11;
50     private static final int SUBSCRIPTION_ID_INCIDENTD = 41;
51     private static final int INCIDENTD_SECTION = -1;
52 
53     private IBuildInfo mCtsBuild;
54 
55     @Override
setUp()56     protected void setUp() throws Exception {
57         super.setUp();
58         assertThat(mCtsBuild).isNotNull();
59         ConfigUtils.removeConfig(getDevice());
60         ReportUtils.clearReports(getDevice());
61         DeviceUtils.installTestApp(getDevice(), MetricsUtils.DEVICE_SIDE_TEST_APK,
62                 MetricsUtils.DEVICE_SIDE_TEST_PACKAGE, mCtsBuild);
63         if (!INCIDENTD_TESTS_ENABLED) {
64             CLog.w(TAG, TAG + " alarm tests are disabled by a flag. Change flag to true to run");
65         }
66         RunUtil.getDefault().sleep(AtomTestUtils.WAIT_TIME_LONG);
67     }
68 
69     @Override
tearDown()70     protected void tearDown() throws Exception {
71         super.tearDown();
72         ConfigUtils.removeConfig(getDevice());
73         ReportUtils.clearReports(getDevice());
74         DeviceUtils.uninstallTestApp(getDevice(), MetricsUtils.DEVICE_SIDE_TEST_PACKAGE);
75     }
76 
77     @Override
setBuild(IBuildInfo buildInfo)78     public void setBuild(IBuildInfo buildInfo) {
79         mCtsBuild = buildInfo;
80     }
81 
testAlarm()82     public void testAlarm() throws Exception {
83         StatsdConfig.Builder config = getBaseConfig();
84         DeviceUtils.turnScreenOn(getDevice());
85         ConfigUtils.uploadConfig(getDevice(), config);
86 
87         String markTime = MetricsUtils.getCurrentLogcatDate(getDevice());
88         RunUtil.getDefault().sleep(9_000);
89 
90         if (INCIDENTD_TESTS_ENABLED) {
91             assertThat(MetricsUtils.didIncidentdFireSince(getDevice(), markTime)).isTrue();
92         }
93     }
94 
95 
getBaseConfig()96     private final StatsdConfig.Builder getBaseConfig() throws Exception {
97         return ConfigUtils.createConfigBuilder(MetricsUtils.DEVICE_SIDE_TEST_PACKAGE)
98                 .addAlarm(Alarm.newBuilder()
99                         .setId(ALARM_ID)
100                         .setOffsetMillis(2)
101                         .setPeriodMillis(5_000) // every 5 seconds.
102                 )
103                 .addSubscription(Subscription.newBuilder()
104                         .setId(SUBSCRIPTION_ID_INCIDENTD)
105                         .setRuleType(Subscription.RuleType.ALARM)
106                         .setRuleId(ALARM_ID)
107                         .setIncidentdDetails(IncidentdDetails.newBuilder()
108                                 .addSection(INCIDENTD_SECTION)));
109     }
110 }
111