2
0

custom_spec.js 3.8 KB

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