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.atom;
17 
18 import android.app.ProcessStateEnum; // From enums.proto for atoms.proto's UidProcessStateChanged.
19 
20 import com.android.os.AtomsProto.Atom;
21 import com.android.os.StatsLog.EventMetricData;
22 
23 import java.util.Arrays;
24 import java.util.HashSet;
25 import java.util.List;
26 import java.util.Set;
27 import java.util.function.Function;
28 import java.util.stream.Collectors;
29 import java.util.stream.Stream;
30 
31 /**
32  * Base class for manipulating process states
33  */
34 public class ProcStateTestCase extends DeviceAtomTestCase {
35 
36   private static final String TAG = "Statsd.ProcStateTestCase";
37 
38   private static final String DEVICE_SIDE_FG_ACTIVITY_COMPONENT
39           = "com.android.server.cts.device.statsd/.StatsdCtsForegroundActivity";
40   private static final String DEVICE_SIDE_FG_SERVICE_COMPONENT
41           = "com.android.server.cts.device.statsd/.StatsdCtsForegroundService";
42 
43   // Constants from the device-side tests (not directly accessible here).
44   public static final String ACTION_END_IMMEDIATELY = "action.end_immediately";
45   public static final String ACTION_BACKGROUND_SLEEP = "action.background_sleep";
46   public static final String ACTION_SLEEP_WHILE_TOP = "action.sleep_top";
47   public static final String ACTION_LONG_SLEEP_WHILE_TOP = "action.long_sleep_top";
48   public static final String ACTION_SHOW_APPLICATION_OVERLAY = "action.show_application_overlay";
49 
50   // Sleep times (ms) that actions invoke device-side.
51   public static final int SLEEP_OF_ACTION_SLEEP_WHILE_TOP = 2_000;
52   public static final int SLEEP_OF_ACTION_LONG_SLEEP_WHILE_TOP = 60_000;
53   public static final int SLEEP_OF_ACTION_BACKGROUND_SLEEP = 2_000;
54   public static final int SLEEP_OF_FOREGROUND_SERVICE = 2_000;
55 
56 
57   /**
58    * Runs an activity (in the foreground) to perform the given action.
59    * @param actionValue the action code constants indicating the desired action to perform.
60    */
61   protected void executeForegroundActivity(String actionValue) throws Exception {
62     getDevice().executeShellCommand(String.format(
63             "am start -n '%s' -e %s %s",
64             DEVICE_SIDE_FG_ACTIVITY_COMPONENT,
65             KEY_ACTION, actionValue));
66   }
67 
68   /**
69    * Runs a simple foreground service.
70    */
71   protected void executeForegroundService() throws Exception {
72     executeForegroundActivity(ACTION_END_IMMEDIATELY);
73     getDevice().executeShellCommand(String.format(
74             "am startservice -n '%s'", DEVICE_SIDE_FG_SERVICE_COMPONENT));
75   }
76 }
77