summaryrefslogtreecommitdiffstats
path: root/dom/base/test/useractivation/test_useractivation_key_events.html
blob: d97906c22bcf012ea4ec4102ae0b0c6f3b3bd48e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<!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>