summaryrefslogtreecommitdiffstats
path: root/toolkit/components/cookiebanners/test/browser/browser_bannerClicking_telemetry_querySelector.js
blob: 0778c736a400f1afba6a04492240760450ce1b0a (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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

/**
 * Wait for the result of checkFn to equal expectedValue and run an assertion
 * once the value matches.
 * @param {function} checkFn - Function which returns value to compare to
 * expectedValue.
 * @param {*} expectedValue - Value to compare against checkFn return value.
 * @param {string} message - Assertion / test message.
 */
async function waitForAndAssertEqual(checkFn, expectedValue, message) {
  let checkFnResult;
  await BrowserTestUtils.waitForCondition(() => {
    checkFnResult = checkFn();
    return checkFnResult == expectedValue;
  }, message);
  Assert.equal(checkFnResult, expectedValue, message);
}

/**
 * Test that query selector histogram data is set / unset.
 * @param {*} options
 * @param {boolean} options.hasTopLevelData - Whether there should be telemetry
 * data for top level windows.
 * @param {boolean} options.hasFrameData = Whether there should be telemetry
 * data for sub-frames.
 */
async function assertQuerySelectorTelemetry({ hasTopLevelData, hasFrameData }) {
  // Ensure we have all data from the content process.
  await Services.fog.testFlushAllChildren();

  let runDurationTopLevel = () =>
    Glean.cookieBannersClick.querySelectorRunDurationPerWindowTopLevel.testGetValue() !=
    null;
  let runDurationFrame = () =>
    Glean.cookieBannersClick.querySelectorRunDurationPerWindowFrame.testGetValue() !=
    null;

  let runCountTopLevel = () =>
    Glean.cookieBannersClick.querySelectorRunCountPerWindowTopLevel.testGetValue() !=
    null;
  let runCountFrame = () =>
    Glean.cookieBannersClick.querySelectorRunCountPerWindowFrame.testGetValue() !=
    null;

  let messagePrefix = hasData => `Should${hasData ? "" : " not"} have`;

  await waitForAndAssertEqual(
    runCountTopLevel,
    hasTopLevelData,
    `${messagePrefix(hasTopLevelData)} top-level run count data.`
  );
  await waitForAndAssertEqual(
    runDurationTopLevel,
    hasTopLevelData,
    `${messagePrefix(hasTopLevelData)} top-level run duration data.`
  );

  await waitForAndAssertEqual(
    runDurationFrame,
    hasFrameData,
    `${messagePrefix(hasFrameData)} sub-frame run duration data.`
  );
  await waitForAndAssertEqual(
    runCountFrame,
    hasFrameData,
    `${messagePrefix(hasFrameData)} sub-frame run count data.`
  );
}

add_setup(clickTestSetup);

/**
 * Tests that, when performing cookie banner clicking, query selector
 * performance telemetry is collected.
 */
add_task(async function test_click_query_selector_telemetry() {
  // Clear telemetry.
  Services.fog.testResetFOG();

  // Enable cookie banner handling.
  await SpecialPowers.pushPrefEnv({
    set: [
      ["cookiebanners.service.mode", Ci.nsICookieBannerService.MODE_REJECT],
    ],
  });

  insertTestClickRules();

  info("No telemetry recorded initially.");
  await assertQuerySelectorTelemetry({
    hasFrameData: false,
    hasTopLevelData: false,
  });

  // No opt out rule for the example.org, the banner shouldn't be clicked.
  info("Top level cookie banner with no matching rule.");
  await openPageAndVerify({
    win: window,
    domain: TEST_DOMAIN_B,
    testURL: TEST_PAGE_B,
    visible: true,
    expected: "NoClick",
  });
  await assertQuerySelectorTelemetry({
    hasFrameData: false,
    hasTopLevelData: true,
  });

  Services.cookieBanners.removeAllExecutedRecords(false);

  info("Top level cookie banner with matching rule.");
  await openPageAndVerify({
    win: window,
    domain: TEST_DOMAIN_A,
    testURL: TEST_PAGE_A,
    visible: false,
    expected: "OptOut",
  });
  await assertQuerySelectorTelemetry({
    hasFrameData: false,
    hasTopLevelData: true,
  });

  Services.cookieBanners.removeAllExecutedRecords(false);

  info("Iframe cookie banner with matching rule.");
  await openIframeAndVerify({
    win: window,
    domain: TEST_DOMAIN_A,
    testURL: TEST_PAGE_A,
    visible: false,
    expected: "OptOut",
  });
  await assertQuerySelectorTelemetry({
    hasFrameData: true,
    hasTopLevelData: true,
  });

  Services.cookieBanners.removeAllExecutedRecords(false);

  // Reset test rules and insert only site-specific rules so we can ensure the
  // clicking mechanism is only running for the iframe, not the top level.
  info("Insert test rules without global rules.");
  insertTestClickRules(false);

  // Clear telemetry.
  info("Clear telemetry.");
  Services.fog.testResetFOG();

  info("Iframe cookie banner with matching rule.");
  await openIframeAndVerify({
    win: window,
    domain: TEST_DOMAIN_A,
    testURL: TEST_PAGE_A,
    visible: false,
    expected: "OptOut",
  });
  await assertQuerySelectorTelemetry({
    hasFrameData: true,
    hasTopLevelData: false,
  });

  // Clear telemetry.
  Services.fog.testResetFOG();
});