Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | // -*- Mode: js2; tab-width: 2; indent-tabs-mode: nil; js2-basic-offset: 2; js2-skip-preprocessor-directives: t; -*- |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this file, |
michael@0 | 4 | * You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | const Cc = Components.classes; |
michael@0 | 7 | const Ci = Components.interfaces; |
michael@0 | 8 | const Cu = Components.utils; |
michael@0 | 9 | |
michael@0 | 10 | Cu.import("resource://gre/modules/Services.jsm"); |
michael@0 | 11 | |
michael@0 | 12 | const TEST_PREFIX = "TEST-"; |
michael@0 | 13 | const TEST_REGEX = new RegExp("^" + TEST_PREFIX); |
michael@0 | 14 | |
michael@0 | 15 | function do_check_array_eq(a1, a2) { |
michael@0 | 16 | do_check_eq(a1.length, a2.length); |
michael@0 | 17 | for (let i = 0; i < a1.length; ++i) { |
michael@0 | 18 | do_check_eq(a1[i], a2[i]); |
michael@0 | 19 | } |
michael@0 | 20 | } |
michael@0 | 21 | |
michael@0 | 22 | function getObserver() { |
michael@0 | 23 | let bridge = Cc["@mozilla.org/android/bridge;1"] |
michael@0 | 24 | .getService(Ci.nsIAndroidBridge); |
michael@0 | 25 | let obsXPCOM = bridge.browserApp.getUITelemetryObserver(); |
michael@0 | 26 | do_check_true(!!obsXPCOM); |
michael@0 | 27 | return obsXPCOM.wrappedJSObject; |
michael@0 | 28 | } |
michael@0 | 29 | |
michael@0 | 30 | /** |
michael@0 | 31 | * The following event test will fail if telemetry isn't enabled. The Java-side |
michael@0 | 32 | * part of this test should have turned it on; fail if it didn't work. |
michael@0 | 33 | */ |
michael@0 | 34 | add_test(function test_enabled() { |
michael@0 | 35 | let obs = getObserver(); |
michael@0 | 36 | do_check_true(!!obs); |
michael@0 | 37 | do_check_true(obs.enabled); |
michael@0 | 38 | run_next_test(); |
michael@0 | 39 | }); |
michael@0 | 40 | |
michael@0 | 41 | add_test(function test_telemetry_events() { |
michael@0 | 42 | let obs = getObserver(); |
michael@0 | 43 | let measurements = obs.getUIMeasurements().filter(function(m) { |
michael@0 | 44 | // Only want events and sessions that were generated by |
michael@0 | 45 | // the Java-side of the test. |
michael@0 | 46 | return TEST_REGEX.test(m.type == "event" ? m.action : m.name); |
michael@0 | 47 | }); |
michael@0 | 48 | |
michael@0 | 49 | let expected = [ |
michael@0 | 50 | ["event", TEST_PREFIX + "enone", "method0", [], null], |
michael@0 | 51 | ["event", TEST_PREFIX + "efoo", "method1", [TEST_PREFIX + "foo"], null], |
michael@0 | 52 | ["event", TEST_PREFIX + "efoo", "method2", [TEST_PREFIX + "foo"], null], |
michael@0 | 53 | ["event", TEST_PREFIX + "efoobar", "method3", [TEST_PREFIX + "foo", TEST_PREFIX + "bar"], "foobarextras"], |
michael@0 | 54 | ["session", TEST_PREFIX + "foo", "reasonfoo"], |
michael@0 | 55 | ["event", TEST_PREFIX + "ebar", "method4", [TEST_PREFIX + "bar"], "barextras"], |
michael@0 | 56 | ["session", TEST_PREFIX + "bar", "reasonbar"], |
michael@0 | 57 | ["event", TEST_PREFIX + "enone", "method5", [], null], |
michael@0 | 58 | ]; |
michael@0 | 59 | |
michael@0 | 60 | do_check_eq(expected.length, measurements.length); |
michael@0 | 61 | |
michael@0 | 62 | for (let i = 0; i < measurements.length; ++i) { |
michael@0 | 63 | let m = measurements[i]; |
michael@0 | 64 | |
michael@0 | 65 | let type = m.type; |
michael@0 | 66 | if (type == "event") { |
michael@0 | 67 | let [type, action, method, sessions, extras] = expected[i]; |
michael@0 | 68 | do_check_eq(m.action, action); |
michael@0 | 69 | do_check_eq(m.method, method); |
michael@0 | 70 | // might receive real sessions in addition to the test ones -- remove the real ones |
michael@0 | 71 | do_check_array_eq(m.sessions.filter(s => TEST_REGEX.test(s)), sessions); |
michael@0 | 72 | do_check_eq(m.extras, extras); |
michael@0 | 73 | continue; |
michael@0 | 74 | } |
michael@0 | 75 | |
michael@0 | 76 | if (type == "session") { |
michael@0 | 77 | let [type, name, reason] = expected[i]; |
michael@0 | 78 | do_check_eq(m.name, name); |
michael@0 | 79 | do_check_eq(m.reason, reason); |
michael@0 | 80 | continue; |
michael@0 | 81 | } |
michael@0 | 82 | } |
michael@0 | 83 | |
michael@0 | 84 | run_next_test(); |
michael@0 | 85 | }); |
michael@0 | 86 | |
michael@0 | 87 | run_next_test(); |