Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / LayoutTests / printing / resources / paged-media-test-utils.js
bloba2d5deb1f6d1483fd7b45e984b68695e5906c74a
1 // The buffer to store the results. We output the results after all
2 // tests finish. Note that we cannot have a DOM element where the
3 // results are stored in HTMLs because the DOM element to store
4 // results may change the number of pages.
5 var _results = '';
6 var _errored = false;
8 function appendResult(result)
10 _results += '<br>' + result;
13 function pageNumberForElementShouldBe(id, expectedPageNumber)
15 var actualPageNumber = internals.pageNumber(document.getElementById(id));
16 if (actualPageNumber == expectedPageNumber)
17 appendResult('PASS: page number of "' + id + '" is ' + actualPageNumber);
18 else {
19 appendResult('FAIL: expected page number of "' + id + '" is ' + expectedPageNumber + '. Was ' + actualPageNumber);
20 _errored = true;
24 function numberOfPagesShouldBe(expectedNumberOfPages, pageWidthInPixels, pageHeightInPixels)
26 // pageWidthInPixels and pageHeightInPixels can be omitted. If omitted, 800x600 is used.
27 var actualNumberOfPages;
28 if (pageWidthInPixels && pageHeightInPixels)
29 actualNumberOfPages = internals.numberOfPages(pageWidthInPixels, pageHeightInPixels);
30 else
31 actualNumberOfPages = internals.numberOfPages();
33 if (actualNumberOfPages == expectedNumberOfPages)
34 appendResult('PASS: number of pages is ' + actualNumberOfPages);
35 else {
36 appendResult('FAIL: expected number of pages is ' + expectedNumberOfPages + '. Was ' + actualNumberOfPages);
37 _errored = true;
41 function runPrintingTest(testFunction)
43 if (window.testRunner) {
44 try {
45 testFunction();
46 } catch (err) {
47 _results += '<p>Exception: ' + err.toString();
48 _errored = true;
51 if (!_errored)
52 _results += '<br>All tests passed';
53 } else {
54 _results += 'This test requires testRunner. You can test this manually with the above description.';
57 var resultElement = document.createElement('p');
58 resultElement.innerHTML = _results;
59 var output = document.getElementById("console") || document.body;
60 output.appendChild(resultElement);
63 function ratioToPageHeightToPixels(heightInRatioToPageHeight)
65 var pageHeightInPixels = 600 * 1.25;
66 return Math.floor(pageHeightInPixels * heightInRatioToPageHeight);
69 function createBlockWithRatioToPageHeight(id, heightInRatioToPageHeight)
71 var element = document.createElement("div");
72 element.id = id;
73 element.style.height = ratioToPageHeightToPixels(heightInRatioToPageHeight) + "px";
74 document.getElementById("sandbox").appendChild(element);
75 return element;
78 function createBlockWithNumberOfLines(id, childLines)
80 var element = document.createElement("div");
81 element.id = id;
82 for (var i = 0; i < childLines; ++i) {
83 element.appendChild(document.createTextNode("line" + i));
84 element.appendChild(document.createElement("br"));
86 // Make sure that one page has about 20 lines.
87 element.style.lineHeight = ratioToPageHeightToPixels(0.05) + "px";
88 document.getElementById("sandbox").appendChild(element);
89 return element;