summaryrefslogtreecommitdiffstats
path: root/browser/modules/test/browser/browser_Telemetry_numberOfSiteOriginsPerDocument.js
blob: 8caaa1ff380f95134ddcd0692ce0fa90ca6d229f (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
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/
 */

"use strict";

ChromeUtils.defineESModuleGetters(this, {
  TelemetryTestUtils: "resource://testing-common/TelemetryTestUtils.sys.mjs",
});

const histogramName = "FX_NUMBER_OF_UNIQUE_SITE_ORIGINS_PER_DOCUMENT";
const testRoot = getRootDirectory(gTestPath).replace(
  "chrome://mochitests/content",
  "http://mochi.test:8888"
);

function windowGlobalDestroyed(id) {
  return BrowserUtils.promiseObserved(
    "window-global-destroyed",
    aWGP => aWGP.innerWindowId == id
  );
}

async function openAndCloseTab(uri) {
  const tab = await BrowserTestUtils.openNewForegroundTab({
    gBrowser,
    opening: uri,
  });

  const innerWindowId =
    tab.linkedBrowser.browsingContext.currentWindowGlobal.innerWindowId;

  const wgpDestroyed = windowGlobalDestroyed(innerWindowId);
  BrowserTestUtils.removeTab(tab);
  await wgpDestroyed;
}

add_task(async function test_numberOfSiteOriginsAfterTabClose() {
  const histogram = TelemetryTestUtils.getAndClearHistogram(histogramName);
  const testPage = `${testRoot}contain_iframe.html`;

  await openAndCloseTab(testPage);

  // testPage contains two origins: mochi.test:8888 and example.com.
  TelemetryTestUtils.assertHistogram(histogram, 2, 1);
});

add_task(async function test_numberOfSiteOriginsAboutBlank() {
  const histogram = TelemetryTestUtils.getAndClearHistogram(histogramName);

  await openAndCloseTab("about:blank");

  const { values } = histogram.snapshot();
  Assert.deepEqual(
    values,
    {},
    `Histogram should have no values; had ${JSON.stringify(values)}`
  );
});

add_task(async function test_numberOfSiteOriginsMultipleNavigations() {
  const histogram = TelemetryTestUtils.getAndClearHistogram(histogramName);
  const testPage = `${testRoot}contain_iframe.html`;

  const tab = await BrowserTestUtils.openNewForegroundTab({
    gBrowser,
    opening: testPage,
    waitForStateStop: true,
  });

  const wgpDestroyedPromises = [
    windowGlobalDestroyed(tab.linkedBrowser.innerWindowID),
  ];

  // Navigate to an interstitial page.
  BrowserTestUtils.loadURIString(tab.linkedBrowser, "about:blank");
  await BrowserTestUtils.browserLoaded(tab.linkedBrowser);

  // Navigate to another test page.
  BrowserTestUtils.loadURIString(tab.linkedBrowser, testPage);
  await BrowserTestUtils.browserLoaded(tab.linkedBrowser);

  wgpDestroyedPromises.push(
    windowGlobalDestroyed(tab.linkedBrowser.innerWindowID)
  );

  BrowserTestUtils.removeTab(tab);
  await Promise.all(wgpDestroyedPromises);

  // testPage has been loaded twice and contains two origins: mochi.test:8888
  // and example.com.
  TelemetryTestUtils.assertHistogram(histogram, 2, 2);
});

add_task(async function test_numberOfSiteOriginsAddAndRemove() {
  const histogram = TelemetryTestUtils.getAndClearHistogram(histogramName);
  const testPage = `${testRoot}blank_iframe.html`;

  const tab = await BrowserTestUtils.openNewForegroundTab({
    gBrowser,
    opening: testPage,
    waitForStateStop: true,
  });

  // Load a subdocument in the page's iframe.
  await SpecialPowers.spawn(tab.linkedBrowser, [], async () => {
    const iframe = content.window.document.querySelector("iframe");
    const loaded = new Promise(resolve => {
      iframe.addEventListener("load", () => resolve(), { once: true });
    });
    iframe.src = "http://example.com";

    await loaded;
  });

  // Load a *new* subdocument in the page's iframe. This will result in the page
  // having had three different origins, but only two at any one time.
  await SpecialPowers.spawn(tab.linkedBrowser, [], async () => {
    const iframe = content.window.document.querySelector("iframe");
    const loaded = new Promise(resolve => {
      iframe.addEventListener("load", () => resolve(), { once: true });
    });
    iframe.src = "http://example.org";

    await loaded;
  });

  const wgpDestroyed = windowGlobalDestroyed(tab.linkedBrowser.innerWindowID);
  BrowserTestUtils.removeTab(tab);
  await wgpDestroyed;

  // The page only ever had two origins at once.
  TelemetryTestUtils.assertHistogram(histogram, 2, 1);
});