michael@0: /* Any copyright is dedicated to the Public Domain. michael@0: * http://creativecommons.org/publicdomain/zero/1.0/ */ michael@0: michael@0: "use strict"; michael@0: michael@0: Cu.import("resource://gre/modules/Metrics.jsm", this); michael@0: Cu.import("resource://gre/modules/Preferences.jsm", this); michael@0: Cu.import("resource://gre/modules/Promise.jsm", this); michael@0: Cu.import("resource://services-sync/main.js", this); michael@0: Cu.import("resource://services-sync/healthreport.jsm", this); michael@0: Cu.import("resource://testing-common/services-common/logging.js", this); michael@0: Cu.import("resource://testing-common/services/healthreport/utils.jsm", this); michael@0: michael@0: function run_test() { michael@0: initTestLogging(); michael@0: michael@0: run_next_test(); michael@0: } michael@0: michael@0: add_task(function test_constructor() { michael@0: let provider = new SyncProvider(); michael@0: }); michael@0: michael@0: // Provider can initialize and de-initialize properly. michael@0: add_task(function* test_init() { michael@0: let storage = yield Metrics.Storage("init"); michael@0: let provider = new SyncProvider(); michael@0: yield provider.init(storage); michael@0: yield provider.shutdown(); michael@0: yield storage.close(); michael@0: }); michael@0: michael@0: add_task(function* test_collect() { michael@0: let storage = yield Metrics.Storage("collect"); michael@0: let provider = new SyncProvider(); michael@0: yield provider.init(storage); michael@0: michael@0: // Initially nothing should be configured. michael@0: let now = new Date(); michael@0: yield provider.collectDailyData(); michael@0: michael@0: let m = provider.getMeasurement("sync", 1); michael@0: let values = yield m.getValues(); michael@0: Assert.equal(values.days.size, 1); michael@0: Assert.ok(values.days.hasDay(now)); michael@0: let day = values.days.getDay(now); michael@0: Assert.ok(day.has("enabled")); michael@0: Assert.ok(day.has("activeProtocol")); michael@0: Assert.ok(day.has("preferredProtocol")); michael@0: Assert.equal(day.get("enabled"), 0); michael@0: Assert.equal(day.get("preferredProtocol"), "1.5"); michael@0: Assert.equal(day.get("activeProtocol"), "1.5", michael@0: "Protocol without setup should be FX Accounts version."); michael@0: michael@0: // Now check for old Sync setup. michael@0: let branch = new Preferences("services.sync."); michael@0: branch.set("username", "foo"); michael@0: branch.reset("fxaccounts.enabled"); michael@0: yield provider.collectDailyData(); michael@0: values = yield m.getValues(); michael@0: Assert.equal(values.days.getDay(now).get("activeProtocol"), "1.1", michael@0: "Protocol with old Sync setup is correct."); michael@0: michael@0: Assert.equal(Weave.Status.__authManager, undefined, "Detect code changes"); michael@0: michael@0: // Let's enable Sync so we can get more useful data. michael@0: // We need to do this because the FHR probe only records more info if Sync michael@0: // is configured properly. michael@0: Weave.Service.identity.account = "johndoe"; michael@0: Weave.Service.identity.basicPassword = "ilovejane"; michael@0: Weave.Service.identity.syncKey = Weave.Utils.generatePassphrase(); michael@0: Weave.Service.clusterURL = "http://localhost/"; michael@0: Assert.equal(Weave.Status.checkSetup(), Weave.STATUS_OK); michael@0: michael@0: yield provider.collectDailyData(); michael@0: values = yield m.getValues(); michael@0: day = values.days.getDay(now); michael@0: Assert.equal(day.get("enabled"), 1); michael@0: michael@0: // An empty account should have 1 device: us. michael@0: let dm = provider.getMeasurement("devices", 1); michael@0: values = yield dm.getValues(); michael@0: Assert.ok(values.days.hasDay(now)); michael@0: day = values.days.getDay(now); michael@0: Assert.equal(day.size, 1); michael@0: let engine = Weave.Service.clientsEngine; michael@0: Assert.ok(engine); michael@0: Assert.ok(day.has(engine.localType)); michael@0: Assert.equal(day.get(engine.localType), 1); michael@0: michael@0: // Add some devices and ensure they show up. michael@0: engine._store._remoteClients["id1"] = {type: "mobile"}; michael@0: engine._store._remoteClients["id2"] = {type: "tablet"}; michael@0: engine._store._remoteClients["id3"] = {type: "mobile"}; michael@0: michael@0: yield provider.collectDailyData(); michael@0: values = yield dm.getValues(); michael@0: day = values.days.getDay(now); michael@0: michael@0: let expected = { michael@0: "foobar": 0, michael@0: "tablet": 1, michael@0: "mobile": 2, michael@0: "desktop": 0, michael@0: }; michael@0: michael@0: for (let type in expected) { michael@0: let count = expected[type]; michael@0: michael@0: if (engine.localType == type) { michael@0: count++; michael@0: } michael@0: michael@0: if (!count) { michael@0: Assert.ok(!day.has(type)); michael@0: } else { michael@0: Assert.ok(day.has(type)); michael@0: Assert.equal(day.get(type), count); michael@0: } michael@0: } michael@0: michael@0: engine._store._remoteClients = {}; michael@0: michael@0: yield provider.shutdown(); michael@0: yield storage.close(); michael@0: }); michael@0: michael@0: add_task(function* test_sync_events() { michael@0: let storage = yield Metrics.Storage("sync_events"); michael@0: let provider = new SyncProvider(); michael@0: yield provider.init(storage); michael@0: michael@0: let m = provider.getMeasurement("sync", 1); michael@0: michael@0: for (let i = 0; i < 5; i++) { michael@0: Services.obs.notifyObservers(null, "weave:service:sync:start", null); michael@0: } michael@0: michael@0: for (let i = 0; i < 3; i++) { michael@0: Services.obs.notifyObservers(null, "weave:service:sync:finish", null); michael@0: } michael@0: michael@0: for (let i = 0; i < 2; i++) { michael@0: Services.obs.notifyObservers(null, "weave:service:sync:error", null); michael@0: } michael@0: michael@0: // Wait for storage to complete. michael@0: yield m.storage.enqueueOperation(() => { michael@0: return Promise.resolve(); michael@0: }); michael@0: michael@0: let values = yield m.getValues(); michael@0: let now = new Date(); michael@0: Assert.ok(values.days.hasDay(now)); michael@0: let day = values.days.getDay(now); michael@0: michael@0: Assert.ok(day.has("syncStart")); michael@0: Assert.ok(day.has("syncSuccess")); michael@0: Assert.ok(day.has("syncError")); michael@0: Assert.equal(day.get("syncStart"), 5); michael@0: Assert.equal(day.get("syncSuccess"), 3); michael@0: Assert.equal(day.get("syncError"), 2); michael@0: michael@0: yield provider.shutdown(); michael@0: yield storage.close(); michael@0: }); michael@0: michael@0: add_task(function* test_healthreporter_json() { michael@0: let reporter = yield getHealthReporter("healthreporter_json"); michael@0: yield reporter.init(); michael@0: try { michael@0: yield reporter._providerManager.registerProvider(new SyncProvider()); michael@0: yield reporter.collectMeasurements(); michael@0: let payload = yield reporter.getJSONPayload(true); michael@0: let now = new Date(); michael@0: let today = reporter._formatDate(now); michael@0: michael@0: Assert.ok(today in payload.data.days); michael@0: let day = payload.data.days[today]; michael@0: michael@0: Assert.ok("org.mozilla.sync.sync" in day); michael@0: Assert.ok("org.mozilla.sync.devices" in day); michael@0: michael@0: let devices = day["org.mozilla.sync.devices"]; michael@0: let engine = Weave.Service.clientsEngine; michael@0: Assert.ok(engine); michael@0: let type = engine.localType; michael@0: Assert.ok(type); michael@0: Assert.ok(type in devices); michael@0: Assert.equal(devices[type], 1); michael@0: } finally { michael@0: reporter._shutdown(); michael@0: } michael@0: });