diff options
Diffstat (limited to 'mobile/android/tests/browser/chrome/test_java_addons.html')
-rw-r--r-- | mobile/android/tests/browser/chrome/test_java_addons.html | 118 |
1 files changed, 118 insertions, 0 deletions
diff --git a/mobile/android/tests/browser/chrome/test_java_addons.html b/mobile/android/tests/browser/chrome/test_java_addons.html new file mode 100644 index 000000000..7a656671a --- /dev/null +++ b/mobile/android/tests/browser/chrome/test_java_addons.html @@ -0,0 +1,118 @@ +<!DOCTYPE HTML> +<html> +<!-- +https://bugzilla.mozilla.org/show_bug.cgi?id=1168407 +Migrated from Robocop https://bugzilla.mozilla.org/show_bug.cgi?id=1184186 +--> +<head> + <meta charset="utf-8"> + <title>Test for Bug 1168407</title> + <script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script> + <script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/SpawnTask.js"></script> + <link rel="stylesheet" type="text/css" href="chrome://global/skin"/> + <link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css"/> + <script type="application/javascript;version=1.7"> + + const { classes: Cc, interfaces: Ci, utils: Cu } = Components; + + Cu.import("resource://gre/modules/JavaAddonManager.jsm"); /*global JavaAddonManager */ + Cu.import("resource://gre/modules/Promise.jsm"); /*global Promise */ + Cu.import("resource://gre/modules/Task.jsm"); /*global Task */ + + const DEX_FILE = "chrome://roboextender/content/javaaddons-test.apk"; + const CLASS = "org.mozilla.javaaddons.test.JavaAddonV1"; + + const MESSAGE = "JavaAddon:V1"; + + add_task(function* testFailureCases() { + info("Loading Java Addon from non-existent class."); + let gotError1 = yield JavaAddonManager.classInstanceFromFile(CLASS + "GARBAGE", DEX_FILE) + .then((result) => false) + .catch((error) => true); + is(gotError1, true, "got expected error for non-existent class"); + + info("Loading Java Addon from non-existent DEX file."); + let gotError2 = yield JavaAddonManager.classInstanceFromFile(CLASS, DEX_FILE + "GARBAGE") + .then((result) => false) + .catch((error) => true); + is(gotError2, true, "got expected error for non-existent DEX file"); + }); + + // Make a request to a dynamically loaded Java Addon; wait for a response. + // Then expect the add-on to make a request; respond. + // Then expect the add-on to make a second request; use it to verify the response to the first request. + add_task(function* testJavaAddonV1() { + info("Loading Java Addon from: " + DEX_FILE); + + let javaAddon = yield JavaAddonManager.classInstanceFromFile(CLASS, DEX_FILE); + isnot(javaAddon, null, "addon is not null"); + isnot(javaAddon._guid, null, "guid is not null"); + is(javaAddon._classname, CLASS, "got expected class"); + is(javaAddon._loaded, true, "addon is loaded"); + + let messagePromise = Promise.defer(); + var count = 0; + function listener(data) { + info("Got request initiated from Java Addon: " + data + ", " + typeof(data) + ", " + JSON.stringify(data)); + count += 1; + messagePromise.resolve(); // It's okay to resolve before returning: we'll wait on the verification promise no matter what. + return { + outputStringKey: "inputStringKey=" + data.inputStringKey, + outputIntKey: data.inputIntKey - 1 + }; + } + javaAddon.addListener(listener, "JavaAddon:V1:Request"); + + let verifierPromise = Promise.defer(); + function verifier(data) { + info("Got verification request initiated from Java Addon: " + data + ", " + typeof(data) + ", " + JSON.stringify(data)); + // These values are from the test Java Addon, after being processed by the :Request listener above. + is(data.outputStringKey, "inputStringKey=raw", "got expected outputStringKey"); + is(data.outputIntKey, 2, "got expected outputIntKey"); + verifierPromise.resolve(); + return {}; + } + javaAddon.addListener(verifier, "JavaAddon:V1:VerificationRequest"); + + let message = {type: MESSAGE, inputStringKey: "test", inputIntKey: 5}; + info("Sending request to Java Addon: " + JSON.stringify(message)); + let output = yield javaAddon.sendRequestForResult(message); + + info("Got response from Java Addon: " + output + ", " + typeof(output) + ", " + JSON.stringify(output)); + is(output.outputStringKey, "inputStringKey=test", "got expected outputStringKey"); + is(output.outputIntKey, 6, "got expected outputIntKey"); + + // We don't worry about timing out: the harness will (very much later) + // kill us if we don't see the expected messages. + + info("Waiting for request initiated from Java Addon."); + yield messagePromise.promise; + is(count, 1, "count is 1"); + + info("Send request for result 2 for request initiated from Java Addon."); + + // The JavaAddon should have removed its listener, so we shouldn't get a response and count should stay the same. + let gotError = yield javaAddon.sendRequestForResult(message) + .then((result) => false) + .catch((error) => true); + is(gotError, true, "got expected error"); + is(count, 1, "count is still 1"); + + info("Waiting for verification request initiated from Java Addon."); + yield verifierPromise.promise; + }); + + </script> +</head> +<body> +<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1168407">Mozilla Bug 1168407</a> +<br> +<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1184186">Migrated from Robocop testJavaAddons</a> +<p id="display"></p> +<div id="content" style="display: none"> + +</div> +<pre id="test"> +</pre> +</body> +</html> |