summaryrefslogtreecommitdiffstats
path: root/toolkit/components/webextensions/test/mochitest/test_ext_background_teardown.html
blob: bb6b2e9709c9fb3b77b611db81f0ba08f78b5611 (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
<!DOCTYPE HTML>
<html>
<head>
  <title>Test for background script teardown</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="/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";

add_task(function* test_background_reload_and_unload() {
  function background() {
    browser.test.onMessage.addListener(msg => {
      browser.test.assertEq("reload-background", msg);
      location.reload();
    });
    browser.test.sendMessage("background-url", location.href);
  }

  let chromeScript = SpecialPowers.loadChromeScript(
      SimpleTest.getTestFileURL("file_teardown_test.js"));
  yield chromeScript.promiseOneMessage("chromescript-startup");

  let extensionData = {
    background,
  };

  let extension = ExtensionTestUtils.loadExtension(extensionData);
  function* getContextEvents() {
    chromeScript.sendAsyncMessage("get-context-events");
    let contextEvents = yield chromeScript.promiseOneMessage("context-events");
    return contextEvents.filter(event => event.extensionId == extension.id);
  }
  yield extension.startup();
  let backgroundUrl = yield extension.awaitMessage("background-url");

  let contextEvents = yield* getContextEvents();
  is(contextEvents.length, 1,
      "ExtensionContext state change after loading an extension");
  is(contextEvents[0].eventType, "load");
  is(contextEvents[0].url, backgroundUrl,
      "The ExtensionContext should be the background page");

  extension.sendMessage("reload-background");
  yield extension.awaitMessage("background-url");

  contextEvents = yield* getContextEvents();
  is(contextEvents.length, 2,
      "ExtensionContext state changes after reloading the background page");
  is(contextEvents[0].eventType, "unload",
      "Unload ExtensionContext of background page");
  is(contextEvents[0].url, backgroundUrl, "ExtensionContext URL = background");
  is(contextEvents[1].eventType, "load",
      "Create new ExtensionContext for background page");
  is(contextEvents[1].url, backgroundUrl, "ExtensionContext URL = background");
  yield extension.unload();

  contextEvents = yield* getContextEvents();
  is(contextEvents.length, 1,
      "ExtensionContext state change after unloading the extension");
  is(contextEvents[0].eventType, "unload",
      "Unload ExtensionContext for background page after extension unloads");
  is(contextEvents[0].url, backgroundUrl, "ExtensionContext URL = background");

  chromeScript.sendAsyncMessage("cleanup");
  chromeScript.destroy();
});
</script>

</body>
</html>