summaryrefslogtreecommitdiffstats
path: root/browser/components/places/tests/browser/browser_library_telemetry.js
blob: 00ca4635d86478b14869636748ac30f8a434336b (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
/* 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"
);

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

// 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;
  });
})();

async function searchHistory(gLibrary, searchTerm) {
  let doc = gLibrary.document;
  let contentTree = doc.getElementById("placeContent");

  let searchBox = doc.getElementById("searchFilter");
  searchBox.value = searchTerm;
  gLibrary.PlacesSearchBox.search(searchBox.value);
  let query = {};
  PlacesUtils.history.queryStringToQuery(
    contentTree.result.root.uri,
    query,
    {}
  );
  Assert.equal(
    query.value.searchTerms,
    searchTerm,
    "Content tree's searchTerms should be text in search box"
  );
}

function searchBookmarks(gLibrary, searchTerm) {
  let searchBox = gLibrary.document.getElementById("searchFilter");
  searchBox.value = searchTerm;
  gLibrary.PlacesSearchBox.search(searchBox.value);
}

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

  // Add some visited pages to history
  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);

  await PlacesUtils.bookmarks.insertTree({
    guid: PlacesUtils.bookmarks.unfiledGuid,
    children: [
      {
        title: "Mozilla",
        url: "https://www.mozilla.org/",
      },
      {
        title: "Example",
        url: "https://sidebar.mozilla.org/",
      },
    ],
  });
  await registerCleanupFunction(async () => {
    await PlacesUtils.history.clear();
    await PlacesUtils.bookmarks.eraseEverything();
  });
});

add_task(async function test_library_history_telemetry() {
  Services.telemetry.clearScalars();
  let cumulativeSearchesHistogram = Services.telemetry.getHistogramById(
    "PLACES_LIBRARY_CUMULATIVE_HISTORY_SEARCHES"
  );

  let gLibrary = await promiseLibrary("History");

  TelemetryTestUtils.assertKeyedScalar(
    TelemetryTestUtils.getProcessScalars("parent", true, true),
    "library.opened",
    "history",
    1
  );

  let currentSelectedLeftPaneNode =
    gLibrary.PlacesOrganizer._places.selectedNode;
  if (
    currentSelectedLeftPaneNode.title == "History" &&
    currentSelectedLeftPaneNode.hasChildren &&
    currentSelectedLeftPaneNode.getChild(0).title == "Today"
  ) {
    // Select "Today" node under History if not already selected
    gLibrary.PlacesOrganizer._places.selectNode(
      currentSelectedLeftPaneNode.getChild(0)
    );
  }

  Assert.equal(
    gLibrary.PlacesOrganizer._places.selectedNode.title,
    "Today",
    "The Today history sidebar button is selected"
  );

  await searchHistory(gLibrary, "mozilla");

  TelemetryTestUtils.assertKeyedScalar(
    TelemetryTestUtils.getProcessScalars("parent", true, true),
    "library.search",
    "history",
    1
  );

  let firstHistoryNode = gLibrary.ContentTree.view.view.nodeForTreeIndex(0);
  Assert.equal(
    firstHistoryNode.uri,
    pages[0],
    "Found history item in the right pane"
  );

  // Double click first History link to open it
  gLibrary.ContentTree.view.selectNode(firstHistoryNode);
  synthesizeClickOnSelectedTreeCell(gLibrary.ContentTree.view, {
    clickCount: 2,
  });

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

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

  cumulativeSearchesHistogram.clear();

  // Close and reopen Libary window
  await promiseLibraryClosed(gLibrary);
  gLibrary = await promiseLibrary("History");

  Assert.equal(
    gLibrary.PlacesOrganizer._places.selectedNode.title,
    "Today",
    "The Today history sidebar button is selected"
  );

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

  // Reject opening all tabs when prompted
  gResponse = 1;

  // Open all history entries
  synthesizeClickOnSelectedTreeCell(gLibrary.PlacesOrganizer._places, {
    button: 1,
  });

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

  // Make 4 searches before opening History links
  await searchHistory(gLibrary, "library a", 1);
  info("First search was performed.");
  await searchHistory(gLibrary, "library b", 2);
  info("Second search was performed.");
  await searchHistory(gLibrary, "library c", 3);
  info("Third search was performed.");
  await searchHistory(gLibrary, "mozilla", 4);
  info("Fourth search was performed.");

  TelemetryTestUtils.assertKeyedScalar(
    TelemetryTestUtils.getProcessScalars("parent", true, true),
    "library.search",
    "history",
    4
  );

  // Accept opening all tabs when prompted
  gResponse = 0;

  // Open all history entries
  synthesizeClickOnSelectedTreeCell(gLibrary.PlacesOrganizer._places, {
    button: 1,
  });

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

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

  synthesizeClickOnSelectedTreeCell(gLibrary.ContentTree.view, {
    button: 2,
    type: "contextmenu",
  });

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

  let openOption = document.getElementById("placesContext_open");
  openOption.click();

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

  synthesizeClickOnSelectedTreeCell(gLibrary.ContentTree.view, {
    button: 2,
    type: "contextmenu",
  });

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

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

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

  let newWinOpened = BrowserTestUtils.waitForNewWindow();

  synthesizeClickOnSelectedTreeCell(gLibrary.ContentTree.view, {
    button: 2,
    type: "contextmenu",
  });

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

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

  let newWin = await newWinOpened;

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

  await BrowserTestUtils.closeWindow(newWin);

  let newPrivateWinOpened = BrowserTestUtils.waitForNewWindow();

  synthesizeClickOnSelectedTreeCell(gLibrary.ContentTree.view, {
    button: 2,
    type: "contextmenu",
  });

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

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

  let newPrivateWin = await newPrivateWinOpened;

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

  await BrowserTestUtils.closeWindow(newPrivateWin);

  cumulativeSearchesHistogram.clear();
  await promiseLibraryClosed(gLibrary);
  await SpecialPowers.popPrefEnv();

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

add_task(async function test_library_bookmarks_telemetry() {
  Services.telemetry.clearScalars();
  let cumulativeSearchesHistogram = TelemetryTestUtils.getAndClearHistogram(
    "PLACES_LIBRARY_CUMULATIVE_BOOKMARK_SEARCHES"
  );

  let library = await promiseLibrary("AllBookmarks");

  TelemetryTestUtils.assertKeyedScalar(
    TelemetryTestUtils.getProcessScalars("parent", true, true),
    "library.opened",
    "bookmarks",
    1
  );

  searchBookmarks(library, "mozilla");

  // reset
  searchBookmarks(library, "");

  // search again
  searchBookmarks(library, "moz");

  TelemetryTestUtils.assertKeyedScalar(
    TelemetryTestUtils.getProcessScalars("parent", true, true),
    "library.search",
    "bookmarks",
    2
  );

  let firstNode = library.ContentTree.view.view.nodeForTreeIndex(0);
  library.ContentTree.view.selectNode(firstNode);

  synthesizeClickOnSelectedTreeCell(library.ContentTree.view, {
    clickCount: 2,
  });

  TelemetryTestUtils.assertKeyedScalar(
    TelemetryTestUtils.getProcessScalars("parent", true, true),
    "library.link",
    "bookmarks",
    1
  );

  TelemetryTestUtils.assertHistogram(cumulativeSearchesHistogram, 2, 1);

  cumulativeSearchesHistogram = TelemetryTestUtils.getAndClearHistogram(
    "PLACES_LIBRARY_CUMULATIVE_BOOKMARK_SEARCHES"
  );

  // do another search to make sure everything has been cleared
  searchBookmarks(library, "moz");

  TelemetryTestUtils.assertKeyedScalar(
    TelemetryTestUtils.getProcessScalars("parent", true, true),
    "library.search",
    "bookmarks",
    1
  );

  firstNode = library.ContentTree.view.view.nodeForTreeIndex(0);
  library.ContentTree.view.selectNode(firstNode);

  synthesizeClickOnSelectedTreeCell(library.ContentTree.view, {
    clickCount: 2,
  });

  TelemetryTestUtils.assertKeyedScalar(
    TelemetryTestUtils.getProcessScalars("parent", true, true),
    "library.link",
    "bookmarks",
    1
  );

  TelemetryTestUtils.assertHistogram(cumulativeSearchesHistogram, 1, 1);

  cumulativeSearchesHistogram.clear();
  await promiseLibraryClosed(library);

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