summaryrefslogtreecommitdiffstats
path: root/devtools/server/tests/chrome/test_webextension-addon-debugging-reload.html
blob: 420cb0df5e949e388b04cffec504be8eb380c876 (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
<!DOCTYPE HTML>
<html>
<!--
Bug 1302702 - Test connect to a webextension addon
-->
<head>
  <meta charset="utf-8">
  <title>Mozilla Bug</title>
  <script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
  <script src="webextension-helpers.js"></script>
  <link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css">
</head>
<body>
<pre id="test">
<script type="application/javascript">
"use strict";

// NOTE: This test installs the webextension addon using the addon manager, so that
// it can be reloaded using the same actor RDP method used by the developer tools.
add_task(async function test_webextension_addon_debugging_reload() {
  const addonID = "test-webext-debugging-reload@test.mozilla.com";
  const addonFile = generateWebExtensionXPI({
    manifest: {
      applications: {
        gecko: {id: addonID},
      },
      background: { scripts: ["background.js"] },
    },
    files: {
      "background.js": function() {
        console.log("background page loaded");
      },
    },
  });

  const {addon} = await promiseInstallFile(addonFile);
  await promiseWebExtensionStartup();

  const addonTarget = await attachAddon(addonID);
  ok(addonTarget, "Got an addon target");

  const matchBackgroundPageFrame = (data) => {
    if (data.frames) {
      const frameFound = data.frames.filter((frame) => {
        return frame.url && frame.url.endsWith("_generated_background_page.html");
      }).pop();

      return !!frameFound;
    }

    return false;
  };

  const matchFrameSelected = (data) => {
    return "selected" in data;
  };

  // Test the target addon actor reload behavior.

  let waitFramesUpdated = waitForFramesUpdated(addonTarget, matchBackgroundPageFrame);
  let collectFrameSelected = collectFrameUpdates(addonTarget, matchFrameSelected);

  is(ExtensionParent.DebugUtils.debugBrowserPromises.size, 1,
     "The expected number of debug browser has been created by the addon actor");

  info("Reloading the target addon");
  reloadAddon(addonTarget, addonID);
  await promiseWebExtensionStartup();

  is(ExtensionParent.DebugUtils.debugBrowserPromises.size, 1,
     "The number of debug browser has not been changed after an addon reload");

  let frames = await waitFramesUpdated;
  const selectedFrame = collectFrameSelected().pop();

  is(selectedFrame.selected, frames[0].id, "The new background page has been selected");
  is(addonTarget.url, frames[0].url,
     "The addon target has the url of the selected frame");

  // Test the target addon actor once reloaded twice.

  waitFramesUpdated = waitForFramesUpdated(addonTarget, matchBackgroundPageFrame);
  collectFrameSelected = collectFrameUpdates(addonTarget, matchFrameSelected);

  info("Reloading the target addon again");
  reloadAddon(addonTarget, addonID);
  await promiseWebExtensionStartup();

  frames = await waitFramesUpdated;
  const newSelectedFrame = collectFrameSelected().pop();

  ok(newSelectedFrame !== selectedFrame,
     "The new selected frame is different from the previous on");
  is(newSelectedFrame.selected, frames[0].id,
     "The new background page has been selected on the second reload");
  is(addonTarget.url, frames[0].url,
     "The addon target has the url of the selected frame");

  // Expect the target to be automatically closed when the addon
  // is finally uninstalled.

  const {client} = addonTarget;
  const waitDebuggingClientClosed = new Promise(resolve => {
    addonTarget.once("close", resolve);
  });

  const waitShutdown = promiseWebExtensionShutdown();
  addon.uninstall();
  await waitShutdown;

  info("Waiting the addon target to be closed on addon uninstall");
  await waitDebuggingClientClosed;

  // Debugging client has to be closed explicitly when
  // the target has been created as remote.
  await client.close();
});

</script>
</pre>
</body>
</html>