summaryrefslogtreecommitdiffstats
path: root/dom/html/test/forms/test_input_date_key_events.html
blob: cd974e50570eee53fcf553f87648c9b96bb275b8 (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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=1286182
-->
<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=1286182">Mozilla Bug 1286182</a>
<p id="display"></p>
<div id="content">
  <input id="input" type="date">
</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 11222016, default order is month, day, year.
    keys: ["11222016"],
    initialVal: "",
    expectedVal: "2016-11-22"
  },
  {
    // Type 3 in the month field will automatically advance to the day field,
    // then type 5 in the day field will automatically advance to the year
    // field.
    keys: ["352016"],
    initialVal: "",
    expectedVal: "2016-03-05"
  },
  {
    // Type 13 in the month field  will set it to the maximum month, which is
    // 12.
    keys: ["13012016"],
    initialVal: "",
    expectedVal: "2016-12-01"
  },
  {
    // Type 00 in the month field will set it to the minimum month, which is 1.
    keys: ["00012016"],
    initialVal: "",
    expectedVal: "2016-01-01"
  },
  {
    // Type 33 in the day field will set it to the maximum day, which is 31.
    keys: ["12332016"],
    initialVal: "",
    expectedVal: "2016-12-31"
  },
  {
    // Type 00 in the day field will set it to the minimum day, which is 1.
    keys: ["12002016"],
    initialVal: "",
    expectedVal: "2016-12-01"
  },
  {
    // Type 275769 in the year field will set it to the maximum year, which is
    // 275760.
    keys: ["0101275769"],
    initialVal: "",
    expectedVal: "275760-01-01"
  },
  {
    // Type 000000 in the year field will set it to the minimum year, which is
    // 0001.
    keys: ["0101000000"],
    initialVal: "",
    expectedVal: "0001-01-01"
  },
  {
    // Advance to year field and decrement.
    keys: ["VK_TAB", "VK_TAB", "VK_DOWN"],
    initialVal: "2016-11-25",
    expectedVal: "2015-11-25"
  },
  {
    // Right key should do the same thing as TAB key.
    keys: ["VK_RIGHT", "VK_RIGHT", "VK_DOWN"],
    initialVal: "2016-11-25",
    expectedVal: "2015-11-25"
  },
  {
    // Advance to day field then back to month field and decrement.
    keys: ["VK_RIGHT", "VK_LEFT", "VK_DOWN"],
    initialVal: "2000-05-01",
    expectedVal: "2000-04-01"
  },
  {
    // Focus starts on the first field, month in this case, and increment.
    keys: ["VK_UP"],
    initialVal: "2000-03-01",
    expectedVal: "2000-04-01"
  },
  {
    // Advance to day field and decrement.
    keys: ["VK_TAB", "VK_DOWN"],
    initialVal: "1234-01-01",
    expectedVal: "1234-01-31"
  },
  {
    // Advance to day field and increment.
    keys: ["VK_TAB", "VK_UP"],
    initialVal: "1234-01-01",
    expectedVal: "1234-01-02"
  },
  {
    // PageUp on month field increments month by 3.
    keys: ["VK_PAGE_UP"],
    initialVal: "1999-01-01",
    expectedVal: "1999-04-01"
  },
  {
    // PageDown on month field decrements month by 3.
    keys: ["VK_PAGE_DOWN"],
    initialVal: "1999-01-01",
    expectedVal: "1999-10-01"
  },
  {
    // PageUp on day field increments day by 7.
    keys: ["VK_TAB", "VK_PAGE_UP"],
    initialVal: "1999-01-01",
    expectedVal: "1999-01-08"
  },
  {
    // PageDown on day field decrements day by 7.
    keys: ["VK_TAB", "VK_PAGE_DOWN"],
    initialVal: "1999-01-01",
    expectedVal: "1999-01-25"
  },
  {
    // PageUp on year field increments year by 10.
    keys: ["VK_TAB", "VK_TAB", "VK_PAGE_UP"],
    initialVal: "1999-01-01",
    expectedVal: "2009-01-01"
  },
  {
    // PageDown on year field decrements year by 10.
    keys: ["VK_TAB", "VK_TAB", "VK_PAGE_DOWN"],
    initialVal: "1999-01-01",
    expectedVal: "1989-01-01"
  },
  {
    // Home key on month field sets it to the minimum month, which is 01.
    keys: ["VK_HOME"],
    initialVal: "2016-06-01",
    expectedVal: "2016-01-01"
  },
  {
    // End key on month field sets it to the maximum month, which is 12.
    keys: ["VK_END"],
    initialVal: "2016-06-01",
    expectedVal: "2016-12-01"
  },
  {
    // Home key on day field sets it to the minimum day, which is 01.
    keys: ["VK_TAB", "VK_HOME"],
    initialVal: "2016-01-10",
    expectedVal: "2016-01-01"
  },
  {
    // End key on day field sets it to the maximum day, which is 31.
    keys: ["VK_TAB", "VK_END"],
    initialVal: "2016-01-10",
    expectedVal: "2016-01-31"
  },
  {
    // Home key on year field sets it to the minimum year, which is 0001.
    keys: ["VK_TAB", "VK_TAB", "VK_HOME"],
    initialVal: "2016-01-01",
    expectedVal: "0001-01-01"
  },
  {
    // End key on year field sets it to the maximum year, which is 275760.
    keys: ["VK_TAB", "VK_TAB", "VK_END"],
    initialVal: "2016-01-01",
    expectedVal: "275760-01-01"
  },
];

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>