Example #1
0
    def test_local_port_scanning(self):
        with patch('uiautomator.next_local_port') as next_local_port:
            self.Adb.return_value.forward_list.return_value = []
            next_local_port.return_value = 1234
            self.assertEqual(AutomatorServer("abcd", None).local_port,
                             next_local_port.return_value)

            next_local_port.return_value = 14321
            self.Adb.return_value.forward_list.return_value = Exception("error")
            self.assertEqual(AutomatorServer("abcd", None).local_port,
                             next_local_port.return_value)
Example #2
0
 def test_local_port_forwarded(self):
     self.Adb.return_value.forward_list.return_value = [
         ("1234", "tcp:1001", "tcp:9009"),
         ("1234", "tcp:1000", "tcp:9008")
     ]
     self.Adb.return_value.device_serial.return_value = "1234"
     self.assertEqual(AutomatorServer("1234").local_port, 1000)
Example #3
0
 def test_start_ping(self):
     with patch("uiautomator.JsonRPCClient") as JsonRPCClient:
         JsonRPCClient.return_value.ping.return_value = "pong"
         server = AutomatorServer()
         server.adb = MagicMock()
         server.adb.forward.return_value = 0
         self.assertEqual(server.ping(), "pong")
Example #4
0
    def test_auto_start(self):
        try:
            import requests
            # import urllib2
        except ImportError:
            raise
            # import urllib.request as urllib2
        with patch("uiautomator.JsonRPCMethod") as JsonRPCMethod:
            # returns = [urllib2.URLError("error"), "ok"]
            returns = [requests.exceptions.ConnectionError("error"), "ok"]

            def side_effect():
                result = returns.pop(0)
                if isinstance(result, Exception):
                    raise result
                return result

            JsonRPCMethod.return_value.side_effect = side_effect
            server = AutomatorServer()
            server.start = MagicMock()
            server.stop = MagicMock()
            self.assertEqual("ok", server.jsonrpc.any_method())
            server.start.assert_called_once_with(timeout=30)
        with patch("uiautomator.JsonRPCMethod") as JsonRPCMethod:
            returns = [JsonRPCError(-32000 - 1, "error msg"), "ok"]

            def side_effect():
                result = returns.pop(0)
                if isinstance(result, Exception):
                    raise result
                return result

            JsonRPCMethod.return_value.side_effect = side_effect
            server = AutomatorServer()
            server.start = MagicMock()
            server.stop = MagicMock()
            self.assertEqual("ok", server.jsonrpc.any_method())
            server.start.assert_called_once_with()
        with patch("uiautomator.JsonRPCMethod") as JsonRPCMethod:
            JsonRPCMethod.return_value.side_effect = JsonRPCError(
                -32000 - 2, "error msg")
            server = AutomatorServer()
            server.start = MagicMock()
            server.stop = MagicMock()
            with self.assertRaises(JsonRPCError):
                server.jsonrpc.any_method()
Example #5
0
 def test_push(self):
     jars = ["bundle.jar", "uiautomator-stub.jar"]
     server = AutomatorServer()
     server.adb = MagicMock()
     self.assertEqual(set(server.push()), set(jars))
     for args in server.adb.cmd.call_args_list:
         self.assertEqual(args[0][0], "push")
         self.assertEqual(args[0][2], "/data/local/tmp/")
Example #6
0
 def test_auto_start(self):
     with patch("uiautomator.JsonRPCClient") as JsonRPCClient:
         JsonRPCClient.ping.return_value = None
         server = AutomatorServer()
         server.start = MagicMock()
         server.stop = MagicMock()
         server.jsonrpc
         server.start.assert_called_once_with()
Example #7
0
 def test_start_success(self):
     server = AutomatorServer()
     server.push = MagicMock()
     server.push.return_value = ["bundle.jar", "uiautomator-stub.jar"]
     server.ping = MagicMock()
     server.ping.return_value = "pong"
     server.adb = MagicMock()
     server.start()
     server.adb.cmd.assert_called_with('shell', 'uiautomator', 'runtest', 'bundle.jar', 'uiautomator-stub.jar', '-c', 'com.github.uiautomatorstub.Stub')
Example #8
0
 def test_start_error(self):
     server = AutomatorServer()
     server.push = MagicMock()
     server.push.return_value = ["bundle.jar", "uiautomator-stub.jar"]
     server.ping = MagicMock()
     server.ping.return_value = None
     server.adb = MagicMock()
     with patch("time.sleep"):
         with self.assertRaises(IOError):
             server.start()
    def test_screenshot(self):
        server = AutomatorServer()
        server.sdk_version = MagicMock()
        server.sdk_version.return_value = 17
        self.assertEqual(server.screenshot(), None)

        server.sdk_version.return_value = 18
        self.urlopen.return_value.read = MagicMock()
        self.urlopen.return_value.read.return_value = b"123456"
        self.assertEqual(server.screenshot(), b"123456")
        self.assertEqual(server.screenshot("/tmp/test.txt"), "/tmp/test.txt")
