Bug 1809422 - Android Unable to Print/Save PDF r=geckoview-reviewers,calu

The canonical browsing context is not available if the current window/tab
is not focused for printing or saving as a PDF.

This patch adds a call to `setFocused` before creating a Gecko PDF to
ensure the canonical browsing context can be used for generating a PDF
to save or print.

Differential Revision: https://phabricator.services.mozilla.com/D185311
This commit is contained in:
Olivia Hall 2023-08-07 14:36:06 +00:00
parent d649d941ce
commit c562764160
2 changed files with 23 additions and 0 deletions

View File

@ -322,4 +322,25 @@ class PrintDelegateTest : BaseSessionTest() {
) )
} }
} }
@NullDelegate(Autofill.Delegate::class)
@Test
fun availableCanonicalBrowsingContext() {
sessionRule.setPrefsUntilTestEnd(mapOf("dom.enable_window_print" to true))
activityRule.scenario.onActivity { activity ->
// CSS rules render this blue on screen and orange on print
mainSession.loadTestPath(ORANGE_PDF_PATH)
mainSession.waitForPageStop()
// Setting to the default delegate (test rules changed it)
mainSession.printDelegate = activity.view.printDelegate
mainSession.setFocused(false)
mainSession.printPageContent()
val centerPixel = printCenterPixelColor()
val orange = rgb(255, 113, 57)
assertTrue(
"Android print opened and rendered.",
sessionRule.waitForResult(centerPixel) == orange,
)
}
}
} }

View File

@ -7542,6 +7542,8 @@ public class GeckoSession {
final @Nullable Long browsingContextId) { final @Nullable Long browsingContextId) {
final GeckoResult<InputStream> geckoResult = new GeckoResult<>(); final GeckoResult<InputStream> geckoResult = new GeckoResult<>();
if (browsingContextId == null) { if (browsingContextId == null) {
// Ensures the canonical browsing context is available
setFocused(true);
this.mWindow.printToPdf(geckoResult); this.mWindow.printToPdf(geckoResult);
} else { } else {
this.mWindow.printToPdf(geckoResult, browsingContextId); this.mWindow.printToPdf(geckoResult, browsingContextId);