summaryrefslogtreecommitdiffstats
path: root/devtools/client/framework/test/browser_toolbox_window_title_changes.js
blob: 176b0b0f659b977ca5171bdc2f6ab2dc956dacc9 (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
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

requestLongerTimeout(5);

var { Toolbox } = require("resource://devtools/client/framework/toolbox.js");

const NAME_1 = "";
const NAME_2 = "Toolbox test for title update";
const NAME_3 = NAME_2;
const NAME_4 = "Toolbox test for another title update";

const URL_1 = "data:text/plain;charset=UTF-8,abcde";
const URL_2 =
  URL_ROOT_ORG_SSL + "browser_toolbox_window_title_changes_page.html";
const URL_3 =
  URL_ROOT_COM_SSL + "browser_toolbox_window_title_changes_page.html";
const URL_4 = `https://example.com/document-builder.sjs?html=<head><title>${NAME_4}</title></head><h1>Hello`;

add_task(async function test() {
  await addTab(URL_1);

  const tab = gBrowser.selectedTab;
  let toolbox = await gDevTools.showToolboxForTab(tab, {
    hostType: Toolbox.HostType.BOTTOM,
  });
  await toolbox.selectTool("webconsole");

  info("Undock toolbox and check title");
  // We have to first switch the host in order to spawn the new top level window
  // on which we are going to listen from title change event
  await toolbox.switchHost(Toolbox.HostType.WINDOW);
  await checkTitle(NAME_1, URL_1, "toolbox undocked");

  info("switch to different tool and check title again");
  await toolbox.selectTool("jsdebugger");
  await checkTitle(NAME_1, URL_1, "tool changed");

  info("navigate to different local url and check title");

  await navigateTo(URL_2);
  info("wait for title change");
  await checkTitle(NAME_2, URL_2, "url changed");

  info("navigate to a real url and check title");
  await navigateTo(URL_3);

  info("wait for title change");
  await checkTitle(NAME_3, URL_3, "url changed");

  info("navigate to another page on the same domain");
  await navigateTo(URL_4);
  await checkTitle(NAME_4, URL_4, "title changed");

  info(
    "destroy toolbox, create new one hosted in a window (with a different tool id), and check title"
  );
  // Give the tools a chance to handle the navigation event before
  // destroying the toolbox.
  await new Promise(resolve => executeSoon(resolve));
  await toolbox.destroy();

  // After destroying the toolbox, open a new one.
  toolbox = await gDevTools.showToolboxForTab(tab, {
    hostType: Toolbox.HostType.WINDOW,
  });
  toolbox.selectTool("webconsole");
  await checkTitle(NAME_4, URL_4, "toolbox destroyed and recreated");

  info("clean up");
  await toolbox.destroy();
  gBrowser.removeCurrentTab();
  Services.prefs.clearUserPref("devtools.toolbox.host");
  Services.prefs.clearUserPref("devtools.toolbox.selectedTool");
});

function getExpectedTitle(name, url) {
  if (name) {
    return `Developer Tools — ${name}${url}`;
  }
  return `Developer Tools — ${url}`;
}

async function checkTitle(name, url, context) {
  info("Check title - " + context);
  await waitFor(
    () => getToolboxWindowTitle() === getExpectedTitle(name, url),
    `Didn't get the expected title ("${getExpectedTitle(name, url)}"`,
    200,
    50
  );
  const expectedTitle = getExpectedTitle(name, url);
  is(getToolboxWindowTitle(), expectedTitle, context);
}

function getToolboxWindowTitle() {
  return Services.wm.getMostRecentWindow("devtools:toolbox").document.title;
}