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 17 package android.harmfulappwarning.cts; 18 19 import com.android.tradefed.device.DeviceNotAvailableException; 20 import com.android.tradefed.device.ITestDevice; 21 import com.android.tradefed.device.PackageInfo; 22 import com.android.tradefed.result.InputStreamSource; 23 import com.android.tradefed.testtype.DeviceJUnit4ClassRunner; 24 import com.android.tradefed.testtype.junit4.BaseHostJUnit4Test; 25 26 import org.junit.Assert; 27 import org.junit.Before; 28 import org.junit.Test; 29 import org.junit.runner.RunWith; 30 31 import java.util.Scanner; 32 33 import static org.junit.Assert.assertEquals; 34 import static org.junit.Assert.assertNull; 35 import static org.junit.Assert.assertTrue; 36 import static org.junit.Assume.assumeFalse; 37 38 /** 39 * Host-side tests for the harmful app launch warning 40 * 41 * <p>These tests have a few different components. This is the host-side part of the test, which is 42 * responsible for setting up the environment and passing off execution to the device-side part 43 * of the test. 44 * 45 * <p>The {@link HarmfulAppWarningDeviceTest} class is the device side of these tests. It attempts to launch 46 * the sample warned application, and verifies the correct behavior of the harmful app warning that 47 * is shown by the platform. 48 * 49 * <p>The third component is the sample app, which is just a placeholder app with a basic activity 50 * that only serves as a target for the harmful app warning. 51 * 52 * <p>Run with: atest CtsHarmfulAppWarningHostTestCases 53 */ 54 @RunWith(DeviceJUnit4ClassRunner.class) 55 public class HarmfulAppWarningTest extends BaseHostJUnit4Test { 56 57 private static final String FEATURE_WEARABLE = "android.hardware.type.watch"; 58 59 private static final String TEST_APP_PACKAGE_NAME = "android.harmfulappwarning.sampleapp"; 60 private static final String TEST_APP_ACTIVITY_CLASS_NAME = "SampleDeviceActivity"; 61 private static final String TEST_APP_LAUNCHED_STRING = "Sample activity started."; 62 63 private static final String WARNING_MESSAGE = "This is a warning message."; 64 private static final String SET_HARMFUL_APP_WARNING_COMMAND = String.format( 65 "cmd package set-harmful-app-warning %s \"" + WARNING_MESSAGE + "\"", 66 TEST_APP_PACKAGE_NAME); 67 68 private static final String CLEAR_HARMFUL_APP_WARNING_COMMAND = String.format( 69 "cmd package set-harmful-app-warning %s", TEST_APP_PACKAGE_NAME); 70 71 private static final String GET_HARMFUL_APP_WARNING_COMMAND = String.format( 72 "cmd package get-harmful-app-warning %s", TEST_APP_PACKAGE_NAME); 73 74 private static final String LIST_PACKAGES_COMMAND = 75 "cmd package list packages --user %d " + TEST_APP_PACKAGE_NAME; 76 77 private ITestDevice mDevice; 78 79 @Before 80 public void setUp() throws Exception { 81 installPackage("CtsHarmfulAppWarningSampleApp.apk"); 82 mDevice = getDevice(); 83 mDevice.clearLogcat(); 84 85 // Skip the tests for wearable devices. This feature is not used on wearable 86 // devices, for now (no wearable UI, etc.) 87 assumeFalse(hasFeature(FEATURE_WEARABLE)); 88 } 89 90 private void runDeviceTest(String testName) throws DeviceNotAvailableException { 91 runDeviceTests("android.harmfulappwarning.testapp", 92 "android.harmfulappwarning.testapp.HarmfulAppWarningDeviceTest", 93 testName); 94 } 95 96 private void verifyHarmfulAppWarningSet() throws DeviceNotAvailableException { 97 String warning = getDevice().executeShellCommand(GET_HARMFUL_APP_WARNING_COMMAND); 98 assertEquals(WARNING_MESSAGE, warning.trim()); 99 } 100 101 private void verifyHarmfulAppWarningUnset() throws DeviceNotAvailableException { 102 String warning = getDevice().executeShellCommand(GET_HARMFUL_APP_WARNING_COMMAND); 103 if (warning != null) { 104 warning = warning.trim(); 105 } 106 assertTrue(warning == null || warning.length() == 0); 107 } 108 109 private void verifySampleAppUninstalled() throws DeviceNotAvailableException { 110 String installedPackage = getDevice().executeShellCommand( 111 String.format(LIST_PACKAGES_COMMAND, getDevice().getCurrentUser())); 112 Assert.assertTrue("Harmful application was not uninstalled", installedPackage.isEmpty()); 113 } 114 115 private void verifySampleAppInstalled() throws DeviceNotAvailableException { 116 String installedPackage = getDevice().executeShellCommand( 117 String.format(LIST_PACKAGES_COMMAND, getDevice().getCurrentUser())); 118 Assert.assertFalse("Harmful application was uninstalled", installedPackage.isEmpty()); 119 } 120 121 /** 122 * A basic smoke test to ensure that we're able to detect the launch of the activity when there 123 * is no warning. 124 */ 125 @Test 126 public void testNormalLaunch() throws Exception { 127 runDeviceTest("testNormalLaunch"); 128 } 129 130 /** 131 * Tests that when the user clicks "launch anyway" on the harmful app warning dialog, the 132 * warning is cleared and the activity is launched. 133 */ 134 @Test 135 public void testLaunchAnyway() throws DeviceNotAvailableException { 136 mDevice.executeShellCommand(SET_HARMFUL_APP_WARNING_COMMAND); 137 runDeviceTest("testLaunchAnyway"); 138 139 verifyHarmfulAppWarningUnset(); 140 } 141 142 /** 143 * Tests that when the user clicks "uninstall" on the harmful app warning dialog, the 144 * application is uninstalled. 145 */ 146 @Test 147 public void testUninstall() throws DeviceNotAvailableException { 148 mDevice.executeShellCommand(SET_HARMFUL_APP_WARNING_COMMAND); 149 runDeviceTest("testUninstall"); 150 verifySampleAppUninstalled(); 151 } 152 153 /** 154 * Tests that no action is taken when the user dismisses the harmful app warning 155 */ 156 @Test 157 public void testDismissDialog() throws DeviceNotAvailableException { 158 mDevice.executeShellCommand(SET_HARMFUL_APP_WARNING_COMMAND); 159 runDeviceTest("testDismissDialog"); 160 verifyHarmfulAppWarningSet(); 161 verifySampleAppInstalled(); 162 } 163 164 protected boolean hasFeature(String featureName) throws DeviceNotAvailableException { 165 return getDevice().executeShellCommand("pm list features").contains(featureName); 166 } 167 } 168