diff options
Diffstat (limited to 'toolkit/components/terminator/tests')
4 files changed, 208 insertions, 0 deletions
diff --git a/toolkit/components/terminator/tests/xpcshell/.eslintrc.js b/toolkit/components/terminator/tests/xpcshell/.eslintrc.js new file mode 100644 index 000000000..d35787cd2 --- /dev/null +++ b/toolkit/components/terminator/tests/xpcshell/.eslintrc.js @@ -0,0 +1,7 @@ +"use strict"; + +module.exports = { + "extends": [ + "../../../../../testing/xpcshell/xpcshell.eslintrc.js" + ] +}; diff --git a/toolkit/components/terminator/tests/xpcshell/test_terminator_record.js b/toolkit/components/terminator/tests/xpcshell/test_terminator_record.js new file mode 100644 index 000000000..248ead9ce --- /dev/null +++ b/toolkit/components/terminator/tests/xpcshell/test_terminator_record.js @@ -0,0 +1,108 @@ +/* Any copyright is dedicated to the Public Domain. + * http://creativecommons.org/publicdomain/zero/1.0/ */ + +"use strict"; + + +// Test that the Shutdown Terminator records durations correctly + +var Cu = Components.utils; +var Cc = Components.classes; +var Ci = Components.interfaces; + +Cu.import("resource://gre/modules/Services.jsm", this); +Cu.import("resource://gre/modules/osfile.jsm", this); +Cu.import("resource://gre/modules/Timer.jsm", this); +Cu.import("resource://gre/modules/Task.jsm", this); + +var {Path, File, Constants} = OS; + +var PATH; +var PATH_TMP; +var terminator; + +add_task(function* init() { + do_get_profile(); + PATH = Path.join(Constants.Path.localProfileDir, "ShutdownDuration.json"); + PATH_TMP = PATH + ".tmp"; + + // Initialize the terminator + // (normally, this is done through the manifest file, but xpcshell + // doesn't take them into account). + do_print("Initializing the Terminator"); + terminator = Cc["@mozilla.org/toolkit/shutdown-terminator;1"]. + createInstance(Ci.nsIObserver); + terminator.observe(null, "profile-after-change", null); +}); + +var promiseShutdownDurationData = Task.async(function*() { + // Wait until PATH exists. + // Timeout if it is never created. + do_print("Waiting for file creation: " + PATH); + while (true) { + if ((yield OS.File.exists(PATH))) { + break; + } + + do_print("The file does not exist yet. Waiting 1 second."); + yield new Promise(resolve => setTimeout(resolve, 1000)); + } + + do_print("The file has been created"); + let raw = yield OS.File.read(PATH, { encoding: "utf-8"} ); + do_print(raw); + return JSON.parse(raw); +}); + +add_task(function* test_record() { + let PHASE0 = "profile-change-teardown"; + let PHASE1 = "profile-before-change"; + let PHASE2 = "xpcom-will-shutdown"; + let t0 = Date.now(); + + do_print("Starting shutdown"); + terminator.observe(null, "profile-change-teardown", null); + + do_print("Moving to next phase"); + terminator.observe(null, PHASE1, null); + + let data = yield promiseShutdownDurationData(); + + let t1 = Date.now(); + + Assert.ok(PHASE0 in data, "The file contains the expected key"); + let duration = data[PHASE0]; + Assert.equal(typeof duration, "number"); + Assert.ok(duration >= 0, "Duration is a non-negative number"); + Assert.ok(duration <= Math.ceil((t1 - t0) / 1000) + 1, + "Duration is reasonable"); + + Assert.equal(Object.keys(data).length, 1, "Data does not contain other durations"); + + do_print("Cleaning up and moving to next phase"); + yield File.remove(PATH); + yield File.remove(PATH_TMP); + + do_print("Waiting at least one tick"); + let WAIT_MS = 2000; + yield new Promise(resolve => setTimeout(resolve, WAIT_MS)); + + terminator.observe(null, PHASE2, null); + data = yield promiseShutdownDurationData(); + + let t2 = Date.now(); + + Assert.equal(Object.keys(data).sort().join(", "), + [PHASE0, PHASE1].sort().join(", "), + "The file contains the expected keys"); + Assert.equal(data[PHASE0], duration, "Duration of phase 0 hasn't changed"); + let duration2 = data[PHASE1]; + Assert.equal(typeof duration2, "number"); + Assert.ok(duration2 >= WAIT_MS / 2000, "We have waited at least " + (WAIT_MS / 2000) + " ticks"); + Assert.ok(duration2 <= Math.ceil((t2 - t1) / 1000) + 1, + "Duration is reasonable"); +}); + +function run_test() { + run_next_test(); +} diff --git a/toolkit/components/terminator/tests/xpcshell/test_terminator_reload.js b/toolkit/components/terminator/tests/xpcshell/test_terminator_reload.js new file mode 100644 index 000000000..1c16395b3 --- /dev/null +++ b/toolkit/components/terminator/tests/xpcshell/test_terminator_reload.js @@ -0,0 +1,85 @@ +/* Any copyright is dedicated to the Public Domain. + * http://creativecommons.org/publicdomain/zero/1.0/ */ + +"use strict"; + + +// Test that the Shutdown Terminator reloads durations correctly + +var Cu = Components.utils; +var Cc = Components.classes; +var Ci = Components.interfaces; + +Cu.import("resource://gre/modules/Services.jsm", this); +Cu.import("resource://gre/modules/osfile.jsm", this); +Cu.import("resource://gre/modules/Timer.jsm", this); +Cu.import("resource://gre/modules/Task.jsm", this); + +var {Path, File, Constants} = OS; + +var PATH; + +var HISTOGRAMS = { + "quit-application": "SHUTDOWN_PHASE_DURATION_TICKS_QUIT_APPLICATION", + "profile-change-teardown": "SHUTDOWN_PHASE_DURATION_TICKS_PROFILE_CHANGE_TEARDOWN", + "profile-before-change": "SHUTDOWN_PHASE_DURATION_TICKS_PROFILE_BEFORE_CHANGE", + "xpcom-will-shutdown": "SHUTDOWN_PHASE_DURATION_TICKS_XPCOM_WILL_SHUTDOWN", +}; + +add_task(function* init() { + do_get_profile(); + PATH = Path.join(Constants.Path.localProfileDir, "ShutdownDuration.json"); +}); + +add_task(function* test_reload() { + do_print("Forging data"); + let data = {}; + let telemetrySnapshots = Services.telemetry.histogramSnapshots; + let i = 0; + for (let k of Object.keys(HISTOGRAMS)) { + let id = HISTOGRAMS[k]; + data[k] = i++; + Assert.equal(telemetrySnapshots[id] || undefined, undefined, "Histogram " + id + " is empty"); + } + + + yield OS.File.writeAtomic(PATH, JSON.stringify(data)); + + const TOPIC = "shutdown-terminator-telemetry-updated"; + + let wait = new Promise(resolve => + Services.obs.addObserver( + function observer() { + do_print("Telemetry has been updated"); + Services.obs.removeObserver(observer, TOPIC); + resolve(); + }, + TOPIC, + false)); + + do_print("Starting nsTerminatorTelemetry"); + let tt = Cc["@mozilla.org/toolkit/shutdown-terminator-telemetry;1"]. + createInstance(Ci.nsIObserver); + tt.observe(null, "profile-after-change", ""); + + do_print("Waiting until telemetry is updated"); + // Now wait until Telemetry is updated + yield wait; + + telemetrySnapshots = Services.telemetry.histogramSnapshots; + for (let k of Object.keys(HISTOGRAMS)) { + let id = HISTOGRAMS[k]; + do_print("Testing histogram " + id); + let snapshot = telemetrySnapshots[id]; + let count = 0; + for (let x of snapshot.counts) { + count += x; + } + Assert.equal(count, 1, "We have added one item"); + } + +}); + +function run_test() { + run_next_test(); +} diff --git a/toolkit/components/terminator/tests/xpcshell/xpcshell.ini b/toolkit/components/terminator/tests/xpcshell/xpcshell.ini new file mode 100644 index 000000000..7f77938aa --- /dev/null +++ b/toolkit/components/terminator/tests/xpcshell/xpcshell.ini @@ -0,0 +1,8 @@ +[DEFAULT] +head= +tail= + +[test_terminator_record.js] +skip-if = debug # Disabled by bug 1242084, bug 1255484 will enable it again. +[test_terminator_reload.js] +skip-if = os == "android" |