summaryrefslogtreecommitdiffstats
path: root/toolkit/components/cookiebanners/test/browser/browser_cookiebanner_webconsole.js
blob: 20b1d490a8d6d0b69993236d94c6c3ca48215c67 (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
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

const { SiteDataTestUtils } = ChromeUtils.importESModule(
  "resource://testing-common/SiteDataTestUtils.sys.mjs"
);

/**
 * Registers a console listener and waits for the cookie banner handled message
 * to be logged.
 * @returns {Promise} - Promise which resolves once the message has been logged.
 */
async function waitForCookieBannerHandledConsoleMsg() {
  let msg;
  let checkFn = msg =>
    msg && msg.match(/handled a cookie banner on behalf of the user./);
  await new Promise(resolve => {
    SpecialPowers.registerConsoleListener(consoleMsg => {
      msg = consoleMsg.message;
      if (checkFn(msg)) {
        resolve();
      }
    });
  });
  SpecialPowers.postConsoleSentinel();

  ok(checkFn(msg), "Observed cookie banner handled console message.");
}

add_setup(clickTestSetup);

/**
 * Tests that when we handle a banner via clicking a message is logged to the
 * website console.
 */
add_task(async function test_banner_clicking_log_web_console() {
  await SpecialPowers.pushPrefEnv({
    set: [
      [
        "cookiebanners.service.mode",
        Ci.nsICookieBannerService.MODE_REJECT_OR_ACCEPT,
      ],
    ],
  });

  insertTestClickRules();

  let consoleMsgPromise = waitForCookieBannerHandledConsoleMsg();

  // Clear the executed records before testing.
  Services.cookieBanners.removeAllExecutedRecords(false);

  info("Handle the banner via click and wait for console message to appear.");
  await openPageAndVerify({
    domain: TEST_DOMAIN_A,
    testURL: TEST_PAGE_A,
    visible: false,
    expected: "OptOut",
  });

  await consoleMsgPromise;
});

/**
 * Tests that when we handle a banner via cookie injection a message is logged
 * to the website console.
 */
add_task(async function test_cookie_injection_log_web_console() {
  await SpecialPowers.pushPrefEnv({
    set: [
      [
        "cookiebanners.service.mode",
        Ci.nsICookieBannerService.MODE_REJECT_OR_ACCEPT,
      ],
      ["cookiebanners.cookieInjector.enabled", true],
    ],
  });

  insertTestCookieRules();

  let consoleMsgPromise = waitForCookieBannerHandledConsoleMsg();

  // Clear the executed records before testing.
  Services.cookieBanners.removeAllExecutedRecords(false);

  info(
    "Handle the banner via cookie injection and wait for console message to appear."
  );
  let tab = await BrowserTestUtils.openNewForegroundTab(
    gBrowser,
    TEST_ORIGIN_A
  );
  BrowserTestUtils.removeTab(tab);

  await consoleMsgPromise;

  // Clear injected cookies.
  await SiteDataTestUtils.clear();
});