summaryrefslogtreecommitdiffstats
path: root/browser/components/places/tests/browser/browser_sidebar_history_telemetry.js
blob: cb27bf66effc5bb33503ec278bd7022f52d11936 (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
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

"use strict";

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

// The prompt returns 1 for cancelled and 0 for accepted.
let gResponse = 1;
(function replacePromptService() {
  let originalPromptService = Services.prompt;
  Services.prompt = {
    QueryInterface: ChromeUtils.generateQI([Ci.nsIPromptService]),
    confirmEx: () => gResponse,
  };
  registerCleanupFunction(() => {
    Services.prompt = originalPromptService;
  });
})();

add_setup(async function () {
  await PlacesUtils.history.clear();

  // Visited pages listed by descending visit date.
  let pages = [
    "https://sidebar.mozilla.org/a",
    "https://sidebar.mozilla.org/b",
    "https://sidebar.mozilla.org/c",
    "https://www.mozilla.org/d",
  ];

  // Add some visited page.
  let time = Date.now();
  let places = [];
  for (let i = 0; i < pages.length; i++) {
    places.push({
      uri: NetUtil.newURI(pages[i]),
      visitDate: (time - i) * 1000,
      transition: PlacesUtils.history.TRANSITION_TYPED,
    });
  }
  await PlacesTestUtils.addVisits(places);

  registerCleanupFunction(async () => {
    await PlacesUtils.history.clear();
  });
});

add_task(async function test_click_multiple_history_entries() {
  await withSidebarTree("history", async tree => {
    tree.ownerDocument.getElementById("byday").doCommand();

    // Select the first link and click on it.
    tree.selectNode(tree.view.nodeForTreeIndex(firstNodeIndex));
    is(
      tree.selectedNode.title,
      "Today",
      "The Today history sidebar button is selected"
    );

    // if a user tries to open all history items and they cancel opening
    // those items in new tabs (due to max tab limit warning),
    // no telemetry should be recorded
    gResponse = 1;
    await SpecialPowers.pushPrefEnv({
      set: [["browser.tabs.maxOpenBeforeWarn", 4]],
    });

    // open multiple history items with a single click
    synthesizeClickOnSelectedTreeCell(tree, { button: 1 });

    TelemetryTestUtils.assertScalarUnset(
      TelemetryTestUtils.getProcessScalars("parent", true, true),
      "sidebar.link"
    );

    // if they proceed with opening history multiple history items despite the warning,
    // telemetry should be recorded
    gResponse = 0;
    synthesizeClickOnSelectedTreeCell(tree, { button: 1 });

    TelemetryTestUtils.assertKeyedScalar(
      TelemetryTestUtils.getProcessScalars("parent", true, true),
      "sidebar.link",
      "history",
      4
    );

    let parentNode = tree.selectedNode;
    if (!parentNode.containerOpen) {
      // Only need to open/expand container node on first run
      synthesizeClickOnSelectedTreeCell(tree);
    }
    if (parentNode.title == "Today" && parentNode.hasChildren) {
      info(`Selecting node with title ${parentNode?.getChild(0)?.title}`);
      tree.selectNode(parentNode.getChild(0));
    }

    synthesizeClickOnSelectedTreeCell(tree, {
      button: 2,
      type: "contextmenu",
    });

    TelemetryTestUtils.assertScalarUnset(
      TelemetryTestUtils.getProcessScalars("parent", true, true),
      "sidebar.link"
    );

    let openNewTabOption = document.getElementById("placesContext_open:newtab");
    openNewTabOption.click();

    TelemetryTestUtils.assertKeyedScalar(
      TelemetryTestUtils.getProcessScalars("parent", true, true),
      "sidebar.link",
      "history",
      1
    );

    if (parentNode.title == "Today" && parentNode.hasChildren) {
      tree.selectNode(parentNode.getChild(0));
    }

    let newWinOpened = BrowserTestUtils.waitForNewWindow();

    synthesizeClickOnSelectedTreeCell(tree, {
      button: 2,
      type: "contextmenu",
    });

    TelemetryTestUtils.assertScalarUnset(
      TelemetryTestUtils.getProcessScalars("parent", true, true),
      "sidebar.link"
    );

    let openNewWindowOption = document.getElementById(
      "placesContext_open:newwindow"
    );
    openNewWindowOption.click();

    let newWin = await newWinOpened;

    TelemetryTestUtils.assertKeyedScalar(
      TelemetryTestUtils.getProcessScalars("parent", true, true),
      "sidebar.link",
      "history",
      1
    );

    await BrowserTestUtils.closeWindow(newWin);

    if (parentNode.title == "Today" && parentNode.hasChildren) {
      tree.selectNode(parentNode.getChild(0));
    }

    let newPrivateWinOpened = BrowserTestUtils.waitForNewWindow();

    synthesizeClickOnSelectedTreeCell(tree, {
      button: 2,
      type: "contextmenu",
    });

    TelemetryTestUtils.assertScalarUnset(
      TelemetryTestUtils.getProcessScalars("parent", true, true),
      "sidebar.link"
    );

    let openNewPrivateWindowOption = document.getElementById(
      "placesContext_open:newprivatewindow"
    );
    openNewPrivateWindowOption.click();

    let newPrivateWin = await newPrivateWinOpened;

    TelemetryTestUtils.assertKeyedScalar(
      TelemetryTestUtils.getProcessScalars("parent", true, true),
      "sidebar.link",
      "history",
      1
    );

    await BrowserTestUtils.closeWindow(newPrivateWin);
  });

  await SpecialPowers.popPrefEnv();

  while (gBrowser.tabs.length > 1) {
    BrowserTestUtils.removeTab(gBrowser.selectedTab);
  }
});

add_task(async function test_search_and_filter() {
  let cumulativeSearchesHistogram = Services.telemetry.getHistogramById(
    "PLACES_SEARCHBAR_CUMULATIVE_SEARCHES"
  );
  cumulativeSearchesHistogram.clear();
  let cumulativeFilterCountHistogram = Services.telemetry.getHistogramById(
    "PLACES_SEARCHBAR_CUMULATIVE_FILTER_COUNT"
  );
  cumulativeFilterCountHistogram.clear();

  await withSidebarTree("history", async tree => {
    // Apply a search filter.
    tree.ownerDocument.getElementById("bylastvisited").doCommand();
    info("Search filter was changed to bylastvisited");

    // Search the tree.
    let searchBox = tree.ownerDocument.getElementById("search-box");
    searchBox.value = "sidebar.mozilla";
    searchBox.doCommand();
    info("Tree was searched with sting sidebar.mozilla");

    searchBox.value = "";
    searchBox.doCommand();
    info("Search was reset");

    // Perform a second search.
    searchBox.value = "sidebar.mozilla";
    searchBox.doCommand();
    info("Second search was performed");

    // Select the first link and click on it.
    tree.selectNode(tree.view.nodeForTreeIndex(firstNodeIndex));
    synthesizeClickOnSelectedTreeCell(tree, { button: 1 });
    info("First link was selected and then clicked on");

    TelemetryTestUtils.assertKeyedScalar(
      TelemetryTestUtils.getProcessScalars("parent", true, true),
      "sidebar.link",
      "history",
      1
    );
  });

  TelemetryTestUtils.assertHistogram(cumulativeSearchesHistogram, 2, 1);
  info("Cumulative search telemetry looks right");

  TelemetryTestUtils.assertHistogram(cumulativeFilterCountHistogram, 1, 1);
  info("Cumulative search filter telemetry looks right");

  cumulativeSearchesHistogram.clear();
  cumulativeFilterCountHistogram.clear();

  await withSidebarTree("history", async tree => {
    // Apply a search filter.
    tree.ownerDocument.getElementById("byday").doCommand();
    info("First search filter applied");

    // Apply another search filter.
    tree.ownerDocument.getElementById("bylastvisited").doCommand();
    info("Second search filter applied");

    // Apply a search filter.
    tree.ownerDocument.getElementById("byday").doCommand();
    info("Third search filter applied");

    // Search the tree.
    let searchBox = tree.ownerDocument.getElementById("search-box");
    searchBox.value = "sidebar.mozilla";
    searchBox.doCommand();

    // Select the first link and click on it.
    tree.selectNode(tree.view.nodeForTreeIndex(firstNodeIndex));
    synthesizeClickOnSelectedTreeCell(tree, { button: 1 });

    TelemetryTestUtils.assertKeyedScalar(
      TelemetryTestUtils.getProcessScalars("parent", true, true),
      "sidebar.link",
      "history",
      1
    );
  });

  TelemetryTestUtils.assertHistogram(cumulativeSearchesHistogram, 1, 1);
  TelemetryTestUtils.assertHistogram(cumulativeFilterCountHistogram, 3, 1);

  cumulativeSearchesHistogram.clear();
  cumulativeFilterCountHistogram.clear();
  while (gBrowser.tabs.length > 1) {
    BrowserTestUtils.removeTab(gBrowser.selectedTab);
  }
});