blob: 2fc5b2c57304a49e963b5e1d9636bbbcb5cd208f [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
Brett Chabotbeaebb02011-02-07 16:16:06 -080018import com.android.tradefed.build.BuildRetrievalError;
19import com.android.tradefed.build.IBuildInfo;
20import com.android.tradefed.build.IBuildProvider;
Brett Chabota17af5d2010-08-23 13:51:17 -070021import com.android.tradefed.command.FatalHostError;
Brett Chabotb1052c22011-01-12 19:44:23 -080022import com.android.tradefed.config.Configuration;
Brett Chabot7e5f8ac2010-05-17 12:27:56 -070023import com.android.tradefed.config.IConfiguration;
Brett Chabot5e4bc502010-06-10 20:10:30 -070024import com.android.tradefed.device.DeviceNotAvailableException;
Brett Chabotf7ad1562010-07-27 11:58:58 -070025import com.android.tradefed.device.IDeviceRecovery;
Brett Chabot74121d82010-01-28 20:14:27 -080026import com.android.tradefed.device.ITestDevice;
Julien Desprez70488e82016-02-05 11:28:34 +000027import com.android.tradefed.device.ITestDevice.RecoveryMode;
Julien Desprez0750c382016-05-23 10:40:26 +010028import com.android.tradefed.device.TestDeviceOptions;
Brett Chabot7e5f8ac2010-05-17 12:27:56 -070029import com.android.tradefed.log.ILeveledLogOutput;
Brett Chabot1d35e662011-05-25 19:48:24 -070030import com.android.tradefed.log.ILogRegistry;
Omari Stephens41978ee2011-02-23 19:03:05 -080031import com.android.tradefed.result.ByteArrayInputStreamSource;
Eric Rowe0b445112013-07-12 09:29:47 -070032import com.android.tradefed.result.ILogSaver;
33import com.android.tradefed.result.ILogSaverListener;
Brett Chabot74121d82010-01-28 20:14:27 -080034import com.android.tradefed.result.ITestInvocationListener;
Omari Stephens203296c2010-09-02 18:20:45 -070035import com.android.tradefed.result.ITestSummaryListener;
Brett Chabot1d35e662011-05-25 19:48:24 -070036import com.android.tradefed.result.InputStreamSource;
Brett Chabot2e441bd2010-09-16 18:25:11 -070037import com.android.tradefed.result.InvocationStatus;
Brett Chabot7e5f8ac2010-05-17 12:27:56 -070038import com.android.tradefed.result.LogDataType;
Eric Rowe0b445112013-07-12 09:29:47 -070039import com.android.tradefed.result.LogFile;
Omari Stephensa01abfe2010-09-07 15:48:44 -070040import com.android.tradefed.result.TestSummary;
Brett Chabotbeaebb02011-02-07 16:16:06 -080041import com.android.tradefed.targetprep.BuildError;
Brett Chabot87803df2013-03-01 16:01:50 -080042import com.android.tradefed.targetprep.ITargetCleaner;
Brett Chabotbeaebb02011-02-07 16:16:06 -080043import com.android.tradefed.targetprep.ITargetPreparer;
Brett Chabot74121d82010-01-28 20:14:27 -080044import com.android.tradefed.testtype.IDeviceTest;
45import com.android.tradefed.testtype.IRemoteTest;
Brett Chabotb1052c22011-01-12 19:44:23 -080046import com.android.tradefed.testtype.IResumableTest;
Eric Rowea3b85852012-07-16 15:07:49 -070047import com.android.tradefed.testtype.IRetriableTest;
Brett Chabot74121d82010-01-28 20:14:27 -080048
Brett Chabot0eabebe2011-09-12 16:15:42 -070049import junit.framework.Test;
50import junit.framework.TestCase;
51
Omari Stephens203296c2010-09-02 18:20:45 -070052import org.easymock.Capture;
Brett Chabot74121d82010-01-28 20:14:27 -080053import org.easymock.EasyMock;
54
Eric Rowe0b445112013-07-12 09:29:47 -070055import java.io.IOException;
56import java.io.InputStream;
Brett Chabot664b8932010-08-06 17:17:33 -070057import java.util.ArrayList;
Brett Chabotbcb497c2010-08-07 17:07:36 -070058import java.util.Collections;
Brett Chabot664b8932010-08-06 17:17:33 -070059import java.util.List;
Brett Chabotbcb497c2010-08-07 17:07:36 -070060import java.util.Map;
Brett Chabot7e5f8ac2010-05-17 12:27:56 -070061
Brett Chabot74121d82010-01-28 20:14:27 -080062/**
63 * Unit tests for {@link TestInvocation}.
64 */
65public class TestInvocationTest extends TestCase {
66
Brett Chaboteba1fd62011-09-13 17:17:45 -070067 private static final String SERIAL = "serial";
Brett Chabotbcb497c2010-08-07 17:07:36 -070068 private static final Map<String, String> EMPTY_MAP = Collections.emptyMap();
Eric Rowe0b445112013-07-12 09:29:47 -070069 private static final String PATH = "path";
70 private static final String URL = "url";
Omari Stephensa01abfe2010-09-07 15:48:44 -070071 private static final TestSummary mSummary = new TestSummary("http://www.url.com/report.txt");
Brett Chabotbcb497c2010-08-07 17:07:36 -070072
Brett Chabot74121d82010-01-28 20:14:27 -080073 /** The {@link TestInvocation} under test, with all dependencies mocked out */
74 private TestInvocation mTestInvocation;
75
Brett Chabotb1052c22011-01-12 19:44:23 -080076 private IConfiguration mStubConfiguration;
77
Brett Chabot7e5f8ac2010-05-17 12:27:56 -070078 // The mock objects.
Brett Chabot74121d82010-01-28 20:14:27 -080079 private ITestDevice mMockDevice;
80 private ITargetPreparer mMockPreparer;
Brett Chabotfe88a152010-08-02 17:52:59 -070081 private IBuildProvider mMockBuildProvider;
Brett Chabot74121d82010-01-28 20:14:27 -080082 private IBuildInfo mMockBuildInfo;
83 private ITestInvocationListener mMockTestListener;
Omari Stephens203296c2010-09-02 18:20:45 -070084 private ITestSummaryListener mMockSummaryListener;
Brett Chabot7e5f8ac2010-05-17 12:27:56 -070085 private ILeveledLogOutput mMockLogger;
Eric Rowe0b445112013-07-12 09:29:47 -070086 private ILogSaver mMockLogSaver;
Brett Chabotf7ad1562010-07-27 11:58:58 -070087 private IDeviceRecovery mMockRecovery;
Omari Stephensa01abfe2010-09-07 15:48:44 -070088 private Capture<List<TestSummary>> mUriCapture;
Brett Chabot1d35e662011-05-25 19:48:24 -070089 private ILogRegistry mMockLogRegistry;
Julien Desprez2040ac02016-03-21 18:05:30 +000090 private IRescheduler mockRescheduler;
Brett Chabot74121d82010-01-28 20:14:27 -080091
92 @Override
93 protected void setUp() throws Exception {
94 super.setUp();
95
Brett Chabote7e335e2011-02-16 14:51:03 -080096 mStubConfiguration = new Configuration("foo", "bar");
Brett Chabotb1052c22011-01-12 19:44:23 -080097
Brett Chabot74121d82010-01-28 20:14:27 -080098 mMockDevice = EasyMock.createMock(ITestDevice.class);
Brett Chabotf7ad1562010-07-27 11:58:58 -070099 mMockRecovery = EasyMock.createMock(IDeviceRecovery.class);
Brett Chabot74121d82010-01-28 20:14:27 -0800100 mMockPreparer = EasyMock.createMock(ITargetPreparer.class);
Brett Chabotfe88a152010-08-02 17:52:59 -0700101 mMockBuildProvider = EasyMock.createMock(IBuildProvider.class);
Brett Chabotb1052c22011-01-12 19:44:23 -0800102
Omari Stephens203296c2010-09-02 18:20:45 -0700103 // Use strict mocks here since the order of Listener calls is important
104 mMockTestListener = EasyMock.createStrictMock(ITestInvocationListener.class);
105 mMockSummaryListener = EasyMock.createStrictMock(ITestSummaryListener.class);
Brett Chabot5dae8722010-09-17 20:05:51 -0700106 mMockBuildInfo = EasyMock.createMock(IBuildInfo.class);
Brett Chabot35d50c62011-03-07 10:39:23 -0800107 mMockLogger = EasyMock.createMock(ILeveledLogOutput.class);
Brett Chabot1d35e662011-05-25 19:48:24 -0700108 mMockLogRegistry = EasyMock.createMock(ILogRegistry.class);
Eric Rowe0b445112013-07-12 09:29:47 -0700109 mMockLogSaver = EasyMock.createMock(ILogSaver.class);
Julien Desprez2040ac02016-03-21 18:05:30 +0000110 mockRescheduler = EasyMock.createMock(IRescheduler.class);
Brett Chabot74121d82010-01-28 20:14:27 -0800111
Brett Chabotb1052c22011-01-12 19:44:23 -0800112 mStubConfiguration.setDeviceRecovery(mMockRecovery);
113 mStubConfiguration.setTargetPreparer(mMockPreparer);
114 mStubConfiguration.setBuildProvider(mMockBuildProvider);
Eric Rowe0b445112013-07-12 09:29:47 -0700115 mStubConfiguration.setLogSaver(mMockLogSaver);
Omari Stephens203296c2010-09-02 18:20:45 -0700116
Brett Chabotad123582011-02-09 18:26:40 -0800117 List<ITestInvocationListener> listenerList = new ArrayList<ITestInvocationListener>(1);
118 listenerList.add(mMockTestListener);
119 listenerList.add(mMockSummaryListener);
120 mStubConfiguration.setTestInvocationListeners(listenerList);
Omari Stephens203296c2010-09-02 18:20:45 -0700121
Brett Chabotb1052c22011-01-12 19:44:23 -0800122 mStubConfiguration.setLogOutput(mMockLogger);
Brett Chaboteba1fd62011-09-13 17:17:45 -0700123 EasyMock.expect(mMockDevice.getSerialNumber()).andStubReturn(SERIAL);
Guang Zhu789a52b2014-01-17 14:27:01 -0800124 EasyMock.expect(mMockDevice.getIDevice()).andStubReturn(null);
Brett Chabotf7ad1562010-07-27 11:58:58 -0700125 mMockDevice.setRecovery(mMockRecovery);
Julien Desprez26bee8d2016-03-29 12:09:48 +0100126 mMockDevice.preInvocationSetup((IBuildInfo)EasyMock.anyObject());
127 EasyMock.expectLastCall().anyTimes();
128 mMockDevice.postInvocationTearDown();
129 EasyMock.expectLastCall().anyTimes();
Brett Chabot74121d82010-01-28 20:14:27 -0800130
Jorge Gonzalez3777e322011-05-11 18:04:29 -0700131 EasyMock.expect(mMockBuildInfo.getBuildId()).andStubReturn("1");
Brett Chabotbcb497c2010-08-07 17:07:36 -0700132 EasyMock.expect(mMockBuildInfo.getBuildAttributes()).andStubReturn(EMPTY_MAP);
Brett Chabot2b79e0f2013-03-18 16:50:16 -0700133 EasyMock.expect(mMockBuildInfo.getBuildBranch()).andStubReturn("branch");
134 EasyMock.expect(mMockBuildInfo.getBuildFlavor()).andStubReturn("flavor");
Brett Chabot9d885432011-07-29 16:25:31 -0700135 EasyMock.expect(mMockBuildInfo.getTestTag()).andStubReturn("");
Brett Chabot1d35e662011-05-25 19:48:24 -0700136 // always expect logger initialization and cleanup calls
137 mMockLogRegistry.registerLogger(mMockLogger);
138 mMockLogger.init();
139 mMockLogger.closeLog();
140 mMockLogRegistry.unregisterLogger();
Omari Stephensa01abfe2010-09-07 15:48:44 -0700141 mUriCapture = new Capture<List<TestSummary>>();
Brett Chabotbcb497c2010-08-07 17:07:36 -0700142
Brett Chabot74121d82010-01-28 20:14:27 -0800143 // create the BaseTestInvocation to test
Brett Chabot5e4bc502010-06-10 20:10:30 -0700144 mTestInvocation = new TestInvocation() {
145 @Override
Brett Chabot1d35e662011-05-25 19:48:24 -0700146 ILogRegistry getLogRegistry() {
147 return mMockLogRegistry;
Brett Chabot5e4bc502010-06-10 20:10:30 -0700148 }
149 };
Brett Chabot74121d82010-01-28 20:14:27 -0800150 }
151
Neal Nguyen2ceabaa2010-05-26 14:42:14 -0700152 @Override
153 protected void tearDown() throws Exception {
154 super.tearDown();
155
156 }
157
Brett Chabot74121d82010-01-28 20:14:27 -0800158 /**
159 * Test the normal case invoke scenario with a {@link IRemoteTest}.
160 * <p/>
161 * Verifies that all external interfaces get notified as expected.
162 */
Brett Chabot87803df2013-03-01 16:01:50 -0800163 public void testInvoke_RemoteTest() throws Throwable {
Brett Chabot74121d82010-01-28 20:14:27 -0800164 IRemoteTest test = EasyMock.createMock(IRemoteTest.class);
Omari Stephens203296c2010-09-02 18:20:45 -0700165 setupMockSuccessListeners();
Julien Desprez26bee8d2016-03-29 12:09:48 +0100166
Brett Chabotad123582011-02-09 18:26:40 -0800167 test.run((ITestInvocationListener)EasyMock.anyObject());
Brett Chabot7e5f8ac2010-05-17 12:27:56 -0700168 setupNormalInvoke(test);
Julien Desprez2040ac02016-03-21 18:05:30 +0000169 EasyMock.replay(mockRescheduler);
170 mTestInvocation.invoke(mMockDevice, mStubConfiguration, mockRescheduler);
171 verifyMocks(test, mockRescheduler);
Omari Stephens203296c2010-09-02 18:20:45 -0700172 verifySummaryListener();
173 }
174
175 /**
176 * Test the normal case invoke scenario with an {@link ITestSummaryListener} masquerading as
177 * an {@link ITestInvocationListener}.
178 * <p/>
179 * Verifies that all external interfaces get notified as expected.
Omari Stephens203296c2010-09-02 18:20:45 -0700180 */
Julien Desprez78fcda22016-06-16 17:35:11 +0100181 public void testInvoke_twoSummary() throws Throwable {
Omari Stephens203296c2010-09-02 18:20:45 -0700182
183 IRemoteTest test = EasyMock.createMock(IRemoteTest.class);
184 setupMockSuccessListeners();
185
Brett Chabotad123582011-02-09 18:26:40 -0800186 test.run((ITestInvocationListener)EasyMock.anyObject());
Omari Stephens203296c2010-09-02 18:20:45 -0700187 setupNormalInvoke(test);
Julien Desprez2040ac02016-03-21 18:05:30 +0000188 EasyMock.replay(mockRescheduler);
189 mTestInvocation.invoke(mMockDevice, mStubConfiguration, mockRescheduler);
190 verifyMocks(test, mockRescheduler);
Omari Stephens203296c2010-09-02 18:20:45 -0700191 verifySummaryListener();
Brett Chabot74121d82010-01-28 20:14:27 -0800192 }
193
194 /**
195 * Test the invoke scenario where build retrieve fails.
Brett Chabot057e16e2010-06-27 21:53:48 -0700196 * <p/>
Brett Chabotc9924302011-06-30 16:48:01 -0700197 * An invocation will be started in this scenario.
Brett Chabot74121d82010-01-28 20:14:27 -0800198 */
Brett Chabot87803df2013-03-01 16:01:50 -0800199 public void testInvoke_buildFailed() throws Throwable {
Brett Chabotc9924302011-06-30 16:48:01 -0700200 BuildRetrievalError exception = new BuildRetrievalError("error", null, mMockBuildInfo);
Brett Chabotfe88a152010-08-02 17:52:59 -0700201 EasyMock.expect(mMockBuildProvider.getBuild()).andThrow(exception);
Brett Chabotc9924302011-06-30 16:48:01 -0700202 setupMockFailureListeners(exception);
Brett Chabot44556372014-03-19 19:07:15 -0700203 setupInvoke();
Brett Chabotccfda2e2011-02-09 14:39:59 -0800204 IRemoteTest test = EasyMock.createMock(IRemoteTest.class);
Brett Chabotb1052c22011-01-12 19:44:23 -0800205 mStubConfiguration.setTest(test);
Brett Chabot44556372014-03-19 19:07:15 -0700206 EasyMock.expect(mMockLogger.getLog())
207 .andReturn(new ByteArrayInputStreamSource(new byte[0]));
208 EasyMock.expect(mMockDevice.getLogcat())
209 .andReturn(new ByteArrayInputStreamSource(new byte[0])).times(2);
Julien Desprez2040ac02016-03-21 18:05:30 +0000210 replayMocks(test, mockRescheduler);
211 mTestInvocation.invoke(mMockDevice, mStubConfiguration, mockRescheduler);
212 verifyMocks(test, mockRescheduler);
Brett Chabot74121d82010-01-28 20:14:27 -0800213 }
214
215 /**
Brett Chabotdd21ddf2010-06-07 14:16:19 -0700216 * Test the invoke scenario where there is no build to test.
217 */
Brett Chabot87803df2013-03-01 16:01:50 -0800218 public void testInvoke_noBuild() throws Throwable {
Brett Chabotfe88a152010-08-02 17:52:59 -0700219 EasyMock.expect(mMockBuildProvider.getBuild()).andReturn(null);
Brett Chabotccfda2e2011-02-09 14:39:59 -0800220 IRemoteTest test = EasyMock.createMock(IRemoteTest.class);
Brett Chabotb1052c22011-01-12 19:44:23 -0800221 mStubConfiguration.setTest(test);
Brett Chabot1d35e662011-05-25 19:48:24 -0700222 mMockLogRegistry.dumpToGlobalLog(mMockLogger);
Brett Chabot44556372014-03-19 19:07:15 -0700223 setupInvoke();
Julien Desprez2040ac02016-03-21 18:05:30 +0000224 replayMocks(test, mockRescheduler);
225 mTestInvocation.invoke(mMockDevice, mStubConfiguration, mockRescheduler);
Brett Chabot057e16e2010-06-27 21:53:48 -0700226 verifyMocks(test);
Brett Chabot74121d82010-01-28 20:14:27 -0800227 }
228
Brett Chabot74121d82010-01-28 20:14:27 -0800229 /**
Eric Rowef85ffac2012-10-03 12:56:12 -0700230 * Test the invoke scenario where there is no build to test for a {@link IRetriableTest}.
231 */
Brett Chabot87803df2013-03-01 16:01:50 -0800232 public void testInvoke_noBuildRetry() throws Throwable {
Eric Rowef85ffac2012-10-03 12:56:12 -0700233 EasyMock.expect(mMockBuildProvider.getBuild()).andReturn(null);
234
235 IRetriableTest test = EasyMock.createMock(IRetriableTest.class);
236 EasyMock.expect(test.isRetriable()).andReturn(Boolean.TRUE);
237
Eric Rowef85ffac2012-10-03 12:56:12 -0700238 EasyMock.expect(mockRescheduler.rescheduleCommand()).andReturn(EasyMock.anyBoolean());
239
240 mStubConfiguration.setTest(test);
241 mStubConfiguration.getCommandOptions().setLoopMode(false);
242 mMockLogRegistry.dumpToGlobalLog(mMockLogger);
Brett Chabot44556372014-03-19 19:07:15 -0700243 setupInvoke();
Eric Rowef85ffac2012-10-03 12:56:12 -0700244 replayMocks(test);
245 EasyMock.replay(mockRescheduler);
246 mTestInvocation.invoke(mMockDevice, mStubConfiguration, mockRescheduler);
247 EasyMock.verify(mockRescheduler);
248 verifyMocks(test);
249 }
250
251 /**
Julien Desprez2040ac02016-03-21 18:05:30 +0000252 * Test the{@link TestInvocation#invoke(ITestDevice, IConfiguration, IRescheduler,
253 * ITestInvocationListener[])} scenario
Eric Rowe0b445112013-07-12 09:29:47 -0700254 * where the test is a {@link IDeviceTest}
Brett Chabot74121d82010-01-28 20:14:27 -0800255 */
Brett Chabot87803df2013-03-01 16:01:50 -0800256 public void testInvoke_deviceTest() throws Throwable {
Brett Chabot2e441bd2010-09-16 18:25:11 -0700257 DeviceConfigTest mockDeviceTest = EasyMock.createMock(DeviceConfigTest.class);
Brett Chabotb1052c22011-01-12 19:44:23 -0800258 mStubConfiguration.setTest(mockDeviceTest);
Brett Chabot2e441bd2010-09-16 18:25:11 -0700259 mockDeviceTest.setDevice(mMockDevice);
Brett Chabotad123582011-02-09 18:26:40 -0800260 mockDeviceTest.run((ITestInvocationListener)EasyMock.anyObject());
Omari Stephens203296c2010-09-02 18:20:45 -0700261 setupMockSuccessListeners();
Brett Chabot2e441bd2010-09-16 18:25:11 -0700262 setupNormalInvoke(mockDeviceTest);
Julien Desprez2040ac02016-03-21 18:05:30 +0000263 EasyMock.replay(mockRescheduler);
264 mTestInvocation.invoke(mMockDevice, mStubConfiguration, mockRescheduler);
265 verifyMocks(mockDeviceTest, mockRescheduler);
Omari Stephens203296c2010-09-02 18:20:45 -0700266 verifySummaryListener();
Brett Chabot74121d82010-01-28 20:14:27 -0800267 }
268
269 /**
270 * Test the invoke scenario where test run throws {@link IllegalArgumentException}
271 *
272 * @throws Exception if unexpected error occurs
273 */
Brett Chabot87803df2013-03-01 16:01:50 -0800274 public void testInvoke_testFail() throws Throwable {
Brett Chabot7e5f8ac2010-05-17 12:27:56 -0700275 IllegalArgumentException exception = new IllegalArgumentException();
Brett Chabot74121d82010-01-28 20:14:27 -0800276 IRemoteTest test = EasyMock.createMock(IRemoteTest.class);
Brett Chabotad123582011-02-09 18:26:40 -0800277 test.run((ITestInvocationListener)EasyMock.anyObject());
Brett Chabot7e5f8ac2010-05-17 12:27:56 -0700278 EasyMock.expectLastCall().andThrow(exception);
Omari Stephens203296c2010-09-02 18:20:45 -0700279 setupMockFailureListeners(exception);
Brett Chabotfe88a152010-08-02 17:52:59 -0700280 mMockBuildProvider.buildNotTested(mMockBuildInfo);
Brett Chabot7e5f8ac2010-05-17 12:27:56 -0700281 setupNormalInvoke(test);
Julien Desprez2040ac02016-03-21 18:05:30 +0000282 EasyMock.replay(mockRescheduler);
Omari Stephens203296c2010-09-02 18:20:45 -0700283 try {
Julien Desprez2040ac02016-03-21 18:05:30 +0000284 mTestInvocation.invoke(mMockDevice, mStubConfiguration, mockRescheduler);
Omari Stephens203296c2010-09-02 18:20:45 -0700285 fail("IllegalArgumentException was not rethrown");
286 } catch (IllegalArgumentException e) {
287 // expected
288 }
Julien Desprez2040ac02016-03-21 18:05:30 +0000289 verifyMocks(test, mockRescheduler);
Omari Stephens203296c2010-09-02 18:20:45 -0700290 verifySummaryListener();
Brett Chabot74121d82010-01-28 20:14:27 -0800291 }
292
293 /**
Brett Chabota17af5d2010-08-23 13:51:17 -0700294 * Test the invoke scenario where test run throws {@link FatalHostError}
295 *
296 * @throws Exception if unexpected error occurs
297 */
Brett Chabot87803df2013-03-01 16:01:50 -0800298 public void testInvoke_fatalError() throws Throwable {
Brett Chabota17af5d2010-08-23 13:51:17 -0700299 FatalHostError exception = new FatalHostError("error");
300 IRemoteTest test = EasyMock.createMock(IRemoteTest.class);
Brett Chabotad123582011-02-09 18:26:40 -0800301 test.run((ITestInvocationListener)EasyMock.anyObject());
Brett Chabota17af5d2010-08-23 13:51:17 -0700302 EasyMock.expectLastCall().andThrow(exception);
Omari Stephens203296c2010-09-02 18:20:45 -0700303 setupMockFailureListeners(exception);
Brett Chabota17af5d2010-08-23 13:51:17 -0700304 mMockBuildProvider.buildNotTested(mMockBuildInfo);
Brett Chabota17af5d2010-08-23 13:51:17 -0700305 setupNormalInvoke(test);
Julien Desprez2040ac02016-03-21 18:05:30 +0000306 EasyMock.replay(mockRescheduler);
Brett Chabota17af5d2010-08-23 13:51:17 -0700307 try {
Julien Desprez2040ac02016-03-21 18:05:30 +0000308 mTestInvocation.invoke(mMockDevice, mStubConfiguration, mockRescheduler);
Brett Chabota17af5d2010-08-23 13:51:17 -0700309 fail("FatalHostError was not rethrown");
310 } catch (FatalHostError e) {
311 // expected
312 }
Julien Desprez2040ac02016-03-21 18:05:30 +0000313 verifyMocks(test, mockRescheduler);
Omari Stephens203296c2010-09-02 18:20:45 -0700314 verifySummaryListener();
Brett Chabota17af5d2010-08-23 13:51:17 -0700315 }
316
317 /**
Eric Rowe0b445112013-07-12 09:29:47 -0700318 * Test the invoke scenario where test run throws {@link DeviceNotAvailableException}
Brett Chabot3759c852010-07-13 17:36:31 -0700319 *
320 * @throws Exception if unexpected error occurs
321 */
Brett Chabot87803df2013-03-01 16:01:50 -0800322 public void testInvoke_deviceNotAvail() throws Throwable {
Brett Chabot3759c852010-07-13 17:36:31 -0700323 DeviceNotAvailableException exception = new DeviceNotAvailableException();
324 IRemoteTest test = EasyMock.createMock(IRemoteTest.class);
Brett Chabotad123582011-02-09 18:26:40 -0800325 test.run((ITestInvocationListener)EasyMock.anyObject());
Brett Chabot3759c852010-07-13 17:36:31 -0700326 EasyMock.expectLastCall().andThrow(exception);
Julien Desprez70488e82016-02-05 11:28:34 +0000327 mMockDevice.setRecoveryMode(RecoveryMode.NONE);
328 EasyMock.expectLastCall();
Omari Stephens203296c2010-09-02 18:20:45 -0700329 setupMockFailureListeners(exception);
Brett Chabotfe88a152010-08-02 17:52:59 -0700330 mMockBuildProvider.buildNotTested(mMockBuildInfo);
Brett Chabot3759c852010-07-13 17:36:31 -0700331 setupNormalInvoke(test);
Julien Desprez2040ac02016-03-21 18:05:30 +0000332 EasyMock.replay(mockRescheduler);
Brett Chabot3759c852010-07-13 17:36:31 -0700333 try {
Julien Desprez2040ac02016-03-21 18:05:30 +0000334 mTestInvocation.invoke(mMockDevice, mStubConfiguration, mockRescheduler);
Brett Chabot3759c852010-07-13 17:36:31 -0700335 fail("DeviceNotAvailableException not thrown");
Brett Chabot3759c852010-07-13 17:36:31 -0700336 } catch (DeviceNotAvailableException e) {
337 // expected
338 }
Julien Desprez2040ac02016-03-21 18:05:30 +0000339 verifyMocks(test, mockRescheduler);
Omari Stephens203296c2010-09-02 18:20:45 -0700340 verifySummaryListener();
Brett Chabot3759c852010-07-13 17:36:31 -0700341 }
342
343 /**
Omari Stephens203296c2010-09-02 18:20:45 -0700344 * Test the invoke scenario where preparer throws {@link BuildError}
Brett Chabotfe88a152010-08-02 17:52:59 -0700345 *
346 * @throws Exception if unexpected error occurs
347 */
Brett Chabot87803df2013-03-01 16:01:50 -0800348 public void testInvoke_buildError() throws Throwable {
Brett Chabotfe88a152010-08-02 17:52:59 -0700349 BuildError exception = new BuildError("error");
350 IRemoteTest test = EasyMock.createMock(IRemoteTest.class);
Brett Chabotb1052c22011-01-12 19:44:23 -0800351 mStubConfiguration.setTest(test);
Brett Chabotfe88a152010-08-02 17:52:59 -0700352 EasyMock.expect(mMockBuildProvider.getBuild()).andReturn(mMockBuildInfo);
Brett Chabot1d35e662011-05-25 19:48:24 -0700353
Brett Chabotfe88a152010-08-02 17:52:59 -0700354 mMockPreparer.setUp(mMockDevice, mMockBuildInfo);
355 EasyMock.expectLastCall().andThrow(exception);
Omari Stephens203296c2010-09-02 18:20:45 -0700356 setupMockFailureListeners(exception);
Eric Rowe4a3369d2011-11-23 12:12:16 -0800357 EasyMock.expect(mMockDevice.getBugreport())
Guang Zhu36c56a22014-12-30 19:56:00 -0800358 .andReturn(new ByteArrayInputStreamSource(new byte[0]));
Brett Chabot44556372014-03-19 19:07:15 -0700359 setupInvokeWithBuild();
Brett Chabotfe88a152010-08-02 17:52:59 -0700360 replayMocks(test);
Julien Desprez2040ac02016-03-21 18:05:30 +0000361 EasyMock.replay(mockRescheduler);
362 mTestInvocation.invoke(mMockDevice, mStubConfiguration, mockRescheduler);
363 verifyMocks(test, mockRescheduler);
Omari Stephens203296c2010-09-02 18:20:45 -0700364 verifySummaryListener();
Brett Chabotfe88a152010-08-02 17:52:59 -0700365 }
366
Brett Chabotfe88a152010-08-02 17:52:59 -0700367 /**
Brett Chabotb1052c22011-01-12 19:44:23 -0800368 * Test the invoke scenario for a {@link IResumableTest}.
369 *
370 * @throws Exception if unexpected error occurs
371 */
Brett Chabot87803df2013-03-01 16:01:50 -0800372 public void testInvoke_resume() throws Throwable {
Brett Chabotb1052c22011-01-12 19:44:23 -0800373 IResumableTest resumableTest = EasyMock.createMock(IResumableTest.class);
374 mStubConfiguration.setTest(resumableTest);
375 ITestInvocationListener resumeListener = EasyMock.createStrictMock(
376 ITestInvocationListener.class);
377 mStubConfiguration.setTestInvocationListener(resumeListener);
378
379 EasyMock.expect(mMockBuildProvider.getBuild()).andReturn(mMockBuildInfo);
380 resumeListener.invocationStarted(mMockBuildInfo);
Moon Kim80553c52014-09-15 13:45:40 -0700381 mMockDevice.clearLastConnectedWifiNetwork();
Brett Chabotc8b709f2011-09-02 09:04:22 -0700382 mMockDevice.setOptions((TestDeviceOptions)EasyMock.anyObject());
Brett Chaboteba1fd62011-09-13 17:17:45 -0700383 mMockBuildInfo.setDeviceSerial(SERIAL);
Brett Chabot44556372014-03-19 19:07:15 -0700384 mMockDevice.startLogcat();
Brett Chabotb1052c22011-01-12 19:44:23 -0800385 mMockPreparer.setUp(mMockDevice, mMockBuildInfo);
386
Brett Chabotad123582011-02-09 18:26:40 -0800387 resumableTest.run((ITestInvocationListener)EasyMock.anyObject());
Brett Chabotb1052c22011-01-12 19:44:23 -0800388 EasyMock.expectLastCall().andThrow(new DeviceNotAvailableException());
389 EasyMock.expect(resumableTest.isResumable()).andReturn(Boolean.TRUE);
Julien Desprez70488e82016-02-05 11:28:34 +0000390 mMockDevice.setRecoveryMode(RecoveryMode.NONE);
391 EasyMock.expectLastCall();
Omari Stephens41978ee2011-02-23 19:03:05 -0800392 EasyMock.expect(mMockDevice.getLogcat())
393 .andReturn(new ByteArrayInputStreamSource(new byte[0]));
394 EasyMock.expect(mMockLogger.getLog())
395 .andReturn(new ByteArrayInputStreamSource(new byte[0]));
Eric Rowe0b445112013-07-12 09:29:47 -0700396 EasyMock.expect(mMockLogSaver.saveLogData(EasyMock.eq(TestInvocation.DEVICE_LOG_NAME),
397 EasyMock.eq(LogDataType.LOGCAT), (InputStream)EasyMock.anyObject())
398 ).andReturn(new LogFile(PATH, URL));
399 EasyMock.expect(mMockLogSaver.saveLogData(EasyMock.eq(TestInvocation.TRADEFED_LOG_NAME),
400 EasyMock.eq(LogDataType.TEXT), (InputStream)EasyMock.anyObject())
401 ).andReturn(new LogFile(PATH, URL));
Brett Chabotb1052c22011-01-12 19:44:23 -0800402 resumeListener.testLog(EasyMock.eq(TestInvocation.DEVICE_LOG_NAME),
Eric Rowecc35ac22013-07-09 12:22:36 -0700403 EasyMock.eq(LogDataType.LOGCAT), (InputStreamSource)EasyMock.anyObject());
Brett Chabotb1052c22011-01-12 19:44:23 -0800404 resumeListener.testLog(EasyMock.eq(TestInvocation.TRADEFED_LOG_NAME),
Omari Stephens41978ee2011-02-23 19:03:05 -0800405 EasyMock.eq(LogDataType.TEXT), (InputStreamSource)EasyMock.anyObject());
Brett Chabotb1052c22011-01-12 19:44:23 -0800406
407 // just return same build and logger for simplicity
408 EasyMock.expect(mMockBuildInfo.clone()).andReturn(mMockBuildInfo);
409 EasyMock.expect(mMockLogger.clone()).andReturn(mMockLogger);
410 IRescheduler mockRescheduler = EasyMock.createMock(IRescheduler.class);
411 Capture<IConfiguration> capturedConfig = new Capture<IConfiguration>();
412 EasyMock.expect(mockRescheduler.scheduleConfig(EasyMock.capture(capturedConfig)))
413 .andReturn(Boolean.TRUE);
414 mMockBuildProvider.cleanUp(mMockBuildInfo);
Guang Zhu0a1c63d2015-01-02 11:03:38 -0800415 mMockDevice.clearLastConnectedWifiNetwork();
Brett Chabot44556372014-03-19 19:07:15 -0700416 mMockDevice.stopLogcat();
Brett Chabotb1052c22011-01-12 19:44:23 -0800417
Brett Chabot35d50c62011-03-07 10:39:23 -0800418 mMockLogger.init();
Eric Rowe0b445112013-07-12 09:29:47 -0700419 mMockLogSaver.invocationStarted(mMockBuildInfo);
Brett Chabotb1052c22011-01-12 19:44:23 -0800420 // now set resumed invocation expectations
Moon Kim80553c52014-09-15 13:45:40 -0700421 mMockDevice.clearLastConnectedWifiNetwork();
Brett Chabotc8b709f2011-09-02 09:04:22 -0700422 mMockDevice.setOptions((TestDeviceOptions)EasyMock.anyObject());
Brett Chaboteba1fd62011-09-13 17:17:45 -0700423 mMockBuildInfo.setDeviceSerial(SERIAL);
Brett Chabot44556372014-03-19 19:07:15 -0700424 mMockDevice.startLogcat();
Brett Chabotb1052c22011-01-12 19:44:23 -0800425 mMockPreparer.setUp(mMockDevice, mMockBuildInfo);
Eric Rowe0b445112013-07-12 09:29:47 -0700426 mMockLogSaver.invocationStarted(mMockBuildInfo);
Brett Chabotb1052c22011-01-12 19:44:23 -0800427 mMockDevice.setRecovery(mMockRecovery);
Brett Chabotad123582011-02-09 18:26:40 -0800428 resumableTest.run((ITestInvocationListener)EasyMock.anyObject());
Omari Stephens41978ee2011-02-23 19:03:05 -0800429 EasyMock.expect(mMockDevice.getLogcat())
430 .andReturn(new ByteArrayInputStreamSource(new byte[0]));
431 EasyMock.expect(mMockLogger.getLog())
432 .andReturn(new ByteArrayInputStreamSource(new byte[0]));
Eric Rowe0b445112013-07-12 09:29:47 -0700433 EasyMock.expect(mMockLogSaver.saveLogData(EasyMock.eq(TestInvocation.DEVICE_LOG_NAME),
434 EasyMock.eq(LogDataType.LOGCAT), (InputStream)EasyMock.anyObject())
435 ).andReturn(new LogFile(PATH, URL));
436 EasyMock.expect(mMockLogSaver.saveLogData(EasyMock.eq(TestInvocation.TRADEFED_LOG_NAME),
437 EasyMock.eq(LogDataType.TEXT), (InputStream)EasyMock.anyObject())
438 ).andReturn(new LogFile(PATH, URL));
Brett Chabotb1052c22011-01-12 19:44:23 -0800439 resumeListener.testLog(EasyMock.eq(TestInvocation.DEVICE_LOG_NAME),
Eric Rowecc35ac22013-07-09 12:22:36 -0700440 EasyMock.eq(LogDataType.LOGCAT), (InputStreamSource)EasyMock.anyObject());
Brett Chabotb1052c22011-01-12 19:44:23 -0800441 resumeListener.testLog(EasyMock.eq(TestInvocation.TRADEFED_LOG_NAME),
Omari Stephens41978ee2011-02-23 19:03:05 -0800442 EasyMock.eq(LogDataType.TEXT), (InputStreamSource)EasyMock.anyObject());
Brett Chabotb1052c22011-01-12 19:44:23 -0800443 resumeListener.invocationEnded(EasyMock.anyLong());
Eric Rowe0b445112013-07-12 09:29:47 -0700444 mMockLogSaver.invocationEnded(EasyMock.anyLong());
Brett Chabotb1052c22011-01-12 19:44:23 -0800445 EasyMock.expect(resumeListener.getSummary()).andReturn(null);
446 mMockBuildInfo.cleanUp();
447 mMockLogger.closeLog();
Guang Zhu0a1c63d2015-01-02 11:03:38 -0800448 mMockDevice.clearLastConnectedWifiNetwork();
Brett Chabot44556372014-03-19 19:07:15 -0700449 mMockDevice.stopLogcat();
Brett Chabotb1052c22011-01-12 19:44:23 -0800450
451 EasyMock.replay(mockRescheduler, resumeListener, resumableTest, mMockPreparer,
Eric Rowe0b445112013-07-12 09:29:47 -0700452 mMockBuildProvider, mMockLogger, mMockLogSaver, mMockDevice, mMockBuildInfo);
Brett Chabotb1052c22011-01-12 19:44:23 -0800453
454 try {
455 mTestInvocation.invoke(mMockDevice, mStubConfiguration, mockRescheduler);
456 fail("DeviceNotAvailableException not thrown");
457 } catch (DeviceNotAvailableException e) {
458 // expect
459 }
460 // now call again, and expect invocation to be resumed properly
461 mTestInvocation.invoke(mMockDevice, capturedConfig.getValue(), mockRescheduler);
462
Eric Rowe0b445112013-07-12 09:29:47 -0700463 EasyMock.verify(mockRescheduler, resumeListener, resumableTest, mMockPreparer,
464 mMockBuildProvider, mMockLogger, mMockLogSaver, mMockDevice, mMockBuildInfo);
Brett Chabotb1052c22011-01-12 19:44:23 -0800465 }
466
467 /**
Eric Rowea3b85852012-07-16 15:07:49 -0700468 * Test the invoke scenario for a {@link IRetriableTest}.
469 *
470 * @throws Exception if unexpected error occurs
471 */
Brett Chabot87803df2013-03-01 16:01:50 -0800472 public void testInvoke_retry() throws Throwable {
Eric Rowea3b85852012-07-16 15:07:49 -0700473 AssertionError exception = new AssertionError();
474 IRetriableTest test = EasyMock.createMock(IRetriableTest.class);
475 test.run((ITestInvocationListener)EasyMock.anyObject());
476 EasyMock.expectLastCall().andThrow(exception);
477 EasyMock.expect(test.isRetriable()).andReturn(Boolean.TRUE);
478 mStubConfiguration.getCommandOptions().setLoopMode(false);
479 IRescheduler mockRescheduler = EasyMock.createMock(IRescheduler.class);
480 EasyMock.expect(mockRescheduler.rescheduleCommand()).andReturn(EasyMock.anyBoolean());
Eric Rowea3b85852012-07-16 15:07:49 -0700481 mMockBuildProvider.buildNotTested(mMockBuildInfo);
Eric Rowe0b445112013-07-12 09:29:47 -0700482 setupMockFailureListeners(exception);
Eric Rowea3b85852012-07-16 15:07:49 -0700483 setupNormalInvoke(test);
484 EasyMock.replay(mockRescheduler);
485 mTestInvocation.invoke(mMockDevice, mStubConfiguration, mockRescheduler);
Eric Rowe0b445112013-07-12 09:29:47 -0700486 verifyMocks(test, mockRescheduler);
Eric Rowea3b85852012-07-16 15:07:49 -0700487 verifySummaryListener();
488 }
489
490 /**
Julien Desprez2040ac02016-03-21 18:05:30 +0000491 * Test the {@link TestInvocation#invoke(ITestDevice, IConfiguration, IRescheduler,
492 * ITestInvocationListener[])} scenario
Eric Rowe0b445112013-07-12 09:29:47 -0700493 * when a {@link ITargetCleaner} is part of the config.
Brett Chabot87803df2013-03-01 16:01:50 -0800494 */
495 public void testInvoke_tearDown() throws Throwable {
496 IRemoteTest test = EasyMock.createNiceMock(IRemoteTest.class);
497 ITargetCleaner mockCleaner = EasyMock.createMock(ITargetCleaner.class);
498 mockCleaner.setUp(mMockDevice, mMockBuildInfo);
499 mockCleaner.tearDown(mMockDevice, mMockBuildInfo, null);
500 mStubConfiguration.getTargetPreparers().add(mockCleaner);
501 setupMockSuccessListeners();
502 setupNormalInvoke(test);
Julien Desprez2040ac02016-03-21 18:05:30 +0000503 EasyMock.replay(mockCleaner, mockRescheduler);
504 mTestInvocation.invoke(mMockDevice, mStubConfiguration, mockRescheduler);
505 verifyMocks(mockCleaner, mockRescheduler);
Brett Chabot87803df2013-03-01 16:01:50 -0800506 verifySummaryListener();
507 }
508
509 /**
Julien Desprez2040ac02016-03-21 18:05:30 +0000510 * Test the {@link TestInvocation#invoke(ITestDevice, IConfiguration, IRescheduler,
511 * ITestInvocationListener[])} scenario
Eric Rowe0b445112013-07-12 09:29:47 -0700512 * when a {@link ITargetCleaner} is part of the config, and the test throws a
Brett Chabot87803df2013-03-01 16:01:50 -0800513 * {@link DeviceNotAvailableException}.
514 */
515 public void testInvoke_tearDown_deviceNotAvail() throws Throwable {
516 DeviceNotAvailableException exception = new DeviceNotAvailableException();
517 IRemoteTest test = EasyMock.createMock(IRemoteTest.class);
518 test.run((ITestInvocationListener)EasyMock.anyObject());
519 EasyMock.expectLastCall().andThrow(exception);
520 ITargetCleaner mockCleaner = EasyMock.createMock(ITargetCleaner.class);
521 mockCleaner.setUp(mMockDevice, mMockBuildInfo);
Guang Zhu36c56a22014-12-30 19:56:00 -0800522 EasyMock.expectLastCall();
523 mockCleaner.tearDown(mMockDevice, mMockBuildInfo, exception);
524 EasyMock.expectLastCall();
Julien Desprez70488e82016-02-05 11:28:34 +0000525 mMockDevice.setRecoveryMode(RecoveryMode.NONE);
526 EasyMock.expectLastCall();
Julien Desprez2040ac02016-03-21 18:05:30 +0000527 EasyMock.replay(mockCleaner, mockRescheduler);
Brett Chabot87803df2013-03-01 16:01:50 -0800528 mStubConfiguration.getTargetPreparers().add(mockCleaner);
529 setupMockFailureListeners(exception);
530 mMockBuildProvider.buildNotTested(mMockBuildInfo);
531 setupNormalInvoke(test);
Brett Chabot87803df2013-03-01 16:01:50 -0800532 try {
Julien Desprez2040ac02016-03-21 18:05:30 +0000533 mTestInvocation.invoke(mMockDevice, mStubConfiguration, mockRescheduler);
Brett Chabot87803df2013-03-01 16:01:50 -0800534 fail("DeviceNotAvailableException not thrown");
535 } catch (DeviceNotAvailableException e) {
536 // expected
537 }
Julien Desprez2040ac02016-03-21 18:05:30 +0000538 verifyMocks(mockCleaner, mockRescheduler);
Brett Chabot87803df2013-03-01 16:01:50 -0800539 verifySummaryListener();
540 }
541
542 /**
Julien Desprez2040ac02016-03-21 18:05:30 +0000543 * Test the {@link TestInvocation#invoke(ITestDevice, IConfiguration, IRescheduler,
544 * ITestInvocationListener[])} scenario
Eric Rowe0b445112013-07-12 09:29:47 -0700545 * when a {@link ITargetCleaner} is part of the config, and the test throws a
Brett Chabot87803df2013-03-01 16:01:50 -0800546 * {@link RuntimeException}.
547 */
548 public void testInvoke_tearDown_runtime() throws Throwable {
549 RuntimeException exception = new RuntimeException();
550 IRemoteTest test = EasyMock.createMock(IRemoteTest.class);
551 test.run((ITestInvocationListener)EasyMock.anyObject());
552 EasyMock.expectLastCall().andThrow(exception);
553 ITargetCleaner mockCleaner = EasyMock.createMock(ITargetCleaner.class);
554 mockCleaner.setUp(mMockDevice, mMockBuildInfo);
555 // tearDown should be called
556 mockCleaner.tearDown(mMockDevice, mMockBuildInfo, exception);
557 mStubConfiguration.getTargetPreparers().add(mockCleaner);
558 setupMockFailureListeners(exception);
559 mMockBuildProvider.buildNotTested(mMockBuildInfo);
560 setupNormalInvoke(test);
Julien Desprez2040ac02016-03-21 18:05:30 +0000561 EasyMock.replay(mockCleaner, mockRescheduler);
Brett Chabot87803df2013-03-01 16:01:50 -0800562 try {
Julien Desprez2040ac02016-03-21 18:05:30 +0000563 mTestInvocation.invoke(mMockDevice, mStubConfiguration, mockRescheduler);
Brett Chabot87803df2013-03-01 16:01:50 -0800564 fail("RuntimeException not thrown");
565 } catch (RuntimeException e) {
566 // expected
567 }
Julien Desprez2040ac02016-03-21 18:05:30 +0000568 verifyMocks(mockCleaner, mockRescheduler);
Brett Chabot87803df2013-03-01 16:01:50 -0800569 verifySummaryListener();
570 }
571
572 /**
Julien Desprez2040ac02016-03-21 18:05:30 +0000573 * Test the {@link TestInvocation#invoke(ITestDevice, IConfiguration, IRescheduler,
574 * ITestInvocationListener[])} scenario
Eric Rowe0b445112013-07-12 09:29:47 -0700575 * when there is {@link ITestInvocationListener} which implements the {@link ILogSaverListener}
576 * interface.
577 */
578 public void testInvoke_logFileSaved() throws Throwable {
579 List<ITestInvocationListener> listenerList =
580 mStubConfiguration.getTestInvocationListeners();
581 ILogSaverListener logSaverListener = EasyMock.createMock(ILogSaverListener.class);
582 listenerList.add(logSaverListener);
583 mStubConfiguration.setTestInvocationListeners(listenerList);
584
585 logSaverListener.setLogSaver(mMockLogSaver);
586 logSaverListener.invocationStarted(mMockBuildInfo);
587 logSaverListener.testLog(EasyMock.eq(TestInvocation.DEVICE_LOG_NAME),
588 EasyMock.eq(LogDataType.LOGCAT), (InputStreamSource)EasyMock.anyObject());
589 logSaverListener.testLogSaved(EasyMock.eq(TestInvocation.DEVICE_LOG_NAME),
590 EasyMock.eq(LogDataType.LOGCAT), (InputStreamSource)EasyMock.anyObject(),
591 (LogFile)EasyMock.anyObject());
592 logSaverListener.testLog(EasyMock.eq(TestInvocation.TRADEFED_LOG_NAME),
593 EasyMock.eq(LogDataType.TEXT), (InputStreamSource)EasyMock.anyObject());
594 logSaverListener.testLogSaved(EasyMock.eq(TestInvocation.TRADEFED_LOG_NAME),
595 EasyMock.eq(LogDataType.TEXT), (InputStreamSource)EasyMock.anyObject(),
596 (LogFile)EasyMock.anyObject());
597 logSaverListener.invocationEnded(EasyMock.anyLong());
598 EasyMock.expect(logSaverListener.getSummary()).andReturn(mSummary);
599
600 IRemoteTest test = EasyMock.createMock(IRemoteTest.class);
601 setupMockSuccessListeners();
602 test.run((ITestInvocationListener)EasyMock.anyObject());
603 setupNormalInvoke(test);
Julien Desprez2040ac02016-03-21 18:05:30 +0000604 EasyMock.replay(logSaverListener, mockRescheduler);
605 mTestInvocation.invoke(mMockDevice, mStubConfiguration, mockRescheduler);
606 verifyMocks(test, logSaverListener, mockRescheduler);
Eric Rowe0b445112013-07-12 09:29:47 -0700607 assertEquals(2, mUriCapture.getValue().size());
608 }
609
610 /**
Brett Chabot74121d82010-01-28 20:14:27 -0800611 * @param test the {@link Test} to use.
612 */
Brett Chabot87803df2013-03-01 16:01:50 -0800613 private void setupNormalInvoke(IRemoteTest test) throws Throwable {
Brett Chabot44556372014-03-19 19:07:15 -0700614 setupInvokeWithBuild();
Brett Chabotb1052c22011-01-12 19:44:23 -0800615 mStubConfiguration.setTest(test);
Brett Chabotfe88a152010-08-02 17:52:59 -0700616 EasyMock.expect(mMockBuildProvider.getBuild()).andReturn(mMockBuildInfo);
Brett Chabotc8b709f2011-09-02 09:04:22 -0700617
Brett Chabot74121d82010-01-28 20:14:27 -0800618 mMockPreparer.setUp(mMockDevice, mMockBuildInfo);
Omari Stephens203296c2010-09-02 18:20:45 -0700619
Brett Chabot44556372014-03-19 19:07:15 -0700620 replayMocks(test);
621 }
622
623 /**
624 * Set up expected calls that occur on every invoke, regardless of result
625 */
626 private void setupInvoke() {
Moon Kim80553c52014-09-15 13:45:40 -0700627 mMockDevice.clearLastConnectedWifiNetwork();
Brett Chabot44556372014-03-19 19:07:15 -0700628 mMockDevice.setOptions((TestDeviceOptions)EasyMock.anyObject());
629 mMockDevice.startLogcat();
Guang Zhu0a1c63d2015-01-02 11:03:38 -0800630 mMockDevice.clearLastConnectedWifiNetwork();
Brett Chabot44556372014-03-19 19:07:15 -0700631 mMockDevice.stopLogcat();
632 }
633
634 /**
635 * Set up expected calls that occur on every invoke that gets a valid build
636 */
637 private void setupInvokeWithBuild() {
638 setupInvoke();
Omari Stephens41978ee2011-02-23 19:03:05 -0800639 EasyMock.expect(mMockDevice.getLogcat())
640 .andReturn(new ByteArrayInputStreamSource(new byte[0])).times(2);
Omari Stephens203296c2010-09-02 18:20:45 -0700641
Brett Chabot44556372014-03-19 19:07:15 -0700642 EasyMock.expect(mMockLogger.getLog())
643 .andReturn(new ByteArrayInputStreamSource(new byte[0]));
644 mMockBuildInfo.setDeviceSerial(SERIAL);
Brett Chabot66d87572010-11-01 19:54:59 -0700645 mMockBuildProvider.cleanUp(mMockBuildInfo);
Brett Chabot057e16e2010-06-27 21:53:48 -0700646 }
647
648 /**
Omari Stephens203296c2010-09-02 18:20:45 -0700649 * Set up expected conditions for the test InvocationListener and SummaryListener
650 * <p/>
651 * The order of calls for a single listener should be:
652 * <ol>
653 * <li>invocationStarted</li>
654 * <li>invocationFailed (if run failed)</li>
655 * <li>testLog(DEVICE_LOG_NAME, ...)</li>
656 * <li>testLog(TRADEFED_LOG_NAME, ...)</li>
657 * <li>putSummary (for an ITestSummaryListener)</li>
658 * <li>invocationEnded</li>
659 * <li>getSummary (for an ITestInvocationListener)</li>
660 * </ol>
661 * However note that, across all listeners, any getSummary call will precede all putSummary
662 * calls.
663 */
Eric Rowe0b445112013-07-12 09:29:47 -0700664 private void setupMockListeners(InvocationStatus status, Throwable throwable)
665 throws IOException {
Omari Stephens203296c2010-09-02 18:20:45 -0700666 // invocationStarted
Eric Rowe0b445112013-07-12 09:29:47 -0700667 mMockLogSaver.invocationStarted(mMockBuildInfo);
Omari Stephens203296c2010-09-02 18:20:45 -0700668 mMockTestListener.invocationStarted(mMockBuildInfo);
669 mMockSummaryListener.invocationStarted(mMockBuildInfo);
670
Guang Zhu36c56a22014-12-30 19:56:00 -0800671 // invocationFailed
672 if (!status.equals(InvocationStatus.SUCCESS)) {
673 mMockTestListener.invocationFailed(EasyMock.eq(throwable));
674 mMockSummaryListener.invocationFailed(EasyMock.eq(throwable));
675 }
676
Eric Rowea3b85852012-07-16 15:07:49 -0700677 if (throwable instanceof BuildError) {
Eric Rowe0b445112013-07-12 09:29:47 -0700678 EasyMock.expect(mMockLogSaver.saveLogData(
679 EasyMock.eq(TestInvocation.BUILD_ERROR_BUGREPORT_NAME),
680 EasyMock.eq(LogDataType.BUGREPORT), (InputStream)EasyMock.anyObject())
681 ).andReturn(new LogFile(PATH, URL));
Eric Rowe4a3369d2011-11-23 12:12:16 -0800682 mMockTestListener.testLog(EasyMock.eq(TestInvocation.BUILD_ERROR_BUGREPORT_NAME),
Eric Rowecc35ac22013-07-09 12:22:36 -0700683 EasyMock.eq(LogDataType.BUGREPORT), (InputStreamSource)EasyMock.anyObject());
Eric Rowe4a3369d2011-11-23 12:12:16 -0800684 mMockSummaryListener.testLog(EasyMock.eq(TestInvocation.BUILD_ERROR_BUGREPORT_NAME),
Eric Rowecc35ac22013-07-09 12:22:36 -0700685 EasyMock.eq(LogDataType.BUGREPORT), (InputStreamSource)EasyMock.anyObject());
Eric Rowe4a3369d2011-11-23 12:12:16 -0800686 }
687
Eric Rowe0b445112013-07-12 09:29:47 -0700688 // saveAndZipLogData (mMockLogFileSaver)
689 EasyMock.expect(mMockLogSaver.saveLogData( EasyMock.eq(TestInvocation.DEVICE_LOG_NAME),
690 EasyMock.eq(LogDataType.LOGCAT), (InputStream)EasyMock.anyObject())
691 ).andReturn(new LogFile(PATH, URL));
Omari Stephens203296c2010-09-02 18:20:45 -0700692 // testLog (mMockTestListener)
693 mMockTestListener.testLog(EasyMock.eq(TestInvocation.DEVICE_LOG_NAME),
Eric Rowecc35ac22013-07-09 12:22:36 -0700694 EasyMock.eq(LogDataType.LOGCAT), (InputStreamSource)EasyMock.anyObject());
Brett Chabot87803df2013-03-01 16:01:50 -0800695 // testLog (mMockSummaryListener)
696 mMockSummaryListener.testLog(EasyMock.eq(TestInvocation.DEVICE_LOG_NAME),
Eric Rowecc35ac22013-07-09 12:22:36 -0700697 EasyMock.eq(LogDataType.LOGCAT), (InputStreamSource)EasyMock.anyObject());
Eric Rowe0b445112013-07-12 09:29:47 -0700698
699 EasyMock.expect(mMockLogSaver.saveLogData(EasyMock.eq(TestInvocation.TRADEFED_LOG_NAME),
700 EasyMock.eq(LogDataType.TEXT), (InputStream)EasyMock.anyObject())
701 ).andReturn(new LogFile(PATH, URL));
Omari Stephens203296c2010-09-02 18:20:45 -0700702 mMockTestListener.testLog(EasyMock.eq(TestInvocation.TRADEFED_LOG_NAME),
Omari Stephens41978ee2011-02-23 19:03:05 -0800703 EasyMock.eq(LogDataType.TEXT), (InputStreamSource)EasyMock.anyObject());
Omari Stephens203296c2010-09-02 18:20:45 -0700704 mMockSummaryListener.testLog(EasyMock.eq(TestInvocation.TRADEFED_LOG_NAME),
Omari Stephens41978ee2011-02-23 19:03:05 -0800705 EasyMock.eq(LogDataType.TEXT), (InputStreamSource)EasyMock.anyObject());
Omari Stephens203296c2010-09-02 18:20:45 -0700706
707 // invocationEnded, getSummary (mMockTestListener)
708 mMockTestListener.invocationEnded(EasyMock.anyLong());
Omari Stephensa01abfe2010-09-07 15:48:44 -0700709 EasyMock.expect(mMockTestListener.getSummary()).andReturn(mSummary);
Omari Stephens203296c2010-09-02 18:20:45 -0700710
711 // putSummary, invocationEnded (mMockSummaryListener)
712 mMockSummaryListener.putSummary(EasyMock.capture(mUriCapture));
713 mMockSummaryListener.invocationEnded(EasyMock.anyLong());
Eric Rowe0b445112013-07-12 09:29:47 -0700714 mMockLogSaver.invocationEnded(EasyMock.anyLong());
Omari Stephens203296c2010-09-02 18:20:45 -0700715 }
716
Eric Rowe0b445112013-07-12 09:29:47 -0700717 private void setupMockSuccessListeners() throws IOException {
Omari Stephens203296c2010-09-02 18:20:45 -0700718 setupMockListeners(InvocationStatus.SUCCESS, null);
719 }
720
Eric Rowe0b445112013-07-12 09:29:47 -0700721 private void setupMockFailureListeners(Throwable throwable) throws IOException {
Eric Rowea3b85852012-07-16 15:07:49 -0700722 setupMockListeners(InvocationStatus.FAILED, throwable);
Omari Stephens203296c2010-09-02 18:20:45 -0700723 }
724
725 private void verifySummaryListener() {
726 // Check that we captured the expected uris List
Omari Stephensa01abfe2010-09-07 15:48:44 -0700727 List<TestSummary> summaries = mUriCapture.getValue();
728 assertEquals(1, summaries.size());
729 assertEquals(mSummary, summaries.get(0));
Omari Stephens203296c2010-09-02 18:20:45 -0700730 }
731
732 /**
Brett Chabot057e16e2010-06-27 21:53:48 -0700733 * Verify all mock objects received expected calls
734 */
Brett Chabot87803df2013-03-01 16:01:50 -0800735 private void verifyMocks(Object... mocks) {
Brett Chabot1d35e662011-05-25 19:48:24 -0700736 // note: intentionally exclude configuration from verification - don't care
Brett Chabot057e16e2010-06-27 21:53:48 -0700737 // what methods are called
Brett Chabot87803df2013-03-01 16:01:50 -0800738 EasyMock.verify(mMockTestListener, mMockSummaryListener, mMockPreparer,
Eric Rowe0b445112013-07-12 09:29:47 -0700739 mMockBuildProvider, mMockLogger, mMockBuildInfo, mMockLogRegistry,
740 mMockLogSaver);
Brett Chabot87803df2013-03-01 16:01:50 -0800741 if (mocks.length > 0) {
742 EasyMock.verify(mocks);
743 }
Brett Chabot057e16e2010-06-27 21:53:48 -0700744 }
745
746 /**
747 * Switch all mock objects into replay mode.
748 */
Brett Chabot87803df2013-03-01 16:01:50 -0800749 private void replayMocks(Object... mocks) {
Eric Rowe0b445112013-07-12 09:29:47 -0700750 EasyMock.replay(mMockTestListener, mMockSummaryListener, mMockPreparer,
751 mMockBuildProvider, mMockLogger, mMockBuildInfo, mMockLogRegistry,
Moon Kim0995f442016-05-25 13:13:40 -0700752 mMockLogSaver, mMockDevice);
Brett Chabot87803df2013-03-01 16:01:50 -0800753 if (mocks.length > 0) {
754 EasyMock.replay(mocks);
755 }
Brett Chabot74121d82010-01-28 20:14:27 -0800756 }
757
758 /**
Brett Chabot74121d82010-01-28 20:14:27 -0800759 * Interface for testing device config pass through.
760 */
Brett Chabot2e441bd2010-09-16 18:25:11 -0700761 private interface DeviceConfigTest extends IRemoteTest, IDeviceTest {
Brett Chabot74121d82010-01-28 20:14:27 -0800762
763 }
764}