summaryrefslogtreecommitdiffstats
path: root/browser/components/privatebrowsing/test/browser/browser_privatebrowsing_about_default_promo.js
blob: dd5c335bc240207e4f4e5b68737a1c39fe3fbd2e (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
141
142
143
144
145
146
147
148
/* 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/. */

const PromoInfo = {
  FOCUS: { enabledPref: "browser.promo.focus.enabled" },
  VPN: { enabledPref: "browser.vpn_promo.enabled" },
  PIN: { enabledPref: "browser.promo.pin.enabled" },
  COOKIE_BANNERS: { enabledPref: "browser.promo.cookiebanners.enabled" },
};

async function resetState() {
  await Promise.all([
    ASRouter.resetMessageState(),
    ASRouter.resetGroupsState(),
    ASRouter.unblockAll(),
  ]);
}

add_setup(async function () {
  registerCleanupFunction(resetState);
  await resetState();
  await SpecialPowers.pushPrefEnv({
    set: [["browser.promo.pin.enabled", false]],
  });
  await ASRouter.onPrefChange();
});

add_task(async function test_privatebrowsing_asrouter_messages_state() {
  await resetState();
  let pinPromoMessage = ASRouter.state.messages.find(
    m => m.id === "PB_NEWTAB_PIN_PROMO"
  );
  Assert.ok(pinPromoMessage, "Pin Promo message found");

  const initialMessages = JSON.parse(JSON.stringify(ASRouter.state.messages));

  let { win, tab } = await openTabAndWaitForRender();

  await SpecialPowers.spawn(tab, [], async function () {
    const promoContainer = content.document.querySelector(".promo");
    ok(promoContainer, "Focus promo is shown");
  });

  Assert.equal(
    ASRouter.state.messages.filter(m => m.id === "PB_NEWTAB_PIN_PROMO").length,
    0,
    "Pin Promo message removed from state when Promotype Pin is disabled"
  );

  for (let msg of initialMessages) {
    let shouldPersist =
      msg.template !== "pb_newtab" ||
      Services.prefs.getBoolPref(
        PromoInfo[msg.content?.promoType]?.enabledPref,
        true
      );
    Assert.equal(
      !!ASRouter.state.messages.find(m => m.id === msg.id),
      shouldPersist,
      shouldPersist
        ? "Message persists in ASRouter state"
        : "Promo message with disabled promoType removed from ASRouter state"
    );
  }
  await BrowserTestUtils.closeWindow(win);
});

add_task(async function test_default_promo() {
  await resetState();

  let { win: win1, tab: tab1 } = await openTabAndWaitForRender();

  await SpecialPowers.spawn(tab1, [], async function () {
    const promoContainer = content.document.querySelector(".promo"); // container which is present if promo is enabled and should show
    const promoHeader = content.document.getElementById("promo-header");

    ok(promoContainer, "Focus promo is shown");
    is(
      promoHeader.textContent,
      "Next-level privacy on mobile",
      "Correct default values are shown"
    );
  });

  let { win: win2 } = await openTabAndWaitForRender();
  let { win: win3 } = await openTabAndWaitForRender();

  let { win: win4, tab: tab4 } = await openTabAndWaitForRender();

  await SpecialPowers.spawn(tab4, [], async function () {
    is(
      content.document.querySelector(".promo button"),
      null,
      "should no longer render the promo after 3 impressions"
    );
  });

  await BrowserTestUtils.closeWindow(win1);
  await BrowserTestUtils.closeWindow(win2);
  await BrowserTestUtils.closeWindow(win3);
  await BrowserTestUtils.closeWindow(win4);
});

// Verify that promos are correctly removed if blocked in another tab.
// See handlePromoOnPreload() in aboutPrivateBrowsing.js
add_task(async function test_remove_promo_from_prerendered_tab_if_blocked() {
  await resetState();

  const { win, tab: tab1 } = await openTabAndWaitForRender();

  await SpecialPowers.spawn(tab1, [], async function () {
    // container which is present if promo message is not blocked
    const promoContainer = content.document.querySelector(".promo");
    ok(promoContainer, "Focus promo is shown in tab 1");
  });

  // Open a new background tab (tab 2) while the promo message is unblocked
  win.openTrustedLinkIn(win.BROWSER_NEW_TAB_URL, "tabshifted");

  // Block the promo in tab 1
  await SpecialPowers.spawn(tab1, [], async function () {
    content.document.getElementById("dismiss-btn").click();
    await ContentTaskUtils.waitForCondition(() => {
      return !content.document.querySelector(".promo");
    }, "The promo container is removed.");
  });

  // Switch to tab 2, invoking the `visibilitychange` handler in
  // handlePromoOnPreload()
  await BrowserTestUtils.switchTab(win.gBrowser, win.gBrowser.tabs[1]);

  // Verify that the promo has now been removed from tab 2
  await SpecialPowers.spawn(
    win.gBrowser.tabs[1].linkedBrowser,
    [],
    // The timing may be weird in Chaos Mode, so wait for it to be removed
    // instead of a single assertion.
    async function () {
      await ContentTaskUtils.waitForCondition(
        () => !content.document.querySelector(".promo"),
        "Focus promo is not shown in a new tab after being dismissed in another tab"
      );
    }
  );

  await BrowserTestUtils.closeWindow(win);
});