From 36d22d82aa202bb199967e9512281e9a53db42c9 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 7 Apr 2024 21:33:14 +0200 Subject: Adding upstream version 115.7.0esr. Signed-off-by: Daniel Baumann --- toolkit/components/crashmonitor/test/unit/head.js | 26 ++++++++++++++++++++++ .../components/crashmonitor/test/unit/test_init.js | 17 ++++++++++++++ .../crashmonitor/test/unit/test_invalid_file.js | 23 +++++++++++++++++++ .../crashmonitor/test/unit/test_invalid_json.js | 19 ++++++++++++++++ .../crashmonitor/test/unit/test_missing_file.js | 13 +++++++++++ .../crashmonitor/test/unit/test_register.js | 25 +++++++++++++++++++++ .../crashmonitor/test/unit/test_valid_file.js | 24 ++++++++++++++++++++ .../components/crashmonitor/test/unit/xpcshell.ini | 10 +++++++++ 8 files changed, 157 insertions(+) create mode 100644 toolkit/components/crashmonitor/test/unit/head.js create mode 100644 toolkit/components/crashmonitor/test/unit/test_init.js create mode 100644 toolkit/components/crashmonitor/test/unit/test_invalid_file.js create mode 100644 toolkit/components/crashmonitor/test/unit/test_invalid_json.js create mode 100644 toolkit/components/crashmonitor/test/unit/test_missing_file.js create mode 100644 toolkit/components/crashmonitor/test/unit/test_register.js create mode 100644 toolkit/components/crashmonitor/test/unit/test_valid_file.js create mode 100644 toolkit/components/crashmonitor/test/unit/xpcshell.ini (limited to 'toolkit/components/crashmonitor/test/unit') diff --git a/toolkit/components/crashmonitor/test/unit/head.js b/toolkit/components/crashmonitor/test/unit/head.js new file mode 100644 index 0000000000..1d3be083de --- /dev/null +++ b/toolkit/components/crashmonitor/test/unit/head.js @@ -0,0 +1,26 @@ +/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at http://mozilla.org/MPL/2.0/. */ + +var { XPCOMUtils } = ChromeUtils.importESModule( + "resource://gre/modules/XPCOMUtils.sys.mjs" +); + +var sessionCheckpointsPath; +var CrashMonitor; + +/** + * Start the tasks of the different tests + */ +function run_test() { + do_get_profile(); + sessionCheckpointsPath = PathUtils.join( + PathUtils.profileDir, + "sessionCheckpoints.json" + ); + ({ CrashMonitor } = ChromeUtils.importESModule( + "resource://gre/modules/CrashMonitor.sys.mjs" + )); + run_next_test(); +} diff --git a/toolkit/components/crashmonitor/test/unit/test_init.js b/toolkit/components/crashmonitor/test/unit/test_init.js new file mode 100644 index 0000000000..633a5701d6 --- /dev/null +++ b/toolkit/components/crashmonitor/test/unit/test_init.js @@ -0,0 +1,17 @@ +/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at http://mozilla.org/MPL/2.0/. */ + +/** + * Test that calling |init| twice throws an error + */ +add_task(function test_init() { + CrashMonitor.init(); + try { + CrashMonitor.init(); + Assert.ok(false); + } catch (ex) { + Assert.ok(true); + } +}); diff --git a/toolkit/components/crashmonitor/test/unit/test_invalid_file.js b/toolkit/components/crashmonitor/test/unit/test_invalid_file.js new file mode 100644 index 0000000000..f64fc9183c --- /dev/null +++ b/toolkit/components/crashmonitor/test/unit/test_invalid_file.js @@ -0,0 +1,23 @@ +/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at http://mozilla.org/MPL/2.0/. */ + +/** + * Test with sessionCheckpoints.json containing invalid data + */ +add_task(async function test_invalid_file() { + // Write bogus data to checkpoint file + let data = "1234"; + await IOUtils.writeUTF8(sessionCheckpointsPath, data, { + tmpPath: sessionCheckpointsPath + ".tmp", + }); + + // An invalid file will cause |init| to return null + let status = await CrashMonitor.init(); + Assert.ok(!!(status === null)); + + // and |previousCheckpoints| will be null + let checkpoints = await CrashMonitor.previousCheckpoints; + Assert.ok(!!(checkpoints === null)); +}); diff --git a/toolkit/components/crashmonitor/test/unit/test_invalid_json.js b/toolkit/components/crashmonitor/test/unit/test_invalid_json.js new file mode 100644 index 0000000000..d49852a280 --- /dev/null +++ b/toolkit/components/crashmonitor/test/unit/test_invalid_json.js @@ -0,0 +1,19 @@ +/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at http://mozilla.org/MPL/2.0/. */ + +/** + * Test with sessionCheckpoints.json containing invalid JSON data + */ +add_task(async function test_invalid_file() { + // Write bogus data to checkpoint file + let data = "[}"; + await IOUtils.writeUTF8(sessionCheckpointsPath, data, { + tmpPath: sessionCheckpointsPath + ".tmp", + }); + + CrashMonitor.init(); + let checkpoints = await CrashMonitor.previousCheckpoints; + Assert.equal(checkpoints, null); +}); diff --git a/toolkit/components/crashmonitor/test/unit/test_missing_file.js b/toolkit/components/crashmonitor/test/unit/test_missing_file.js new file mode 100644 index 0000000000..a8b86fe6c1 --- /dev/null +++ b/toolkit/components/crashmonitor/test/unit/test_missing_file.js @@ -0,0 +1,13 @@ +/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at http://mozilla.org/MPL/2.0/. */ + +/** + * Test with non-existing sessionCheckpoints.json + */ +add_task(async function test_missing_file() { + CrashMonitor.init(); + let checkpoints = await CrashMonitor.previousCheckpoints; + Assert.equal(checkpoints, null); +}); diff --git a/toolkit/components/crashmonitor/test/unit/test_register.js b/toolkit/components/crashmonitor/test/unit/test_register.js new file mode 100644 index 0000000000..1957af4ee1 --- /dev/null +++ b/toolkit/components/crashmonitor/test/unit/test_register.js @@ -0,0 +1,25 @@ +/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at http://mozilla.org/MPL/2.0/. */ + +/** + * Test that CrashMonitor.jsm is correctly loaded from XPCOM component + */ +add_task(function test_register() { + let cm = Cc["@mozilla.org/toolkit/crashmonitor;1"].createInstance( + Ci.nsIObserver + ); + + // Send "profile-after-change" to trigger the initialization + cm.observe(null, "profile-after-change", null); + + // If CrashMonitor was initialized properly a new call to |init| + // should fail + try { + CrashMonitor.init(); + Assert.ok(false); + } catch (ex) { + Assert.ok(true); + } +}); diff --git a/toolkit/components/crashmonitor/test/unit/test_valid_file.js b/toolkit/components/crashmonitor/test/unit/test_valid_file.js new file mode 100644 index 0000000000..2a15d25a08 --- /dev/null +++ b/toolkit/components/crashmonitor/test/unit/test_valid_file.js @@ -0,0 +1,24 @@ +/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at http://mozilla.org/MPL/2.0/. */ + +/** + * Test with sessionCheckpoints.json containing valid data + */ +add_task(async function test_valid_file() { + // Write valid data to checkpoint file + await IOUtils.writeJSON( + sessionCheckpointsPath, + { "final-ui-startup": true }, + { + tmpPath: sessionCheckpointsPath + ".tmp", + } + ); + + CrashMonitor.init(); + let checkpoints = await CrashMonitor.previousCheckpoints; + + Assert.ok(checkpoints["final-ui-startup"]); + Assert.equal(Object.keys(checkpoints).length, 1); +}); diff --git a/toolkit/components/crashmonitor/test/unit/xpcshell.ini b/toolkit/components/crashmonitor/test/unit/xpcshell.ini new file mode 100644 index 0000000000..263fd13e55 --- /dev/null +++ b/toolkit/components/crashmonitor/test/unit/xpcshell.ini @@ -0,0 +1,10 @@ +[DEFAULT] +head = head.js +skip-if = toolkit == 'android' + +[test_init.js] +[test_valid_file.js] +[test_invalid_file.js] +[test_invalid_json.js] +[test_missing_file.js] +[test_register.js] -- cgit v1.2.3