browser/experiments/test/xpcshell/test_healthreport.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* Any copyright is dedicated to the Public Domain.
michael@0 2 * http://creativecommons.org/publicdomain/zero/1.0/ */
michael@0 3
michael@0 4 "use strict";
michael@0 5
michael@0 6 Cu.import("resource://gre/modules/Metrics.jsm");
michael@0 7 Cu.import("resource://gre/modules/Task.jsm");
michael@0 8 Cu.import("resource:///modules/experiments/Experiments.jsm");
michael@0 9 Cu.import("resource://testing-common/services/healthreport/utils.jsm");
michael@0 10 Cu.import("resource://testing-common/services-common/logging.js");
michael@0 11
michael@0 12 const kMeasurementVersion = 2;
michael@0 13
michael@0 14 function getStorageAndProvider(name) {
michael@0 15 return Task.spawn(function* get() {
michael@0 16 let storage = yield Metrics.Storage(name);
michael@0 17 let provider = new ExperimentsProvider();
michael@0 18 yield provider.init(storage);
michael@0 19
michael@0 20 return [storage, provider];
michael@0 21 });
michael@0 22 }
michael@0 23
michael@0 24 function run_test() {
michael@0 25 run_next_test();
michael@0 26 }
michael@0 27
michael@0 28 add_test(function setup() {
michael@0 29 do_get_profile();
michael@0 30 initTestLogging();
michael@0 31
michael@0 32 Services.prefs.setBoolPref(PREF_EXPERIMENTS_ENABLED, true);
michael@0 33 Services.prefs.setBoolPref(PREF_TELEMETRY_ENABLED, true);
michael@0 34 Services.prefs.setBoolPref(PREF_HEALTHREPORT_ENABLED, true);
michael@0 35
michael@0 36 run_next_test();
michael@0 37 });
michael@0 38
michael@0 39 add_task(function test_constructor() {
michael@0 40 Experiments.instance();
michael@0 41 yield Experiments._mainTask;
michael@0 42 let provider = new ExperimentsProvider();
michael@0 43 });
michael@0 44
michael@0 45 add_task(function* test_init() {
michael@0 46 let storage = yield Metrics.Storage("init");
michael@0 47 let provider = new ExperimentsProvider();
michael@0 48 yield provider.init(storage);
michael@0 49 yield provider.shutdown();
michael@0 50 yield storage.close();
michael@0 51 });
michael@0 52
michael@0 53 add_task(function* test_collect() {
michael@0 54 let [storage, provider] = yield getStorageAndProvider("no_active");
michael@0 55
michael@0 56 // Initial state should not report anything.
michael@0 57 yield provider.collectDailyData();
michael@0 58 let m = provider.getMeasurement("info", kMeasurementVersion);
michael@0 59 let values = yield m.getValues();
michael@0 60 Assert.equal(values.days.size, 0, "Have no data if no experiments known.");
michael@0 61
michael@0 62 // An old experiment that ended today should be reported.
michael@0 63 replaceExperiments(provider._experiments, FAKE_EXPERIMENTS_2);
michael@0 64 let now = new Date(FAKE_EXPERIMENTS_2[0].endDate);
michael@0 65 defineNow(provider._experiments._policy, now.getTime());
michael@0 66
michael@0 67 yield provider.collectDailyData();
michael@0 68 values = yield m.getValues();
michael@0 69 Assert.equal(values.days.size, 1, "Have 1 day of data");
michael@0 70 Assert.ok(values.days.hasDay(now), "Has day the experiment ended.");
michael@0 71 let day = values.days.getDay(now);
michael@0 72 Assert.ok(day.has("lastActive"), "Has lastActive field.");
michael@0 73 Assert.equal(day.get("lastActive"), "id2", "Last active ID is sane.");
michael@0 74 Assert.strictEqual(day.get("lastActiveBranch"), undefined,
michael@0 75 "no branch should be set yet");
michael@0 76
michael@0 77 // Making an experiment active replaces the lastActive value.
michael@0 78 replaceExperiments(provider._experiments, FAKE_EXPERIMENTS_1);
michael@0 79 yield provider.collectDailyData();
michael@0 80 values = yield m.getValues();
michael@0 81 day = values.days.getDay(now);
michael@0 82 Assert.equal(day.get("lastActive"), "id1", "Last active ID is the active experiment.");
michael@0 83 Assert.equal(day.get("lastActiveBranch"), "foo",
michael@0 84 "Experiment branch should be visible");
michael@0 85
michael@0 86 // And make sure the observer works.
michael@0 87 replaceExperiments(provider._experiments, FAKE_EXPERIMENTS_2);
michael@0 88 Services.obs.notifyObservers(null, "experiments-changed", null);
michael@0 89 // This may not wait long enough. It relies on the SQL insert happening
michael@0 90 // in the same tick as the observer notification.
michael@0 91 yield storage.enqueueOperation(function () {
michael@0 92 return Promise.resolve();
michael@0 93 });
michael@0 94
michael@0 95 values = yield m.getValues();
michael@0 96 day = values.days.getDay(now);
michael@0 97 Assert.equal(day.get("lastActive"), "id2", "Last active ID set by observer.");
michael@0 98
michael@0 99 yield provider.shutdown();
michael@0 100 yield storage.close();
michael@0 101 });
michael@0 102
michael@0 103 add_task(function* test_healthreporterJSON() {
michael@0 104 let reporter = yield getHealthReporter("healthreporterJSON");
michael@0 105 yield reporter.init();
michael@0 106 try {
michael@0 107 yield reporter._providerManager.registerProvider(new ExperimentsProvider());
michael@0 108 let experiments = Experiments.instance();
michael@0 109 defineNow(experiments._policy, Date.now());
michael@0 110 replaceExperiments(experiments, FAKE_EXPERIMENTS_1);
michael@0 111 yield reporter.collectMeasurements();
michael@0 112
michael@0 113 let payload = yield reporter.getJSONPayload(true);
michael@0 114 let today = reporter._formatDate(reporter._policy.now());
michael@0 115
michael@0 116 Assert.ok(today in payload.data.days, "Day in payload.");
michael@0 117 let day = payload.data.days[today];
michael@0 118
michael@0 119 Assert.ok("org.mozilla.experiments.info" in day, "Measurement present.");
michael@0 120 let m = day["org.mozilla.experiments.info"];
michael@0 121 Assert.ok("lastActive" in m, "lastActive field present.");
michael@0 122 Assert.equal(m["lastActive"], "id1", "Last active ID proper.");
michael@0 123 } finally {
michael@0 124 reporter._shutdown();
michael@0 125 }
michael@0 126 });

mercurial