diff --git a/tools/monkeyrunner/jython/test/MonkeyRunner_test.py b/tools/monkeyrunner/jython/test/MonkeyRunner_test.py new file mode 100644 index 000000000..cc4d1f296 --- /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 000000000..2dd0ab440 --- /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 ba1abafbc..aa9623465 100644 --- a/tools/monkeyrunner/src/com/android/monkeyrunner/MonkeyImage.java +++ b/tools/monkeyrunner/src/com/android/monkeyrunner/MonkeyImage.java @@ -22,6 +22,7 @@ import com.android.monkeyrunner.doc.MonkeyRunnerExported; 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 abstract class MonkeyImage { 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 d144780b2..66ceedd3a 100644 --- a/tools/monkeyrunner/src/com/android/monkeyrunner/MonkeyRunner.java +++ b/tools/monkeyrunner/src/com/android/monkeyrunner/MonkeyRunner.java @@ -91,7 +91,7 @@ public class MonkeyRunner { @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 2a1721f89..7e31ea538 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 @@ public class CaptureRawAndConvertedImage { } } + 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 @@ public class CaptureRawAndConvertedImage { // 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 000000000..c5f0d67f7 --- /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... classes) { + TestSuite suite = new TestSuite(); + for (Class 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 cac80f492..9ef180045 100644 Binary files a/tools/monkeyrunner/test/resources/com/android/monkeyrunner/image1.png and b/tools/monkeyrunner/test/resources/com/android/monkeyrunner/image1.png differ diff --git a/tools/monkeyrunner/test/resources/com/android/monkeyrunner/image1.raw b/tools/monkeyrunner/test/resources/com/android/monkeyrunner/image1.raw index a22879393..99ec01303 100644 Binary files a/tools/monkeyrunner/test/resources/com/android/monkeyrunner/image1.raw and b/tools/monkeyrunner/test/resources/com/android/monkeyrunner/image1.raw differ diff --git a/tools/monkeyrunner/test/resources/com/android/monkeyrunner/image2.png b/tools/monkeyrunner/test/resources/com/android/monkeyrunner/image2.png index e6922f1e2..03ff0c1e2 100644 Binary files a/tools/monkeyrunner/test/resources/com/android/monkeyrunner/image2.png and b/tools/monkeyrunner/test/resources/com/android/monkeyrunner/image2.png differ diff --git a/tools/monkeyrunner/test/resources/com/android/monkeyrunner/image2.raw b/tools/monkeyrunner/test/resources/com/android/monkeyrunner/image2.raw index 77333cb6e..06e5b47aa 100644 Binary files a/tools/monkeyrunner/test/resources/com/android/monkeyrunner/image2.raw and b/tools/monkeyrunner/test/resources/com/android/monkeyrunner/image2.raw differ