michael@0: /* Any copyright is dedicated to the Public Domain. michael@0: http://creativecommons.org/publicdomain/zero/1.0/ */ michael@0: michael@0: const TEST_URI = "data:text/html;charset=utf-8,
browser_telemetry_button_scratchpad.js
"; michael@0: michael@0: // Because we need to gather stats for the period of time that a tool has been michael@0: // opened we make use of setTimeout() to create tool active times. michael@0: const TOOL_DELAY = 200; michael@0: michael@0: let promise = Cu.import("resource://gre/modules/devtools/deprecated-sync-thenables.js", {}).Promise; michael@0: let {Services} = Cu.import("resource://gre/modules/Services.jsm", {}); michael@0: michael@0: let require = Cu.import("resource://gre/modules/devtools/Loader.jsm", {}).devtools.require; michael@0: let Telemetry = require("devtools/shared/telemetry"); michael@0: michael@0: let numScratchpads = 0; michael@0: michael@0: function init() { michael@0: Telemetry.prototype.telemetryInfo = {}; michael@0: Telemetry.prototype._oldlog = Telemetry.prototype.log; michael@0: Telemetry.prototype.log = function(histogramId, value) { michael@0: if (histogramId) { michael@0: if (!this.telemetryInfo[histogramId]) { michael@0: this.telemetryInfo[histogramId] = []; michael@0: } michael@0: michael@0: this.telemetryInfo[histogramId].push(value); michael@0: } michael@0: }; michael@0: michael@0: Services.ww.registerNotification(windowObserver); michael@0: testButton("command-button-scratchpad"); michael@0: } michael@0: michael@0: function testButton(id) { michael@0: info("Testing " + id); michael@0: michael@0: let target = TargetFactory.forTab(gBrowser.selectedTab); michael@0: michael@0: gDevTools.showToolbox(target, "inspector").then(function(toolbox) { michael@0: info("inspector opened"); michael@0: michael@0: let button = toolbox.doc.querySelector("#" + id); michael@0: ok(button, "Captain, we have the button"); michael@0: michael@0: delayedClicks(button, 4).then(null, console.error); michael@0: }).then(null, console.error); michael@0: } michael@0: michael@0: function windowObserver(aSubject, aTopic, aData) { michael@0: if (aTopic == "domwindowopened") { michael@0: let win = aSubject.QueryInterface(Ci.nsIDOMWindow); michael@0: win.addEventListener("load", function onLoad() { michael@0: win.removeEventListener("load", onLoad, false); michael@0: michael@0: if (win.Scratchpad) { michael@0: win.Scratchpad.addObserver({ michael@0: onReady: function() { michael@0: win.Scratchpad.removeObserver(this); michael@0: numScratchpads++; michael@0: win.close(); michael@0: michael@0: info("another scratchpad was opened and closed, count is now " + numScratchpads); michael@0: michael@0: if (numScratchpads === 4) { michael@0: Services.ww.unregisterNotification(windowObserver); michael@0: info("4 scratchpads have been opened and closed, checking results"); michael@0: checkResults("_SCRATCHPAD_"); michael@0: } michael@0: }, michael@0: }); michael@0: } michael@0: }, false); michael@0: } michael@0: } michael@0: michael@0: function delayedClicks(node, clicks) { michael@0: let deferred = promise.defer(); michael@0: let clicked = 0; michael@0: michael@0: // See TOOL_DELAY for why we need setTimeout here michael@0: setTimeout(function delayedClick() { michael@0: info("Clicking button " + node.id); michael@0: node.click(); michael@0: clicked++; michael@0: michael@0: if (clicked >= clicks) { michael@0: deferred.resolve(node); michael@0: } else { michael@0: setTimeout(delayedClick, TOOL_DELAY); michael@0: } michael@0: }, TOOL_DELAY); michael@0: michael@0: return deferred.promise; michael@0: } michael@0: michael@0: function checkResults(histIdFocus) { michael@0: let result = Telemetry.prototype.telemetryInfo; michael@0: michael@0: for (let [histId, value] of Iterator(result)) { michael@0: if (histId.startsWith("DEVTOOLS_INSPECTOR_") || michael@0: !histId.contains(histIdFocus)) { michael@0: // Inspector stats are tested in michael@0: // browser_telemetry_toolboxtabs_{toolname}.js so we skip them here michael@0: // because we only open the inspector once for this test. michael@0: continue; michael@0: } michael@0: michael@0: if (histId.endsWith("OPENED_PER_USER_FLAG")) { michael@0: ok(value.length === 1 && value[0] === true, michael@0: "Per user value " + histId + " has a single value of true"); michael@0: } else if (histId.endsWith("OPENED_BOOLEAN")) { michael@0: ok(value.length > 1, histId + " has more than one entry"); michael@0: michael@0: let okay = value.every(function(element) { michael@0: return element === true; michael@0: }); michael@0: michael@0: ok(okay, "All " + histId + " entries are === true"); michael@0: } else if (histId.endsWith("TIME_ACTIVE_SECONDS")) { michael@0: ok(value.length > 1, histId + " has more than one entry"); michael@0: michael@0: let okay = value.every(function(element) { michael@0: return element > 0; michael@0: }); michael@0: michael@0: ok(okay, "All " + histId + " entries have time > 0"); michael@0: } michael@0: } michael@0: michael@0: finishUp(); michael@0: } michael@0: michael@0: function finishUp() { michael@0: gBrowser.removeCurrentTab(); michael@0: michael@0: Telemetry.prototype.log = Telemetry.prototype._oldlog; michael@0: delete Telemetry.prototype._oldlog; michael@0: delete Telemetry.prototype.telemetryInfo; michael@0: michael@0: TargetFactory = Services = promise = require = numScratchpads = null; michael@0: michael@0: finish(); michael@0: } michael@0: michael@0: function test() { michael@0: waitForExplicitFinish(); michael@0: gBrowser.selectedTab = gBrowser.addTab(); michael@0: gBrowser.selectedBrowser.addEventListener("load", function() { michael@0: gBrowser.selectedBrowser.removeEventListener("load", arguments.callee, true); michael@0: waitForFocus(init, content); michael@0: }, true); michael@0: michael@0: content.location = TEST_URI; michael@0: }