Bug 1301452 - Test written for tab spinner probes draft
authorKirk Steuber <ksteuber@mozilla.com>
Fri, 16 Sep 2016 15:55:55 -0700
changeset 416182 8a7a56d1402d5b8a82d1eac0f636de62cfc3b810
parent 415108 fd0564234eca242b7fb753a110312679020f8059
child 531776 837e95d97dd531df7b65123f09f4bb60a7bb9478
push id30057
push userksteuber@mozilla.com
push dateWed, 21 Sep 2016 16:49:59 +0000
bugs1301452
milestone52.0a1
Bug 1301452 - Test written for tab spinner probes MozReview-Commit-ID: C7oEA6ofntz
browser/base/content/test/tabs/browser.ini
browser/base/content/test/tabs/browser_tabSpinnerProbe.js
browser/base/moz.build
new file mode 100644
--- /dev/null
+++ b/browser/base/content/test/tabs/browser.ini
@@ -0,0 +1,2 @@
+[browser_tabSpinnerProbe.js]
+skip-if = !e10s # Tab spinner is e10s only.
new file mode 100644
--- /dev/null
+++ b/browser/base/content/test/tabs/browser_tabSpinnerProbe.js
@@ -0,0 +1,87 @@
+"use strict";
+
+/**
+ * Tests the FX_TAB_SWITCH_SPINNER_VISIBLE_MS and
+ * FX_TAB_SWITCH_SPINNER_VISIBLE_LONG_MS telemetry probes
+ */
+let gMinHangTime = 500; // ms
+let gMaxHangTime = 5 * 1000; // ms
+
+/**
+ * Make a data URI for a generic webpage with a script that hangs for a given
+ * amount of time.
+ * @param  {?Number} aHangMs Number of milliseconds that the hang should last.
+ *                   Defaults to 0.
+ * @return {String}  The data URI generated.
+ */
+function makeDataURI(aHangMs = 0) {
+  return `data:text/html,
+    <html>
+      <head>
+        <meta charset="utf-8"/>
+        <title>Tab Spinner Test</title>
+        <script>
+          function hang() {
+            let hangDuration = ${aHangMs};
+            if (hangDuration > 0) {
+              let startTime = window.performance.now();
+              while(window.performance.now() - startTime < hangDuration) {}
+            }
+          }
+        </script>
+      </head>
+      <body>
+        <h1 id='header'>Tab Spinner Test</h1>
+      </body>
+    </html>`;
+}
+
+/**
+ * Returns the sum of all values in an array.
+ * @param  {Array}  aArray An array of integers
+ * @return {Number} The sum of the integers in the array
+ */
+function sum(aArray) {
+  return aArray.reduce(function(previousValue, currentValue) {
+    return previousValue + currentValue;
+  });
+}
+
+/**
+ * A generator intended to be run as a Task. It tests one of the tab spinner
+ * telemetry probes.
+ * @param {String} aProbe The probe to test. Should be one of:
+ *                  - FX_TAB_SWITCH_SPINNER_VISIBLE_MS
+ *                  - FX_TAB_SWITCH_SPINNER_VISIBLE_LONG_MS
+ */
+function* testProbe(aProbe) {
+  info(`Testing probe: ${aProbe}`);
+  let histogram = Services.telemetry.getHistogramById(aProbe);
+  let buckets = histogram.snapshot().ranges.filter(function(value) {
+    return (value > gMinHangTime && value < gMaxHangTime);
+  });
+  let delayTime = buckets[0]; // Pick a bucket arbitrarily
+
+  // The tab spinner does not show up instantly. We need to hang for a little
+  // bit of extra time to account for the tab spinner delay.
+  delayTime += gBrowser.selectedTab.linkedBrowser.getTabBrowser()._getSwitcher().TAB_SWITCH_TIMEOUT;
+  let dataURI1 = makeDataURI(delayTime);
+  let dataURI2 = makeDataURI();
+
+  let tab1 = yield BrowserTestUtils.openNewForegroundTab(gBrowser, dataURI1);
+  histogram.clear();
+  // Queue a hang in the content process when the
+  // event loop breathes next.
+  ContentTask.spawn(tab1.linkedBrowser, null, function*() {
+    content.wrappedJSObject.hang();
+  });
+  let tab2 = yield BrowserTestUtils.openNewForegroundTab(gBrowser, dataURI2);
+  let snapshot = histogram.snapshot();
+  yield BrowserTestUtils.removeTab(tab2);
+  yield BrowserTestUtils.removeTab(tab1);
+  ok(sum(snapshot.counts) > 0,
+   `Spinner probe should now have a value in some bucket`);
+}
+
+add_task(testProbe.bind(null, "FX_TAB_SWITCH_SPINNER_VISIBLE_MS"));
+add_task(testProbe.bind(null, "FX_TAB_SWITCH_SPINNER_VISIBLE_LONG_MS"));
--- a/browser/base/moz.build
+++ b/browser/base/moz.build
@@ -18,16 +18,17 @@ BROWSER_CHROME_MANIFESTS += [
     'content/test/alerts/browser.ini',
     'content/test/general/browser.ini',
     'content/test/newtab/browser.ini',
     'content/test/plugins/browser.ini',
     'content/test/popupNotifications/browser.ini',
     'content/test/referrer/browser.ini',
     'content/test/social/browser.ini',
     'content/test/tabPrompts/browser.ini',
+    'content/test/tabs/browser.ini',
     'content/test/urlbar/browser.ini',
     'content/test/webrtc/browser.ini',
 ]
 
 DEFINES['MOZ_APP_VERSION'] = CONFIG['MOZ_APP_VERSION']
 DEFINES['MOZ_APP_VERSION_DISPLAY'] = CONFIG['MOZ_APP_VERSION_DISPLAY']
 
 DEFINES['APP_LICENSE_BLOCK'] = '%s/content/overrides/app-license.html' % SRCDIR