aboutsummaryrefslogtreecommitdiffstats
path: root/ddms/libs/ddmlib/tests/src
diff options
context:
space:
mode:
authorThe Android Open Source Project <initial-contribution@android.com>2009-03-03 19:29:09 -0800
committerThe Android Open Source Project <initial-contribution@android.com>2009-03-03 19:29:09 -0800
commit55a2c71f27d3e0b8344597c7f281e687cb7aeb1b (patch)
treeecd18b995aea8eeeb8b3823266280d41245bf0f7 /ddms/libs/ddmlib/tests/src
parent82ea7a177797b844b252effea5c7c7c5d63ea4ac (diff)
downloadsdk-55a2c71f27d3e0b8344597c7f281e687cb7aeb1b.zip
sdk-55a2c71f27d3e0b8344597c7f281e687cb7aeb1b.tar.gz
sdk-55a2c71f27d3e0b8344597c7f281e687cb7aeb1b.tar.bz2
auto import from //depot/cupcake/@135843
Diffstat (limited to 'ddms/libs/ddmlib/tests/src')
-rw-r--r--ddms/libs/ddmlib/tests/src/com/android/ddmlib/testrunner/InstrumentationResultParserTest.java245
-rw-r--r--ddms/libs/ddmlib/tests/src/com/android/ddmlib/testrunner/RemoteAndroidTestRunnerTest.java248
2 files changed, 493 insertions, 0 deletions
diff --git a/ddms/libs/ddmlib/tests/src/com/android/ddmlib/testrunner/InstrumentationResultParserTest.java b/ddms/libs/ddmlib/tests/src/com/android/ddmlib/testrunner/InstrumentationResultParserTest.java
new file mode 100644
index 0000000..77d10c1
--- /dev/null
+++ b/ddms/libs/ddmlib/tests/src/com/android/ddmlib/testrunner/InstrumentationResultParserTest.java
@@ -0,0 +1,245 @@
+/*
+ * Copyright (C) 2008 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.ddmlib.testrunner;
+
+import junit.framework.TestCase;
+
+
+/**
+ * Tests InstrumentationResultParser.
+ */
+public class InstrumentationResultParserTest extends TestCase {
+
+ private InstrumentationResultParser mParser;
+ private VerifyingTestResult mTestResult;
+
+ // static dummy test names to use for validation
+ private static final String CLASS_NAME = "com.test.FooTest";
+ private static final String TEST_NAME = "testFoo";
+ private static final String STACK_TRACE = "java.lang.AssertionFailedException";
+
+ /**
+ * @param name - test name
+ */
+ public InstrumentationResultParserTest(String name) {
+ super(name);
+ }
+
+ /**
+ * @see junit.framework.TestCase#setUp()
+ */
+ @Override
+ protected void setUp() throws Exception {
+ super.setUp();
+ mTestResult = new VerifyingTestResult();
+ mParser = new InstrumentationResultParser(mTestResult);
+ }
+
+ /**
+ * Tests that the test run started and test start events is sent on first
+ * bundle received.
+ */
+ public void testTestStarted() {
+ StringBuilder output = buildCommonResult();
+ addStartCode(output);
+
+ injectTestString(output.toString());
+ assertCommonAttributes();
+ assertEquals(0, mTestResult.mNumTestsRun);
+ }
+
+ /**
+ * Tests that a single successful test execution.
+ */
+ public void testTestSuccess() {
+ StringBuilder output = buildCommonResult();
+ addStartCode(output);
+ addCommonStatus(output);
+ addSuccessCode(output);
+
+ injectTestString(output.toString());
+ assertCommonAttributes();
+ assertEquals(1, mTestResult.mNumTestsRun);
+ assertEquals(null, mTestResult.mTestStatus);
+ }
+
+ /**
+ * Test basic parsing of failed test case.
+ */
+ public void testTestFailed() {
+ StringBuilder output = buildCommonResult();
+ addStartCode(output);
+ addCommonStatus(output);
+ addStackTrace(output);
+ addFailureCode(output);
+
+ injectTestString(output.toString());
+ assertCommonAttributes();
+
+ assertEquals(1, mTestResult.mNumTestsRun);
+ assertEquals(ITestRunListener.TestFailure.FAILURE, mTestResult.mTestStatus);
+ assertEquals(STACK_TRACE, mTestResult.mTrace);
+ }
+
+ /**
+ * Test basic parsing and conversion of time from output.
+ */
+ public void testTimeParsing() {
+ final String timeString = "Time: 4.9";
+ injectTestString(timeString);
+ assertEquals(4900, mTestResult.mTestTime);
+ }
+
+ /**
+ * builds a common test result using TEST_NAME and TEST_CLASS.
+ */
+ private StringBuilder buildCommonResult() {
+ StringBuilder output = new StringBuilder();
+ // add test start bundle
+ addCommonStatus(output);
+ addStatusCode(output, "1");
+ // add end test bundle, without status
+ addCommonStatus(output);
+ return output;
+ }
+
+ /**
+ * Adds common status results to the provided output.
+ */
+ private void addCommonStatus(StringBuilder output) {
+ addStatusKey(output, "stream", "\r\n" + CLASS_NAME);
+ addStatusKey(output, "test", TEST_NAME);
+ addStatusKey(output, "class", CLASS_NAME);
+ addStatusKey(output, "current", "1");
+ addStatusKey(output, "numtests", "1");
+ addStatusKey(output, "id", "InstrumentationTestRunner");
+ }
+
+ /**
+ * Adds a stack trace status bundle to output.
+ */
+ private void addStackTrace(StringBuilder output) {
+ addStatusKey(output, "stack", STACK_TRACE);
+
+ }
+
+ /**
+ * Helper method to add a status key-value bundle.
+ */
+ private void addStatusKey(StringBuilder outputBuilder, String key,
+ String value) {
+ outputBuilder.append("INSTRUMENTATION_STATUS: ");
+ outputBuilder.append(key);
+ outputBuilder.append('=');
+ outputBuilder.append(value);
+ outputBuilder.append("\r\n");
+ }
+
+ private void addStartCode(StringBuilder outputBuilder) {
+ addStatusCode(outputBuilder, "1");
+ }
+
+ private void addSuccessCode(StringBuilder outputBuilder) {
+ addStatusCode(outputBuilder, "0");
+ }
+
+ private void addFailureCode(StringBuilder outputBuilder) {
+ addStatusCode(outputBuilder, "-2");
+ }
+
+ private void addStatusCode(StringBuilder outputBuilder, String value) {
+ outputBuilder.append("INSTRUMENTATION_STATUS_CODE: ");
+ outputBuilder.append(value);
+ outputBuilder.append("\r\n");
+ }
+
+ /**
+ * inject a test string into the result parser.
+ *
+ * @param result
+ */
+ private void injectTestString(String result) {
+ byte[] data = result.getBytes();
+ mParser.addOutput(data, 0, data.length);
+ mParser.flush();
+ }
+
+ private void assertCommonAttributes() {
+ assertEquals(CLASS_NAME, mTestResult.mSuiteName);
+ assertEquals(1, mTestResult.mTestCount);
+ assertEquals(TEST_NAME, mTestResult.mTestName);
+ }
+
+ /**
+ * A specialized test listener that stores a single test events.
+ */
+ private class VerifyingTestResult implements ITestRunListener {
+
+ String mSuiteName;
+ int mTestCount;
+ int mNumTestsRun;
+ String mTestName;
+ long mTestTime;
+ TestFailure mTestStatus;
+ String mTrace;
+ boolean mStopped;
+
+ VerifyingTestResult() {
+ mNumTestsRun = 0;
+ mTestStatus = null;
+ mStopped = false;
+ }
+
+ public void testEnded(TestIdentifier test) {
+ mNumTestsRun++;
+ assertEquals("Unexpected class name", mSuiteName, test.getClassName());
+ assertEquals("Unexpected test ended", mTestName, test.getTestName());
+
+ }
+
+ public void testFailed(TestFailure status, TestIdentifier test, String trace) {
+ mTestStatus = status;
+ mTrace = trace;
+ assertEquals("Unexpected class name", mSuiteName, test.getClassName());
+ assertEquals("Unexpected test ended", mTestName, test.getTestName());
+ }
+
+ public void testRunEnded(long elapsedTime) {
+ mTestTime = elapsedTime;
+
+ }
+
+ public void testRunStarted(int testCount) {
+ mTestCount = testCount;
+ }
+
+ public void testRunStopped(long elapsedTime) {
+ mTestTime = elapsedTime;
+ mStopped = true;
+ }
+
+ public void testStarted(TestIdentifier test) {
+ mSuiteName = test.getClassName();
+ mTestName = test.getTestName();
+ }
+
+ public void testRunFailed(String errorMessage) {
+ // ignored
+ }
+ }
+
+}
diff --git a/ddms/libs/ddmlib/tests/src/com/android/ddmlib/testrunner/RemoteAndroidTestRunnerTest.java b/ddms/libs/ddmlib/tests/src/com/android/ddmlib/testrunner/RemoteAndroidTestRunnerTest.java
new file mode 100644
index 0000000..9acaaf9
--- /dev/null
+++ b/ddms/libs/ddmlib/tests/src/com/android/ddmlib/testrunner/RemoteAndroidTestRunnerTest.java
@@ -0,0 +1,248 @@
+/*
+ * Copyright (C) 2008 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.ddmlib.testrunner;
+
+import com.android.ddmlib.Client;
+import com.android.ddmlib.FileListingService;
+import com.android.ddmlib.IDevice;
+import com.android.ddmlib.IShellOutputReceiver;
+import com.android.ddmlib.RawImage;
+import com.android.ddmlib.SyncService;
+import com.android.ddmlib.Device.DeviceState;
+import com.android.ddmlib.log.LogReceiver;
+
+import junit.framework.TestCase;
+
+import java.io.IOException;
+import java.util.Map;
+
+/**
+ * Tests RemoteAndroidTestRunner.
+ */
+public class RemoteAndroidTestRunnerTest extends TestCase {
+
+ private RemoteAndroidTestRunner mRunner;
+ private MockDevice mMockDevice;
+
+ private static final String TEST_PACKAGE = "com.test";
+ private static final String TEST_RUNNER = "com.test.InstrumentationTestRunner";
+
+ /**
+ * @see junit.framework.TestCase#setUp()
+ */
+ @Override
+ protected void setUp() throws Exception {
+ mMockDevice = new MockDevice();
+ mRunner = new RemoteAndroidTestRunner(TEST_PACKAGE, TEST_RUNNER, mMockDevice);
+ }
+
+ /**
+ * Test the basic case building of the instrumentation runner command with no arguments.
+ */
+ public void testRun() {
+ mRunner.run(new EmptyListener());
+ assertStringsEquals(String.format("am instrument -w -r %s/%s", TEST_PACKAGE, TEST_RUNNER),
+ mMockDevice.getLastShellCommand());
+ }
+
+ /**
+ * Test the building of the instrumentation runner command with log set.
+ */
+ public void testRunWithLog() {
+ mRunner.setLogOnly(true);
+ mRunner.run(new EmptyListener());
+ assertStringsEquals(String.format("am instrument -w -r -e log true %s/%s", TEST_PACKAGE,
+ TEST_RUNNER), mMockDevice.getLastShellCommand());
+ }
+
+ /**
+ * Test the building of the instrumentation runner command with method set.
+ */
+ public void testRunWithMethod() {
+ final String className = "FooTest";
+ final String testName = "fooTest";
+ mRunner.setMethodName(className, testName);
+ mRunner.run(new EmptyListener());
+ assertStringsEquals(String.format("am instrument -w -r -e class %s#%s %s/%s", className,
+ testName, TEST_PACKAGE, TEST_RUNNER), mMockDevice.getLastShellCommand());
+ }
+
+ /**
+ * Test the building of the instrumentation runner command with extra args set.
+ */
+ public void testRunWithExtraArgs() {
+ final String extraArgs = "blah";
+ mRunner.setExtraArgs(extraArgs);
+ mRunner.run(new EmptyListener());
+ assertStringsEquals(String.format("am instrument -w -r %s %s/%s", extraArgs,
+ TEST_PACKAGE, TEST_RUNNER), mMockDevice.getLastShellCommand());
+ }
+
+
+ /**
+ * Assert two strings are equal ignoring whitespace.
+ */
+ private void assertStringsEquals(String str1, String str2) {
+ String strippedStr1 = str1.replaceAll(" ", "");
+ String strippedStr2 = str2.replaceAll(" ", "");
+ assertEquals(strippedStr1, strippedStr2);
+ }
+
+ /**
+ * A dummy device that does nothing except store the provided executed shell command for
+ * later retrieval.
+ */
+ private static class MockDevice implements IDevice {
+
+ private String mLastShellCommand;
+
+ /**
+ * Stores the provided command for later retrieval from getLastShellCommand.
+ */
+ public void executeShellCommand(String command,
+ IShellOutputReceiver receiver) throws IOException {
+ mLastShellCommand = command;
+ }
+
+ /**
+ * Get the last command provided to executeShellCommand.
+ */
+ public String getLastShellCommand() {
+ return mLastShellCommand;
+ }
+
+ public boolean createForward(int localPort, int remotePort) {
+ throw new UnsupportedOperationException();
+ }
+
+ public Client getClient(String applicationName) {
+ throw new UnsupportedOperationException();
+ }
+
+ public String getClientName(int pid) {
+ throw new UnsupportedOperationException();
+ }
+
+ public Client[] getClients() {
+ throw new UnsupportedOperationException();
+ }
+
+ public FileListingService getFileListingService() {
+ throw new UnsupportedOperationException();
+ }
+
+ public Map<String, String> getProperties() {
+ throw new UnsupportedOperationException();
+ }
+
+ public String getProperty(String name) {
+ throw new UnsupportedOperationException();
+ }
+
+ public int getPropertyCount() {
+ throw new UnsupportedOperationException();
+ }
+
+ public RawImage getScreenshot() throws IOException {
+ throw new UnsupportedOperationException();
+ }
+
+ public String getSerialNumber() {
+ throw new UnsupportedOperationException();
+ }
+
+ public DeviceState getState() {
+ throw new UnsupportedOperationException();
+ }
+
+ public SyncService getSyncService() {
+ throw new UnsupportedOperationException();
+ }
+
+ public boolean hasClients() {
+ throw new UnsupportedOperationException();
+ }
+
+ public boolean isBootLoader() {
+ throw new UnsupportedOperationException();
+ }
+
+ public boolean isEmulator() {
+ throw new UnsupportedOperationException();
+ }
+
+ public boolean isOffline() {
+ throw new UnsupportedOperationException();
+ }
+
+ public boolean isOnline() {
+ throw new UnsupportedOperationException();
+ }
+
+ public boolean removeForward(int localPort, int remotePort) {
+ throw new UnsupportedOperationException();
+ }
+
+ public void runEventLogService(LogReceiver receiver) throws IOException {
+ throw new UnsupportedOperationException();
+ }
+
+ public void runLogService(String logname, LogReceiver receiver) throws IOException {
+ throw new UnsupportedOperationException();
+ }
+
+ public String getAvdName() {
+ return "";
+ }
+
+ }
+
+ /**
+ * An empty implementation of ITestRunListener.
+ */
+ private static class EmptyListener implements ITestRunListener {
+
+ public void testEnded(TestIdentifier test) {
+ // ignore
+ }
+
+ public void testFailed(TestFailure status, TestIdentifier test, String trace) {
+ // ignore
+ }
+
+ public void testRunEnded(long elapsedTime) {
+ // ignore
+ }
+
+ public void testRunFailed(String errorMessage) {
+ // ignore
+ }
+
+ public void testRunStarted(int testCount) {
+ // ignore
+ }
+
+ public void testRunStopped(long elapsedTime) {
+ // ignore
+ }
+
+ public void testStarted(TestIdentifier test) {
+ // ignore
+ }
+
+ }
+}