summaryrefslogtreecommitdiffstats
path: root/browser/base/content/test/general/healthreport_testRemoteCommands.html
blob: 7978914f2be424c6e5a62dcda0401e6eac9f206b (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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
<html>
  <head>
    <meta charset="utf-8">
<script type="application/javascript;version=1.7"
            src="healthreport_pingData.js">
</script>
<script type="application/javascript;version=1.7">

function init() {
  window.addEventListener("message", doTest, false);
  doTest();
}

function checkSubmissionValue(payload, expectedValue) {
  return payload.enabled == expectedValue;
}

function isArray(arg) {
  return Object.prototype.toString.call(arg) === '[object Array]';
}

function writeDiagnostic(text) {
  let node = document.createTextNode(text);
  let br = document.createElement("br");
  document.body.appendChild(node);
  document.body.appendChild(br);
}

function validateCurrentTelemetryEnvironment(data) {
  // Simple check for now: check that the received object has the expected
  // top-level properties.
  const expectedKeys = ["profile", "settings", "system", "build", "partner", "addons"];
  return expectedKeys.every(key => (key in data));
}

function validateCurrentTelemetryPingData(ping) {
  // Simple check for now: check that the received object has the expected
  // top-level properties and that the type and reason match.
  const expectedKeys = ["environment", "clientId", "payload", "application",
                        "version", "type", "id"];
  return expectedKeys.every(key => (key in ping)) &&
         (ping.type == "main") &&
         ("info" in ping.payload) &&
         ("reason" in ping.payload.info) &&
         (ping.payload.info.reason == "gather-subsession-payload");
}

function validateTelemetryPingList(list) {
  if (!isArray(list)) {
    console.log("Telemetry ping list is not an array.");
    return false;
  }

  // Telemetry may generate other pings (e.g. "deletion" pings), so filter those
  // out.
  const TEST_TYPES_REGEX = /^test-telemetryArchive/;
  list = list.filter(p => TEST_TYPES_REGEX.test(p.type));

  if (list.length != TEST_PINGS.length) {
    console.log("Telemetry ping length is not correct.");
    return false;
  }

  let valid = true;
  for (let i=0; i<list.length; ++i) {
    let received = list[i];
    let expected = TEST_PINGS[i];
    if (received.type != expected.type ||
        received.timestampCreated != expected.date.getTime()) {
      writeDiagnostic("Telemetry ping " + i + " does not match.");
      writeDiagnostic("Expected: " + JSON.stringify(expected));
      writeDiagnostic("Received: " + JSON.stringify(received));
      valid = false;
    } else {
      writeDiagnostic("Telemetry ping " + i + " matches.");
    }
  }

  return true;
}

function validateTelemetryPingData(expected, received) {
  const receivedDate = new Date(received.creationDate);
  if (received.id != expected.id ||
      received.type != expected.type ||
      receivedDate.getTime() != expected.date.getTime()) {
    writeDiagnostic("Telemetry ping data for " + expected.id + " doesn't match.");
    writeDiagnostic("Expected: " + JSON.stringify(expected));
    writeDiagnostic("Received: " + JSON.stringify(received));
    return false;
  }

  writeDiagnostic("Telemetry ping data for " + expected.id + " matched.");
  return true;
}

var tests = [
{
  info: "Checking initial value is enabled",
  event: "RequestCurrentPrefs",
  payloadType: "prefs",
  validateResponse: function(payload) {
    return checkSubmissionValue(payload, true);
  },
},
{
  info: "Verifying disabling works",
  event: "DisableDataSubmission",
  payloadType: "prefs",
  validateResponse: function(payload) {
    return checkSubmissionValue(payload, false);
  },
},
{
  info: "Verifying we're still disabled",
  event: "RequestCurrentPrefs",
  payloadType: "prefs",
  validateResponse: function(payload) {
    return checkSubmissionValue(payload, false);
  },
},
{
  info: "Verifying that we can get the current ping data while submission is disabled",
  event: "RequestCurrentPingData",
  payloadType: "telemetry-current-ping-data",
  validateResponse: function(payload) {
    return validateCurrentTelemetryPingData(payload);
  },
},
{
  info: "Verifying enabling works",
  event: "EnableDataSubmission",
  payloadType: "prefs",
  validateResponse: function(payload) {
    return checkSubmissionValue(payload, true);
  },
},
{
  info: "Verifying we're still re-enabled",
  event: "RequestCurrentPrefs",
  payloadType: "prefs",
  validateResponse: function(payload) {
    return checkSubmissionValue(payload, true);
  },
},
{
  info: "Verifying that we can get the current Telemetry environment data",
  event: "RequestCurrentEnvironment",
  payloadType: "telemetry-current-environment-data",
  validateResponse: function(payload) {
    return validateCurrentTelemetryEnvironment(payload);
  },
},
{
  info: "Verifying that we can get the current Telemetry ping data",
  event: "RequestCurrentPingData",
  payloadType: "telemetry-current-ping-data",
  validateResponse: function(payload) {
    return validateCurrentTelemetryPingData(payload);
  },
},
{
  info: "Verifying that we get the proper Telemetry ping list",
  event: "RequestTelemetryPingList",
  payloadType: "telemetry-ping-list",
  validateResponse: function(payload) {
    // Validate the ping list
    if (!validateTelemetryPingList(payload)) {
      return false;
    }

    // Now that we received the ping ids, set up additional test tasks
    // that check loading the individual pings.
    for (let i=0; i<TEST_PINGS.length; ++i) {
      TEST_PINGS[i].id = payload[i].id;
      tests.push({
        info: "Verifying that we can get the proper Telemetry ping data #" + (i + 1),
        event: "RequestTelemetryPingData",
        eventData: { id: TEST_PINGS[i].id },
        payloadType: "telemetry-ping-data",
        validateResponse: function(payload) {
          return validateTelemetryPingData(TEST_PINGS[i], payload.pingData);
        },
      });
    }

    return true;
  },
},
];

var currentTest = -1;
function doTest(evt) {
  if (evt) {
    if (currentTest < 0 || !evt.data.content)
      return; // not yet testing

    var test = tests[currentTest];
    if (evt.data.type != test.payloadType)
      return; // skip unrequested events

    var error = JSON.stringify(evt.data.content);
    var pass = false;
    try {
      pass = test.validateResponse(evt.data.content)
    } catch (e) {}
    reportResult(test.info, pass, error);
  }
  // start the next test if there are any left
  if (tests[++currentTest])
    sendToBrowser(tests[currentTest].event, tests[currentTest].eventData);
  else
    reportFinished();
}

function reportResult(info, pass, error) {
  var data = {type: "testResult", info: info, pass: pass, error: error};
  var event = new CustomEvent("FirefoxHealthReportTestResponse", {detail: {data: data}, bubbles: true});
  document.dispatchEvent(event);
}

function reportFinished(cmd) {
  var data = {type: "testsComplete", count: tests.length};
  var event = new CustomEvent("FirefoxHealthReportTestResponse", {detail: {data: data}, bubbles: true});
  document.dispatchEvent(event);
}

function sendToBrowser(type, eventData) {
  eventData = eventData || {};
  let detail = {command: type};
  for (let key of Object.keys(eventData)) {
    detail[key] = eventData[key];
  }

  var event = new CustomEvent("RemoteHealthReportCommand", {detail: detail, bubbles: true});
  document.dispatchEvent(event);
}

</script>
  </head>
  <body onload="init()">
  </body>
</html>