summaryrefslogtreecommitdiffstats
path: root/browser/components/urlbar/tests/browser/browser_strip_on_share_telemetry.js
blob: 7b6fb53e3b7f2dc63b66a8c36fd360a5625f8608 (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
"use strict";

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

let listService;

const STRIP_ON_SHARE_PARAMS_REMOVED = "STRIP_ON_SHARE_PARAMS_REMOVED";
const STRIP_ON_SHARE_LENGTH_DECREASE = "STRIP_ON_SHARE_LENGTH_DECREASE";

add_setup(async function () {
  await SpecialPowers.pushPrefEnv({
    set: [
      ["privacy.query_stripping.strip_on_share.enabled", true],
      ["privacy.query_stripping.enabled", false],
    ],
  });

  // Get the list service so we can wait for it to be fully initialized before running tests.
  listService = Cc["@mozilla.org/query-stripping-list-service;1"].getService(
    Ci.nsIURLQueryStrippingListService
  );

  await listService.testWaitForInit();
});

// Checking telemetry for single query params being stripped
add_task(async function testSingleQueryParam() {
  let originalURI = "https://www.example.com/?utm_source=1";
  let strippedURI = "https://www.example.com/";

  // Calculating length difference between URLs to check correct telemetry label
  let lengthDiff = originalURI.length - strippedURI.length;

  let paramHistogram = TelemetryTestUtils.getAndClearHistogram(
    STRIP_ON_SHARE_PARAMS_REMOVED
  );
  let lengthHistogram = TelemetryTestUtils.getAndClearHistogram(
    STRIP_ON_SHARE_LENGTH_DECREASE
  );

  await testStripOnShare(originalURI, strippedURI);

  // The "1" Label is being checked as 1 Query Param is being stripped
  TelemetryTestUtils.assertHistogram(paramHistogram, 1, 1);
  TelemetryTestUtils.assertHistogram(lengthHistogram, lengthDiff, 1);

  await testStripOnShare(originalURI, strippedURI);

  TelemetryTestUtils.assertHistogram(paramHistogram, 1, 2);
  TelemetryTestUtils.assertHistogram(lengthHistogram, lengthDiff, 2);
});

// Checking telemetry for mutliple query params being stripped
add_task(async function testMultiQueryParams() {
  let originalURI = "https://www.example.com/?utm_source=1&utm_ad=1&utm_id=1";
  let strippedURI = "https://www.example.com/";

  // Calculating length difference between URLs to check correct telemetry label
  let lengthDiff = originalURI.length - strippedURI.length;

  let paramHistogram = TelemetryTestUtils.getAndClearHistogram(
    STRIP_ON_SHARE_PARAMS_REMOVED
  );
  let lengthHistogram = TelemetryTestUtils.getAndClearHistogram(
    STRIP_ON_SHARE_LENGTH_DECREASE
  );

  await testStripOnShare(originalURI, strippedURI);

  // The "3" Label is being checked as 3 Query Params are being stripped
  TelemetryTestUtils.assertHistogram(paramHistogram, 3, 1);
  TelemetryTestUtils.assertHistogram(lengthHistogram, lengthDiff, 1);

  await testStripOnShare(originalURI, strippedURI);

  TelemetryTestUtils.assertHistogram(paramHistogram, 3, 2);
  TelemetryTestUtils.assertHistogram(lengthHistogram, lengthDiff, 2);
});

async function testStripOnShare(validUrl, strippedUrl) {
  await BrowserTestUtils.withNewTab(validUrl, async function () {
    gURLBar.focus();
    gURLBar.select();
    let menuitem = await promiseContextualMenuitem("strip-on-share");
    Assert.ok(BrowserTestUtils.isVisible(menuitem), "Menu item is visible");
    let hidePromise = BrowserTestUtils.waitForEvent(
      menuitem.parentElement,
      "popuphidden"
    );
    // Make sure the clean copy of the link will be copied to the clipboard
    await SimpleTest.promiseClipboardChange(strippedUrl, () => {
      menuitem.closest("menupopup").activateItem(menuitem);
    });
    await hidePromise;
  });
}