annotation_spec.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. /* Copyright 2020 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 {
  16. closePages,
  17. getSelector,
  18. getQuerySelector,
  19. loadAndWait,
  20. } = require("./test_utils.js");
  21. describe("Annotation highlight", () => {
  22. describe("annotation-highlight.pdf", () => {
  23. let pages;
  24. beforeAll(async () => {
  25. pages = await loadAndWait(
  26. "annotation-highlight.pdf",
  27. "[data-annotation-id='19R']"
  28. );
  29. });
  30. afterAll(async () => {
  31. await closePages(pages);
  32. });
  33. it("must show a popup on mouseover", async () => {
  34. await Promise.all(
  35. pages.map(async ([browserName, page]) => {
  36. let hidden = await page.$eval(
  37. "[data-annotation-id='21R']",
  38. el => el.hidden
  39. );
  40. expect(hidden).withContext(`In ${browserName}`).toEqual(true);
  41. await page.hover("[data-annotation-id='19R']");
  42. await page.waitForSelector("[data-annotation-id='21R']", {
  43. visible: true,
  44. timeout: 0,
  45. });
  46. hidden = await page.$eval(
  47. "[data-annotation-id='21R']",
  48. el => el.hidden
  49. );
  50. expect(hidden).withContext(`In ${browserName}`).toEqual(false);
  51. })
  52. );
  53. });
  54. });
  55. });
  56. describe("Checkbox annotation", () => {
  57. describe("issue12706.pdf", () => {
  58. let pages;
  59. beforeAll(async () => {
  60. pages = await loadAndWait("issue12706.pdf", "[data-annotation-id='63R']");
  61. });
  62. afterAll(async () => {
  63. await closePages(pages);
  64. });
  65. it("must let checkboxes with the same name behave like radio buttons", async () => {
  66. const selectors = [63, 70, 79].map(n => `[data-annotation-id='${n}R']`);
  67. await Promise.all(
  68. pages.map(async ([browserName, page]) => {
  69. for (const selector of selectors) {
  70. await page.click(selector);
  71. await page.waitForFunction(
  72. `document.querySelector("${selector} > :first-child").checked`
  73. );
  74. for (const otherSelector of selectors) {
  75. const checked = await page.$eval(
  76. `${otherSelector} > :first-child`,
  77. el => el.checked
  78. );
  79. expect(checked)
  80. .withContext(`In ${browserName}`)
  81. .toBe(selector === otherSelector);
  82. }
  83. }
  84. })
  85. );
  86. });
  87. });
  88. describe("issue15597.pdf", () => {
  89. let pages;
  90. beforeAll(async () => {
  91. pages = await loadAndWait("issue15597.pdf", "[data-annotation-id='7R']");
  92. });
  93. afterAll(async () => {
  94. await closePages(pages);
  95. });
  96. it("must check the checkbox", async () => {
  97. await Promise.all(
  98. pages.map(async ([browserName, page]) => {
  99. const selector = "[data-annotation-id='7R']";
  100. await page.click(selector);
  101. await page.waitForFunction(
  102. `document.querySelector("${selector} > :first-child").checked`
  103. );
  104. expect(true).withContext(`In ${browserName}`).toEqual(true);
  105. })
  106. );
  107. });
  108. });
  109. });
  110. describe("Text widget", () => {
  111. describe("issue13271.pdf", () => {
  112. let pages;
  113. beforeAll(async () => {
  114. pages = await loadAndWait("issue13271.pdf", "[data-annotation-id='24R']");
  115. });
  116. afterAll(async () => {
  117. await closePages(pages);
  118. });
  119. it("must update all the fields with the same value", async () => {
  120. const base = "hello world";
  121. await Promise.all(
  122. pages.map(async ([browserName, page]) => {
  123. await page.type(getSelector("25R"), base);
  124. await page.waitForFunction(`${getQuerySelector("24R")}.value !== ""`);
  125. await page.waitForFunction(`${getQuerySelector("26R")}.value !== ""`);
  126. let text = await page.$eval(getSelector("24R"), el => el.value);
  127. expect(text).withContext(`In ${browserName}`).toEqual(base);
  128. text = await page.$eval(getSelector("26R"), el => el.value);
  129. expect(text).withContext(`In ${browserName}`).toEqual(base);
  130. })
  131. );
  132. });
  133. });
  134. });
  135. describe("Annotation and storage", () => {
  136. describe("issue14023.pdf", () => {
  137. let pages;
  138. beforeAll(async () => {
  139. pages = await loadAndWait("issue14023.pdf", "[data-annotation-id='64R']");
  140. });
  141. afterAll(async () => {
  142. await closePages(pages);
  143. });
  144. it("must let checkboxes with the same name behave like radio buttons", async () => {
  145. const text1 = "hello world!";
  146. const text2 = "!dlrow olleh";
  147. await Promise.all(
  148. pages.map(async ([browserName, page]) => {
  149. // Text field.
  150. await page.type(getSelector("64R"), text1);
  151. // Checkbox.
  152. await page.click("[data-annotation-id='65R']");
  153. // Radio.
  154. await page.click("[data-annotation-id='67R']");
  155. for (const [pageNumber, textId, checkId, radio1Id, radio2Id] of [
  156. [2, "18R", "19R", "21R", "20R"],
  157. [5, "23R", "24R", "22R", "25R"],
  158. ]) {
  159. await page.evaluate(n => {
  160. window.document
  161. .querySelectorAll(`[data-page-number="${n}"][class="page"]`)[0]
  162. .scrollIntoView();
  163. }, pageNumber);
  164. // Need to wait to have a displayed text input.
  165. await page.waitForSelector(getSelector(textId), {
  166. timeout: 0,
  167. });
  168. const text = await page.$eval(getSelector(textId), el => el.value);
  169. expect(text).withContext(`In ${browserName}`).toEqual(text1);
  170. let checked = await page.$eval(
  171. getSelector(checkId),
  172. el => el.checked
  173. );
  174. expect(checked).toEqual(true);
  175. checked = await page.$eval(getSelector(radio1Id), el => el.checked);
  176. expect(checked).toEqual(false);
  177. checked = await page.$eval(getSelector(radio2Id), el => el.checked);
  178. expect(checked).toEqual(false);
  179. }
  180. // Change data on page 5 and check that other pages changed.
  181. // Text field.
  182. await page.type(getSelector("23R"), text2);
  183. // Checkbox.
  184. await page.click("[data-annotation-id='24R']");
  185. // Radio.
  186. await page.click("[data-annotation-id='25R']");
  187. for (const [pageNumber, textId, checkId, radio1Id, radio2Id] of [
  188. [1, "64R", "65R", "67R", "68R"],
  189. [2, "18R", "19R", "21R", "20R"],
  190. ]) {
  191. await page.evaluate(n => {
  192. window.document
  193. .querySelectorAll(`[data-page-number="${n}"][class="page"]`)[0]
  194. .scrollIntoView();
  195. }, pageNumber);
  196. // Need to wait to have a displayed text input.
  197. await page.waitForSelector(getSelector(textId), {
  198. timeout: 0,
  199. });
  200. const text = await page.$eval(getSelector(textId), el => el.value);
  201. expect(text)
  202. .withContext(`In ${browserName}`)
  203. .toEqual(text2 + text1);
  204. let checked = await page.$eval(
  205. getSelector(checkId),
  206. el => el.checked
  207. );
  208. expect(checked).toEqual(false);
  209. checked = await page.$eval(getSelector(radio1Id), el => el.checked);
  210. expect(checked).toEqual(false);
  211. checked = await page.$eval(getSelector(radio2Id), el => el.checked);
  212. expect(checked).toEqual(false);
  213. }
  214. })
  215. );
  216. });
  217. });
  218. });
  219. describe("ResetForm action", () => {
  220. describe("resetform.pdf", () => {
  221. let pages;
  222. beforeAll(async () => {
  223. pages = await loadAndWait("resetform.pdf", "[data-annotation-id='63R']");
  224. });
  225. afterAll(async () => {
  226. await closePages(pages);
  227. });
  228. it("must reset all fields", async () => {
  229. await Promise.all(
  230. pages.map(async ([browserName, page]) => {
  231. const base = "hello world";
  232. for (let i = 63; i <= 67; i++) {
  233. await page.type(getSelector(`${i}R`), base);
  234. }
  235. const selectors = [69, 71, 75].map(
  236. n => `[data-annotation-id='${n}R']`
  237. );
  238. for (const selector of selectors) {
  239. await page.click(selector);
  240. }
  241. await page.select(getSelector("78R"), "b");
  242. await page.select(getSelector("81R"), "f");
  243. await page.click("[data-annotation-id='82R']");
  244. await page.waitForFunction(`${getQuerySelector("63R")}.value === ""`);
  245. for (let i = 63; i <= 68; i++) {
  246. const text = await page.$eval(getSelector(`${i}R`), el => el.value);
  247. expect(text).withContext(`In ${browserName}`).toEqual("");
  248. }
  249. const ids = [69, 71, 72, 73, 74, 75, 76, 77];
  250. for (const id of ids) {
  251. const checked = await page.$eval(
  252. getSelector(`${id}R`),
  253. el => el.checked
  254. );
  255. expect(checked).withContext(`In ${browserName}`).toEqual(false);
  256. }
  257. let selected = await page.$eval(
  258. `${getSelector("78R")} [value="a"]`,
  259. el => el.selected
  260. );
  261. expect(selected).withContext(`In ${browserName}`).toEqual(true);
  262. selected = await page.$eval(
  263. `${getSelector("81R")} [value="d"]`,
  264. el => el.selected
  265. );
  266. expect(selected).withContext(`In ${browserName}`).toEqual(true);
  267. })
  268. );
  269. });
  270. it("must reset some fields", async () => {
  271. await Promise.all(
  272. pages.map(async ([browserName, page]) => {
  273. const base = "hello world";
  274. for (let i = 63; i <= 68; i++) {
  275. await page.type(getSelector(`${i}R`), base);
  276. }
  277. const selectors = [69, 71, 72, 73, 75].map(
  278. n => `[data-annotation-id='${n}R']`
  279. );
  280. for (const selector of selectors) {
  281. await page.click(selector);
  282. }
  283. await page.select(getSelector("78R"), "b");
  284. await page.select(getSelector("81R"), "f");
  285. await page.click("[data-annotation-id='84R']");
  286. await page.waitForFunction(`${getQuerySelector("63R")}.value === ""`);
  287. for (let i = 63; i <= 68; i++) {
  288. const expected = (i - 3) % 2 === 0 ? "" : base;
  289. const text = await page.$eval(getSelector(`${i}R`), el => el.value);
  290. expect(text).withContext(`In ${browserName}`).toEqual(expected);
  291. }
  292. let ids = [69, 72, 73, 74, 76, 77];
  293. for (const id of ids) {
  294. const checked = await page.$eval(
  295. getSelector(`${id}R`),
  296. el => el.checked
  297. );
  298. expect(checked)
  299. .withContext(`In ${browserName + id}`)
  300. .toEqual(false);
  301. }
  302. ids = [71, 75];
  303. for (const id of ids) {
  304. const checked = await page.$eval(
  305. getSelector(`${id}R`),
  306. el => el.checked
  307. );
  308. expect(checked).withContext(`In ${browserName}`).toEqual(true);
  309. }
  310. let selected = await page.$eval(
  311. `${getSelector("78R")} [value="a"]`,
  312. el => el.selected
  313. );
  314. expect(selected).withContext(`In ${browserName}`).toEqual(true);
  315. selected = await page.$eval(
  316. `${getSelector("81R")} [value="f"]`,
  317. el => el.selected
  318. );
  319. expect(selected).withContext(`In ${browserName}`).toEqual(true);
  320. })
  321. );
  322. });
  323. });
  324. });