ui_utils_spec.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * JavaScript code in this page
  4. *
  5. * Copyright 2022 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 _ui_utils = require("../../web/ui_utils.js");
  24. describe("ui_utils", function () {
  25. describe("isValidRotation", function () {
  26. it("should reject non-integer angles", function () {
  27. expect((0, _ui_utils.isValidRotation)()).toEqual(false);
  28. expect((0, _ui_utils.isValidRotation)(null)).toEqual(false);
  29. expect((0, _ui_utils.isValidRotation)(NaN)).toEqual(false);
  30. expect((0, _ui_utils.isValidRotation)([90])).toEqual(false);
  31. expect((0, _ui_utils.isValidRotation)("90")).toEqual(false);
  32. expect((0, _ui_utils.isValidRotation)(90.5)).toEqual(false);
  33. });
  34. it("should reject non-multiple of 90 degree angles", function () {
  35. expect((0, _ui_utils.isValidRotation)(45)).toEqual(false);
  36. expect((0, _ui_utils.isValidRotation)(-123)).toEqual(false);
  37. });
  38. it("should accept valid angles", function () {
  39. expect((0, _ui_utils.isValidRotation)(0)).toEqual(true);
  40. expect((0, _ui_utils.isValidRotation)(90)).toEqual(true);
  41. expect((0, _ui_utils.isValidRotation)(-270)).toEqual(true);
  42. expect((0, _ui_utils.isValidRotation)(540)).toEqual(true);
  43. });
  44. });
  45. describe("isPortraitOrientation", function () {
  46. it("should be portrait orientation", function () {
  47. expect((0, _ui_utils.isPortraitOrientation)({
  48. width: 200,
  49. height: 400
  50. })).toEqual(true);
  51. expect((0, _ui_utils.isPortraitOrientation)({
  52. width: 500,
  53. height: 500
  54. })).toEqual(true);
  55. });
  56. it("should be landscape orientation", function () {
  57. expect((0, _ui_utils.isPortraitOrientation)({
  58. width: 600,
  59. height: 300
  60. })).toEqual(false);
  61. });
  62. });
  63. describe("parseQueryString", function () {
  64. it("should parse one key/value pair", function () {
  65. const parameters = (0, _ui_utils.parseQueryString)("key1=value1");
  66. expect(parameters.size).toEqual(1);
  67. expect(parameters.get("key1")).toEqual("value1");
  68. });
  69. it("should parse multiple key/value pairs", function () {
  70. const parameters = (0, _ui_utils.parseQueryString)("key1=value1&key2=value2&key3=value3");
  71. expect(parameters.size).toEqual(3);
  72. expect(parameters.get("key1")).toEqual("value1");
  73. expect(parameters.get("key2")).toEqual("value2");
  74. expect(parameters.get("key3")).toEqual("value3");
  75. });
  76. it("should parse keys without values", function () {
  77. const parameters = (0, _ui_utils.parseQueryString)("key1");
  78. expect(parameters.size).toEqual(1);
  79. expect(parameters.get("key1")).toEqual("");
  80. });
  81. it("should decode encoded key/value pairs", function () {
  82. const parameters = (0, _ui_utils.parseQueryString)("k%C3%ABy1=valu%C3%AB1");
  83. expect(parameters.size).toEqual(1);
  84. expect(parameters.get("këy1")).toEqual("valuë1");
  85. });
  86. it("should convert keys to lowercase", function () {
  87. const parameters = (0, _ui_utils.parseQueryString)("Key1=Value1&KEY2=Value2");
  88. expect(parameters.size).toEqual(2);
  89. expect(parameters.get("key1")).toEqual("Value1");
  90. expect(parameters.get("key2")).toEqual("Value2");
  91. });
  92. });
  93. describe("removeNullCharacters", function () {
  94. it("should not modify string without null characters", function () {
  95. const str = "string without null chars";
  96. expect((0, _ui_utils.removeNullCharacters)(str)).toEqual("string without null chars");
  97. });
  98. it("should modify string with null characters", function () {
  99. const str = "string\x00With\x00Null\x00Chars";
  100. expect((0, _ui_utils.removeNullCharacters)(str)).toEqual("stringWithNullChars");
  101. });
  102. it("should modify string with non-displayable characters", function () {
  103. const str = Array.from(Array(32).keys()).map(x => String.fromCharCode(x) + "a").join("");
  104. const expected = "a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a";
  105. expect((0, _ui_utils.removeNullCharacters)(str, true)).toEqual(expected);
  106. });
  107. });
  108. describe("getPageSizeInches", function () {
  109. it("gets page size (in inches)", function () {
  110. const page = {
  111. view: [0, 0, 595.28, 841.89],
  112. userUnit: 1.0,
  113. rotate: 0
  114. };
  115. const {
  116. width,
  117. height
  118. } = (0, _ui_utils.getPageSizeInches)(page);
  119. expect(+width.toPrecision(3)).toEqual(8.27);
  120. expect(+height.toPrecision(4)).toEqual(11.69);
  121. });
  122. it("gets page size (in inches), for non-default /Rotate entry", function () {
  123. const pdfPage1 = {
  124. view: [0, 0, 612, 792],
  125. userUnit: 1,
  126. rotate: 0
  127. };
  128. const {
  129. width: width1,
  130. height: height1
  131. } = (0, _ui_utils.getPageSizeInches)(pdfPage1);
  132. expect(width1).toEqual(8.5);
  133. expect(height1).toEqual(11);
  134. const pdfPage2 = {
  135. view: [0, 0, 612, 792],
  136. userUnit: 1,
  137. rotate: 90
  138. };
  139. const {
  140. width: width2,
  141. height: height2
  142. } = (0, _ui_utils.getPageSizeInches)(pdfPage2);
  143. expect(width2).toEqual(11);
  144. expect(height2).toEqual(8.5);
  145. });
  146. });
  147. describe("getVisibleElements", function () {
  148. const BORDER_WIDTH = 9;
  149. const SPACING = 2 * BORDER_WIDTH - 7;
  150. function makePages(lines) {
  151. const result = [];
  152. let lineTop = 0,
  153. id = 0;
  154. for (const line of lines) {
  155. const lineHeight = line.reduce(function (maxHeight, pair) {
  156. return Math.max(maxHeight, pair[1]);
  157. }, 0);
  158. let offsetLeft = -BORDER_WIDTH;
  159. for (const [clientWidth, clientHeight] of line) {
  160. const offsetTop = lineTop + (lineHeight - clientHeight) / 2 - BORDER_WIDTH;
  161. const div = {
  162. offsetLeft,
  163. offsetTop,
  164. clientWidth,
  165. clientHeight,
  166. clientLeft: BORDER_WIDTH,
  167. clientTop: BORDER_WIDTH
  168. };
  169. result.push({
  170. id,
  171. div
  172. });
  173. ++id;
  174. offsetLeft += clientWidth + SPACING;
  175. }
  176. lineTop += lineHeight + SPACING;
  177. }
  178. return result;
  179. }
  180. function slowGetVisibleElements(scroll, pages) {
  181. const views = [],
  182. ids = new Set();
  183. const {
  184. scrollLeft,
  185. scrollTop
  186. } = scroll;
  187. const scrollRight = scrollLeft + scroll.clientWidth;
  188. const scrollBottom = scrollTop + scroll.clientHeight;
  189. for (const view of pages) {
  190. const {
  191. div
  192. } = view;
  193. const viewLeft = div.offsetLeft + div.clientLeft;
  194. const viewRight = viewLeft + div.clientWidth;
  195. const viewTop = div.offsetTop + div.clientTop;
  196. const viewBottom = viewTop + div.clientHeight;
  197. if (viewLeft < scrollRight && viewRight > scrollLeft && viewTop < scrollBottom && viewBottom > scrollTop) {
  198. const hiddenHeight = Math.max(0, scrollTop - viewTop) + Math.max(0, viewBottom - scrollBottom);
  199. const hiddenWidth = Math.max(0, scrollLeft - viewLeft) + Math.max(0, viewRight - scrollRight);
  200. const fractionHeight = (div.clientHeight - hiddenHeight) / div.clientHeight;
  201. const fractionWidth = (div.clientWidth - hiddenWidth) / div.clientWidth;
  202. const percent = fractionHeight * fractionWidth * 100 | 0;
  203. views.push({
  204. id: view.id,
  205. x: viewLeft,
  206. y: viewTop,
  207. view,
  208. percent,
  209. widthPercent: fractionWidth * 100 | 0
  210. });
  211. ids.add(view.id);
  212. }
  213. }
  214. return {
  215. first: views[0],
  216. last: views.at(-1),
  217. views,
  218. ids
  219. };
  220. }
  221. function scrollOverDocument(pages, horizontal = false, rtl = false) {
  222. const size = pages.reduce(function (max, {
  223. div
  224. }) {
  225. return Math.max(max, horizontal ? Math.abs(div.offsetLeft + div.clientLeft + div.clientWidth) : div.offsetTop + div.clientTop + div.clientHeight);
  226. }, 0);
  227. for (let i = -size; i < size; i += 7) {
  228. for (let j = i + 5; j < size; j += j - i) {
  229. const scrollEl = horizontal ? {
  230. scrollTop: 0,
  231. scrollLeft: i,
  232. clientHeight: 10000,
  233. clientWidth: j - i
  234. } : {
  235. scrollTop: i,
  236. scrollLeft: 0,
  237. clientHeight: j - i,
  238. clientWidth: 10000
  239. };
  240. expect((0, _ui_utils.getVisibleElements)({
  241. scrollEl,
  242. views: pages,
  243. sortByVisibility: false,
  244. horizontal,
  245. rtl
  246. })).toEqual(slowGetVisibleElements(scrollEl, pages));
  247. }
  248. }
  249. }
  250. it("with pages of varying height", function () {
  251. const pages = makePages([[[50, 20], [20, 50]], [[30, 12], [12, 30]], [[20, 50], [50, 20]], [[50, 20], [20, 50]]]);
  252. scrollOverDocument(pages);
  253. });
  254. it("widescreen challenge", function () {
  255. const pages = makePages([[[10, 50], [10, 60], [10, 70], [10, 80], [10, 90]], [[10, 90], [10, 80], [10, 70], [10, 60], [10, 50]], [[10, 50], [10, 60], [10, 70], [10, 80], [10, 90]]]);
  256. scrollOverDocument(pages);
  257. });
  258. it("works with horizontal scrolling", function () {
  259. const pages = makePages([[[10, 50], [20, 20], [30, 10]]]);
  260. scrollOverDocument(pages, true);
  261. });
  262. it("works with horizontal scrolling with RTL-documents", function () {
  263. const pages = makePages([[[-10, 50], [-20, 20], [-30, 10]]]);
  264. scrollOverDocument(pages, true, true);
  265. });
  266. it("handles `sortByVisibility` correctly", function () {
  267. const scrollEl = {
  268. scrollTop: 75,
  269. scrollLeft: 0,
  270. clientHeight: 750,
  271. clientWidth: 1500
  272. };
  273. const views = makePages([[[100, 150]], [[100, 150]], [[100, 150]]]);
  274. const visible = (0, _ui_utils.getVisibleElements)({
  275. scrollEl,
  276. views
  277. });
  278. const visibleSorted = (0, _ui_utils.getVisibleElements)({
  279. scrollEl,
  280. views,
  281. sortByVisibility: true
  282. });
  283. const viewsOrder = [],
  284. viewsSortedOrder = [];
  285. for (const view of visible.views) {
  286. viewsOrder.push(view.id);
  287. }
  288. for (const view of visibleSorted.views) {
  289. viewsSortedOrder.push(view.id);
  290. }
  291. expect(viewsOrder).toEqual([0, 1, 2]);
  292. expect(viewsSortedOrder).toEqual([1, 2, 0]);
  293. });
  294. it("handles views being empty", function () {
  295. const scrollEl = {
  296. scrollTop: 10,
  297. scrollLeft: 0,
  298. clientHeight: 750,
  299. clientWidth: 1500
  300. };
  301. const views = [];
  302. expect((0, _ui_utils.getVisibleElements)({
  303. scrollEl,
  304. views
  305. })).toEqual({
  306. first: undefined,
  307. last: undefined,
  308. views: [],
  309. ids: new Set()
  310. });
  311. });
  312. it("handles all views being hidden (without errors)", function () {
  313. const scrollEl = {
  314. scrollTop: 100000,
  315. scrollLeft: 0,
  316. clientHeight: 750,
  317. clientWidth: 1500
  318. };
  319. const views = makePages([[[100, 150]], [[100, 150]], [[100, 150]]]);
  320. expect((0, _ui_utils.getVisibleElements)({
  321. scrollEl,
  322. views
  323. })).toEqual({
  324. first: undefined,
  325. last: undefined,
  326. views: [],
  327. ids: new Set()
  328. });
  329. });
  330. describe("backtrackBeforeAllVisibleElements", function () {
  331. const tallPage = [10, 50];
  332. const shortPage = [10, 10];
  333. const top1 = 20 + SPACING + 40;
  334. const top2 = 20 + SPACING + 10;
  335. it("handles case 1", function () {
  336. const pages = makePages([[[10, 20], [10, 20], [10, 20], [10, 20]], [tallPage, shortPage, tallPage, shortPage], [[10, 50], [10, 50], [10, 50], [10, 50]], [[10, 20], [10, 20], [10, 20], [10, 20]], [[10, 20]]]);
  337. const bsResult = 4;
  338. expect((0, _ui_utils.backtrackBeforeAllVisibleElements)(bsResult, pages, top1)).toEqual(4);
  339. });
  340. it("handles case 2", function () {
  341. const pages = makePages([[[10, 20], [10, 20], [10, 20], [10, 20]], [tallPage, shortPage, tallPage, tallPage], [[10, 50], [10, 50], [10, 50], [10, 50]], [[10, 20], [10, 20], [10, 20], [10, 20]]]);
  342. const bsResult = 6;
  343. expect((0, _ui_utils.backtrackBeforeAllVisibleElements)(bsResult, pages, top1)).toEqual(4);
  344. });
  345. it("handles case 3", function () {
  346. const pages = makePages([[[10, 20], [10, 20], [10, 20], [10, 20]], [tallPage, shortPage, tallPage, shortPage], [[10, 50], [10, 50], [10, 50], [10, 50]], [[10, 20], [10, 20], [10, 20], [10, 20]]]);
  347. const bsResult = 8;
  348. expect((0, _ui_utils.backtrackBeforeAllVisibleElements)(bsResult, pages, top1)).toEqual(4);
  349. });
  350. it("handles case 4", function () {
  351. const pages = makePages([[[10, 20], [10, 20], [10, 20], [10, 20]], [tallPage, shortPage, tallPage, shortPage], [[10, 50], [10, 50], [10, 50], [10, 50]], [[10, 20], [10, 20], [10, 20], [10, 20]]]);
  352. const bsResult = 4;
  353. expect((0, _ui_utils.backtrackBeforeAllVisibleElements)(bsResult, pages, top2)).toEqual(4);
  354. });
  355. });
  356. });
  357. });