summaryrefslogtreecommitdiffstats
path: root/devtools/client/aboutdebugging/test/browser/helper-telemetry.js
blob: ecb09fb24169f50eef4da1e44ba5c9e4586fec20 (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
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

/* import-globals-from head.js */

/**
 * Reset all telemetry events.
 */
function setupTelemetryTest() {
  // Let's reset the counts.
  Services.telemetry.clearEvents();

  // Ensure no events have been logged
  const ALL_CHANNELS = Ci.nsITelemetry.DATASET_ALL_CHANNELS;
  const snapshot = Services.telemetry.snapshotEvents(ALL_CHANNELS, true);
  ok(!snapshot.parent, "No events have been logged for the main process");
}
/* exported setupTelemetryTest */

/**
 * Check that the logged telemetry events exactly match the array of expected events.
 * Will compare the number of events, the event methods, and the event extras including
 * the about:debugging session id.
 */
function checkTelemetryEvents(expectedEvents, expectedSessionId) {
  const evts = readAboutDebuggingEvents();
  is(evts.length, expectedEvents.length, "Expected number of events");

  function _eventHasExpectedExtras(e, expectedEvent) {
    const expectedExtras = Object.keys(expectedEvent.extras);
    return expectedExtras.every(extra => {
      return e.extras[extra] === expectedEvent.extras[extra];
    });
  }

  for (const expectedEvent of expectedEvents) {
    const sameMethodEvents = evts.filter(
      e => e.method === expectedEvent.method
    );
    ok(
      !!sameMethodEvents.length,
      "Found event for method: " + expectedEvent.method
    );

    const sameExtrasEvents = sameMethodEvents.filter(e =>
      _eventHasExpectedExtras(e, expectedEvent)
    );
    Assert.strictEqual(
      sameExtrasEvents.length,
      1,
      "Found exactly one event matching the expected extras"
    );
    if (sameExtrasEvents.length === 0) {
      info(JSON.stringify(sameMethodEvents));
    }
    is(
      sameExtrasEvents[0].extras.session_id,
      expectedSessionId,
      "Select page event has the expected session"
    );
  }

  return evts;
}
/* exported checkTelemetryEvents */

/**
 * Retrieve the session id from an "open" event.
 * Note that calling this will "clear" all the events.
 */
function getOpenEventSessionId() {
  const openEvents = readAboutDebuggingEvents().filter(
    e => e.method === "open_adbg"
  );
  ok(!!openEvents[0], "Found an about:debugging open event");
  return openEvents[0].extras.session_id;
}
/* exported getOpenEventSessionId */

/**
 * Read all the pending events that have "aboutdebugging" as their object property.
 * WARNING: Calling this method also flushes/clears the events.
 */
function readAboutDebuggingEvents() {
  const ALL_CHANNELS = Ci.nsITelemetry.DATASET_ALL_CHANNELS;
  // Retrieve and clear telemetry events.
  const snapshot = Services.telemetry.snapshotEvents(ALL_CHANNELS, true);
  // about:debugging events are logged in the parent process
  const parentEvents = snapshot.parent || [];

  return parentEvents
    .map(_toEventObject)
    .filter(e => e.object === "aboutdebugging");
}
/* exported getLoggedEvents */

/**
 * The telemetry event data structure is simply an array. This helper remaps the array to
 * an object with more user friendly properties.
 */
function _toEventObject(rawEvent) {
  return {
    // Category is typically devtools.main for us.
    category: rawEvent[1],
    // Method is the event's name (eg open, select_page etc...)
    method: rawEvent[2],
    // Object will usually be aboutdebugging for our tests
    object: rawEvent[3],
    // Value is usually empty for devtools events
    value: rawEvent[4],
    // Extras contain all the details of the event, including the session_id.
    extras: rawEvent[5],
  };
}