|
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_scratchpad.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 let numScratchpads = 0; |
|
17 |
|
18 function init() { |
|
19 Telemetry.prototype.telemetryInfo = {}; |
|
20 Telemetry.prototype._oldlog = Telemetry.prototype.log; |
|
21 Telemetry.prototype.log = function(histogramId, value) { |
|
22 if (histogramId) { |
|
23 if (!this.telemetryInfo[histogramId]) { |
|
24 this.telemetryInfo[histogramId] = []; |
|
25 } |
|
26 |
|
27 this.telemetryInfo[histogramId].push(value); |
|
28 } |
|
29 }; |
|
30 |
|
31 Services.ww.registerNotification(windowObserver); |
|
32 testButton("command-button-scratchpad"); |
|
33 } |
|
34 |
|
35 function testButton(id) { |
|
36 info("Testing " + id); |
|
37 |
|
38 let target = TargetFactory.forTab(gBrowser.selectedTab); |
|
39 |
|
40 gDevTools.showToolbox(target, "inspector").then(function(toolbox) { |
|
41 info("inspector opened"); |
|
42 |
|
43 let button = toolbox.doc.querySelector("#" + id); |
|
44 ok(button, "Captain, we have the button"); |
|
45 |
|
46 delayedClicks(button, 4).then(null, console.error); |
|
47 }).then(null, console.error); |
|
48 } |
|
49 |
|
50 function windowObserver(aSubject, aTopic, aData) { |
|
51 if (aTopic == "domwindowopened") { |
|
52 let win = aSubject.QueryInterface(Ci.nsIDOMWindow); |
|
53 win.addEventListener("load", function onLoad() { |
|
54 win.removeEventListener("load", onLoad, false); |
|
55 |
|
56 if (win.Scratchpad) { |
|
57 win.Scratchpad.addObserver({ |
|
58 onReady: function() { |
|
59 win.Scratchpad.removeObserver(this); |
|
60 numScratchpads++; |
|
61 win.close(); |
|
62 |
|
63 info("another scratchpad was opened and closed, count is now " + numScratchpads); |
|
64 |
|
65 if (numScratchpads === 4) { |
|
66 Services.ww.unregisterNotification(windowObserver); |
|
67 info("4 scratchpads have been opened and closed, checking results"); |
|
68 checkResults("_SCRATCHPAD_"); |
|
69 } |
|
70 }, |
|
71 }); |
|
72 } |
|
73 }, false); |
|
74 } |
|
75 } |
|
76 |
|
77 function delayedClicks(node, clicks) { |
|
78 let deferred = promise.defer(); |
|
79 let clicked = 0; |
|
80 |
|
81 // See TOOL_DELAY for why we need setTimeout here |
|
82 setTimeout(function delayedClick() { |
|
83 info("Clicking button " + node.id); |
|
84 node.click(); |
|
85 clicked++; |
|
86 |
|
87 if (clicked >= clicks) { |
|
88 deferred.resolve(node); |
|
89 } else { |
|
90 setTimeout(delayedClick, TOOL_DELAY); |
|
91 } |
|
92 }, TOOL_DELAY); |
|
93 |
|
94 return deferred.promise; |
|
95 } |
|
96 |
|
97 function checkResults(histIdFocus) { |
|
98 let result = Telemetry.prototype.telemetryInfo; |
|
99 |
|
100 for (let [histId, value] of Iterator(result)) { |
|
101 if (histId.startsWith("DEVTOOLS_INSPECTOR_") || |
|
102 !histId.contains(histIdFocus)) { |
|
103 // Inspector stats are tested in |
|
104 // browser_telemetry_toolboxtabs_{toolname}.js so we skip them here |
|
105 // because we only open the inspector once for this test. |
|
106 continue; |
|
107 } |
|
108 |
|
109 if (histId.endsWith("OPENED_PER_USER_FLAG")) { |
|
110 ok(value.length === 1 && value[0] === true, |
|
111 "Per user value " + histId + " has a single value of true"); |
|
112 } else if (histId.endsWith("OPENED_BOOLEAN")) { |
|
113 ok(value.length > 1, histId + " has more than one entry"); |
|
114 |
|
115 let okay = value.every(function(element) { |
|
116 return element === true; |
|
117 }); |
|
118 |
|
119 ok(okay, "All " + histId + " entries are === true"); |
|
120 } else if (histId.endsWith("TIME_ACTIVE_SECONDS")) { |
|
121 ok(value.length > 1, histId + " has more than one entry"); |
|
122 |
|
123 let okay = value.every(function(element) { |
|
124 return element > 0; |
|
125 }); |
|
126 |
|
127 ok(okay, "All " + histId + " entries have time > 0"); |
|
128 } |
|
129 } |
|
130 |
|
131 finishUp(); |
|
132 } |
|
133 |
|
134 function finishUp() { |
|
135 gBrowser.removeCurrentTab(); |
|
136 |
|
137 Telemetry.prototype.log = Telemetry.prototype._oldlog; |
|
138 delete Telemetry.prototype._oldlog; |
|
139 delete Telemetry.prototype.telemetryInfo; |
|
140 |
|
141 TargetFactory = Services = promise = require = numScratchpads = null; |
|
142 |
|
143 finish(); |
|
144 } |
|
145 |
|
146 function test() { |
|
147 waitForExplicitFinish(); |
|
148 gBrowser.selectedTab = gBrowser.addTab(); |
|
149 gBrowser.selectedBrowser.addEventListener("load", function() { |
|
150 gBrowser.selectedBrowser.removeEventListener("load", arguments.callee, true); |
|
151 waitForFocus(init, content); |
|
152 }, true); |
|
153 |
|
154 content.location = TEST_URI; |
|
155 } |