1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/browser/devtools/shared/test/browser_telemetry_toolboxtabs_jsprofiler.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,110 @@ 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_toolboxtabs_jsprofiler.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: promise} = Cu.import("resource://gre/modules/devtools/deprecated-sync-thenables.js", {}); 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 + openToolboxTabTwice("jsprofiler", false); 1.33 +} 1.34 + 1.35 +function openToolboxTabTwice(id, secondPass) { 1.36 + let target = TargetFactory.forTab(gBrowser.selectedTab); 1.37 + 1.38 + gDevTools.showToolbox(target, id).then(function(toolbox) { 1.39 + info("Toolbox tab " + id + " opened"); 1.40 + 1.41 + toolbox.once("destroyed", function() { 1.42 + if (secondPass) { 1.43 + checkResults(); 1.44 + } else { 1.45 + openToolboxTabTwice(id, true); 1.46 + } 1.47 + }); 1.48 + // We use a timeout to check the tools active time 1.49 + setTimeout(function() { 1.50 + gDevTools.closeToolbox(target); 1.51 + }, TOOL_DELAY); 1.52 + }).then(null, reportError); 1.53 +} 1.54 + 1.55 +function checkResults() { 1.56 + let result = Telemetry.prototype.telemetryInfo; 1.57 + 1.58 + for (let [histId, value] of Iterator(result)) { 1.59 + if (histId.endsWith("OPENED_PER_USER_FLAG")) { 1.60 + ok(value.length === 1 && value[0] === true, 1.61 + "Per user value " + histId + " has a single value of true"); 1.62 + } else if (histId.endsWith("OPENED_BOOLEAN")) { 1.63 + ok(value.length > 1, histId + " has more than one entry"); 1.64 + 1.65 + let okay = value.every(function(element) { 1.66 + return element === true; 1.67 + }); 1.68 + 1.69 + ok(okay, "All " + histId + " entries are === true"); 1.70 + } else if (histId.endsWith("TIME_ACTIVE_SECONDS")) { 1.71 + ok(value.length > 1, histId + " has more than one entry"); 1.72 + 1.73 + let okay = value.every(function(element) { 1.74 + return element > 0; 1.75 + }); 1.76 + 1.77 + ok(okay, "All " + histId + " entries have time > 0"); 1.78 + } 1.79 + } 1.80 + 1.81 + finishUp(); 1.82 +} 1.83 + 1.84 +function reportError(error) { 1.85 + let stack = " " + error.stack.replace(/\n?.*?@/g, "\n JS frame :: "); 1.86 + 1.87 + ok(false, "ERROR: " + error + " at " + error.fileName + ":" + 1.88 + error.lineNumber + "\n\nStack trace:" + stack); 1.89 + finishUp(); 1.90 +} 1.91 + 1.92 +function finishUp() { 1.93 + gBrowser.removeCurrentTab(); 1.94 + 1.95 + Telemetry.prototype.log = Telemetry.prototype._oldlog; 1.96 + delete Telemetry.prototype._oldlog; 1.97 + delete Telemetry.prototype.telemetryInfo; 1.98 + 1.99 + TargetFactory = Services = promise = require = null; 1.100 + 1.101 + finish(); 1.102 +} 1.103 + 1.104 +function test() { 1.105 + waitForExplicitFinish(); 1.106 + gBrowser.selectedTab = gBrowser.addTab(); 1.107 + gBrowser.selectedBrowser.addEventListener("load", function() { 1.108 + gBrowser.selectedBrowser.removeEventListener("load", arguments.callee, true); 1.109 + waitForFocus(init, content); 1.110 + }, true); 1.111 + 1.112 + content.location = TEST_URI; 1.113 +}