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: const {utils: Cu} = Components; michael@0: michael@0: const MILLISECONDS_PER_DAY = 24 * 60 * 60 * 1000; michael@0: michael@0: // Create profile directory before use. michael@0: // It can be no older than a day ago…. michael@0: let profile_creation_lower = Date.now() - MILLISECONDS_PER_DAY; michael@0: do_get_profile(); michael@0: michael@0: Cu.import("resource://gre/modules/Promise.jsm"); michael@0: Cu.import("resource://gre/modules/Metrics.jsm"); michael@0: Cu.import("resource://gre/modules/services/healthreport/profile.jsm"); michael@0: Cu.import("resource://gre/modules/Task.jsm"); michael@0: michael@0: michael@0: function MockProfileMetadataProvider(name="MockProfileMetadataProvider") { michael@0: this.name = name; michael@0: ProfileMetadataProvider.call(this); michael@0: } michael@0: MockProfileMetadataProvider.prototype = { michael@0: __proto__: ProfileMetadataProvider.prototype, michael@0: michael@0: getProfileCreationDays: function getProfileCreationDays() { michael@0: return Promise.resolve(1234); michael@0: }, michael@0: }; michael@0: michael@0: michael@0: function run_test() { michael@0: run_next_test(); michael@0: } michael@0: michael@0: /** michael@0: * Ensure that OS.File works in our environment. michael@0: * This test can go once there are xpcshell tests for OS.File. michael@0: */ michael@0: add_test(function use_os_file() { michael@0: Cu.import("resource://gre/modules/osfile.jsm") michael@0: michael@0: // Ensure that we get constants, too. michael@0: do_check_neq(OS.Constants.Path.profileDir, null); michael@0: michael@0: let iterator = new OS.File.DirectoryIterator("."); michael@0: iterator.forEach(function onEntry(entry) { michael@0: print("Got " + entry.path); michael@0: }).then(function onSuccess() { michael@0: iterator.close(); michael@0: print("Done."); michael@0: run_next_test(); michael@0: }, function onFail() { michael@0: iterator.close(); michael@0: do_throw("Iterating over current directory failed."); michael@0: }); michael@0: }); michael@0: michael@0: function getAccessor() { michael@0: let acc = new ProfileCreationTimeAccessor(); michael@0: print("Profile is " + acc.profilePath); michael@0: return acc; michael@0: } michael@0: michael@0: add_test(function test_time_accessor_no_file() { michael@0: let acc = getAccessor(); michael@0: michael@0: // There should be no file yet. michael@0: acc.readTimes() michael@0: .then(function onSuccess(json) { michael@0: do_throw("File existed!"); michael@0: }, michael@0: function onFailure() { michael@0: run_next_test(); michael@0: }); michael@0: }); michael@0: michael@0: add_task(function test_time_accessor_named_file() { michael@0: let acc = getAccessor(); michael@0: michael@0: // There should be no file yet. michael@0: yield acc.writeTimes({created: 12345}, "test.json"); michael@0: let json = yield acc.readTimes("test.json") michael@0: print("Read: " + JSON.stringify(json)); michael@0: do_check_eq(12345, json.created); michael@0: }); michael@0: michael@0: add_task(function test_time_accessor_creates_file() { michael@0: let lower = profile_creation_lower; michael@0: michael@0: // Ensure that provided contents are merged, and existing michael@0: // files can be overwritten. These two things occur if we michael@0: // read and then decide that we have to write. michael@0: let acc = getAccessor(); michael@0: let existing = {abc: "123", easy: "abc"}; michael@0: let expected; michael@0: michael@0: let created = yield acc.computeAndPersistTimes(existing, "test2.json") michael@0: let upper = Date.now() + 1000; michael@0: print(lower + " < " + created + " <= " + upper); michael@0: do_check_true(lower < created); michael@0: do_check_true(upper >= created); michael@0: expected = created; michael@0: michael@0: let json = yield acc.readTimes("test2.json") michael@0: print("Read: " + JSON.stringify(json)); michael@0: do_check_eq("123", json.abc); michael@0: do_check_eq("abc", json.easy); michael@0: do_check_eq(expected, json.created); michael@0: }); michael@0: michael@0: add_task(function test_time_accessor_all() { michael@0: let lower = profile_creation_lower; michael@0: let acc = getAccessor(); michael@0: let expected; michael@0: let created = yield acc.created michael@0: let upper = Date.now() + 1000; michael@0: do_check_true(lower < created); michael@0: do_check_true(upper >= created); michael@0: expected = created; michael@0: michael@0: let again = yield acc.created michael@0: do_check_eq(expected, again); michael@0: }); michael@0: michael@0: add_test(function test_constructor() { michael@0: let provider = new ProfileMetadataProvider("named"); michael@0: run_next_test(); michael@0: }); michael@0: michael@0: add_test(function test_profile_files() { michael@0: let provider = new ProfileMetadataProvider(); michael@0: michael@0: function onSuccess(answer) { michael@0: let now = Date.now() / MILLISECONDS_PER_DAY; michael@0: print("Got " + answer + ", versus now = " + now); michael@0: do_check_true(answer < now); michael@0: run_next_test(); michael@0: } michael@0: michael@0: function onFailure(ex) { michael@0: do_throw("Directory iteration failed: " + ex); michael@0: } michael@0: michael@0: provider.getProfileCreationDays().then(onSuccess, onFailure); michael@0: }); michael@0: michael@0: // A generic test helper. We use this with both real michael@0: // and mock providers in these tests. michael@0: function test_collect_constant(provider) { michael@0: return Task.spawn(function () { michael@0: yield provider.collectConstantData(); michael@0: michael@0: let m = provider.getMeasurement("age", 1); michael@0: do_check_neq(m, null); michael@0: let values = yield m.getValues(); michael@0: do_check_eq(values.singular.size, 1); michael@0: do_check_true(values.singular.has("profileCreation")); michael@0: michael@0: throw new Task.Result(values.singular.get("profileCreation")[1]); michael@0: }); michael@0: } michael@0: michael@0: add_task(function test_collect_constant_mock() { michael@0: let storage = yield Metrics.Storage("collect_constant_mock"); michael@0: let provider = new MockProfileMetadataProvider(); michael@0: yield provider.init(storage); michael@0: michael@0: let v = yield test_collect_constant(provider); michael@0: do_check_eq(v, 1234); michael@0: michael@0: yield storage.close(); michael@0: }); michael@0: michael@0: add_task(function test_collect_constant_real() { michael@0: let provider = new ProfileMetadataProvider(); michael@0: let storage = yield Metrics.Storage("collect_constant_real"); michael@0: yield provider.init(storage); michael@0: michael@0: let v = yield test_collect_constant(provider); michael@0: michael@0: let ms = v * MILLISECONDS_PER_DAY; michael@0: let lower = profile_creation_lower; michael@0: let upper = Date.now() + 1000; michael@0: print("Day: " + v); michael@0: print("msec: " + ms); michael@0: print("Lower: " + lower); michael@0: print("Upper: " + upper); michael@0: do_check_true(lower <= ms); michael@0: do_check_true(upper >= ms); michael@0: michael@0: yield storage.close(); michael@0: }); michael@0: