1 /* 2 * Copyright (C) 2019 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 com.android.scenario; 17 18 import com.android.tradefed.config.Option; 19 import com.android.tradefed.config.OptionClass; 20 import com.android.tradefed.device.DeviceNotAvailableException; 21 import com.android.tradefed.result.ITestInvocationListener; 22 import com.android.tradefed.testtype.AndroidJUnitTest; 23 24 import com.google.common.annotations.VisibleForTesting; 25 26 import java.util.ArrayList; 27 import java.util.List; 28 29 /** A test that runs app setup scenarios only. */ 30 @OptionClass(alias = "app-setup") 31 public class AppSetup extends AndroidJUnitTest { 32 @Option( 33 name = "drop-cache-when-finished", 34 description = "Clear the cache when setup is finished." 35 ) 36 private boolean mDropCacheWhenFinished = false; 37 38 @Option( 39 name = "apps-to-kill-when-finished", 40 description = "List of app package names to kill when setup is finished." 41 ) 42 private List<String> mAppsToKillWhenFinished = new ArrayList<>(); 43 44 static final String DROP_CACHE_COMMAND = "echo 3 > /proc/sys/vm/drop_caches"; 45 static final String KILL_APP_COMMAND_TEMPLATE = "am force-stop %s"; 46 AppSetup()47 public AppSetup() { 48 super(); 49 // Specifically target the app setup scenarios. 50 setPackageName("android.platform.test.scenario"); 51 addIncludeAnnotation("android.platform.test.scenario.annotation.AppSetup"); 52 } 53 54 /** 55 * Run the test the same way the superclass does and perform the additional setup/cleanup steps. 56 */ 57 @Override run(final ITestInvocationListener listener)58 public void run(final ITestInvocationListener listener) throws DeviceNotAvailableException { 59 runTest(listener); 60 61 // TODO(harrytczhang@): Switch to a solution based on test rule injection after b/123281375. 62 if (mDropCacheWhenFinished) { 63 getDevice().executeShellCommand(DROP_CACHE_COMMAND); 64 } 65 for (String packageName : mAppsToKillWhenFinished) { 66 getDevice().executeShellCommand(String.format(KILL_APP_COMMAND_TEMPLATE, packageName)); 67 } 68 } 69 70 /** 71 * Enable tests to stub out the actual run. 72 * 73 * @hide 74 */ 75 @VisibleForTesting runTest(final ITestInvocationListener listener)76 protected void runTest(final ITestInvocationListener listener) 77 throws DeviceNotAvailableException { 78 super.run(listener); 79 } 80 } 81