find_spec.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /* Copyright 2021 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. const { closePages, loadAndWait } = require("./test_utils.js");
  16. function fuzzyMatch(a, b, browserName, pixelFuzz = 3) {
  17. expect(a)
  18. .withContext(`In ${browserName}`)
  19. .toBeLessThan(b + pixelFuzz);
  20. expect(a)
  21. .withContext(`In ${browserName}`)
  22. .toBeGreaterThan(b - pixelFuzz);
  23. }
  24. describe("find bar", () => {
  25. describe("highlight all", () => {
  26. let pages;
  27. beforeAll(async () => {
  28. pages = await loadAndWait("find_all.pdf#zoom=100", ".textLayer");
  29. });
  30. afterAll(async () => {
  31. await closePages(pages);
  32. });
  33. it("must highlight text in the right position", async () => {
  34. await Promise.all(
  35. pages.map(async ([browserName, page]) => {
  36. await page.click("#viewFind");
  37. await page.waitForSelector("#viewFind", { hidden: false });
  38. await page.type("#findInput", "a");
  39. await page.click("#findHighlightAll");
  40. await page.waitForSelector(".textLayer .highlight");
  41. // The PDF has the text "AB BA" in a monospace font.
  42. // Make sure we have the right number of highlighted divs.
  43. const highlights = await page.$$(".textLayer .highlight");
  44. expect(highlights.length).withContext(`In ${browserName}`).toEqual(2);
  45. const glyphWidth = 15.98; // From the PDF.
  46. const pageDiv = await page.$(".page canvas");
  47. const pageBox = await pageDiv.boundingBox();
  48. const firstA = await highlights[0].boundingBox();
  49. const secondA = await highlights[1].boundingBox();
  50. // Subtract the page offset from the text bounding boxes;
  51. firstA.x -= pageBox.x;
  52. firstA.y -= pageBox.y;
  53. secondA.x -= pageBox.x;
  54. secondA.y -= pageBox.y;
  55. // They should be on the same line.
  56. expect(firstA.y).withContext(`In ${browserName}`).toEqual(secondA.y);
  57. const fontSize = 26.66; // From the PDF.
  58. // The highlighted text has more padding.
  59. fuzzyMatch(firstA.height, fontSize + 5, browserName);
  60. fuzzyMatch(secondA.height, fontSize + 5, browserName);
  61. const expectedFirstAX = 28;
  62. fuzzyMatch(firstA.x, expectedFirstAX, browserName);
  63. // The second 'A' should be 4 glyphs widths from the first.
  64. fuzzyMatch(secondA.x, expectedFirstAX + glyphWidth * 4, browserName);
  65. })
  66. );
  67. });
  68. });
  69. describe("highlight all", () => {
  70. let pages;
  71. beforeAll(async () => {
  72. pages = await loadAndWait("xfa_imm5257e.pdf#zoom=100", ".xfaLayer");
  73. });
  74. afterAll(async () => {
  75. await closePages(pages);
  76. });
  77. it("must search xfa correctly", async () => {
  78. await Promise.all(
  79. pages.map(async ([browserName, page]) => {
  80. await page.click("#viewFind");
  81. await page.waitForSelector("#viewFind", { hidden: false });
  82. await page.type("#findInput", "city");
  83. await page.waitForSelector("#findInput[data-status='']");
  84. await page.waitForSelector(".xfaLayer .highlight");
  85. const resultElement = await page.waitForSelector("#findResultsCount");
  86. const resultText = await resultElement.evaluate(el => el.textContent);
  87. expect(resultText).toEqual("1 of 7 matches");
  88. const selectedElement = await page.waitForSelector(
  89. ".highlight.selected"
  90. );
  91. const selectedText = await selectedElement.evaluate(
  92. el => el.textContent
  93. );
  94. expect(selectedText).toEqual("City");
  95. })
  96. );
  97. });
  98. });
  99. });