• 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.IDevice;
20 import com.android.ddmlib.IShellOutputReceiver;
21 
22 import org.easymock.EasyMock;
23 
24 import java.io.IOException;
25 import java.util.Collections;
26 
27 import junit.framework.TestCase;
28 
29 /**
30  * Unit tests for {@link RemoteAndroidTestRunner}.
31  */
32 public class RemoteAndroidTestRunnerTest extends TestCase {
33 
34     private RemoteAndroidTestRunner mRunner;
35     private IDevice mMockDevice;
36     private ITestRunListener mMockListener;
37 
38     private static final String TEST_PACKAGE = "com.test";
39     private static final String TEST_RUNNER = "com.test.InstrumentationTestRunner";
40 
41     /**
42      * @see junit.framework.TestCase#setUp()
43      */
44     @Override
setUp()45     protected void setUp() throws Exception {
46         mMockDevice = EasyMock.createMock(IDevice.class);
47         EasyMock.expect(mMockDevice.getSerialNumber()).andStubReturn("serial");
48         mMockListener = EasyMock.createNiceMock(ITestRunListener.class);
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 Exception {
56         String expectedCmd = EasyMock.eq(String.format("am instrument -w -r  %s/%s", TEST_PACKAGE,
57                 TEST_RUNNER));
58         runAndVerify(expectedCmd);
59     }
60 
61     /**
62      * Test the building of the instrumentation runner command with log set.
63      */
testRun_withLog()64     public void testRun_withLog() throws Exception {
65         mRunner.setLogOnly(true);
66         String expectedCmd = EasyMock.contains("-e log true");
67         runAndVerify(expectedCmd);
68     }
69 
70     /**
71      * Test the building of the instrumentation runner command with method set.
72      */
testRun_withMethod()73     public void testRun_withMethod() throws Exception {
74         final String className = "FooTest";
75         final String testName = "fooTest";
76         mRunner.setMethodName(className, testName);
77         String expectedCmd = EasyMock.contains(String.format("-e class %s#%s", className,
78                 testName));
79         runAndVerify(expectedCmd);
80     }
81 
82     /**
83      * Test the building of the instrumentation runner command with test package set.
84      */
testRun_withPackage()85     public void testRun_withPackage() throws Exception {
86         final String packageName = "foo.test";
87         mRunner.setTestPackageName(packageName);
88         String expectedCmd = EasyMock.contains(String.format("-e package %s", packageName));
89         runAndVerify(expectedCmd);
90     }
91 
92     /**
93      * Test the building of the instrumentation runner command with extra argument added.
94      */
testRun_withAddInstrumentationArg()95     public void testRun_withAddInstrumentationArg() throws Exception {
96         final String extraArgName = "blah";
97         final String extraArgValue = "blahValue";
98         mRunner.addInstrumentationArg(extraArgName, extraArgValue);
99         String expectedCmd = EasyMock.contains(String.format("-e %s %s", extraArgName,
100                 extraArgValue));
101         runAndVerify(expectedCmd);
102     }
103 
104     /**
105      * Test run when the device throws a IOException
106      */
107     @SuppressWarnings("unchecked")
testRun_ioException()108     public void testRun_ioException() throws Exception {
109         mMockDevice.executeShellCommand((String)EasyMock.anyObject(), (IShellOutputReceiver)
110                 EasyMock.anyObject(), EasyMock.eq(0));
111         EasyMock.expectLastCall().andThrow(new IOException());
112         // verify that the listeners run started, run failure, and run ended methods are called
113         mMockListener.testRunStarted(TEST_PACKAGE, 0);
114         mMockListener.testRunFailed((String)EasyMock.anyObject());
115         mMockListener.testRunEnded(EasyMock.anyLong(), EasyMock.eq(Collections.EMPTY_MAP));
116 
117         EasyMock.replay(mMockDevice, mMockListener);
118         try {
119             mRunner.run(mMockListener);
120             fail("IOException not thrown");
121         } catch (IOException e) {
122             // expected
123         }
124         EasyMock.verify(mMockDevice, mMockListener);
125     }
126 
127     /**
128      * Calls {@link RemoteAndroidTestRunner#run(ITestRunListener...)} and verifies the given
129      * <var>expectedCmd</var> pattern was received by the mock device.
130      */
runAndVerify(String expectedCmd)131     private void runAndVerify(String expectedCmd) throws Exception {
132         mMockDevice.executeShellCommand(expectedCmd, (IShellOutputReceiver)
133                 EasyMock.anyObject(), EasyMock.eq(0));
134         EasyMock.replay(mMockDevice);
135         mRunner.run(mMockListener);
136         EasyMock.verify(mMockDevice);
137     }
138 }
139