tor-browser

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

browser_aboutdebugging_telemetry_runtime_updates.js (4343B)


      1 /* Any copyright is dedicated to the Public Domain.
      2 * http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 "use strict";
      5 
      6 /* import-globals-from helper-telemetry.js */
      7 Services.scriptloader.loadSubScript(
      8  CHROME_URL_ROOT + "helper-telemetry.js",
      9  this
     10 );
     11 
     12 const DEVICE_A = "Device A";
     13 const USB_RUNTIME_1 = {
     14  id: "runtime-id-1",
     15  deviceName: DEVICE_A,
     16  name: "Runtime 1",
     17  shortName: "R1",
     18 };
     19 
     20 const USB_RUNTIME_2 = {
     21  id: "runtime-id-2",
     22  deviceName: DEVICE_A,
     23  name: "Runtime 2",
     24  shortName: "R2",
     25 };
     26 
     27 const DEVICE_A_EXTRAS = {
     28  connection_type: "usb",
     29  device_name: DEVICE_A,
     30 };
     31 
     32 const RUNTIME_1_EXTRAS = {
     33  connection_type: "usb",
     34  device_name: USB_RUNTIME_1.deviceName,
     35  runtime_name: USB_RUNTIME_1.shortName,
     36 };
     37 
     38 const RUNTIME_2_EXTRAS = {
     39  connection_type: "usb",
     40  device_name: USB_RUNTIME_2.deviceName,
     41  runtime_name: USB_RUNTIME_2.shortName,
     42 };
     43 
     44 /**
     45 * Check that telemetry events are recorded for USB runtimes when:
     46 * - adding a device/runtime
     47 * - removing a device/runtime
     48 * - connecting to a runtime
     49 */
     50 add_task(async function testUsbRuntimeUpdates() {
     51  // enable USB devices mocks
     52  const mocks = new Mocks();
     53  setupTelemetryTest();
     54 
     55  const { tab, document } = await openAboutDebugging();
     56 
     57  const sessionId = getOpenEventSessionId();
     58  ok(!isNaN(sessionId), "Open event has a valid session id");
     59 
     60  await addUsbRuntime(USB_RUNTIME_1, mocks, document);
     61 
     62  let evts = checkTelemetryEvents(
     63    [
     64      { method: "device_added", extras: DEVICE_A_EXTRAS },
     65      { method: "runtime_added", extras: RUNTIME_1_EXTRAS },
     66    ],
     67    sessionId
     68  );
     69 
     70  // Now that a first telemetry event has been logged for RUNTIME_1, retrieve the id
     71  // generated for telemetry, and check that we keep logging the same id for all events
     72  // related to runtime 1.
     73  const runtime1Id = evts.filter(e => e.method === "runtime_added")[0].extras
     74    .runtime_id;
     75  const runtime1Extras = Object.assign({}, RUNTIME_1_EXTRAS, {
     76    runtime_id: runtime1Id,
     77  });
     78  // Same as runtime1Extras, but the runtime name should be the complete one.
     79  const runtime1ConnectedExtras = Object.assign({}, runtime1Extras, {
     80    runtime_name: USB_RUNTIME_1.name,
     81  });
     82 
     83  await connectToRuntime(USB_RUNTIME_1.deviceName, document);
     84 
     85  checkTelemetryEvents(
     86    [
     87      { method: "runtime_connected", extras: runtime1ConnectedExtras },
     88      { method: "connection_attempt", extras: { status: "start" } },
     89      { method: "connection_attempt", extras: { status: "success" } },
     90      { method: "select_page", extras: { page_type: "runtime" } },
     91    ],
     92    sessionId
     93  );
     94 
     95  info("Add a second runtime");
     96  await addUsbRuntime(USB_RUNTIME_2, mocks, document);
     97  evts = checkTelemetryEvents(
     98    [{ method: "runtime_added", extras: RUNTIME_2_EXTRAS }],
     99    sessionId
    100  );
    101 
    102  // Similar to what we did for RUNTIME_1,w e want to check we reuse the same telemetry id
    103  // for all the events related to RUNTIME_2.
    104  const runtime2Id = evts.filter(e => e.method === "runtime_added")[0].extras
    105    .runtime_id;
    106  const runtime2Extras = Object.assign({}, RUNTIME_2_EXTRAS, {
    107    runtime_id: runtime2Id,
    108  });
    109 
    110  info("Remove runtime 1");
    111  await removeUsbRuntime(USB_RUNTIME_1, mocks, document);
    112 
    113  checkTelemetryEvents(
    114    [
    115      { method: "select_page", extras: { page_type: "runtime" } },
    116      { method: "runtime_disconnected", extras: runtime1ConnectedExtras },
    117      { method: "runtime_removed", extras: runtime1Extras },
    118    ],
    119    sessionId
    120  );
    121 
    122  info("Remove runtime 2");
    123  await removeUsbRuntime(USB_RUNTIME_2, mocks, document);
    124 
    125  checkTelemetryEvents(
    126    [
    127      { method: "runtime_removed", extras: runtime2Extras },
    128      { method: "device_removed", extras: DEVICE_A_EXTRAS },
    129    ],
    130    sessionId
    131  );
    132 
    133  await removeTab(tab);
    134 });
    135 
    136 async function addUsbRuntime(runtime, mocks, doc) {
    137  mocks.createUSBRuntime(runtime.id, {
    138    deviceName: runtime.deviceName,
    139    name: runtime.name,
    140    shortName: runtime.shortName,
    141  });
    142  mocks.emitUSBUpdate();
    143 
    144  info("Wait for the runtime to appear in the sidebar");
    145  await waitUntil(() => findSidebarItemByText(runtime.shortName, doc));
    146 }
    147 
    148 async function removeUsbRuntime(runtime, mocks, doc) {
    149  mocks.removeRuntime(runtime.id);
    150  mocks.emitUSBUpdate();
    151  await waitUntil(
    152    () =>
    153      !findSidebarItemByText(runtime.name, doc) &&
    154      !findSidebarItemByText(runtime.shortName, doc)
    155  );
    156 }