summaryrefslogtreecommitdiffstats
path: root/mobile/android/tests/browser/chrome/test_java_addons.html
blob: 7a656671aef74460a02adb831841a05ab6d53598 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
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>