gecko-dev/toolkit/components/startup/tests/unit/test_startup_crash.js
Kris Maglione e930b89c34 Bug 1514594: Part 3 - Change ChromeUtils.import API.
***
Bug 1514594: Part 3a - Change ChromeUtils.import to return an exports object; not pollute global. r=mccr8

This changes the behavior of ChromeUtils.import() to return an exports object,
rather than a module global, in all cases except when `null` is passed as a
second argument, and changes the default behavior not to pollute the global
scope with the module's exports. Thus, the following code written for the old
model:

  ChromeUtils.import("resource://gre/modules/Services.jsm");

is approximately the same as the following, in the new model:

  var {Services} = ChromeUtils.import("resource://gre/modules/Services.jsm");

Since the two behaviors are mutually incompatible, this patch will land with a
scripted rewrite to update all existing callers to use the new model rather
than the old.
***
Bug 1514594: Part 3b - Mass rewrite all JS code to use the new ChromeUtils.import API. rs=Gijs

This was done using the followng script:

https://bitbucket.org/kmaglione/m-c-rewrites/src/tip/processors/cu-import-exports.jsm
***
Bug 1514594: Part 3c - Update ESLint plugin for ChromeUtils.import API changes. r=Standard8

Differential Revision: https://phabricator.services.mozilla.com/D16747
***
Bug 1514594: Part 3d - Remove/fix hundreds of duplicate imports from sync tests. r=Gijs

Differential Revision: https://phabricator.services.mozilla.com/D16748
***
Bug 1514594: Part 3e - Remove no-op ChromeUtils.import() calls. r=Gijs

Differential Revision: https://phabricator.services.mozilla.com/D16749
***
Bug 1514594: Part 3f.1 - Cleanup various test corner cases after mass rewrite. r=Gijs
***
Bug 1514594: Part 3f.2 - Cleanup various non-test corner cases after mass rewrite. r=Gijs

Differential Revision: https://phabricator.services.mozilla.com/D16750

--HG--
extra : rebase_source : 359574ee3064c90f33bf36c2ebe3159a24cc8895
extra : histedit_source : b93c8f42808b1599f9122d7842d2c0b3e656a594%2C64a3a4e3359dc889e2ab2b49461bab9e27fc10a7
2019-01-17 10:18:31 -08:00

296 lines
13 KiB
JavaScript

/* Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/ */
const {Services} = ChromeUtils.import("resource://gre/modules/Services.jsm");
createAppInfo("xpcshell@tests.mozilla.org", "XPCShell", "1", "10.0");
var prefService = Services.prefs;
var appStartup = Services.startup;
const pref_last_success = "toolkit.startup.last_success";
const pref_recent_crashes = "toolkit.startup.recent_crashes";
const pref_max_resumed_crashes = "toolkit.startup.max_resumed_crashes";
const pref_always_use_safe_mode = "toolkit.startup.always_use_safe_mode";
function run_test() {
prefService.setBoolPref(pref_always_use_safe_mode, true);
resetTestEnv(0);
test_trackStartupCrashBegin();
test_trackStartupCrashEnd();
test_trackStartupCrashBegin_safeMode();
test_trackStartupCrashEnd_safeMode();
test_maxResumed();
resetTestEnv(0);
prefService.clearUserPref(pref_always_use_safe_mode);
}
// reset prefs to default
function resetTestEnv(replacedLockTime) {
try {
// call begin to reset mStartupCrashTrackingEnded
appStartup.trackStartupCrashBegin();
} catch (x) { }
prefService.setIntPref(pref_max_resumed_crashes, 2);
prefService.clearUserPref(pref_recent_crashes);
gAppInfo.replacedLockTime = replacedLockTime;
prefService.clearUserPref(pref_last_success);
}
function now_seconds() {
return ms_to_s(Date.now());
}
function ms_to_s(ms) {
return Math.floor(ms / 1000);
}
function test_trackStartupCrashBegin() {
let max_resumed = prefService.getIntPref(pref_max_resumed_crashes);
Assert.ok(!gAppInfo.inSafeMode);
// first run with startup crash tracking - existing profile lock
let replacedLockTime = Date.now();
resetTestEnv(replacedLockTime);
Assert.ok(!prefService.prefHasUserValue(pref_recent_crashes));
Assert.ok(!prefService.prefHasUserValue(pref_last_success));
Assert.equal(replacedLockTime, gAppInfo.replacedLockTime);
try {
Assert.ok(!appStartup.trackStartupCrashBegin());
do_throw("Should have thrown since last_success is not set");
} catch (x) { }
Assert.ok(!prefService.prefHasUserValue(pref_last_success));
Assert.ok(!prefService.prefHasUserValue(pref_recent_crashes));
Assert.ok(!appStartup.automaticSafeModeNecessary);
// first run with startup crash tracking - no existing profile lock
replacedLockTime = 0;
resetTestEnv(replacedLockTime);
Assert.ok(!prefService.prefHasUserValue(pref_recent_crashes));
Assert.ok(!prefService.prefHasUserValue(pref_last_success));
Assert.equal(replacedLockTime, gAppInfo.replacedLockTime);
try {
Assert.ok(!appStartup.trackStartupCrashBegin());
do_throw("Should have thrown since last_success is not set");
} catch (x) { }
Assert.ok(!prefService.prefHasUserValue(pref_last_success));
Assert.ok(!prefService.prefHasUserValue(pref_recent_crashes));
Assert.ok(!appStartup.automaticSafeModeNecessary);
// normal startup - last startup was success
replacedLockTime = Date.now();
resetTestEnv(replacedLockTime);
Assert.ok(!prefService.prefHasUserValue(pref_recent_crashes));
prefService.setIntPref(pref_last_success, ms_to_s(replacedLockTime));
Assert.equal(ms_to_s(replacedLockTime), prefService.getIntPref(pref_last_success));
Assert.ok(!appStartup.trackStartupCrashBegin());
Assert.equal(ms_to_s(replacedLockTime), prefService.getIntPref(pref_last_success));
Assert.ok(!prefService.prefHasUserValue(pref_recent_crashes));
Assert.ok(!appStartup.automaticSafeModeNecessary);
// normal startup with 1 recent crash
resetTestEnv(replacedLockTime);
prefService.setIntPref(pref_recent_crashes, 1);
prefService.setIntPref(pref_last_success, ms_to_s(replacedLockTime));
Assert.ok(!appStartup.trackStartupCrashBegin());
Assert.equal(ms_to_s(replacedLockTime), prefService.getIntPref(pref_last_success));
Assert.equal(1, prefService.getIntPref(pref_recent_crashes));
Assert.ok(!appStartup.automaticSafeModeNecessary);
// normal startup with max_resumed_crashes crash
resetTestEnv(replacedLockTime);
prefService.setIntPref(pref_recent_crashes, max_resumed);
prefService.setIntPref(pref_last_success, ms_to_s(replacedLockTime));
Assert.ok(!appStartup.trackStartupCrashBegin());
Assert.equal(ms_to_s(replacedLockTime), prefService.getIntPref(pref_last_success));
Assert.equal(max_resumed, prefService.getIntPref(pref_recent_crashes));
Assert.ok(!appStartup.automaticSafeModeNecessary);
// normal startup with too many recent crashes
resetTestEnv(replacedLockTime);
prefService.setIntPref(pref_recent_crashes, max_resumed + 1);
prefService.setIntPref(pref_last_success, ms_to_s(replacedLockTime));
Assert.ok(appStartup.trackStartupCrashBegin());
// should remain the same since the last startup was not a crash
Assert.equal(max_resumed + 1, prefService.getIntPref(pref_recent_crashes));
Assert.ok(appStartup.automaticSafeModeNecessary);
// normal startup with too many recent crashes and startup crash tracking disabled
resetTestEnv(replacedLockTime);
prefService.setIntPref(pref_max_resumed_crashes, -1);
prefService.setIntPref(pref_recent_crashes, max_resumed + 1);
prefService.setIntPref(pref_last_success, ms_to_s(replacedLockTime));
Assert.ok(!appStartup.trackStartupCrashBegin());
// should remain the same since the last startup was not a crash
Assert.equal(max_resumed + 1, prefService.getIntPref(pref_recent_crashes));
// returns false when disabled
Assert.ok(!appStartup.automaticSafeModeNecessary);
Assert.equal(-1, prefService.getIntPref(pref_max_resumed_crashes));
// normal startup after 1 non-recent crash (1 year ago), no other recent
replacedLockTime = Date.now() - 365 * 24 * 60 * 60 * 1000;
resetTestEnv(replacedLockTime);
prefService.setIntPref(pref_last_success, ms_to_s(replacedLockTime) - 365 * 24 * 60 * 60);
Assert.ok(!appStartup.trackStartupCrashBegin());
// recent crash count pref should be unset since the last crash was not recent
Assert.ok(!prefService.prefHasUserValue(pref_recent_crashes));
Assert.ok(!appStartup.automaticSafeModeNecessary);
// normal startup after 1 crash (1 minute ago), no other recent
replacedLockTime = Date.now() - 60 * 1000;
resetTestEnv(replacedLockTime);
prefService.setIntPref(pref_last_success, ms_to_s(replacedLockTime) - 60 * 60); // last success - 1 hour ago
Assert.ok(!appStartup.trackStartupCrashBegin());
// recent crash count pref should be created with value 1
Assert.equal(1, prefService.getIntPref(pref_recent_crashes));
Assert.ok(!appStartup.automaticSafeModeNecessary);
// normal startup after another crash (1 minute ago), 1 already
prefService.setIntPref(pref_last_success, ms_to_s(replacedLockTime) - 60 * 60); // last success - 1 hour ago
replacedLockTime = Date.now() - 60 * 1000;
gAppInfo.replacedLockTime = replacedLockTime;
Assert.ok(!appStartup.trackStartupCrashBegin());
// recent crash count pref should be incremented by 1
Assert.equal(2, prefService.getIntPref(pref_recent_crashes));
Assert.ok(!appStartup.automaticSafeModeNecessary);
// normal startup after another crash (1 minute ago), 2 already
prefService.setIntPref(pref_last_success, ms_to_s(replacedLockTime) - 60 * 60); // last success - 1 hour ago
Assert.ok(appStartup.trackStartupCrashBegin());
// recent crash count pref should be incremented by 1
Assert.equal(3, prefService.getIntPref(pref_recent_crashes));
Assert.ok(appStartup.automaticSafeModeNecessary);
// normal startup after 1 non-recent crash (1 year ago), 3 crashes already
replacedLockTime = Date.now() - 365 * 24 * 60 * 60 * 1000;
resetTestEnv(replacedLockTime);
prefService.setIntPref(pref_last_success, ms_to_s(replacedLockTime) - 60 * 60); // last success - 1 hour ago
Assert.ok(!appStartup.trackStartupCrashBegin());
// recent crash count should be unset since the last crash was not recent
Assert.ok(!prefService.prefHasUserValue(pref_recent_crashes));
Assert.ok(!appStartup.automaticSafeModeNecessary);
}
function test_trackStartupCrashEnd() {
// successful startup with no last_success (upgrade test)
let replacedLockTime = Date.now() - 10 * 1000; // 10s ago
resetTestEnv(replacedLockTime);
try {
appStartup.trackStartupCrashBegin(); // required to be called before end
do_throw("Should have thrown since last_success is not set");
} catch (x) { }
appStartup.trackStartupCrashEnd();
Assert.ok(!prefService.prefHasUserValue(pref_recent_crashes));
Assert.ok(!prefService.prefHasUserValue(pref_last_success));
// successful startup - should set last_success
replacedLockTime = Date.now() - 10 * 1000; // 10s ago
resetTestEnv(replacedLockTime);
prefService.setIntPref(pref_last_success, ms_to_s(replacedLockTime));
appStartup.trackStartupCrashBegin(); // required to be called before end
appStartup.trackStartupCrashEnd();
// ensure last_success was set since we have declared a succesful startup
// main timestamp doesn't get set in XPCShell so approximate with now
Assert.ok(prefService.getIntPref(pref_last_success) <= now_seconds());
Assert.ok(prefService.getIntPref(pref_last_success) >= now_seconds() - 4 * 60 * 60);
Assert.ok(!prefService.prefHasUserValue(pref_recent_crashes));
// successful startup with 1 recent crash
resetTestEnv(replacedLockTime);
prefService.setIntPref(pref_last_success, ms_to_s(replacedLockTime));
prefService.setIntPref(pref_recent_crashes, 1);
appStartup.trackStartupCrashBegin(); // required to be called before end
appStartup.trackStartupCrashEnd();
// ensure recent_crashes was cleared since we have declared a succesful startup
Assert.ok(!prefService.prefHasUserValue(pref_recent_crashes));
}
function test_trackStartupCrashBegin_safeMode() {
gAppInfo.inSafeMode = true;
resetTestEnv(0);
let max_resumed = prefService.getIntPref(pref_max_resumed_crashes);
// check manual safe mode doesn't change prefs without crash
let replacedLockTime = Date.now() - 10 * 1000; // 10s ago
resetTestEnv(replacedLockTime);
prefService.setIntPref(pref_last_success, ms_to_s(replacedLockTime));
Assert.ok(!prefService.prefHasUserValue(pref_recent_crashes));
Assert.ok(prefService.prefHasUserValue(pref_last_success));
Assert.ok(!appStartup.automaticSafeModeNecessary);
Assert.ok(!appStartup.trackStartupCrashBegin());
Assert.ok(!prefService.prefHasUserValue(pref_recent_crashes));
Assert.ok(prefService.prefHasUserValue(pref_last_success));
Assert.ok(!appStartup.automaticSafeModeNecessary);
// check forced safe mode doesn't change prefs without crash
replacedLockTime = Date.now() - 10 * 1000; // 10s ago
resetTestEnv(replacedLockTime);
prefService.setIntPref(pref_last_success, ms_to_s(replacedLockTime));
prefService.setIntPref(pref_recent_crashes, max_resumed + 1);
Assert.equal(max_resumed + 1, prefService.getIntPref(pref_recent_crashes));
Assert.ok(prefService.prefHasUserValue(pref_last_success));
Assert.ok(!appStartup.automaticSafeModeNecessary);
Assert.ok(appStartup.trackStartupCrashBegin());
Assert.equal(max_resumed + 1, prefService.getIntPref(pref_recent_crashes));
Assert.ok(prefService.prefHasUserValue(pref_last_success));
Assert.ok(appStartup.automaticSafeModeNecessary);
// check forced safe mode after old crash
replacedLockTime = Date.now() - 365 * 24 * 60 * 60 * 1000;
resetTestEnv(replacedLockTime);
// one year ago
let last_success = ms_to_s(replacedLockTime) - 365 * 24 * 60 * 60;
prefService.setIntPref(pref_last_success, last_success);
prefService.setIntPref(pref_recent_crashes, max_resumed + 1);
Assert.equal(max_resumed + 1, prefService.getIntPref(pref_recent_crashes));
Assert.ok(prefService.prefHasUserValue(pref_last_success));
Assert.ok(appStartup.automaticSafeModeNecessary);
Assert.ok(appStartup.trackStartupCrashBegin());
Assert.equal(max_resumed + 1, prefService.getIntPref(pref_recent_crashes));
Assert.equal(last_success, prefService.getIntPref(pref_last_success));
Assert.ok(appStartup.automaticSafeModeNecessary);
}
function test_trackStartupCrashEnd_safeMode() {
gAppInfo.inSafeMode = true;
let replacedLockTime = Date.now();
resetTestEnv(replacedLockTime);
let max_resumed = prefService.getIntPref(pref_max_resumed_crashes);
prefService.setIntPref(pref_last_success, ms_to_s(replacedLockTime) - 24 * 60 * 60);
// ensure recent_crashes were not cleared in manual safe mode
prefService.setIntPref(pref_recent_crashes, 1);
appStartup.trackStartupCrashBegin(); // required to be called before end
appStartup.trackStartupCrashEnd();
Assert.equal(1, prefService.getIntPref(pref_recent_crashes));
// recent_crashes should be set to max_resumed in forced safe mode to allow the user
// to try and start in regular mode after making changes.
prefService.setIntPref(pref_recent_crashes, max_resumed + 1);
appStartup.trackStartupCrashBegin(); // required to be called before end
appStartup.trackStartupCrashEnd();
Assert.equal(max_resumed, prefService.getIntPref(pref_recent_crashes));
}
function test_maxResumed() {
resetTestEnv(0);
gAppInfo.inSafeMode = false;
let max_resumed = prefService.getIntPref(pref_max_resumed_crashes);
let replacedLockTime = Date.now();
resetTestEnv(replacedLockTime);
prefService.setIntPref(pref_max_resumed_crashes, -1);
prefService.setIntPref(pref_recent_crashes, max_resumed + 1);
prefService.setIntPref(pref_last_success, ms_to_s(replacedLockTime) - 24 * 60 * 60);
appStartup.trackStartupCrashBegin();
// should remain the same since the last startup was not a crash
Assert.equal(max_resumed + 2, prefService.getIntPref(pref_recent_crashes));
Assert.ok(!appStartup.automaticSafeModeNecessary);
}