summaryrefslogtreecommitdiffstats
path: root/toolkit/components/extensions/test/mochitest/test_ext_test.html
blob: fef31e0e217b7a28a2920f24d2cc8ee4745718cd (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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
<!DOCTYPE HTML>
<html>
<head>
  <title>Testing test</title>
  <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="head.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css">
</head>
<body>

<script>
"use strict";

function loadExtensionAndInterceptTest(extensionData) {
  let results = [];
  let testResolve;
  let testDone = new Promise(resolve => { testResolve = resolve; });
  let handler = {
    testResult(...result) {
      result.pop();
      results.push(result);
      SimpleTest.info(`Received test result: ${JSON.stringify(result)}`);
    },

    testMessage(msg, ...args) {
      results.push(["test-message", msg, ...args]);
      SimpleTest.info(`Received message: ${msg} ${JSON.stringify(args)}`);
      if (msg === "This is the last browser.test call") {
        testResolve();
      }
    },
  };
  let extension = SpecialPowers.loadExtension(extensionData, handler);
  SimpleTest.registerCleanupFunction(() => {
    if (extension.state == "pending" || extension.state == "running") {
      SimpleTest.ok(false, "Extension left running at test shutdown");
      return extension.unload();
    } else if (extension.state == "unloading") {
      SimpleTest.ok(false, "Extension not fully unloaded at test shutdown");
    }
  });
  extension.awaitResults = () => testDone.then(() => results);
  return extension;
}

function testScript() {
  // Note: The result of these browser.test calls are intercepted by the test.
  // See verifyTestResults for the expectations of each browser.test call.
  browser.test.notifyPass("dot notifyPass");
  browser.test.notifyFail("dot notifyFail");
  browser.test.log("dot log");
  browser.test.fail("dot fail");
  browser.test.succeed("dot succeed");
  browser.test.assertTrue(true);
  browser.test.assertFalse(false);
  browser.test.assertEq("", "");

  let obj = {};
  let arr = [];
  let dom = document.createElement("body");
  browser.test.assertTrue(obj, "Object truthy");
  browser.test.assertTrue(arr, "Array truthy");
  browser.test.assertTrue(dom, "Element truthy");
  browser.test.assertTrue(true, "True truthy");
  browser.test.assertTrue(false, "False truthy");
  browser.test.assertTrue(null, "Null truthy");
  browser.test.assertTrue(undefined, "Void truthy");
  browser.test.assertTrue(false, document.createElement("html"));

  browser.test.assertFalse(obj, "Object falsey");
  browser.test.assertFalse(arr, "Array falsey");
  browser.test.assertFalse(dom, "Element falsey");
  browser.test.assertFalse(true, "True falsey");
  browser.test.assertFalse(false, "False falsey");
  browser.test.assertFalse(null, "Null falsey");
  browser.test.assertFalse(undefined, "Void falsey");
  browser.test.assertFalse(true, document.createElement("head"));

  browser.test.assertEq(obj, obj, "Object equality");
  browser.test.assertEq(arr, arr, "Array equality");
  browser.test.assertEq(dom, dom, "Element equality");
  browser.test.assertEq(null, null, "Null equality");
  browser.test.assertEq(undefined, undefined, "Void equality");

  browser.test.assertEq({}, {}, "Object reference ineqality");
  browser.test.assertEq([], [], "Array reference ineqality");
  browser.test.assertEq(dom, document.createElement("body"), "Element ineqality");
  browser.test.assertEq(null, undefined, "Null and void ineqality");
  browser.test.assertEq(true, false, document.createElement("div"));

  obj = {
    toString() {
      return "Dynamic toString forbidden";
    },
  };
  browser.test.assertEq(obj, obj, "obj with dynamic toString()");
  browser.test.sendMessage("Ran test at", location.protocol);
  browser.test.sendMessage("This is the last browser.test call");
}

function verifyTestResults(results, shortName, expectedProtocol) {
  let expectations = [
    ["test-done", true, "dot notifyPass"],
    ["test-done", false, "dot notifyFail"],
    ["test-log", true, "dot log"],
    ["test-result", false, "dot fail"],
    ["test-result", true, "dot succeed"],
    ["test-result", true, "undefined"],
    ["test-result", true, "undefined"],
    ["test-eq", true, "undefined", "", ""],

    ["test-result", true, "Object truthy"],
    ["test-result", true, "Array truthy"],
    ["test-result", true, "Element truthy"],
    ["test-result", true, "True truthy"],
    ["test-result", false, "False truthy"],
    ["test-result", false, "Null truthy"],
    ["test-result", false, "Void truthy"],
    ["test-result", false, "[object HTMLHtmlElement]"],

    ["test-result", false, "Object falsey"],
    ["test-result", false, "Array falsey"],
    ["test-result", false, "Element falsey"],
    ["test-result", false, "True falsey"],
    ["test-result", true, "False falsey"],
    ["test-result", true, "Null falsey"],
    ["test-result", true, "Void falsey"],
    ["test-result", false, "[object HTMLHeadElement]"],

    ["test-eq", true, "Object equality", "[object Object]", "[object Object]"],
    ["test-eq", true, "Array equality", "", ""],
    ["test-eq", true, "Element equality", "[object HTMLBodyElement]", "[object HTMLBodyElement]"],
    ["test-eq", true, "Null equality", "null", "null"],
    ["test-eq", true, "Void equality", "undefined", "undefined"],

    ["test-eq", false, "Object reference ineqality", "[object Object]", "[object Object] (different)"],
    ["test-eq", false, "Array reference ineqality", "", " (different)"],
    ["test-eq", false, "Element ineqality", "[object HTMLBodyElement]", "[object HTMLBodyElement] (different)"],
    ["test-eq", false, "Null and void ineqality", "null", "undefined"],
    ["test-eq", false, "[object HTMLDivElement]", "true", "false"],

    ["test-eq", true, "obj with dynamic toString()", "[object Object]", "[object Object]"],

    ["test-message", "Ran test at", expectedProtocol],
    ["test-message", "This is the last browser.test call"],
  ];

  expectations.forEach((expectation, i) => {
    let msg = expectation.slice(2).join(" - ");
    isDeeply(results[i], expectation, `${shortName} (${msg})`);
  });
  is(results[expectations.length], undefined, "No more results");
}

add_task(function* test_test_in_background() {
  let extensionData = {
    background: `(${testScript})()`,
  };

  let extension = loadExtensionAndInterceptTest(extensionData);
  yield extension.startup();
  let results = yield extension.awaitResults();
  verifyTestResults(results, "background page", "moz-extension:");
  yield extension.unload();
});

add_task(function* test_test_in_content_script() {
  let extensionData = {
    manifest: {
      content_scripts: [{
        matches: ["http://mochi.test/*/file_sample.html"],
        js: ["contentscript.js"],
      }],
    },
    files: {
      "contentscript.js": `(${testScript})()`,
    },
  };

  let extension = loadExtensionAndInterceptTest(extensionData);
  yield extension.startup();
  let win = window.open("file_sample.html");
  let results = yield extension.awaitResults();
  win.close();
  verifyTestResults(results, "content script", "http:");
  yield extension.unload();
});
</script>
</body>
</html>