UNPKG

3.87 kBJavaScriptView Raw
1/**
2 * @licstart The following is the entire license notice for the
3 * Javascript code in this page
4 *
5 * Copyright 2017 Mozilla Foundation
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *
19 * @licend The above is the entire license notice for the
20 * Javascript code in this page
21 */
22'use strict';
23
24var _test_utils = require('./test_utils');
25
26var _dom_utils = require('../../display/dom_utils');
27
28var _api = require('../../display/api');
29
30var _is_node = require('../../shared/is_node');
31
32var _is_node2 = _interopRequireDefault(_is_node);
33
34function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
35
36function getTopLeftPixel(canvasContext) {
37 var imgData = canvasContext.getImageData(0, 0, 1, 1);
38 return {
39 r: imgData.data[0],
40 g: imgData.data[1],
41 b: imgData.data[2],
42 a: imgData.data[3]
43 };
44}
45describe('custom canvas rendering', function () {
46 var transparentGetDocumentParams = (0, _test_utils.buildGetDocumentParams)('transparent.pdf');
47 var CanvasFactory = void 0;
48 var loadingTask = void 0;
49 var page = void 0;
50 beforeAll(function (done) {
51 if ((0, _is_node2.default)()) {} else {
52 CanvasFactory = new _dom_utils.DOMCanvasFactory();
53 }
54 loadingTask = (0, _api.getDocument)(transparentGetDocumentParams);
55 loadingTask.promise.then(function (doc) {
56 return doc.getPage(1);
57 }).then(function (data) {
58 page = data;
59 done();
60 }).catch(function (reason) {
61 done.fail(reason);
62 });
63 });
64 afterAll(function (done) {
65 CanvasFactory = null;
66 page = null;
67 loadingTask.destroy().then(done);
68 });
69 it('renders to canvas with a default white background', function (done) {
70 if ((0, _is_node2.default)()) {
71 pending('TODO: Support Canvas testing in Node.js.');
72 }
73 var viewport = page.getViewport(1);
74 var canvasAndCtx = CanvasFactory.create(viewport.width, viewport.height);
75 page.render({
76 canvasContext: canvasAndCtx.context,
77 viewport: viewport
78 }).then(function () {
79 var _getTopLeftPixel = getTopLeftPixel(canvasAndCtx.context),
80 r = _getTopLeftPixel.r,
81 g = _getTopLeftPixel.g,
82 b = _getTopLeftPixel.b,
83 a = _getTopLeftPixel.a;
84
85 CanvasFactory.destroy(canvasAndCtx);
86 expect(r).toEqual(255);
87 expect(g).toEqual(255);
88 expect(b).toEqual(255);
89 expect(a).toEqual(255);
90 done();
91 }).catch(function (reason) {
92 done(reason);
93 });
94 });
95 it('renders to canvas with a custom background', function (done) {
96 if ((0, _is_node2.default)()) {
97 pending('TODO: Support Canvas testing in Node.js.');
98 }
99 var viewport = page.getViewport(1);
100 var canvasAndCtx = CanvasFactory.create(viewport.width, viewport.height);
101 page.render({
102 canvasContext: canvasAndCtx.context,
103 viewport: viewport,
104 background: 'rgba(255,0,0,1.0)'
105 }).then(function () {
106 var _getTopLeftPixel2 = getTopLeftPixel(canvasAndCtx.context),
107 r = _getTopLeftPixel2.r,
108 g = _getTopLeftPixel2.g,
109 b = _getTopLeftPixel2.b,
110 a = _getTopLeftPixel2.a;
111
112 CanvasFactory.destroy(canvasAndCtx);
113 expect(r).toEqual(255);
114 expect(g).toEqual(0);
115 expect(b).toEqual(0);
116 expect(a).toEqual(255);
117 done();
118 }).catch(function (reason) {
119 done(reason);
120 });
121 });
122});
\No newline at end of file