tor-browser

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

browser_caching_position.js (6491B)


      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/layout.js */
      8 loadScripts({ name: "layout.js", dir: MOCHITESTS_DIR });
      9 
     10 function getCachedBounds(acc) {
     11  let cachedBounds = "";
     12  try {
     13    cachedBounds = acc.cache.getStringProperty("relative-bounds");
     14  } catch (e) {
     15    ok(false, "Unable to fetch cached bounds from cache!");
     16  }
     17  return cachedBounds;
     18 }
     19 
     20 async function testCoordinates(accDoc, id, expectedWidthPx, expectedHeightPx) {
     21  let acc = findAccessibleChildByID(accDoc, id, [Ci.nsIAccessibleImage]);
     22  if (!acc) {
     23    return;
     24  }
     25 
     26  let screenX = {};
     27  let screenY = {};
     28  let windowX = {};
     29  let windowY = {};
     30  let parentX = {};
     31  let parentY = {};
     32 
     33  // get screen coordinates.
     34  acc.getImagePosition(
     35    nsIAccessibleCoordinateType.COORDTYPE_SCREEN_RELATIVE,
     36    screenX,
     37    screenY
     38  );
     39  // get window coordinates.
     40  acc.getImagePosition(
     41    nsIAccessibleCoordinateType.COORDTYPE_WINDOW_RELATIVE,
     42    windowX,
     43    windowY
     44  );
     45  // get parent related coordinates.
     46  acc.getImagePosition(
     47    nsIAccessibleCoordinateType.COORDTYPE_PARENT_RELATIVE,
     48    parentX,
     49    parentY
     50  );
     51  // XXX For linked images, a negative parentY value is returned, and the
     52  // screenY coordinate is the link's screenY coordinate minus 1.
     53  // Until this is fixed, set parentY to -1 if it's negative.
     54  if (parentY.value < 0) {
     55    parentY.value = -1;
     56  }
     57 
     58  // See if asking image for child at image's screen coordinates gives
     59  // correct accessible. getChildAtPoint operates on screen coordinates.
     60  let tempAcc = null;
     61  try {
     62    tempAcc = acc.getChildAtPoint(screenX.value, screenY.value);
     63  } catch (e) {}
     64  is(tempAcc, acc, "Wrong accessible returned for position of " + id + "!");
     65 
     66  // get image's parent.
     67  let imageParentAcc = null;
     68  try {
     69    imageParentAcc = acc.parent;
     70  } catch (e) {}
     71  ok(imageParentAcc, "no parent accessible for " + id + "!");
     72 
     73  if (imageParentAcc) {
     74    // See if parent's screen coordinates plus image's parent relative
     75    // coordinates equal to image's screen coordinates.
     76    let parentAccX = {};
     77    let parentAccY = {};
     78    let parentAccWidth = {};
     79    let parentAccHeight = {};
     80    imageParentAcc.getBounds(
     81      parentAccX,
     82      parentAccY,
     83      parentAccWidth,
     84      parentAccHeight
     85    );
     86    is(
     87      parentAccX.value + parentX.value,
     88      screenX.value,
     89      "Wrong screen x coordinate for " + id + "!"
     90    );
     91    // XXX see bug 456344
     92    // is(
     93    //   parentAccY.value + parentY.value,
     94    //   screenY.value,
     95    //   "Wrong screen y coordinate for " + id + "!"
     96    // );
     97  }
     98 
     99  let [expectedW, expectedH] = CSSToDevicePixels(
    100    window,
    101    expectedWidthPx,
    102    expectedHeightPx
    103  );
    104  let width = {};
    105  let height = {};
    106  acc.getImageSize(width, height);
    107  is(width.value, expectedW, "Wrong width for " + id + "!");
    108  is(height.value, expectedH, "wrong height for " + id + "!");
    109 }
    110 
    111 addAccessibleTask(
    112  `
    113  <br>Simple image:<br>
    114  <img id="nonLinkedImage" src="http://example.com/a11y/accessible/tests/mochitest/moz.png"/>
    115  <br>Linked image:<br>
    116  <a href="http://www.mozilla.org"><img id="linkedImage" src="http://example.com/a11y/accessible/tests/mochitest/moz.png"></a>
    117  <br>Image with longdesc:<br>
    118  <img id="longdesc" src="http://example.com/a11y/accessible/tests/mochitest/moz.png" longdesc="longdesc_src.html"
    119       alt="Image of Mozilla logo"/>
    120  <br>Image with invalid url in longdesc:<br>
    121  <img id="invalidLongdesc" src="http://example.com/a11y/accessible/tests/mochitest/moz.png" longdesc="longdesc src.html"
    122       alt="Image of Mozilla logo"/>
    123  <br>Image with click and longdesc:<br>
    124  <img id="clickAndLongdesc" src="http://example.com/a11y/accessible/tests/mochitest/moz.png" longdesc="longdesc_src.html"
    125       alt="Another image of Mozilla logo" onclick="alert('Clicked!');"/>
    126 
    127  <br>image described by a link to be treated as longdesc<br>
    128  <img id="longdesc2" src="http://example.com/a11y/accessible/tests/mochitest/moz.png" aria-describedby="describing_link"
    129       alt="Second Image of Mozilla logo"/>
    130  <a id="describing_link" href="longdesc_src.html">link to description of image</a>
    131 
    132  <br>Image described by a link to be treated as longdesc with whitespaces<br>
    133  <img id="longdesc3" src="http://example.com/a11y/accessible/tests/mochitest/moz.png" aria-describedby="describing_link2"
    134       alt="Second Image of Mozilla logo"/>
    135  <a id="describing_link2" href="longdesc src.html">link to description of image</a>
    136 
    137  <br>Image with click:<br>
    138  <img id="click" src="http://example.com/a11y/accessible/tests/mochitest/moz.png"
    139       alt="A third image of Mozilla logo" onclick="alert('Clicked, too!');"/>
    140  `,
    141  async function (browser, docAcc) {
    142    // Test non-linked image
    143    await testCoordinates(docAcc, "nonLinkedImage", 89, 38);
    144 
    145    // Test linked image
    146    await testCoordinates(docAcc, "linkedImage", 89, 38);
    147 
    148    // Image with long desc
    149    await testCoordinates(docAcc, "longdesc", 89, 38);
    150 
    151    // Image with invalid url in long desc
    152    await testCoordinates(docAcc, "invalidLongdesc", 89, 38);
    153 
    154    // Image with click and long desc
    155    await testCoordinates(docAcc, "clickAndLongdesc", 89, 38);
    156 
    157    // Image with click
    158    await testCoordinates(docAcc, "click", 89, 38);
    159 
    160    // Image with long desc
    161    await testCoordinates(docAcc, "longdesc2", 89, 38);
    162 
    163    // Image described by HTML:a@href with whitespaces
    164    await testCoordinates(docAcc, "longdesc3", 89, 38);
    165  }
    166 );
    167 
    168 addAccessibleTask(
    169  `
    170  <br>Linked image:<br>
    171  <a href="http://www.mozilla.org"><img id="linkedImage" src="http://example.com/a11y/accessible/tests/mochitest/moz.png"></a>
    172  `,
    173  async function (browser, docAcc) {
    174    const imgAcc = findAccessibleChildByID(docAcc, "linkedImage", [
    175      Ci.nsIAccessibleImage,
    176    ]);
    177    const origCachedBounds = getCachedBounds(imgAcc);
    178 
    179    await invokeContentTask(browser, [], () => {
    180      const imgNode = content.document.getElementById("linkedImage");
    181      imgNode.style = "margin-left: 1000px; margin-top: 500px;";
    182    });
    183 
    184    await untilCacheOk(() => {
    185      return origCachedBounds != getCachedBounds(imgAcc);
    186    }, "Cached bounds update after mutation");
    187  },
    188  {
    189    // We can only access the `cache` attribute of an accessible when
    190    // the cache is enabled and we're in a remote browser.
    191    topLevel: true,
    192    iframe: true,
    193  }
    194 );