summaryrefslogtreecommitdiffstats
path: root/toolkit/components/extensions/test/mochitest/test_ext_sendmessage_no_receiver.html
blob: 96af6558e1bd63e8e352f7948759b035e1ba2f1a (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
<!DOCTYPE html>
<html>
<head>
  <title>WebExtension test</title>
  <meta charset="utf-8">
  <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  <script type="text/javascript" src="/tests/SimpleTest/SpawnTask.js"></script>
  <script type="text/javascript" src="/tests/SimpleTest/ExtensionTestUtils.js"></script>
  <script type="text/javascript" src="head.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css">
</head>
<body>
<script>
"use strict";

function loadContentScriptExtension(contentScript) {
  let extensionData = {
    manifest: {
      "content_scripts": [{
        "js": ["contentscript.js"],
        "matches": ["http://mochi.test/*/file_sample.html"],
      }],
    },
    files: {
      "contentscript.js": contentScript,
    },
  };
  return ExtensionTestUtils.loadExtension(extensionData);
}

add_task(function* test_content_script_sendMessage_without_listener() {
  async function contentScript() {
    await browser.test.assertRejects(
      browser.runtime.sendMessage("msg"),
      "Could not establish connection. Receiving end does not exist.");

    browser.test.notifyPass("sendMessage callback was invoked");
  }

  let extension = loadContentScriptExtension(contentScript);
  yield extension.startup();

  let win = window.open("file_sample.html");
  yield extension.awaitFinish("sendMessage callback was invoked");
  win.close();

  yield extension.unload();
});

add_task(function* test_content_script_chrome_sendMessage_without_listener() {
  function contentScript() {
    /* globals chrome */
    browser.test.assertEq(null, chrome.runtime.lastError, "no lastError before call");
    let retval = chrome.runtime.sendMessage("msg");
    browser.test.assertEq(null, chrome.runtime.lastError, "no lastError after call");
    // TODO(robwu): Fix the implementation and uncomment the next expectation.
    // When content script APIs are schema-based (bugzil.la/1287007) this bug will be fixed for free.
    // browser.test.assertEq(undefined, retval, "return value of chrome.runtime.sendMessage without callback");
    browser.test.assertTrue(retval instanceof Promise, "TODO: chrome.runtime.sendMessage should return undefined, not a promise");

    let isAsyncCall = false;
    retval = chrome.runtime.sendMessage("msg", reply => {
      browser.test.assertEq(undefined, reply, "no reply");
      browser.test.assertTrue(isAsyncCall, "chrome.runtime.sendMessage's callback must be called asynchronously");
      browser.test.assertEq(undefined, retval, "return value of chrome.runtime.sendMessage with callback");
      browser.test.assertEq("Could not establish connection. Receiving end does not exist.", chrome.runtime.lastError.message);
      browser.test.notifyPass("finished chrome.runtime.sendMessage");
    });
    isAsyncCall = true;
  }

  let extension = loadContentScriptExtension(contentScript);
  yield extension.startup();

  let win = window.open("file_sample.html");
  yield extension.awaitFinish("finished chrome.runtime.sendMessage");
  win.close();

  yield extension.unload();
});
</script>
</body>
</html>