helper.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. import sinon from "sinon";
  2. import Dimensions from "dom/dimensions";
  3. import GraphicsBuilder from "dom/graphics_builder";
  4. import TextBuilder from "dom/text_builder";
  5. import TTYCell from "dom/tty_cell";
  6. import MockRange from "mocks/range";
  7. import TextNodes from "fixtures/text_nodes";
  8. import CanvasPixels from "fixtures/canvas_pixels";
  9. var sandbox = sinon.sandbox.create();
  10. let getPixelsStub;
  11. let channel = { name: 1 };
  12. beforeEach(() => {
  13. sandbox
  14. .stub(Dimensions.prototype, "_getOrCreateMeasuringBox")
  15. .returns(element);
  16. sandbox.stub(Dimensions.prototype, "sendMessage").returns(true);
  17. sandbox.stub(GraphicsBuilder.prototype, "hideText").returns(true);
  18. sandbox.stub(GraphicsBuilder.prototype, "showText").returns(true);
  19. sandbox.stub(GraphicsBuilder.prototype, "_scaleCanvas").returns(true);
  20. sandbox.stub(GraphicsBuilder.prototype, "_unScaleCanvas").returns(true);
  21. sandbox.stub(TextBuilder.prototype, "_getAllInputBoxes").returns([]);
  22. sandbox.stub(TTYCell.prototype, "isHighestLayer").returns(true);
  23. getPixelsStub = sandbox.stub(GraphicsBuilder.prototype, "_getPixelData");
  24. });
  25. afterEach(() => {
  26. sandbox.restore();
  27. });
  28. global.dimensions = {
  29. char: {
  30. width: 1,
  31. height: 2
  32. }
  33. };
  34. global.document = {
  35. addEventListener: () => {},
  36. body: {
  37. contains: () => {
  38. return true;
  39. }
  40. },
  41. getElementById: () => {},
  42. getElementsByTagName: () => {
  43. return [
  44. {
  45. innerHTML: "Google"
  46. }
  47. ];
  48. },
  49. createRange: () => {
  50. return new MockRange();
  51. },
  52. createElement: () => {
  53. return {
  54. getContext: () => {}
  55. };
  56. },
  57. documentElement: {
  58. scrollWidth: null,
  59. scrollHeight: null
  60. },
  61. location: {
  62. href: "https://www.google.com"
  63. },
  64. scrollX: 0,
  65. scrollY: 0,
  66. innerWidth: null,
  67. innerHeight: null
  68. };
  69. global.DEVELOPMENT = false;
  70. global.PRODUCTION = false;
  71. global.TEST = true;
  72. global.window = global.document;
  73. global.performance = {
  74. now: () => {}
  75. };
  76. let element = {
  77. getBoundingClientRect: () => {
  78. return {
  79. width: global.dimensions.char.width,
  80. height: global.dimensions.char.height
  81. };
  82. }
  83. };
  84. function _setupMockDOMSize() {
  85. const width = global.mock_DOM_template[0].length;
  86. const height = global.mock_DOM_template.length * 2;
  87. global.document.documentElement.scrollWidth = width;
  88. global.document.documentElement.scrollHeight = height;
  89. global.document.innerWidth = width;
  90. global.document.innerHeight = height;
  91. }
  92. function _setupDimensions() {
  93. let dimensions = new Dimensions();
  94. _setupMockDOMSize();
  95. dimensions.tty.width = global.tty.width;
  96. dimensions.tty.height = global.tty.height;
  97. dimensions.frame.x_scroll = global.tty.x_scroll;
  98. dimensions.frame.y_scroll = global.tty.y_scroll;
  99. dimensions.update();
  100. dimensions.setSubFrameDimensions(global.frame_type);
  101. return dimensions;
  102. }
  103. function _setupGraphicsBuilder(type) {
  104. let dimensions = _setupDimensions();
  105. let canvas_pixels = new CanvasPixels(dimensions);
  106. if (type === "with_text") {
  107. getPixelsStub.onCall(0).returns(canvas_pixels.with_text());
  108. getPixelsStub.onCall(1).returns(canvas_pixels.without_text());
  109. getPixelsStub.onCall(2).returns(canvas_pixels.scaled());
  110. } else {
  111. getPixelsStub.onCall(0).returns(canvas_pixels.scaled());
  112. }
  113. let config = {
  114. "http-server": {
  115. "jpeg-compression": 0.9,
  116. render_delay: 0
  117. }
  118. };
  119. let graphics_builder = new GraphicsBuilder(channel, dimensions, config);
  120. return graphics_builder;
  121. }
  122. let functions = {
  123. runTextBuilder: callback => {
  124. let text_nodes = new TextNodes();
  125. let graphics_builder = _setupGraphicsBuilder("with_text");
  126. let text_builder = new TextBuilder(
  127. channel,
  128. graphics_builder.dimensions,
  129. graphics_builder,
  130. {
  131. browsh: {
  132. use_experimental_text_visibility: true
  133. }
  134. }
  135. );
  136. graphics_builder._getScreenshotWithText(() => {
  137. graphics_builder._getScreenshotWithoutText();
  138. graphics_builder.__getScaledScreenshot();
  139. text_builder._text_nodes = text_nodes.build();
  140. text_builder._updateState();
  141. text_builder._positionTextNodes();
  142. callback(text_builder);
  143. });
  144. },
  145. runGraphicsBuilder: () => {
  146. let graphics_builder = _setupGraphicsBuilder();
  147. graphics_builder.__getScaledScreenshot();
  148. graphics_builder._serialiseFrame();
  149. return graphics_builder;
  150. }
  151. };
  152. export default functions;