Wed, 31 Dec 2014 06:09:35 +0100
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_responsive.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 function init() {
17 Telemetry.prototype.telemetryInfo = {};
18 Telemetry.prototype._oldlog = Telemetry.prototype.log;
19 Telemetry.prototype.log = function(histogramId, value) {
20 if (histogramId) {
21 if (!this.telemetryInfo[histogramId]) {
22 this.telemetryInfo[histogramId] = [];
23 }
25 this.telemetryInfo[histogramId].push(value);
26 }
27 };
29 testButton("command-button-responsive");
30 }
32 function testButton(id) {
33 info("Testing " + id);
35 let target = TargetFactory.forTab(gBrowser.selectedTab);
37 gDevTools.showToolbox(target, "inspector").then(function(toolbox) {
38 info("inspector opened");
40 let button = toolbox.doc.querySelector("#" + id);
41 ok(button, "Captain, we have the button");
43 delayedClicks(button, 4).then(function() {
44 checkResults("_RESPONSIVE_");
45 });
46 }).then(null, console.error);
47 }
49 function delayedClicks(node, clicks) {
50 let deferred = promise.defer();
51 let clicked = 0;
53 // See TOOL_DELAY for why we need setTimeout here
54 setTimeout(function delayedClick() {
55 info("Clicking button " + node.id);
56 node.click();
57 clicked++;
59 if (clicked >= clicks) {
60 deferred.resolve(node);
61 } else {
62 setTimeout(delayedClick, TOOL_DELAY);
63 }
64 }, TOOL_DELAY);
66 return deferred.promise;
67 }
69 function checkResults(histIdFocus) {
70 let result = Telemetry.prototype.telemetryInfo;
72 for (let [histId, value] of Iterator(result)) {
73 if (histId.startsWith("DEVTOOLS_INSPECTOR_") ||
74 !histId.contains(histIdFocus)) {
75 // Inspector stats are tested in
76 // browser_telemetry_toolboxtabs_{toolname}.js so we skip them here
77 // because we only open the inspector once for this test.
78 continue;
79 }
81 if (histId.endsWith("OPENED_PER_USER_FLAG")) {
82 ok(value.length === 1 && value[0] === true,
83 "Per user value " + histId + " has a single value of true");
84 } else if (histId.endsWith("OPENED_BOOLEAN")) {
85 ok(value.length > 1, histId + " has more than one entry");
87 let okay = value.every(function(element) {
88 return element === true;
89 });
91 ok(okay, "All " + histId + " entries are === true");
92 } else if (histId.endsWith("TIME_ACTIVE_SECONDS")) {
93 ok(value.length > 1, histId + " has more than one entry");
95 let okay = value.every(function(element) {
96 return element > 0;
97 });
99 ok(okay, "All " + histId + " entries have time > 0");
100 }
101 }
103 finishUp();
104 }
106 function finishUp() {
107 gBrowser.removeCurrentTab();
109 Telemetry.prototype.log = Telemetry.prototype._oldlog;
110 delete Telemetry.prototype._oldlog;
111 delete Telemetry.prototype.telemetryInfo;
113 TargetFactory = Services = promise = require = null;
115 finish();
116 }
118 function test() {
119 waitForExplicitFinish();
120 gBrowser.selectedTab = gBrowser.addTab();
121 gBrowser.selectedBrowser.addEventListener("load", function() {
122 gBrowser.selectedBrowser.removeEventListener("load", arguments.callee, true);
123 waitForFocus(init, content);
124 }, true);
126 content.location = TEST_URI;
127 }