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 | /* 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", this); |
michael@0 | 7 | Cu.import("resource://gre/modules/Preferences.jsm", this); |
michael@0 | 8 | Cu.import("resource://gre/modules/Promise.jsm", this); |
michael@0 | 9 | Cu.import("resource://services-sync/main.js", this); |
michael@0 | 10 | Cu.import("resource://services-sync/healthreport.jsm", this); |
michael@0 | 11 | Cu.import("resource://testing-common/services-common/logging.js", this); |
michael@0 | 12 | Cu.import("resource://testing-common/services/healthreport/utils.jsm", this); |
michael@0 | 13 | |
michael@0 | 14 | function run_test() { |
michael@0 | 15 | initTestLogging(); |
michael@0 | 16 | |
michael@0 | 17 | run_next_test(); |
michael@0 | 18 | } |
michael@0 | 19 | |
michael@0 | 20 | add_task(function test_constructor() { |
michael@0 | 21 | let provider = new SyncProvider(); |
michael@0 | 22 | }); |
michael@0 | 23 | |
michael@0 | 24 | // Provider can initialize and de-initialize properly. |
michael@0 | 25 | add_task(function* test_init() { |
michael@0 | 26 | let storage = yield Metrics.Storage("init"); |
michael@0 | 27 | let provider = new SyncProvider(); |
michael@0 | 28 | yield provider.init(storage); |
michael@0 | 29 | yield provider.shutdown(); |
michael@0 | 30 | yield storage.close(); |
michael@0 | 31 | }); |
michael@0 | 32 | |
michael@0 | 33 | add_task(function* test_collect() { |
michael@0 | 34 | let storage = yield Metrics.Storage("collect"); |
michael@0 | 35 | let provider = new SyncProvider(); |
michael@0 | 36 | yield provider.init(storage); |
michael@0 | 37 | |
michael@0 | 38 | // Initially nothing should be configured. |
michael@0 | 39 | let now = new Date(); |
michael@0 | 40 | yield provider.collectDailyData(); |
michael@0 | 41 | |
michael@0 | 42 | let m = provider.getMeasurement("sync", 1); |
michael@0 | 43 | let values = yield m.getValues(); |
michael@0 | 44 | Assert.equal(values.days.size, 1); |
michael@0 | 45 | Assert.ok(values.days.hasDay(now)); |
michael@0 | 46 | let day = values.days.getDay(now); |
michael@0 | 47 | Assert.ok(day.has("enabled")); |
michael@0 | 48 | Assert.ok(day.has("activeProtocol")); |
michael@0 | 49 | Assert.ok(day.has("preferredProtocol")); |
michael@0 | 50 | Assert.equal(day.get("enabled"), 0); |
michael@0 | 51 | Assert.equal(day.get("preferredProtocol"), "1.5"); |
michael@0 | 52 | Assert.equal(day.get("activeProtocol"), "1.5", |
michael@0 | 53 | "Protocol without setup should be FX Accounts version."); |
michael@0 | 54 | |
michael@0 | 55 | // Now check for old Sync setup. |
michael@0 | 56 | let branch = new Preferences("services.sync."); |
michael@0 | 57 | branch.set("username", "foo"); |
michael@0 | 58 | branch.reset("fxaccounts.enabled"); |
michael@0 | 59 | yield provider.collectDailyData(); |
michael@0 | 60 | values = yield m.getValues(); |
michael@0 | 61 | Assert.equal(values.days.getDay(now).get("activeProtocol"), "1.1", |
michael@0 | 62 | "Protocol with old Sync setup is correct."); |
michael@0 | 63 | |
michael@0 | 64 | Assert.equal(Weave.Status.__authManager, undefined, "Detect code changes"); |
michael@0 | 65 | |
michael@0 | 66 | // Let's enable Sync so we can get more useful data. |
michael@0 | 67 | // We need to do this because the FHR probe only records more info if Sync |
michael@0 | 68 | // is configured properly. |
michael@0 | 69 | Weave.Service.identity.account = "johndoe"; |
michael@0 | 70 | Weave.Service.identity.basicPassword = "ilovejane"; |
michael@0 | 71 | Weave.Service.identity.syncKey = Weave.Utils.generatePassphrase(); |
michael@0 | 72 | Weave.Service.clusterURL = "http://localhost/"; |
michael@0 | 73 | Assert.equal(Weave.Status.checkSetup(), Weave.STATUS_OK); |
michael@0 | 74 | |
michael@0 | 75 | yield provider.collectDailyData(); |
michael@0 | 76 | values = yield m.getValues(); |
michael@0 | 77 | day = values.days.getDay(now); |
michael@0 | 78 | Assert.equal(day.get("enabled"), 1); |
michael@0 | 79 | |
michael@0 | 80 | // An empty account should have 1 device: us. |
michael@0 | 81 | let dm = provider.getMeasurement("devices", 1); |
michael@0 | 82 | values = yield dm.getValues(); |
michael@0 | 83 | Assert.ok(values.days.hasDay(now)); |
michael@0 | 84 | day = values.days.getDay(now); |
michael@0 | 85 | Assert.equal(day.size, 1); |
michael@0 | 86 | let engine = Weave.Service.clientsEngine; |
michael@0 | 87 | Assert.ok(engine); |
michael@0 | 88 | Assert.ok(day.has(engine.localType)); |
michael@0 | 89 | Assert.equal(day.get(engine.localType), 1); |
michael@0 | 90 | |
michael@0 | 91 | // Add some devices and ensure they show up. |
michael@0 | 92 | engine._store._remoteClients["id1"] = {type: "mobile"}; |
michael@0 | 93 | engine._store._remoteClients["id2"] = {type: "tablet"}; |
michael@0 | 94 | engine._store._remoteClients["id3"] = {type: "mobile"}; |
michael@0 | 95 | |
michael@0 | 96 | yield provider.collectDailyData(); |
michael@0 | 97 | values = yield dm.getValues(); |
michael@0 | 98 | day = values.days.getDay(now); |
michael@0 | 99 | |
michael@0 | 100 | let expected = { |
michael@0 | 101 | "foobar": 0, |
michael@0 | 102 | "tablet": 1, |
michael@0 | 103 | "mobile": 2, |
michael@0 | 104 | "desktop": 0, |
michael@0 | 105 | }; |
michael@0 | 106 | |
michael@0 | 107 | for (let type in expected) { |
michael@0 | 108 | let count = expected[type]; |
michael@0 | 109 | |
michael@0 | 110 | if (engine.localType == type) { |
michael@0 | 111 | count++; |
michael@0 | 112 | } |
michael@0 | 113 | |
michael@0 | 114 | if (!count) { |
michael@0 | 115 | Assert.ok(!day.has(type)); |
michael@0 | 116 | } else { |
michael@0 | 117 | Assert.ok(day.has(type)); |
michael@0 | 118 | Assert.equal(day.get(type), count); |
michael@0 | 119 | } |
michael@0 | 120 | } |
michael@0 | 121 | |
michael@0 | 122 | engine._store._remoteClients = {}; |
michael@0 | 123 | |
michael@0 | 124 | yield provider.shutdown(); |
michael@0 | 125 | yield storage.close(); |
michael@0 | 126 | }); |
michael@0 | 127 | |
michael@0 | 128 | add_task(function* test_sync_events() { |
michael@0 | 129 | let storage = yield Metrics.Storage("sync_events"); |
michael@0 | 130 | let provider = new SyncProvider(); |
michael@0 | 131 | yield provider.init(storage); |
michael@0 | 132 | |
michael@0 | 133 | let m = provider.getMeasurement("sync", 1); |
michael@0 | 134 | |
michael@0 | 135 | for (let i = 0; i < 5; i++) { |
michael@0 | 136 | Services.obs.notifyObservers(null, "weave:service:sync:start", null); |
michael@0 | 137 | } |
michael@0 | 138 | |
michael@0 | 139 | for (let i = 0; i < 3; i++) { |
michael@0 | 140 | Services.obs.notifyObservers(null, "weave:service:sync:finish", null); |
michael@0 | 141 | } |
michael@0 | 142 | |
michael@0 | 143 | for (let i = 0; i < 2; i++) { |
michael@0 | 144 | Services.obs.notifyObservers(null, "weave:service:sync:error", null); |
michael@0 | 145 | } |
michael@0 | 146 | |
michael@0 | 147 | // Wait for storage to complete. |
michael@0 | 148 | yield m.storage.enqueueOperation(() => { |
michael@0 | 149 | return Promise.resolve(); |
michael@0 | 150 | }); |
michael@0 | 151 | |
michael@0 | 152 | let values = yield m.getValues(); |
michael@0 | 153 | let now = new Date(); |
michael@0 | 154 | Assert.ok(values.days.hasDay(now)); |
michael@0 | 155 | let day = values.days.getDay(now); |
michael@0 | 156 | |
michael@0 | 157 | Assert.ok(day.has("syncStart")); |
michael@0 | 158 | Assert.ok(day.has("syncSuccess")); |
michael@0 | 159 | Assert.ok(day.has("syncError")); |
michael@0 | 160 | Assert.equal(day.get("syncStart"), 5); |
michael@0 | 161 | Assert.equal(day.get("syncSuccess"), 3); |
michael@0 | 162 | Assert.equal(day.get("syncError"), 2); |
michael@0 | 163 | |
michael@0 | 164 | yield provider.shutdown(); |
michael@0 | 165 | yield storage.close(); |
michael@0 | 166 | }); |
michael@0 | 167 | |
michael@0 | 168 | add_task(function* test_healthreporter_json() { |
michael@0 | 169 | let reporter = yield getHealthReporter("healthreporter_json"); |
michael@0 | 170 | yield reporter.init(); |
michael@0 | 171 | try { |
michael@0 | 172 | yield reporter._providerManager.registerProvider(new SyncProvider()); |
michael@0 | 173 | yield reporter.collectMeasurements(); |
michael@0 | 174 | let payload = yield reporter.getJSONPayload(true); |
michael@0 | 175 | let now = new Date(); |
michael@0 | 176 | let today = reporter._formatDate(now); |
michael@0 | 177 | |
michael@0 | 178 | Assert.ok(today in payload.data.days); |
michael@0 | 179 | let day = payload.data.days[today]; |
michael@0 | 180 | |
michael@0 | 181 | Assert.ok("org.mozilla.sync.sync" in day); |
michael@0 | 182 | Assert.ok("org.mozilla.sync.devices" in day); |
michael@0 | 183 | |
michael@0 | 184 | let devices = day["org.mozilla.sync.devices"]; |
michael@0 | 185 | let engine = Weave.Service.clientsEngine; |
michael@0 | 186 | Assert.ok(engine); |
michael@0 | 187 | let type = engine.localType; |
michael@0 | 188 | Assert.ok(type); |
michael@0 | 189 | Assert.ok(type in devices); |
michael@0 | 190 | Assert.equal(devices[type], 1); |
michael@0 | 191 | } finally { |
michael@0 | 192 | reporter._shutdown(); |
michael@0 | 193 | } |
michael@0 | 194 | }); |