chromium/third_party/blink/web_tests/fast/events/contextmenu-follows-focus.html

<!doctype html>
<script src="../../resources/testharness.js"></script>
<script src="../../resources/testharnessreport.js"></script>
<script>
var contextForMenu;
function catchContextMenu(event) {
  contextForMenu = event.currentTarget.tagName;
}

function focusLinkAndAssertLinkIsTargetOfContextMenu() {
  eventSender.keyDown('Escape'); // Hide menu.
  document.querySelector('A').focus();
  contextForMenu = undefined;
  eventSender.keyDown('ContextMenu');
  assert_equals(contextForMenu, 'A',
      'ContextMenu should use the focused link as target.');
}
</script>

<div contenteditable oncontextmenu="catchContextMenu(event);">Some editable text.</div>
<span oncontextmenu="catchContextMenu(event);">Some text to select.</span>
<input oncontextmenu="catchContextMenu(event);">
<a href="www" oncontextmenu="catchContextMenu(event);">A<br>link</a>

<script>
test(function() {
  assert_own_property(window, 'eventSender', 'This test requires eventSender.');

  document.querySelector('INPUT').focus();
  eventSender.keyDown('ContextMenu');
  assert_equals(contextForMenu, 'INPUT',
      'ContextMenu should use the focused input field as target.');
  focusLinkAndAssertLinkIsTargetOfContextMenu();
}, 'ContextMenu should target the focused link (not the unfocused field).');

test(function() {
  assert_own_property(window, 'eventSender', 'This test requires eventSender.');

  document.querySelector('div').focus();
  eventSender.keyDown('ContextMenu');
  assert_equals(contextForMenu, 'DIV',
      'ContextMenu should use the editable div\'s caret as target.');
  focusLinkAndAssertLinkIsTargetOfContextMenu();
}, 'ContextMenu should target the focused link (not the div\'s caret).');

test(function() {
  assert_own_property(window, 'eventSender', 'This test requires eventSender.');

  const div = document.querySelector('div');
  div.focus();
  window.getSelection().selectAllChildren(div);
  eventSender.keyDown('ContextMenu');
  assert_equals(contextForMenu, 'DIV',
      'ContextMenu should use the editable div\'s range selection as target.');
  focusLinkAndAssertLinkIsTargetOfContextMenu();
}, 'ContextMenu should target the focused link (not the div\'s selection).');

test(function() {
  assert_own_property(window, 'eventSender', 'This test requires eventSender.');

  const span = document.querySelector('span');
  window.getSelection().selectAllChildren(span);
  focusLinkAndAssertLinkIsTargetOfContextMenu();
}, 'ContextMenu should target the focused link (not the unfocused selection).');
</script>