tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

browser_treeupdate_doc.js (10360B)


      1 /* This Source Code Form is subject to the terms of the Mozilla Public
      2 * License, v. 2.0. If a copy of the MPL was not distributed with this
      3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      4 
      5 "use strict";
      6 
      7 /* import-globals-from ../../mochitest/role.js */
      8 loadScripts({ name: "role.js", dir: MOCHITESTS_DIR });
      9 
     10 const iframeSrc = `data:text/html,
     11  <html>
     12    <head>
     13      <meta charset='utf-8'/>
     14      <title>Inner Iframe</title>
     15    </head>
     16    <body id='inner-iframe'></body>
     17  </html>`;
     18 
     19 addAccessibleTask(
     20  `
     21  <iframe id="iframe" src="${iframeSrc}"></iframe>`,
     22  async function (browser, accDoc) {
     23    // ID of the iframe that is being tested
     24    const id = "inner-iframe";
     25 
     26    let iframe = findAccessibleChildByID(accDoc, id);
     27 
     28    /* ================= Initial tree check =================================== */
     29    let tree = {
     30      role: ROLE_DOCUMENT,
     31      children: [],
     32    };
     33    testAccessibleTree(iframe, tree);
     34 
     35    /* ================= Write iframe document ================================ */
     36    let reorderEventPromise = waitForEvent(EVENT_REORDER, id);
     37    await invokeContentTask(browser, [id], contentId => {
     38      let docNode = content.document.getElementById("iframe").contentDocument;
     39      let newHTMLNode = docNode.createElement("html");
     40      let newBodyNode = docNode.createElement("body");
     41      let newTextNode = docNode.createTextNode("New Wave");
     42      newBodyNode.id = contentId;
     43      newBodyNode.appendChild(newTextNode);
     44      newHTMLNode.appendChild(newBodyNode);
     45      docNode.replaceChild(newHTMLNode, docNode.documentElement);
     46    });
     47    await reorderEventPromise;
     48 
     49    tree = {
     50      role: ROLE_DOCUMENT,
     51      children: [
     52        {
     53          role: ROLE_TEXT_LEAF,
     54          name: "New Wave",
     55        },
     56      ],
     57    };
     58    testAccessibleTree(iframe, tree);
     59 
     60    /* ================= Replace iframe HTML element ========================== */
     61    reorderEventPromise = waitForEvent(EVENT_REORDER, id);
     62    await invokeContentTask(browser, [id], contentId => {
     63      let docNode = content.document.getElementById("iframe").contentDocument;
     64      // We can't use open/write/close outside of iframe document because of
     65      // security error.
     66      let script = docNode.createElement("script");
     67      script.textContent = `
     68      document.open();
     69      document.write('<body id="${contentId}">hello</body>');
     70      document.close();`;
     71      docNode.body.appendChild(script);
     72    });
     73    await reorderEventPromise;
     74 
     75    tree = {
     76      role: ROLE_DOCUMENT,
     77      children: [
     78        {
     79          role: ROLE_TEXT_LEAF,
     80          name: "hello",
     81        },
     82      ],
     83    };
     84    testAccessibleTree(iframe, tree);
     85 
     86    /* ================= Replace iframe body ================================== */
     87    reorderEventPromise = waitForEvent(EVENT_REORDER, id);
     88    await invokeContentTask(browser, [id], contentId => {
     89      let docNode = content.document.getElementById("iframe").contentDocument;
     90      let newBodyNode = docNode.createElement("body");
     91      let newTextNode = docNode.createTextNode("New Hello");
     92      newBodyNode.id = contentId;
     93      newBodyNode.appendChild(newTextNode);
     94      newBodyNode.setAttribute("role", "application");
     95      docNode.documentElement.replaceChild(newBodyNode, docNode.body);
     96    });
     97    await reorderEventPromise;
     98 
     99    tree = {
    100      role: ROLE_APPLICATION,
    101      children: [
    102        {
    103          role: ROLE_TEXT_LEAF,
    104          name: "New Hello",
    105        },
    106      ],
    107    };
    108    testAccessibleTree(iframe, tree);
    109 
    110    /* ================= Open iframe document ================================= */
    111    reorderEventPromise = waitForEvent(EVENT_REORDER, id);
    112    await invokeContentTask(browser, [id], contentId => {
    113      // Open document.
    114      let docNode = content.document.getElementById("iframe").contentDocument;
    115      let script = docNode.createElement("script");
    116      script.textContent = `
    117      function closeMe() {
    118        document.write('Works?');
    119        document.close();
    120      }
    121      window.closeMe = closeMe;
    122      document.open();
    123      document.write('<body id="${contentId}"></body>');`;
    124      docNode.body.appendChild(script);
    125    });
    126    await reorderEventPromise;
    127 
    128    tree = {
    129      role: ROLE_DOCUMENT,
    130      children: [],
    131    };
    132    testAccessibleTree(iframe, tree);
    133 
    134    /* ================= Close iframe document ================================ */
    135    reorderEventPromise = waitForEvent(EVENT_REORDER, id);
    136    await invokeContentTask(browser, [], () => {
    137      // Write and close document.
    138      let docNode = content.document.getElementById("iframe").contentDocument;
    139      docNode.write("Works?");
    140      docNode.close();
    141    });
    142    await reorderEventPromise;
    143 
    144    tree = {
    145      role: ROLE_DOCUMENT,
    146      children: [
    147        {
    148          role: ROLE_TEXT_LEAF,
    149          name: "Works?",
    150        },
    151      ],
    152    };
    153    testAccessibleTree(iframe, tree);
    154 
    155    /* ================= Remove HTML from iframe document ===================== */
    156    reorderEventPromise = waitForEvent(EVENT_REORDER, iframe);
    157    await invokeContentTask(browser, [], () => {
    158      // Remove HTML element.
    159      let docNode = content.document.getElementById("iframe").contentDocument;
    160      docNode.firstChild.remove();
    161    });
    162    let event = await reorderEventPromise;
    163 
    164    ok(
    165      event.accessible instanceof nsIAccessibleDocument,
    166      "Reorder should happen on the document"
    167    );
    168    tree = {
    169      role: ROLE_DOCUMENT,
    170      children: [],
    171    };
    172    testAccessibleTree(iframe, tree);
    173 
    174    /* ================= Insert HTML to iframe document ======================= */
    175    reorderEventPromise = waitForEvent(EVENT_REORDER, id);
    176    await invokeContentTask(browser, [id], contentId => {
    177      // Insert HTML element.
    178      let docNode = content.document.getElementById("iframe").contentDocument;
    179      let html = docNode.createElement("html");
    180      let body = docNode.createElement("body");
    181      let text = docNode.createTextNode("Haha");
    182      body.appendChild(text);
    183      body.id = contentId;
    184      html.appendChild(body);
    185      docNode.appendChild(html);
    186    });
    187    await reorderEventPromise;
    188 
    189    tree = {
    190      role: ROLE_DOCUMENT,
    191      children: [
    192        {
    193          role: ROLE_TEXT_LEAF,
    194          name: "Haha",
    195        },
    196      ],
    197    };
    198    testAccessibleTree(iframe, tree);
    199 
    200    /* ================= Remove body from iframe document ===================== */
    201    reorderEventPromise = waitForEvent(EVENT_REORDER, iframe);
    202    await invokeContentTask(browser, [], () => {
    203      // Remove body element.
    204      let docNode = content.document.getElementById("iframe").contentDocument;
    205      docNode.documentElement.removeChild(docNode.body);
    206    });
    207    event = await reorderEventPromise;
    208 
    209    ok(
    210      event.accessible instanceof nsIAccessibleDocument,
    211      "Reorder should happen on the document"
    212    );
    213    tree = {
    214      role: ROLE_DOCUMENT,
    215      children: [],
    216    };
    217    testAccessibleTree(iframe, tree);
    218 
    219    /* ================ Insert element under document element while body missed */
    220    reorderEventPromise = waitForEvent(EVENT_REORDER, iframe);
    221    await invokeContentTask(browser, [], () => {
    222      let docNode = content.document.getElementById("iframe").contentDocument;
    223      let inputNode = (content.window.inputNode =
    224        docNode.createElement("input"));
    225      docNode.documentElement.appendChild(inputNode);
    226    });
    227    event = await reorderEventPromise;
    228 
    229    ok(
    230      event.accessible instanceof nsIAccessibleDocument,
    231      "Reorder should happen on the document"
    232    );
    233    tree = {
    234      DOCUMENT: [{ ENTRY: [] }],
    235    };
    236    testAccessibleTree(iframe, tree);
    237 
    238    reorderEventPromise = waitForEvent(EVENT_REORDER, iframe);
    239    await invokeContentTask(browser, [], () => {
    240      let docEl =
    241        content.document.getElementById("iframe").contentDocument
    242          .documentElement;
    243      // Remove aftermath of this test before next test starts.
    244      docEl.firstChild.remove();
    245    });
    246    // Make sure reorder event was fired and that the input was removed.
    247    await reorderEventPromise;
    248    tree = {
    249      role: ROLE_DOCUMENT,
    250      children: [],
    251    };
    252    testAccessibleTree(iframe, tree);
    253 
    254    /* ================= Insert body to iframe document ======================= */
    255    reorderEventPromise = waitForEvent(EVENT_REORDER, id);
    256    await invokeContentTask(browser, [id], contentId => {
    257      // Write and close document.
    258      let docNode = content.document.getElementById("iframe").contentDocument;
    259      // Insert body element.
    260      let body = docNode.createElement("body");
    261      let text = docNode.createTextNode("Yo ho ho i butylka roma!");
    262      body.appendChild(text);
    263      body.id = contentId;
    264      docNode.documentElement.appendChild(body);
    265    });
    266    await reorderEventPromise;
    267 
    268    tree = {
    269      role: ROLE_DOCUMENT,
    270      children: [
    271        {
    272          role: ROLE_TEXT_LEAF,
    273          name: "Yo ho ho i butylka roma!",
    274        },
    275      ],
    276    };
    277    testAccessibleTree(iframe, tree);
    278 
    279    /* ================= Change source ======================================== */
    280    reorderEventPromise = waitForEvent(EVENT_REORDER, "iframe");
    281    await invokeSetAttribute(
    282      browser,
    283      "iframe",
    284      "src",
    285      `data:text/html,<html><body id="${id}"><input></body></html>`
    286    );
    287    event = await reorderEventPromise;
    288 
    289    tree = {
    290      INTERNAL_FRAME: [{ DOCUMENT: [{ ENTRY: [] }] }],
    291    };
    292    testAccessibleTree(event.accessible, tree);
    293    iframe = findAccessibleChildByID(event.accessible, id);
    294 
    295    /* ================= Replace iframe body on ARIA role body ================ */
    296    reorderEventPromise = waitForEvent(EVENT_REORDER, id);
    297    await invokeContentTask(browser, [id], contentId => {
    298      let docNode = content.document.getElementById("iframe").contentDocument;
    299      let newBodyNode = docNode.createElement("body");
    300      let newTextNode = docNode.createTextNode("New Hello");
    301      newBodyNode.appendChild(newTextNode);
    302      newBodyNode.setAttribute("role", "application");
    303      newBodyNode.id = contentId;
    304      docNode.documentElement.replaceChild(newBodyNode, docNode.body);
    305    });
    306    await reorderEventPromise;
    307 
    308    tree = {
    309      role: ROLE_APPLICATION,
    310      children: [
    311        {
    312          role: ROLE_TEXT_LEAF,
    313          name: "New Hello",
    314        },
    315      ],
    316    };
    317    testAccessibleTree(iframe, tree);
    318  },
    319  { iframe: true, remoteIframe: true }
    320 );