tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

browser_telemetry_sidebar.js (5506B)


      1 /* Any copyright is dedicated to the Public Domain.
      2   http://creativecommons.org/publicdomain/zero/1.0/ */
      3 /* eslint-disable mozilla/no-arbitrary-setTimeout */
      4 
      5 "use strict";
      6 
      7 const TEST_URI =
      8  "data:text/html;charset=utf-8,<p>browser_telemetry_sidebar.js</p>";
      9 const ALL_CHANNELS = Ci.nsITelemetry.DATASET_ALL_CHANNELS;
     10 
     11 // Because we need to gather stats for the period of time that a tool has been
     12 // opened we make use of setTimeout() to create tool active times.
     13 const TOOL_DELAY = 200;
     14 
     15 const DATA = [
     16  {
     17    timestamp: null,
     18    category: "devtools.main",
     19    method: "sidepanel_changed",
     20    object: "inspector",
     21    value: null,
     22    extra: {
     23      oldpanel: "layoutview",
     24      newpanel: "animationinspector",
     25    },
     26  },
     27  {
     28    timestamp: null,
     29    category: "devtools.main",
     30    method: "sidepanel_changed",
     31    object: "inspector",
     32    value: null,
     33    extra: {
     34      oldpanel: "animationinspector",
     35      newpanel: "fontinspector",
     36    },
     37  },
     38  {
     39    timestamp: null,
     40    category: "devtools.main",
     41    method: "sidepanel_changed",
     42    object: "inspector",
     43    value: null,
     44    extra: {
     45      oldpanel: "fontinspector",
     46      newpanel: "layoutview",
     47    },
     48  },
     49  {
     50    timestamp: null,
     51    category: "devtools.main",
     52    method: "sidepanel_changed",
     53    object: "inspector",
     54    value: null,
     55    extra: {
     56      oldpanel: "layoutview",
     57      newpanel: "computedview",
     58    },
     59  },
     60  {
     61    timestamp: null,
     62    category: "devtools.main",
     63    method: "sidepanel_changed",
     64    object: "inspector",
     65    value: null,
     66    extra: {
     67      oldpanel: "computedview",
     68      newpanel: "animationinspector",
     69    },
     70  },
     71  {
     72    timestamp: null,
     73    category: "devtools.main",
     74    method: "sidepanel_changed",
     75    object: "inspector",
     76    value: null,
     77    extra: {
     78      oldpanel: "animationinspector",
     79      newpanel: "fontinspector",
     80    },
     81  },
     82  {
     83    timestamp: null,
     84    category: "devtools.main",
     85    method: "sidepanel_changed",
     86    object: "inspector",
     87    value: null,
     88    extra: {
     89      oldpanel: "fontinspector",
     90      newpanel: "layoutview",
     91    },
     92  },
     93  {
     94    timestamp: null,
     95    category: "devtools.main",
     96    method: "sidepanel_changed",
     97    object: "inspector",
     98    value: null,
     99    extra: {
    100      oldpanel: "layoutview",
    101      newpanel: "computedview",
    102    },
    103  },
    104 ];
    105 
    106 add_task(async function () {
    107  // Let's reset the counts.
    108  Services.telemetry.clearEvents();
    109 
    110  // Ensure no events have been logged
    111  const snapshot = Services.telemetry.snapshotEvents(ALL_CHANNELS, true);
    112  ok(!snapshot.parent, "No events have been logged for the main process");
    113 
    114  await addTab(TEST_URI);
    115  startTelemetry();
    116 
    117  const tab = gBrowser.selectedTab;
    118  const toolbox = await gDevTools.showToolboxForTab(tab, {
    119    toolId: "inspector",
    120  });
    121  info("inspector opened");
    122 
    123  await testSidebar(toolbox);
    124  checkResults();
    125  checkEventTelemetry();
    126 
    127  await toolbox.destroy();
    128  gBrowser.removeCurrentTab();
    129 });
    130 
    131 function testSidebar(toolbox) {
    132  info("Testing sidebar");
    133 
    134  const inspector = toolbox.getCurrentPanel();
    135  let sidebarTools = [
    136    "computedview",
    137    "layoutview",
    138    "fontinspector",
    139    "animationinspector",
    140  ];
    141 
    142  // Concatenate the array with itself so that we can open each tool twice.
    143  sidebarTools = [...sidebarTools, ...sidebarTools];
    144 
    145  return new Promise(resolve => {
    146    // See TOOL_DELAY for why we need setTimeout here
    147    setTimeout(function selectSidebarTab() {
    148      const tool = sidebarTools.pop();
    149      if (tool) {
    150        inspector.sidebar.select(tool);
    151        setTimeout(function () {
    152          setTimeout(selectSidebarTab, TOOL_DELAY);
    153        }, TOOL_DELAY);
    154      } else {
    155        resolve();
    156      }
    157    }, TOOL_DELAY);
    158  });
    159 }
    160 
    161 function checkResults() {
    162  // For help generating these tests use generateTelemetryTests("DEVTOOLS_")
    163  // here.
    164  checkTelemetry(
    165    "DEVTOOLS_INSPECTOR_OPENED_COUNT",
    166    "",
    167    { 0: 1, 1: 0 },
    168    "array"
    169  );
    170  checkTelemetry("DEVTOOLS_RULEVIEW_OPENED_COUNT", "", { 0: 1, 1: 0 }, "array");
    171  checkTelemetry(
    172    "DEVTOOLS_COMPUTEDVIEW_OPENED_COUNT",
    173    "",
    174    { 0: 2, 1: 0 },
    175    "array"
    176  );
    177  checkTelemetry(
    178    "DEVTOOLS_LAYOUTVIEW_OPENED_COUNT",
    179    "",
    180    { 0: 3, 1: 0 },
    181    "array"
    182  );
    183  checkTelemetry(
    184    "DEVTOOLS_FONTINSPECTOR_OPENED_COUNT",
    185    "",
    186    { 0: 2, 1: 0 },
    187    "array"
    188  );
    189  checkTelemetry(
    190    "DEVTOOLS_COMPUTEDVIEW_TIME_ACTIVE_SECONDS",
    191    "",
    192    null,
    193    "hasentries"
    194  );
    195  checkTelemetry(
    196    "DEVTOOLS_LAYOUTVIEW_TIME_ACTIVE_SECONDS",
    197    "",
    198    null,
    199    "hasentries"
    200  );
    201  checkTelemetry(
    202    "DEVTOOLS_FONTINSPECTOR_TIME_ACTIVE_SECONDS",
    203    "",
    204    null,
    205    "hasentries"
    206  );
    207 }
    208 
    209 function checkEventTelemetry() {
    210  const snapshot = Services.telemetry.snapshotEvents(ALL_CHANNELS, true);
    211  const events = snapshot.parent.filter(
    212    event =>
    213      event[1] === "devtools.main" &&
    214      event[2] === "sidepanel_changed" &&
    215      event[3] === "inspector" &&
    216      event[4] === null
    217  );
    218 
    219  for (const i in DATA) {
    220    const [timestamp, category, method, object, value, extra] = events[i];
    221    const expected = DATA[i];
    222 
    223    // ignore timestamp
    224    Assert.greater(timestamp, 0, "timestamp is greater than 0");
    225    is(category, expected.category, "category is correct");
    226    is(method, expected.method, "method is correct");
    227    is(object, expected.object, "object is correct");
    228    is(value, expected.value, "value is correct");
    229 
    230    is(extra.oldpanel, expected.extra.oldpanel, "oldpanel is correct");
    231    is(extra.newpanel, expected.extra.newpanel, "newpanel is correct");
    232  }
    233 }