1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/browser/devtools/shared/test/browser_telemetry_button_scratchpad.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,155 @@ 1.4 +/* Any copyright is dedicated to the Public Domain. 1.5 + http://creativecommons.org/publicdomain/zero/1.0/ */ 1.6 + 1.7 +const TEST_URI = "data:text/html;charset=utf-8,<p>browser_telemetry_button_scratchpad.js</p>"; 1.8 + 1.9 +// Because we need to gather stats for the period of time that a tool has been 1.10 +// opened we make use of setTimeout() to create tool active times. 1.11 +const TOOL_DELAY = 200; 1.12 + 1.13 +let promise = Cu.import("resource://gre/modules/devtools/deprecated-sync-thenables.js", {}).Promise; 1.14 +let {Services} = Cu.import("resource://gre/modules/Services.jsm", {}); 1.15 + 1.16 +let require = Cu.import("resource://gre/modules/devtools/Loader.jsm", {}).devtools.require; 1.17 +let Telemetry = require("devtools/shared/telemetry"); 1.18 + 1.19 +let numScratchpads = 0; 1.20 + 1.21 +function init() { 1.22 + Telemetry.prototype.telemetryInfo = {}; 1.23 + Telemetry.prototype._oldlog = Telemetry.prototype.log; 1.24 + Telemetry.prototype.log = function(histogramId, value) { 1.25 + if (histogramId) { 1.26 + if (!this.telemetryInfo[histogramId]) { 1.27 + this.telemetryInfo[histogramId] = []; 1.28 + } 1.29 + 1.30 + this.telemetryInfo[histogramId].push(value); 1.31 + } 1.32 + }; 1.33 + 1.34 + Services.ww.registerNotification(windowObserver); 1.35 + testButton("command-button-scratchpad"); 1.36 +} 1.37 + 1.38 +function testButton(id) { 1.39 + info("Testing " + id); 1.40 + 1.41 + let target = TargetFactory.forTab(gBrowser.selectedTab); 1.42 + 1.43 + gDevTools.showToolbox(target, "inspector").then(function(toolbox) { 1.44 + info("inspector opened"); 1.45 + 1.46 + let button = toolbox.doc.querySelector("#" + id); 1.47 + ok(button, "Captain, we have the button"); 1.48 + 1.49 + delayedClicks(button, 4).then(null, console.error); 1.50 + }).then(null, console.error); 1.51 +} 1.52 + 1.53 +function windowObserver(aSubject, aTopic, aData) { 1.54 + if (aTopic == "domwindowopened") { 1.55 + let win = aSubject.QueryInterface(Ci.nsIDOMWindow); 1.56 + win.addEventListener("load", function onLoad() { 1.57 + win.removeEventListener("load", onLoad, false); 1.58 + 1.59 + if (win.Scratchpad) { 1.60 + win.Scratchpad.addObserver({ 1.61 + onReady: function() { 1.62 + win.Scratchpad.removeObserver(this); 1.63 + numScratchpads++; 1.64 + win.close(); 1.65 + 1.66 + info("another scratchpad was opened and closed, count is now " + numScratchpads); 1.67 + 1.68 + if (numScratchpads === 4) { 1.69 + Services.ww.unregisterNotification(windowObserver); 1.70 + info("4 scratchpads have been opened and closed, checking results"); 1.71 + checkResults("_SCRATCHPAD_"); 1.72 + } 1.73 + }, 1.74 + }); 1.75 + } 1.76 + }, false); 1.77 + } 1.78 +} 1.79 + 1.80 +function delayedClicks(node, clicks) { 1.81 + let deferred = promise.defer(); 1.82 + let clicked = 0; 1.83 + 1.84 + // See TOOL_DELAY for why we need setTimeout here 1.85 + setTimeout(function delayedClick() { 1.86 + info("Clicking button " + node.id); 1.87 + node.click(); 1.88 + clicked++; 1.89 + 1.90 + if (clicked >= clicks) { 1.91 + deferred.resolve(node); 1.92 + } else { 1.93 + setTimeout(delayedClick, TOOL_DELAY); 1.94 + } 1.95 + }, TOOL_DELAY); 1.96 + 1.97 + return deferred.promise; 1.98 +} 1.99 + 1.100 +function checkResults(histIdFocus) { 1.101 + let result = Telemetry.prototype.telemetryInfo; 1.102 + 1.103 + for (let [histId, value] of Iterator(result)) { 1.104 + if (histId.startsWith("DEVTOOLS_INSPECTOR_") || 1.105 + !histId.contains(histIdFocus)) { 1.106 + // Inspector stats are tested in 1.107 + // browser_telemetry_toolboxtabs_{toolname}.js so we skip them here 1.108 + // because we only open the inspector once for this test. 1.109 + continue; 1.110 + } 1.111 + 1.112 + if (histId.endsWith("OPENED_PER_USER_FLAG")) { 1.113 + ok(value.length === 1 && value[0] === true, 1.114 + "Per user value " + histId + " has a single value of true"); 1.115 + } else if (histId.endsWith("OPENED_BOOLEAN")) { 1.116 + ok(value.length > 1, histId + " has more than one entry"); 1.117 + 1.118 + let okay = value.every(function(element) { 1.119 + return element === true; 1.120 + }); 1.121 + 1.122 + ok(okay, "All " + histId + " entries are === true"); 1.123 + } else if (histId.endsWith("TIME_ACTIVE_SECONDS")) { 1.124 + ok(value.length > 1, histId + " has more than one entry"); 1.125 + 1.126 + let okay = value.every(function(element) { 1.127 + return element > 0; 1.128 + }); 1.129 + 1.130 + ok(okay, "All " + histId + " entries have time > 0"); 1.131 + } 1.132 + } 1.133 + 1.134 + finishUp(); 1.135 +} 1.136 + 1.137 +function finishUp() { 1.138 + gBrowser.removeCurrentTab(); 1.139 + 1.140 + Telemetry.prototype.log = Telemetry.prototype._oldlog; 1.141 + delete Telemetry.prototype._oldlog; 1.142 + delete Telemetry.prototype.telemetryInfo; 1.143 + 1.144 + TargetFactory = Services = promise = require = numScratchpads = null; 1.145 + 1.146 + finish(); 1.147 +} 1.148 + 1.149 +function test() { 1.150 + waitForExplicitFinish(); 1.151 + gBrowser.selectedTab = gBrowser.addTab(); 1.152 + gBrowser.selectedBrowser.addEventListener("load", function() { 1.153 + gBrowser.selectedBrowser.removeEventListener("load", arguments.callee, true); 1.154 + waitForFocus(init, content); 1.155 + }, true); 1.156 + 1.157 + content.location = TEST_URI; 1.158 +}