2
0

custom_spec.js 3.5 KB

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