|
1 /* Any copyright is dedicated to the Public Domain. |
|
2 http://creativecommons.org/publicdomain/zero/1.0/ */ |
|
3 |
|
4 const TEST_URI = "data:text/html;charset=utf-8,<p>browser_telemetry_button_paintflashing.js</p>"; |
|
5 |
|
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; |
|
9 |
|
10 let promise = Cu.import("resource://gre/modules/devtools/deprecated-sync-thenables.js", {}).Promise; |
|
11 let {Services} = Cu.import("resource://gre/modules/Services.jsm", {}); |
|
12 |
|
13 let require = Cu.import("resource://gre/modules/devtools/Loader.jsm", {}).devtools.require; |
|
14 let Telemetry = require("devtools/shared/telemetry"); |
|
15 |
|
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 } |
|
24 |
|
25 this.telemetryInfo[histogramId].push(value); |
|
26 } |
|
27 }; |
|
28 |
|
29 testButton("command-button-paintflashing"); |
|
30 } |
|
31 |
|
32 function testButton(id) { |
|
33 info("Testing " + id); |
|
34 |
|
35 let target = TargetFactory.forTab(gBrowser.selectedTab); |
|
36 |
|
37 gDevTools.showToolbox(target, "inspector").then(function(toolbox) { |
|
38 info("inspector opened"); |
|
39 |
|
40 let button = toolbox.doc.querySelector("#" + id); |
|
41 ok(button, "Captain, we have the button"); |
|
42 |
|
43 delayedClicks(button, 4).then(function() { |
|
44 checkResults("_PAINTFLASHING_"); |
|
45 }); |
|
46 }).then(null, console.error); |
|
47 } |
|
48 |
|
49 function delayedClicks(node, clicks) { |
|
50 let deferred = promise.defer(); |
|
51 let clicked = 0; |
|
52 |
|
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++; |
|
58 |
|
59 if (clicked >= clicks) { |
|
60 deferred.resolve(node); |
|
61 } else { |
|
62 setTimeout(delayedClick, TOOL_DELAY); |
|
63 } |
|
64 }, TOOL_DELAY); |
|
65 |
|
66 return deferred.promise; |
|
67 } |
|
68 |
|
69 function checkResults(histIdFocus) { |
|
70 let result = Telemetry.prototype.telemetryInfo; |
|
71 |
|
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 } |
|
80 |
|
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"); |
|
86 |
|
87 let okay = value.every(function(element) { |
|
88 return element === true; |
|
89 }); |
|
90 |
|
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"); |
|
94 |
|
95 let okay = value.every(function(element) { |
|
96 return element > 0; |
|
97 }); |
|
98 |
|
99 ok(okay, "All " + histId + " entries have time > 0"); |
|
100 } |
|
101 } |
|
102 |
|
103 finishUp(); |
|
104 } |
|
105 |
|
106 function finishUp() { |
|
107 gBrowser.removeCurrentTab(); |
|
108 |
|
109 Telemetry.prototype.log = Telemetry.prototype._oldlog; |
|
110 delete Telemetry.prototype._oldlog; |
|
111 delete Telemetry.prototype.telemetryInfo; |
|
112 |
|
113 TargetFactory = Services = promise = require = null; |
|
114 |
|
115 finish(); |
|
116 } |
|
117 |
|
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); |
|
125 |
|
126 content.location = TEST_URI; |
|
127 } |