summaryrefslogtreecommitdiffstats
path: root/dom/l10n/tests/mochitest/document_l10n/test_telemetry.html
blob: b528ebc7ea0252b2eef07375fef2b3c81fc2bdbb (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
<!DOCTYPE HTML>
<html>
<head>
  <meta charset="utf-8">
  <title>Test DocumentL10n Telemetry</title>
  <script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css">
  <script type="application/javascript">
  "use strict";
  const { BrowserTestUtils } = ChromeUtils.import(
    "resource://testing-common/BrowserTestUtils.jsm"
  );

  SimpleTest.waitForExplicitFinish();

  function countValues(snapshot, key) {
    if (!snapshot.hasOwnProperty(key)) {
      return 0;
    }
    let values = snapshot[key].values;
    return Object.values(values).reduce((sum, n) => sum + n, 0);
  }

  (async function() {
      let histogram = Services.telemetry
          .getKeyedHistogramById("L10N_DOCUMENT_INITIAL_TRANSLATION_TIME_US");
      let snapshot = histogram.snapshot();

      // In some cases the test runs before first window is localized.
      // We just want to ensure that we didn't register more than 1
      // first window telemetry.
      let firstWindowCount = countValues(snapshot, "browser_first_window");
      is(firstWindowCount < 2, true);

      histogram.clear();

      // Open a new window
      let win = await BrowserTestUtils.openNewBrowserWindow({
        waitForTabURL: "about:blank",
      });

      // Telemetry in testing is flaky and when landing this test
      // we saw cases where the snapshot did not contain the new
      // window telemetry at this moment.
      //
      // We're going to use `waitForCondition` to test for
      // the telemetry to be eventually recorded.
      BrowserTestUtils.waitForCondition(() => {
          snapshot = histogram.snapshot();

          // We want to make sure that since we cleared histogram
          // just one new window of either type has been opened.
          let browserWindowsCount =
              countValues(snapshot, "browser_new_window") +
              countValues(snapshot, "browser_first_window");
          return browserWindowsCount == 1;
      });

      // Open preferences in a new tab
      let tab = BrowserTestUtils.addTab(
          win.gBrowser,
          "about:preferences"
      );
      await BrowserTestUtils.browserLoaded(tab.linkedBrowser);

      // Similarly to the above, we've seen cases where the telemetry
      // was not present right after the tab was opened, so
      // we'll use `waitForCondition` here.
      BrowserTestUtils.waitForCondition(() => {
          snapshot = histogram.snapshot();

          return countValues(snapshot, "about:preferences") == 1;
      });

      await BrowserTestUtils.closeWindow(win);

      SimpleTest.finish();
  })();
  </script>
</head>
<body>
</body>
</html>