2
0

custom_spec.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. function getTopLeftPixel(canvasContext) {
  21. var imgData = canvasContext.getImageData(0, 0, 1, 1);
  22. return {
  23. r: imgData.data[0],
  24. g: imgData.data[1],
  25. b: imgData.data[2],
  26. a: imgData.data[3]
  27. };
  28. }
  29. describe('custom canvas rendering', function () {
  30. var transparentGetDocumentParams = (0, _test_utils.buildGetDocumentParams)('transparent.pdf');
  31. var CanvasFactory = void 0;
  32. var loadingTask = void 0;
  33. var page = void 0;
  34. beforeAll(function (done) {
  35. if ((0, _util.isNodeJS)()) {} else {
  36. CanvasFactory = new _dom_utils.DOMCanvasFactory();
  37. }
  38. loadingTask = (0, _api.getDocument)(transparentGetDocumentParams);
  39. loadingTask.promise.then(function (doc) {
  40. return doc.getPage(1);
  41. }).then(function (data) {
  42. page = data;
  43. done();
  44. }).catch(function (reason) {
  45. done.fail(reason);
  46. });
  47. });
  48. afterAll(function (done) {
  49. CanvasFactory = null;
  50. page = null;
  51. loadingTask.destroy().then(done);
  52. });
  53. it('renders to canvas with a default white background', function (done) {
  54. if ((0, _util.isNodeJS)()) {
  55. pending('TODO: Support Canvas testing in Node.js.');
  56. }
  57. var viewport = page.getViewport(1);
  58. var canvasAndCtx = CanvasFactory.create(viewport.width, viewport.height);
  59. page.render({
  60. canvasContext: canvasAndCtx.context,
  61. viewport: viewport
  62. }).then(function () {
  63. var _getTopLeftPixel = getTopLeftPixel(canvasAndCtx.context),
  64. r = _getTopLeftPixel.r,
  65. g = _getTopLeftPixel.g,
  66. b = _getTopLeftPixel.b,
  67. a = _getTopLeftPixel.a;
  68. CanvasFactory.destroy(canvasAndCtx);
  69. expect(r).toEqual(255);
  70. expect(g).toEqual(255);
  71. expect(b).toEqual(255);
  72. expect(a).toEqual(255);
  73. done();
  74. }).catch(function (reason) {
  75. done(reason);
  76. });
  77. });
  78. it('renders to canvas with a custom background', function (done) {
  79. if ((0, _util.isNodeJS)()) {
  80. pending('TODO: Support Canvas testing in Node.js.');
  81. }
  82. var viewport = page.getViewport(1);
  83. var canvasAndCtx = CanvasFactory.create(viewport.width, viewport.height);
  84. page.render({
  85. canvasContext: canvasAndCtx.context,
  86. viewport: viewport,
  87. background: 'rgba(255,0,0,1.0)'
  88. }).then(function () {
  89. var _getTopLeftPixel2 = getTopLeftPixel(canvasAndCtx.context),
  90. r = _getTopLeftPixel2.r,
  91. g = _getTopLeftPixel2.g,
  92. b = _getTopLeftPixel2.b,
  93. a = _getTopLeftPixel2.a;
  94. CanvasFactory.destroy(canvasAndCtx);
  95. expect(r).toEqual(255);
  96. expect(g).toEqual(0);
  97. expect(b).toEqual(0);
  98. expect(a).toEqual(255);
  99. done();
  100. }).catch(function (reason) {
  101. done(reason);
  102. });
  103. });
  104. });