From 086c044dc34dfc0f74fbe41f4ecb402b2cd34884 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Fri, 19 Apr 2024 03:13:33 +0200 Subject: Merging upstream version 125.0.1. Signed-off-by: Daniel Baumann --- toolkit/components/pdfjs/test/browser.toml | 2 + .../test/browser_pdfjs_caret_browsing_mode.js | 82 ++++++++++++ .../pdfjs/test/browser_pdfjs_download_button.js | 2 +- .../test/browser_pdfjs_editing_contextmenu.js | 140 ++++++++++++++------- .../pdfjs/test/browser_pdfjs_nonpdf_filename.js | 2 +- .../pdfjs/test/browser_pdfjs_octet_stream.js | 2 +- .../components/pdfjs/test/browser_pdfjs_saveas.js | 2 +- .../pdfjs/test/browser_pdfjs_stamp_telemetry.js | 2 +- toolkit/components/pdfjs/test/head.js | 22 ++++ 9 files changed, 206 insertions(+), 50 deletions(-) create mode 100644 toolkit/components/pdfjs/test/browser_pdfjs_caret_browsing_mode.js (limited to 'toolkit/components/pdfjs/test') diff --git a/toolkit/components/pdfjs/test/browser.toml b/toolkit/components/pdfjs/test/browser.toml index 4d045fd33e..b60d554c24 100644 --- a/toolkit/components/pdfjs/test/browser.toml +++ b/toolkit/components/pdfjs/test/browser.toml @@ -4,6 +4,8 @@ support-files = [ "head.js", ] +["browser_pdfjs_caret_browsing_mode.js"] + ["browser_pdfjs_download_button.js"] ["browser_pdfjs_editing_contextmenu.js"] diff --git a/toolkit/components/pdfjs/test/browser_pdfjs_caret_browsing_mode.js b/toolkit/components/pdfjs/test/browser_pdfjs_caret_browsing_mode.js new file mode 100644 index 0000000000..a7b2a518d9 --- /dev/null +++ b/toolkit/components/pdfjs/test/browser_pdfjs_caret_browsing_mode.js @@ -0,0 +1,82 @@ +/* Any copyright is dedicated to the Public Domain. + * http://creativecommons.org/publicdomain/zero/1.0/ */ + +const RELATIVE_DIR = "toolkit/components/pdfjs/test/"; +const TESTROOT = "https://example.com/browser/" + RELATIVE_DIR; +const pdfUrl = TESTROOT + "file_pdfjs_test.pdf"; +const caretBrowsingModePref = "accessibility.browsewithcaret"; + +// Test telemetry. +add_task(async function test() { + let mimeService = Cc["@mozilla.org/mime;1"].getService(Ci.nsIMIMEService); + let handlerInfo = mimeService.getFromTypeAndExtension( + "application/pdf", + "pdf" + ); + + // Make sure pdf.js is the default handler. + is( + handlerInfo.alwaysAskBeforeHandling, + false, + "pdf handler defaults to always-ask is false" + ); + is( + handlerInfo.preferredAction, + Ci.nsIHandlerInfo.handleInternally, + "pdf handler defaults to internal" + ); + + info("Pref action: " + handlerInfo.preferredAction); + + await BrowserTestUtils.withNewTab( + { gBrowser, url: "about:blank" }, + async function test_caret_browsing_mode(browser) { + await waitForPdfJS(browser, pdfUrl); + + let promise = BrowserTestUtils.waitForContentEvent( + browser, + "updatedPreference", + false, + null, + true + ); + await SpecialPowers.pushPrefEnv({ + set: [[caretBrowsingModePref, true]], + }); + await promise; + await TestUtils.waitForTick(); + + await SpecialPowers.spawn(browser, [], async function () { + const viewer = content.wrappedJSObject.PDFViewerApplication; + Assert.ok( + viewer.supportsCaretBrowsingMode, + "Caret browsing mode is supported" + ); + }); + + promise = BrowserTestUtils.waitForContentEvent( + browser, + "updatedPreference", + false, + null, + true + ); + await SpecialPowers.popPrefEnv(); + await promise; + await TestUtils.waitForTick(); + + await SpecialPowers.spawn(browser, [], async function () { + const viewer = content.wrappedJSObject.PDFViewerApplication; + Assert.ok( + !viewer.supportsCaretBrowsingMode, + "Caret browsing mode isn't supported" + ); + }); + + await SpecialPowers.spawn(browser, [], async function () { + const viewer = content.wrappedJSObject.PDFViewerApplication; + await viewer.close(); + }); + } + ); +}); diff --git a/toolkit/components/pdfjs/test/browser_pdfjs_download_button.js b/toolkit/components/pdfjs/test/browser_pdfjs_download_button.js index 1e3c00620c..1470d80f0e 100644 --- a/toolkit/components/pdfjs/test/browser_pdfjs_download_button.js +++ b/toolkit/components/pdfjs/test/browser_pdfjs_download_button.js @@ -7,7 +7,7 @@ const RELATIVE_DIR = "toolkit/components/pdfjs/test/"; const TESTROOT = "https://example.com/browser/" + RELATIVE_DIR; var MockFilePicker = SpecialPowers.MockFilePicker; -MockFilePicker.init(window); +MockFilePicker.init(window.browsingContext); MockFilePicker.returnValue = MockFilePicker.returnOK; var tempDir; diff --git a/toolkit/components/pdfjs/test/browser_pdfjs_editing_contextmenu.js b/toolkit/components/pdfjs/test/browser_pdfjs_editing_contextmenu.js index d857bb6aac..779a3a6ad4 100644 --- a/toolkit/components/pdfjs/test/browser_pdfjs_editing_contextmenu.js +++ b/toolkit/components/pdfjs/test/browser_pdfjs_editing_contextmenu.js @@ -32,9 +32,9 @@ async function openContextMenuAt(browser, x, y) { * Open a context menu and get the pdfjs entries * @param {Object} browser * @param {Object} box - * @returns {Map} the pdfjs menu entries. + * @returns {Promise>} the pdfjs menu entries. */ -async function getContextMenuItems(browser, box) { +function getContextMenuItems(browser, box) { return new Promise(resolve => { setTimeout(async () => { const { x, y, width, height } = box; @@ -48,6 +48,7 @@ async function getContextMenuItems(browser, box) { "context-pdfjs-delete", "context-pdfjs-selectall", "context-sep-pdfjs-selectall", + "context-pdfjs-highlight-selection", ]; await openContextMenuAt(browser, x + width / 2, y + height / 2); @@ -68,7 +69,7 @@ async function getContextMenuItems(browser, box) { * and returs the pdfjs menu entries. * @param {Object} browser * @param {string} selector - * @returns {Map} the pdfjs menu entries. + * @returns {Promise>} the pdfjs menu entries. */ async function getContextMenuItemsOn(browser, selector) { const box = await SpecialPowers.spawn( @@ -129,35 +130,36 @@ function assertMenuitems(menuitems, expected) { elmt => !elmt.id.includes("-sep-") && !elmt.hidden && - elmt.getAttribute("disabled") === "false" + ["", "false"].includes(elmt.getAttribute("disabled")) ) .map(elmt => elmt.id), expected ); } -// Text copy, paste, undo, redo, delete and select all in using the context -// menu. -add_task(async function test() { - let mimeService = Cc["@mozilla.org/mime;1"].getService(Ci.nsIMIMEService); - let handlerInfo = mimeService.getFromTypeAndExtension( - "application/pdf", - "pdf" - ); - - // Make sure pdf.js is the default handler. - is( - handlerInfo.alwaysAskBeforeHandling, - false, - "pdf handler defaults to always-ask is false" - ); - is( - handlerInfo.preferredAction, - Ci.nsIHandlerInfo.handleInternally, - "pdf handler defaults to internal" +async function waitAndCheckEmptyContextMenu(browser) { + // check that PDF is opened with internal viewer + await waitForPdfJSAllLayers(browser, TESTROOT + "file_pdfjs_test.pdf", [ + ["annotationEditorLayer", "annotationLayer", "textLayer", "canvasWrapper"], + ["annotationEditorLayer", "textLayer", "canvasWrapper"], + ]); + + const spanBox = await getSpanBox(browser, "and found references"); + const menuitems = await getContextMenuItems(browser, spanBox); + + // Nothing have been edited, hence the context menu doesn't contain any + // pdf entries. + Assert.ok( + [...menuitems.values()].every(elmt => elmt.hidden), + "No visible pdf menuitem" ); + await hideContextMenu(browser); +} - info("Pref action: " + handlerInfo.preferredAction); +// Text copy, paste, undo, redo, delete and select all in using the context +// menu. +add_task(async function test_copy_paste_undo_redo() { + makePDFJSHandler(); await BrowserTestUtils.withNewTab( { gBrowser, url: "about:blank" }, @@ -168,27 +170,8 @@ add_task(async function test() { set: [["pdfjs.annotationEditorMode", 0]], }); - // check that PDF is opened with internal viewer - await waitForPdfJSAllLayers(browser, TESTROOT + "file_pdfjs_test.pdf", [ - [ - "annotationEditorLayer", - "annotationLayer", - "textLayer", - "canvasWrapper", - ], - ["annotationEditorLayer", "textLayer", "canvasWrapper"], - ]); - + await waitAndCheckEmptyContextMenu(browser); const spanBox = await getSpanBox(browser, "and found references"); - let menuitems = await getContextMenuItems(browser, spanBox); - - // Nothing have been edited, hence the context menu doesn't contain any - // pdf entries. - Assert.ok( - [...menuitems.values()].every(elmt => elmt.hidden), - "No visible pdf menuitem" - ); - await hideContextMenu(browser); await enableEditor(browser, "FreeText"); await addFreeText(browser, "hello", spanBox); @@ -211,7 +194,7 @@ add_task(async function test() { Assert.equal(await countElements(browser, ".selectedEditor"), 0); - menuitems = await getContextMenuItems(browser, spanBox); + let menuitems = await getContextMenuItems(browser, spanBox); assertMenuitems(menuitems, [ "context-pdfjs-undo", // Last created editor is undoable "context-pdfjs-selectall", // and selectable. @@ -374,6 +357,73 @@ add_task(async function test() { await SpecialPowers.spawn(browser, [], async function () { var viewer = content.wrappedJSObject.PDFViewerApplication; + viewer.pdfDocument.annotationStorage.resetModified(); + await viewer.close(); + }); + } + ); +}); + +add_task(async function test_highlight_selection() { + makePDFJSHandler(); + + await BrowserTestUtils.withNewTab( + { gBrowser, url: "about:blank" }, + async function (browser) { + await SpecialPowers.pushPrefEnv({ + set: [ + ["pdfjs.annotationEditorMode", 0], + ["pdfjs.enableHighlightEditor", true], + ], + }); + + await waitAndCheckEmptyContextMenu(browser); + const spanBox = await getSpanBox(browser, "and found references"); + + const changePromise = BrowserTestUtils.waitForContentEvent( + browser, + "annotationeditorstateschanged", + false, + null, + true + ); + await clickAt( + browser, + spanBox.x + spanBox.width / 2, + spanBox.y + spanBox.height / 2, + 2 + ); + await changePromise; + await TestUtils.waitForTick(); + + const mozBox = await getSpanBox(browser, "Mozilla automated testing"); + const menuitems = await getContextMenuItems(browser, mozBox); + + assertMenuitems(menuitems, ["context-pdfjs-highlight-selection"]); + + const telemetryPromise = BrowserTestUtils.waitForContentEvent( + browser, + "reporttelemetry", + false, + null, + true + ); + await clickOnItem( + browser, + menuitems, + "context-pdfjs-highlight-selection" + ); + await telemetryPromise; + + Assert.equal( + await countElements(browser, ".highlightEditor"), + 1, + "An highlight editor must have been added" + ); + + await SpecialPowers.spawn(browser, [], async function () { + var viewer = content.wrappedJSObject.PDFViewerApplication; + viewer.pdfDocument.annotationStorage.resetModified(); await viewer.close(); }); } diff --git a/toolkit/components/pdfjs/test/browser_pdfjs_nonpdf_filename.js b/toolkit/components/pdfjs/test/browser_pdfjs_nonpdf_filename.js index ee08cd45d1..e1723bef6a 100644 --- a/toolkit/components/pdfjs/test/browser_pdfjs_nonpdf_filename.js +++ b/toolkit/components/pdfjs/test/browser_pdfjs_nonpdf_filename.js @@ -13,7 +13,7 @@ const LINK_PAGE_URL = TESTROOT + "file_pdf_download_link.html"; add_task(async function test_filename_nonpdf_extension() { var MockFilePicker = SpecialPowers.MockFilePicker; - MockFilePicker.init(window); + MockFilePicker.init(window.browsingContext); let filepickerNamePromise = new Promise(resolve => { MockFilePicker.showCallback = function (fp) { resolve(fp.defaultString); diff --git a/toolkit/components/pdfjs/test/browser_pdfjs_octet_stream.js b/toolkit/components/pdfjs/test/browser_pdfjs_octet_stream.js index d2b4fe310f..da630f726c 100644 --- a/toolkit/components/pdfjs/test/browser_pdfjs_octet_stream.js +++ b/toolkit/components/pdfjs/test/browser_pdfjs_octet_stream.js @@ -5,7 +5,7 @@ const TESTROOT = getRootDirectory(gTestPath).replace( "chrome://mochitests/content/", - "http://mochi.test:8888/" + "https://example.com/" ); // Get a ref to the pdf we want to open. diff --git a/toolkit/components/pdfjs/test/browser_pdfjs_saveas.js b/toolkit/components/pdfjs/test/browser_pdfjs_saveas.js index a1bfc18a91..dcb77c25fe 100644 --- a/toolkit/components/pdfjs/test/browser_pdfjs_saveas.js +++ b/toolkit/components/pdfjs/test/browser_pdfjs_saveas.js @@ -7,7 +7,7 @@ const RELATIVE_DIR = "toolkit/components/pdfjs/test/"; const TESTROOT = "http://example.com/browser/" + RELATIVE_DIR; var MockFilePicker = SpecialPowers.MockFilePicker; -MockFilePicker.init(window); +MockFilePicker.init(window.browsingContext); Services.scriptloader.loadSubScript( "chrome://mochitests/content/browser/toolkit/content/tests/browser/common/mockTransfer.js", diff --git a/toolkit/components/pdfjs/test/browser_pdfjs_stamp_telemetry.js b/toolkit/components/pdfjs/test/browser_pdfjs_stamp_telemetry.js index b8955f77e3..2ad0179cdc 100644 --- a/toolkit/components/pdfjs/test/browser_pdfjs_stamp_telemetry.js +++ b/toolkit/components/pdfjs/test/browser_pdfjs_stamp_telemetry.js @@ -10,7 +10,7 @@ Services.scriptloader.loadSubScript( ); const MockFilePicker = SpecialPowers.MockFilePicker; -MockFilePicker.init(window); +MockFilePicker.init(window.browsingContext); MockFilePicker.returnValue = MockFilePicker.returnOK; const file = new FileUtils.File(getTestFilePath("moz.png")); MockFilePicker.setFiles([file]); diff --git a/toolkit/components/pdfjs/test/head.js b/toolkit/components/pdfjs/test/head.js index 04c9543b5d..99e2bc7fb7 100644 --- a/toolkit/components/pdfjs/test/head.js +++ b/toolkit/components/pdfjs/test/head.js @@ -426,3 +426,25 @@ async function cleanupDownloads(listId = Downloads.PUBLIC) { await download.finalize(); } } + +function makePDFJSHandler() { + let mimeService = Cc["@mozilla.org/mime;1"].getService(Ci.nsIMIMEService); + let handlerInfo = mimeService.getFromTypeAndExtension( + "application/pdf", + "pdf" + ); + + // Make sure pdf.js is the default handler. + is( + handlerInfo.alwaysAskBeforeHandling, + false, + "pdf handler defaults to always-ask is false" + ); + is( + handlerInfo.preferredAction, + Ci.nsIHandlerInfo.handleInternally, + "pdf handler defaults to internal" + ); + + info("Pref action: " + handlerInfo.preferredAction); +} -- cgit v1.2.3