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 static com.google.common.truth.Truth.assertThat; 19 20 import android.app.ProcessStateEnum; // From enums.proto for atoms.proto's UidProcessStateChanged. 21 import android.cts.statsd.metric.MetricsUtils; 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.os.AtomsProto.Atom; 27 import com.android.os.StatsLog.EventMetricData; 28 import com.android.tradefed.build.IBuildInfo; 29 import com.android.tradefed.testtype.DeviceTestCase; 30 import com.android.tradefed.testtype.IBuildReceiver; 31 import com.android.tradefed.util.RunUtil; 32 33 import java.util.Arrays; 34 import java.util.HashSet; 35 import java.util.List; 36 import java.util.Set; 37 import java.util.function.Function; 38 import java.util.stream.Collectors; 39 import java.util.stream.Stream; 40 41 /** 42 * Base class for manipulating process states 43 */ 44 public class ProcStateTestCase extends DeviceTestCase implements IBuildReceiver { 45 46 private static final String TAG = "Statsd.ProcStateTestCase"; 47 48 private static final String DEVICE_SIDE_FG_ACTIVITY_COMPONENT 49 = "com.android.server.cts.device.statsd/.StatsdCtsForegroundActivity"; 50 private static final String DEVICE_SIDE_FG_SERVICE_COMPONENT 51 = "com.android.server.cts.device.statsd/.StatsdCtsForegroundService"; 52 53 private static final String KEY_ACTION = "action"; 54 55 // Constants from the device-side tests (not directly accessible here). 56 public static final String ACTION_END_IMMEDIATELY = "action.end_immediately"; 57 public static final String ACTION_BACKGROUND_SLEEP = "action.background_sleep"; 58 public static final String ACTION_SLEEP_WHILE_TOP = "action.sleep_top"; 59 public static final String ACTION_LONG_SLEEP_WHILE_TOP = "action.long_sleep_top"; 60 public static final String ACTION_SHOW_APPLICATION_OVERLAY = "action.show_application_overlay"; 61 62 // Sleep times (ms) that actions invoke device-side. 63 public static final int SLEEP_OF_ACTION_SLEEP_WHILE_TOP = 2_000; 64 public static final int SLEEP_OF_ACTION_LONG_SLEEP_WHILE_TOP = 60_000; 65 public static final int SLEEP_OF_ACTION_BACKGROUND_SLEEP = 2_000; 66 public static final int SLEEP_OF_FOREGROUND_SERVICE = 2_000; 67 68 protected IBuildInfo mCtsBuild; 69 70 @Override setUp()71 protected void setUp() throws Exception { 72 super.setUp(); 73 assertThat(mCtsBuild).isNotNull(); 74 ConfigUtils.removeConfig(getDevice()); 75 ReportUtils.clearReports(getDevice()); 76 DeviceUtils.installTestApp(getDevice(), MetricsUtils.DEVICE_SIDE_TEST_APK, 77 MetricsUtils.DEVICE_SIDE_TEST_PACKAGE, mCtsBuild); 78 RunUtil.getDefault().sleep(1000); 79 } 80 81 @Override tearDown()82 protected void tearDown() throws Exception { 83 ConfigUtils.removeConfig(getDevice()); 84 ReportUtils.clearReports(getDevice()); 85 DeviceUtils.uninstallTestApp(getDevice(), MetricsUtils.DEVICE_SIDE_TEST_PACKAGE); 86 super.tearDown(); 87 } 88 89 @Override setBuild(IBuildInfo buildInfo)90 public void setBuild(IBuildInfo buildInfo) { 91 mCtsBuild = buildInfo; 92 } 93 94 /** 95 * Runs an activity (in the foreground) to perform the given action. 96 * 97 * @param actionValue the action code constants indicating the desired action to perform. 98 */ executeForegroundActivity(String actionValue)99 protected void executeForegroundActivity(String actionValue) throws Exception { 100 getDevice().executeShellCommand(String.format( 101 "am start -n '%s' -e %s %s", 102 DEVICE_SIDE_FG_ACTIVITY_COMPONENT, 103 KEY_ACTION, actionValue)); 104 } 105 106 /** 107 * Runs a simple foreground service. 108 */ executeForegroundService()109 protected void executeForegroundService() throws Exception { 110 executeForegroundActivity(ACTION_END_IMMEDIATELY); 111 getDevice().executeShellCommand(String.format( 112 "am startservice -n '%s'", DEVICE_SIDE_FG_SERVICE_COMPONENT)); 113 } 114 } 115