Wed, 31 Dec 2014 07:53:36 +0100
Correct small whitespace inconsistency, lost while renaming variables.
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 | const MILLISECONDS_PER_DAY = 24 * 60 * 60 * 1000; |
michael@0 | 9 | |
michael@0 | 10 | // Create profile directory before use. |
michael@0 | 11 | // It can be no older than a day ago…. |
michael@0 | 12 | let profile_creation_lower = Date.now() - MILLISECONDS_PER_DAY; |
michael@0 | 13 | do_get_profile(); |
michael@0 | 14 | |
michael@0 | 15 | Cu.import("resource://gre/modules/Promise.jsm"); |
michael@0 | 16 | Cu.import("resource://gre/modules/Metrics.jsm"); |
michael@0 | 17 | Cu.import("resource://gre/modules/services/healthreport/profile.jsm"); |
michael@0 | 18 | Cu.import("resource://gre/modules/Task.jsm"); |
michael@0 | 19 | |
michael@0 | 20 | |
michael@0 | 21 | function MockProfileMetadataProvider(name="MockProfileMetadataProvider") { |
michael@0 | 22 | this.name = name; |
michael@0 | 23 | ProfileMetadataProvider.call(this); |
michael@0 | 24 | } |
michael@0 | 25 | MockProfileMetadataProvider.prototype = { |
michael@0 | 26 | __proto__: ProfileMetadataProvider.prototype, |
michael@0 | 27 | |
michael@0 | 28 | getProfileCreationDays: function getProfileCreationDays() { |
michael@0 | 29 | return Promise.resolve(1234); |
michael@0 | 30 | }, |
michael@0 | 31 | }; |
michael@0 | 32 | |
michael@0 | 33 | |
michael@0 | 34 | function run_test() { |
michael@0 | 35 | run_next_test(); |
michael@0 | 36 | } |
michael@0 | 37 | |
michael@0 | 38 | /** |
michael@0 | 39 | * Ensure that OS.File works in our environment. |
michael@0 | 40 | * This test can go once there are xpcshell tests for OS.File. |
michael@0 | 41 | */ |
michael@0 | 42 | add_test(function use_os_file() { |
michael@0 | 43 | Cu.import("resource://gre/modules/osfile.jsm") |
michael@0 | 44 | |
michael@0 | 45 | // Ensure that we get constants, too. |
michael@0 | 46 | do_check_neq(OS.Constants.Path.profileDir, null); |
michael@0 | 47 | |
michael@0 | 48 | let iterator = new OS.File.DirectoryIterator("."); |
michael@0 | 49 | iterator.forEach(function onEntry(entry) { |
michael@0 | 50 | print("Got " + entry.path); |
michael@0 | 51 | }).then(function onSuccess() { |
michael@0 | 52 | iterator.close(); |
michael@0 | 53 | print("Done."); |
michael@0 | 54 | run_next_test(); |
michael@0 | 55 | }, function onFail() { |
michael@0 | 56 | iterator.close(); |
michael@0 | 57 | do_throw("Iterating over current directory failed."); |
michael@0 | 58 | }); |
michael@0 | 59 | }); |
michael@0 | 60 | |
michael@0 | 61 | function getAccessor() { |
michael@0 | 62 | let acc = new ProfileCreationTimeAccessor(); |
michael@0 | 63 | print("Profile is " + acc.profilePath); |
michael@0 | 64 | return acc; |
michael@0 | 65 | } |
michael@0 | 66 | |
michael@0 | 67 | add_test(function test_time_accessor_no_file() { |
michael@0 | 68 | let acc = getAccessor(); |
michael@0 | 69 | |
michael@0 | 70 | // There should be no file yet. |
michael@0 | 71 | acc.readTimes() |
michael@0 | 72 | .then(function onSuccess(json) { |
michael@0 | 73 | do_throw("File existed!"); |
michael@0 | 74 | }, |
michael@0 | 75 | function onFailure() { |
michael@0 | 76 | run_next_test(); |
michael@0 | 77 | }); |
michael@0 | 78 | }); |
michael@0 | 79 | |
michael@0 | 80 | add_task(function test_time_accessor_named_file() { |
michael@0 | 81 | let acc = getAccessor(); |
michael@0 | 82 | |
michael@0 | 83 | // There should be no file yet. |
michael@0 | 84 | yield acc.writeTimes({created: 12345}, "test.json"); |
michael@0 | 85 | let json = yield acc.readTimes("test.json") |
michael@0 | 86 | print("Read: " + JSON.stringify(json)); |
michael@0 | 87 | do_check_eq(12345, json.created); |
michael@0 | 88 | }); |
michael@0 | 89 | |
michael@0 | 90 | add_task(function test_time_accessor_creates_file() { |
michael@0 | 91 | let lower = profile_creation_lower; |
michael@0 | 92 | |
michael@0 | 93 | // Ensure that provided contents are merged, and existing |
michael@0 | 94 | // files can be overwritten. These two things occur if we |
michael@0 | 95 | // read and then decide that we have to write. |
michael@0 | 96 | let acc = getAccessor(); |
michael@0 | 97 | let existing = {abc: "123", easy: "abc"}; |
michael@0 | 98 | let expected; |
michael@0 | 99 | |
michael@0 | 100 | let created = yield acc.computeAndPersistTimes(existing, "test2.json") |
michael@0 | 101 | let upper = Date.now() + 1000; |
michael@0 | 102 | print(lower + " < " + created + " <= " + upper); |
michael@0 | 103 | do_check_true(lower < created); |
michael@0 | 104 | do_check_true(upper >= created); |
michael@0 | 105 | expected = created; |
michael@0 | 106 | |
michael@0 | 107 | let json = yield acc.readTimes("test2.json") |
michael@0 | 108 | print("Read: " + JSON.stringify(json)); |
michael@0 | 109 | do_check_eq("123", json.abc); |
michael@0 | 110 | do_check_eq("abc", json.easy); |
michael@0 | 111 | do_check_eq(expected, json.created); |
michael@0 | 112 | }); |
michael@0 | 113 | |
michael@0 | 114 | add_task(function test_time_accessor_all() { |
michael@0 | 115 | let lower = profile_creation_lower; |
michael@0 | 116 | let acc = getAccessor(); |
michael@0 | 117 | let expected; |
michael@0 | 118 | let created = yield acc.created |
michael@0 | 119 | let upper = Date.now() + 1000; |
michael@0 | 120 | do_check_true(lower < created); |
michael@0 | 121 | do_check_true(upper >= created); |
michael@0 | 122 | expected = created; |
michael@0 | 123 | |
michael@0 | 124 | let again = yield acc.created |
michael@0 | 125 | do_check_eq(expected, again); |
michael@0 | 126 | }); |
michael@0 | 127 | |
michael@0 | 128 | add_test(function test_constructor() { |
michael@0 | 129 | let provider = new ProfileMetadataProvider("named"); |
michael@0 | 130 | run_next_test(); |
michael@0 | 131 | }); |
michael@0 | 132 | |
michael@0 | 133 | add_test(function test_profile_files() { |
michael@0 | 134 | let provider = new ProfileMetadataProvider(); |
michael@0 | 135 | |
michael@0 | 136 | function onSuccess(answer) { |
michael@0 | 137 | let now = Date.now() / MILLISECONDS_PER_DAY; |
michael@0 | 138 | print("Got " + answer + ", versus now = " + now); |
michael@0 | 139 | do_check_true(answer < now); |
michael@0 | 140 | run_next_test(); |
michael@0 | 141 | } |
michael@0 | 142 | |
michael@0 | 143 | function onFailure(ex) { |
michael@0 | 144 | do_throw("Directory iteration failed: " + ex); |
michael@0 | 145 | } |
michael@0 | 146 | |
michael@0 | 147 | provider.getProfileCreationDays().then(onSuccess, onFailure); |
michael@0 | 148 | }); |
michael@0 | 149 | |
michael@0 | 150 | // A generic test helper. We use this with both real |
michael@0 | 151 | // and mock providers in these tests. |
michael@0 | 152 | function test_collect_constant(provider) { |
michael@0 | 153 | return Task.spawn(function () { |
michael@0 | 154 | yield provider.collectConstantData(); |
michael@0 | 155 | |
michael@0 | 156 | let m = provider.getMeasurement("age", 1); |
michael@0 | 157 | do_check_neq(m, null); |
michael@0 | 158 | let values = yield m.getValues(); |
michael@0 | 159 | do_check_eq(values.singular.size, 1); |
michael@0 | 160 | do_check_true(values.singular.has("profileCreation")); |
michael@0 | 161 | |
michael@0 | 162 | throw new Task.Result(values.singular.get("profileCreation")[1]); |
michael@0 | 163 | }); |
michael@0 | 164 | } |
michael@0 | 165 | |
michael@0 | 166 | add_task(function test_collect_constant_mock() { |
michael@0 | 167 | let storage = yield Metrics.Storage("collect_constant_mock"); |
michael@0 | 168 | let provider = new MockProfileMetadataProvider(); |
michael@0 | 169 | yield provider.init(storage); |
michael@0 | 170 | |
michael@0 | 171 | let v = yield test_collect_constant(provider); |
michael@0 | 172 | do_check_eq(v, 1234); |
michael@0 | 173 | |
michael@0 | 174 | yield storage.close(); |
michael@0 | 175 | }); |
michael@0 | 176 | |
michael@0 | 177 | add_task(function test_collect_constant_real() { |
michael@0 | 178 | let provider = new ProfileMetadataProvider(); |
michael@0 | 179 | let storage = yield Metrics.Storage("collect_constant_real"); |
michael@0 | 180 | yield provider.init(storage); |
michael@0 | 181 | |
michael@0 | 182 | let v = yield test_collect_constant(provider); |
michael@0 | 183 | |
michael@0 | 184 | let ms = v * MILLISECONDS_PER_DAY; |
michael@0 | 185 | let lower = profile_creation_lower; |
michael@0 | 186 | let upper = Date.now() + 1000; |
michael@0 | 187 | print("Day: " + v); |
michael@0 | 188 | print("msec: " + ms); |
michael@0 | 189 | print("Lower: " + lower); |
michael@0 | 190 | print("Upper: " + upper); |
michael@0 | 191 | do_check_true(lower <= ms); |
michael@0 | 192 | do_check_true(upper >= ms); |
michael@0 | 193 | |
michael@0 | 194 | yield storage.close(); |
michael@0 | 195 | }); |
michael@0 | 196 |