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.atom.AtomTestCase;
21 
22 import com.android.internal.os.StatsdConfigProto;
23 import com.android.internal.os.StatsdConfigProto.Alarm;
24 import com.android.internal.os.StatsdConfigProto.IncidentdDetails;
25 import com.android.internal.os.StatsdConfigProto.StatsdConfig;
26 import com.android.internal.os.StatsdConfigProto.Subscription;
27 import com.android.tradefed.log.LogUtil.CLog;
28 
29 import java.util.List;
30 
31 /**
32  * Statsd Anomaly Detection tests.
33  */
34 public class AlarmTests extends AtomTestCase {
35 
36     private static final String TAG = "Statsd.AnomalyDetectionTests";
37 
38     private static final boolean INCIDENTD_TESTS_ENABLED = false;
39 
40     // Config constants
41     private static final int ALARM_ID = 11;
42     private static final int SUBSCRIPTION_ID_INCIDENTD = 41;
43     private static final int INCIDENTD_SECTION = -1;
44 
45     @Override
setUp()46     protected void setUp() throws Exception {
47         super.setUp();
48         if (!INCIDENTD_TESTS_ENABLED) {
49             CLog.w(TAG, TAG + " alarm tests are disabled by a flag. Change flag to true to run");
50         }
51     }
52 
testAlarm()53     public void testAlarm() throws Exception {
54         StatsdConfig.Builder config = getBaseConfig();
55         turnScreenOn();
56         uploadConfig(config);
57 
58         String markTime = getCurrentLogcatDate();
59         Thread.sleep(9_000);
60 
61         if (INCIDENTD_TESTS_ENABLED) assertThat(didIncidentdFireSince(markTime)).isTrue();
62     }
63 
64 
getBaseConfig()65     private final StatsdConfig.Builder getBaseConfig() throws Exception {
66       return createConfigBuilder()
67           .addAlarm(Alarm.newBuilder().setId(ALARM_ID).setOffsetMillis(2).setPeriodMillis(
68               5_000) // every 5 seconds.
69               )
70           .addSubscription(Subscription.newBuilder()
71                                .setId(SUBSCRIPTION_ID_INCIDENTD)
72                                .setRuleType(Subscription.RuleType.ALARM)
73                                .setRuleId(ALARM_ID)
74                                .setIncidentdDetails(
75                                    IncidentdDetails.newBuilder().addSection(INCIDENTD_SECTION)));
76     }
77 }
78