tor-browser

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

head.js (5405B)


      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 /* exported verifyAttributeCached, verifyAttributeCachedNoRetry,
      8            testAttributeCachePresence, testCachingPerPlatform */
      9 
     10 // Load the shared-head file first.
     11 Services.scriptloader.loadSubScript(
     12  "chrome://mochitests/content/browser/accessible/tests/browser/shared-head.js",
     13  this
     14 );
     15 
     16 // Loading and common.js from accessible/tests/mochitest/ for all tests, as
     17 // well as events.js.
     18 loadScripts(
     19  { name: "common.js", dir: MOCHITESTS_DIR },
     20  { name: "layout.js", dir: MOCHITESTS_DIR },
     21  { name: "promisified-events.js", dir: MOCHITESTS_DIR }
     22 );
     23 
     24 /**
     25 * Verifies that the given attribute is cached on the given acc. Retries until
     26 * a timeout via untilCacheOk.
     27 *
     28 * @param {nsIAccessible} accessible the accessible where the attribute to query
     29 *                                   should be cached
     30 * @param {string}        attribute  the attribute to query in the cache
     31 */
     32 async function verifyAttributeCached(accessible, attribute) {
     33  // Wait until the attribute is present in the cache.
     34  await untilCacheOk(() => {
     35    try {
     36      accessible.cache.getStringProperty(attribute);
     37      return true;
     38    } catch (e) {
     39      return false;
     40    }
     41  }, `${attribute} is present in the cache`);
     42 }
     43 
     44 /**
     45 * Verifies that the given attribute is cached on the given acc. Doesn't retry
     46 * until a timeout.
     47 *
     48 * @param {nsIAccessible} accessible the accessible where the attribute to query
     49 *                                   should be cached
     50 * @param {string}        attribute  the attribute to query in the cache
     51 */
     52 function verifyAttributeCachedNoRetry(accessible, attribute) {
     53  try {
     54    accessible.cache.getStringProperty(attribute);
     55    ok(true, `${attribute} is present in the cache`);
     56  } catch (e) {
     57    ok(false, `${attribute} is not present in the cache`);
     58  }
     59 }
     60 
     61 /**
     62 * @callback QueryCallback A function taking no arguments that queries an
     63 *                         attribute that may be cached, e.g., bounds, state
     64 */
     65 
     66 /**
     67 * Verifies that the given attribute isn't cached. Then, forces the
     68 * accessibility service to activate those cache domains by running the provided
     69 * query function, which queries the attribute. Finally, verifies that the
     70 * attribute is present in the cache.
     71 *
     72 * @param  {nsIAccessible}  accessible the accessible where the attribute to
     73 *                                     query should be cached
     74 * @param  {string}         attribute  the attribute to query in the cache
     75 * @param  {QueryCallback}  queryCb    the callback that this function will
     76 *                                     invoke to query the given attribute
     77 */
     78 async function testAttributeCachePresence(accessible, attribute, queryCb) {
     79  // Verify that the attribute isn't cached.
     80  let hasAttribute;
     81  try {
     82    accessible.cache.getStringProperty(attribute);
     83    hasAttribute = true;
     84  } catch (e) {
     85    hasAttribute = false;
     86  }
     87  ok(!hasAttribute, `${attribute} is not present in cache`);
     88 
     89  // Force attribute to be cached by querying it.
     90  info(`Querying ${attribute} in cache`);
     91  queryCb();
     92 
     93  // Wait until the attribute is present in the cache.
     94  await verifyAttributeCached(accessible, attribute);
     95 }
     96 
     97 /**
     98 * Verify that the given attribute is properly cached, taking into account
     99 * platform considerations which may affect what is testable. Ideally, test
    100 * attribute absence and presence, but only presence may be possible.
    101 *
    102 * @param  {nsIAccessible}  accessible the accessible where the attribute to
    103 *                                     query should be cached
    104 * @param  {string}         attribute  the attribute to query in the cache
    105 * @param  {QueryCallback}  queryCb    the callback that this function will
    106 *                                     invoke to query the given attribute
    107 */
    108 async function testCachingPerPlatform(accessible, attribute, queryCb) {
    109  // On Linux, the implementation of PlatformEvent for EVENT_NAME_CHANGE calls
    110  // RemoteAccessible::Name during the test setup, which unavoidably queries the
    111  // Text cache domain. Therefore, on Linux we avoid checking for the absence of
    112  // the Text domain attributes. Similarly, we cache the viewport on Linux
    113  // before a test is ready to run.
    114  if (
    115    AppConstants.platform == "linux" &&
    116    (attribute == "language" ||
    117      attribute == "text" ||
    118      attribute == "style" ||
    119      attribute == "viewport")
    120  ) {
    121    queryCb();
    122    await verifyAttributeCached(accessible, attribute);
    123  } else if (
    124    AppConstants.platform == "macosx" &&
    125    (attribute == "headers" ||
    126      attribute == "colspan" ||
    127      attribute == "rowspan" ||
    128      attribute == "layout-guess" ||
    129      attribute == "language" ||
    130      attribute == "text" ||
    131      attribute == "style" ||
    132      attribute == "viewport")
    133  ) {
    134    // These attributes work on macOS, but aren't consistent. Events may happen
    135    // before document load complete that cause caching before the test starts.
    136    // So, in the (common) event that that doesn't happen, call the queryCb to
    137    // ensure the necessary cache request happens. See bug 1916578.
    138    queryCb();
    139    await verifyAttributeCached(accessible, attribute);
    140  } else {
    141    await testAttributeCachePresence(accessible, attribute, queryCb);
    142  }
    143 }