summaryrefslogtreecommitdiffstats
path: root/browser/components/privatebrowsing/test/browser/browser_privatebrowsing_windowtitle.js
blob: de99b19a4426aa6ff8b6a503c02289e40d93c7dd (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
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

// This test makes sure that the window title changes correctly while switching
// from and to private browsing mode.

"use strict";

add_task(async function test() {
  const testPageURL =
    "http://mochi.test:8888/browser/" +
    "browser/components/privatebrowsing/test/browser/browser_privatebrowsing_windowtitle_page.html";
  requestLongerTimeout(2);

  // initialization of expected titles
  let test_title = "Test title";
  let app_name = document.title;

  // XXX: Bug 1597849 - Dehardcode titles by fetching them from Fluent
  //                    to compare with the actual values.
  const isMacOS = AppConstants.platform == "macosx";

  let pb_postfix = isMacOS ? ` — Private Browsing` : ` Private Browsing`;
  let page_with_title = isMacOS ? test_title : `${test_title}${app_name}`;
  let page_without_title = app_name;
  let about_pb_title = app_name;
  let pb_page_with_title = isMacOS
    ? `${test_title}${pb_postfix}`
    : `${test_title}${app_name}${pb_postfix}`;
  let pb_page_without_title = `${app_name}${pb_postfix}`;
  let pb_about_pb_title = `${app_name}${pb_postfix}`;

  async function testTabTitle(aWindow, url, insidePB, expected_title) {
    let tab = await BrowserTestUtils.openNewForegroundTab(aWindow.gBrowser);
    BrowserTestUtils.startLoadingURIString(tab.linkedBrowser, url);
    await BrowserTestUtils.browserLoaded(tab.linkedBrowser);

    await BrowserTestUtils.waitForCondition(() => {
      return aWindow.document.title === expected_title;
    }, `Window title should be ${expected_title}, got ${aWindow.document.title}`);

    is(
      aWindow.document.title,
      expected_title,
      "The window title for " +
        url +
        " is correct (" +
        (insidePB ? "inside" : "outside") +
        " private browsing mode)"
    );

    let win = aWindow.gBrowser.replaceTabWithWindow(tab);
    await BrowserTestUtils.waitForEvent(win, "load", false);

    await BrowserTestUtils.waitForCondition(() => {
      return win.document.title === expected_title;
    }, `Window title should be ${expected_title}, got ${win.document.title}`);

    is(
      win.document.title,
      expected_title,
      "The window title for " +
        url +
        " detached tab is correct (" +
        (insidePB ? "inside" : "outside") +
        " private browsing mode)"
    );

    await Promise.all([
      BrowserTestUtils.closeWindow(win),
      BrowserTestUtils.closeWindow(aWindow),
    ]);
  }

  function openWin(isPrivate) {
    return BrowserTestUtils.openNewBrowserWindow({ private: isPrivate });
  }
  await testTabTitle(
    await openWin(false),
    "about:blank",
    false,
    page_without_title
  );
  await testTabTitle(await openWin(false), testPageURL, false, page_with_title);
  await testTabTitle(
    await openWin(false),
    "about:privatebrowsing",
    false,
    about_pb_title
  );
  await testTabTitle(
    await openWin(true),
    "about:blank",
    true,
    pb_page_without_title
  );
  await testTabTitle(
    await openWin(true),
    testPageURL,
    true,
    pb_page_with_title
  );
  await testTabTitle(
    await openWin(true),
    "about:privatebrowsing",
    true,
    pb_about_pb_title
  );

  await SpecialPowers.pushPrefEnv({
    set: [["privacy.exposeContentTitleInWindow.pbm", false]],
  });
  await testTabTitle(await openWin(false), testPageURL, false, page_with_title);
  await testTabTitle(
    await openWin(true),
    testPageURL,
    true,
    pb_page_without_title
  );
  await SpecialPowers.pushPrefEnv({
    set: [
      ["privacy.exposeContentTitleInWindow", false],
      ["privacy.exposeContentTitleInWindow.pbm", true],
    ],
  });
  await testTabTitle(
    await openWin(false),
    testPageURL,
    false,
    page_without_title
  );
  // The generic preference set to false is intended to override the PBM one
  await testTabTitle(
    await openWin(true),
    testPageURL,
    true,
    pb_page_without_title
  );
});