michael@0: // -*- Mode: js2; tab-width: 2; indent-tabs-mode: nil; js2-basic-offset: 2; js2-skip-preprocessor-directives: t; -*- michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this file, michael@0: * You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: const Cc = Components.classes; michael@0: const Ci = Components.interfaces; michael@0: const Cu = Components.utils; michael@0: michael@0: Cu.import("resource://gre/modules/Services.jsm"); michael@0: michael@0: const TEST_PREFIX = "TEST-"; michael@0: const TEST_REGEX = new RegExp("^" + TEST_PREFIX); michael@0: michael@0: function do_check_array_eq(a1, a2) { michael@0: do_check_eq(a1.length, a2.length); michael@0: for (let i = 0; i < a1.length; ++i) { michael@0: do_check_eq(a1[i], a2[i]); michael@0: } michael@0: } michael@0: michael@0: function getObserver() { michael@0: let bridge = Cc["@mozilla.org/android/bridge;1"] michael@0: .getService(Ci.nsIAndroidBridge); michael@0: let obsXPCOM = bridge.browserApp.getUITelemetryObserver(); michael@0: do_check_true(!!obsXPCOM); michael@0: return obsXPCOM.wrappedJSObject; michael@0: } michael@0: michael@0: /** michael@0: * The following event test will fail if telemetry isn't enabled. The Java-side michael@0: * part of this test should have turned it on; fail if it didn't work. michael@0: */ michael@0: add_test(function test_enabled() { michael@0: let obs = getObserver(); michael@0: do_check_true(!!obs); michael@0: do_check_true(obs.enabled); michael@0: run_next_test(); michael@0: }); michael@0: michael@0: add_test(function test_telemetry_events() { michael@0: let obs = getObserver(); michael@0: let measurements = obs.getUIMeasurements().filter(function(m) { michael@0: // Only want events and sessions that were generated by michael@0: // the Java-side of the test. michael@0: return TEST_REGEX.test(m.type == "event" ? m.action : m.name); michael@0: }); michael@0: michael@0: let expected = [ michael@0: ["event", TEST_PREFIX + "enone", "method0", [], null], michael@0: ["event", TEST_PREFIX + "efoo", "method1", [TEST_PREFIX + "foo"], null], michael@0: ["event", TEST_PREFIX + "efoo", "method2", [TEST_PREFIX + "foo"], null], michael@0: ["event", TEST_PREFIX + "efoobar", "method3", [TEST_PREFIX + "foo", TEST_PREFIX + "bar"], "foobarextras"], michael@0: ["session", TEST_PREFIX + "foo", "reasonfoo"], michael@0: ["event", TEST_PREFIX + "ebar", "method4", [TEST_PREFIX + "bar"], "barextras"], michael@0: ["session", TEST_PREFIX + "bar", "reasonbar"], michael@0: ["event", TEST_PREFIX + "enone", "method5", [], null], michael@0: ]; michael@0: michael@0: do_check_eq(expected.length, measurements.length); michael@0: michael@0: for (let i = 0; i < measurements.length; ++i) { michael@0: let m = measurements[i]; michael@0: michael@0: let type = m.type; michael@0: if (type == "event") { michael@0: let [type, action, method, sessions, extras] = expected[i]; michael@0: do_check_eq(m.action, action); michael@0: do_check_eq(m.method, method); michael@0: // might receive real sessions in addition to the test ones -- remove the real ones michael@0: do_check_array_eq(m.sessions.filter(s => TEST_REGEX.test(s)), sessions); michael@0: do_check_eq(m.extras, extras); michael@0: continue; michael@0: } michael@0: michael@0: if (type == "session") { michael@0: let [type, name, reason] = expected[i]; michael@0: do_check_eq(m.name, name); michael@0: do_check_eq(m.reason, reason); michael@0: continue; michael@0: } michael@0: } michael@0: michael@0: run_next_test(); michael@0: }); michael@0: michael@0: run_next_test();