summaryrefslogtreecommitdiffstats
path: root/toolkit/components/printing/tests/browser_print_stream.js
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 19:33:14 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 19:33:14 +0000
commit36d22d82aa202bb199967e9512281e9a53db42c9 (patch)
tree105e8c98ddea1c1e4784a60a5a6410fa416be2de /toolkit/components/printing/tests/browser_print_stream.js
parentInitial commit. (diff)
downloadfirefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.tar.xz
firefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.zip
Adding upstream version 115.7.0esr.upstream/115.7.0esr
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'toolkit/components/printing/tests/browser_print_stream.js')
-rw-r--r--toolkit/components/printing/tests/browser_print_stream.js102
1 files changed, 102 insertions, 0 deletions
diff --git a/toolkit/components/printing/tests/browser_print_stream.js b/toolkit/components/printing/tests/browser_print_stream.js
new file mode 100644
index 0000000000..dff1ee8762
--- /dev/null
+++ b/toolkit/components/printing/tests/browser_print_stream.js
@@ -0,0 +1,102 @@
+//creativecommons.org/publicdomain/zero/1.0/ */
+
+"use strict";
+
+const PSSVC = Cc["@mozilla.org/gfx/printsettings-service;1"].getService(
+ Ci.nsIPrintSettingsService
+);
+
+async function printToDestination(aBrowser, aDestination) {
+ let tmpDir = Services.dirsvc.get("TmpD", Ci.nsIFile);
+ let fileName = `printDestinationTest-${aDestination}.pdf`;
+ let filePath = PathUtils.join(tmpDir.path, fileName);
+
+ info(`Printing to ${filePath}`);
+
+ let settings = PSSVC.createNewPrintSettings();
+ settings.outputFormat = Ci.nsIPrintSettings.kOutputFormatPDF;
+ settings.outputDestination = aDestination;
+
+ settings.headerStrCenter = "";
+ settings.headerStrLeft = "";
+ settings.headerStrRight = "";
+ settings.footerStrCenter = "";
+ settings.footerStrLeft = "";
+ settings.footerStrRight = "";
+
+ settings.unwriteableMarginTop = 1; /* Just to ensure settings are respected on both */
+ let outStream = null;
+ if (aDestination == Ci.nsIPrintSettings.kOutputDestinationFile) {
+ settings.toFileName = PathUtils.join(tmpDir.path, fileName);
+ } else {
+ is(aDestination, Ci.nsIPrintSettings.kOutputDestinationStream);
+ outStream = Cc["@mozilla.org/network/file-output-stream;1"].createInstance(
+ Ci.nsIFileOutputStream
+ );
+ let tmpFile = tmpDir.clone();
+ tmpFile.append(fileName);
+ outStream.init(tmpFile, -1, 0o666, 0);
+ settings.outputStream = outStream;
+ }
+
+ await aBrowser.browsingContext.print(settings);
+
+ return filePath;
+}
+
+add_task(async function testPrintToStream() {
+ await PrintHelper.withTestPage(async helper => {
+ let filePath = await printToDestination(
+ helper.sourceBrowser,
+ Ci.nsIPrintSettings.kOutputDestinationFile
+ );
+ let streamPath = await printToDestination(
+ helper.sourceBrowser,
+ Ci.nsIPrintSettings.kOutputDestinationStream
+ );
+
+ // In Cocoa the CGContext adds a hash, plus there are other minor
+ // non-user-visible differences, so we need to be a bit more sloppy there.
+ //
+ // We see one byte difference in Windows and Linux on automation sometimes,
+ // though files are consistently the same locally, that needs
+ // investigation, but it's probably harmless.
+ const maxSizeDifference = AppConstants.platform == "macosx" ? 100 : 2;
+
+ // Buffering shenanigans? Wait for sizes to match... There's no great
+ // IOUtils methods to force a flush without writing anything...
+ await TestUtils.waitForCondition(async function () {
+ let fileStat = await IOUtils.stat(filePath);
+ let streamStat = await IOUtils.stat(streamPath);
+
+ ok(fileStat.size > 0, "File file should not be empty: " + fileStat.size);
+ ok(
+ streamStat.size > 0,
+ "Stream file should not be empty: " + streamStat.size
+ );
+ return Math.abs(fileStat.size - streamStat.size) <= maxSizeDifference;
+ }, "Sizes should (almost) match");
+
+ if (false) {
+ // This doesn't work reliably on automation, but works locally, see
+ // above...
+ let fileData = await IOUtils.read(filePath);
+ let streamData = await IOUtils.read(streamPath);
+ ok(!!fileData.length, "File should not be empty");
+ is(fileData.length, streamData.length, "File size should be equal");
+ for (let i = 0; i < fileData.length; ++i) {
+ if (fileData[i] != streamData[i]) {
+ is(
+ fileData[i],
+ streamData[i],
+ `Files should be equal (byte ${i} different)`
+ );
+ break;
+ }
+ }
+ }
+
+ await IOUtils.remove(filePath);
+ await IOUtils.remove(streamPath);
+ });
+});