summaryrefslogtreecommitdiffstats
path: root/devtools/client/aboutdebugging/test/browser/browser_aboutdebugging_telemetry_runtime_updates.js
blob: 51934550a837e52b27839e16e4768d0d91927154 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

/* import-globals-from helper-telemetry.js */
Services.scriptloader.loadSubScript(
  CHROME_URL_ROOT + "helper-telemetry.js",
  this
);

const DEVICE_A = "Device A";
const USB_RUNTIME_1 = {
  id: "runtime-id-1",
  deviceName: DEVICE_A,
  name: "Runtime 1",
  shortName: "R1",
};

const USB_RUNTIME_2 = {
  id: "runtime-id-2",
  deviceName: DEVICE_A,
  name: "Runtime 2",
  shortName: "R2",
};

const DEVICE_A_EXTRAS = {
  connection_type: "usb",
  device_name: DEVICE_A,
};

const RUNTIME_1_EXTRAS = {
  connection_type: "usb",
  device_name: USB_RUNTIME_1.deviceName,
  runtime_name: USB_RUNTIME_1.shortName,
};

const RUNTIME_2_EXTRAS = {
  connection_type: "usb",
  device_name: USB_RUNTIME_2.deviceName,
  runtime_name: USB_RUNTIME_2.shortName,
};

/**
 * Check that telemetry events are recorded for USB runtimes when:
 * - adding a device/runtime
 * - removing a device/runtime
 * - connecting to a runtime
 */
add_task(async function testUsbRuntimeUpdates() {
  // enable USB devices mocks
  const mocks = new Mocks();
  setupTelemetryTest();

  const { tab, document } = await openAboutDebugging();

  const sessionId = getOpenEventSessionId();
  ok(!isNaN(sessionId), "Open event has a valid session id");

  await addUsbRuntime(USB_RUNTIME_1, mocks, document);

  let evts = checkTelemetryEvents(
    [
      { method: "device_added", extras: DEVICE_A_EXTRAS },
      { method: "runtime_added", extras: RUNTIME_1_EXTRAS },
    ],
    sessionId
  );

  // Now that a first telemetry event has been logged for RUNTIME_1, retrieve the id
  // generated for telemetry, and check that we keep logging the same id for all events
  // related to runtime 1.
  const runtime1Id = evts.filter(e => e.method === "runtime_added")[0].extras
    .runtime_id;
  const runtime1Extras = Object.assign({}, RUNTIME_1_EXTRAS, {
    runtime_id: runtime1Id,
  });
  // Same as runtime1Extras, but the runtime name should be the complete one.
  const runtime1ConnectedExtras = Object.assign({}, runtime1Extras, {
    runtime_name: USB_RUNTIME_1.name,
  });

  await connectToRuntime(USB_RUNTIME_1.deviceName, document);

  checkTelemetryEvents(
    [
      { method: "runtime_connected", extras: runtime1ConnectedExtras },
      { method: "connection_attempt", extras: { status: "start" } },
      { method: "connection_attempt", extras: { status: "success" } },
    ],
    sessionId
  );

  info("Add a second runtime");
  await addUsbRuntime(USB_RUNTIME_2, mocks, document);
  evts = checkTelemetryEvents(
    [{ method: "runtime_added", extras: RUNTIME_2_EXTRAS }],
    sessionId
  );

  // Similar to what we did for RUNTIME_1,w e want to check we reuse the same telemetry id
  // for all the events related to RUNTIME_2.
  const runtime2Id = evts.filter(e => e.method === "runtime_added")[0].extras
    .runtime_id;
  const runtime2Extras = Object.assign({}, RUNTIME_2_EXTRAS, {
    runtime_id: runtime2Id,
  });

  info("Remove runtime 1");
  await removeUsbRuntime(USB_RUNTIME_1, mocks, document);

  checkTelemetryEvents(
    [
      { method: "runtime_disconnected", extras: runtime1ConnectedExtras },
      { method: "runtime_removed", extras: runtime1Extras },
    ],
    sessionId
  );

  info("Remove runtime 2");
  await removeUsbRuntime(USB_RUNTIME_2, mocks, document);

  checkTelemetryEvents(
    [
      { method: "runtime_removed", extras: runtime2Extras },
      { method: "device_removed", extras: DEVICE_A_EXTRAS },
    ],
    sessionId
  );

  await removeTab(tab);
});

async function addUsbRuntime(runtime, mocks, doc) {
  mocks.createUSBRuntime(runtime.id, {
    deviceName: runtime.deviceName,
    name: runtime.name,
    shortName: runtime.shortName,
  });
  mocks.emitUSBUpdate();

  info("Wait for the runtime to appear in the sidebar");
  await waitUntil(() => findSidebarItemByText(runtime.shortName, doc));
}

async function removeUsbRuntime(runtime, mocks, doc) {
  mocks.removeRuntime(runtime.id);
  mocks.emitUSBUpdate();
  await waitUntil(
    () =>
      !findSidebarItemByText(runtime.name, doc) &&
      !findSidebarItemByText(runtime.shortName, doc)
  );
}