Skip to content

Commit

Permalink
[marionette] Implement pageRanges support.
Browse files Browse the repository at this point in the history
This allows printing only specified page ranges.

We take them in the form ["1-2", 4, "5-"] and convert to a flat array
of the form [1,2,4,4,5,2147483647], which is appropriate for
nsIPrintSettings.

Testing relies on the fact that wpt vendored pdf.js for print
reftests; using the same mechaism we can ensure the correct pages were
printed.

Differential Revision: https://phabricator.services.mozilla.com/D97600

bugzilla-url: https://bugzilla.mozilla.org/show_bug.cgi?id=1678347
gecko-commit: b709a9d0378a70877df4a17def06ae792ee2dcf2
gecko-reviewers: webdriver-reviewers, whimboo
  • Loading branch information
jgraham authored and moz-wptsync-bot committed Nov 8, 2021
1 parent d55ae32 commit 399ddc8
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 2 deletions.
21 changes: 21 additions & 0 deletions webdriver/tests/print/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
def load_pdf_document(session, inline, pdf_data):
"""Load a PDF document in the browser using pdf.js"""
session.url = inline("""
<!doctype html>
<script src="/_pdf_js/pdf.js"></script>
<canvas></canvas>
<script>
async function getText() {
pages = [];
let loadingTask = pdfjsLib.getDocument({data: atob("%s")});
let pdf = await loadingTask.promise;
for (let pageNumber=1; pageNumber<=pdf.numPages; pageNumber++) {
let page = await pdf.getPage(pageNumber);
textContent = await page.getTextContent()
text = textContent.items.map(x => x.str).join("");
pages.push(text);
}
return pages
}
</script>
""" % pdf_data)
55 changes: 53 additions & 2 deletions webdriver/tests/print/printcmd.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# META: timeout=long
from base64 import decodebytes

import pytest

from . import load_pdf_document
from tests.support.asserts import assert_error, assert_success


Expand Down Expand Up @@ -41,6 +43,7 @@ def test_html_document(session, inline):
# TODO: Test that the output is reasonable
assert_pdf(pdf)


def test_large_html_document(session, inline):
session.url = inline("<canvas id=\"image\"></canvas>")

Expand Down Expand Up @@ -77,6 +80,48 @@ def test_large_html_document(session, inline):
assert_pdf(pdf)


@pytest.mark.parametrize("ranges,expected", [
(["2-4"], ["Page 2", "Page 3", "Page 4"]),
(["2-4", "2-3"], ["Page 2", "Page 3", "Page 4"]),
(["2-4", "3-5"], ["Page 2", "Page 3", "Page 4", "Page 5"]),
(["9-"], ["Page 9", "Page 10"]),
(["-2"], ["Page 1", "Page 2"]),
(["7"], ["Page 7"]),
(["-2", "9-", "7"], ["Page 1", "Page 2", "Page 7", "Page 9", "Page 10"]),
(["-5", "2-"], ["Page 1", "Page 2", "Page 3", "Page 4", "Page 5", "Page 6", "Page 7", "Page 8", "Page 9", "Page 10"]),
([], ["Page 1", "Page 2", "Page 3", "Page 4", "Page 5", "Page 6", "Page 7", "Page 8", "Page 9", "Page 10"]),
])
def test_page_ranges_document(session, inline, ranges, expected):
session.url = inline("""
<style>
div {page-break-after: always}
</style>
<div>Page 1</div>
<div>Page 2</div>
<div>Page 3</div>
<div>Page 4</div>
<div>Page 5</div>
<div>Page 6</div>
<div>Page 7</div>
<div>Page 8</div>
<div>Page 9</div>
<div>Page 10</div>""")

response = do_print(session, {
"pageRanges": ranges
})
value = assert_success(response)
pdf = decodebytes(value.encode())
# TODO: Test that the output is reasonable
assert_pdf(pdf)

load_pdf_document(session, inline, value)
pages = session.execute_async_script("""let callback = arguments[arguments.length - 1];
window.getText().then(pages => callback(pages));""")
assert pages == expected


@pytest.mark.parametrize("options", [{"orientation": 0},
{"orientation": "foo"},
{"scale": "1"},
Expand All @@ -85,7 +130,13 @@ def test_large_html_document(session, inline):
{"margin": {"top": "1"}},
{"margin": {"bottom": -1}},
{"page": {"height": False}},
{"shrinkToFit": "false"}])
def test_invalid(session, options):
{"shrinkToFit": "false"},
{"pageRanges": ["3-2"]},
{"pageRanges": ["a-2"]},
{"pageRanges": ["1:2"]},
{"pageRanges": ["1-2-3"]},
{"pageRanges": [None]},
{"pageRanges": ["1-2", {}]}])
def test_page_ranges_invalid(session, options):
response = do_print(session, options)
assert_error(response, "invalid argument")

0 comments on commit 399ddc8

Please sign in to comment.