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