aboutsummaryrefslogtreecommitdiff
path: root/contexts/data/lib/closure-library/closure/goog/events/keycodes_test.html
blob: 68a274f1d9da6a49e0708968ac6207908b121196 (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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<!DOCTYPE html>
<html>
<!--
Copyright 2010 The Closure Library Authors. All Rights Reserved.

Use of this source code is governed by the Apache License, Version 2.0.
See the COPYING file for details.

Author: nicksantos@google.com (Nick Santos)
-->
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>JsUnit tests for goog.events.KeyCodes</title>
<script src="../base.js"></script>
<script>

goog.require('goog.events.BrowserEvent');
goog.require('goog.events.KeyCodes');
goog.require('goog.object');
goog.require('goog.testing.jsunit');


</script>
</head>
<body>

<script>

var KeyCodes = goog.events.KeyCodes;

function testTextModifyingKeys() {
  var specialTextModifiers = goog.object.createSet(
      KeyCodes.BACKSPACE,
      KeyCodes.DELETE,
      KeyCodes.ENTER,
      KeyCodes.MAC_ENTER,
      KeyCodes.TAB,
      KeyCodes.WIN_IME);

  if (!goog.userAgent.GECKO) {
    specialTextModifiers[KeyCodes.WIN_KEY_FF_LINUX] = 1;
  }

  for (var keyId in KeyCodes) {
    var key = KeyCodes[keyId];
    if (goog.isFunction(key)) {
      // skip static methods
      continue;
    }

    var fakeEvent = createEventWithKeyCode(key);

    if (KeyCodes.isCharacterKey(key) || (key in specialTextModifiers)) {
      assertTrue('Expected key to modify text: ' + keyId,
          KeyCodes.isTextModifyingKeyEvent(fakeEvent));
    } else {
      assertFalse('Expected key to not modify text: ' + keyId,
          KeyCodes.isTextModifyingKeyEvent(fakeEvent));
    }
  }

  for (var i = KeyCodes.FIRST_MEDIA_KEY; i <= KeyCodes.LAST_MEDIA_KEY; i++) {
    var fakeEvent = createEventWithKeyCode(i);
    assertFalse('Expected key to not modify text: ' + i,
        KeyCodes.isTextModifyingKeyEvent(fakeEvent));
  }
}

function testKeyCodeZero() {
  var zeroEvent = createEventWithKeyCode(0);
  assertEquals(
      !goog.userAgent.GECKO,
      KeyCodes.isTextModifyingKeyEvent(zeroEvent));
  assertEquals(
      goog.userAgent.WEBKIT,
      KeyCodes.isCharacterKey(0));
}

function testPhantomKey() {
  // KeyCode 255 deserves its own test to make sure this does not regress,
  // because it's so weird. See the comments in the KeyCode enum.
  var fakeEvent = createEventWithKeyCode(goog.events.KeyCodes.PHANTOM);
  assertFalse('Expected phantom key to not modify text',
      KeyCodes.isTextModifyingKeyEvent(fakeEvent));
  assertFalse(KeyCodes.isCharacterKey(fakeEvent));
}

function testNonUsKeyboards() {
  var fakeEvent = createEventWithKeyCode(1092 /* Russian a */);
  assertTrue('Expected key to not modify text: 1092',
      KeyCodes.isTextModifyingKeyEvent(fakeEvent));
}

function createEventWithKeyCode(i) {
  var fakeEvent = new goog.events.BrowserEvent('keydown');
  fakeEvent.keyCode = i;
  return fakeEvent;
}

function testNormalizeGeckoKeyCode() {
  KeyCodes = goog.events.KeyCodes;

  // Test Gecko-specific key codes.
  assertEquals(
      goog.events.KeyCodes.normalizeGeckoKeyCode(KeyCodes.FF_EQUALS),
      KeyCodes.EQUALS);
  assertEquals(
      goog.events.KeyCodes.normalizeGeckoKeyCode(KeyCodes.FF_SEMICOLON),
      KeyCodes.SEMICOLON);
  assertEquals(
      goog.events.KeyCodes.normalizeGeckoKeyCode(KeyCodes.MAC_FF_META),
      KeyCodes.META);
  assertEquals(
      goog.events.KeyCodes.normalizeGeckoKeyCode(KeyCodes.WIN_KEY_FF_LINUX),
      KeyCodes.WIN_KEY);

  // Test general key codes.
  assertEquals(
      goog.events.KeyCodes.normalizeGeckoKeyCode(KeyCodes.COMMA),
      KeyCodes.COMMA);
}
</script>

</body>
</html>