Add in python unit tests and fix up java unit tests.
Change-Id: I3930946364d5ca8a5d18e4a2b36a7afa298ef2de
diff --git a/tools/monkeyrunner/jython/test/MonkeyRunner_test.py b/tools/monkeyrunner/jython/test/MonkeyRunner_test.py
new file mode 100644
index 0000000..cc4d1f2
--- /dev/null
+++ b/tools/monkeyrunner/jython/test/MonkeyRunner_test.py
@@ -0,0 +1,54 @@
+#!/usr/bin/python2.4
+#
+# Copyright 2010, 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.
+
+"""Test cases for com.android.monkeyrunner.MonkeyRunner."""
+
+import time
+import unittest
+
+from com.android.monkeyrunner import MonkeyRunner
+
+
+class TestMonkeyRunnerArgParsing(unittest.TestCase):
+ """Test ArgParsing for the MonkeyRunner methods."""
+ def testWaitForConnectionNoArgs(self):
+ MonkeyRunner.waitForConnection()
+
+ def testWaitForConnectionSingleArg(self):
+ MonkeyRunner.waitForConnection(2)
+
+ def testWaitForConnectionDoubleArg(self):
+ MonkeyRunner.waitForConnection(2, '*')
+
+ def testWaitForConnectionKeywordArg(self):
+ MonkeyRunner.waitForConnection(timeout=2, deviceId='foo')
+
+ def testWaitForConnectionKeywordArgTooMany(self):
+ try:
+ MonkeyRunner.waitForConnection(timeout=2, deviceId='foo', extra='fail')
+ except TypeError:
+ return
+ self.fail('Should have raised TypeError')
+
+ def testSleep(self):
+ start = time.time()
+ MonkeyRunner.sleep(1.5)
+ end = time.time()
+
+ self.assertTrue(end - start >= 1.5)
+
+if __name__ == '__main__':
+ unittest.main()
diff --git a/tools/monkeyrunner/jython/test/all_tests.py b/tools/monkeyrunner/jython/test/all_tests.py
new file mode 100644
index 0000000..2dd0ab4
--- /dev/null
+++ b/tools/monkeyrunner/jython/test/all_tests.py
@@ -0,0 +1,49 @@
+#!/usr/bin/python2.4
+#
+# Copyright 2010, 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.
+
+"""Test runner to run all the tests in this package."""
+
+import os
+import re
+import sys
+import unittest
+
+
+TESTCASE_RE = re.compile('_test\.py$')
+
+
+def AllTestFilesInDir(path):
+ """Finds all the unit test files in the given path."""
+ return filter(TESTCASE_RE.search, os.listdir(path))
+
+
+def suite(loader=unittest.defaultTestLoader):
+ """Creates the all_tests TestSuite."""
+ script_parent_path = os.path.abspath(os.path.dirname(sys.argv[0]))
+ # Find all the _test.py files in the same directory we are in
+ test_files = AllTestFilesInDir(script_parent_path)
+ # Convert them into module names
+ module_names = [os.path.splitext(f)[0] for f in test_files]
+ # And import them
+ modules = map(__import__, module_names)
+ # And create the test suite for all these modules
+ return unittest.TestSuite([loader.loadTestsFromModule(m) for m in modules])
+
+if __name__ == '__main__':
+ result = unittest.TextTestRunner().run(suite())
+ if not result.wasSuccessful():
+ # On failure return an error code
+ sys.exit(1)
diff --git a/tools/monkeyrunner/src/com/android/monkeyrunner/MonkeyImage.java b/tools/monkeyrunner/src/com/android/monkeyrunner/MonkeyImage.java
index ba1abaf..aa96234 100644
--- a/tools/monkeyrunner/src/com/android/monkeyrunner/MonkeyImage.java
+++ b/tools/monkeyrunner/src/com/android/monkeyrunner/MonkeyImage.java
@@ -22,6 +22,7 @@
import org.python.core.ArgParser;
import org.python.core.PyObject;
+import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
@@ -85,8 +86,16 @@
public boolean writeToFile(String path, String format) {
BufferedImage image = createBufferedImage();
+
+ // Convert the image to ARGB so ImageIO writes it out nicely
+ BufferedImage argb = new BufferedImage(image.getWidth(), image.getHeight(),
+ BufferedImage.TYPE_INT_ARGB);
+ Graphics g = argb.createGraphics();
+ g.drawImage(image, 0, 0, null);
+ g.dispose();
+
try {
- ImageIO.write(image, format, new File(path));
+ ImageIO.write(argb, format, new File(path));
} catch (IOException e) {
return false;
}
diff --git a/tools/monkeyrunner/src/com/android/monkeyrunner/MonkeyRunner.java b/tools/monkeyrunner/src/com/android/monkeyrunner/MonkeyRunner.java
index d144780..66ceedd 100644
--- a/tools/monkeyrunner/src/com/android/monkeyrunner/MonkeyRunner.java
+++ b/tools/monkeyrunner/src/com/android/monkeyrunner/MonkeyRunner.java
@@ -91,7 +91,7 @@
@MonkeyRunnerExported(doc = "Simple help command to dump the MonkeyRunner supported " +
"commands",
returns = "The help text")
- public static String help(PyObject[] args, String[] kws) {
+ public static String help(PyObject[] args, String[] kws) {
ArgParser ap = JythonUtils.createArgParser(args, kws);
Preconditions.checkNotNull(ap);
diff --git a/tools/monkeyrunner/src/com/android/monkeyrunner/adb/image/CaptureRawAndConvertedImage.java b/tools/monkeyrunner/src/com/android/monkeyrunner/adb/image/CaptureRawAndConvertedImage.java
index 2a1721f..7e31ea5 100644
--- a/tools/monkeyrunner/src/com/android/monkeyrunner/adb/image/CaptureRawAndConvertedImage.java
+++ b/tools/monkeyrunner/src/com/android/monkeyrunner/adb/image/CaptureRawAndConvertedImage.java
@@ -87,6 +87,12 @@
}
}
+ private static void writeOutImage(RawImage screenshot, String name) throws IOException {
+ ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(name));
+ out.writeObject(new MonkeyRunnerRawImage(screenshot));
+ out.close();
+ }
+
public static void main(String[] args) throws IOException {
AdbBackend backend = new AdbBackend();
MonkeyDevice device = backend.waitForConnection();
@@ -95,11 +101,6 @@
// write out to a file
snapshot.writeToFile("output.png", "png");
writeOutImage(snapshot.getRawImage(), "output.raw");
- }
-
- private static void writeOutImage(RawImage screenshot, String name) throws IOException {
- ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(name));
- out.writeObject(new MonkeyRunnerRawImage(screenshot));
- out.close();
+ System.exit(0);
}
}
diff --git a/tools/monkeyrunner/test/com/android/monkeyrunner/AllTests.java b/tools/monkeyrunner/test/com/android/monkeyrunner/AllTests.java
new file mode 100644
index 0000000..c5f0d67
--- /dev/null
+++ b/tools/monkeyrunner/test/com/android/monkeyrunner/AllTests.java
@@ -0,0 +1,45 @@
+/*
+ * Copyright (C) 2010 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.monkeyrunner;
+
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestResult;
+import junit.framework.TestSuite;
+import junit.textui.TestRunner;
+
+/**
+ * Test suite to run all the tests for MonkeyRunner.
+ */
+public class AllTests {
+ public static Test suite(Class<? extends TestCase>... classes) {
+ TestSuite suite = new TestSuite();
+ for (Class<? extends TestCase> clz : classes) {
+ suite.addTestSuite(clz);
+ }
+ return suite;
+ }
+
+ public static void main(String args[]) {
+ TestRunner tr = new TestRunner();
+ TestResult result = tr.doRun(AllTests.suite(ImageUtilsTest.class, JythonUtilsTest.class));
+ if (result.wasSuccessful()) {
+ System.exit(0);
+ } else {
+ System.exit(1);
+ }
+ }
+}
diff --git a/tools/monkeyrunner/test/resources/com/android/monkeyrunner/image1.png b/tools/monkeyrunner/test/resources/com/android/monkeyrunner/image1.png
index cac80f4..9ef1800 100644
--- a/tools/monkeyrunner/test/resources/com/android/monkeyrunner/image1.png
+++ b/tools/monkeyrunner/test/resources/com/android/monkeyrunner/image1.png
Binary files differ
diff --git a/tools/monkeyrunner/test/resources/com/android/monkeyrunner/image1.raw b/tools/monkeyrunner/test/resources/com/android/monkeyrunner/image1.raw
index a228793..99ec013 100644
--- a/tools/monkeyrunner/test/resources/com/android/monkeyrunner/image1.raw
+++ b/tools/monkeyrunner/test/resources/com/android/monkeyrunner/image1.raw
Binary files differ
diff --git a/tools/monkeyrunner/test/resources/com/android/monkeyrunner/image2.png b/tools/monkeyrunner/test/resources/com/android/monkeyrunner/image2.png
index e6922f1..03ff0c1 100644
--- a/tools/monkeyrunner/test/resources/com/android/monkeyrunner/image2.png
+++ b/tools/monkeyrunner/test/resources/com/android/monkeyrunner/image2.png
Binary files differ
diff --git a/tools/monkeyrunner/test/resources/com/android/monkeyrunner/image2.raw b/tools/monkeyrunner/test/resources/com/android/monkeyrunner/image2.raw
index 77333cb..06e5b47 100644
--- a/tools/monkeyrunner/test/resources/com/android/monkeyrunner/image2.raw
+++ b/tools/monkeyrunner/test/resources/com/android/monkeyrunner/image2.raw
Binary files differ