summaryrefslogtreecommitdiffstats
path: root/toolkit/components/cookiebanners/test/browser/browser_bannerClicking_globalRules_telemetry.js
blob: 6d9e96914d5b7a2989f1d45af25f78f00259aacc (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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

add_setup(clickTestSetup);

async function verifyDetectedCMPTelemetry(expected) {
  // Ensure we have all data from the content process.
  await Services.fog.testFlushAllChildren();

  let LABELS = [
    "trustarcbar",
    "quantcast",
    "borlabs",
    "consentmanagernet",
    "cookiebot",
    "complianz",
    "onetrust",
    "didomi",
    "sourcepoint",
  ];

  let testMetricState = doAssert => {
    for (let label of LABELS) {
      let expectedValue = expected[label] ?? null;
      if (doAssert) {
        is(
          Glean.cookieBannersCmp.detectedCmp[label].testGetValue(),
          expectedValue,
          `Counter for label '${label}' has correct state.`
        );
      } else if (
        Glean.cookieBannersCmp.detectedCmp[label].testGetValue() !==
        expectedValue
      ) {
        return false;
      }
    }

    return true;
  };

  // Wait for the labeled counter to match the expected state. Returns greedy on
  // mismatch.
  try {
    await TestUtils.waitForCondition(
      testMetricState,
      "Waiting for cookieBannersClick.result metric to match."
    );
  } finally {
    // Test again but this time with assertions and test all labels.
    testMetricState(true);
  }
}

async function verifyRatioTelemetry(expected) {
  // Ensure we have all data from the content process.
  await Services.fog.testFlushAllChildren();

  let cmpMetric = Glean.cookieBannersCmp.ratioHandledByCmpRule;

  let testMetricState = doAssert => {
    let cmpValue = cmpMetric.testGetValue();

    if (!cmpValue) {
      return false;
    }

    if (!doAssert) {
      return (
        cmpValue.numerator == expected.cmp.numerator &&
        cmpValue.denominator == expected.cmp.denominator
      );
    }

    is(
      cmpValue.numerator,
      expected.cmp.numerator,
      "The numerator of rateHandledByCmpRule is expected"
    );
    is(
      cmpValue.denominator,
      expected.cmp.denominator,
      "The denominator of rateHandledByCmpRule is expected"
    );

    return true;
  };

  // Wait for matching the expected state.
  try {
    await TestUtils.waitForCondition(
      testMetricState,
      "Waiting for metric to match."
    );
  } finally {
    // Test again but this time with assertions and test all labels.
    testMetricState(true);
  }
}

add_task(async function test_cmp_telemetry() {
  await SpecialPowers.pushPrefEnv({
    set: [
      ["cookiebanners.service.mode", Ci.nsICookieBannerService.MODE_REJECT],
      ["cookiebanners.service.enableGlobalRules", true],
      ["cookiebanners.bannerClicking.maxTriesPerSiteAndSession", 0],
    ],
  });

  info("Clearing existing rules");
  Services.cookieBanners.resetRules(false);

  info("Inserting global test rules.");

  info(
    "Add global ruleA with a predefined identifier. This rule will handle the banner."
  );
  let ruleA = Cc["@mozilla.org/cookie-banner-rule;1"].createInstance(
    Ci.nsICookieBannerRule
  );
  ruleA.id = "trustarcbar";
  ruleA.domains = [];
  ruleA.addClickRule(
    "div#banner",
    false,
    Ci.nsIClickRule.RUN_TOP,
    null,
    "button#optOut",
    "button#optIn"
  );
  Services.cookieBanners.insertRule(ruleA);

  info("Add global ruleC with another predefined identifier.");
  let ruleC = Cc["@mozilla.org/cookie-banner-rule;1"].createInstance(
    Ci.nsICookieBannerRule
  );
  ruleC.id = "quantcast";
  ruleC.domains = [];
  ruleC.addClickRule(
    "div#banner",
    false,
    Ci.nsIClickRule.RUN_TOP,
    null,
    "button#nonExistingOptOut",
    "button#nonExistingOptIn"
  );
  Services.cookieBanners.insertRule(ruleC);

  info("Open a test page and verify the telemetry");
  await openPageAndVerify({
    domain: TEST_DOMAIN_A,
    testURL: TEST_PAGE_A,
    visible: false,
    expected: "OptOut",
  });

  await verifyRatioTelemetry({
    cmp: { numerator: 1, denominator: 1 },
  });
  await verifyDetectedCMPTelemetry({
    trustarcbar: 1,
    quantcast: 1,
  });

  info("Open the test page again and verify if the telemetry gets updated");
  await openPageAndVerify({
    domain: TEST_DOMAIN_A,
    testURL: TEST_PAGE_A,
    visible: false,
    expected: "OptOut",
  });

  await verifyRatioTelemetry({
    cmp: { numerator: 2, denominator: 2 },
  });
  await verifyDetectedCMPTelemetry({
    trustarcbar: 2,
    quantcast: 2,
  });

  info("Add domain specific rule which also targets the existing banner.");
  let ruleDomain = Cc["@mozilla.org/cookie-banner-rule;1"].createInstance(
    Ci.nsICookieBannerRule
  );
  ruleDomain.id = genUUID();
  ruleDomain.domains = [TEST_DOMAIN_A];
  ruleDomain.addClickRule(
    "div#banner",
    false,
    Ci.nsIClickRule.RUN_TOP,
    null,
    "button#optOut",
    "button#optIn"
  );
  Services.cookieBanners.insertRule(ruleDomain);

  info("Open the test page and now the site-specific rule will handle it");
  await openPageAndVerify({
    domain: TEST_DOMAIN_A,
    testURL: TEST_PAGE_A,
    visible: false,
    expected: "OptOut",
  });

  info("Verify that the CMP telemetry doesn't get updated.");
  await verifyDetectedCMPTelemetry({
    trustarcbar: 2,
    quantcast: 2,
  });

  info(
    "Verify the handled ratio telemetry for site-specific rule gets updated."
  );
  await verifyRatioTelemetry({
    cmp: { numerator: 2, denominator: 3 },
  });

  // Clear Telemetry
  await Services.fog.testFlushAllChildren();
  Services.fog.testResetFOG();
});