summaryrefslogtreecommitdiffstats
path: root/dom/html/test/forms/test_input_time_key_events.html
blob: 755db38ffc38e1b1a14e19cb2d6270418ab761ba (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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=1288591
-->
<head>
  <title>Test key events for time control</title>
  <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  <script type="text/javascript" src="/tests/SimpleTest/EventUtils.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
  <meta charset="UTF-8">
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1288591">Mozilla Bug 1288591</a>
<p id="display"></p>
<div id="content">
  <input id="input" type="time">
</div>
<pre id="test">
<script type="application/javascript">

SimpleTest.waitForExplicitFinish();
// Turn off Spatial Navigation because it hijacks arrow keydown events:
SimpleTest.waitForFocus(function() {
  SpecialPowers.pushPrefEnv({"set":[["snav.enabled", false]]}, function() {
    test();
    SimpleTest.finish();
  });
});

var testData = [
  /**
   * keys: keys to send to the input element.
   * initialVal: initial value set to the input element.
   * expectedVal: expected value of the input element after sending the keys.
   */
  {
    // Type 1030 and select AM.
    keys: ["1030", "VK_DOWN"],
    initialVal: "",
    expectedVal: "10:30"
  },
  {
    // Type 3 in the hour field will automatically advance to the minute field.
    keys: ["330", "VK_DOWN"],
    initialVal: "",
    expectedVal: "03:30"
  },
  {
    // Type 5 in the hour field will automatically advance to the minute field.
    // Type 7 in the minute field will automatically advance to the AM/PM field.
    keys: ["57", "VK_DOWN"],
    initialVal: "",
    expectedVal: "05:07"
  },
  {
    // Advance to AM/PM field and change to PM.
    keys: ["VK_TAB", "VK_TAB", "VK_DOWN"],
    initialVal: "10:30",
    expectedVal: "22:30"
  },
  {
    // Right key should do the same thing as TAB key.
    keys: ["VK_RIGHT", "VK_RIGHT", "VK_DOWN"],
    initialVal: "10:30",
    expectedVal: "22:30"
  },
  {
    // Advance to minute field then back to hour field and decrement.
    keys: ["VK_RIGHT", "VK_LEFT", "VK_DOWN"],
    initialVal: "10:30",
    expectedVal: "09:30"
  },
  {
    // Focus starts on the first field, hour in this case, and increment.
    keys: ["VK_UP"],
    initialVal: "16:00",
    expectedVal: "17:00"
  },
  {
    // Advance to minute field and decrement.
    keys: ["VK_TAB", "VK_DOWN"],
    initialVal: "16:00",
    expectedVal: "16:59"
  },
  {
    // Advance to minute field and increment.
    keys: ["VK_TAB", "VK_UP"],
    initialVal: "16:59",
    expectedVal: "16:00"
  },
  {
    // PageUp on hour field increments hour by 3.
    keys: ["VK_PAGE_UP"],
    initialVal: "05:00",
    expectedVal: "08:00"
  },
  {
    // PageDown on hour field decrements hour by 3.
    keys: ["VK_PAGE_DOWN"],
    initialVal: "05:00",
    expectedVal: "02:00"
  },
  {
    // PageUp on minute field increments minute by 10.
    keys: ["VK_TAB", "VK_PAGE_UP"],
    initialVal: "14:00",
    expectedVal: "14:10"
  },
  {
    // PageDown on minute field decrements minute by 10.
    keys: ["VK_TAB", "VK_PAGE_DOWN"],
    initialVal: "14:00",
    expectedVal: "14:50"
  },
  {
    // Home key on hour field sets it to the minimum hour, which is 1 in 12-hour
    // clock.
    keys: ["VK_HOME"],
    initialVal: "03:10",
    expectedVal: "01:10"
  },
  {
    // End key on hour field sets it to the maximum hour, which is 12 in 12-hour
    // clock.
    keys: ["VK_END"],
    initialVal: "03:10",
    expectedVal: "00:10"
  },
  {
    // Home key on minute field sets it to the minimum minute, which is 0.
    keys: ["VK_TAB", "VK_HOME"],
    initialVal: "19:30",
    expectedVal: "19:00"
  },
  {
    // End key on minute field sets it to the minimum minute, which is 59.
    keys: ["VK_TAB", "VK_END"],
    initialVal: "19:30",
    expectedVal: "19:59"
  },
  // Second field will show up when needed.
  {
    // PageUp on second field increments second by 10.
    keys: ["VK_TAB", "VK_TAB", "VK_PAGE_UP"],
    initialVal: "08:10:10",
    expectedVal: "08:10:20"
  },
  {
    // PageDown on second field increments second by 10.
    keys: ["VK_TAB", "VK_TAB", "VK_PAGE_DOWN"],
    initialVal: "08:10:10",
    expectedVal: "08:10:00"
  },
  {
    // Home key on second field sets it to the minimum second, which is 0.
    keys: ["VK_TAB", "VK_TAB", "VK_HOME"],
    initialVal: "16:00:30",
    expectedVal: "16:00:00"
  },
  {
    // End key on second field sets it to the minimum second, which is 59.
    keys: ["VK_TAB", "VK_TAB", "VK_END"],
    initialVal: "16:00:30",
    expectedVal: "16:00:59"
  },
];

function sendKeys(aKeys) {
  for (let i = 0; i < aKeys.length; i++) {
    let key = aKeys[i];
    if (key.startsWith("VK")) {
      synthesizeKey(key, {});
    } else {
      sendString(key);
    }
  }
}

function test() {
  var elem = document.getElementById("input");

  for (let { keys, initialVal, expectedVal } of testData) {
    elem.focus();
    elem.value = initialVal;
    sendKeys(keys);
    elem.blur();
    is(elem.value, expectedVal,
       "Test with " + keys + ", result should be " + expectedVal);
    elem.value = "";
  }
}

</script>
</pre>
</body>
</html>