summaryrefslogtreecommitdiffstats
path: root/devtools/client/webide/test/test_telemetry.html
blob: 225ddb89b370b2d43dbc86b6f0ac73b4d3489438 (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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
<!DOCTYPE html>

<html>

  <head>
    <meta charset="utf8">
    <title></title>

    <script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
    <script type="application/javascript" src="chrome://mochikit/content/chrome-harness.js"></script>
    <script type="application/javascript;version=1.8" src="head.js"></script>
    <link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css">
  </head>

  <body>

    <script type="application/javascript;version=1.8">
      const Telemetry = require("devtools/client/shared/telemetry");
      const { _DeprecatedUSBRuntime, _WiFiRuntime, _SimulatorRuntime,
              _gRemoteRuntime, _gLocalRuntime, RuntimeTypes }
            = require("devtools/client/webide/modules/runtimes");

      // Because we need to gather stats for the period of time that a tool has
      // been opened we make use of setTimeout() to create tool active times.
      const TOOL_DELAY = 200;

      function patchTelemetry() {
        Telemetry.prototype.telemetryInfo = {};
        Telemetry.prototype._oldlog = Telemetry.prototype.log;
        Telemetry.prototype.log = function(histogramId, value) {
          if (histogramId) {
            if (!this.telemetryInfo[histogramId]) {
              this.telemetryInfo[histogramId] = [];
            }
            this.telemetryInfo[histogramId].push(value);
          }
        }
        Telemetry.prototype._oldlogKeyed = Telemetry.prototype.logKeyed;
        Telemetry.prototype.logKeyed = function(histogramId, key, value) {
          // This simple reduction is enough to test WebIDE's usage
          this.log(`${histogramId}|${key}`, value);
        }
      }

      function resetTelemetry() {
        Telemetry.prototype.log = Telemetry.prototype._oldlog;
        Telemetry.prototype.logKeyed = Telemetry.prototype._oldlogKeyed;
        delete Telemetry.prototype._oldlog;
        delete Telemetry.prototype._oldlogKeyed;
        delete Telemetry.prototype.telemetryInfo;
      }

      function cycleWebIDE() {
        return Task.spawn(function*() {
          let win = yield openWebIDE();
          // Wait a bit, so we're open for a non-zero time
          yield waitForTime(TOOL_DELAY);
          yield closeWebIDE(win);
        });
      }

      function addFakeRuntimes(win) {
        // We use the real runtimes here (and switch out some functionality)
        // so we can ensure that logging happens as it would in real use.

        let usb = new _DeprecatedUSBRuntime("fakeUSB");
        // Use local pipe instead
        usb.connect = function(connection) {
          ok(connection, win.AppManager.connection, "connection is valid");
          connection.host = null; // force connectPipe
          connection.connect();
          return promise.resolve();
        };
        win.AppManager.runtimeList.usb.push(usb);

        let wifi = new _WiFiRuntime("fakeWiFi");
        // Use local pipe instead
        wifi.connect = function(connection) {
          ok(connection, win.AppManager.connection, "connection is valid");
          connection.host = null; // force connectPipe
          connection.connect();
          return promise.resolve();
        };
        win.AppManager.runtimeList.wifi.push(wifi);

        let sim = new _SimulatorRuntime({ id: "fakeSimulator" });
        // Use local pipe instead
        sim.connect = function(connection) {
          ok(connection, win.AppManager.connection, "connection is valid");
          connection.host = null; // force connectPipe
          connection.connect();
          return promise.resolve();
        };
        Object.defineProperty(sim, "name", {
          get() {
            return this.version;
          }
        });
        win.AppManager.runtimeList.simulator.push(sim);

        let remote = _gRemoteRuntime;
        // Use local pipe instead
        remote.connect = function(connection) {
          ok(connection, win.AppManager.connection, "connection is valid");
          connection.host = null; // force connectPipe
          connection.connect();
          return promise.resolve();
        };
        let local = _gLocalRuntime;

        let other = Object.create(_gLocalRuntime);
        other.type = RuntimeTypes.OTHER;

        win.AppManager.runtimeList.other = [remote, local, other];

        win.AppManager.update("runtime-list");
      }

      function addTestApp(win) {
        return Task.spawn(function*() {
          let packagedAppLocation = getTestFilePath("../app");
          let winProject = getProjectWindow(win);
          let onValidated = waitForUpdate(win, "project-validated");
          let onDetails = waitForUpdate(win, "details");
          yield winProject.projectList.importPackagedApp(packagedAppLocation);
          yield onValidated;
          yield onDetails;
        });
      }

      function startConnection(win, docRuntime, type, index) {
        let panelNode = docRuntime.querySelector("#runtime-panel");
        let items = panelNode.querySelectorAll(".runtime-panel-item-" + type);
        if (index === undefined) {
          is(items.length, 1, "Found one runtime button");
        }

        let deferred = promise.defer();
        win.AppManager.connection.once(
            win.Connection.Events.CONNECTED,
            () => deferred.resolve());

        items[index || 0].click();

        return deferred.promise;
      }

      function waitUntilConnected(win) {
        return Task.spawn(function*() {
          ok(win.document.querySelector("window").className, "busy", "UI is busy");
          yield win.UI._busyPromise;
          is(Object.keys(DebuggerServer._connections).length, 1, "Connected");
          // Logging runtime info needs to use the device actor
          yield waitForUpdate(win, "runtime-global-actors");
          // Ensure detailed telemetry is recorded
          yield waitForUpdate(win, "runtime-telemetry");
        });
      }

      function connectToRuntime(win, docRuntime, type, index) {
        return Task.spawn(function*() {
          startConnection(win, docRuntime, type, index);
          yield waitUntilConnected(win);
        });
      }

      function checkResults() {
        let result = Telemetry.prototype.telemetryInfo;
        for (let [histId, value] of Iterator(result)) {
          if (histId === "DEVTOOLS_WEBIDE_IMPORT_PROJECT_BOOLEAN") {
            ok(value.length === 1 && !!value[0],
               histId + " has 1 successful entry");
          } else if (histId ===
                     "DEVTOOLS_WEBIDE_PROJECT_EDITOR_OPENED_COUNT") {
            ok(value.length === 1 && !!value[0],
               histId + " has 1 successful entry");
          } else if (histId === "DEVTOOLS_WEBIDE_OPENED_COUNT") {
            ok(value.length > 1, histId + " has more than one entry");

            let okay = value.every(function(element) {
              return !!element;
            });

            ok(okay, "All " + histId + " entries are true");
          } else if (histId.endsWith("WEBIDE_TIME_ACTIVE_SECONDS")) {
            ok(value.length > 1, histId + " has more than one entry");

            let okay = value.every(function(element) {
              return element > 0;
            });

            ok(okay, "All " + histId + " entries have time > 0");
          } else if (histId.endsWith("EDITOR_TIME_ACTIVE_SECONDS")) {
            ok(value.length === 1 && value[0] > 0,
               histId + " has 1 entry with time > 0");
          } else if (histId === "DEVTOOLS_WEBIDE_CONNECTION_RESULT") {
            ok(value.length === 6, histId + " has 6 connection results");

            let okay = value.every(function(element) {
              return !!element;
            });

            ok(okay, "All " + histId + " connections succeeded");
          } else if (histId.endsWith("CONNECTION_RESULT")) {
            ok(value.length === 1 && !!value[0],
               histId + " has 1 successful connection");
          } else if (histId === "DEVTOOLS_WEBIDE_CONNECTION_TIME_SECONDS") {
            ok(value.length === 6, histId + " has 6 connection results");

            let okay = value.every(function(element) {
              return element > 0;
            });

            ok(okay, "All " + histId + " connections have time > 0");
          } else if (histId.endsWith("USED")) {
            ok(value.length === 6, histId + " has 6 connection actions");

            let okay = value.every(function(element) {
              return !element;
            });

            ok(okay, "All " + histId + " actions were skipped");
          } else if (histId === "DEVTOOLS_WEBIDE_CONNECTED_RUNTIME_TYPE|USB") {
            is(value.length, 1, histId + " has 1 connection results");
          } else if (histId === "DEVTOOLS_WEBIDE_CONNECTED_RUNTIME_TYPE|WIFI") {
            is(value.length, 1, histId + " has 1 connection results");
          } else if (histId === "DEVTOOLS_WEBIDE_CONNECTED_RUNTIME_TYPE|SIMULATOR") {
            is(value.length, 1, histId + " has 1 connection results");
          } else if (histId === "DEVTOOLS_WEBIDE_CONNECTED_RUNTIME_TYPE|REMOTE") {
            is(value.length, 1, histId + " has 1 connection results");
          } else if (histId === "DEVTOOLS_WEBIDE_CONNECTED_RUNTIME_TYPE|LOCAL") {
            is(value.length, 1, histId + " has 1 connection results");
          } else if (histId === "DEVTOOLS_WEBIDE_CONNECTED_RUNTIME_TYPE|OTHER") {
            is(value.length, 1, histId + " has 1 connection results");
          } else if (histId === "DEVTOOLS_WEBIDE_CONNECTED_RUNTIME_ID|fakeUSB") {
            is(value.length, 1, histId + " has 1 connection results");
          } else if (histId === "DEVTOOLS_WEBIDE_CONNECTED_RUNTIME_ID|fakeWiFi") {
            is(value.length, 1, histId + " has 1 connection results");
          } else if (histId === "DEVTOOLS_WEBIDE_CONNECTED_RUNTIME_ID|fakeSimulator") {
            is(value.length, 1, histId + " has 1 connection results");
          } else if (histId === "DEVTOOLS_WEBIDE_CONNECTED_RUNTIME_ID|unknown") {
            is(value.length, 1, histId + " has 1 connection results");
          } else if (histId === "DEVTOOLS_WEBIDE_CONNECTED_RUNTIME_ID|local") {
            is(value.length, 2, histId + " has 2 connection results");
          } else if (histId.startsWith("DEVTOOLS_WEBIDE_CONNECTED_RUNTIME_PROCESSOR")) {
            let processor = histId.split("|")[1];
            is(processor, Services.appinfo.XPCOMABI.split("-")[0], "Found runtime processor");
            is(value.length, 6, histId + " has 6 connection results");
          } else if (histId.startsWith("DEVTOOLS_WEBIDE_CONNECTED_RUNTIME_OS")) {
            let os = histId.split("|")[1];
            is(os, Services.appinfo.OS, "Found runtime OS");
            is(value.length, 6, histId + " has 6 connection results");
          } else if (histId.startsWith("DEVTOOLS_WEBIDE_CONNECTED_RUNTIME_PLATFORM_VERSION")) {
            let platformversion = histId.split("|")[1];
            is(platformversion, Services.appinfo.platformVersion, "Found runtime platform version");
            is(value.length, 6, histId + " has 6 connection results");
          } else if (histId.startsWith("DEVTOOLS_WEBIDE_CONNECTED_RUNTIME_APP_TYPE")) {
            let apptype = histId.split("|")[1];
            is(apptype, "firefox", "Found runtime app type");
            is(value.length, 6, histId + " has 6 connection results");
          } else if (histId.startsWith("DEVTOOLS_WEBIDE_CONNECTED_RUNTIME_VERSION")) {
            let version = histId.split("|")[1];
            is(version, Services.appinfo.version, "Found runtime version");
            is(value.length, 6, histId + " has 6 connection results");
          } else {
            ok(false, "Unexpected " + histId + " was logged");
          }
        }
      }

      window.onload = function() {
        SimpleTest.testInChaosMode();
        SimpleTest.waitForExplicitFinish();

        let win;

        SimpleTest.registerCleanupFunction(() => {
          return Task.spawn(function*() {
            if (win) {
              yield closeWebIDE(win);
            }
            DebuggerServer.destroy();
            yield removeAllProjects();
            resetTelemetry();
          });
        });

        Task.spawn(function*() {
          if (!DebuggerServer.initialized) {
            DebuggerServer.init();
            DebuggerServer.addBrowserActors();
          }

          patchTelemetry();

          // Cycle once, so we can test for multiple opens
          yield cycleWebIDE();

          win = yield openWebIDE();
          let docRuntime = getRuntimeDocument(win);

          // Wait a bit, so we're open for a non-zero time
          yield waitForTime(TOOL_DELAY);
          addFakeRuntimes(win);
          yield addTestApp(win);

          // Each one should log a connection result and non-zero connection
          // time
          yield connectToRuntime(win, docRuntime, "usb");
          yield connectToRuntime(win, docRuntime, "wifi");
          yield connectToRuntime(win, docRuntime, "simulator");
          yield connectToRuntime(win, docRuntime, "other", 0 /* remote */);
          yield connectToRuntime(win, docRuntime, "other", 1 /* local */);
          yield connectToRuntime(win, docRuntime, "other", 2 /* other */);
          yield closeWebIDE(win);
          win = null;

          checkResults();

          SimpleTest.finish();
        });
      }
    </script>
  </body>
</html>