summaryrefslogtreecommitdiffstats
path: root/toolkit/components/pdfjs/test/browser_pdfjs_js.js
blob: 67909623cd6afccb4ab6ddc1cbe89a841be94e2f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

const RELATIVE_DIR = "toolkit/components/pdfjs/test/";
const TESTROOT = "http://example.com/browser/" + RELATIVE_DIR;

// Test js in pdf file.
add_task(async function test_js_sandbox() {
  await BrowserTestUtils.withNewTab(
    { gBrowser, url: "about:blank" },
    async function (browser) {
      await SpecialPowers.pushPrefEnv({
        set: [["pdfjs.enableScripting", true]],
      });

      await Promise.all([
        waitForPdfJSAnnotationLayer(browser, TESTROOT + "file_pdfjs_js.pdf"),
        waitForPdfJSSandbox(browser),
      ]);

      await SpecialPowers.spawn(browser, [], async () => {
        const { PdfSandbox } = ChromeUtils.importESModule(
          "resource://pdf.js/PdfSandbox.sys.mjs"
        );

        let sandboxDestroyCount = 0;
        const originalDestroy = PdfSandbox.prototype.destroy;
        PdfSandbox.prototype.destroy = function () {
          const obj = this.sandbox.eval("({})");
          originalDestroy.apply(this, arguments);
          sandboxDestroyCount++;
          ok(Cu.isDeadWrapper(obj), "Sandbox must have been nuked");
        };

        const document = content.document;
        const button = document.querySelector("[data-annotation-id='16R'] a");
        button.dispatchEvent(new content.Event("click"));

        const text = document.querySelector(`[data-element-id="15R"]`);

        is(text.value, "test", "Text field must containt 'test' string");

        content.addEventListener("unload", () => {
          is(sandboxDestroyCount, 1, "Sandbox must have been destroyed");
        });
      });
    }
  );
});