1 /* 2 * Copyright (C) 2017 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.tradefed.testtype.suite; 17 18 import static org.junit.Assert.assertEquals; 19 import static org.junit.Assert.assertTrue; 20 import static org.mockito.Mockito.doReturn; 21 import static org.mockito.Mockito.mock; 22 import static org.mockito.Mockito.spy; 23 24 import com.android.tradefed.build.DeviceBuildInfo; 25 import com.android.tradefed.build.IDeviceBuildInfo; 26 import com.android.tradefed.config.IConfiguration; 27 import com.android.tradefed.config.OptionSetter; 28 import com.android.tradefed.device.ITestDevice; 29 import com.android.tradefed.result.ITestInvocationListener; 30 import com.android.tradefed.targetprep.ITargetPreparer; 31 import com.android.tradefed.testtype.Abi; 32 import com.android.tradefed.testtype.IAbi; 33 import com.android.tradefed.testtype.IRemoteTest; 34 import com.android.tradefed.testtype.InstrumentationTest; 35 import com.android.tradefed.testtype.UiAutomatorTest; 36 import com.android.tradefed.util.AbiUtils; 37 38 import org.junit.Before; 39 import org.junit.Test; 40 import org.junit.runner.RunWith; 41 import org.junit.runners.JUnit4; 42 import java.io.File; 43 import java.util.ArrayList; 44 import java.util.HashSet; 45 import java.util.LinkedHashMap; 46 import java.util.List; 47 import java.util.Set; 48 49 /** Unit tests for {@link AtestRunner}. */ 50 @RunWith(JUnit4.class) 51 public class AtestRunnerTest { 52 53 private static final String ABI = "armeabi-v7a"; 54 private static final String TEST_NAME_FMT = ABI + " %s"; 55 private static final String INSTRUMENTATION_TEST_NAME = 56 String.format(TEST_NAME_FMT, "tf/instrumentation"); 57 58 private AtestRunner mSpyRunner; 59 private OptionSetter setter; 60 private IDeviceBuildInfo mBuildInfo; 61 private ITestDevice mMockDevice; 62 private String classA = "fully.qualified.classA"; 63 private String classB = "fully.qualified.classB"; 64 private String method1 = "method1"; 65 66 @Before setUp()67 public void setUp() throws Exception { 68 mSpyRunner = spy(new AtestRunner()); 69 mBuildInfo = spy(new DeviceBuildInfo()); 70 mMockDevice = mock(ITestDevice.class); 71 mSpyRunner.setBuild(mBuildInfo); 72 mSpyRunner.setDevice(mMockDevice); 73 74 // Hardcode the abis to avoid failures related to running the tests against a particular 75 // abi build of tradefed. 76 Set<IAbi> abis = new HashSet<>(); 77 abis.add(new Abi(ABI, AbiUtils.getBitness(ABI))); 78 doReturn(abis).when(mSpyRunner).getAbis(mMockDevice); 79 doReturn(new File("some-dir")).when(mBuildInfo).getTestsDir(); 80 } 81 82 @Test testLoadTests_one()83 public void testLoadTests_one() throws Exception { 84 setter = new OptionSetter(mSpyRunner); 85 setter.setOptionValue("include-filter", "tf/fake"); 86 LinkedHashMap<String, IConfiguration> configMap = mSpyRunner.loadTests(); 87 assertEquals(1, configMap.size()); 88 assertTrue(configMap.containsKey(String.format(TEST_NAME_FMT, "tf/fake"))); 89 } 90 91 @Test testLoadTests_two()92 public void testLoadTests_two() throws Exception { 93 setter = new OptionSetter(mSpyRunner); 94 setter.setOptionValue("include-filter", "tf/fake"); 95 setter.setOptionValue("include-filter", "tf/func"); 96 LinkedHashMap<String, IConfiguration> configMap = mSpyRunner.loadTests(); 97 assertEquals(2, configMap.size()); 98 assertTrue(configMap.containsKey(String.format(TEST_NAME_FMT, "tf/fake"))); 99 assertTrue(configMap.containsKey(String.format(TEST_NAME_FMT, "tf/func"))); 100 } 101 102 @Test testLoadTests_filter()103 public void testLoadTests_filter() throws Exception { 104 setter = new OptionSetter(mSpyRunner); 105 setter.setOptionValue("include-filter", "tf/uiautomator"); 106 setter.setOptionValue("atest-include-filter", "tf/uiautomator:" + classA); 107 setter.setOptionValue("atest-include-filter", "tf/uiautomator:" + classB + "#" + method1); 108 LinkedHashMap<String, IConfiguration> configMap = mSpyRunner.loadTests(); 109 assertEquals(1, configMap.size()); 110 String testName = String.format(TEST_NAME_FMT, "tf/uiautomator"); 111 assertTrue(configMap.containsKey(testName)); 112 IConfiguration config = configMap.get(testName); 113 List<IRemoteTest> tests = config.getTests(); 114 assertEquals(1, tests.size()); 115 UiAutomatorTest test = (UiAutomatorTest) tests.get(0); 116 List<String> classFilters = new ArrayList<String>(); 117 classFilters.add(classA); 118 classFilters.add(classB + "#" + method1); 119 assertEquals(classFilters, test.getClassNames()); 120 } 121 122 @Test testLoadTests_ignoreFilter()123 public void testLoadTests_ignoreFilter() throws Exception { 124 setter = new OptionSetter(mSpyRunner); 125 setter.setOptionValue("include-filter", "suite/base-suite1"); 126 setter.setOptionValue("atest-include-filter", "suite/base-suite1:" + classA); 127 LinkedHashMap<String, IConfiguration> configMap = mSpyRunner.loadTests(); 128 assertEquals(1, configMap.size()); 129 String testName = String.format(TEST_NAME_FMT, "suite/base-suite1"); 130 assertTrue(configMap.containsKey(testName)); 131 IConfiguration config = configMap.get(testName); 132 List<IRemoteTest> tests = config.getTests(); 133 assertEquals(1, tests.size()); 134 BaseTestSuite test = (BaseTestSuite) tests.get(0); 135 assertEquals(new HashSet<String>(), test.getIncludeFilter()); 136 } 137 138 @Test testWaitForDebugger()139 public void testWaitForDebugger() throws Exception { 140 OptionSetter setter = new OptionSetter(mSpyRunner); 141 setter.setOptionValue("wait-for-debugger", "true"); 142 setter.setOptionValue("include-filter", "tf/instrumentation"); 143 LinkedHashMap<String, IConfiguration> configMap = mSpyRunner.loadTests(); 144 assertEquals(1, configMap.size()); 145 IConfiguration config = configMap.get(INSTRUMENTATION_TEST_NAME); 146 IRemoteTest test = config.getTests().get(0); 147 assertTrue(((InstrumentationTest) test).getDebug()); 148 } 149 150 @Test testdisableTargetPreparers()151 public void testdisableTargetPreparers() throws Exception { 152 OptionSetter setter = new OptionSetter(mSpyRunner); 153 setter.setOptionValue("disable-target-preparers", "true"); 154 setter.setOptionValue("include-filter", "tf/instrumentation"); 155 LinkedHashMap<String, IConfiguration> configMap = mSpyRunner.loadTests(); 156 assertEquals(1, configMap.size()); 157 IConfiguration config = configMap.get(INSTRUMENTATION_TEST_NAME); 158 for (ITargetPreparer targetPreparer : config.getTargetPreparers()) { 159 assertTrue(targetPreparer.isDisabled()); 160 } 161 } 162 163 @Test testdisableTargetPreparersUnset()164 public void testdisableTargetPreparersUnset() throws Exception { 165 OptionSetter setter = new OptionSetter(mSpyRunner); 166 setter.setOptionValue("include-filter", "tf/instrumentation"); 167 LinkedHashMap<String, IConfiguration> configMap = mSpyRunner.loadTests(); 168 assertEquals(1, configMap.size()); 169 IConfiguration config = configMap.get(INSTRUMENTATION_TEST_NAME); 170 for (ITargetPreparer targetPreparer : config.getTargetPreparers()) { 171 assertTrue(!targetPreparer.isDisabled()); 172 } 173 } 174 175 @Test testDisableTearDown()176 public void testDisableTearDown() throws Exception { 177 OptionSetter setter = new OptionSetter(mSpyRunner); 178 setter.setOptionValue("disable-teardown", "true"); 179 setter.setOptionValue("include-filter", "tf/instrumentation"); 180 LinkedHashMap<String, IConfiguration> configMap = mSpyRunner.loadTests(); 181 IConfiguration config = configMap.get(INSTRUMENTATION_TEST_NAME); 182 for (ITargetPreparer targetPreparer : config.getTargetPreparers()) { 183 assertTrue(targetPreparer.isTearDownDisabled()); 184 } 185 } 186 187 @Test testDisableTearDownUnset()188 public void testDisableTearDownUnset() throws Exception { 189 OptionSetter setter = new OptionSetter(mSpyRunner); 190 setter.setOptionValue("include-filter", "tf/instrumentation"); 191 LinkedHashMap<String, IConfiguration> configMap = mSpyRunner.loadTests(); 192 assertEquals(1, configMap.size()); 193 IConfiguration config = configMap.get(INSTRUMENTATION_TEST_NAME); 194 for (ITargetPreparer targetPreparer : config.getTargetPreparers()) { 195 assertTrue(!targetPreparer.isTearDownDisabled()); 196 } 197 } 198 199 @Test testCreateModuleListener()200 public void testCreateModuleListener() throws Exception { 201 OptionSetter setter = new OptionSetter(mSpyRunner); 202 setter.setOptionValue("subprocess-report-port", "55555"); 203 List<ITestInvocationListener> listeners = mSpyRunner.createModuleListeners(); 204 assertEquals(1, listeners.size()); 205 } 206 } 207