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 | const {utils: Cu} = Components; |
michael@0 | 7 | |
michael@0 | 8 | Cu.import("resource://gre/modules/Metrics.jsm"); |
michael@0 | 9 | Cu.import("resource://gre/modules/Services.jsm"); |
michael@0 | 10 | let bsp = Cu.import("resource://gre/modules/services/healthreport/providers.jsm"); |
michael@0 | 11 | |
michael@0 | 12 | const DEFAULT_ENGINES = [ |
michael@0 | 13 | {name: "Amazon.com", identifier: "amazondotcom"}, |
michael@0 | 14 | {name: "Bing", identifier: "bing"}, |
michael@0 | 15 | {name: "Google", identifier: "google"}, |
michael@0 | 16 | {name: "Yahoo", identifier: "yahoo"}, |
michael@0 | 17 | {name: "Foobar Search", identifier: "foobar"}, |
michael@0 | 18 | ]; |
michael@0 | 19 | |
michael@0 | 20 | function MockSearchCountMeasurement() { |
michael@0 | 21 | bsp.SearchCountMeasurement3.call(this); |
michael@0 | 22 | } |
michael@0 | 23 | MockSearchCountMeasurement.prototype = { |
michael@0 | 24 | __proto__: bsp.SearchCountMeasurement3.prototype, |
michael@0 | 25 | }; |
michael@0 | 26 | |
michael@0 | 27 | function MockSearchesProvider() { |
michael@0 | 28 | SearchesProvider.call(this); |
michael@0 | 29 | } |
michael@0 | 30 | MockSearchesProvider.prototype = { |
michael@0 | 31 | __proto__: SearchesProvider.prototype, |
michael@0 | 32 | measurementTypes: [MockSearchCountMeasurement], |
michael@0 | 33 | }; |
michael@0 | 34 | |
michael@0 | 35 | function run_test() { |
michael@0 | 36 | run_next_test(); |
michael@0 | 37 | } |
michael@0 | 38 | |
michael@0 | 39 | add_test(function test_constructor() { |
michael@0 | 40 | let provider = new SearchesProvider(); |
michael@0 | 41 | |
michael@0 | 42 | run_next_test(); |
michael@0 | 43 | }); |
michael@0 | 44 | |
michael@0 | 45 | add_task(function* test_record() { |
michael@0 | 46 | let storage = yield Metrics.Storage("record"); |
michael@0 | 47 | let provider = new MockSearchesProvider(); |
michael@0 | 48 | |
michael@0 | 49 | yield provider.init(storage); |
michael@0 | 50 | |
michael@0 | 51 | let now = new Date(); |
michael@0 | 52 | |
michael@0 | 53 | // Record searches for all but one of our defaults, and one engine that's |
michael@0 | 54 | // not a default. |
michael@0 | 55 | for (let engine of DEFAULT_ENGINES.concat([{name: "Not Default", identifier: "notdef"}])) { |
michael@0 | 56 | if (engine.identifier == "yahoo") { |
michael@0 | 57 | continue; |
michael@0 | 58 | } |
michael@0 | 59 | yield provider.recordSearch(engine, "abouthome"); |
michael@0 | 60 | yield provider.recordSearch(engine, "contextmenu"); |
michael@0 | 61 | yield provider.recordSearch(engine, "newtab"); |
michael@0 | 62 | yield provider.recordSearch(engine, "searchbar"); |
michael@0 | 63 | yield provider.recordSearch(engine, "urlbar"); |
michael@0 | 64 | } |
michael@0 | 65 | |
michael@0 | 66 | // Invalid sources should throw. |
michael@0 | 67 | let errored = false; |
michael@0 | 68 | try { |
michael@0 | 69 | yield provider.recordSearch(DEFAULT_ENGINES[0], "bad source"); |
michael@0 | 70 | } catch (ex) { |
michael@0 | 71 | errored = true; |
michael@0 | 72 | } finally { |
michael@0 | 73 | do_check_true(errored); |
michael@0 | 74 | } |
michael@0 | 75 | |
michael@0 | 76 | let m = provider.getMeasurement("counts", 3); |
michael@0 | 77 | let data = yield m.getValues(); |
michael@0 | 78 | do_check_eq(data.days.size, 1); |
michael@0 | 79 | do_check_true(data.days.hasDay(now)); |
michael@0 | 80 | |
michael@0 | 81 | let day = data.days.getDay(now); |
michael@0 | 82 | for (let engine of DEFAULT_ENGINES) { |
michael@0 | 83 | let identifier = engine.identifier; |
michael@0 | 84 | let expected = identifier != "yahoo"; |
michael@0 | 85 | |
michael@0 | 86 | for (let source of ["abouthome", "contextmenu", "searchbar", "urlbar"]) { |
michael@0 | 87 | let field = identifier + "." + source; |
michael@0 | 88 | if (expected) { |
michael@0 | 89 | do_check_true(day.has(field)); |
michael@0 | 90 | do_check_eq(day.get(field), 1); |
michael@0 | 91 | } else { |
michael@0 | 92 | do_check_false(day.has(field)); |
michael@0 | 93 | } |
michael@0 | 94 | } |
michael@0 | 95 | } |
michael@0 | 96 | |
michael@0 | 97 | // Also, check that our non-default engine contributed, with a computed |
michael@0 | 98 | // identifier. |
michael@0 | 99 | let identifier = "notdef"; |
michael@0 | 100 | for (let source of ["abouthome", "contextmenu", "searchbar", "urlbar"]) { |
michael@0 | 101 | let field = identifier + "." + source; |
michael@0 | 102 | do_check_true(day.has(field)); |
michael@0 | 103 | } |
michael@0 | 104 | |
michael@0 | 105 | yield storage.close(); |
michael@0 | 106 | }); |
michael@0 | 107 | |
michael@0 | 108 | add_task(function* test_includes_other_fields() { |
michael@0 | 109 | let storage = yield Metrics.Storage("includes_other_fields"); |
michael@0 | 110 | let provider = new MockSearchesProvider(); |
michael@0 | 111 | |
michael@0 | 112 | yield provider.init(storage); |
michael@0 | 113 | let m = provider.getMeasurement("counts", 3); |
michael@0 | 114 | |
michael@0 | 115 | // Register a search against a provider that isn't live in this session. |
michael@0 | 116 | let id = yield m.storage.registerField(m.id, "test.searchbar", |
michael@0 | 117 | Metrics.Storage.FIELD_DAILY_COUNTER); |
michael@0 | 118 | |
michael@0 | 119 | let testField = "test.searchbar"; |
michael@0 | 120 | let now = new Date(); |
michael@0 | 121 | yield m.storage.incrementDailyCounterFromFieldID(id, now); |
michael@0 | 122 | |
michael@0 | 123 | // Make sure we don't know about it. |
michael@0 | 124 | do_check_false(testField in m.fields); |
michael@0 | 125 | |
michael@0 | 126 | // But we want to include it in payloads. |
michael@0 | 127 | do_check_true(m.shouldIncludeField(testField)); |
michael@0 | 128 | |
michael@0 | 129 | // And we do so. |
michael@0 | 130 | let data = yield provider.storage.getMeasurementValues(m.id); |
michael@0 | 131 | let serializer = m.serializer(m.SERIALIZE_JSON); |
michael@0 | 132 | let formatted = serializer.daily(data.days.getDay(now)); |
michael@0 | 133 | do_check_true(testField in formatted); |
michael@0 | 134 | do_check_eq(formatted[testField], 1); |
michael@0 | 135 | |
michael@0 | 136 | yield storage.close(); |
michael@0 | 137 | }); |
michael@0 | 138 | |
michael@0 | 139 | add_task(function* test_default_search_engine() { |
michael@0 | 140 | let storage = yield Metrics.Storage("default_search_engine"); |
michael@0 | 141 | let provider = new SearchesProvider(); |
michael@0 | 142 | yield provider.init(storage); |
michael@0 | 143 | |
michael@0 | 144 | let m = provider.getMeasurement("engines", 1); |
michael@0 | 145 | |
michael@0 | 146 | // Ensure no collection if Telemetry not enabled. |
michael@0 | 147 | Services.prefs.setBoolPref("toolkit.telemetry.enabled", false); |
michael@0 | 148 | |
michael@0 | 149 | let now = new Date(); |
michael@0 | 150 | yield provider.collectDailyData(); |
michael@0 | 151 | |
michael@0 | 152 | let data = yield m.getValues(); |
michael@0 | 153 | Assert.equal(data.days.hasDay(now), false); |
michael@0 | 154 | |
michael@0 | 155 | // Now enable telemetry and ensure we populate. |
michael@0 | 156 | Services.prefs.setBoolPref("toolkit.telemetry.enabled", true); |
michael@0 | 157 | |
michael@0 | 158 | yield provider.collectDailyData(); |
michael@0 | 159 | data = yield m.getValues(); |
michael@0 | 160 | Assert.ok(data.days.hasDay(now)); |
michael@0 | 161 | |
michael@0 | 162 | let day = data.days.getDay(now); |
michael@0 | 163 | Assert.equal(day.size, 1); |
michael@0 | 164 | Assert.ok(day.has("default")); |
michael@0 | 165 | |
michael@0 | 166 | // test environment doesn't have a default engine. |
michael@0 | 167 | Assert.equal(day.get("default"), "NONE"); |
michael@0 | 168 | |
michael@0 | 169 | Services.search.addEngineWithDetails("testdefault", |
michael@0 | 170 | "http://localhost/icon.png", |
michael@0 | 171 | null, |
michael@0 | 172 | "test description", |
michael@0 | 173 | "GET", |
michael@0 | 174 | "http://localhost/search/%s"); |
michael@0 | 175 | let engine1 = Services.search.getEngineByName("testdefault"); |
michael@0 | 176 | Assert.ok(engine1); |
michael@0 | 177 | Services.search.defaultEngine = engine1; |
michael@0 | 178 | |
michael@0 | 179 | yield provider.collectDailyData(); |
michael@0 | 180 | data = yield m.getValues(); |
michael@0 | 181 | Assert.equal(data.days.getDay(now).get("default"), "other-testdefault"); |
michael@0 | 182 | |
michael@0 | 183 | yield storage.close(); |
michael@0 | 184 | }); |