custom_spec.js 3.6 KB

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