Example #10
0
 def test_download_and_push(self):
     jars = ["bundle.jar", "uiautomator-stub.jar"]
     with patch("os.path.exists") as exists:
         with patch("os.stat") as stat:
             server = AutomatorServer()
             server.adb = MagicMock()
             exists.return_value = True
             stat.st_size = 1024
             self.assertEqual(set(server.download_and_push()), set(jars))
             for args in server.adb.cmd.call_args_list:
                 self.assertEqual(args[0][0], "push")
                 self.assertEqual(args[0][2], "/data/local/tmp/")
Example #11
0
 def test_download_and_push_download(self):
     jars = ["bundle.jar", "uiautomator-stub.jar"]
     with patch("os.path.exists") as exists:
         with patch("os.mkdir") as mkdir:
             with patch("%s.open" % open.__class__.__module__,
                        mock_open(),
                        create=True) as m_open:
                 server = AutomatorServer()
                 server.adb = MagicMock()
                 exists.return_value = False
                 self.assertEqual(set(server.download_and_push()),
                                  set(jars))
                 self.assertEqual(len(m_open.call_args_list), len(jars))
Example #12
0
    def test_stop_started_server(self):
        server = AutomatorServer()
        server.adb = MagicMock()
        server.uiautomator_process = process = MagicMock()
        process.poll.return_value = None
        server.stop()
        process.wait.assert_called_once_with()

        server.uiautomator_process = process = MagicMock()
        process.poll.return_value = None
        self.urlopen.side_effect = IOError("error")
        server.stop()
        process.kill.assert_called_once_with()
Example #13
0
 def test_stop(self):
     results = [
         b"USER     PID   PPID  VSIZE  RSS     WCHAN    PC         NAME\n\rsystem    372   126   635596 104808 ffffffff 00000000 S uiautomator",
         b"USER     PID   PPID  VSIZE  RSS     WCHAN    PC         NAME\r\nsystem    372   126   635596 104808 ffffffff 00000000 S uiautomator",
         b"USER     PID   PPID  VSIZE  RSS     WCHAN    PC         NAME\nsystem    372   126   635596 104808 ffffffff 00000000 S uiautomator",
         b"USER     PID   PPID  VSIZE  RSS     WCHAN    PC         NAME\rsystem    372   126   635596 104808 ffffffff 00000000 S uiautomator"
     ]
     for r in results:
         server = AutomatorServer()
         server.adb = MagicMock()
         server.adb.cmd.return_value.communicate.return_value = (r, "")
         server.stop()
         self.assertEqual(server.adb.cmd.call_args_list,
                          [call("shell", "ps", "-C", "uiautomator"), call("shell", "kill", "-9", "372")])
Example #14
0
    def test_start_success_over_api_18(self):
        server = AutomatorServer()
        server.push = MagicMock()
        server.push.return_value = ["bundle.jar", "uiautomator-stub.jar"]

        server.ping = MagicMock()
        server.ping.return_value = "pong"

        server.sdk_version = MagicMock()
        server.sdk_version.return_value = 20

        server.adb = MagicMock()
        server.start()
        server.adb.cmd.assert_called_with(
            "shell", "am", "instrument", "-w",
            "com.github.uiautomator.test/android.support.test.runner.AndroidJUnitRunner"
        )
Example #15
0
 def test_device_port(self):
     self.assertEqual(AutomatorServer().device_port, 9008)
Example #16
0
 def test_local_port(self):
     self.assertEqual(AutomatorServer("1234", 9010).local_port, 9010)
     self.Adb.assert_called_once_with(serial="1234",
                                      adb_server_host=None,
                                      adb_server_port=None)
Example #17
0
 def test_start_ping_none(self):
     with patch("uiautomator.JsonRPCClient") as JsonRPCClient:
         JsonRPCClient.return_value.ping.side_effect = Exception("error")
         server = AutomatorServer()
         self.assertEqual(server.ping(), None)
Example #18
0
"""start and quit rpc server on device.
"""
from uiautomator import AutomatorServer

r = AutomatorServer()
print "RPC server is going to stop ..."
r.stop()

if not r.alive:
    print "Sever is stopped."
else:
    print "Somthing wrong"