summaryrefslogtreecommitdiffstats
path: root/browser/components/extensions/test/browser/browser_ext_devtools_network_targetSwitch.js
blob: 6e8873a561195468ade1e86a100521bc64faed1e (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
/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
/* vim: set sts=2 sw=2 et tw=80: */
"use strict";

// Like most of the mochitest-browser devtools test,
// on debug test machine, it takes about 50s to run the test.
requestLongerTimeout(4);

loadTestSubscript("head_devtools.js");

const MAIN_PROCESS_PAGE = "about:robots";
const CONTENT_PROCESS_PAGE = "http://example.com/";

async function testOnNavigatedEvent(uri, tab, toolbox, extension) {
  const onNavigated = extension.awaitMessage("network-onNavigated");
  const onSwitched = toolbox.commands.targetCommand.once("switched-target");
  BrowserTestUtils.loadURIString(tab.linkedBrowser, uri);
  await onSwitched;
  const result = await onNavigated;
  is(result, uri, "devtools.network.onNavigated works correctly");
}

/**
 * This test checks whether network works well even target-switching happens.
 */
add_task(async () => {
  const tab = await BrowserTestUtils.openNewForegroundTab(
    gBrowser,
    CONTENT_PROCESS_PAGE
  );

  async function devtools_page() {
    browser.devtools.network.onNavigated.addListener(url => {
      browser.test.sendMessage("network-onNavigated", url);
    });

    browser.test.sendMessage("devtools-page-loaded");
  }

  const extension = ExtensionTestUtils.loadExtension({
    manifest: {
      devtools_page: "devtools_page.html",
    },
    files: {
      "devtools_page.html": `<!DOCTYPE html>
        <html>
          <head>
            <meta charset="utf-8">
          </head>
          <body>
            <script src="devtools_page.js"></script>
          </body>
        </html>`,
      "devtools_page.js": devtools_page,
    },
  });
  await extension.startup();

  info("Open the developer toolbox");
  const toolbox = await openToolboxForTab(tab);

  info("Wait the devtools page load");
  await extension.awaitMessage("devtools-page-loaded");

  info("Navigate to a page running on main process");
  await testOnNavigatedEvent(MAIN_PROCESS_PAGE, tab, toolbox, extension);

  info("Return to a page running on content process again");
  await testOnNavigatedEvent(CONTENT_PROCESS_PAGE, tab, toolbox, extension);

  await extension.unload();
  await closeToolboxForTab(tab);
  BrowserTestUtils.removeTab(tab);
});