1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/browser/devtools/shared/test/browser_telemetry_sidebar.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,115 @@ 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_sidebar.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 + testSidebar(); 1.33 +} 1.34 + 1.35 +function testSidebar() { 1.36 + info("Testing sidebar"); 1.37 + 1.38 + let target = TargetFactory.forTab(gBrowser.selectedTab); 1.39 + 1.40 + gDevTools.showToolbox(target, "inspector").then(function(toolbox) { 1.41 + let inspector = toolbox.getCurrentPanel(); 1.42 + let sidebarTools = ["ruleview", "computedview", "fontinspector", "layoutview"]; 1.43 + 1.44 + // Concatenate the array with itself so that we can open each tool twice. 1.45 + sidebarTools.push.apply(sidebarTools, sidebarTools); 1.46 + 1.47 + // See TOOL_DELAY for why we need setTimeout here 1.48 + setTimeout(function selectSidebarTab() { 1.49 + let tool = sidebarTools.pop(); 1.50 + if (tool) { 1.51 + inspector.sidebar.select(tool); 1.52 + setTimeout(function() { 1.53 + setTimeout(selectSidebarTab, TOOL_DELAY); 1.54 + }, TOOL_DELAY); 1.55 + } else { 1.56 + checkResults(); 1.57 + } 1.58 + }, TOOL_DELAY); 1.59 + }); 1.60 +} 1.61 + 1.62 +function checkResults() { 1.63 + let result = Telemetry.prototype.telemetryInfo; 1.64 + 1.65 + for (let [histId, value] of Iterator(result)) { 1.66 + if (histId.startsWith("DEVTOOLS_INSPECTOR_")) { 1.67 + // Inspector stats are tested in browser_telemetry_toolboxtabs.js so we 1.68 + // skip them here because we only open the inspector once for this test. 1.69 + continue; 1.70 + } 1.71 + 1.72 + if (histId.endsWith("OPENED_PER_USER_FLAG")) { 1.73 + ok(value.length === 1 && value[0] === true, 1.74 + "Per user value " + histId + " has a single value of true"); 1.75 + } else if (histId.endsWith("OPENED_BOOLEAN")) { 1.76 + ok(value.length > 1, histId + " has more than one entry"); 1.77 + 1.78 + let okay = value.every(function(element) { 1.79 + return element === true; 1.80 + }); 1.81 + 1.82 + ok(okay, "All " + histId + " entries are === true"); 1.83 + } else if (histId.endsWith("TIME_ACTIVE_SECONDS")) { 1.84 + ok(value.length > 1, histId + " has more than one entry"); 1.85 + 1.86 + let okay = value.every(function(element) { 1.87 + return element > 0; 1.88 + }); 1.89 + 1.90 + ok(okay, "All " + histId + " entries have time > 0"); 1.91 + } 1.92 + } 1.93 + 1.94 + finishUp(); 1.95 +} 1.96 + 1.97 +function finishUp() { 1.98 + gBrowser.removeCurrentTab(); 1.99 + 1.100 + Telemetry.prototype.log = Telemetry.prototype._oldlog; 1.101 + delete Telemetry.prototype._oldlog; 1.102 + delete Telemetry.prototype.telemetryInfo; 1.103 + 1.104 + TargetFactory = Services = promise = require = null; 1.105 + 1.106 + finish(); 1.107 +} 1.108 + 1.109 +function test() { 1.110 + waitForExplicitFinish(); 1.111 + gBrowser.selectedTab = gBrowser.addTab(); 1.112 + gBrowser.selectedBrowser.addEventListener("load", function() { 1.113 + gBrowser.selectedBrowser.removeEventListener("load", arguments.callee, true); 1.114 + waitForFocus(init, content); 1.115 + }, true); 1.116 + 1.117 + content.location = TEST_URI; 1.118 +}