summaryrefslogtreecommitdiffstats
path: root/widget/tests/test_keypress_event_with_alt_on_mac.html
blob: 01d4100f97849a876f77d645391cd4159f0ded6c (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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<!DOCTYPE html>
<html>
<head>
  <title>Testing if keypress event is fired when alt key is pressed</title>
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <script src="/tests/SimpleTest/EventUtils.js"></script>
  <script src="/tests/SimpleTest/NativeKeyCodes.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css">
</head>
<body>
<div id="display">
  <input id="input">
  <input id="password" type="password">
  <input id="readonly-input" readonly>
  <textarea id="textarea"></textarea>
  <textarea id="readonly-textarea" readonly></textarea>
  <button id="button">button</button>
</div>
<div id="content" style="display: none">
</div>
<pre id="test">
</pre>

<script class="testbody" type="application/javascript">
SimpleTest.waitForExplicitFinish();

async function testNativeKey(aKeyboardLayout, aNativeKeyCode, aModifiers,
                             aChars, aUnmodifiedChars) {
  // XXX Need to listen keyup event here because synthesizeNativeKey() does not
  //     guarantee that its callback will be called after "keypress" and "keyup".
  let waitForKeyUp = new Promise(resolve => {
    document.addEventListener("keyup", resolve, {once: true});
  });
  let keypress;
  document.addEventListener("keypress", (aKeyPressEvent) => {
    keypress = aKeyPressEvent;
  }, {once: true});
  synthesizeNativeKey(aKeyboardLayout, aNativeKeyCode, aModifiers, aChars, aUnmodifiedChars);
  await waitForKeyUp;
  return keypress;
}

async function runTests() {
  const kTests =
    [ { target: "input", isEditable: true },
      { target: "password", isEditable: true },
      { target: "readonly-input", isEditable: false },
      { target: "textarea", isEditable: true },
      { target: "readonly-textarea", isEditable: false },
      { target: "button", isEditable: false } ];
  for (const kTest of kTests) {
    let element = document.getElementById(kTest.target);
    element.focus();

    const kDescription = kTest.target + ": ";

    let keypress = await testNativeKey(KEYBOARD_LAYOUT_EN_US, MAC_VK_ANSI_A, {}, "a", "a");
    ok(keypress,
       kDescription + "'a' key press should cause firing keypress event");

    keypress = await testNativeKey(KEYBOARD_LAYOUT_EN_US, MAC_VK_ANSI_A, {shiftKey: true}, "A", "A");
    ok(keypress,
       kDescription + "'a' key press with shift key should cause firing keypress event");
    ok(keypress.shiftKey,
       kDescription + "shiftKey of 'a' key press with shift key should be true");

    // When a key inputs a character with option key, we need to unset altKey for our editor.
    // Otherwise, altKey should be true for compatibility with the other browsers.
    keypress = await testNativeKey(KEYBOARD_LAYOUT_EN_US, MAC_VK_ANSI_A, {altKey: true}, "\u00E5", "a");
    ok(keypress,
       kDescription + "'a' key press with option key should cause firing keypress event");
    is(keypress.altKey, !kTest.isEditable,
       kDescription + "altKey of 'a' key press with option key should be " + !kTest.isEditable);

    keypress = await testNativeKey(KEYBOARD_LAYOUT_EN_US, MAC_VK_ANSI_A, {altKey: true, shiftKey: true}, "\u00C5", "A");
    ok(keypress,
       kDescription + "'a' key press with option key  and shift key should cause firing keypress event");
    is(keypress.altKey, !kTest.isEditable,
       kDescription + "altKey of 'a' key press with option key and shift key should be " + !kTest.isEditable);
    ok(keypress.shiftKey,
       kDescription + "shiftKey of 'a' key press with option key and shift key should be true");

    keypress = await testNativeKey(KEYBOARD_LAYOUT_EN_US, MAC_VK_ANSI_A, {ctrlKey: true}, "\u0001", "a");
    ok(!keypress,
       kDescription + "'a' key press with control key should not cause firing keypress event");

    keypress = await testNativeKey(KEYBOARD_LAYOUT_EN_US, MAC_VK_ANSI_A, {altKey: true, ctrlKey: true}, "\u0001", "a");
    ok(!keypress,
       kDescription + "'a' key press with option key and control key should not cause firing keypress event");

    // XXX Cannot test with command key for now since keyup event won't be fired due to macOS's limitation.

    // Some keys of Arabic - PC keyboard layout do not input any character with option key.
    // In such case, we shouldn't dispatch keypress event.
    keypress = await testNativeKey(KEYBOARD_LAYOUT_ARABIC_PC, MAC_VK_ANSI_7, {altKey: true}, "", "\u0667");
    ok(!keypress,
       kDescription + "'7' key press with option key should not cause firing keypress event");
  }

  SimpleTest.finish();
}

SimpleTest.waitForFocus(runTests);
</script>
</body>
</html>