chromium/third_party/blink/web_tests/accessibility/aom-contextmenu-action.html

<!DOCTYPE HTML>
<script src="../resources/gc.js"></script>
<script src="../resources/testharness.js"></script>
<script src="../resources/testharnessreport.js"></script>
<script src="../http/tests/resources/permissions-helper.js"></script>
<script src="resources/aom-helper.js"></script>

<!--

Accessibility Object Model
Explainer: https://github.com/WICG/aom/blob/gh-pages/explainer.md
Spec: https://wicg.github.io/aom/spec/

-->
<script>
test(function(t) {
    assert_true(internals.runtimeFlags.accessibilityObjectModelEnabled);
}, "Make sure that Accessibility Object Model is enabled");
</script>

<button id="context_menu_target">Context Menu Target</button>

<script>
async_test(function(t) {
  enableAccessibilityEventsPermission().then(function() {
    var target = document.getElementById("context_menu_target");
    var axTarget = accessibilityController.accessibleElementById("context_menu_target");

    var gotDOMEvent = false;
    target.addEventListener("contextmenu", function(event) {
      event.preventDefault();
      gotDOMEvent = true;
    });
    var gotAccessibleEvent = false;
    target.accessibleNode.onaccessiblecontextmenu = function() {
      gotAccessibleEvent = true;
    };
    axTarget.showMenu();

    assert_true(gotAccessibleEvent);
    assert_true(gotDOMEvent);
    t.done();
  });
}, "AccessibleNode.onaccessiblecontextmenu");

test(function(t) {
  assert_true(internals.runtimeFlags.synthesizedKeyboardEventsForAccessibilityActionsEnabled);
}, "Make sure that keyboard event synthesis is enabled");

const axTarget =
    accessibilityController.accessibleElementById("context_menu_target");

promise_test(function(t) {
  return new Promise(resolve => {
    window.addEventListener('keydown', resolve);
    axTarget.showMenu();
  }).then(event => {
    assert_equals(event.type, "keydown");
    assert_equals(event.keyCode, 93);  // 93 = context menu key.
    assert_equals(event.target, document.getElementById("context_menu_target"));
  });
}, "Test for synthesized keydown event in context menu accessible action");

promise_test(function(t) {
  return new Promise(resolve => {
    window.addEventListener('contextmenu', resolve);
    axTarget.showMenu();
  }).then(event => {
    event.preventDefault();
    return new Promise(resolve => {
      window.addEventListener('keyup', resolve);
    });
  }).then(event => {
    assert_equals(event.type, "keyup");
    assert_equals(event.keyCode, 93);  // 93 = context menu key.
    assert_equals(event.target, document.getElementById("context_menu_target"));
  });
}, "Test for synthesized keyup event in context menu accessible action after non-default contextmenu event");

async_test(function(t) {
    let gotContextMenuEvent = false;
    function contextMenuListener(e) {
      // If we called `e.preventDefault()` here, the test would fail.
      gotContextMenuEvent = true;
    }
    window.addEventListener('contextmenu', contextMenuListener);

    let gotKeyUpEvent = false;
    function keyupListener(e) {
      gotKeyUpEvent = true;
    }
    window.addEventListener('keyup', keyupListener);

    axTarget.showMenu();

    assert_true(gotContextMenuEvent);
    assert_false(gotKeyUpEvent);

    window.removeEventListener('contextmenu', contextMenuListener);
    window.removeEventListener('keyup', keyupListener);
    t.done();
}, "Test for no synthesized keyup event in context menu accessible action after default contextmenu event");
</script>