pdf_viewer_spec.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. import { PDFPageViewBuffer } from "../../web/pdf_viewer.js";
  16. describe("PDFViewer", function () {
  17. describe("PDFPageViewBuffer", function () {
  18. function createViewsMap(startId, endId) {
  19. const map = new Map();
  20. for (let id = startId; id <= endId; id++) {
  21. map.set(id, {
  22. id,
  23. destroy: () => {},
  24. });
  25. }
  26. return map;
  27. }
  28. it("handles `push` correctly", function () {
  29. const buffer = new PDFPageViewBuffer(3);
  30. const viewsMap = createViewsMap(1, 5),
  31. iterator = viewsMap.values();
  32. for (let i = 0; i < 3; i++) {
  33. const view = iterator.next().value;
  34. buffer.push(view);
  35. }
  36. // Ensure that the correct views are inserted.
  37. expect([...buffer]).toEqual([
  38. viewsMap.get(1),
  39. viewsMap.get(2),
  40. viewsMap.get(3),
  41. ]);
  42. for (let i = 3; i < 5; i++) {
  43. const view = iterator.next().value;
  44. buffer.push(view);
  45. }
  46. // Ensure that the correct views are evicted.
  47. expect([...buffer]).toEqual([
  48. viewsMap.get(3),
  49. viewsMap.get(4),
  50. viewsMap.get(5),
  51. ]);
  52. });
  53. it("handles `resize` correctly", function () {
  54. const buffer = new PDFPageViewBuffer(5);
  55. const viewsMap = createViewsMap(1, 5),
  56. iterator = viewsMap.values();
  57. for (let i = 0; i < 5; i++) {
  58. const view = iterator.next().value;
  59. buffer.push(view);
  60. }
  61. // Ensure that keeping the size constant won't evict any views.
  62. buffer.resize(5);
  63. expect([...buffer]).toEqual([
  64. viewsMap.get(1),
  65. viewsMap.get(2),
  66. viewsMap.get(3),
  67. viewsMap.get(4),
  68. viewsMap.get(5),
  69. ]);
  70. // Ensure that increasing the size won't evict any views.
  71. buffer.resize(10);
  72. expect([...buffer]).toEqual([
  73. viewsMap.get(1),
  74. viewsMap.get(2),
  75. viewsMap.get(3),
  76. viewsMap.get(4),
  77. viewsMap.get(5),
  78. ]);
  79. // Ensure that decreasing the size will evict the correct views.
  80. buffer.resize(3);
  81. expect([...buffer]).toEqual([
  82. viewsMap.get(3),
  83. viewsMap.get(4),
  84. viewsMap.get(5),
  85. ]);
  86. });
  87. it("handles `resize` correctly, with `idsToKeep` provided", function () {
  88. const buffer = new PDFPageViewBuffer(5);
  89. const viewsMap = createViewsMap(1, 5),
  90. iterator = viewsMap.values();
  91. for (let i = 0; i < 5; i++) {
  92. const view = iterator.next().value;
  93. buffer.push(view);
  94. }
  95. // Ensure that keeping the size constant won't evict any views,
  96. // while re-ordering them correctly.
  97. buffer.resize(5, new Set([1, 2]));
  98. expect([...buffer]).toEqual([
  99. viewsMap.get(3),
  100. viewsMap.get(4),
  101. viewsMap.get(5),
  102. viewsMap.get(1),
  103. viewsMap.get(2),
  104. ]);
  105. // Ensure that increasing the size won't evict any views,
  106. // while re-ordering them correctly.
  107. buffer.resize(10, new Set([3, 4, 5]));
  108. expect([...buffer]).toEqual([
  109. viewsMap.get(1),
  110. viewsMap.get(2),
  111. viewsMap.get(3),
  112. viewsMap.get(4),
  113. viewsMap.get(5),
  114. ]);
  115. // Ensure that decreasing the size will evict the correct views,
  116. // while re-ordering the remaining ones correctly.
  117. buffer.resize(3, new Set([1, 2, 5]));
  118. expect([...buffer]).toEqual([
  119. viewsMap.get(1),
  120. viewsMap.get(2),
  121. viewsMap.get(5),
  122. ]);
  123. });
  124. it("handles `has` correctly", function () {
  125. const buffer = new PDFPageViewBuffer(3);
  126. const viewsMap = createViewsMap(1, 2),
  127. iterator = viewsMap.values();
  128. for (let i = 0; i < 1; i++) {
  129. const view = iterator.next().value;
  130. buffer.push(view);
  131. }
  132. expect(buffer.has(viewsMap.get(1))).toEqual(true);
  133. expect(buffer.has(viewsMap.get(2))).toEqual(false);
  134. });
  135. });
  136. });