browser/devtools/shared/test/browser_telemetry_button_scratchpad.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial