diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-19 01:47:29 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-19 01:47:29 +0000 |
commit | 0ebf5bdf043a27fd3dfb7f92e0cb63d88954c44d (patch) | |
tree | a31f07c9bcca9d56ce61e9a1ffd30ef350d513aa /tools/code-coverage/tests | |
parent | Initial commit. (diff) | |
download | firefox-esr-0ebf5bdf043a27fd3dfb7f92e0cb63d88954c44d.tar.xz firefox-esr-0ebf5bdf043a27fd3dfb7f92e0cb63d88954c44d.zip |
Adding upstream version 115.8.0esr.upstream/115.8.0esr
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'tools/code-coverage/tests')
7 files changed, 388 insertions, 0 deletions
diff --git a/tools/code-coverage/tests/mochitest/mochitest.ini b/tools/code-coverage/tests/mochitest/mochitest.ini new file mode 100644 index 0000000000..f2c8782e76 --- /dev/null +++ b/tools/code-coverage/tests/mochitest/mochitest.ini @@ -0,0 +1,3 @@ +[DEFAULT] + +[test_coverage_specialpowers.html] diff --git a/tools/code-coverage/tests/mochitest/test_coverage_specialpowers.html b/tools/code-coverage/tests/mochitest/test_coverage_specialpowers.html new file mode 100644 index 0000000000..301206ac48 --- /dev/null +++ b/tools/code-coverage/tests/mochitest/test_coverage_specialpowers.html @@ -0,0 +1,38 @@ +<!DOCTYPE HTML> +<html> +<!-- +https://bugzilla.mozilla.org/show_bug.cgi?id=1380659 +--> +<head> + <meta charset="utf-8"> + <title>Test for Bug 123456</title> + <script src="/tests/SimpleTest/SimpleTest.js"></script> + <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/> + <script type="application/javascript"> + + /** Test for Bug 1380659 **/ + + SimpleTest.waitForExplicitFinish(); + + (async function() { + await SpecialPowers.requestDumpCoverageCounters(); + SimpleTest.ok(true, "Counters dumped."); + + await SpecialPowers.requestResetCoverageCounters(); + SimpleTest.ok(true, "Counters reset."); + + SimpleTest.finish(); + })(); + + </script> +</head> +<body> +<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1380659">Mozilla Bug 1380659</a> +<p id="display"></p> +<div id="content" style="display: none"> + +</div> +<pre id="test"> +</pre> +</body> +</html> diff --git a/tools/code-coverage/tests/xpcshell/head.js b/tools/code-coverage/tests/xpcshell/head.js new file mode 100644 index 0000000000..3642c5794c --- /dev/null +++ b/tools/code-coverage/tests/xpcshell/head.js @@ -0,0 +1,100 @@ +/* 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 { AppConstants } = ChromeUtils.importESModule( + "resource://gre/modules/AppConstants.sys.mjs" +); + +function getFiles() { + // This is the directory where gcov is emitting the gcda files. + const jsCoveragePath = Services.env.get("JS_CODE_COVERAGE_OUTPUT_DIR"); + + const jsCoverageDir = Cc["@mozilla.org/file/local;1"].createInstance( + Ci.nsIFile + ); + jsCoverageDir.initWithPath(jsCoveragePath); + + let files = []; + + let entries = jsCoverageDir.directoryEntries; + while (entries.hasMoreElements()) { + files.push(entries.nextFile); + } + + return files; +} + +function diffFiles(files_after, files_before) { + let files_before_set = new Set(files_before.map(file => file.leafName)); + return files_after.filter(file => !files_before_set.has(file.leafName)); +} + +const BASENAME_RE = new RegExp("([^/\\\\]+)$"); + +function parseRecords(files) { + let records = new Map(); + + for (let file of files) { + const lines = Cu.readUTF8File(file).split("\n"); + let currentSF = null; + + for (let line of lines) { + let [recordType, ...recordContent] = line.split(":"); + recordContent = recordContent.join(":"); + + switch (recordType) { + case "FNDA": { + if (currentSF == null) { + throw new Error("SF missing"); + } + + let [hits, name] = recordContent.split(","); + currentSF.push({ + type: "FNDA", + hits, + name, + }); + break; + } + + case "FN": { + if (currentSF == null) { + throw new Error("SF missing"); + } + + let name = recordContent.split(",")[1]; + currentSF.push({ + type: "FN", + name, + }); + break; + } + + case "SF": { + if ( + recordContent.startsWith("resource:") || + recordContent.startsWith("chrome:") + ) { + recordContent = recordContent.split("/").at(-1); + } else { + if (AppConstants.platform == "win") { + recordContent = recordContent.replace(/\//g, "\\"); + } + const match = BASENAME_RE.exec(recordContent); + if (match.length) { + recordContent = match[0]; + } + } + + currentSF = []; + + records.set(recordContent, currentSF); + break; + } + } + } + } + + return records; +} diff --git a/tools/code-coverage/tests/xpcshell/support.js b/tools/code-coverage/tests/xpcshell/support.js new file mode 100644 index 0000000000..9189427111 --- /dev/null +++ b/tools/code-coverage/tests/xpcshell/support.js @@ -0,0 +1,5 @@ +function test_code_coverage_func2() { + return 22; +} + +test_code_coverage_func2(); diff --git a/tools/code-coverage/tests/xpcshell/test_basic.js b/tools/code-coverage/tests/xpcshell/test_basic.js new file mode 100644 index 0000000000..9523a37ca2 --- /dev/null +++ b/tools/code-coverage/tests/xpcshell/test_basic.js @@ -0,0 +1,115 @@ +/* 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/. */ + +function test_code_coverage_func1() { + return 22; +} + +function test_code_coverage_func2() { + return 22; +} + +async function run_test() { + do_test_pending(); + + Assert.ok("@mozilla.org/tools/code-coverage;1" in Cc); + + const codeCoverageCc = Cc["@mozilla.org/tools/code-coverage;1"]; + Assert.ok(!!codeCoverageCc); + + const codeCoverage = codeCoverageCc.getService(Ci.nsICodeCoverage); + Assert.ok(!!codeCoverage); + + const files_orig = getFiles(); + + test_code_coverage_func1(); + + // Flush counters for the first time, we should see this function executed, but test_code_coverage_func not executed. + await codeCoverage.flushCounters(); + + const first_flush_files = getFiles(); + const first_flush_records = parseRecords( + diffFiles(first_flush_files, files_orig) + ); + + Assert.ok(first_flush_records.has("test_basic.js")); + let fnRecords = first_flush_records + .get("test_basic.js") + .filter(record => record.type == "FN"); + let fndaRecords = first_flush_records + .get("test_basic.js") + .filter(record => record.type == "FNDA"); + Assert.ok(fnRecords.some(record => record.name == "top-level")); + Assert.ok(fnRecords.some(record => record.name == "run_test")); + Assert.ok( + fnRecords.some(record => record.name == "test_code_coverage_func1") + ); + Assert.ok( + fndaRecords.some(record => record.name == "run_test" && record.hits == 1) + ); + Assert.ok( + !fndaRecords.some(record => record.name == "run_test" && record.hits != 1) + ); + Assert.ok( + fndaRecords.some( + record => record.name == "test_code_coverage_func1" && record.hits == 1 + ) + ); + Assert.ok( + !fndaRecords.some( + record => record.name == "test_code_coverage_func1" && record.hits != 1 + ) + ); + Assert.ok( + !fndaRecords.some(record => record.name == "test_code_coverage_func2") + ); + + test_code_coverage_func2(); + + // Flush counters for the second time, we should see this function not executed, but test_code_coverage_func executed. + await codeCoverage.flushCounters(); + + const second_flush_files = getFiles(); + const second_flush_records = parseRecords( + diffFiles(second_flush_files, first_flush_files) + ); + + Assert.ok(second_flush_records.has("test_basic.js")); + fnRecords = second_flush_records + .get("test_basic.js") + .filter(record => record.type == "FN"); + fndaRecords = second_flush_records + .get("test_basic.js") + .filter(record => record.type == "FNDA"); + Assert.ok(fnRecords.some(record => record.name == "top-level")); + Assert.ok(fnRecords.some(record => record.name == "run_test")); + Assert.ok( + fnRecords.some(record => record.name == "test_code_coverage_func1") + ); + Assert.ok( + fnRecords.some(record => record.name == "test_code_coverage_func2") + ); + Assert.ok( + fndaRecords.some( + record => record.name == "test_code_coverage_func1" && record.hits == 0 + ) + ); + Assert.ok( + !fndaRecords.some( + record => record.name == "test_code_coverage_func1" && record.hits != 0 + ) + ); + Assert.ok( + fndaRecords.some( + record => record.name == "test_code_coverage_func2" && record.hits == 1 + ) + ); + Assert.ok( + !fndaRecords.some( + record => record.name == "test_code_coverage_func2" && record.hits != 1 + ) + ); + + do_test_finished(); +} diff --git a/tools/code-coverage/tests/xpcshell/test_basic_child_and_parent.js b/tools/code-coverage/tests/xpcshell/test_basic_child_and_parent.js new file mode 100644 index 0000000000..f074c20776 --- /dev/null +++ b/tools/code-coverage/tests/xpcshell/test_basic_child_and_parent.js @@ -0,0 +1,120 @@ +/* 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/. */ + +function test_code_coverage_func1() { + return 22; +} + +async function run_test() { + do_load_child_test_harness(); + do_test_pending(); + + const codeCoverage = Cc["@mozilla.org/tools/code-coverage;1"].getService( + Ci.nsICodeCoverage + ); + + const files_orig = getFiles(); + + test_code_coverage_func1(); + + await codeCoverage.flushCounters(); + + const first_flush_files = getFiles(); + const first_flush_records = parseRecords( + diffFiles(first_flush_files, files_orig) + ); + + Assert.ok(first_flush_records.has("test_basic_child_and_parent.js")); + Assert.ok(!first_flush_records.has("support.js")); + let fnRecords = first_flush_records + .get("test_basic_child_and_parent.js") + .filter(record => record.type == "FN"); + let fndaRecords = first_flush_records + .get("test_basic_child_and_parent.js") + .filter(record => record.type == "FNDA"); + Assert.ok(fnRecords.some(record => record.name == "top-level")); + Assert.ok(fnRecords.some(record => record.name == "run_test")); + Assert.ok( + fnRecords.some(record => record.name == "test_code_coverage_func1") + ); + Assert.ok( + fndaRecords.some(record => record.name == "run_test" && record.hits == 1) + ); + Assert.ok( + !fndaRecords.some(record => record.name == "run_test" && record.hits != 1) + ); + Assert.ok( + fndaRecords.some( + record => record.name == "test_code_coverage_func1" && record.hits == 1 + ) + ); + Assert.ok( + !fndaRecords.some( + record => record.name == "test_code_coverage_func1" && record.hits != 1 + ) + ); + + sendCommand("load('support.js');", async function () { + await codeCoverage.flushCounters(); + + const second_flush_files = getFiles(); + const second_flush_records = parseRecords( + diffFiles(second_flush_files, first_flush_files) + ); + + Assert.ok(second_flush_records.has("test_basic_child_and_parent.js")); + fnRecords = second_flush_records + .get("test_basic_child_and_parent.js") + .filter(record => record.type == "FN"); + fndaRecords = second_flush_records + .get("test_basic_child_and_parent.js") + .filter(record => record.type == "FNDA"); + Assert.ok(fnRecords.some(record => record.name == "top-level")); + Assert.ok(fnRecords.some(record => record.name == "run_test")); + Assert.ok( + fnRecords.some(record => record.name == "test_code_coverage_func1") + ); + Assert.ok( + fndaRecords.some( + record => record.name == "test_code_coverage_func1" && record.hits == 0 + ) + ); + Assert.ok( + !fndaRecords.some( + record => record.name == "test_code_coverage_func1" && record.hits != 0 + ) + ); + Assert.ok(second_flush_records.has("support.js")); + fnRecords = second_flush_records + .get("support.js") + .filter(record => record.type == "FN"); + fndaRecords = second_flush_records + .get("support.js") + .filter(record => record.type == "FNDA"); + Assert.ok(fnRecords.some(record => record.name == "top-level")); + Assert.ok( + fnRecords.some(record => record.name == "test_code_coverage_func2") + ); + Assert.ok( + fndaRecords.some(record => record.name == "top-level" && record.hits == 1) + ); + Assert.ok( + !fndaRecords.some( + record => record.name == "top-level" && record.hits != 1 + ) + ); + Assert.ok( + fndaRecords.some( + record => record.name == "test_code_coverage_func2" && record.hits == 1 + ) + ); + Assert.ok( + !fndaRecords.some( + record => record.name == "test_code_coverage_func2" && record.hits != 1 + ) + ); + + do_test_finished(); + }); +} diff --git a/tools/code-coverage/tests/xpcshell/xpcshell.ini b/tools/code-coverage/tests/xpcshell/xpcshell.ini new file mode 100644 index 0000000000..93f756be8b --- /dev/null +++ b/tools/code-coverage/tests/xpcshell/xpcshell.ini @@ -0,0 +1,7 @@ +[DEFAULT] +head = head.js +support-files = + support.js + +[test_basic.js] +[test_basic_child_and_parent.js] |