1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/browser/devtools/shared/test/browser_telemetry_button_responsive.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,127 @@ 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_responsive.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 +function init() { 1.20 + Telemetry.prototype.telemetryInfo = {}; 1.21 + Telemetry.prototype._oldlog = Telemetry.prototype.log; 1.22 + Telemetry.prototype.log = function(histogramId, value) { 1.23 + if (histogramId) { 1.24 + if (!this.telemetryInfo[histogramId]) { 1.25 + this.telemetryInfo[histogramId] = []; 1.26 + } 1.27 + 1.28 + this.telemetryInfo[histogramId].push(value); 1.29 + } 1.30 + }; 1.31 + 1.32 + testButton("command-button-responsive"); 1.33 +} 1.34 + 1.35 +function testButton(id) { 1.36 + info("Testing " + id); 1.37 + 1.38 + let target = TargetFactory.forTab(gBrowser.selectedTab); 1.39 + 1.40 + gDevTools.showToolbox(target, "inspector").then(function(toolbox) { 1.41 + info("inspector opened"); 1.42 + 1.43 + let button = toolbox.doc.querySelector("#" + id); 1.44 + ok(button, "Captain, we have the button"); 1.45 + 1.46 + delayedClicks(button, 4).then(function() { 1.47 + checkResults("_RESPONSIVE_"); 1.48 + }); 1.49 + }).then(null, console.error); 1.50 +} 1.51 + 1.52 +function delayedClicks(node, clicks) { 1.53 + let deferred = promise.defer(); 1.54 + let clicked = 0; 1.55 + 1.56 + // See TOOL_DELAY for why we need setTimeout here 1.57 + setTimeout(function delayedClick() { 1.58 + info("Clicking button " + node.id); 1.59 + node.click(); 1.60 + clicked++; 1.61 + 1.62 + if (clicked >= clicks) { 1.63 + deferred.resolve(node); 1.64 + } else { 1.65 + setTimeout(delayedClick, TOOL_DELAY); 1.66 + } 1.67 + }, TOOL_DELAY); 1.68 + 1.69 + return deferred.promise; 1.70 +} 1.71 + 1.72 +function checkResults(histIdFocus) { 1.73 + let result = Telemetry.prototype.telemetryInfo; 1.74 + 1.75 + for (let [histId, value] of Iterator(result)) { 1.76 + if (histId.startsWith("DEVTOOLS_INSPECTOR_") || 1.77 + !histId.contains(histIdFocus)) { 1.78 + // Inspector stats are tested in 1.79 + // browser_telemetry_toolboxtabs_{toolname}.js so we skip them here 1.80 + // because we only open the inspector once for this test. 1.81 + continue; 1.82 + } 1.83 + 1.84 + if (histId.endsWith("OPENED_PER_USER_FLAG")) { 1.85 + ok(value.length === 1 && value[0] === true, 1.86 + "Per user value " + histId + " has a single value of true"); 1.87 + } else if (histId.endsWith("OPENED_BOOLEAN")) { 1.88 + ok(value.length > 1, histId + " has more than one entry"); 1.89 + 1.90 + let okay = value.every(function(element) { 1.91 + return element === true; 1.92 + }); 1.93 + 1.94 + ok(okay, "All " + histId + " entries are === true"); 1.95 + } else if (histId.endsWith("TIME_ACTIVE_SECONDS")) { 1.96 + ok(value.length > 1, histId + " has more than one entry"); 1.97 + 1.98 + let okay = value.every(function(element) { 1.99 + return element > 0; 1.100 + }); 1.101 + 1.102 + ok(okay, "All " + histId + " entries have time > 0"); 1.103 + } 1.104 + } 1.105 + 1.106 + finishUp(); 1.107 +} 1.108 + 1.109 +function finishUp() { 1.110 + gBrowser.removeCurrentTab(); 1.111 + 1.112 + Telemetry.prototype.log = Telemetry.prototype._oldlog; 1.113 + delete Telemetry.prototype._oldlog; 1.114 + delete Telemetry.prototype.telemetryInfo; 1.115 + 1.116 + TargetFactory = Services = promise = require = null; 1.117 + 1.118 + finish(); 1.119 +} 1.120 + 1.121 +function test() { 1.122 + waitForExplicitFinish(); 1.123 + gBrowser.selectedTab = gBrowser.addTab(); 1.124 + gBrowser.selectedBrowser.addEventListener("load", function() { 1.125 + gBrowser.selectedBrowser.removeEventListener("load", arguments.callee, true); 1.126 + waitForFocus(init, content); 1.127 + }, true); 1.128 + 1.129 + content.location = TEST_URI; 1.130 +}