Backed out changeset f594e6f00208 (bug 1940883) for causing crashes in bug 1941164.
[gecko.git] / toolkit / components / pdfjs / test / browser_pdfjs_js.js
bloba9b36caa03bdbb6aa592506fa93e271ac5f21a4a
1 /* Any copyright is dedicated to the Public Domain.
2  * http://creativecommons.org/publicdomain/zero/1.0/ */
4 const RELATIVE_DIR = "toolkit/components/pdfjs/test/";
5 const TESTROOT = "http://example.com/browser/" + RELATIVE_DIR;
7 // Test js in pdf file.
8 add_task(async function test_js_sandbox() {
9   await BrowserTestUtils.withNewTab(
10     { gBrowser, url: "about:blank" },
11     async function (browser) {
12       await SpecialPowers.pushPrefEnv({
13         set: [["pdfjs.enableScripting", true]],
14       });
16       await Promise.all([
17         waitForPdfJSAnnotationLayer(browser, TESTROOT + "file_pdfjs_js.pdf"),
18         waitForPdfJSSandbox(browser),
19       ]);
21       await SpecialPowers.spawn(browser, [], async () => {
22         const { PdfSandbox } = ChromeUtils.importESModule(
23           "resource://pdf.js/PdfSandbox.sys.mjs"
24         );
26         let sandboxDestroyCount = 0;
27         const originalDestroy = PdfSandbox.prototype.destroy;
28         PdfSandbox.prototype.destroy = function () {
29           const obj = this.sandbox.eval("({})");
30           originalDestroy.apply(this, arguments);
31           sandboxDestroyCount++;
32           ok(Cu.isDeadWrapper(obj), "Sandbox must have been nuked");
33         };
35         const document = content.document;
36         const button = document.querySelector("[data-annotation-id='16R'] a");
37         button.dispatchEvent(new content.Event("click"));
39         const text = document.querySelector(`[data-element-id="15R"]`);
41         is(text.value, "test", "Text field must containt 'test' string");
43         content.addEventListener("unload", () => {
44           is(sandboxDestroyCount, 1, "Sandbox must have been destroyed");
45         });
46       });
48       await waitForPdfJSClose(browser);
49       await SpecialPowers.popPrefEnv();
50     }
51   );
52 });