summaryrefslogtreecommitdiffstats
path: root/browser/components/privatebrowsing/test/browser/browser_privatebrowsing_windowtitle.js
blob: 3f71dd2586a8fbac881cfe12e9ccfa4d334e49a4 (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
/* 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.loadURIString(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
  );
});