browser/devtools/shared/test/browser_telemetry_button_scratchpad.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     1 /* Any copyright is dedicated to the Public Domain.
     2    http://creativecommons.org/publicdomain/zero/1.0/ */
     4 const TEST_URI = "data:text/html;charset=utf-8,<p>browser_telemetry_button_scratchpad.js</p>";
     6 // Because we need to gather stats for the period of time that a tool has been
     7 // opened we make use of setTimeout() to create tool active times.
     8 const TOOL_DELAY = 200;
    10 let promise = Cu.import("resource://gre/modules/devtools/deprecated-sync-thenables.js", {}).Promise;
    11 let {Services} = Cu.import("resource://gre/modules/Services.jsm", {});
    13 let require = Cu.import("resource://gre/modules/devtools/Loader.jsm", {}).devtools.require;
    14 let Telemetry = require("devtools/shared/telemetry");
    16 let numScratchpads = 0;
    18 function init() {
    19   Telemetry.prototype.telemetryInfo = {};
    20   Telemetry.prototype._oldlog = Telemetry.prototype.log;
    21   Telemetry.prototype.log = function(histogramId, value) {
    22     if (histogramId) {
    23       if (!this.telemetryInfo[histogramId]) {
    24         this.telemetryInfo[histogramId] = [];
    25       }
    27       this.telemetryInfo[histogramId].push(value);
    28     }
    29   };
    31   Services.ww.registerNotification(windowObserver);
    32   testButton("command-button-scratchpad");
    33 }
    35 function testButton(id) {
    36   info("Testing " + id);
    38   let target = TargetFactory.forTab(gBrowser.selectedTab);
    40   gDevTools.showToolbox(target, "inspector").then(function(toolbox) {
    41     info("inspector opened");
    43     let button = toolbox.doc.querySelector("#" + id);
    44     ok(button, "Captain, we have the button");
    46     delayedClicks(button, 4).then(null, console.error);
    47   }).then(null, console.error);
    48 }
    50 function windowObserver(aSubject, aTopic, aData) {
    51   if (aTopic == "domwindowopened") {
    52     let win = aSubject.QueryInterface(Ci.nsIDOMWindow);
    53     win.addEventListener("load", function onLoad() {
    54       win.removeEventListener("load", onLoad, false);
    56       if (win.Scratchpad) {
    57         win.Scratchpad.addObserver({
    58           onReady: function() {
    59             win.Scratchpad.removeObserver(this);
    60             numScratchpads++;
    61             win.close();
    63             info("another scratchpad was opened and closed, count is now " + numScratchpads);
    65             if (numScratchpads === 4) {
    66               Services.ww.unregisterNotification(windowObserver);
    67               info("4 scratchpads have been opened and closed, checking results");
    68               checkResults("_SCRATCHPAD_");
    69             }
    70           },
    71         });
    72       }
    73     }, false);
    74   }
    75 }
    77 function delayedClicks(node, clicks) {
    78   let deferred = promise.defer();
    79   let clicked = 0;
    81   // See TOOL_DELAY for why we need setTimeout here
    82   setTimeout(function delayedClick() {
    83     info("Clicking button " + node.id);
    84     node.click();
    85     clicked++;
    87     if (clicked >= clicks) {
    88       deferred.resolve(node);
    89     } else {
    90       setTimeout(delayedClick, TOOL_DELAY);
    91     }
    92   }, TOOL_DELAY);
    94   return deferred.promise;
    95 }
    97 function checkResults(histIdFocus) {
    98   let result = Telemetry.prototype.telemetryInfo;
   100   for (let [histId, value] of Iterator(result)) {
   101     if (histId.startsWith("DEVTOOLS_INSPECTOR_") ||
   102         !histId.contains(histIdFocus)) {
   103       // Inspector stats are tested in
   104       // browser_telemetry_toolboxtabs_{toolname}.js so we skip them here
   105       // because we only open the inspector once for this test.
   106       continue;
   107     }
   109     if (histId.endsWith("OPENED_PER_USER_FLAG")) {
   110       ok(value.length === 1 && value[0] === true,
   111          "Per user value " + histId + " has a single value of true");
   112     } else if (histId.endsWith("OPENED_BOOLEAN")) {
   113       ok(value.length > 1, histId + " has more than one entry");
   115       let okay = value.every(function(element) {
   116         return element === true;
   117       });
   119       ok(okay, "All " + histId + " entries are === true");
   120     } else if (histId.endsWith("TIME_ACTIVE_SECONDS")) {
   121       ok(value.length > 1, histId + " has more than one entry");
   123       let okay = value.every(function(element) {
   124         return element > 0;
   125       });
   127       ok(okay, "All " + histId + " entries have time > 0");
   128     }
   129   }
   131   finishUp();
   132 }
   134 function finishUp() {
   135   gBrowser.removeCurrentTab();
   137   Telemetry.prototype.log = Telemetry.prototype._oldlog;
   138   delete Telemetry.prototype._oldlog;
   139   delete Telemetry.prototype.telemetryInfo;
   141   TargetFactory = Services = promise = require = numScratchpads = null;
   143   finish();
   144 }
   146 function test() {
   147   waitForExplicitFinish();
   148   gBrowser.selectedTab = gBrowser.addTab();
   149   gBrowser.selectedBrowser.addEventListener("load", function() {
   150     gBrowser.selectedBrowser.removeEventListener("load", arguments.callee, true);
   151     waitForFocus(init, content);
   152   }, true);
   154   content.location = TEST_URI;
   155 }

mercurial