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 @Option( 45 name = "disable", 46 description = "Set it to true to disable AppSetup test." 47 ) 48 private boolean mDisable = false; 49 50 static final String DROP_CACHE_COMMAND = "echo 3 > /proc/sys/vm/drop_caches"; 51 static final String KILL_APP_COMMAND_TEMPLATE = "am force-stop %s"; 52 AppSetup()53 public AppSetup() { 54 super(); 55 // Specifically target the app setup scenarios. 56 setPackageName("android.platform.test.scenario"); 57 addIncludeAnnotation("android.platform.test.scenario.annotation.AppSetup"); 58 } 59 60 /** 61 * Run the test the same way the superclass does and perform the additional setup/cleanup steps. 62 */ 63 @Override run(final ITestInvocationListener listener)64 public void run(final ITestInvocationListener listener) throws DeviceNotAvailableException { 65 if(mDisable) { 66 return; 67 } 68 runTest(listener); 69 70 // TODO(harrytczhang@): Switch to a solution based on test rule injection after b/123281375. 71 if (mDropCacheWhenFinished) { 72 getDevice().executeShellCommand(DROP_CACHE_COMMAND); 73 } 74 for (String packageName : mAppsToKillWhenFinished) { 75 getDevice().executeShellCommand(String.format(KILL_APP_COMMAND_TEMPLATE, packageName)); 76 } 77 } 78 79 /** 80 * Enable tests to stub out the actual run. 81 * 82 * @hide 83 */ 84 @VisibleForTesting runTest(final ITestInvocationListener listener)85 protected void runTest(final ITestInvocationListener listener) 86 throws DeviceNotAvailableException { 87 super.run(listener); 88 } 89 } 90