Bug 1361855 - Record TAB_COUNT histogram during TabOpen event; r?Gijs draft
authorLie Ryan <lie.1296@gmail.com>
Fri, 09 Jun 2017 18:40:24 +0000
changeset 647633 70f9e6205b028f9e3f1797bc5a2cf8d9d0fc61ad
parent 647632 a0be7ab05a55945bf8dd073a87e603f9d0327610
child 647634 77f0130b6835cf6ca400fccee816a1b6959adfec
push id74488
push userbmo:lie.1296@gmail.com
push dateWed, 16 Aug 2017 17:50:44 +0000
reviewersGijs
bugs1361855
milestone57.0a1
Bug 1361855 - Record TAB_COUNT histogram during TabOpen event; r?Gijs MozReview-Commit-ID: 7ZHakmLZYHu
browser/modules/BrowserUsageTelemetry.jsm
browser/modules/test/browser/browser_UsageTelemetry.js
--- a/browser/modules/BrowserUsageTelemetry.jsm
+++ b/browser/modules/BrowserUsageTelemetry.jsm
@@ -602,16 +602,18 @@ let BrowserUsageTelemetry = {
    *        is computed manually if not provided.
    */
   _onTabOpen(tabCount = 0) {
     // Use the provided tab count if available. Otherwise, go on and compute it.
     tabCount = tabCount || getOpenTabsAndWinsCounts().tabCount;
     // Update the "tab opened" count and its maximum.
     Services.telemetry.scalarAdd(TAB_OPEN_EVENT_COUNT_SCALAR_NAME, 1);
     Services.telemetry.scalarSetMaximum(MAX_TAB_COUNT_SCALAR_NAME, tabCount);
+
+    this._recordTabCount(tabCount);
   },
 
   /**
    * Tracks the window count and registers the listeners for the tab count.
    * @param{Object} win The window object.
    */
   _onWindowOpen(win) {
     // Make sure to have a |nsIDOMWindow|.
--- a/browser/modules/test/browser/browser_UsageTelemetry.js
+++ b/browser/modules/test/browser/browser_UsageTelemetry.js
@@ -262,8 +262,44 @@ add_task(async function test_URIAndDomai
   await BrowserTestUtils.loadURI(newWin.gBrowser.selectedBrowser, TEST_PAGE);
   await BrowserTestUtils.browserLoaded(newWin.gBrowser.selectedBrowser);
   checkCounts({totalURIs: 5, domainCount: 2, totalUnfilteredURIs: 6});
 
   // Clean up.
   await BrowserTestUtils.removeTab(firstTab);
   await BrowserTestUtils.closeWindow(newWin);
 });
+
+function checkTabCountHistogram(result, expected, message) {
+  let expectedPadded = result.counts.map((val, idx) => idx < expected.length ? expected[idx] : 0);
+  Assert.deepEqual(result.counts, expectedPadded, message);
+}
+
+add_task(async function test_tabsHistogram() {
+  let openedTabs = [];
+  let tabCountHist = getAndClearHistogram("TAB_COUNT");
+
+  checkTabCountHistogram(tabCountHist.snapshot(), [0, 0], "TAB_COUNT telemetry - initial tab counts")
+
+  // Add a new tab and check that the count is right.
+  openedTabs.push(await BrowserTestUtils.openNewForegroundTab(gBrowser, "about:blank"));
+  checkTabCountHistogram(tabCountHist.snapshot(), [0, 0, 1], "TAB_COUNT telemetry - opening tabs");
+
+  // Add two new tabs in the same window.
+  openedTabs.push(await BrowserTestUtils.openNewForegroundTab(gBrowser, "about:blank"));
+  openedTabs.push(await BrowserTestUtils.openNewForegroundTab(gBrowser, "about:blank"));
+  checkTabCountHistogram(tabCountHist.snapshot(), [0, 0, 1, 1, 1], "TAB_COUNT telemetry - opening more tabs");
+
+  // Add a new window and then some tabs in it. A new window starts with one tab.
+  let win = await BrowserTestUtils.openNewBrowserWindow();
+  checkTabCountHistogram(tabCountHist.snapshot(), [0, 0, 1, 1, 1, 1], "TAB_COUNT telemetry - opening window");
+
+  openedTabs.push(await BrowserTestUtils.openNewForegroundTab(win.gBrowser, "about:blank"));
+  openedTabs.push(await BrowserTestUtils.openNewForegroundTab(win.gBrowser, "about:blank"));
+  openedTabs.push(await BrowserTestUtils.openNewForegroundTab(win.gBrowser, "about:blank"));
+  checkTabCountHistogram(tabCountHist.snapshot(), [0, 0, 1, 1, 1, 1, 1, 1, 1], "TAB_COUNT telemetry - opening more tabs in another window");
+
+  // Remove all the extra windows and tabs.
+  for (let openTab of openedTabs) {
+    await BrowserTestUtils.removeTab(openTab);
+  }
+  await BrowserTestUtils.closeWindow(win);
+});