From 9c9126f41e18d5fd7871dc97ed5d9a7cd08968b5 Mon Sep 17 00:00:00 2001 From: Vicamo Yang Date: Thu, 20 Mar 2014 13:01:25 +0800 Subject: [PATCH] Bug 985537: add runEmulatorShell to Marionette. r=mdas --- .../marionette/client/marionette/emulator.py | 4 ++++ .../client/marionette/marionette.py | 22 +++++++++++++++++-- .../marionette/tests/unit/test_emulator.py | 8 +++++++ .../marionette/marionette-frame-manager.js | 2 ++ testing/marionette/marionette-listener.js | 8 +++++++ testing/marionette/marionette-server.js | 12 ++++++++++ testing/marionette/marionette-simpletest.js | 7 +++++- 7 files changed, 60 insertions(+), 3 deletions(-) diff --git a/testing/marionette/client/marionette/emulator.py b/testing/marionette/client/marionette/emulator.py index 3483180c38c..86aab4bdc8f 100644 --- a/testing/marionette/client/marionette/emulator.py +++ b/testing/marionette/client/marionette/emulator.py @@ -225,6 +225,10 @@ class Emulator(object): self._get_telnet_response() return self._get_telnet_response(command) + def _run_shell(self, args): + args.insert(0, 'shell') + return self._run_adb(args).split('\r\n') + def close(self): if self.is_running and self._emulator_launched: self.proc.kill() diff --git a/testing/marionette/client/marionette/marionette.py b/testing/marionette/client/marionette/marionette.py index 1cb2df72e73..d54485818d2 100644 --- a/testing/marionette/client/marionette/marionette.py +++ b/testing/marionette/client/marionette/marionette.py @@ -608,8 +608,16 @@ class Marionette(object): # Process any emulator commands that are sent from a script # while it's executing. - while response.get("emulator_cmd"): - response = self._handle_emulator_cmd(response) + while True: + if response.get("emulator_cmd"): + response = self._handle_emulator_cmd(response) + continue; + + if response.get("emulator_shell"): + response = self._handle_emulator_shell(response) + continue; + + break; if response_key in response: return response[response_key] @@ -626,6 +634,16 @@ class Marionette(object): "id": response.get("id"), "result": result}) + def _handle_emulator_shell(self, response): + args = response.get("emulator_shell") + if not isinstance(args, list) or not self.emulator: + raise MarionetteException( + "No emulator in this test to run shell command against") + result = self.emulator._run_shell(args) + return self.client.send({"name": "emulatorCmdResult", + "id": response.get("id"), + "result": result}) + def _handle_error(self, response): if 'error' in response and isinstance(response['error'], dict): status = response['error'].get('status', 500) diff --git a/testing/marionette/client/marionette/tests/unit/test_emulator.py b/testing/marionette/client/marionette/tests/unit/test_emulator.py index 91cb2742797..2c615c0a05f 100644 --- a/testing/marionette/client/marionette/tests/unit/test_emulator.py +++ b/testing/marionette/client/marionette/tests/unit/test_emulator.py @@ -17,6 +17,14 @@ class TestEmulatorContent(MarionetteTestCase): """); self.assertEqual(result, expected) + def test_emulator_shell(self): + self.marionette.set_script_timeout(10000) + expected = ["Hello World!", ""] + result = self.marionette.execute_async_script(""" + runEmulatorShell(["echo", "Hello World!"], marionetteScriptFinished) + """); + self.assertEqual(result, expected) + def test_emulator_order(self): self.marionette.set_script_timeout(10000) self.assertRaises(MarionetteException, diff --git a/testing/marionette/marionette-frame-manager.js b/testing/marionette/marionette-frame-manager.js index f97817b80f2..8dcab97d001 100644 --- a/testing/marionette/marionette-frame-manager.js +++ b/testing/marionette/marionette-frame-manager.js @@ -163,6 +163,7 @@ FrameManager.prototype = { messageManager.addWeakMessageListener("Marionette:shareData", this.server); messageManager.addWeakMessageListener("Marionette:register", this.server); messageManager.addWeakMessageListener("Marionette:runEmulatorCmd", this.server); + messageManager.addWeakMessageListener("Marionette:runEmulatorShell", this.server); messageManager.addWeakMessageListener("Marionette:switchToModalOrigin", this.server); messageManager.addWeakMessageListener("Marionette:switchToFrame", this.server); messageManager.addWeakMessageListener("Marionette:switchedToFrame", this.server); @@ -190,6 +191,7 @@ FrameManager.prototype = { messageManager.removeWeakMessageListener("Marionette:shareData", this.server); messageManager.removeWeakMessageListener("Marionette:register", this.server); messageManager.removeWeakMessageListener("Marionette:runEmulatorCmd", this.server); + messageManager.removeWeakMessageListener("Marionette:runEmulatorShell", this.server); messageManager.removeWeakMessageListener("Marionette:switchToFrame", this.server); messageManager.removeWeakMessageListener("Marionette:switchedToFrame", this.server); messageManager.removeWeakMessageListener("MarionetteFrame:handleModal", this); diff --git a/testing/marionette/marionette-listener.js b/testing/marionette/marionette-listener.js index dbda4fce3a1..f8c2e11c078 100644 --- a/testing/marionette/marionette-listener.js +++ b/testing/marionette/marionette-listener.js @@ -2019,6 +2019,14 @@ function runEmulatorCmd(cmd, callback) { _emu_cb_id += 1; } +function runEmulatorShell(args, callback) { + if (callback) { + _emu_cbs[_emu_cb_id] = callback; + } + sendAsyncMessage("Marionette:runEmulatorShell", {emulator_shell: args, id: _emu_cb_id}); + _emu_cb_id += 1; +} + function emulatorCmdResult(msg) { let message = msg.json; if (!sandbox) { diff --git a/testing/marionette/marionette-server.js b/testing/marionette/marionette-server.js index a2c7b402891..45f9c2b6e68 100644 --- a/testing/marionette/marionette-server.js +++ b/testing/marionette/marionette-server.js @@ -2128,6 +2128,17 @@ MarionetteServerConnection.prototype = { this._emu_cb_id += 1; }, + runEmulatorShell: function runEmulatorShell(args, callback) { + if (callback) { + if (!this._emu_cbs) { + this._emu_cbs = {}; + } + this._emu_cbs[this._emu_cb_id] = callback; + } + this.sendToClient({emulator_shell: args, id: this._emu_cb_id}, -1); + this._emu_cb_id += 1; + }, + emulatorCmdResult: function emulatorCmdResult(message) { if (this.context != "chrome") { this.sendAsync("emulatorCmdResult", message, -1); @@ -2325,6 +2336,7 @@ MarionetteServerConnection.prototype = { } break; case "Marionette:runEmulatorCmd": + case "Marionette:runEmulatorShell": this.sendToClient(message.json, -1); break; case "Marionette:switchToFrame": diff --git a/testing/marionette/marionette-simpletest.js b/testing/marionette/marionette-simpletest.js index 3a87ebd3d3e..2ae9032ee82 100644 --- a/testing/marionette/marionette-simpletest.js +++ b/testing/marionette/marionette-simpletest.js @@ -22,7 +22,7 @@ this.Marionette = function Marionette(scope, window, context, logObj, timeout, Marionette.prototype = { exports: ['ok', 'is', 'isnot', 'log', 'getLogs', 'generate_results', 'waitFor', - 'runEmulatorCmd', 'TEST_PASS', 'TEST_KNOWN_FAIL', + 'runEmulatorCmd', 'runEmulatorShell', 'TEST_PASS', 'TEST_KNOWN_FAIL', 'TEST_UNEXPECTED_FAIL'], ok: function Marionette__ok(condition, name, passString, failString, diag) { @@ -163,5 +163,10 @@ Marionette.prototype = { this.scope.runEmulatorCmd(cmd, callback); }, + runEmulatorShell: function runEmulatorShell(args, callback) { + this.heartbeatCallback(); + this.scope.runEmulatorShell(args, callback); + }, + };