summaryrefslogtreecommitdiffstats
path: root/browser/modules/test/browser/browser_UsageTelemetry_private_and_restore.js
blob: ab0c8651b658d3770edfad867fd3727dd23701bf (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
"use strict";

const triggeringPrincipal_base64 = E10SUtils.SERIALIZED_SYSTEMPRINCIPAL;

const MAX_CONCURRENT_TABS = "browser.engagement.max_concurrent_tab_count";
const TAB_EVENT_COUNT = "browser.engagement.tab_open_event_count";
const MAX_CONCURRENT_WINDOWS = "browser.engagement.max_concurrent_window_count";
const WINDOW_OPEN_COUNT = "browser.engagement.window_open_event_count";
const TOTAL_URI_COUNT = "browser.engagement.total_uri_count";
const UNFILTERED_URI_COUNT = "browser.engagement.unfiltered_uri_count";
const UNIQUE_DOMAINS_COUNT = "browser.engagement.unique_domains_count";
const TOTAL_URI_COUNT_NORMAL_AND_PRIVATE_MODE =
  "browser.engagement.total_uri_count_normal_and_private_mode";

BrowserUsageTelemetry._onTabsOpenedTask._timeoutMs = 0;
registerCleanupFunction(() => {
  BrowserUsageTelemetry._onTabsOpenedTask._timeoutMs = undefined;
});

function promiseBrowserStateRestored() {
  return new Promise(resolve => {
    Services.obs.addObserver(function observer(aSubject, aTopic) {
      Services.obs.removeObserver(
        observer,
        "sessionstore-browser-state-restored"
      );
      resolve();
    }, "sessionstore-browser-state-restored");
  });
}

add_task(async function test_privateMode() {
  // Let's reset the counts.
  Services.telemetry.clearScalars();
  Services.fog.testResetFOG();

  // Open a private window and load a website in it.
  let privateWin = await BrowserTestUtils.openNewBrowserWindow({
    private: true,
  });
  await BrowserTestUtils.firstBrowserLoaded(privateWin);
  BrowserTestUtils.loadURIString(
    privateWin.gBrowser.selectedBrowser,
    "https://example.com/"
  );
  await BrowserTestUtils.browserLoaded(
    privateWin.gBrowser.selectedBrowser,
    false,
    "https://example.com/"
  );

  // Check that tab and window count is recorded.
  const scalars = TelemetryTestUtils.getProcessScalars("parent");

  ok(
    !(TOTAL_URI_COUNT in scalars),
    "We should not track URIs in private mode."
  );
  ok(
    !(UNFILTERED_URI_COUNT in scalars),
    "We should not track URIs in private mode."
  );
  ok(
    !(UNIQUE_DOMAINS_COUNT in scalars),
    "We should not track unique domains in private mode."
  );
  is(
    scalars[TAB_EVENT_COUNT],
    1,
    "The number of open tab event count must match the expected value."
  );
  is(
    scalars[MAX_CONCURRENT_TABS],
    2,
    "The maximum tab count must match the expected value."
  );
  is(
    scalars[WINDOW_OPEN_COUNT],
    1,
    "The number of window open event count must match the expected value."
  );
  is(
    scalars[MAX_CONCURRENT_WINDOWS],
    2,
    "The maximum window count must match the expected value."
  );
  is(
    scalars[TOTAL_URI_COUNT_NORMAL_AND_PRIVATE_MODE],
    1,
    "We should include URIs in private mode as part of the actual total URI count."
  );
  is(
    Glean.browserEngagement.uriCount.testGetValue(),
    1,
    "We should record the URI count in Glean as well."
  );

  // Clean up.
  await BrowserTestUtils.closeWindow(privateWin);
});

add_task(async function test_sessionRestore() {
  const PREF_RESTORE_ON_DEMAND = "browser.sessionstore.restore_on_demand";
  Services.prefs.setBoolPref(PREF_RESTORE_ON_DEMAND, false);
  registerCleanupFunction(() => {
    Services.prefs.clearUserPref(PREF_RESTORE_ON_DEMAND);
  });

  // Let's reset the counts.
  Services.telemetry.clearScalars();

  // The first window will be put into the already open window and the second
  // window will be opened with _openWindowWithState, which is the source of the problem.
  const state = {
    windows: [
      {
        tabs: [
          {
            entries: [
              { url: "http://example.org", triggeringPrincipal_base64 },
            ],
            extData: { uniq: 3785 },
          },
        ],
        selected: 1,
      },
    ],
  };

  // Save the current session.
  let { SessionStore } = ChromeUtils.importESModule(
    "resource:///modules/sessionstore/SessionStore.sys.mjs"
  );

  // Load the custom state and wait for SSTabRestored, as we want to make sure
  // that the URI counting code was hit.
  let tabRestored = BrowserTestUtils.waitForEvent(
    gBrowser.tabContainer,
    "SSTabRestored"
  );
  SessionStore.setBrowserState(JSON.stringify(state));
  await tabRestored;

  // Check that the URI is not recorded.
  const scalars = TelemetryTestUtils.getProcessScalars("parent");

  ok(
    !(TOTAL_URI_COUNT in scalars),
    "We should not track URIs from restored sessions."
  );
  ok(
    !(UNFILTERED_URI_COUNT in scalars),
    "We should not track URIs from restored sessions."
  );
  ok(
    !(UNIQUE_DOMAINS_COUNT in scalars),
    "We should not track unique domains from restored sessions."
  );

  // Restore the original session and cleanup.
  let sessionRestored = promiseBrowserStateRestored();
  SessionStore.setBrowserState(JSON.stringify(state));
  await sessionRestored;
});