• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2008 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.ddmlib.testrunner;
18 
19 import com.android.ddmlib.Client;
20 import com.android.ddmlib.FileListingService;
21 import com.android.ddmlib.IDevice;
22 import com.android.ddmlib.IShellOutputReceiver;
23 import com.android.ddmlib.RawImage;
24 import com.android.ddmlib.SyncService;
25 import com.android.ddmlib.log.LogReceiver;
26 
27 import java.io.IOException;
28 import java.util.Map;
29 
30 import junit.framework.TestCase;
31 
32 /**
33  * Tests RemoteAndroidTestRunner.
34  */
35 public class RemoteAndroidTestRunnerTest extends TestCase {
36 
37     private RemoteAndroidTestRunner mRunner;
38     private MockDevice mMockDevice;
39 
40     private static final String TEST_PACKAGE = "com.test";
41     private static final String TEST_RUNNER = "com.test.InstrumentationTestRunner";
42 
43     /**
44      * @see junit.framework.TestCase#setUp()
45      */
46     @Override
setUp()47     protected void setUp() throws Exception {
48         mMockDevice = new MockDevice();
49         mRunner = new RemoteAndroidTestRunner(TEST_PACKAGE, TEST_RUNNER, mMockDevice);
50     }
51 
52     /**
53      * Test the basic case building of the instrumentation runner command with no arguments.
54      */
testRun()55     public void testRun() throws IOException {
56         mRunner.run(new EmptyListener());
57         assertStringsEquals(String.format("am instrument -w -r %s/%s", TEST_PACKAGE, TEST_RUNNER),
58                 mMockDevice.getLastShellCommand());
59     }
60 
61     /**
62      * Test the building of the instrumentation runner command with log set.
63      */
testRunWithLog()64     public void testRunWithLog() throws IOException {
65         mRunner.setLogOnly(true);
66         mRunner.run(new EmptyListener());
67         assertStringsEquals(String.format("am instrument -w -r -e log true %s/%s", TEST_PACKAGE,
68                 TEST_RUNNER), mMockDevice.getLastShellCommand());
69     }
70 
71     /**
72      * Test the building of the instrumentation runner command with method set.
73      */
testRunWithMethod()74     public void testRunWithMethod() throws IOException {
75         final String className = "FooTest";
76         final String testName = "fooTest";
77         mRunner.setMethodName(className, testName);
78         mRunner.run(new EmptyListener());
79         assertStringsEquals(String.format("am instrument -w -r -e class %s#%s %s/%s", className,
80                 testName, TEST_PACKAGE, TEST_RUNNER), mMockDevice.getLastShellCommand());
81     }
82 
83     /**
84      * Test the building of the instrumentation runner command with test package set.
85      */
testRunWithPackage()86     public void testRunWithPackage() throws IOException {
87         final String packageName = "foo.test";
88         mRunner.setTestPackageName(packageName);
89         mRunner.run(new EmptyListener());
90         assertStringsEquals(String.format("am instrument -w -r -e package %s %s/%s", packageName,
91                 TEST_PACKAGE, TEST_RUNNER), mMockDevice.getLastShellCommand());
92     }
93 
94     /**
95      * Test the building of the instrumentation runner command with extra argument added.
96      */
testRunWithAddInstrumentationArg()97     public void testRunWithAddInstrumentationArg() throws IOException {
98         final String extraArgName = "blah";
99         final String extraArgValue = "blahValue";
100         mRunner.addInstrumentationArg(extraArgName, extraArgValue);
101         mRunner.run(new EmptyListener());
102         assertStringsEquals(String.format("am instrument -w -r -e %s %s %s/%s", extraArgName,
103                 extraArgValue, TEST_PACKAGE, TEST_RUNNER), mMockDevice.getLastShellCommand());
104     }
105 
106 
107     /**
108      * Assert two strings are equal ignoring whitespace.
109      */
assertStringsEquals(String str1, String str2)110     private void assertStringsEquals(String str1, String str2) {
111         String strippedStr1 = str1.replaceAll(" ", "");
112         String strippedStr2 = str2.replaceAll(" ", "");
113         assertEquals(strippedStr1, strippedStr2);
114     }
115 
116     /**
117      * A dummy device that does nothing except store the provided executed shell command for
118      * later retrieval.
119      */
120     private static class MockDevice implements IDevice {
121 
122         private String mLastShellCommand;
123 
124         /**
125          * Stores the provided command for later retrieval from getLastShellCommand.
126          */
executeShellCommand(String command, IShellOutputReceiver receiver)127         public void executeShellCommand(String command,
128                 IShellOutputReceiver receiver) throws IOException {
129             mLastShellCommand = command;
130         }
131 
132         /**
133          * Stores the provided command for later retrieval from getLastShellCommand.
134          */
executeShellCommand(String command, IShellOutputReceiver receiver, int timeout)135         public void executeShellCommand(String command,
136                 IShellOutputReceiver receiver, int timeout) throws IOException {
137             mLastShellCommand = command;
138         }
139 
140         /**
141          * Get the last command provided to executeShellCommand.
142          */
getLastShellCommand()143         public String getLastShellCommand() {
144             return mLastShellCommand;
145         }
146 
createForward(int localPort, int remotePort)147         public void createForward(int localPort, int remotePort) {
148             throw new UnsupportedOperationException();
149         }
150 
getClient(String applicationName)151         public Client getClient(String applicationName) {
152             throw new UnsupportedOperationException();
153         }
154 
getClientName(int pid)155         public String getClientName(int pid) {
156             throw new UnsupportedOperationException();
157         }
158 
getClients()159         public Client[] getClients() {
160             throw new UnsupportedOperationException();
161         }
162 
getFileListingService()163         public FileListingService getFileListingService() {
164             throw new UnsupportedOperationException();
165         }
166 
getProperties()167         public Map<String, String> getProperties() {
168             throw new UnsupportedOperationException();
169         }
170 
getProperty(String name)171         public String getProperty(String name) {
172             throw new UnsupportedOperationException();
173         }
174 
getPropertyCount()175         public int getPropertyCount() {
176             throw new UnsupportedOperationException();
177         }
178 
getMountPoint(String name)179         public String getMountPoint(String name) {
180             throw new UnsupportedOperationException();
181         }
182 
getScreenshot()183         public RawImage getScreenshot() throws IOException {
184             throw new UnsupportedOperationException();
185         }
186 
getSerialNumber()187         public String getSerialNumber() {
188             return "fakeserial";
189         }
190 
getState()191         public DeviceState getState() {
192             throw new UnsupportedOperationException();
193         }
194 
getSyncService()195         public SyncService getSyncService() {
196             throw new UnsupportedOperationException();
197         }
198 
hasClients()199         public boolean hasClients() {
200             throw new UnsupportedOperationException();
201         }
202 
isBootLoader()203         public boolean isBootLoader() {
204             throw new UnsupportedOperationException();
205         }
206 
isEmulator()207         public boolean isEmulator() {
208             throw new UnsupportedOperationException();
209         }
210 
isOffline()211         public boolean isOffline() {
212             throw new UnsupportedOperationException();
213         }
214 
isOnline()215         public boolean isOnline() {
216             throw new UnsupportedOperationException();
217         }
218 
removeForward(int localPort, int remotePort)219         public void removeForward(int localPort, int remotePort) {
220             throw new UnsupportedOperationException();
221         }
222 
runEventLogService(LogReceiver receiver)223         public void runEventLogService(LogReceiver receiver) throws IOException {
224             throw new UnsupportedOperationException();
225         }
226 
runLogService(String logname, LogReceiver receiver)227         public void runLogService(String logname, LogReceiver receiver) throws IOException {
228             throw new UnsupportedOperationException();
229         }
230 
getAvdName()231         public String getAvdName() {
232             return "";
233         }
234 
installPackage(String packageFilePath, boolean reinstall)235         public String installPackage(String packageFilePath, boolean reinstall)
236                 throws IOException {
237             throw new UnsupportedOperationException();
238         }
239 
uninstallPackage(String packageName)240         public String uninstallPackage(String packageName) throws IOException {
241             throw new UnsupportedOperationException();
242         }
243 
installRemotePackage(String remoteFilePath, boolean reinstall)244         public String installRemotePackage(String remoteFilePath,
245                 boolean reinstall) throws IOException {
246             throw new UnsupportedOperationException();
247         }
248 
removeRemotePackage(String remoteFilePath)249         public void removeRemotePackage(String remoteFilePath)
250                 throws IOException {
251             throw new UnsupportedOperationException();
252         }
253 
syncPackageToDevice(String localFilePath)254         public String syncPackageToDevice(String localFilePath)
255                 throws IOException {
256             throw new UnsupportedOperationException();
257         }
258 
reboot(String into)259         public void reboot(String into) throws IOException {
260             throw new UnsupportedOperationException();
261         }
262     }
263 
264     /**
265      * An empty implementation of ITestRunListener.
266      */
267     private static class EmptyListener implements ITestRunListener {
268 
testEnded(TestIdentifier test)269         public void testEnded(TestIdentifier test) {
270             // ignore
271         }
272 
testFailed(TestFailure status, TestIdentifier test, String trace)273         public void testFailed(TestFailure status, TestIdentifier test, String trace) {
274             // ignore
275         }
276 
testRunEnded(long elapsedTime)277         public void testRunEnded(long elapsedTime) {
278             // ignore
279         }
280 
testRunFailed(String errorMessage)281         public void testRunFailed(String errorMessage) {
282             // ignore
283         }
284 
testRunStarted(int testCount)285         public void testRunStarted(int testCount) {
286             // ignore
287         }
288 
testRunStopped(long elapsedTime)289         public void testRunStopped(long elapsedTime) {
290             // ignore
291         }
292 
testStarted(TestIdentifier test)293         public void testStarted(TestIdentifier test) {
294             // ignore
295         }
296     }
297 }
298