Merge "Add in python unit tests and fix up java unit tests."
This commit is contained in:
54
tools/monkeyrunner/jython/test/MonkeyRunner_test.py
Normal file
54
tools/monkeyrunner/jython/test/MonkeyRunner_test.py
Normal file
@@ -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()
|
||||||
49
tools/monkeyrunner/jython/test/all_tests.py
Normal file
49
tools/monkeyrunner/jython/test/all_tests.py
Normal file
@@ -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)
|
||||||
@@ -22,6 +22,7 @@ import com.android.monkeyrunner.doc.MonkeyRunnerExported;
|
|||||||
import org.python.core.ArgParser;
|
import org.python.core.ArgParser;
|
||||||
import org.python.core.PyObject;
|
import org.python.core.PyObject;
|
||||||
|
|
||||||
|
import java.awt.Graphics;
|
||||||
import java.awt.image.BufferedImage;
|
import java.awt.image.BufferedImage;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
@@ -85,8 +86,16 @@ public abstract class MonkeyImage {
|
|||||||
|
|
||||||
public boolean writeToFile(String path, String format) {
|
public boolean writeToFile(String path, String format) {
|
||||||
BufferedImage image = createBufferedImage();
|
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 {
|
try {
|
||||||
ImageIO.write(image, format, new File(path));
|
ImageIO.write(argb, format, new File(path));
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -91,7 +91,7 @@ public class MonkeyRunner {
|
|||||||
@MonkeyRunnerExported(doc = "Simple help command to dump the MonkeyRunner supported " +
|
@MonkeyRunnerExported(doc = "Simple help command to dump the MonkeyRunner supported " +
|
||||||
"commands",
|
"commands",
|
||||||
returns = "The help text")
|
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);
|
ArgParser ap = JythonUtils.createArgParser(args, kws);
|
||||||
Preconditions.checkNotNull(ap);
|
Preconditions.checkNotNull(ap);
|
||||||
|
|
||||||
|
|||||||
@@ -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 {
|
public static void main(String[] args) throws IOException {
|
||||||
AdbBackend backend = new AdbBackend();
|
AdbBackend backend = new AdbBackend();
|
||||||
MonkeyDevice device = backend.waitForConnection();
|
MonkeyDevice device = backend.waitForConnection();
|
||||||
@@ -95,11 +101,6 @@ public class CaptureRawAndConvertedImage {
|
|||||||
// write out to a file
|
// write out to a file
|
||||||
snapshot.writeToFile("output.png", "png");
|
snapshot.writeToFile("output.png", "png");
|
||||||
writeOutImage(snapshot.getRawImage(), "output.raw");
|
writeOutImage(snapshot.getRawImage(), "output.raw");
|
||||||
}
|
System.exit(0);
|
||||||
|
|
||||||
private static void writeOutImage(RawImage screenshot, String name) throws IOException {
|
|
||||||
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(name));
|
|
||||||
out.writeObject(new MonkeyRunnerRawImage(screenshot));
|
|
||||||
out.close();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Binary file not shown.
|
Before Width: | Height: | Size: 78 KiB After Width: | Height: | Size: 180 KiB |
Binary file not shown.
Binary file not shown.
|
Before Width: | Height: | Size: 385 KiB After Width: | Height: | Size: 80 KiB |
Binary file not shown.
Reference in New Issue
Block a user