summaryrefslogtreecommitdiffstats
path: root/toolkit/components/cookiebanners/test/browser/head.js
blob: d88de7e20bd218aa238c127a2568437b1a22577a (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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

const TEST_DOMAIN_A = "example.com";
const TEST_DOMAIN_B = "example.org";
const TEST_DOMAIN_C = "example.net";

const TEST_ORIGIN_A = "https://" + TEST_DOMAIN_A;
const TEST_ORIGIN_B = "https://" + TEST_DOMAIN_B;
const TEST_ORIGIN_C = "https://" + TEST_DOMAIN_C;

const TEST_PATH = getRootDirectory(gTestPath).replace(
  "chrome://mochitests/content",
  ""
);

const TEST_PAGE_A = TEST_ORIGIN_A + TEST_PATH + "file_banner.html";
const TEST_PAGE_B = TEST_ORIGIN_B + TEST_PATH + "file_banner.html";
// Page C has a different banner element ID than A and B.
const TEST_PAGE_C = TEST_ORIGIN_C + TEST_PATH + "file_banner_b.html";

function genUUID() {
  return Services.uuid.generateUUID().number.slice(1, -1);
}

/**
 * Common setup function for cookie banner handling tests.
 */
async function testSetup() {
  await SpecialPowers.pushPrefEnv({
    set: [
      // Enable debug logging.
      ["cookiebanners.listService.logLevel", "Debug"],
      // Avoid importing rules from RemoteSettings. They may interfere with test
      // rules / assertions.
      ["cookiebanners.listService.testSkipRemoteSettings", true],
    ],
  });

  // Reset GLEAN (FOG) telemetry to avoid data bleeding over from other tests.
  Services.fog.testResetFOG();

  registerCleanupFunction(() => {
    Services.prefs.clearUserPref("cookiebanners.service.mode");
    Services.prefs.clearUserPref("cookiebanners.service.mode.privateBrowsing");
    if (Services.cookieBanners.isEnabled) {
      // Restore original rules.
      Services.cookieBanners.resetRules(true);
    }
  });
}

/**
 * Setup function for click tests.
 */
async function clickTestSetup() {
  await testSetup();

  await SpecialPowers.pushPrefEnv({
    set: [
      // Enable debug logging.
      ["cookiebanners.bannerClicking.logLevel", "Debug"],
      ["cookiebanners.bannerClicking.testing", true],
      ["cookiebanners.bannerClicking.timeout", 500],
      ["cookiebanners.bannerClicking.enabled", true],
      ["cookiebanners.cookieInjector.enabled", false],
    ],
  });
}

/**
 * Setup function for cookie injector tests.
 */
async function cookieInjectorTestSetup() {
  await testSetup();

  await SpecialPowers.pushPrefEnv({
    set: [
      ["cookiebanners.cookieInjector.enabled", true],
      // Required to dispatch cookiebanner events.
      ["cookiebanners.bannerClicking.enabled", true],
    ],
  });
}

/**
 * A helper function returns a promise which resolves when the banner clicking
 * is finished for the given domain.
 *
 * @param {String} domain the domain that should run the banner clicking.
 */
function promiseBannerClickingFinish(domain) {
  return new Promise(resolve => {
    Services.obs.addObserver(function observer(subject, topic, data) {
      if (data != domain) {
        return;
      }

      Services.obs.removeObserver(
        observer,
        "cookie-banner-test-clicking-finish"
      );
      resolve();
    }, "cookie-banner-test-clicking-finish");
  });
}

/**
 * A helper function to verify the banner state of the given browsingContext.
 *
 * @param {BrowsingContext} bc - the browsing context
 * @param {boolean} visible - if the banner should be visible.
 * @param {boolean} expected - the expected banner click state.
 * @param {string} [bannerId] - id of the cookie banner element.
 */
async function verifyBannerState(bc, visible, expected, bannerId = "banner") {
  info("Verify the cookie banner state.");

  await SpecialPowers.spawn(
    bc,
    [visible, expected, bannerId],
    (visible, expected, bannerId) => {
      let banner = content.document.getElementById(bannerId);

      is(
        banner.checkVisibility({
          checkOpacity: true,
          checkVisibilityCSS: true,
        }),
        visible,
        `The banner element should be ${visible ? "visible" : "hidden"}`
      );

      let result = content.document.getElementById("result");

      is(result.textContent, expected, "The build click state is correct.");
    }
  );
}

/**
 * A helper function to open the test page and verify the banner state.
 *
 * @param {Window} [win] - the chrome window object.
 * @param {String} domain - the domain of the testing page.
 * @param {String} testURL - the url of the testing page.
 * @param {boolean} visible - if the banner should be visible.
 * @param {boolean} expected - the expected banner click state.
 * @param {string} [bannerId] - id of the cookie banner element.
 * @param {boolean} [keepTabOpen] - whether to leave the tab open after the test
 * function completed.
 */
async function openPageAndVerify({
  win = window,
  domain,
  testURL,
  visible,
  expected,
  bannerId = "banner",
  keepTabOpen = false,
}) {
  info(`Opening ${testURL}`);

  let promise = promiseBannerClickingFinish(domain);

  let tab = await BrowserTestUtils.openNewForegroundTab(win.gBrowser, testURL);

  await promise;

  await verifyBannerState(tab.linkedBrowser, visible, expected, bannerId);

  if (!keepTabOpen) {
    BrowserTestUtils.removeTab(tab);
  }
}

/**
 * A helper function to open the test page in an iframe and verify the banner
 * state in the iframe.
 *
 * @param {Window} win - the chrome window object.
 * @param {String} domain - the domain of the testing iframe page.
 * @param {String} testURL - the url of the testing iframe page.
 * @param {boolean} visible - if the banner should be visible.
 * @param {boolean} expected - the expected banner click state.
 */
async function openIframeAndVerify({
  win,
  domain,
  testURL,
  visible,
  expected,
}) {
  let tab = await BrowserTestUtils.openNewForegroundTab(
    win.gBrowser,
    TEST_ORIGIN_C
  );

  let promise = promiseBannerClickingFinish(domain);

  let iframeBC = await SpecialPowers.spawn(
    tab.linkedBrowser,
    [testURL],
    async testURL => {
      let iframe = content.document.createElement("iframe");
      iframe.src = testURL;
      content.document.body.appendChild(iframe);
      await ContentTaskUtils.waitForEvent(iframe, "load");

      return iframe.browsingContext;
    }
  );

  await promise;
  await verifyBannerState(iframeBC, visible, expected);

  BrowserTestUtils.removeTab(tab);
}

/**
 * A helper function to insert testing rules.
 */
function insertTestClickRules() {
  info("Clearing existing rules");
  Services.cookieBanners.resetRules(false);

  info("Inserting test rules.");

  info("Add opt-out click rule for DOMAIN_A.");
  let ruleA = Cc["@mozilla.org/cookie-banner-rule;1"].createInstance(
    Ci.nsICookieBannerRule
  );
  ruleA.id = genUUID();
  ruleA.domains = [TEST_DOMAIN_A];

  ruleA.addClickRule(
    "div#banner",
    false,
    Ci.nsIClickRule.RUN_ALL,
    null,
    "button#optOut",
    "button#optIn"
  );
  Services.cookieBanners.insertRule(ruleA);

  info("Add opt-in click rule for DOMAIN_B.");
  let ruleB = Cc["@mozilla.org/cookie-banner-rule;1"].createInstance(
    Ci.nsICookieBannerRule
  );
  ruleB.id = genUUID();
  ruleB.domains = [TEST_DOMAIN_B];

  ruleB.addClickRule(
    "div#banner",
    false,
    Ci.nsIClickRule.RUN_ALL,
    null,
    null,
    "button#optIn"
  );
  Services.cookieBanners.insertRule(ruleB);

  info("Add global ruleC which targets a non-existing banner (presence).");
  let ruleC = Cc["@mozilla.org/cookie-banner-rule;1"].createInstance(
    Ci.nsICookieBannerRule
  );
  ruleC.id = genUUID();
  ruleC.domains = [];
  ruleC.addClickRule(
    "div#nonExistingBanner",
    false,
    Ci.nsIClickRule.RUN_ALL,
    null,
    null,
    "button#optIn"
  );
  Services.cookieBanners.insertRule(ruleC);

  info("Add global ruleD which targets a non-existing banner (presence).");
  let ruleD = Cc["@mozilla.org/cookie-banner-rule;1"].createInstance(
    Ci.nsICookieBannerRule
  );
  ruleD.id = genUUID();
  ruleD.domains = [];
  ruleD.addClickRule(
    "div#nonExistingBanner2",
    false,
    Ci.nsIClickRule.RUN_ALL,
    null,
    "button#optOut",
    "button#optIn"
  );
  Services.cookieBanners.insertRule(ruleD);
}

/**
 * Inserts cookie injection test rules for TEST_DOMAIN_A and TEST_DOMAIN_B.
 */
function insertTestCookieRules() {
  info("Clearing existing rules");
  Services.cookieBanners.resetRules(false);

  info("Inserting test rules.");

  let ruleA = Cc["@mozilla.org/cookie-banner-rule;1"].createInstance(
    Ci.nsICookieBannerRule
  );
  ruleA.domains = [TEST_DOMAIN_A, TEST_DOMAIN_C];

  Services.cookieBanners.insertRule(ruleA);
  ruleA.addCookie(
    true,
    `cookieConsent_${TEST_DOMAIN_A}_1`,
    "optOut1",
    null, // empty host to fall back to .<domain>
    "/",
    3600,
    "",
    false,
    false,
    false,
    0,
    0
  );
  ruleA.addCookie(
    true,
    `cookieConsent_${TEST_DOMAIN_A}_2`,
    "optOut2",
    null,
    "/",
    3600,
    "",
    false,
    false,
    false,
    0,
    0
  );

  // An opt-in cookie rule for DOMAIN_B.
  let ruleB = Cc["@mozilla.org/cookie-banner-rule;1"].createInstance(
    Ci.nsICookieBannerRule
  );
  ruleB.domains = [TEST_DOMAIN_B];

  Services.cookieBanners.insertRule(ruleB);
  ruleB.addCookie(
    false,
    `cookieConsent_${TEST_DOMAIN_B}_1`,
    "optIn1",
    TEST_DOMAIN_B,
    "/",
    3600,
    "UNSET",
    false,
    false,
    true,
    0,
    0
  );
}

/**
 * Test the Glean.cookieBannersClick.result metric.
 * @param {*} expected - Object mapping labels to counters. Omitted labels are
 * asserted to be in initial state (undefined =^ 0)
 * @param {boolean} [resetFOG] - Whether to reset all FOG telemetry after the
 * method has finished.
 */
async function testClickResultTelemetry(expected, resetFOG = true) {
  // TODO: Bug 1805653: Enable tests for Linux.
  if (AppConstants.platform == "linux") {
    ok(true, "Skip click telemetry tests on linux.");
    return;
  }

  // Ensure we have all data from the content process.
  await Services.fog.testFlushAllChildren();

  let labels = [
    "success",
    "success_cookie_injected",
    "success_dom_content_loaded",
    "success_mutation_pre_load",
    "success_mutation_post_load",
    "fail",
    "fail_banner_not_found",
    "fail_banner_not_visible",
    "fail_button_not_found",
    "fail_no_rule_for_mode",
    "fail_actor_destroyed",
  ];

  let testMetricState = doAssert => {
    for (let label of labels) {
      if (doAssert) {
        is(
          Glean.cookieBannersClick.result[label].testGetValue(),
          expected[label],
          `Counter for label '${label}' has correct state.`
        );
      } else if (
        Glean.cookieBannersClick.result[label].testGetValue() !==
        expected[label]
      ) {
        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);

    // Reset telemetry, even if the test condition above throws. This is to
    // avoid failing subsequent tests in case of a test failure.
    if (resetFOG) {
      Services.fog.testResetFOG();
    }
  }
}

/**
 * Triggers a cookie banner handling feature and tests the events dispatched.
 * @param {*} options - Test options.
 * @param {nsICookieBannerService::Modes} options.mode - The cookie banner
 * service mode to test with.
 * @param {boolean} options.detectOnly - Whether the service should be enabled
 * in detection only mode, where it does not handle banners.
 * @param {function} options.initFn - Function to call for test initialization.
 * @param {function} options.triggerFn - Function to call to trigger the banner
 * handling feature.
 * @param {string} options.testURL - URL where the test will trigger the banner
 * handling feature.
 * @returns {Promise} Resolves when the test completes, after cookie banner
 * events.
 */
async function runEventTest({ mode, detectOnly, initFn, triggerFn, testURL }) {
  await SpecialPowers.pushPrefEnv({
    set: [
      ["cookiebanners.service.mode", mode],
      ["cookiebanners.service.detectOnly", detectOnly],
    ],
  });

  await initFn();

  let expectEventDetected = mode != Ci.nsICookieBannerService.MODE_DISABLED;
  let expectEventHandled =
    !detectOnly &&
    (mode == Ci.nsICookieBannerService.MODE_REJECT ||
      mode == Ci.nsICookieBannerService.MODE_REJECT_OR_ACCEPT);

  let eventObservedDetected = false;
  let eventObservedHandled = false;

  // This is a bit hacky, we use side effects caused by the checkFn we pass into
  // waitForEvent to keep track of whether an event fired.
  let promiseEventDetected = BrowserTestUtils.waitForEvent(
    window,
    "cookiebannerdetected",
    false,
    () => {
      eventObservedDetected = true;
      return true;
    }
  );
  let promiseEventHandled = BrowserTestUtils.waitForEvent(
    window,
    "cookiebannerhandled",
    false,
    () => {
      eventObservedHandled = true;
      return true;
    }
  );

  // If we expect any events check which one comes first.
  let firstEventPromise;
  if (expectEventDetected || expectEventHandled) {
    firstEventPromise = Promise.race([
      promiseEventHandled,
      promiseEventDetected,
    ]);
  }

  await triggerFn();

  let eventDetected;
  if (expectEventDetected) {
    eventDetected = await promiseEventDetected;

    is(
      eventDetected.type,
      "cookiebannerdetected",
      "Should dispatch cookiebannerdetected event."
    );
  }

  let eventHandled;
  if (expectEventHandled) {
    eventHandled = await promiseEventHandled;
    is(
      eventHandled.type,
      "cookiebannerhandled",
      "Should dispatch cookiebannerhandled event."
    );
  }

  // For MODE_DISABLED this array will be empty, because we don't expect any
  // events to be dispatched.
  let eventsToTest = [eventDetected, eventHandled].filter(event => !!event);

  for (let event of eventsToTest) {
    info(`Testing properties of event ${event.type}`);

    let { windowContext } = event.detail;
    ok(
      windowContext,
      `Event ${event.type} detail should contain a WindowContext`
    );

    let browser = windowContext.browsingContext.top.embedderElement;
    ok(
      browser,
      "WindowContext should have an associated top embedder element."
    );
    is(
      browser.tagName,
      "browser",
      "The top embedder element should be a browser"
    );
    let chromeWin = browser.ownerGlobal;
    is(
      chromeWin,
      window,
      "The chrome window associated with the browser should match the window where the cookie banner was handled."
    );
    is(
      chromeWin.gBrowser.selectedBrowser,
      browser,
      "The browser associated with the event should be the selected browser."
    );
    is(
      browser.currentURI.spec,
      testURL,
      "The browser's URI spec should match the cookie banner test page."
    );
  }

  let firstEvent = await firstEventPromise;
  is(
    expectEventDetected || expectEventHandled,
    !!firstEvent,
    "Should have observed the first event if banner clicking is enabled."
  );

  if (expectEventDetected || expectEventHandled) {
    is(
      firstEvent.type,
      "cookiebannerdetected",
      "Detected event should be dispatched first"
    );
  }

  is(
    eventObservedDetected,
    expectEventDetected,
    `Should ${
      expectEventDetected ? "" : "not "
    }have observed 'cookiebannerdetected' event for mode ${mode}`
  );
  is(
    eventObservedHandled,
    expectEventHandled,
    `Should ${
      expectEventHandled ? "" : "not "
    }have observed 'cookiebannerhandled' event for mode ${mode}`
  );

  // Clean up pending promises by dispatching artificial cookiebanner events.
  // Otherwise the test fails because of pending event listeners which
  // BrowserTestUtils.waitForEvent registered.
  for (let eventType of ["cookiebannerdetected", "cookiebannerhandled"]) {
    let event = new CustomEvent(eventType, {
      bubbles: true,
      cancelable: false,
    });
    window.windowUtils.dispatchEventToChromeOnly(window, event);
  }

  await promiseEventDetected;
  await promiseEventHandled;
}