blob: 127986f9382c9c73f20f9a4afc654ee9b875f35c [file] [log] [blame]
Brett Chabot74121d82010-01-28 20:14:27 -08001/*
2 * Copyright (C) 2010 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 */
16package com.android.tradefed.invoker;
17
jdesprezbc580f92017-06-02 11:41:40 -070018import static org.mockito.Mockito.doReturn;
19
20import com.android.ddmlib.IDevice;
jdesprezcf545552017-07-12 02:08:08 -070021import com.android.tradefed.build.BuildInfo;
Brett Chabotbeaebb02011-02-07 16:16:06 -080022import com.android.tradefed.build.BuildRetrievalError;
23import com.android.tradefed.build.IBuildInfo;
jdesprez53387682018-02-27 14:10:46 -080024import com.android.tradefed.build.IBuildInfo.BuildInfoProperties;
Brett Chabotbeaebb02011-02-07 16:16:06 -080025import com.android.tradefed.build.IBuildProvider;
jdesprezda087c02017-06-26 14:23:07 -070026import com.android.tradefed.build.IDeviceBuildInfo;
jdesprez8689aa32018-03-15 09:36:13 -070027import com.android.tradefed.build.IDeviceBuildInfo.ExternalLinkedDir;
Julien Desprezaa9ea852016-11-25 10:56:35 +000028import com.android.tradefed.build.IDeviceBuildProvider;
29import com.android.tradefed.command.CommandOptions;
Julien Desprez75fbb802016-11-15 12:35:58 +000030import com.android.tradefed.command.CommandRunner.ExitCode;
Brett Chabota17af5d2010-08-23 13:51:17 -070031import com.android.tradefed.command.FatalHostError;
Julien Desprezaa9ea852016-11-25 10:56:35 +000032import com.android.tradefed.command.ICommandOptions;
Julien Desprez07fe1532016-08-12 11:48:32 +010033import com.android.tradefed.command.remote.DeviceDescriptor;
Brett Chabotb1052c22011-01-12 19:44:23 -080034import com.android.tradefed.config.Configuration;
Julien Desprez47c287f2016-07-14 12:16:57 +010035import com.android.tradefed.config.ConfigurationDef;
36import com.android.tradefed.config.DeviceConfigurationHolder;
Brett Chabot7e5f8ac2010-05-17 12:27:56 -070037import com.android.tradefed.config.IConfiguration;
Moon Kimb78076b2016-07-06 15:12:07 -070038import com.android.tradefed.config.IConfigurationFactory;
Julien Desprez47c287f2016-07-14 12:16:57 +010039import com.android.tradefed.config.IDeviceConfiguration;
jdesprez839825d2018-01-31 12:14:23 -080040import com.android.tradefed.config.Option;
jdesprezbc580f92017-06-02 11:41:40 -070041import com.android.tradefed.config.OptionSetter;
Julien Desprez07fe1532016-08-12 11:48:32 +010042import com.android.tradefed.device.DeviceAllocationState;
Brett Chabot5e4bc502010-06-10 20:10:30 -070043import com.android.tradefed.device.DeviceNotAvailableException;
Brett Chabotf7ad1562010-07-27 11:58:58 -070044import com.android.tradefed.device.IDeviceRecovery;
jdesprezbc580f92017-06-02 11:41:40 -070045import com.android.tradefed.device.INativeDevice;
Brett Chabot74121d82010-01-28 20:14:27 -080046import com.android.tradefed.device.ITestDevice;
Julien Desprez70488e82016-02-05 11:28:34 +000047import com.android.tradefed.device.ITestDevice.RecoveryMode;
jdesprezbc580f92017-06-02 11:41:40 -070048import com.android.tradefed.device.StubDevice;
Julien Desprez0750c382016-05-23 10:40:26 +010049import com.android.tradefed.device.TestDeviceOptions;
jdesprez267ab892017-09-08 15:30:06 -070050import com.android.tradefed.device.metric.BaseDeviceMetricCollector;
jdesprez267ab892017-09-08 15:30:06 -070051import com.android.tradefed.device.metric.DeviceMetricData;
jdesprez5ae744c2017-10-27 10:36:53 -070052import com.android.tradefed.device.metric.IMetricCollector;
jdesprez7509fca2018-04-02 16:36:56 -070053import com.android.tradefed.guice.InvocationScope;
jdesprezbc580f92017-06-02 11:41:40 -070054import com.android.tradefed.invoker.shard.IShardHelper;
55import com.android.tradefed.invoker.shard.ShardHelper;
56import com.android.tradefed.invoker.shard.StrictShardHelper;
Brett Chabot7e5f8ac2010-05-17 12:27:56 -070057import com.android.tradefed.log.ILeveledLogOutput;
Brett Chabot1d35e662011-05-25 19:48:24 -070058import com.android.tradefed.log.ILogRegistry;
jdesprez39039212018-04-04 16:44:35 -070059import com.android.tradefed.metrics.proto.MetricMeasurement.Measurements;
60import com.android.tradefed.metrics.proto.MetricMeasurement.Metric;
Omari Stephens41978ee2011-02-23 19:03:05 -080061import com.android.tradefed.result.ByteArrayInputStreamSource;
Eric Rowe0b445112013-07-12 09:29:47 -070062import com.android.tradefed.result.ILogSaver;
63import com.android.tradefed.result.ILogSaverListener;
Brett Chabot74121d82010-01-28 20:14:27 -080064import com.android.tradefed.result.ITestInvocationListener;
Omari Stephens203296c2010-09-02 18:20:45 -070065import com.android.tradefed.result.ITestSummaryListener;
Brett Chabot1d35e662011-05-25 19:48:24 -070066import com.android.tradefed.result.InputStreamSource;
Brett Chabot2e441bd2010-09-16 18:25:11 -070067import com.android.tradefed.result.InvocationStatus;
Brett Chabot7e5f8ac2010-05-17 12:27:56 -070068import com.android.tradefed.result.LogDataType;
Eric Rowe0b445112013-07-12 09:29:47 -070069import com.android.tradefed.result.LogFile;
jdesprez821a0052018-01-16 03:37:16 -080070import com.android.tradefed.result.TestDescription;
Omari Stephensa01abfe2010-09-07 15:48:44 -070071import com.android.tradefed.result.TestSummary;
Brett Chabotbeaebb02011-02-07 16:16:06 -080072import com.android.tradefed.targetprep.BuildError;
Brett Chabot87803df2013-03-01 16:01:50 -080073import com.android.tradefed.targetprep.ITargetCleaner;
Brett Chabotbeaebb02011-02-07 16:16:06 -080074import com.android.tradefed.targetprep.ITargetPreparer;
Brett Chabot74121d82010-01-28 20:14:27 -080075import com.android.tradefed.testtype.IDeviceTest;
Julien Desprezaa9ea852016-11-25 10:56:35 +000076import com.android.tradefed.testtype.IInvocationContextReceiver;
Brett Chabot74121d82010-01-28 20:14:27 -080077import com.android.tradefed.testtype.IRemoteTest;
Brett Chabotb1052c22011-01-12 19:44:23 -080078import com.android.tradefed.testtype.IResumableTest;
Eric Rowea3b85852012-07-16 15:07:49 -070079import com.android.tradefed.testtype.IRetriableTest;
Julien Desprez2763ca02016-12-06 14:39:33 +000080import com.android.tradefed.testtype.IShardableTest;
Moon Kimb78076b2016-07-06 15:12:07 -070081import com.android.tradefed.testtype.IStrictShardableTest;
jdesprez267ab892017-09-08 15:30:06 -070082import com.android.tradefed.testtype.StubTest;
jdesprezda087c02017-06-26 14:23:07 -070083import com.android.tradefed.util.FileUtil;
jdesprez8689aa32018-03-15 09:36:13 -070084import com.android.tradefed.util.SystemUtil.EnvVariable;
Brett Chabot74121d82010-01-28 20:14:27 -080085
jdesprezbc580f92017-06-02 11:41:40 -070086import com.google.common.util.concurrent.SettableFuture;
87
Brett Chabot0eabebe2011-09-12 16:15:42 -070088import junit.framework.Test;
89import junit.framework.TestCase;
90
Jed Estepa97349e2016-11-02 20:44:09 -070091import org.easymock.Capture;
92import org.easymock.EasyMock;
jdesprezbc580f92017-06-02 11:41:40 -070093import org.mockito.Mockito;
Jed Estepa97349e2016-11-02 20:44:09 -070094
jdesprezda087c02017-06-26 14:23:07 -070095import java.io.File;
Eric Rowe0b445112013-07-12 09:29:47 -070096import java.io.IOException;
97import java.io.InputStream;
Brett Chabot664b8932010-08-06 17:17:33 -070098import java.util.ArrayList;
Brett Chabotbcb497c2010-08-07 17:07:36 -070099import java.util.Collections;
Julien Desprez3ff41822018-05-21 17:59:56 -0700100import java.util.HashMap;
jdesprez53387682018-02-27 14:10:46 -0800101import java.util.HashSet;
Brett Chabot664b8932010-08-06 17:17:33 -0700102import java.util.List;
Brett Chabotbcb497c2010-08-07 17:07:36 -0700103import java.util.Map;
jdesprez53387682018-02-27 14:10:46 -0800104import java.util.Set;
Brett Chabot7e5f8ac2010-05-17 12:27:56 -0700105
jdesprezd0e00da2017-09-07 18:54:49 -0700106/** Unit tests for {@link TestInvocation}. */
107@SuppressWarnings("MustBeClosedChecker")
Brett Chabot74121d82010-01-28 20:14:27 -0800108public class TestInvocationTest extends TestCase {
109
Brett Chaboteba1fd62011-09-13 17:17:45 -0700110 private static final String SERIAL = "serial";
Brett Chabotbcb497c2010-08-07 17:07:36 -0700111 private static final Map<String, String> EMPTY_MAP = Collections.emptyMap();
Eric Rowe0b445112013-07-12 09:29:47 -0700112 private static final String PATH = "path";
113 private static final String URL = "url";
Omari Stephensa01abfe2010-09-07 15:48:44 -0700114 private static final TestSummary mSummary = new TestSummary("http://www.url.com/report.txt");
Michael Rosenfeld55012252017-01-12 15:45:19 -0800115 private static final InputStreamSource EMPTY_STREAM_SOURCE =
116 new ByteArrayInputStreamSource(new byte[0]);
117 private static final String LOGCAT_NAME_ERROR =
Michael Rosenfeldb0f58d22017-01-26 14:38:32 -0800118 TestInvocation.getDeviceLogName(TestInvocation.Stage.ERROR);
Michael Rosenfeld55012252017-01-12 15:45:19 -0800119 private static final String LOGCAT_NAME_SETUP =
Michael Rosenfeldb0f58d22017-01-26 14:38:32 -0800120 TestInvocation.getDeviceLogName(TestInvocation.Stage.SETUP);
Michael Rosenfeld55012252017-01-12 15:45:19 -0800121 private static final String LOGCAT_NAME_TEST =
Michael Rosenfeldb0f58d22017-01-26 14:38:32 -0800122 TestInvocation.getDeviceLogName(TestInvocation.Stage.TEST);
Michael Rosenfeld55012252017-01-12 15:45:19 -0800123 private static final String LOGCAT_NAME_TEARDOWN =
Michael Rosenfeldb0f58d22017-01-26 14:38:32 -0800124 TestInvocation.getDeviceLogName(TestInvocation.Stage.TEARDOWN);
Brett Chabot74121d82010-01-28 20:14:27 -0800125 /** The {@link TestInvocation} under test, with all dependencies mocked out */
126 private TestInvocation mTestInvocation;
127
Brett Chabotb1052c22011-01-12 19:44:23 -0800128 private IConfiguration mStubConfiguration;
Julien Desprez47c287f2016-07-14 12:16:57 +0100129 private IConfiguration mStubMultiConfiguration;
130
131 private IInvocationContext mStubInvocationMetadata;
Brett Chabotb1052c22011-01-12 19:44:23 -0800132
Brett Chabot7e5f8ac2010-05-17 12:27:56 -0700133 // The mock objects.
Brett Chabot74121d82010-01-28 20:14:27 -0800134 private ITestDevice mMockDevice;
135 private ITargetPreparer mMockPreparer;
Brett Chabotfe88a152010-08-02 17:52:59 -0700136 private IBuildProvider mMockBuildProvider;
Brett Chabot74121d82010-01-28 20:14:27 -0800137 private IBuildInfo mMockBuildInfo;
138 private ITestInvocationListener mMockTestListener;
Omari Stephens203296c2010-09-02 18:20:45 -0700139 private ITestSummaryListener mMockSummaryListener;
Brett Chabot7e5f8ac2010-05-17 12:27:56 -0700140 private ILeveledLogOutput mMockLogger;
Eric Rowe0b445112013-07-12 09:29:47 -0700141 private ILogSaver mMockLogSaver;
Brett Chabotf7ad1562010-07-27 11:58:58 -0700142 private IDeviceRecovery mMockRecovery;
Omari Stephensa01abfe2010-09-07 15:48:44 -0700143 private Capture<List<TestSummary>> mUriCapture;
Brett Chabot1d35e662011-05-25 19:48:24 -0700144 private ILogRegistry mMockLogRegistry;
Moon Kimb78076b2016-07-06 15:12:07 -0700145 private IConfigurationFactory mMockConfigFactory;
Julien Desprez2040ac02016-03-21 18:05:30 +0000146 private IRescheduler mockRescheduler;
Julien Desprez07fe1532016-08-12 11:48:32 +0100147 private DeviceDescriptor mFakeDescriptor;
Brett Chabot74121d82010-01-28 20:14:27 -0800148
149 @Override
150 protected void setUp() throws Exception {
151 super.setUp();
152
Brett Chabote7e335e2011-02-16 14:51:03 -0800153 mStubConfiguration = new Configuration("foo", "bar");
Julien Desprez47c287f2016-07-14 12:16:57 +0100154 mStubMultiConfiguration = new Configuration("foo", "bar");
Brett Chabotb1052c22011-01-12 19:44:23 -0800155
Brett Chabot74121d82010-01-28 20:14:27 -0800156 mMockDevice = EasyMock.createMock(ITestDevice.class);
Brett Chabotf7ad1562010-07-27 11:58:58 -0700157 mMockRecovery = EasyMock.createMock(IDeviceRecovery.class);
Brett Chabot74121d82010-01-28 20:14:27 -0800158 mMockPreparer = EasyMock.createMock(ITargetPreparer.class);
Brett Chabotfe88a152010-08-02 17:52:59 -0700159 mMockBuildProvider = EasyMock.createMock(IBuildProvider.class);
Brett Chabotb1052c22011-01-12 19:44:23 -0800160
Omari Stephens203296c2010-09-02 18:20:45 -0700161 // Use strict mocks here since the order of Listener calls is important
162 mMockTestListener = EasyMock.createStrictMock(ITestInvocationListener.class);
163 mMockSummaryListener = EasyMock.createStrictMock(ITestSummaryListener.class);
Brett Chabot5dae8722010-09-17 20:05:51 -0700164 mMockBuildInfo = EasyMock.createMock(IBuildInfo.class);
Brett Chabot35d50c62011-03-07 10:39:23 -0800165 mMockLogger = EasyMock.createMock(ILeveledLogOutput.class);
Brett Chabot1d35e662011-05-25 19:48:24 -0700166 mMockLogRegistry = EasyMock.createMock(ILogRegistry.class);
Eric Rowe0b445112013-07-12 09:29:47 -0700167 mMockLogSaver = EasyMock.createMock(ILogSaver.class);
Moon Kimb78076b2016-07-06 15:12:07 -0700168 mMockConfigFactory = EasyMock.createMock(IConfigurationFactory.class);
Julien Desprez2040ac02016-03-21 18:05:30 +0000169 mockRescheduler = EasyMock.createMock(IRescheduler.class);
Brett Chabot74121d82010-01-28 20:14:27 -0800170
Brett Chabotb1052c22011-01-12 19:44:23 -0800171 mStubConfiguration.setDeviceRecovery(mMockRecovery);
172 mStubConfiguration.setTargetPreparer(mMockPreparer);
173 mStubConfiguration.setBuildProvider(mMockBuildProvider);
Julien Desprez47c287f2016-07-14 12:16:57 +0100174
jdesprez5ae744c2017-10-27 10:36:53 -0700175 EasyMock.expect(mMockPreparer.isDisabled()).andStubReturn(false);
mikehoran458b2b12018-02-28 16:07:13 -0800176 EasyMock.expect(mMockPreparer.isTearDownDisabled()).andStubReturn(false);
jdesprez5ae744c2017-10-27 10:36:53 -0700177
Julien Desprez47c287f2016-07-14 12:16:57 +0100178 List<IDeviceConfiguration> deviceConfigs = new ArrayList<IDeviceConfiguration>();
Julien Desprez53353e62016-08-12 15:24:33 +0100179 IDeviceConfiguration device1 =
180 new DeviceConfigurationHolder(ConfigurationDef.DEFAULT_DEVICE_NAME);
Julien Desprez47c287f2016-07-14 12:16:57 +0100181 device1.addSpecificConfig(mMockRecovery);
182 device1.addSpecificConfig(mMockPreparer);
183 device1.addSpecificConfig(mMockBuildProvider);
184 deviceConfigs.add(device1);
185 mStubMultiConfiguration.setDeviceConfigList(deviceConfigs);
186
Eric Rowe0b445112013-07-12 09:29:47 -0700187 mStubConfiguration.setLogSaver(mMockLogSaver);
Julien Desprez47c287f2016-07-14 12:16:57 +0100188 mStubMultiConfiguration.setLogSaver(mMockLogSaver);
Omari Stephens203296c2010-09-02 18:20:45 -0700189
Brett Chabotad123582011-02-09 18:26:40 -0800190 List<ITestInvocationListener> listenerList = new ArrayList<ITestInvocationListener>(1);
191 listenerList.add(mMockTestListener);
192 listenerList.add(mMockSummaryListener);
193 mStubConfiguration.setTestInvocationListeners(listenerList);
Julien Desprez47c287f2016-07-14 12:16:57 +0100194 mStubMultiConfiguration.setTestInvocationListeners(listenerList);
Omari Stephens203296c2010-09-02 18:20:45 -0700195
Brett Chabotb1052c22011-01-12 19:44:23 -0800196 mStubConfiguration.setLogOutput(mMockLogger);
Julien Desprez47c287f2016-07-14 12:16:57 +0100197 mStubMultiConfiguration.setLogOutput(mMockLogger);
Brett Chaboteba1fd62011-09-13 17:17:45 -0700198 EasyMock.expect(mMockDevice.getSerialNumber()).andStubReturn(SERIAL);
Guang Zhu789a52b2014-01-17 14:27:01 -0800199 EasyMock.expect(mMockDevice.getIDevice()).andStubReturn(null);
Brett Chabotf7ad1562010-07-27 11:58:58 -0700200 mMockDevice.setRecovery(mMockRecovery);
Julien Desprez26bee8d2016-03-29 12:09:48 +0100201 mMockDevice.preInvocationSetup((IBuildInfo)EasyMock.anyObject());
202 EasyMock.expectLastCall().anyTimes();
203 mMockDevice.postInvocationTearDown();
204 EasyMock.expectLastCall().anyTimes();
Julien Desprez07fe1532016-08-12 11:48:32 +0100205 mFakeDescriptor = new DeviceDescriptor(SERIAL, false, DeviceAllocationState.Available,
206 "unknown", "unknown", "unknown", "unknown", "unknown");
207 EasyMock.expect(mMockDevice.getDeviceDescriptor()).andStubReturn(mFakeDescriptor);
Brett Chabot74121d82010-01-28 20:14:27 -0800208
Jorge Gonzalez3777e322011-05-11 18:04:29 -0700209 EasyMock.expect(mMockBuildInfo.getBuildId()).andStubReturn("1");
Brett Chabotbcb497c2010-08-07 17:07:36 -0700210 EasyMock.expect(mMockBuildInfo.getBuildAttributes()).andStubReturn(EMPTY_MAP);
Brett Chabot2b79e0f2013-03-18 16:50:16 -0700211 EasyMock.expect(mMockBuildInfo.getBuildBranch()).andStubReturn("branch");
212 EasyMock.expect(mMockBuildInfo.getBuildFlavor()).andStubReturn("flavor");
jdesprez53387682018-02-27 14:10:46 -0800213 EasyMock.expect(mMockBuildInfo.getProperties()).andStubReturn(new HashSet<>());
Julien Desprezf7f32c92016-07-27 10:07:50 +0100214
Brett Chabot1d35e662011-05-25 19:48:24 -0700215 // always expect logger initialization and cleanup calls
216 mMockLogRegistry.registerLogger(mMockLogger);
217 mMockLogger.init();
218 mMockLogger.closeLog();
219 mMockLogRegistry.unregisterLogger();
Omari Stephensa01abfe2010-09-07 15:48:44 -0700220 mUriCapture = new Capture<List<TestSummary>>();
Brett Chabotbcb497c2010-08-07 17:07:36 -0700221
Julien Desprez47c287f2016-07-14 12:16:57 +0100222 mStubInvocationMetadata = new InvocationContext();
223 mStubInvocationMetadata.addAllocatedDevice(ConfigurationDef.DEFAULT_DEVICE_NAME,
224 mMockDevice);
Julien Desprez53353e62016-08-12 15:24:33 +0100225 mStubInvocationMetadata.addDeviceBuildInfo(ConfigurationDef.DEFAULT_DEVICE_NAME,
226 mMockBuildInfo);
Julien Desprez47c287f2016-07-14 12:16:57 +0100227
Brett Chabot74121d82010-01-28 20:14:27 -0800228 // create the BaseTestInvocation to test
jdesprezbc580f92017-06-02 11:41:40 -0700229 mTestInvocation =
230 new TestInvocation() {
231 @Override
232 ILogRegistry getLogRegistry() {
233 return mMockLogRegistry;
234 }
Moon Kimb78076b2016-07-06 15:12:07 -0700235
jdesprezbc580f92017-06-02 11:41:40 -0700236 @Override
jdesprez481a83f2018-03-14 16:20:39 -0700237 public IInvocationExecution createInvocationExec(boolean isSandboxed) {
jdesprez85129dd2017-11-13 06:05:57 -0800238 return new InvocationExecution() {
239 @Override
240 protected IShardHelper createShardHelper() {
241 return new ShardHelper();
242 }
243 };
jdesprezbc580f92017-06-02 11:41:40 -0700244 }
245
246 @Override
247 protected void setExitCode(ExitCode code, Throwable stack) {
248 // empty on purpose
249 }
jdesprez7509fca2018-04-02 16:36:56 -0700250
251 @Override
252 InvocationScope getInvocationScope() {
253 // Avoid re-entry in the current TF invocation scope for unit tests.
254 return new InvocationScope();
255 }
jdesprezbc580f92017-06-02 11:41:40 -0700256 };
Brett Chabot74121d82010-01-28 20:14:27 -0800257 }
258
Neal Nguyen2ceabaa2010-05-26 14:42:14 -0700259 @Override
260 protected void tearDown() throws Exception {
261 super.tearDown();
262
263 }
264
Brett Chabot74121d82010-01-28 20:14:27 -0800265 /**
266 * Test the normal case invoke scenario with a {@link IRemoteTest}.
267 * <p/>
268 * Verifies that all external interfaces get notified as expected.
269 */
Brett Chabot87803df2013-03-01 16:01:50 -0800270 public void testInvoke_RemoteTest() throws Throwable {
Brett Chabot74121d82010-01-28 20:14:27 -0800271 IRemoteTest test = EasyMock.createMock(IRemoteTest.class);
Omari Stephens203296c2010-09-02 18:20:45 -0700272 setupMockSuccessListeners();
Julien Desprez26bee8d2016-03-29 12:09:48 +0100273
Brett Chabotad123582011-02-09 18:26:40 -0800274 test.run((ITestInvocationListener)EasyMock.anyObject());
Brett Chabot7e5f8ac2010-05-17 12:27:56 -0700275 setupNormalInvoke(test);
Julien Desprez2040ac02016-03-21 18:05:30 +0000276 EasyMock.replay(mockRescheduler);
Julien Desprez47c287f2016-07-14 12:16:57 +0100277 mTestInvocation.invoke(mStubInvocationMetadata, mStubConfiguration, mockRescheduler);
278 verifyMocks(test, mockRescheduler);
279 verifySummaryListener();
280 }
281
282 /**
283 * Test the normal case for multi invoke scenario with a {@link IRemoteTest}.
284 * <p/>
285 * Verifies that all external interfaces get notified as expected.
286 */
287 public void testInvokeMulti_RemoteTest() throws Throwable {
288 IRemoteTest test = EasyMock.createMock(IRemoteTest.class);
289 setupMockSuccessListeners();
290
291 test.run((ITestInvocationListener)EasyMock.anyObject());
292 setupNormalInvoke(test);
293 EasyMock.replay(mockRescheduler);
Julien Desprez53353e62016-08-12 15:24:33 +0100294 mTestInvocation.invoke(mStubInvocationMetadata, mStubMultiConfiguration, mockRescheduler);
Julien Desprez2040ac02016-03-21 18:05:30 +0000295 verifyMocks(test, mockRescheduler);
Omari Stephens203296c2010-09-02 18:20:45 -0700296 verifySummaryListener();
297 }
298
299 /**
300 * Test the normal case invoke scenario with an {@link ITestSummaryListener} masquerading as
301 * an {@link ITestInvocationListener}.
302 * <p/>
303 * Verifies that all external interfaces get notified as expected.
Omari Stephens203296c2010-09-02 18:20:45 -0700304 */
Julien Desprez78fcda22016-06-16 17:35:11 +0100305 public void testInvoke_twoSummary() throws Throwable {
Omari Stephens203296c2010-09-02 18:20:45 -0700306
307 IRemoteTest test = EasyMock.createMock(IRemoteTest.class);
308 setupMockSuccessListeners();
309
Brett Chabotad123582011-02-09 18:26:40 -0800310 test.run((ITestInvocationListener)EasyMock.anyObject());
Omari Stephens203296c2010-09-02 18:20:45 -0700311 setupNormalInvoke(test);
Julien Desprez2040ac02016-03-21 18:05:30 +0000312 EasyMock.replay(mockRescheduler);
Julien Desprez47c287f2016-07-14 12:16:57 +0100313 mTestInvocation.invoke(mStubInvocationMetadata, mStubConfiguration, mockRescheduler);
Julien Desprez2040ac02016-03-21 18:05:30 +0000314 verifyMocks(test, mockRescheduler);
Omari Stephens203296c2010-09-02 18:20:45 -0700315 verifySummaryListener();
Brett Chabot74121d82010-01-28 20:14:27 -0800316 }
317
318 /**
319 * Test the invoke scenario where build retrieve fails.
Brett Chabot057e16e2010-06-27 21:53:48 -0700320 * <p/>
Brett Chabotc9924302011-06-30 16:48:01 -0700321 * An invocation will be started in this scenario.
Brett Chabot74121d82010-01-28 20:14:27 -0800322 */
Brett Chabot87803df2013-03-01 16:01:50 -0800323 public void testInvoke_buildFailed() throws Throwable {
Brett Chabotc9924302011-06-30 16:48:01 -0700324 BuildRetrievalError exception = new BuildRetrievalError("error", null, mMockBuildInfo);
Brett Chabotfe88a152010-08-02 17:52:59 -0700325 EasyMock.expect(mMockBuildProvider.getBuild()).andThrow(exception);
Julien Desprez949ba3a2016-11-28 15:18:32 +0000326 EasyMock.expect(mMockBuildInfo.getTestTag()).andStubReturn(null);
Brett Chabotc9924302011-06-30 16:48:01 -0700327 setupMockFailureListeners(exception);
Brett Chabot44556372014-03-19 19:07:15 -0700328 setupInvoke();
Brett Chabotccfda2e2011-02-09 14:39:59 -0800329 IRemoteTest test = EasyMock.createMock(IRemoteTest.class);
Julien Desprez949ba3a2016-11-28 15:18:32 +0000330 CommandOptions cmdOptions = new CommandOptions();
331 final String expectedTestTag = "TEST_TAG";
332 cmdOptions.setTestTag(expectedTestTag);
333 mStubConfiguration.setCommandOptions(cmdOptions);
Brett Chabotb1052c22011-01-12 19:44:23 -0800334 mStubConfiguration.setTest(test);
Michael Rosenfeld55012252017-01-12 15:45:19 -0800335 EasyMock.expect(mMockLogger.getLog()).andReturn(EMPTY_STREAM_SOURCE);
336 EasyMock.expect(mMockDevice.getLogcat()).andReturn(EMPTY_STREAM_SOURCE).times(2);
337 mMockDevice.clearLogcat();
338 EasyMock.expectLastCall().times(2);
jdesprez3dff70b2017-04-18 10:39:13 -0700339 mMockLogRegistry.unregisterLogger();
340 mMockLogRegistry.dumpToGlobalLog(mMockLogger);
341 mMockLogger.closeLog();
jdespreza90f9722017-08-11 16:48:24 -0700342 mMockBuildProvider.cleanUp(mMockBuildInfo);
Julien Desprez2040ac02016-03-21 18:05:30 +0000343 replayMocks(test, mockRescheduler);
Julien Desprez47c287f2016-07-14 12:16:57 +0100344 mTestInvocation.invoke(mStubInvocationMetadata, mStubConfiguration, mockRescheduler);
Julien Desprez2040ac02016-03-21 18:05:30 +0000345 verifyMocks(test, mockRescheduler);
Julien Desprez949ba3a2016-11-28 15:18:32 +0000346 // invocation test tag was updated.
347 assertEquals(expectedTestTag, mStubInvocationMetadata.getTestTag());
Brett Chabot74121d82010-01-28 20:14:27 -0800348 }
349
350 /**
Brett Chabotdd21ddf2010-06-07 14:16:19 -0700351 * Test the invoke scenario where there is no build to test.
352 */
Brett Chabot87803df2013-03-01 16:01:50 -0800353 public void testInvoke_noBuild() throws Throwable {
Brett Chabotfe88a152010-08-02 17:52:59 -0700354 EasyMock.expect(mMockBuildProvider.getBuild()).andReturn(null);
Brett Chabotccfda2e2011-02-09 14:39:59 -0800355 IRemoteTest test = EasyMock.createMock(IRemoteTest.class);
Brett Chabotb1052c22011-01-12 19:44:23 -0800356 mStubConfiguration.setTest(test);
jdespreza90f9722017-08-11 16:48:24 -0700357 mMockBuildProvider.cleanUp(mMockBuildInfo);
Brett Chabot1d35e662011-05-25 19:48:24 -0700358 mMockLogRegistry.dumpToGlobalLog(mMockLogger);
Brett Chabot44556372014-03-19 19:07:15 -0700359 setupInvoke();
Julien Desprez2040ac02016-03-21 18:05:30 +0000360 replayMocks(test, mockRescheduler);
Julien Desprez47c287f2016-07-14 12:16:57 +0100361 mTestInvocation.invoke(mStubInvocationMetadata, mStubConfiguration, mockRescheduler);
Brett Chabot057e16e2010-06-27 21:53:48 -0700362 verifyMocks(test);
Brett Chabot74121d82010-01-28 20:14:27 -0800363 }
364
Brett Chabot74121d82010-01-28 20:14:27 -0800365 /**
Eric Rowef85ffac2012-10-03 12:56:12 -0700366 * Test the invoke scenario where there is no build to test for a {@link IRetriableTest}.
367 */
Brett Chabot87803df2013-03-01 16:01:50 -0800368 public void testInvoke_noBuildRetry() throws Throwable {
Eric Rowef85ffac2012-10-03 12:56:12 -0700369 EasyMock.expect(mMockBuildProvider.getBuild()).andReturn(null);
370
371 IRetriableTest test = EasyMock.createMock(IRetriableTest.class);
372 EasyMock.expect(test.isRetriable()).andReturn(Boolean.TRUE);
373
Eric Rowef85ffac2012-10-03 12:56:12 -0700374 EasyMock.expect(mockRescheduler.rescheduleCommand()).andReturn(EasyMock.anyBoolean());
375
376 mStubConfiguration.setTest(test);
377 mStubConfiguration.getCommandOptions().setLoopMode(false);
378 mMockLogRegistry.dumpToGlobalLog(mMockLogger);
jdespreza90f9722017-08-11 16:48:24 -0700379 EasyMock.expectLastCall().times(1);
Brett Chabot44556372014-03-19 19:07:15 -0700380 setupInvoke();
jdespreza90f9722017-08-11 16:48:24 -0700381 mMockBuildProvider.cleanUp(mMockBuildInfo);
Eric Rowef85ffac2012-10-03 12:56:12 -0700382 replayMocks(test);
383 EasyMock.replay(mockRescheduler);
Julien Desprez47c287f2016-07-14 12:16:57 +0100384 mTestInvocation.invoke(mStubInvocationMetadata, mStubConfiguration, mockRescheduler);
Eric Rowef85ffac2012-10-03 12:56:12 -0700385 EasyMock.verify(mockRescheduler);
386 verifyMocks(test);
387 }
388
389 /**
Julien Desprez47c287f2016-07-14 12:16:57 +0100390 * Test the{@link TestInvocation#invoke(IInvocationContext, IConfiguration, IRescheduler,
Julien Desprez2040ac02016-03-21 18:05:30 +0000391 * ITestInvocationListener[])} scenario
Eric Rowe0b445112013-07-12 09:29:47 -0700392 * where the test is a {@link IDeviceTest}
Brett Chabot74121d82010-01-28 20:14:27 -0800393 */
Brett Chabot87803df2013-03-01 16:01:50 -0800394 public void testInvoke_deviceTest() throws Throwable {
Brett Chabot2e441bd2010-09-16 18:25:11 -0700395 DeviceConfigTest mockDeviceTest = EasyMock.createMock(DeviceConfigTest.class);
Brett Chabotb1052c22011-01-12 19:44:23 -0800396 mStubConfiguration.setTest(mockDeviceTest);
Brett Chabot2e441bd2010-09-16 18:25:11 -0700397 mockDeviceTest.setDevice(mMockDevice);
Brett Chabotad123582011-02-09 18:26:40 -0800398 mockDeviceTest.run((ITestInvocationListener)EasyMock.anyObject());
Omari Stephens203296c2010-09-02 18:20:45 -0700399 setupMockSuccessListeners();
Brett Chabot2e441bd2010-09-16 18:25:11 -0700400 setupNormalInvoke(mockDeviceTest);
Julien Desprez2040ac02016-03-21 18:05:30 +0000401 EasyMock.replay(mockRescheduler);
Julien Desprez47c287f2016-07-14 12:16:57 +0100402 mTestInvocation.invoke(mStubInvocationMetadata, mStubConfiguration, mockRescheduler);
Julien Desprez2040ac02016-03-21 18:05:30 +0000403 verifyMocks(mockDeviceTest, mockRescheduler);
Omari Stephens203296c2010-09-02 18:20:45 -0700404 verifySummaryListener();
Brett Chabot74121d82010-01-28 20:14:27 -0800405 }
406
407 /**
408 * Test the invoke scenario where test run throws {@link IllegalArgumentException}
409 *
410 * @throws Exception if unexpected error occurs
411 */
Brett Chabot87803df2013-03-01 16:01:50 -0800412 public void testInvoke_testFail() throws Throwable {
Brett Chabot7e5f8ac2010-05-17 12:27:56 -0700413 IllegalArgumentException exception = new IllegalArgumentException();
Brett Chabot74121d82010-01-28 20:14:27 -0800414 IRemoteTest test = EasyMock.createMock(IRemoteTest.class);
Brett Chabotad123582011-02-09 18:26:40 -0800415 test.run((ITestInvocationListener)EasyMock.anyObject());
Brett Chabot7e5f8ac2010-05-17 12:27:56 -0700416 EasyMock.expectLastCall().andThrow(exception);
Omari Stephens203296c2010-09-02 18:20:45 -0700417 setupMockFailureListeners(exception);
Brett Chabotfe88a152010-08-02 17:52:59 -0700418 mMockBuildProvider.buildNotTested(mMockBuildInfo);
Brett Chabot7e5f8ac2010-05-17 12:27:56 -0700419 setupNormalInvoke(test);
Julien Desprez2040ac02016-03-21 18:05:30 +0000420 EasyMock.replay(mockRescheduler);
Omari Stephens203296c2010-09-02 18:20:45 -0700421 try {
Julien Desprez47c287f2016-07-14 12:16:57 +0100422 mTestInvocation.invoke(mStubInvocationMetadata, mStubConfiguration, mockRescheduler);
Omari Stephens203296c2010-09-02 18:20:45 -0700423 fail("IllegalArgumentException was not rethrown");
424 } catch (IllegalArgumentException e) {
425 // expected
426 }
Julien Desprez2040ac02016-03-21 18:05:30 +0000427 verifyMocks(test, mockRescheduler);
Omari Stephens203296c2010-09-02 18:20:45 -0700428 verifySummaryListener();
Brett Chabot74121d82010-01-28 20:14:27 -0800429 }
430
431 /**
Brett Chabota17af5d2010-08-23 13:51:17 -0700432 * Test the invoke scenario where test run throws {@link FatalHostError}
433 *
434 * @throws Exception if unexpected error occurs
435 */
Brett Chabot87803df2013-03-01 16:01:50 -0800436 public void testInvoke_fatalError() throws Throwable {
Brett Chabota17af5d2010-08-23 13:51:17 -0700437 FatalHostError exception = new FatalHostError("error");
438 IRemoteTest test = EasyMock.createMock(IRemoteTest.class);
Brett Chabotad123582011-02-09 18:26:40 -0800439 test.run((ITestInvocationListener)EasyMock.anyObject());
Brett Chabota17af5d2010-08-23 13:51:17 -0700440 EasyMock.expectLastCall().andThrow(exception);
Omari Stephens203296c2010-09-02 18:20:45 -0700441 setupMockFailureListeners(exception);
Brett Chabota17af5d2010-08-23 13:51:17 -0700442 mMockBuildProvider.buildNotTested(mMockBuildInfo);
Brett Chabota17af5d2010-08-23 13:51:17 -0700443 setupNormalInvoke(test);
Julien Desprez2040ac02016-03-21 18:05:30 +0000444 EasyMock.replay(mockRescheduler);
Brett Chabota17af5d2010-08-23 13:51:17 -0700445 try {
Julien Desprez47c287f2016-07-14 12:16:57 +0100446 mTestInvocation.invoke(mStubInvocationMetadata, mStubConfiguration, mockRescheduler);
Brett Chabota17af5d2010-08-23 13:51:17 -0700447 fail("FatalHostError was not rethrown");
448 } catch (FatalHostError e) {
449 // expected
450 }
Julien Desprez2040ac02016-03-21 18:05:30 +0000451 verifyMocks(test, mockRescheduler);
Omari Stephens203296c2010-09-02 18:20:45 -0700452 verifySummaryListener();
Brett Chabota17af5d2010-08-23 13:51:17 -0700453 }
454
455 /**
Eric Rowe0b445112013-07-12 09:29:47 -0700456 * Test the invoke scenario where test run throws {@link DeviceNotAvailableException}
Brett Chabot3759c852010-07-13 17:36:31 -0700457 *
458 * @throws Exception if unexpected error occurs
459 */
Brett Chabot87803df2013-03-01 16:01:50 -0800460 public void testInvoke_deviceNotAvail() throws Throwable {
Julien Desprez47c287f2016-07-14 12:16:57 +0100461 DeviceNotAvailableException exception = new DeviceNotAvailableException("ERROR", SERIAL);
Brett Chabot3759c852010-07-13 17:36:31 -0700462 IRemoteTest test = EasyMock.createMock(IRemoteTest.class);
Brett Chabotad123582011-02-09 18:26:40 -0800463 test.run((ITestInvocationListener)EasyMock.anyObject());
Brett Chabot3759c852010-07-13 17:36:31 -0700464 EasyMock.expectLastCall().andThrow(exception);
Julien Desprez70488e82016-02-05 11:28:34 +0000465 mMockDevice.setRecoveryMode(RecoveryMode.NONE);
466 EasyMock.expectLastCall();
Omari Stephens203296c2010-09-02 18:20:45 -0700467 setupMockFailureListeners(exception);
Brett Chabotfe88a152010-08-02 17:52:59 -0700468 mMockBuildProvider.buildNotTested(mMockBuildInfo);
Brett Chabot3759c852010-07-13 17:36:31 -0700469 setupNormalInvoke(test);
Julien Desprez2040ac02016-03-21 18:05:30 +0000470 EasyMock.replay(mockRescheduler);
Brett Chabot3759c852010-07-13 17:36:31 -0700471 try {
Julien Desprez47c287f2016-07-14 12:16:57 +0100472 mTestInvocation.invoke(mStubInvocationMetadata, mStubConfiguration, mockRescheduler);
Brett Chabot3759c852010-07-13 17:36:31 -0700473 fail("DeviceNotAvailableException not thrown");
Brett Chabot3759c852010-07-13 17:36:31 -0700474 } catch (DeviceNotAvailableException e) {
475 // expected
476 }
Julien Desprez2040ac02016-03-21 18:05:30 +0000477 verifyMocks(test, mockRescheduler);
Omari Stephens203296c2010-09-02 18:20:45 -0700478 verifySummaryListener();
Brett Chabot3759c852010-07-13 17:36:31 -0700479 }
480
481 /**
Omari Stephens203296c2010-09-02 18:20:45 -0700482 * Test the invoke scenario where preparer throws {@link BuildError}
Brett Chabotfe88a152010-08-02 17:52:59 -0700483 *
484 * @throws Exception if unexpected error occurs
485 */
Brett Chabot87803df2013-03-01 16:01:50 -0800486 public void testInvoke_buildError() throws Throwable {
Julien Desprez07fe1532016-08-12 11:48:32 +0100487 BuildError exception = new BuildError("error", mFakeDescriptor);
Brett Chabotfe88a152010-08-02 17:52:59 -0700488 IRemoteTest test = EasyMock.createMock(IRemoteTest.class);
Brett Chabotb1052c22011-01-12 19:44:23 -0800489 mStubConfiguration.setTest(test);
Brett Chabotfe88a152010-08-02 17:52:59 -0700490 EasyMock.expect(mMockBuildProvider.getBuild()).andReturn(mMockBuildInfo);
Brett Chabot1d35e662011-05-25 19:48:24 -0700491
Brett Chabotfe88a152010-08-02 17:52:59 -0700492 mMockPreparer.setUp(mMockDevice, mMockBuildInfo);
493 EasyMock.expectLastCall().andThrow(exception);
Omari Stephens203296c2010-09-02 18:20:45 -0700494 setupMockFailureListeners(exception);
Michael Rosenfeld55012252017-01-12 15:45:19 -0800495 EasyMock.expect(mMockDevice.getBugreport()).andReturn(EMPTY_STREAM_SOURCE);
Brett Chabot44556372014-03-19 19:07:15 -0700496 setupInvokeWithBuild();
Brett Chabotfe88a152010-08-02 17:52:59 -0700497 replayMocks(test);
Julien Desprez2040ac02016-03-21 18:05:30 +0000498 EasyMock.replay(mockRescheduler);
Julien Desprez47c287f2016-07-14 12:16:57 +0100499 mTestInvocation.invoke(mStubInvocationMetadata, mStubConfiguration, mockRescheduler);
Julien Desprez2040ac02016-03-21 18:05:30 +0000500 verifyMocks(test, mockRescheduler);
Omari Stephens203296c2010-09-02 18:20:45 -0700501 verifySummaryListener();
Brett Chabotfe88a152010-08-02 17:52:59 -0700502 }
503
Brett Chabotfe88a152010-08-02 17:52:59 -0700504 /**
Brett Chabotb1052c22011-01-12 19:44:23 -0800505 * Test the invoke scenario for a {@link IResumableTest}.
506 *
507 * @throws Exception if unexpected error occurs
508 */
Brett Chabot87803df2013-03-01 16:01:50 -0800509 public void testInvoke_resume() throws Throwable {
Brett Chabotb1052c22011-01-12 19:44:23 -0800510 IResumableTest resumableTest = EasyMock.createMock(IResumableTest.class);
511 mStubConfiguration.setTest(resumableTest);
512 ITestInvocationListener resumeListener = EasyMock.createStrictMock(
513 ITestInvocationListener.class);
514 mStubConfiguration.setTestInvocationListener(resumeListener);
515
516 EasyMock.expect(mMockBuildProvider.getBuild()).andReturn(mMockBuildInfo);
Julien Desprez53353e62016-08-12 15:24:33 +0100517 resumeListener.invocationStarted(mStubInvocationMetadata);
Moon Kim80553c52014-09-15 13:45:40 -0700518 mMockDevice.clearLastConnectedWifiNetwork();
Brett Chabotc8b709f2011-09-02 09:04:22 -0700519 mMockDevice.setOptions((TestDeviceOptions)EasyMock.anyObject());
Brett Chaboteba1fd62011-09-13 17:17:45 -0700520 mMockBuildInfo.setDeviceSerial(SERIAL);
Julien Desprezf7f32c92016-07-27 10:07:50 +0100521 EasyMock.expect(mMockBuildInfo.getTestTag()).andStubReturn("");
Brett Chabot44556372014-03-19 19:07:15 -0700522 mMockDevice.startLogcat();
Brett Chabotb1052c22011-01-12 19:44:23 -0800523 mMockPreparer.setUp(mMockDevice, mMockBuildInfo);
524
Michael Rosenfeld55012252017-01-12 15:45:19 -0800525 resumableTest.run((ITestInvocationListener) EasyMock.anyObject());
Julien Desprez47c287f2016-07-14 12:16:57 +0100526 EasyMock.expectLastCall().andThrow(new DeviceNotAvailableException("ERROR", SERIAL));
Brett Chabotb1052c22011-01-12 19:44:23 -0800527 EasyMock.expect(resumableTest.isResumable()).andReturn(Boolean.TRUE);
Julien Desprez70488e82016-02-05 11:28:34 +0000528 mMockDevice.setRecoveryMode(RecoveryMode.NONE);
529 EasyMock.expectLastCall();
Michael Rosenfeld55012252017-01-12 15:45:19 -0800530 EasyMock.expect(mMockDevice.getLogcat()).andReturn(EMPTY_STREAM_SOURCE).times(3);
531 mMockDevice.clearLogcat();
532 EasyMock.expectLastCall().times(3);
533 EasyMock.expect(mMockLogger.getLog()).andReturn(EMPTY_STREAM_SOURCE);
534 EasyMock.expect(
535 mMockLogSaver.saveLogData(
536 EasyMock.eq(LOGCAT_NAME_SETUP),
537 EasyMock.eq(LogDataType.LOGCAT),
538 (InputStream) EasyMock.anyObject()))
jdesprez3bc2fdc2018-03-13 11:44:10 -0700539 .andReturn(new LogFile(PATH, URL, LogDataType.TEXT));
Michael Rosenfeld55012252017-01-12 15:45:19 -0800540 EasyMock.expect(
541 mMockLogSaver.saveLogData(
542 EasyMock.eq(LOGCAT_NAME_TEST),
543 EasyMock.eq(LogDataType.LOGCAT),
544 (InputStream) EasyMock.anyObject()))
jdesprez3bc2fdc2018-03-13 11:44:10 -0700545 .andReturn(new LogFile(PATH, URL, LogDataType.TEXT));
Michael Rosenfeld55012252017-01-12 15:45:19 -0800546 EasyMock.expect(
547 mMockLogSaver.saveLogData(
548 EasyMock.eq(LOGCAT_NAME_TEARDOWN),
549 EasyMock.eq(LogDataType.LOGCAT),
550 (InputStream) EasyMock.anyObject()))
jdesprez3bc2fdc2018-03-13 11:44:10 -0700551 .andReturn(new LogFile(PATH, URL, LogDataType.TEXT));
Michael Rosenfeld55012252017-01-12 15:45:19 -0800552 EasyMock.expect(
553 mMockLogSaver.saveLogData(
554 EasyMock.eq(TestInvocation.TRADEFED_LOG_NAME),
555 EasyMock.eq(LogDataType.TEXT),
556 (InputStream) EasyMock.anyObject()))
jdesprez3bc2fdc2018-03-13 11:44:10 -0700557 .andReturn(new LogFile(PATH, URL, LogDataType.TEXT));
Michael Rosenfeld55012252017-01-12 15:45:19 -0800558 resumeListener.testLog(
559 EasyMock.eq(LOGCAT_NAME_SETUP),
560 EasyMock.eq(LogDataType.LOGCAT),
561 (InputStreamSource) EasyMock.anyObject());
562 resumeListener.testLog(
563 EasyMock.eq(LOGCAT_NAME_TEST),
564 EasyMock.eq(LogDataType.LOGCAT),
565 (InputStreamSource) EasyMock.anyObject());
566 resumeListener.testLog(
567 EasyMock.eq(LOGCAT_NAME_TEARDOWN),
568 EasyMock.eq(LogDataType.LOGCAT),
569 (InputStreamSource) EasyMock.anyObject());
Brett Chabotb1052c22011-01-12 19:44:23 -0800570 resumeListener.testLog(EasyMock.eq(TestInvocation.TRADEFED_LOG_NAME),
Omari Stephens41978ee2011-02-23 19:03:05 -0800571 EasyMock.eq(LogDataType.TEXT), (InputStreamSource)EasyMock.anyObject());
Brett Chabotb1052c22011-01-12 19:44:23 -0800572
573 // just return same build and logger for simplicity
574 EasyMock.expect(mMockBuildInfo.clone()).andReturn(mMockBuildInfo);
575 EasyMock.expect(mMockLogger.clone()).andReturn(mMockLogger);
576 IRescheduler mockRescheduler = EasyMock.createMock(IRescheduler.class);
577 Capture<IConfiguration> capturedConfig = new Capture<IConfiguration>();
578 EasyMock.expect(mockRescheduler.scheduleConfig(EasyMock.capture(capturedConfig)))
579 .andReturn(Boolean.TRUE);
jdesprez58e413e2018-03-09 10:30:17 -0800580 // When resuming the original build provider is still going to handle the clean up.
Brett Chabotb1052c22011-01-12 19:44:23 -0800581 mMockBuildProvider.cleanUp(mMockBuildInfo);
jdesprez58e413e2018-03-09 10:30:17 -0800582 EasyMock.expectLastCall().times(4);
Guang Zhu0a1c63d2015-01-02 11:03:38 -0800583 mMockDevice.clearLastConnectedWifiNetwork();
Brett Chabot44556372014-03-19 19:07:15 -0700584 mMockDevice.stopLogcat();
Brett Chabotb1052c22011-01-12 19:44:23 -0800585
Brett Chabot35d50c62011-03-07 10:39:23 -0800586 mMockLogger.init();
Julien Desprez53353e62016-08-12 15:24:33 +0100587 mMockLogSaver.invocationStarted(mStubInvocationMetadata);
Brett Chabotb1052c22011-01-12 19:44:23 -0800588 // now set resumed invocation expectations
Moon Kim80553c52014-09-15 13:45:40 -0700589 mMockDevice.clearLastConnectedWifiNetwork();
Brett Chabotc8b709f2011-09-02 09:04:22 -0700590 mMockDevice.setOptions((TestDeviceOptions)EasyMock.anyObject());
Brett Chaboteba1fd62011-09-13 17:17:45 -0700591 mMockBuildInfo.setDeviceSerial(SERIAL);
Julien Desprezf7f32c92016-07-27 10:07:50 +0100592 mMockBuildInfo.setTestTag(EasyMock.eq("stub"));
593 EasyMock.expectLastCall().times(2);
Brett Chabot44556372014-03-19 19:07:15 -0700594 mMockDevice.startLogcat();
Brett Chabotb1052c22011-01-12 19:44:23 -0800595 mMockPreparer.setUp(mMockDevice, mMockBuildInfo);
Julien Desprez53353e62016-08-12 15:24:33 +0100596 mMockLogSaver.invocationStarted(mStubInvocationMetadata);
Brett Chabotb1052c22011-01-12 19:44:23 -0800597 mMockDevice.setRecovery(mMockRecovery);
Brett Chabotad123582011-02-09 18:26:40 -0800598 resumableTest.run((ITestInvocationListener)EasyMock.anyObject());
Michael Rosenfeld55012252017-01-12 15:45:19 -0800599 EasyMock.expect(mMockDevice.getLogcat()).andReturn(EMPTY_STREAM_SOURCE).times(3);
600 mMockDevice.clearLogcat();
601 EasyMock.expectLastCall().times(3);
602 EasyMock.expect(mMockLogger.getLog()).andReturn(EMPTY_STREAM_SOURCE);
603 EasyMock.expect(
604 mMockLogSaver.saveLogData(
605 EasyMock.eq(LOGCAT_NAME_SETUP),
606 EasyMock.eq(LogDataType.LOGCAT),
607 (InputStream) EasyMock.anyObject()))
jdesprez3bc2fdc2018-03-13 11:44:10 -0700608 .andReturn(new LogFile(PATH, URL, LogDataType.TEXT));
Michael Rosenfeld55012252017-01-12 15:45:19 -0800609 EasyMock.expect(
610 mMockLogSaver.saveLogData(
611 EasyMock.eq(LOGCAT_NAME_TEST),
612 EasyMock.eq(LogDataType.LOGCAT),
613 (InputStream) EasyMock.anyObject()))
jdesprez3bc2fdc2018-03-13 11:44:10 -0700614 .andReturn(new LogFile(PATH, URL, LogDataType.TEXT));
Michael Rosenfeld55012252017-01-12 15:45:19 -0800615 EasyMock.expect(
616 mMockLogSaver.saveLogData(
617 EasyMock.eq(LOGCAT_NAME_TEARDOWN),
618 EasyMock.eq(LogDataType.LOGCAT),
619 (InputStream) EasyMock.anyObject()))
jdesprez3bc2fdc2018-03-13 11:44:10 -0700620 .andReturn(new LogFile(PATH, URL, LogDataType.TEXT));
Michael Rosenfeld55012252017-01-12 15:45:19 -0800621 EasyMock.expect(
622 mMockLogSaver.saveLogData(
623 EasyMock.eq(TestInvocation.TRADEFED_LOG_NAME),
624 EasyMock.eq(LogDataType.TEXT),
625 (InputStream) EasyMock.anyObject()))
jdesprez3bc2fdc2018-03-13 11:44:10 -0700626 .andReturn(new LogFile(PATH, URL, LogDataType.TEXT));
Michael Rosenfeld55012252017-01-12 15:45:19 -0800627 resumeListener.testLog(
628 EasyMock.eq(LOGCAT_NAME_SETUP),
629 EasyMock.eq(LogDataType.LOGCAT),
630 (InputStreamSource) EasyMock.anyObject());
631 resumeListener.testLog(
632 EasyMock.eq(LOGCAT_NAME_TEST),
633 EasyMock.eq(LogDataType.LOGCAT),
634 (InputStreamSource) EasyMock.anyObject());
635 resumeListener.testLog(
636 EasyMock.eq(LOGCAT_NAME_TEARDOWN),
637 EasyMock.eq(LogDataType.LOGCAT),
638 (InputStreamSource) EasyMock.anyObject());
639 resumeListener.testLog(
640 EasyMock.eq(TestInvocation.TRADEFED_LOG_NAME),
641 EasyMock.eq(LogDataType.TEXT),
642 (InputStreamSource) EasyMock.anyObject());
Brett Chabotb1052c22011-01-12 19:44:23 -0800643 resumeListener.invocationEnded(EasyMock.anyLong());
Eric Rowe0b445112013-07-12 09:29:47 -0700644 mMockLogSaver.invocationEnded(EasyMock.anyLong());
Brett Chabotb1052c22011-01-12 19:44:23 -0800645 EasyMock.expect(resumeListener.getSummary()).andReturn(null);
Brett Chabotb1052c22011-01-12 19:44:23 -0800646 mMockLogger.closeLog();
jdesprez3dff70b2017-04-18 10:39:13 -0700647 EasyMock.expectLastCall().times(3);
Guang Zhu0a1c63d2015-01-02 11:03:38 -0800648 mMockDevice.clearLastConnectedWifiNetwork();
Brett Chabot44556372014-03-19 19:07:15 -0700649 mMockDevice.stopLogcat();
Brett Chabotb1052c22011-01-12 19:44:23 -0800650 EasyMock.replay(mockRescheduler, resumeListener, resumableTest, mMockPreparer,
Eric Rowe0b445112013-07-12 09:29:47 -0700651 mMockBuildProvider, mMockLogger, mMockLogSaver, mMockDevice, mMockBuildInfo);
Brett Chabotb1052c22011-01-12 19:44:23 -0800652
653 try {
Julien Desprez47c287f2016-07-14 12:16:57 +0100654 mTestInvocation.invoke(mStubInvocationMetadata, mStubConfiguration, mockRescheduler);
Brett Chabotb1052c22011-01-12 19:44:23 -0800655 fail("DeviceNotAvailableException not thrown");
656 } catch (DeviceNotAvailableException e) {
657 // expect
658 }
659 // now call again, and expect invocation to be resumed properly
Julien Desprez47c287f2016-07-14 12:16:57 +0100660 mTestInvocation.invoke(mStubInvocationMetadata, capturedConfig.getValue(), mockRescheduler);
Brett Chabotb1052c22011-01-12 19:44:23 -0800661
Eric Rowe0b445112013-07-12 09:29:47 -0700662 EasyMock.verify(mockRescheduler, resumeListener, resumableTest, mMockPreparer,
663 mMockBuildProvider, mMockLogger, mMockLogSaver, mMockDevice, mMockBuildInfo);
Brett Chabotb1052c22011-01-12 19:44:23 -0800664 }
665
666 /**
Eric Rowea3b85852012-07-16 15:07:49 -0700667 * Test the invoke scenario for a {@link IRetriableTest}.
668 *
669 * @throws Exception if unexpected error occurs
670 */
Brett Chabot87803df2013-03-01 16:01:50 -0800671 public void testInvoke_retry() throws Throwable {
Eric Rowea3b85852012-07-16 15:07:49 -0700672 AssertionError exception = new AssertionError();
673 IRetriableTest test = EasyMock.createMock(IRetriableTest.class);
674 test.run((ITestInvocationListener)EasyMock.anyObject());
675 EasyMock.expectLastCall().andThrow(exception);
676 EasyMock.expect(test.isRetriable()).andReturn(Boolean.TRUE);
677 mStubConfiguration.getCommandOptions().setLoopMode(false);
678 IRescheduler mockRescheduler = EasyMock.createMock(IRescheduler.class);
679 EasyMock.expect(mockRescheduler.rescheduleCommand()).andReturn(EasyMock.anyBoolean());
Eric Rowea3b85852012-07-16 15:07:49 -0700680 mMockBuildProvider.buildNotTested(mMockBuildInfo);
Eric Rowe0b445112013-07-12 09:29:47 -0700681 setupMockFailureListeners(exception);
Eric Rowea3b85852012-07-16 15:07:49 -0700682 setupNormalInvoke(test);
683 EasyMock.replay(mockRescheduler);
Julien Desprez47c287f2016-07-14 12:16:57 +0100684 mTestInvocation.invoke(mStubInvocationMetadata, mStubConfiguration, mockRescheduler);
Eric Rowe0b445112013-07-12 09:29:47 -0700685 verifyMocks(test, mockRescheduler);
Eric Rowea3b85852012-07-16 15:07:49 -0700686 verifySummaryListener();
687 }
688
689 /**
Julien Desprez47c287f2016-07-14 12:16:57 +0100690 * Test the {@link TestInvocation#invoke(IInvocationContext, IConfiguration, IRescheduler,
Julien Desprez2040ac02016-03-21 18:05:30 +0000691 * ITestInvocationListener[])} scenario
Eric Rowe0b445112013-07-12 09:29:47 -0700692 * when a {@link ITargetCleaner} is part of the config.
Brett Chabot87803df2013-03-01 16:01:50 -0800693 */
694 public void testInvoke_tearDown() throws Throwable {
695 IRemoteTest test = EasyMock.createNiceMock(IRemoteTest.class);
696 ITargetCleaner mockCleaner = EasyMock.createMock(ITargetCleaner.class);
jdesprez5ae744c2017-10-27 10:36:53 -0700697 EasyMock.expect(mockCleaner.isDisabled()).andReturn(false).times(2);
mikehoran458b2b12018-02-28 16:07:13 -0800698 EasyMock.expect(mockCleaner.isTearDownDisabled()).andReturn(false);
Brett Chabot87803df2013-03-01 16:01:50 -0800699 mockCleaner.setUp(mMockDevice, mMockBuildInfo);
700 mockCleaner.tearDown(mMockDevice, mMockBuildInfo, null);
701 mStubConfiguration.getTargetPreparers().add(mockCleaner);
702 setupMockSuccessListeners();
703 setupNormalInvoke(test);
Julien Desprez2040ac02016-03-21 18:05:30 +0000704 EasyMock.replay(mockCleaner, mockRescheduler);
Julien Desprez47c287f2016-07-14 12:16:57 +0100705 mTestInvocation.invoke(mStubInvocationMetadata, mStubConfiguration, mockRescheduler);
Julien Desprez2040ac02016-03-21 18:05:30 +0000706 verifyMocks(mockCleaner, mockRescheduler);
Brett Chabot87803df2013-03-01 16:01:50 -0800707 verifySummaryListener();
708 }
709
710 /**
Julien Desprez47c287f2016-07-14 12:16:57 +0100711 * Test the {@link TestInvocation#invoke(IInvocationContext, IConfiguration, IRescheduler,
Julien Desprez2040ac02016-03-21 18:05:30 +0000712 * ITestInvocationListener[])} scenario
Eric Rowe0b445112013-07-12 09:29:47 -0700713 * when a {@link ITargetCleaner} is part of the config, and the test throws a
Brett Chabot87803df2013-03-01 16:01:50 -0800714 * {@link DeviceNotAvailableException}.
715 */
716 public void testInvoke_tearDown_deviceNotAvail() throws Throwable {
Julien Desprez47c287f2016-07-14 12:16:57 +0100717 DeviceNotAvailableException exception = new DeviceNotAvailableException("ERROR", SERIAL);
Brett Chabot87803df2013-03-01 16:01:50 -0800718 IRemoteTest test = EasyMock.createMock(IRemoteTest.class);
719 test.run((ITestInvocationListener)EasyMock.anyObject());
720 EasyMock.expectLastCall().andThrow(exception);
721 ITargetCleaner mockCleaner = EasyMock.createMock(ITargetCleaner.class);
jdesprez5ae744c2017-10-27 10:36:53 -0700722 EasyMock.expect(mockCleaner.isDisabled()).andReturn(false).times(2);
mikehoran458b2b12018-02-28 16:07:13 -0800723 EasyMock.expect(mockCleaner.isTearDownDisabled()).andReturn(false);
Brett Chabot87803df2013-03-01 16:01:50 -0800724 mockCleaner.setUp(mMockDevice, mMockBuildInfo);
Guang Zhu36c56a22014-12-30 19:56:00 -0800725 EasyMock.expectLastCall();
726 mockCleaner.tearDown(mMockDevice, mMockBuildInfo, exception);
727 EasyMock.expectLastCall();
Julien Desprez70488e82016-02-05 11:28:34 +0000728 mMockDevice.setRecoveryMode(RecoveryMode.NONE);
729 EasyMock.expectLastCall();
Julien Desprez2040ac02016-03-21 18:05:30 +0000730 EasyMock.replay(mockCleaner, mockRescheduler);
Brett Chabot87803df2013-03-01 16:01:50 -0800731 mStubConfiguration.getTargetPreparers().add(mockCleaner);
732 setupMockFailureListeners(exception);
733 mMockBuildProvider.buildNotTested(mMockBuildInfo);
734 setupNormalInvoke(test);
Brett Chabot87803df2013-03-01 16:01:50 -0800735 try {
Julien Desprez47c287f2016-07-14 12:16:57 +0100736 mTestInvocation.invoke(mStubInvocationMetadata, mStubConfiguration, mockRescheduler);
Brett Chabot87803df2013-03-01 16:01:50 -0800737 fail("DeviceNotAvailableException not thrown");
738 } catch (DeviceNotAvailableException e) {
739 // expected
740 }
Julien Desprez2040ac02016-03-21 18:05:30 +0000741 verifyMocks(mockCleaner, mockRescheduler);
Brett Chabot87803df2013-03-01 16:01:50 -0800742 verifySummaryListener();
743 }
744
745 /**
Julien Desprez47c287f2016-07-14 12:16:57 +0100746 * Test the {@link TestInvocation#invoke(IInvocationContext, IConfiguration, IRescheduler,
Julien Desprez2040ac02016-03-21 18:05:30 +0000747 * ITestInvocationListener[])} scenario
Eric Rowe0b445112013-07-12 09:29:47 -0700748 * when a {@link ITargetCleaner} is part of the config, and the test throws a
Brett Chabot87803df2013-03-01 16:01:50 -0800749 * {@link RuntimeException}.
750 */
751 public void testInvoke_tearDown_runtime() throws Throwable {
752 RuntimeException exception = new RuntimeException();
753 IRemoteTest test = EasyMock.createMock(IRemoteTest.class);
754 test.run((ITestInvocationListener)EasyMock.anyObject());
755 EasyMock.expectLastCall().andThrow(exception);
756 ITargetCleaner mockCleaner = EasyMock.createMock(ITargetCleaner.class);
jdesprez5ae744c2017-10-27 10:36:53 -0700757 EasyMock.expect(mockCleaner.isDisabled()).andReturn(false).times(2);
mikehoran458b2b12018-02-28 16:07:13 -0800758 EasyMock.expect(mockCleaner.isTearDownDisabled()).andReturn(false);
Brett Chabot87803df2013-03-01 16:01:50 -0800759 mockCleaner.setUp(mMockDevice, mMockBuildInfo);
760 // tearDown should be called
761 mockCleaner.tearDown(mMockDevice, mMockBuildInfo, exception);
762 mStubConfiguration.getTargetPreparers().add(mockCleaner);
763 setupMockFailureListeners(exception);
764 mMockBuildProvider.buildNotTested(mMockBuildInfo);
765 setupNormalInvoke(test);
Julien Desprez2040ac02016-03-21 18:05:30 +0000766 EasyMock.replay(mockCleaner, mockRescheduler);
Brett Chabot87803df2013-03-01 16:01:50 -0800767 try {
Julien Desprez47c287f2016-07-14 12:16:57 +0100768 mTestInvocation.invoke(mStubInvocationMetadata, mStubConfiguration, mockRescheduler);
Brett Chabot87803df2013-03-01 16:01:50 -0800769 fail("RuntimeException not thrown");
770 } catch (RuntimeException e) {
771 // expected
772 }
Julien Desprez2040ac02016-03-21 18:05:30 +0000773 verifyMocks(mockCleaner, mockRescheduler);
Brett Chabot87803df2013-03-01 16:01:50 -0800774 verifySummaryListener();
775 }
776
777 /**
Julien Desprez47c287f2016-07-14 12:16:57 +0100778 * Test the {@link TestInvocation#invoke(IInvocationContext, IConfiguration, IRescheduler,
Julien Desprez2040ac02016-03-21 18:05:30 +0000779 * ITestInvocationListener[])} scenario
Eric Rowe0b445112013-07-12 09:29:47 -0700780 * when there is {@link ITestInvocationListener} which implements the {@link ILogSaverListener}
781 * interface.
782 */
783 public void testInvoke_logFileSaved() throws Throwable {
784 List<ITestInvocationListener> listenerList =
785 mStubConfiguration.getTestInvocationListeners();
786 ILogSaverListener logSaverListener = EasyMock.createMock(ILogSaverListener.class);
787 listenerList.add(logSaverListener);
788 mStubConfiguration.setTestInvocationListeners(listenerList);
789
790 logSaverListener.setLogSaver(mMockLogSaver);
Julien Desprez53353e62016-08-12 15:24:33 +0100791 logSaverListener.invocationStarted(mStubInvocationMetadata);
Michael Rosenfeld55012252017-01-12 15:45:19 -0800792 logSaverListener.testLog(
793 EasyMock.eq(LOGCAT_NAME_SETUP),
794 EasyMock.eq(LogDataType.LOGCAT),
795 (InputStreamSource) EasyMock.anyObject());
796 logSaverListener.testLogSaved(
797 EasyMock.eq(LOGCAT_NAME_SETUP),
798 EasyMock.eq(LogDataType.LOGCAT),
799 (InputStreamSource) EasyMock.anyObject(),
800 (LogFile) EasyMock.anyObject());
jdesprezd55b47c2018-03-19 09:50:38 -0700801 logSaverListener.logAssociation(EasyMock.eq(LOGCAT_NAME_SETUP), EasyMock.anyObject());
Michael Rosenfeld55012252017-01-12 15:45:19 -0800802 logSaverListener.testLog(
803 EasyMock.eq(LOGCAT_NAME_TEST),
804 EasyMock.eq(LogDataType.LOGCAT),
805 (InputStreamSource) EasyMock.anyObject());
806 logSaverListener.testLogSaved(
807 EasyMock.eq(LOGCAT_NAME_TEST),
808 EasyMock.eq(LogDataType.LOGCAT),
809 (InputStreamSource) EasyMock.anyObject(),
810 (LogFile) EasyMock.anyObject());
jdesprezd55b47c2018-03-19 09:50:38 -0700811 logSaverListener.logAssociation(EasyMock.eq(LOGCAT_NAME_TEST), EasyMock.anyObject());
Michael Rosenfeld55012252017-01-12 15:45:19 -0800812 logSaverListener.testLog(
813 EasyMock.eq(LOGCAT_NAME_TEARDOWN),
814 EasyMock.eq(LogDataType.LOGCAT),
815 (InputStreamSource) EasyMock.anyObject());
816 logSaverListener.testLogSaved(
817 EasyMock.eq(LOGCAT_NAME_TEARDOWN),
818 EasyMock.eq(LogDataType.LOGCAT),
819 (InputStreamSource) EasyMock.anyObject(),
820 (LogFile) EasyMock.anyObject());
jdesprezd55b47c2018-03-19 09:50:38 -0700821 logSaverListener.logAssociation(EasyMock.eq(LOGCAT_NAME_TEARDOWN), EasyMock.anyObject());
Michael Rosenfeld55012252017-01-12 15:45:19 -0800822 logSaverListener.testLog(
823 EasyMock.eq(TestInvocation.TRADEFED_LOG_NAME),
824 EasyMock.eq(LogDataType.TEXT),
825 (InputStreamSource) EasyMock.anyObject());
826 logSaverListener.testLogSaved(
827 EasyMock.eq(TestInvocation.TRADEFED_LOG_NAME),
828 EasyMock.eq(LogDataType.TEXT),
829 (InputStreamSource) EasyMock.anyObject(),
830 (LogFile) EasyMock.anyObject());
jdesprezd55b47c2018-03-19 09:50:38 -0700831 logSaverListener.logAssociation(
832 EasyMock.eq(TestInvocation.TRADEFED_LOG_NAME), EasyMock.anyObject());
Eric Rowe0b445112013-07-12 09:29:47 -0700833 logSaverListener.invocationEnded(EasyMock.anyLong());
834 EasyMock.expect(logSaverListener.getSummary()).andReturn(mSummary);
835
836 IRemoteTest test = EasyMock.createMock(IRemoteTest.class);
837 setupMockSuccessListeners();
838 test.run((ITestInvocationListener)EasyMock.anyObject());
839 setupNormalInvoke(test);
Julien Desprez2040ac02016-03-21 18:05:30 +0000840 EasyMock.replay(logSaverListener, mockRescheduler);
Julien Desprez47c287f2016-07-14 12:16:57 +0100841 mTestInvocation.invoke(mStubInvocationMetadata, mStubConfiguration, mockRescheduler);
Julien Desprez2040ac02016-03-21 18:05:30 +0000842 verifyMocks(test, logSaverListener, mockRescheduler);
Eric Rowe0b445112013-07-12 09:29:47 -0700843 assertEquals(2, mUriCapture.getValue().size());
844 }
845
846 /**
Julien Desprez47c287f2016-07-14 12:16:57 +0100847 * Test the {@link TestInvocation#invoke(IInvocationContext, IConfiguration, IRescheduler,
848 * ITestInvocationListener[])}
Moon Kimb78076b2016-07-06 15:12:07 -0700849 * scenario with {@link IStrictShardableTest} when a shard index is given.
850 */
851 public void testInvoke_strictShardableTest_withShardIndex() throws Throwable {
jdesprezbc580f92017-06-02 11:41:40 -0700852 mTestInvocation =
853 new TestInvocation() {
854 @Override
855 ILogRegistry getLogRegistry() {
856 return mMockLogRegistry;
857 }
858
859 @Override
jdesprez481a83f2018-03-14 16:20:39 -0700860 public IInvocationExecution createInvocationExec(boolean isSandboxed) {
jdesprez85129dd2017-11-13 06:05:57 -0800861 return new InvocationExecution() {
862 @Override
863 protected IShardHelper createShardHelper() {
864 return new StrictShardHelper();
865 }
866 };
jdesprezbc580f92017-06-02 11:41:40 -0700867 }
868
869 @Override
870 protected void setExitCode(ExitCode code, Throwable stack) {
871 // empty on purpose
872 }
jdesprez7509fca2018-04-02 16:36:56 -0700873
874 @Override
875 InvocationScope getInvocationScope() {
876 // Avoid re-entry in the current TF invocation scope for unit tests.
877 return new InvocationScope();
878 }
jdesprezbc580f92017-06-02 11:41:40 -0700879 };
Moon Kimb78076b2016-07-06 15:12:07 -0700880 String[] commandLine = {"config", "arg"};
881 int shardCount = 10;
882 int shardIndex = 5;
883 IStrictShardableTest test = EasyMock.createMock(IStrictShardableTest.class);
884 IRemoteTest testShard = EasyMock.createMock(IRemoteTest.class);
885 mStubConfiguration.setTest(test);
886 mStubConfiguration.setCommandLine(commandLine);
887 mStubConfiguration.getCommandOptions().setShardCount(shardCount);
888 mStubConfiguration.getCommandOptions().setShardIndex(shardIndex);
889
890 setupInvokeWithBuild();
891 setupMockSuccessListeners();
892 EasyMock.expect(mMockBuildProvider.getBuild()).andReturn(mMockBuildInfo);
893 mMockBuildInfo.addBuildAttribute("command_line_args", "config arg");
894 mMockBuildInfo.addBuildAttribute("shard_count", "10");
895 mMockBuildInfo.addBuildAttribute("shard_index", "5");
896 EasyMock.expect(test.getTestShard(shardCount, shardIndex)).andReturn(testShard);
Julien Desprez8427fc22016-05-31 15:56:01 +0100897 testShard.run((ITestInvocationListener)EasyMock.anyObject());
Moon Kimb78076b2016-07-06 15:12:07 -0700898 mMockPreparer.setUp(mMockDevice, mMockBuildInfo);
899 replayMocks(test, testShard);
900
Julien Desprez47c287f2016-07-14 12:16:57 +0100901 mTestInvocation.invoke(mStubInvocationMetadata, mStubConfiguration, mockRescheduler);
Moon Kimb78076b2016-07-06 15:12:07 -0700902
903 verifyMocks(test, testShard);
904 }
905
906 /**
Julien Desprez47c287f2016-07-14 12:16:57 +0100907 * Test the {@link TestInvocation#invoke(IInvocationContext, IConfiguration, IRescheduler,
908 * ITestInvocationListener[])}
Moon Kim7bc070b2016-07-14 12:29:42 -0700909 * scenario with non-{@link IStrictShardableTest} when shard index 0 is given.
910 */
911 public void testInvoke_nonStrictShardableTest_withShardIndexZero() throws Throwable {
912 String[] commandLine = {"config", "arg"};
913 int shardCount = 10;
914 int shardIndex = 0;
915 IRemoteTest test = EasyMock.createMock(IRemoteTest.class);
916 mStubConfiguration.setTest(test);
917 mStubConfiguration.setCommandLine(commandLine);
918 mStubConfiguration.getCommandOptions().setShardCount(shardCount);
919 mStubConfiguration.getCommandOptions().setShardIndex(shardIndex);
920
921 setupInvokeWithBuild();
922 setupMockSuccessListeners();
923 EasyMock.expect(mMockBuildProvider.getBuild()).andReturn(mMockBuildInfo);
924 mMockBuildInfo.addBuildAttribute("command_line_args", "config arg");
925 mMockBuildInfo.addBuildAttribute("shard_count", "10");
926 mMockBuildInfo.addBuildAttribute("shard_index", "0");
927 test.run((ITestInvocationListener)EasyMock.anyObject());
928 mMockPreparer.setUp(mMockDevice, mMockBuildInfo);
929 replayMocks(test);
930
Julien Desprez47c287f2016-07-14 12:16:57 +0100931 mTestInvocation.invoke(mStubInvocationMetadata, mStubConfiguration, mockRescheduler);
Moon Kim7bc070b2016-07-14 12:29:42 -0700932
933 verifyMocks(test);
934 }
935
936 /**
Julien Desprez47c287f2016-07-14 12:16:57 +0100937 * Test the {@link TestInvocation#invoke(IInvocationContext, IConfiguration, IRescheduler,
938 * ITestInvocationListener[])}
Moon Kim7bc070b2016-07-14 12:29:42 -0700939 * scenario with non-{@link IStrictShardableTest} when a shard index non-0 is given.
940 */
941 public void testInvoke_nonStrictShardableTest_withShardIndexNonZero() throws Throwable {
jdesprezbc580f92017-06-02 11:41:40 -0700942 mTestInvocation =
943 new TestInvocation() {
944 @Override
945 ILogRegistry getLogRegistry() {
946 return mMockLogRegistry;
947 }
948
949 @Override
jdesprez481a83f2018-03-14 16:20:39 -0700950 public IInvocationExecution createInvocationExec(boolean isSandboxed) {
jdesprez85129dd2017-11-13 06:05:57 -0800951 return new InvocationExecution() {
952 @Override
953 protected IShardHelper createShardHelper() {
954 return new StrictShardHelper();
955 }
956 };
jdesprezbc580f92017-06-02 11:41:40 -0700957 }
958
959 @Override
960 protected void setExitCode(ExitCode code, Throwable stack) {
961 // empty on purpose
962 }
jdesprez7509fca2018-04-02 16:36:56 -0700963
964 @Override
965 InvocationScope getInvocationScope() {
966 // Avoid re-entry in the current TF invocation scope for unit tests.
967 return new InvocationScope();
968 }
jdesprezbc580f92017-06-02 11:41:40 -0700969 };
Moon Kim7bc070b2016-07-14 12:29:42 -0700970 String[] commandLine = {"config", "arg"};
971 int shardCount = 10;
972 int shardIndex = 1;
Julien Desprez47c287f2016-07-14 12:16:57 +0100973 IStrictShardableTest shardableTest = EasyMock.createMock(IStrictShardableTest.class);
Moon Kim7bc070b2016-07-14 12:29:42 -0700974 IRemoteTest test = EasyMock.createMock(IRemoteTest.class);
Julien Desprez47c287f2016-07-14 12:16:57 +0100975 EasyMock.expect(shardableTest.getTestShard(10, 1)).andReturn(test);
976 test.run((ITestInvocationListener)EasyMock.anyObject());
977 EasyMock.expectLastCall();
978 mStubConfiguration.setTest(shardableTest);
Moon Kim7bc070b2016-07-14 12:29:42 -0700979 mStubConfiguration.setCommandLine(commandLine);
980 mStubConfiguration.getCommandOptions().setShardCount(shardCount);
981 mStubConfiguration.getCommandOptions().setShardIndex(shardIndex);
982
983 setupInvokeWithBuild();
984 setupMockSuccessListeners();
985 EasyMock.expect(mMockBuildProvider.getBuild()).andReturn(mMockBuildInfo);
986 mMockBuildInfo.addBuildAttribute("command_line_args", "config arg");
987 mMockBuildInfo.addBuildAttribute("shard_count", "10");
988 mMockBuildInfo.addBuildAttribute("shard_index", "1");
989 mMockPreparer.setUp(mMockDevice, mMockBuildInfo);
Julien Desprez47c287f2016-07-14 12:16:57 +0100990 replayMocks(shardableTest, test);
Moon Kim7bc070b2016-07-14 12:29:42 -0700991
Julien Desprez47c287f2016-07-14 12:16:57 +0100992 mTestInvocation.invoke(mStubInvocationMetadata, mStubConfiguration, mockRescheduler);
Moon Kim7bc070b2016-07-14 12:29:42 -0700993
Julien Desprez47c287f2016-07-14 12:16:57 +0100994 verifyMocks(shardableTest, test);
Moon Kim7bc070b2016-07-14 12:29:42 -0700995 }
996
997 /**
Julien Desprezf7f32c92016-07-27 10:07:50 +0100998 * Test the test-tag is set when the IBuildInfo's test-tag is not.
999 */
1000 public void testInvoke_testtag() throws Throwable {
1001 String[] commandLine = {"run", "empty"};
1002 mStubConfiguration.setCommandLine(commandLine);
1003 mStubConfiguration.getCommandOptions().setTestTag("not-default");
1004
1005 setupInvoke();
Michael Rosenfeld55012252017-01-12 15:45:19 -08001006 EasyMock.expect(mMockDevice.getLogcat()).andReturn(EMPTY_STREAM_SOURCE).times(3);
1007 mMockDevice.clearLogcat();
1008 EasyMock.expectLastCall().times(3);
1009 EasyMock.expect(mMockLogger.getLog()).andReturn(EMPTY_STREAM_SOURCE);
Julien Desprezf7f32c92016-07-27 10:07:50 +01001010 mMockBuildInfo.setDeviceSerial(SERIAL);
1011 mMockBuildProvider.cleanUp(mMockBuildInfo);
jdespreza90f9722017-08-11 16:48:24 -07001012 EasyMock.expectLastCall().times(2);
Julien Desprezf7f32c92016-07-27 10:07:50 +01001013 setupMockSuccessListeners();
1014 EasyMock.expect(mMockBuildProvider.getBuild()).andReturn(mMockBuildInfo);
1015 mMockBuildInfo.addBuildAttribute("command_line_args", "run empty");
1016 mMockPreparer.setUp(mMockDevice, mMockBuildInfo);
1017 // Default build is "stub" so we set the test-tag
1018 mMockBuildInfo.setTestTag(EasyMock.eq("not-default"));
1019 EasyMock.expectLastCall();
1020 EasyMock.expect(mMockBuildInfo.getTestTag()).andStubReturn("stub");
jdesprez3dff70b2017-04-18 10:39:13 -07001021 mMockLogRegistry.unregisterLogger();
1022 mMockLogRegistry.dumpToGlobalLog(mMockLogger);
1023 mMockLogger.closeLog();
Julien Desprezf7f32c92016-07-27 10:07:50 +01001024 replayMocks();
Julien Desprez53353e62016-08-12 15:24:33 +01001025 mTestInvocation.invoke(mStubInvocationMetadata, mStubConfiguration, mockRescheduler);
Julien Desprezf7f32c92016-07-27 10:07:50 +01001026 verifyMocks();
1027 }
1028
1029 /**
1030 * Test the test-tag of the IBuildInfo is not modified when the CommandOption default test-tag
1031 * is not modified.
1032 */
1033 public void testInvoke_testtag_notset() throws Throwable {
1034 String[] commandLine = {"run", "empty"};
1035 mStubConfiguration.setCommandLine(commandLine);
1036 setupInvoke();
Michael Rosenfeld55012252017-01-12 15:45:19 -08001037 EasyMock.expect(mMockDevice.getLogcat()).andReturn(EMPTY_STREAM_SOURCE).times(3);
1038 mMockDevice.clearLogcat();
1039 EasyMock.expectLastCall().times(3);
1040 EasyMock.expect(mMockLogger.getLog()).andReturn(EMPTY_STREAM_SOURCE);
Julien Desprezf7f32c92016-07-27 10:07:50 +01001041 mMockBuildInfo.setDeviceSerial(SERIAL);
1042 mMockBuildProvider.cleanUp(mMockBuildInfo);
jdespreza90f9722017-08-11 16:48:24 -07001043 EasyMock.expectLastCall().times(2);
Julien Desprezf7f32c92016-07-27 10:07:50 +01001044 setupMockSuccessListeners();
1045 EasyMock.expect(mMockBuildProvider.getBuild()).andReturn(mMockBuildInfo);
1046 mMockBuildInfo.addBuildAttribute("command_line_args", "run empty");
1047 mMockPreparer.setUp(mMockDevice, mMockBuildInfo);
1048 EasyMock.expect(mMockBuildInfo.getTestTag()).andStubReturn("buildprovidertesttag");
jdesprez3dff70b2017-04-18 10:39:13 -07001049 mMockLogRegistry.dumpToGlobalLog(mMockLogger);
1050 mMockLogRegistry.unregisterLogger();
1051 mMockLogger.closeLog();
Julien Desprezf7f32c92016-07-27 10:07:50 +01001052 replayMocks();
Julien Desprez53353e62016-08-12 15:24:33 +01001053 mTestInvocation.invoke(mStubInvocationMetadata, mStubConfiguration, mockRescheduler);
Julien Desprezf7f32c92016-07-27 10:07:50 +01001054 verifyMocks();
1055 }
1056
1057 /**
1058 * Test the test-tag of the IBuildInfo is not set and Command Option is not set either.
1059 * A default 'stub' test-tag is set to ensure reporting is done.
1060 */
1061 public void testInvoke_notesttag() throws Throwable {
1062 String[] commandLine = {"run", "empty"};
1063 mStubConfiguration.setCommandLine(commandLine);
1064 setupInvoke();
Michael Rosenfeld55012252017-01-12 15:45:19 -08001065 EasyMock.expect(mMockDevice.getLogcat()).andReturn(EMPTY_STREAM_SOURCE).times(3);
1066 mMockDevice.clearLogcat();
1067 EasyMock.expectLastCall().times(3);
1068 EasyMock.expect(mMockLogger.getLog()).andReturn(EMPTY_STREAM_SOURCE);
Julien Desprezf7f32c92016-07-27 10:07:50 +01001069 mMockBuildInfo.setDeviceSerial(SERIAL);
1070 mMockBuildProvider.cleanUp(mMockBuildInfo);
jdespreza90f9722017-08-11 16:48:24 -07001071 EasyMock.expectLastCall().times(2);
Julien Desprezf7f32c92016-07-27 10:07:50 +01001072 setupMockSuccessListeners();
1073 EasyMock.expect(mMockBuildProvider.getBuild()).andReturn(mMockBuildInfo);
1074 mMockBuildInfo.addBuildAttribute("command_line_args", "run empty");
1075 mMockPreparer.setUp(mMockDevice, mMockBuildInfo);
1076 EasyMock.expect(mMockBuildInfo.getTestTag()).andStubReturn(null);
1077 mMockBuildInfo.setTestTag(EasyMock.eq("stub"));
1078 EasyMock.expectLastCall();
jdesprez3dff70b2017-04-18 10:39:13 -07001079 mMockLogRegistry.dumpToGlobalLog(mMockLogger);
1080 mMockLogRegistry.unregisterLogger();
1081 mMockLogger.closeLog();
Julien Desprezf7f32c92016-07-27 10:07:50 +01001082 replayMocks();
Julien Desprez53353e62016-08-12 15:24:33 +01001083 mTestInvocation.invoke(mStubInvocationMetadata, mStubConfiguration, mockRescheduler);
Julien Desprezf7f32c92016-07-27 10:07:50 +01001084 verifyMocks();
1085 }
Julien Desprezaa9ea852016-11-25 10:56:35 +00001086
1087 /**
1088 * Helper tests class to expose all the interfaces needed for the tests.
1089 */
1090 private interface IFakeBuildProvider extends IDeviceBuildProvider, IInvocationContextReceiver {
1091 }
1092
1093 /**
1094 * Test the injection of test-tag from TestInvocation to the build provider via the
1095 * {@link IInvocationContextReceiver}.
1096 */
1097 public void testInvoke_buildProviderNeedTestTag() throws Throwable {
1098 final String testTag = "THISISTHETAG";
1099 String[] commandLine = {"run", "empty"};
1100 mStubConfiguration.setCommandLine(commandLine);
1101 ICommandOptions commandOption = new CommandOptions();
1102 commandOption.setTestTag(testTag);
1103 IFakeBuildProvider mockProvider = EasyMock.createMock(IFakeBuildProvider.class);
1104 mStubConfiguration.setBuildProvider(mockProvider);
1105 mStubConfiguration.setCommandOptions(commandOption);
1106 setupInvoke();
Michael Rosenfeld55012252017-01-12 15:45:19 -08001107 EasyMock.expect(mMockDevice.getLogcat()).andReturn(EMPTY_STREAM_SOURCE).times(3);
1108 mMockDevice.clearLogcat();
1109 EasyMock.expectLastCall().times(3);
1110 EasyMock.expect(mMockLogger.getLog()).andReturn(EMPTY_STREAM_SOURCE);
Julien Desprezaa9ea852016-11-25 10:56:35 +00001111 mMockBuildInfo.setDeviceSerial(SERIAL);
1112 setupMockSuccessListeners();
1113 mMockBuildInfo.addBuildAttribute("command_line_args", "run empty");
1114 mMockPreparer.setUp(mMockDevice, mMockBuildInfo);
1115 EasyMock.expect(mMockBuildInfo.getTestTag()).andStubReturn(null);
1116 // Validate proper tag is set on the build.
1117 mMockBuildInfo.setTestTag(EasyMock.eq(testTag));
1118 mockProvider.setInvocationContext((IInvocationContext)EasyMock.anyObject());
1119 EasyMock.expect(mockProvider.getBuild(mMockDevice)).andReturn(mMockBuildInfo);
1120 mockProvider.cleanUp(mMockBuildInfo);
jdespreza90f9722017-08-11 16:48:24 -07001121 EasyMock.expectLastCall().times(2);
jdesprez3dff70b2017-04-18 10:39:13 -07001122 mMockLogRegistry.dumpToGlobalLog(mMockLogger);
1123 mMockLogRegistry.unregisterLogger();
1124 mMockLogger.closeLog();
1125
Julien Desprezaa9ea852016-11-25 10:56:35 +00001126 replayMocks(mockProvider);
1127 mTestInvocation.invoke(mStubInvocationMetadata, mStubConfiguration, mockRescheduler);
1128 verifyMocks(mockProvider);
1129 }
1130
Julien Desprezf7f32c92016-07-27 10:07:50 +01001131 /**
Moon Kimb78076b2016-07-06 15:12:07 -07001132 * Set up expected conditions for normal run up to the part where tests are run.
1133 *
Brett Chabot74121d82010-01-28 20:14:27 -08001134 * @param test the {@link Test} to use.
1135 */
Brett Chabot87803df2013-03-01 16:01:50 -08001136 private void setupNormalInvoke(IRemoteTest test) throws Throwable {
Brett Chabot44556372014-03-19 19:07:15 -07001137 setupInvokeWithBuild();
Brett Chabotb1052c22011-01-12 19:44:23 -08001138 mStubConfiguration.setTest(test);
Julien Desprez47c287f2016-07-14 12:16:57 +01001139 mStubMultiConfiguration.setTest(test);
Brett Chabotfe88a152010-08-02 17:52:59 -07001140 EasyMock.expect(mMockBuildProvider.getBuild()).andReturn(mMockBuildInfo);
Brett Chabotc8b709f2011-09-02 09:04:22 -07001141
Brett Chabot74121d82010-01-28 20:14:27 -08001142 mMockPreparer.setUp(mMockDevice, mMockBuildInfo);
Brett Chabot44556372014-03-19 19:07:15 -07001143 replayMocks(test);
1144 }
1145
1146 /**
1147 * Set up expected calls that occur on every invoke, regardless of result
1148 */
1149 private void setupInvoke() {
Moon Kim80553c52014-09-15 13:45:40 -07001150 mMockDevice.clearLastConnectedWifiNetwork();
Brett Chabot44556372014-03-19 19:07:15 -07001151 mMockDevice.setOptions((TestDeviceOptions)EasyMock.anyObject());
1152 mMockDevice.startLogcat();
Guang Zhu0a1c63d2015-01-02 11:03:38 -08001153 mMockDevice.clearLastConnectedWifiNetwork();
Brett Chabot44556372014-03-19 19:07:15 -07001154 mMockDevice.stopLogcat();
1155 }
1156
1157 /**
1158 * Set up expected calls that occur on every invoke that gets a valid build
1159 */
1160 private void setupInvokeWithBuild() {
1161 setupInvoke();
Michael Rosenfeld55012252017-01-12 15:45:19 -08001162 EasyMock.expect(mMockDevice.getLogcat()).andReturn(EMPTY_STREAM_SOURCE).times(3);
1163 mMockDevice.clearLogcat();
1164 EasyMock.expectLastCall().times(3);
Omari Stephens203296c2010-09-02 18:20:45 -07001165
Michael Rosenfeld55012252017-01-12 15:45:19 -08001166 EasyMock.expect(mMockLogger.getLog()).andReturn(EMPTY_STREAM_SOURCE);
Brett Chabot44556372014-03-19 19:07:15 -07001167 mMockBuildInfo.setDeviceSerial(SERIAL);
Brett Chabot66d87572010-11-01 19:54:59 -07001168 mMockBuildProvider.cleanUp(mMockBuildInfo);
jdespreza90f9722017-08-11 16:48:24 -07001169 EasyMock.expectLastCall().anyTimes();
Julien Desprezf7f32c92016-07-27 10:07:50 +01001170 mMockBuildInfo.setTestTag(EasyMock.eq("stub"));
1171 EasyMock.expectLastCall();
1172 EasyMock.expect(mMockBuildInfo.getTestTag()).andStubReturn("");
jdesprez3dff70b2017-04-18 10:39:13 -07001173
1174 mMockLogRegistry.unregisterLogger();
1175 mMockLogRegistry.dumpToGlobalLog(mMockLogger);
1176 mMockLogger.closeLog();
Brett Chabot057e16e2010-06-27 21:53:48 -07001177 }
1178
1179 /**
Omari Stephens203296c2010-09-02 18:20:45 -07001180 * Set up expected conditions for the test InvocationListener and SummaryListener
Michael Rosenfeld55012252017-01-12 15:45:19 -08001181 *
1182 * <p>The order of calls for a single listener should be:
1183 *
Omari Stephens203296c2010-09-02 18:20:45 -07001184 * <ol>
Michael Rosenfeld55012252017-01-12 15:45:19 -08001185 * <li>invocationStarted
1186 * <li>testLog(LOGCAT_NAME_SETUP, ...) (if no build or retrieval error)
1187 * <li>invocationFailed (if run failed)
1188 * <li>testLog(LOGCAT_NAME_ERROR, ...) (if build retrieval error)
1189 * <li>testLog(LOGCAT_NAME_TEST, ...) (otherwise)
1190 * <li>testLog(build error bugreport, ...) (otherwise and if build error)
1191 * <li>testLog(LOGCAT_NAME_TEARDOWN, ...) (otherwise)
1192 * <li>testLog(TRADEFED_LOG_NAME, ...)
1193 * <li>putSummary (for an ITestSummaryListener)
1194 * <li>invocationEnded
1195 * <li>getSummary (for an ITestInvocationListener)
Omari Stephens203296c2010-09-02 18:20:45 -07001196 * </ol>
Michael Rosenfeld55012252017-01-12 15:45:19 -08001197 *
Omari Stephens203296c2010-09-02 18:20:45 -07001198 * However note that, across all listeners, any getSummary call will precede all putSummary
1199 * calls.
1200 */
Eric Rowe0b445112013-07-12 09:29:47 -07001201 private void setupMockListeners(InvocationStatus status, Throwable throwable)
1202 throws IOException {
Omari Stephens203296c2010-09-02 18:20:45 -07001203 // invocationStarted
Julien Desprez53353e62016-08-12 15:24:33 +01001204 mMockLogSaver.invocationStarted(mStubInvocationMetadata);
1205 mMockTestListener.invocationStarted(mStubInvocationMetadata);
1206 mMockSummaryListener.invocationStarted(mStubInvocationMetadata);
Omari Stephens203296c2010-09-02 18:20:45 -07001207
Michael Rosenfeldd7d700b2017-10-18 10:09:13 -07001208 if (!(throwable instanceof BuildRetrievalError)) {
Michael Rosenfeld55012252017-01-12 15:45:19 -08001209 EasyMock.expect(
1210 mMockLogSaver.saveLogData(
1211 EasyMock.eq(LOGCAT_NAME_SETUP),
1212 EasyMock.eq(LogDataType.LOGCAT),
1213 (InputStream) EasyMock.anyObject()))
jdesprez3bc2fdc2018-03-13 11:44:10 -07001214 .andReturn(new LogFile(PATH, URL, LogDataType.TEXT));
Michael Rosenfeld55012252017-01-12 15:45:19 -08001215 mMockTestListener.testLog(
1216 EasyMock.eq(LOGCAT_NAME_SETUP),
1217 EasyMock.eq(LogDataType.LOGCAT),
1218 (InputStreamSource) EasyMock.anyObject());
1219 mMockSummaryListener.testLog(
1220 EasyMock.eq(LOGCAT_NAME_SETUP),
1221 EasyMock.eq(LogDataType.LOGCAT),
1222 (InputStreamSource) EasyMock.anyObject());
1223 }
1224
Guang Zhu36c56a22014-12-30 19:56:00 -08001225 // invocationFailed
1226 if (!status.equals(InvocationStatus.SUCCESS)) {
1227 mMockTestListener.invocationFailed(EasyMock.eq(throwable));
1228 mMockSummaryListener.invocationFailed(EasyMock.eq(throwable));
1229 }
1230
Michael Rosenfeld55012252017-01-12 15:45:19 -08001231 if (throwable instanceof BuildRetrievalError) {
1232 // Handle logcat error listeners
1233 EasyMock.expect(
1234 mMockLogSaver.saveLogData(
1235 EasyMock.eq(LOGCAT_NAME_ERROR),
1236 EasyMock.eq(LogDataType.LOGCAT),
1237 (InputStream) EasyMock.anyObject()))
jdesprez3bc2fdc2018-03-13 11:44:10 -07001238 .andReturn(new LogFile(PATH, URL, LogDataType.TEXT));
Julien Desprez47c287f2016-07-14 12:16:57 +01001239 mMockTestListener.testLog(
Michael Rosenfeld55012252017-01-12 15:45:19 -08001240 EasyMock.eq(LOGCAT_NAME_ERROR),
1241 EasyMock.eq(LogDataType.LOGCAT),
1242 (InputStreamSource) EasyMock.anyObject());
Julien Desprez47c287f2016-07-14 12:16:57 +01001243 mMockSummaryListener.testLog(
Michael Rosenfeld55012252017-01-12 15:45:19 -08001244 EasyMock.eq(LOGCAT_NAME_ERROR),
1245 EasyMock.eq(LogDataType.LOGCAT),
1246 (InputStreamSource) EasyMock.anyObject());
1247 } else {
1248 // Handle test logcat listeners
1249 EasyMock.expect(
1250 mMockLogSaver.saveLogData(
1251 EasyMock.eq(LOGCAT_NAME_TEST),
1252 EasyMock.eq(LogDataType.LOGCAT),
1253 (InputStream) EasyMock.anyObject()))
jdesprez3bc2fdc2018-03-13 11:44:10 -07001254 .andReturn(new LogFile(PATH, URL, LogDataType.TEXT));
Michael Rosenfeld55012252017-01-12 15:45:19 -08001255 mMockTestListener.testLog(
1256 EasyMock.eq(LOGCAT_NAME_TEST),
1257 EasyMock.eq(LogDataType.LOGCAT),
1258 (InputStreamSource) EasyMock.anyObject());
1259 mMockSummaryListener.testLog(
1260 EasyMock.eq(LOGCAT_NAME_TEST),
1261 EasyMock.eq(LogDataType.LOGCAT),
1262 (InputStreamSource) EasyMock.anyObject());
1263 // Handle build error bugreport listeners
1264 if (throwable instanceof BuildError) {
1265 EasyMock.expect(
jdesprezc3537f72017-09-05 17:27:14 -07001266 mMockDevice.logBugreport(
Michael Rosenfeld55012252017-01-12 15:45:19 -08001267 EasyMock.eq(
1268 TestInvocation.BUILD_ERROR_BUGREPORT_NAME
1269 + "_"
1270 + SERIAL),
jdesprezc3537f72017-09-05 17:27:14 -07001271 EasyMock.anyObject()))
1272 .andReturn(true);
Michael Rosenfeld55012252017-01-12 15:45:19 -08001273 }
1274 // Handle teardown logcat listeners
1275 EasyMock.expect(
1276 mMockLogSaver.saveLogData(
1277 EasyMock.eq(LOGCAT_NAME_TEARDOWN),
1278 EasyMock.eq(LogDataType.LOGCAT),
1279 (InputStream) EasyMock.anyObject()))
jdesprez3bc2fdc2018-03-13 11:44:10 -07001280 .andReturn(new LogFile(PATH, URL, LogDataType.TEXT));
Michael Rosenfeld55012252017-01-12 15:45:19 -08001281 mMockTestListener.testLog(
1282 EasyMock.eq(LOGCAT_NAME_TEARDOWN),
1283 EasyMock.eq(LogDataType.LOGCAT),
1284 (InputStreamSource) EasyMock.anyObject());
1285 mMockSummaryListener.testLog(
1286 EasyMock.eq(LOGCAT_NAME_TEARDOWN),
1287 EasyMock.eq(LogDataType.LOGCAT),
1288 (InputStreamSource) EasyMock.anyObject());
Eric Rowe4a3369d2011-11-23 12:12:16 -08001289 }
1290
Michael Rosenfeld55012252017-01-12 15:45:19 -08001291 EasyMock.expect(
1292 mMockLogSaver.saveLogData(
1293 EasyMock.eq(TestInvocation.TRADEFED_LOG_NAME),
1294 EasyMock.eq(LogDataType.TEXT),
1295 (InputStream) EasyMock.anyObject()))
jdesprez3bc2fdc2018-03-13 11:44:10 -07001296 .andReturn(new LogFile(PATH, URL, LogDataType.TEXT));
Omari Stephens203296c2010-09-02 18:20:45 -07001297 mMockTestListener.testLog(EasyMock.eq(TestInvocation.TRADEFED_LOG_NAME),
Omari Stephens41978ee2011-02-23 19:03:05 -08001298 EasyMock.eq(LogDataType.TEXT), (InputStreamSource)EasyMock.anyObject());
Omari Stephens203296c2010-09-02 18:20:45 -07001299 mMockSummaryListener.testLog(EasyMock.eq(TestInvocation.TRADEFED_LOG_NAME),
Omari Stephens41978ee2011-02-23 19:03:05 -08001300 EasyMock.eq(LogDataType.TEXT), (InputStreamSource)EasyMock.anyObject());
Omari Stephens203296c2010-09-02 18:20:45 -07001301
1302 // invocationEnded, getSummary (mMockTestListener)
1303 mMockTestListener.invocationEnded(EasyMock.anyLong());
Omari Stephensa01abfe2010-09-07 15:48:44 -07001304 EasyMock.expect(mMockTestListener.getSummary()).andReturn(mSummary);
Omari Stephens203296c2010-09-02 18:20:45 -07001305
1306 // putSummary, invocationEnded (mMockSummaryListener)
1307 mMockSummaryListener.putSummary(EasyMock.capture(mUriCapture));
1308 mMockSummaryListener.invocationEnded(EasyMock.anyLong());
Eric Rowe0b445112013-07-12 09:29:47 -07001309 mMockLogSaver.invocationEnded(EasyMock.anyLong());
Omari Stephens203296c2010-09-02 18:20:45 -07001310 }
1311
Julien Desprez2763ca02016-12-06 14:39:33 +00001312 /**
1313 * Test the {@link TestInvocation#invoke(IInvocationContext, IConfiguration, IRescheduler,
1314 * ITestInvocationListener[])} scenario with {@link IShardableTest}.
1315 */
1316 public void testInvoke_shardableTest_legacy() throws Throwable {
jdesprez8ba33462017-04-05 16:51:13 -07001317 String command = "empty --test-tag t";
1318 String[] commandLine = {"empty", "--test-tag", "t"};
Julien Desprez2763ca02016-12-06 14:39:33 +00001319 int shardCount = 2;
1320 IShardableTest test = EasyMock.createMock(IShardableTest.class);
1321 List<IRemoteTest> shards = new ArrayList<>();
1322 IRemoteTest shard1 = EasyMock.createMock(IRemoteTest.class);
1323 IRemoteTest shard2 = EasyMock.createMock(IRemoteTest.class);
1324 shards.add(shard1);
1325 shards.add(shard2);
1326 EasyMock.expect(test.split()).andReturn(shards);
1327 mStubConfiguration.setTest(test);
1328 mStubConfiguration.setCommandLine(commandLine);
jdespreza90f9722017-08-11 16:48:24 -07001329 mMockBuildProvider.cleanUp(mMockBuildInfo);
Julien Desprez2763ca02016-12-06 14:39:33 +00001330 setupInvoke();
jdesprez8ba33462017-04-05 16:51:13 -07001331 setupNShardInvocation(shardCount, command);
jdesprez3dff70b2017-04-18 10:39:13 -07001332 mMockLogRegistry.dumpToGlobalLog(mMockLogger);
Julien Desprez2763ca02016-12-06 14:39:33 +00001333 replayMocks(test, mockRescheduler, shard1, shard2);
1334 mTestInvocation.invoke(mStubInvocationMetadata, mStubConfiguration, mockRescheduler);
1335 verifyMocks(test, mockRescheduler, shard1, shard2);
1336 }
1337
jdesprezbc580f92017-06-02 11:41:40 -07001338 /**
1339 * Test that {@link TestInvocation#logDeviceBatteryLevel(IInvocationContext, String)} is not
1340 * adding battery information for placeholder device.
1341 */
1342 public void testLogDeviceBatteryLevel_placeholderDevice() {
1343 final String fakeEvent = "event";
1344 IInvocationContext context = new InvocationContext();
1345 ITestDevice device1 = EasyMock.createMock(ITestDevice.class);
1346 EasyMock.expect(device1.getIDevice()).andReturn(new StubDevice("serial1"));
1347 context.addAllocatedDevice("device1", device1);
1348 EasyMock.replay(device1);
1349 mTestInvocation.logDeviceBatteryLevel(context, fakeEvent);
1350 EasyMock.verify(device1);
1351 assertEquals(0, context.getAttributes().size());
1352 }
1353
1354 /**
1355 * Test that {@link TestInvocation#logDeviceBatteryLevel(IInvocationContext, String)} is adding
1356 * battery information for physical real device.
1357 */
1358 public void testLogDeviceBatteryLevel_physicalDevice() {
1359 final String fakeEvent = "event";
1360 IInvocationContext context = new InvocationContext();
1361 ITestDevice device1 = EasyMock.createMock(ITestDevice.class);
1362 IDevice idevice = Mockito.mock(IDevice.class);
1363 EasyMock.expect(device1.getIDevice()).andReturn(idevice);
1364 SettableFuture<Integer> future = SettableFuture.create();
1365 future.set(50);
1366 doReturn(future).when(idevice).getBattery(Mockito.anyLong(), Mockito.any());
1367 EasyMock.expect(device1.getSerialNumber()).andReturn("serial1");
1368 context.addAllocatedDevice("device1", device1);
jdesprezcf545552017-07-12 02:08:08 -07001369 context.addDeviceBuildInfo("device1", new BuildInfo());
jdesprezbc580f92017-06-02 11:41:40 -07001370 EasyMock.replay(device1);
1371 mTestInvocation.logDeviceBatteryLevel(context, fakeEvent);
1372 EasyMock.verify(device1);
jdesprezcf545552017-07-12 02:08:08 -07001373 assertEquals(1, context.getBuildInfo("device1").getBuildAttributes().size());
1374 assertEquals(
1375 "50",
1376 context.getBuildInfo("device1")
1377 .getBuildAttributes()
1378 .get("serial1-battery-" + fakeEvent));
jdesprezbc580f92017-06-02 11:41:40 -07001379 }
1380
1381 /**
1382 * Test that {@link TestInvocation#logDeviceBatteryLevel(IInvocationContext, String)} is adding
1383 * battery information for multiple physical real device.
1384 */
1385 public void testLogDeviceBatteryLevel_physicalDevice_multi() {
1386 final String fakeEvent = "event";
1387 IInvocationContext context = new InvocationContext();
1388 ITestDevice device1 = EasyMock.createMock(ITestDevice.class);
1389 IDevice idevice = Mockito.mock(IDevice.class);
1390 EasyMock.expect(device1.getSerialNumber()).andReturn("serial1");
1391 EasyMock.expect(device1.getIDevice()).andReturn(idevice);
1392 SettableFuture<Integer> future = SettableFuture.create();
1393 future.set(50);
1394 doReturn(future).when(idevice).getBattery(Mockito.anyLong(), Mockito.any());
1395 ITestDevice device2 = EasyMock.createMock(ITestDevice.class);
1396 EasyMock.expect(device2.getIDevice()).andReturn(idevice);
1397 EasyMock.expect(device2.getSerialNumber()).andReturn("serial2");
1398 context.addAllocatedDevice("device1", device1);
jdesprezcf545552017-07-12 02:08:08 -07001399 context.addDeviceBuildInfo("device1", new BuildInfo());
jdesprezbc580f92017-06-02 11:41:40 -07001400 context.addAllocatedDevice("device2", device2);
jdesprezcf545552017-07-12 02:08:08 -07001401 context.addDeviceBuildInfo("device2", new BuildInfo());
jdesprezbc580f92017-06-02 11:41:40 -07001402 EasyMock.replay(device1, device2);
1403 mTestInvocation.logDeviceBatteryLevel(context, fakeEvent);
1404 EasyMock.verify(device1, device2);
jdesprezcf545552017-07-12 02:08:08 -07001405 assertEquals(1, context.getBuildInfo("device1").getBuildAttributes().size());
1406 assertEquals(1, context.getBuildInfo("device2").getBuildAttributes().size());
1407 assertEquals(
1408 "50",
1409 context.getBuildInfo("device1")
1410 .getBuildAttributes()
1411 .get("serial1-battery-" + fakeEvent));
1412 assertEquals(
1413 "50",
1414 context.getBuildInfo("device2")
1415 .getBuildAttributes()
1416 .get("serial2-battery-" + fakeEvent));
jdesprezbc580f92017-06-02 11:41:40 -07001417 }
1418
1419 /**
1420 * Test that {@link TestInvocation#logDeviceBatteryLevel(IInvocationContext, String)} is adding
1421 * battery information for multiple physical real device, and ignore stub device if any.
1422 */
1423 public void testLogDeviceBatteryLevel_physicalDevice_stub_multi() {
1424 final String fakeEvent = "event";
1425 IInvocationContext context = new InvocationContext();
1426 ITestDevice device1 = EasyMock.createMock(ITestDevice.class);
1427 IDevice idevice = Mockito.mock(IDevice.class);
1428 EasyMock.expect(device1.getSerialNumber()).andReturn("serial1");
1429 EasyMock.expect(device1.getIDevice()).andReturn(idevice);
1430 SettableFuture<Integer> future = SettableFuture.create();
1431 future.set(50);
1432 doReturn(future).when(idevice).getBattery(Mockito.anyLong(), Mockito.any());
1433 ITestDevice device2 = EasyMock.createMock(ITestDevice.class);
1434 EasyMock.expect(device2.getIDevice()).andReturn(idevice);
1435 EasyMock.expect(device2.getSerialNumber()).andReturn("serial2");
1436 ITestDevice device3 = EasyMock.createMock(ITestDevice.class);
1437 EasyMock.expect(device1.getIDevice()).andStubReturn(new StubDevice("stub1"));
1438 ITestDevice device4 = EasyMock.createMock(ITestDevice.class);
1439 EasyMock.expect(device1.getIDevice()).andStubReturn(new StubDevice("stub2"));
1440 context.addAllocatedDevice("device1", device1);
jdesprezcf545552017-07-12 02:08:08 -07001441 context.addDeviceBuildInfo("device1", new BuildInfo());
jdesprezbc580f92017-06-02 11:41:40 -07001442 context.addAllocatedDevice("device2", device2);
jdesprezcf545552017-07-12 02:08:08 -07001443 context.addDeviceBuildInfo("device2", new BuildInfo());
jdesprezbc580f92017-06-02 11:41:40 -07001444 context.addAllocatedDevice("device3", device3);
1445 context.addAllocatedDevice("device4", device4);
1446 EasyMock.replay(device1, device2);
1447 mTestInvocation.logDeviceBatteryLevel(context, fakeEvent);
1448 EasyMock.verify(device1, device2);
jdesprezcf545552017-07-12 02:08:08 -07001449 assertEquals(1, context.getBuildInfo("device1").getBuildAttributes().size());
1450 assertEquals(1, context.getBuildInfo("device2").getBuildAttributes().size());
1451 assertEquals(
1452 "50",
1453 context.getBuildInfo("device1")
1454 .getBuildAttributes()
1455 .get("serial1-battery-" + fakeEvent));
1456 assertEquals(
1457 "50",
1458 context.getBuildInfo("device2")
1459 .getBuildAttributes()
1460 .get("serial2-battery-" + fakeEvent));
jdesprezbc580f92017-06-02 11:41:40 -07001461 }
1462
jdesprez8ba33462017-04-05 16:51:13 -07001463 /** Helper to set the expectation for N number of shards. */
1464 private void setupNShardInvocation(int numShard, String commandLine) throws Exception {
Julien Desprez2763ca02016-12-06 14:39:33 +00001465 mMockBuildInfo.setTestTag(EasyMock.eq("stub"));
1466 EasyMock.expectLastCall();
1467 EasyMock.expect(mMockBuildProvider.getBuild()).andReturn(mMockBuildInfo);
1468 EasyMock.expect(mMockBuildInfo.getTestTag()).andStubReturn("");
jdesprez8ba33462017-04-05 16:51:13 -07001469 mMockBuildInfo.addBuildAttribute("command_line_args", commandLine);
Julien Desprez2763ca02016-12-06 14:39:33 +00001470 mMockTestListener.invocationStarted((IInvocationContext)EasyMock.anyObject());
1471 EasyMock.expectLastCall();
1472 mMockSummaryListener.invocationStarted((IInvocationContext)EasyMock.anyObject());
1473 EasyMock.expectLastCall();
1474 EasyMock.expect(mMockLogger.clone()).andReturn(mMockLogger).times(numShard);
1475 EasyMock.expect(mMockBuildInfo.clone()).andReturn(mMockBuildInfo).times(numShard);
1476 EasyMock.expect(mockRescheduler.scheduleConfig(EasyMock.anyObject()))
1477 .andReturn(true).times(numShard);
1478 mMockBuildInfo.setDeviceSerial(SERIAL);
1479 EasyMock.expectLastCall();
1480 mMockBuildProvider.cleanUp(EasyMock.anyObject());
1481 EasyMock.expectLastCall();
1482 }
1483
Eric Rowe0b445112013-07-12 09:29:47 -07001484 private void setupMockSuccessListeners() throws IOException {
Omari Stephens203296c2010-09-02 18:20:45 -07001485 setupMockListeners(InvocationStatus.SUCCESS, null);
1486 }
1487
Eric Rowe0b445112013-07-12 09:29:47 -07001488 private void setupMockFailureListeners(Throwable throwable) throws IOException {
Eric Rowea3b85852012-07-16 15:07:49 -07001489 setupMockListeners(InvocationStatus.FAILED, throwable);
Omari Stephens203296c2010-09-02 18:20:45 -07001490 }
1491
1492 private void verifySummaryListener() {
1493 // Check that we captured the expected uris List
Omari Stephensa01abfe2010-09-07 15:48:44 -07001494 List<TestSummary> summaries = mUriCapture.getValue();
1495 assertEquals(1, summaries.size());
1496 assertEquals(mSummary, summaries.get(0));
Omari Stephens203296c2010-09-02 18:20:45 -07001497 }
1498
1499 /**
Brett Chabot057e16e2010-06-27 21:53:48 -07001500 * Verify all mock objects received expected calls
1501 */
Brett Chabot87803df2013-03-01 16:01:50 -08001502 private void verifyMocks(Object... mocks) {
Brett Chabot1d35e662011-05-25 19:48:24 -07001503 // note: intentionally exclude configuration from verification - don't care
Brett Chabot057e16e2010-06-27 21:53:48 -07001504 // what methods are called
Brett Chabot87803df2013-03-01 16:01:50 -08001505 EasyMock.verify(mMockTestListener, mMockSummaryListener, mMockPreparer,
Eric Rowe0b445112013-07-12 09:29:47 -07001506 mMockBuildProvider, mMockLogger, mMockBuildInfo, mMockLogRegistry,
1507 mMockLogSaver);
Brett Chabot87803df2013-03-01 16:01:50 -08001508 if (mocks.length > 0) {
1509 EasyMock.verify(mocks);
1510 }
Brett Chabot057e16e2010-06-27 21:53:48 -07001511 }
1512
1513 /**
1514 * Switch all mock objects into replay mode.
1515 */
Brett Chabot87803df2013-03-01 16:01:50 -08001516 private void replayMocks(Object... mocks) {
Eric Rowe0b445112013-07-12 09:29:47 -07001517 EasyMock.replay(mMockTestListener, mMockSummaryListener, mMockPreparer,
1518 mMockBuildProvider, mMockLogger, mMockBuildInfo, mMockLogRegistry,
Moon Kimb78076b2016-07-06 15:12:07 -07001519 mMockLogSaver, mMockDevice, mMockConfigFactory);
Brett Chabot87803df2013-03-01 16:01:50 -08001520 if (mocks.length > 0) {
1521 EasyMock.replay(mocks);
1522 }
Brett Chabot74121d82010-01-28 20:14:27 -08001523 }
1524
1525 /**
Brett Chabot74121d82010-01-28 20:14:27 -08001526 * Interface for testing device config pass through.
1527 */
Brett Chabot2e441bd2010-09-16 18:25:11 -07001528 private interface DeviceConfigTest extends IRemoteTest, IDeviceTest {
Brett Chabot74121d82010-01-28 20:14:27 -08001529
1530 }
jdesprezbc580f92017-06-02 11:41:40 -07001531
1532 /**
1533 * Test {@link INativeDevice#preInvocationSetup(IBuildInfo info)} is called when command option
1534 * skip-pre-device-setup is not set.
1535 */
1536 public void testNotSkipPreDeviceSetup() throws Throwable {
1537 IInvocationContext context = new InvocationContext();
1538 ITestDevice device1 = EasyMock.createMock(ITestDevice.class);
1539 IDevice idevice = Mockito.mock(IDevice.class);
1540 context.addAllocatedDevice("DEFAULT_DEVICE", device1);
1541 EasyMock.expect(device1.getSerialNumber()).andReturn("serial1").anyTimes();
1542 EasyMock.expect(device1.getIDevice()).andReturn(idevice).anyTimes();
1543 EasyMock.expect(device1.getLogcat()).andReturn(EMPTY_STREAM_SOURCE).times(1);
1544 device1.clearLogcat();
1545 EasyMock.expectLastCall().once();
1546 device1.preInvocationSetup((IBuildInfo) EasyMock.anyObject());
1547 EasyMock.expectLastCall().once();
1548
1549 CommandOptions commandOption = new CommandOptions();
1550 OptionSetter setter = new OptionSetter(commandOption);
1551 setter.setOptionValue("skip-pre-device-setup", "false");
1552 mStubConfiguration.setCommandOptions(commandOption);
1553
jdesprez5ae744c2017-10-27 10:36:53 -07001554 EasyMock.expect(mMockPreparer.isDisabled()).andReturn(true);
mikehoran458b2b12018-02-28 16:07:13 -08001555 // Not expect isTearDownDisabled.
jdesprez5ae744c2017-10-27 10:36:53 -07001556
jdesprezbc580f92017-06-02 11:41:40 -07001557 ITestInvocationListener listener = EasyMock.createStrictMock(ITestInvocationListener.class);
1558 listener.testLog(
1559 EasyMock.eq(LOGCAT_NAME_SETUP),
1560 EasyMock.eq(LogDataType.LOGCAT),
1561 (InputStreamSource) EasyMock.anyObject());
1562
jdesprez5ae744c2017-10-27 10:36:53 -07001563 EasyMock.replay(device1, listener, mMockPreparer);
jdesprez94cdfeb2017-09-18 10:49:24 -07001564 new InvocationExecution().doSetup(context, mStubConfiguration, listener);
jdesprez5ae744c2017-10-27 10:36:53 -07001565 EasyMock.verify(device1, listener, mMockPreparer);
jdesprez94cdfeb2017-09-18 10:49:24 -07001566
jdesprezbc580f92017-06-02 11:41:40 -07001567 }
1568
1569 /**
1570 * Test {@link INativeDevice#preInvocationSetup(IBuildInfo info)} is not called when command
1571 * option skip-pre-device-setup is set.
1572 */
1573 public void testSkipPreDeviceSetup() throws Throwable {
1574 IInvocationContext context = new InvocationContext();
1575 ITestDevice device1 = EasyMock.createMock(ITestDevice.class);
1576 IDevice idevice = Mockito.mock(IDevice.class);
1577 context.addAllocatedDevice("DEFAULT_DEVICE", device1);
1578 EasyMock.expect(device1.getSerialNumber()).andReturn("serial1").anyTimes();
1579 EasyMock.expect(device1.getIDevice()).andReturn(idevice).anyTimes();
1580 EasyMock.expect(device1.getLogcat()).andReturn(EMPTY_STREAM_SOURCE).times(1);
1581 device1.clearLogcat();
1582 EasyMock.expectLastCall().once();
1583
1584 CommandOptions commandOption = new CommandOptions();
1585 OptionSetter setter = new OptionSetter(commandOption);
1586 setter.setOptionValue("skip-pre-device-setup", "true");
1587 mStubConfiguration.setCommandOptions(commandOption);
1588
jdesprez5ae744c2017-10-27 10:36:53 -07001589 EasyMock.expect(mMockPreparer.isDisabled()).andReturn(true);
mikehoran458b2b12018-02-28 16:07:13 -08001590 // Not expect isTearDownDisabled
jdesprez5ae744c2017-10-27 10:36:53 -07001591
jdesprezbc580f92017-06-02 11:41:40 -07001592 ITestInvocationListener listener = EasyMock.createStrictMock(ITestInvocationListener.class);
1593 listener.testLog(
1594 EasyMock.eq(LOGCAT_NAME_SETUP),
1595 EasyMock.eq(LogDataType.LOGCAT),
1596 (InputStreamSource) EasyMock.anyObject());
1597
jdesprez5ae744c2017-10-27 10:36:53 -07001598 EasyMock.replay(device1, listener, mMockPreparer);
jdesprez94cdfeb2017-09-18 10:49:24 -07001599 new InvocationExecution().doSetup(context, mStubConfiguration, listener);
jdesprez5ae744c2017-10-27 10:36:53 -07001600 EasyMock.verify(device1, listener, mMockPreparer);
jdesprezbc580f92017-06-02 11:41:40 -07001601 }
jdesprezda087c02017-06-26 14:23:07 -07001602
1603 /**
1604 * Test when a {@link IDeviceBuildInfo} is passing through we do not attempt to add any external
1605 * directories when there is none coming from environment.
1606 */
1607 public void testInvoke_deviceInfoBuild_noEnv() throws Throwable {
jdespreza04c03e2017-12-19 05:53:37 -08001608 mTestInvocation =
1609 new TestInvocation() {
1610 @Override
1611 ILogRegistry getLogRegistry() {
1612 return mMockLogRegistry;
1613 }
1614
1615 @Override
jdesprez481a83f2018-03-14 16:20:39 -07001616 public IInvocationExecution createInvocationExec(boolean isSandboxed) {
jdespreza04c03e2017-12-19 05:53:37 -08001617 return new InvocationExecution() {
1618 @Override
1619 protected IShardHelper createShardHelper() {
1620 return new ShardHelper();
1621 }
1622
1623 @Override
jdesprez8689aa32018-03-15 09:36:13 -07001624 File getExternalTestCasesDirs(EnvVariable envVar) {
jdespreza04c03e2017-12-19 05:53:37 -08001625 // Return empty list to ensure we do not have any environment loaded
jdesprez8689aa32018-03-15 09:36:13 -07001626 return null;
jdespreza04c03e2017-12-19 05:53:37 -08001627 }
1628 };
1629 }
1630
1631 @Override
1632 protected void setExitCode(ExitCode code, Throwable stack) {
1633 // empty on purpose
1634 }
jdesprez7509fca2018-04-02 16:36:56 -07001635
1636 @Override
1637 InvocationScope getInvocationScope() {
1638 // Avoid re-entry in the current TF invocation scope for unit tests.
1639 return new InvocationScope();
1640 }
jdespreza04c03e2017-12-19 05:53:37 -08001641 };
jdesprezda087c02017-06-26 14:23:07 -07001642 mMockBuildInfo = EasyMock.createMock(IDeviceBuildInfo.class);
jdesprez53387682018-02-27 14:10:46 -08001643 EasyMock.expect(mMockBuildInfo.getProperties()).andStubReturn(new HashSet<>());
jdesprezda087c02017-06-26 14:23:07 -07001644 IRemoteTest test = EasyMock.createNiceMock(IRemoteTest.class);
1645 ITargetCleaner mockCleaner = EasyMock.createMock(ITargetCleaner.class);
jdesprez5ae744c2017-10-27 10:36:53 -07001646 EasyMock.expect(mockCleaner.isDisabled()).andReturn(false).times(2);
mikehoran458b2b12018-02-28 16:07:13 -08001647 EasyMock.expect(mockCleaner.isTearDownDisabled()).andReturn(false);
jdesprezda087c02017-06-26 14:23:07 -07001648 mockCleaner.setUp(mMockDevice, mMockBuildInfo);
1649 mockCleaner.tearDown(mMockDevice, mMockBuildInfo, null);
1650 mStubConfiguration.getTargetPreparers().add(mockCleaner);
1651
1652 File tmpTestsDir = FileUtil.createTempDir("invocation-tests-dir");
1653 try {
1654 EasyMock.expect(((IDeviceBuildInfo) mMockBuildInfo).getTestsDir())
1655 .andReturn(tmpTestsDir);
1656 setupMockSuccessListeners();
1657 setupNormalInvoke(test);
1658 EasyMock.replay(mockCleaner, mockRescheduler);
1659 mTestInvocation.invoke(mStubInvocationMetadata, mStubConfiguration, mockRescheduler);
1660 verifyMocks(mockCleaner, mockRescheduler);
1661 verifySummaryListener();
1662 } finally {
1663 FileUtil.recursiveDelete(tmpTestsDir);
1664 }
1665 }
1666
1667 /**
1668 * Test when a {@link IDeviceBuildInfo} is passing through we attempt to add the external
1669 * directories to it when they are available.
1670 */
1671 public void testInvoke_deviceInfoBuild_withEnv() throws Throwable {
1672 File tmpTestsDir = FileUtil.createTempDir("invocation-tests-dir");
1673 File tmpExternalTestsDir = FileUtil.createTempDir("external-tf-dir");
1674 File tmpTestsFile = FileUtil.createTempFile("testsfile", "txt", tmpExternalTestsDir);
1675 try {
1676 mTestInvocation =
1677 new TestInvocation() {
1678 @Override
1679 ILogRegistry getLogRegistry() {
1680 return mMockLogRegistry;
1681 }
1682
1683 @Override
jdesprez481a83f2018-03-14 16:20:39 -07001684 public IInvocationExecution createInvocationExec(boolean isSandboxed) {
jdesprez85129dd2017-11-13 06:05:57 -08001685 return new InvocationExecution() {
1686 @Override
1687 protected IShardHelper createShardHelper() {
1688 return new ShardHelper();
1689 }
jdespreza1731b22017-12-06 06:05:52 -08001690
1691 @Override
jdesprez8689aa32018-03-15 09:36:13 -07001692 File getExternalTestCasesDirs(EnvVariable envVar) {
1693 if (EnvVariable.ANDROID_TARGET_OUT_TESTCASES.equals(envVar)) {
1694 return tmpExternalTestsDir;
1695 }
1696 return null;
jdespreza1731b22017-12-06 06:05:52 -08001697 }
jdesprez85129dd2017-11-13 06:05:57 -08001698 };
jdesprezda087c02017-06-26 14:23:07 -07001699 }
1700
1701 @Override
1702 protected void setExitCode(ExitCode code, Throwable stack) {
1703 // empty on purpose
1704 }
jdesprez7509fca2018-04-02 16:36:56 -07001705
1706 @Override
1707 InvocationScope getInvocationScope() {
1708 // Avoid re-entry in the current TF invocation scope for unit tests.
1709 return new InvocationScope();
1710 }
jdesprezda087c02017-06-26 14:23:07 -07001711 };
1712 mMockBuildInfo = EasyMock.createMock(IDeviceBuildInfo.class);
1713 IRemoteTest test = EasyMock.createNiceMock(IRemoteTest.class);
1714 ITargetCleaner mockCleaner = EasyMock.createMock(ITargetCleaner.class);
jdesprez5ae744c2017-10-27 10:36:53 -07001715 EasyMock.expect(mockCleaner.isDisabled()).andReturn(false).times(2);
mikehoran458b2b12018-02-28 16:07:13 -08001716 EasyMock.expect(mockCleaner.isTearDownDisabled()).andReturn(false);
jdesprezda087c02017-06-26 14:23:07 -07001717 mockCleaner.setUp(mMockDevice, mMockBuildInfo);
1718 mockCleaner.tearDown(mMockDevice, mMockBuildInfo, null);
1719 mStubConfiguration.getTargetPreparers().add(mockCleaner);
1720
jdespreza04c03e2017-12-19 05:53:37 -08001721 mMockBuildInfo.setFile(
jdesprez8689aa32018-03-15 09:36:13 -07001722 EasyMock.contains(ExternalLinkedDir.TARGET_LINKED_DIR.toString()),
jdespreza04c03e2017-12-19 05:53:37 -08001723 EasyMock.anyObject(),
1724 EasyMock.eq("v1"));
jdesprezda087c02017-06-26 14:23:07 -07001725 EasyMock.expect(((IDeviceBuildInfo) mMockBuildInfo).getTestsDir())
1726 .andReturn(tmpTestsDir);
jdesprez53387682018-02-27 14:10:46 -08001727 EasyMock.expect(mMockBuildInfo.getProperties()).andStubReturn(new HashSet<>());
jdesprezda087c02017-06-26 14:23:07 -07001728
1729 setupMockSuccessListeners();
1730 setupNormalInvoke(test);
1731 EasyMock.replay(mockCleaner, mockRescheduler);
1732 mTestInvocation.invoke(mStubInvocationMetadata, mStubConfiguration, mockRescheduler);
1733 verifyMocks(mockCleaner, mockRescheduler);
1734 verifySummaryListener();
1735 // Check that the external directory was copied in the testsDir.
1736 assertTrue(tmpTestsDir.listFiles().length == 1);
jdespreze5093342017-11-16 06:16:06 -08001737 // external-tf-dir - the symlink is the original file name + randomized sequence
1738 assertTrue(
jdesprez8689aa32018-03-15 09:36:13 -07001739 tmpTestsDir
1740 .listFiles()[0]
1741 .getName()
1742 .startsWith(ExternalLinkedDir.TARGET_LINKED_DIR.toString()));
jdesprezda087c02017-06-26 14:23:07 -07001743 // testsfile.txt
1744 assertTrue(tmpTestsDir.listFiles()[0].listFiles().length == 1);
1745 assertEquals(
1746 tmpTestsFile.getName(), tmpTestsDir.listFiles()[0].listFiles()[0].getName());
1747 } finally {
1748 FileUtil.recursiveDelete(tmpTestsDir);
1749 FileUtil.recursiveDelete(tmpExternalTestsDir);
1750 }
1751 }
jdesprez267ab892017-09-08 15:30:06 -07001752
jdesprez53387682018-02-27 14:10:46 -08001753 /**
1754 * Test when a {@link IDeviceBuildInfo} is passing through we do not attempt to add the external
1755 * directories to it, since {@link BuildInfoProperties} is set to skip the linking.
1756 */
1757 public void testInvoke_deviceInfoBuild_withEnv_andSkipProperty() throws Throwable {
1758 File tmpTestsDir = FileUtil.createTempDir("invocation-tests-dir");
1759 File tmpExternalTestsDir = FileUtil.createTempDir("external-tf-dir");
1760 FileUtil.createTempFile("testsfile", "txt", tmpExternalTestsDir);
1761 try {
1762 mTestInvocation =
1763 new TestInvocation() {
1764 @Override
1765 ILogRegistry getLogRegistry() {
1766 return mMockLogRegistry;
1767 }
1768
1769 @Override
jdesprez481a83f2018-03-14 16:20:39 -07001770 public IInvocationExecution createInvocationExec(boolean isSandboxed) {
jdesprez53387682018-02-27 14:10:46 -08001771 return new InvocationExecution() {
1772 @Override
1773 protected IShardHelper createShardHelper() {
1774 return new ShardHelper();
1775 }
1776
1777 @Override
jdesprez8689aa32018-03-15 09:36:13 -07001778 File getExternalTestCasesDirs(EnvVariable envVar) {
1779 return tmpExternalTestsDir;
jdesprez53387682018-02-27 14:10:46 -08001780 }
1781 };
1782 }
1783
1784 @Override
1785 protected void setExitCode(ExitCode code, Throwable stack) {
1786 // empty on purpose
1787 }
jdesprez7509fca2018-04-02 16:36:56 -07001788
1789 @Override
1790 InvocationScope getInvocationScope() {
1791 // Avoid re-entry in the current TF invocation scope for unit tests.
1792 return new InvocationScope();
1793 }
jdesprez53387682018-02-27 14:10:46 -08001794 };
1795 mMockBuildInfo = EasyMock.createMock(IDeviceBuildInfo.class);
1796 IRemoteTest test = EasyMock.createNiceMock(IRemoteTest.class);
1797 ITargetCleaner mockCleaner = EasyMock.createMock(ITargetCleaner.class);
1798 EasyMock.expect(mockCleaner.isDisabled()).andReturn(false).times(2);
mikehoran458b2b12018-02-28 16:07:13 -08001799 EasyMock.expect(mockCleaner.isTearDownDisabled()).andReturn(false);
jdesprez53387682018-02-27 14:10:46 -08001800 mockCleaner.setUp(mMockDevice, mMockBuildInfo);
1801 mockCleaner.tearDown(mMockDevice, mMockBuildInfo, null);
1802 mStubConfiguration.getTargetPreparers().add(mockCleaner);
1803
1804 Set<BuildInfoProperties> prop = new HashSet<>();
1805 prop.add(BuildInfoProperties.DO_NOT_LINK_TESTS_DIR);
1806 EasyMock.expect(mMockBuildInfo.getProperties()).andStubReturn(prop);
1807
1808 setupMockSuccessListeners();
1809 setupNormalInvoke(test);
1810 EasyMock.replay(mockCleaner, mockRescheduler);
1811 mTestInvocation.invoke(mStubInvocationMetadata, mStubConfiguration, mockRescheduler);
1812 verifyMocks(mockCleaner, mockRescheduler);
1813 verifySummaryListener();
1814 // Check that the external directory was NOT copied in the testsDir.
1815 assertTrue(tmpTestsDir.listFiles().length == 0);
1816 } finally {
1817 FileUtil.recursiveDelete(tmpTestsDir);
1818 FileUtil.recursiveDelete(tmpExternalTestsDir);
1819 }
1820 }
1821
jdesprez839825d2018-01-31 12:14:23 -08001822 public static class TestableCollector extends BaseDeviceMetricCollector {
jdesprez267ab892017-09-08 15:30:06 -07001823
jdesprez839825d2018-01-31 12:14:23 -08001824 @Option(name = "name")
jdesprez267ab892017-09-08 15:30:06 -07001825 private String mName;
1826
jdesprez839825d2018-01-31 12:14:23 -08001827 public TestableCollector() {}
1828
jdesprez267ab892017-09-08 15:30:06 -07001829 public TestableCollector(String name) {
1830 mName = name;
1831 }
1832
1833 @Override
jdesprez71540902017-10-16 12:53:36 -07001834 public void onTestRunEnd(
jdesprez39039212018-04-04 16:44:35 -07001835 DeviceMetricData runData, final Map<String, Metric> currentRunMetrics) {
1836 runData.addMetric(
1837 mName,
1838 Metric.newBuilder()
1839 .setMeasurements(
1840 Measurements.newBuilder().setSingleString(mName).build()));
jdesprez267ab892017-09-08 15:30:06 -07001841 }
1842 }
1843
1844 /**
1845 * Test that when {@link IMetricCollector} are used, they wrap and call in sequence the listener
1846 * so all metrics end up on the final receiver.
1847 */
1848 public void testMetricCollectionChain() throws Exception {
1849 IConfiguration configuration = new Configuration("test", "description");
1850 StubTest test = new StubTest();
1851 OptionSetter setter = new OptionSetter(test);
1852 setter.setOptionValue("run-a-test", "true");
1853 configuration.setTest(test);
1854
1855 List<IMetricCollector> collectors = new ArrayList<>();
1856 collectors.add(new TestableCollector("collector1"));
1857 collectors.add(new TestableCollector("collector2"));
1858 collectors.add(new TestableCollector("collector3"));
1859 collectors.add(new TestableCollector("collector4"));
1860 configuration.setDeviceMetricCollectors(collectors);
1861
1862 mMockTestListener.testRunStarted("TestStub", 1);
jdesprez821a0052018-01-16 03:37:16 -08001863 TestDescription testId = new TestDescription("StubTest", "StubMethod");
jdesprez267ab892017-09-08 15:30:06 -07001864 mMockTestListener.testStarted(EasyMock.eq(testId), EasyMock.anyLong());
1865 mMockTestListener.testEnded(
Julien Despreze349ced2018-05-23 09:21:28 -07001866 EasyMock.eq(testId),
1867 EasyMock.anyLong(),
1868 EasyMock.eq(new HashMap<String, Metric>()));
Julien Desprez3ff41822018-05-21 17:59:56 -07001869 Capture<HashMap<String, Metric>> captured = new Capture<>();
jdesprez267ab892017-09-08 15:30:06 -07001870 mMockTestListener.testRunEnded(EasyMock.anyLong(), EasyMock.capture(captured));
1871 EasyMock.replay(mMockTestListener);
jdesprez94cdfeb2017-09-18 10:49:24 -07001872 new InvocationExecution()
1873 .runTests(mStubInvocationMetadata, configuration, mMockTestListener);
jdesprez267ab892017-09-08 15:30:06 -07001874 EasyMock.verify(mMockTestListener);
1875 // The collectors are called in sequence
1876 List<String> listKeys = new ArrayList<>(captured.getValue().keySet());
jdesprez39039212018-04-04 16:44:35 -07001877 assertEquals(4, listKeys.size());
jdesprez267ab892017-09-08 15:30:06 -07001878 assertEquals("collector4", listKeys.get(0));
1879 assertEquals("collector3", listKeys.get(1));
1880 assertEquals("collector2", listKeys.get(2));
1881 assertEquals("collector1", listKeys.get(3));
1882 }
Brett Chabot74121d82010-01-28 20:14:27 -08001883}