tor-browser

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

browser_toggle_radio_check.js (8595B)


      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 /* import-globals-from ../../mochitest/states.js */
      9 loadScripts(
     10  { name: "role.js", dir: MOCHITESTS_DIR },
     11  { name: "states.js", dir: MOCHITESTS_DIR }
     12 );
     13 
     14 /**
     15 * Test input[type=checkbox]
     16 */
     17 addAccessibleTask(
     18  `<input type="checkbox" id="vehicle"><label for="vehicle"> Bike</label>`,
     19  async (browser, accDoc) => {
     20    let checkbox = getNativeInterface(accDoc, "vehicle");
     21    await untilCacheIs(
     22      () => checkbox.getAttributeValue("AXValue"),
     23      0,
     24      "Correct initial value"
     25    );
     26 
     27    let actions = checkbox.actionNames;
     28    ok(actions.includes("AXPress"), "Has press action");
     29 
     30    let evt = waitForMacEvent("AXValueChanged", "vehicle");
     31    checkbox.performAction("AXPress");
     32    await evt;
     33    await untilCacheIs(
     34      () => checkbox.getAttributeValue("AXValue"),
     35      1,
     36      "Correct checked value"
     37    );
     38 
     39    evt = waitForMacEvent("AXValueChanged", "vehicle");
     40    checkbox.performAction("AXPress");
     41    await evt;
     42    await untilCacheIs(
     43      () => checkbox.getAttributeValue("AXValue"),
     44      0,
     45      "Correct checked value"
     46    );
     47  }
     48 );
     49 
     50 /**
     51 * Test aria-pressed toggle buttons
     52 */
     53 addAccessibleTask(
     54  `<button id="toggle" aria-pressed="false">toggle</button>`,
     55  async (browser, accDoc) => {
     56    // Set up a callback to change the toggle value
     57    await SpecialPowers.spawn(browser, [], () => {
     58      content.document.getElementById("toggle").onclick = e => {
     59        let curVal = e.target.getAttribute("aria-pressed");
     60        let nextVal = curVal == "false" ? "true" : "false";
     61        e.target.setAttribute("aria-pressed", nextVal);
     62      };
     63    });
     64 
     65    let toggle = getNativeInterface(accDoc, "toggle");
     66    await untilCacheIs(
     67      () => toggle.getAttributeValue("AXValue"),
     68      0,
     69      "Correct initial value"
     70    );
     71 
     72    let actions = toggle.actionNames;
     73    ok(actions.includes("AXPress"), "Has press action");
     74 
     75    let evt = waitForMacEvent("AXValueChanged", "toggle");
     76    toggle.performAction("AXPress");
     77    await evt;
     78    await untilCacheIs(
     79      () => toggle.getAttributeValue("AXValue"),
     80      1,
     81      "Correct checked value"
     82    );
     83 
     84    evt = waitForMacEvent("AXValueChanged", "toggle");
     85    toggle.performAction("AXPress");
     86    await evt;
     87    await untilCacheIs(
     88      () => toggle.getAttributeValue("AXValue"),
     89      0,
     90      "Correct checked value"
     91    );
     92  }
     93 );
     94 
     95 /**
     96 * Test aria-checked with tri state
     97 */
     98 addAccessibleTask(
     99  `<button role="checkbox" id="checkbox" aria-checked="false">toggle</button>`,
    100  async (browser, accDoc) => {
    101    // Set up a callback to change the toggle value
    102    await SpecialPowers.spawn(browser, [], () => {
    103      content.document.getElementById("checkbox").onclick = e => {
    104        const states = ["false", "true", "mixed"];
    105        let currState = e.target.getAttribute("aria-checked");
    106        let nextState = states[(states.indexOf(currState) + 1) % states.length];
    107        e.target.setAttribute("aria-checked", nextState);
    108      };
    109    });
    110    let checkbox = getNativeInterface(accDoc, "checkbox");
    111    await untilCacheIs(
    112      () => checkbox.getAttributeValue("AXValue"),
    113      0,
    114      "Correct initial value"
    115    );
    116 
    117    let actions = checkbox.actionNames;
    118    ok(actions.includes("AXPress"), "Has press action");
    119 
    120    let evt = waitForMacEvent("AXValueChanged", "checkbox");
    121    checkbox.performAction("AXPress");
    122    await evt;
    123    await untilCacheIs(
    124      () => checkbox.getAttributeValue("AXValue"),
    125      1,
    126      "Correct checked value"
    127    );
    128 
    129    // Changing from checked to mixed fires two events. Make sure we wait until
    130    // the second so we're asserting based on the latest state.
    131    evt = waitForMacEvent("AXValueChanged", iface => {
    132      return (
    133        iface.getAttributeValue("AXDOMIdentifier") == "checkbox" &&
    134        iface.getAttributeValue("AXValue") == 2
    135      );
    136    });
    137    checkbox.performAction("AXPress");
    138    await evt;
    139    is(checkbox.getAttributeValue("AXValue"), 2, "Correct checked value");
    140  }
    141 );
    142 
    143 /**
    144 * Test input[type=radio]
    145 */
    146 addAccessibleTask(
    147  `<input type="radio" id="huey" name="drone" value="huey" checked>
    148   <label for="huey">Huey</label>
    149   <input type="radio" id="dewey" name="drone" value="dewey">
    150   <label for="dewey">Dewey</label>`,
    151  async (browser, accDoc) => {
    152    let huey = getNativeInterface(accDoc, "huey");
    153    await untilCacheIs(
    154      () => huey.getAttributeValue("AXValue"),
    155      1,
    156      "Correct initial value for huey"
    157    );
    158 
    159    let dewey = getNativeInterface(accDoc, "dewey");
    160    await untilCacheIs(
    161      () => dewey.getAttributeValue("AXValue"),
    162      0,
    163      "Correct initial value for dewey"
    164    );
    165 
    166    let actions = dewey.actionNames;
    167    ok(actions.includes("AXPress"), "Has press action");
    168 
    169    let evt = Promise.all([
    170      waitForMacEvent("AXValueChanged", "huey"),
    171      waitForMacEvent("AXValueChanged", "dewey"),
    172    ]);
    173    dewey.performAction("AXPress");
    174    await evt;
    175    await untilCacheIs(
    176      () => dewey.getAttributeValue("AXValue"),
    177      1,
    178      "Correct checked value for dewey"
    179    );
    180    await untilCacheIs(
    181      () => huey.getAttributeValue("AXValue"),
    182      0,
    183      "Correct checked value for huey"
    184    );
    185  }
    186 );
    187 
    188 /**
    189 * Test role=switch
    190 */
    191 addAccessibleTask(
    192  `<div role="switch" aria-checked="false" id="sw">hello</div>`,
    193  async (browser, accDoc) => {
    194    let sw = getNativeInterface(accDoc, "sw");
    195    await untilCacheIs(
    196      () => sw.getAttributeValue("AXValue"),
    197      0,
    198      "Initially switch is off"
    199    );
    200    is(sw.getAttributeValue("AXRole"), "AXCheckBox", "Has correct role");
    201    is(sw.getAttributeValue("AXSubrole"), "AXSwitch", "Has correct subrole");
    202 
    203    let stateChanged = Promise.all([
    204      waitForMacEvent("AXValueChanged", "sw"),
    205      waitForStateChange("sw", STATE_CHECKED, true),
    206    ]);
    207 
    208    // We should get a state change event, and a value change.
    209    await SpecialPowers.spawn(browser, [], () => {
    210      content.document
    211        .getElementById("sw")
    212        .setAttribute("aria-checked", "true");
    213    });
    214 
    215    await stateChanged;
    216 
    217    await untilCacheIs(
    218      () => sw.getAttributeValue("AXValue"),
    219      1,
    220      "Switch is now on"
    221    );
    222  }
    223 );
    224 
    225 /**
    226 * Test input[type=checkbox] with role=menuitemcheckbox
    227 */
    228 addAccessibleTask(
    229  `<input type="checkbox" role="menuitemcheckbox" id="vehicle"><label for="vehicle"> Bike</label>`,
    230  async (browser, accDoc) => {
    231    let checkbox = getNativeInterface(accDoc, "vehicle");
    232    await untilCacheIs(
    233      () => checkbox.getAttributeValue("AXValue"),
    234      0,
    235      "Correct initial value"
    236    );
    237 
    238    let actions = checkbox.actionNames;
    239    ok(actions.includes("AXPress"), "Has press action");
    240 
    241    let evt = waitForMacEvent("AXValueChanged", "vehicle");
    242    checkbox.performAction("AXPress");
    243    await evt;
    244    await untilCacheIs(
    245      () => checkbox.getAttributeValue("AXValue"),
    246      1,
    247      "Correct checked value"
    248    );
    249 
    250    evt = waitForMacEvent("AXValueChanged", "vehicle");
    251    checkbox.performAction("AXPress");
    252    await evt;
    253    await untilCacheIs(
    254      () => checkbox.getAttributeValue("AXValue"),
    255      0,
    256      "Correct checked value"
    257    );
    258  }
    259 );
    260 
    261 /**
    262 * Test input[type=radio] with role=menuitemradio
    263 */
    264 addAccessibleTask(
    265  `<input type="radio" role="menuitemradio" id="huey" name="drone" value="huey" checked>
    266   <label for="huey">Huey</label>
    267   <input type="radio" role="menuitemradio" id="dewey" name="drone" value="dewey">
    268   <label for="dewey">Dewey</label>`,
    269  async (browser, accDoc) => {
    270    let huey = getNativeInterface(accDoc, "huey");
    271    await untilCacheIs(
    272      () => huey.getAttributeValue("AXValue"),
    273      1,
    274      "Correct initial value for huey"
    275    );
    276 
    277    let dewey = getNativeInterface(accDoc, "dewey");
    278    await untilCacheIs(
    279      () => dewey.getAttributeValue("AXValue"),
    280      0,
    281      "Correct initial value for dewey"
    282    );
    283 
    284    let actions = dewey.actionNames;
    285    ok(actions.includes("AXPress"), "Has press action");
    286 
    287    let evt = Promise.all([
    288      waitForMacEvent("AXValueChanged", "huey"),
    289      waitForMacEvent("AXValueChanged", "dewey"),
    290    ]);
    291    dewey.performAction("AXPress");
    292    await evt;
    293    await untilCacheIs(
    294      () => dewey.getAttributeValue("AXValue"),
    295      1,
    296      "Correct checked value for dewey"
    297    );
    298    await untilCacheIs(
    299      () => huey.getAttributeValue("AXValue"),
    300      0,
    301      "Correct checked value for huey"
    302    );
    303  }
    304 );