<!DOCTYPE HTML>
<html>
<head>
  <title>User activation test: key events</title>
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <script src="/tests/SimpleTest/EventUtils.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
<script>

function synthesizeKeyAndWait(aKey, aEvent) {
  let promise = new Promise(aResolve => {
    document.addEventListener("keydown", function(e) {
      e.preventDefault();
      aResolve();
    }, { once: true });
  });
  synthesizeKey(aKey, aEvent, window);
  return promise;
}

add_task(async function TestPrintableKey() {
  let tests = [ 'a', 'b', 'c', 'A', 'B', '1', '2', '3' ];

  for (let key of tests) {
    SpecialPowers.wrap(document).clearUserGestureActivation();
    await synthesizeKeyAndWait(key, {});
    ok(SpecialPowers.wrap(document).hasBeenUserGestureActivated,
       `check has-been-user-activated for ${key}`);
    ok(SpecialPowers.wrap(document).hasValidTransientUserGestureActivation,
       `check has-valid-transient-user-activation for ${key}`);
  }
});

add_task(async function TestNonPrintableKey() {
  let tests = [ [ "KEY_Alt", false],
                [ "KEY_Backspace", false],
                [ "KEY_Escape" , false ],
                [ "KEY_Tab" , false ],
                // Treat as user input
                [ "KEY_Enter", true],
                [ " ", true] ];

  for (let [key, expectedResult] of tests) {
    SpecialPowers.wrap(document).clearUserGestureActivation();
    await synthesizeKeyAndWait(key, {});
    is(SpecialPowers.wrap(document).hasBeenUserGestureActivated, expectedResult,
       `check has-been-user-activated for "${key}"`);
    is(SpecialPowers.wrap(document).hasValidTransientUserGestureActivation, expectedResult,
       `check has-valid-transient-user-activation for "${key}"`);
  }
});

add_task(async function TestModifier() {
  let tests = [ [ 'a', { altKey: true }, false],
                [ 'a', { ctrlKey: true }, false],
                [ 'a', { metaKey: true }, false],
                [ 'a', { osKey: true }, false],
                [ 'c', { altKey: true }, false ],
                [ 'c', { osKey: true }, false ],
                [ 'v', { altKey: true }, false ],
                [ 'v', { osKey: true }, false ],
                [ 'x', { altKey: true }, false ],
                [ 'x', { osKey: true }, false ],
                // Treat as user input
                [ 'a', { altGraphKey: true }, true ],
                [ 'a', { fnKey: true }, true ],
                [ 'a', { shiftKey: true }, true ],
                [ 'c', { accelKey: true }, true ],
                [ 'v', { accelKey: true }, true ],
                [ 'x', { accelKey: true }, true ] ];

  for (let [key, event, expectedResult] of tests) {
    SpecialPowers.wrap(document).clearUserGestureActivation();
    await synthesizeKeyAndWait(key, event);
    is(SpecialPowers.wrap(document).hasBeenUserGestureActivated, expectedResult,
       `check has-been-user-activated for ${key} with ${JSON.stringify(event)}`);
    is(SpecialPowers.wrap(document).hasValidTransientUserGestureActivation, expectedResult,
       `check has-valid-transient-user-activation for ${key} with ${JSON.stringify(event)}`);
  }
});

add_task(async function endTests() {
  // Reset the activation flag in order not to interfere following test in the
  // verify mode which would run the test using same document couple times.
  SpecialPowers.wrap(document).clearUserGestureActivation();
});

</script>
</body>
