Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | <!DOCTYPE HTML> |
michael@0 | 2 | <html> |
michael@0 | 3 | <head> |
michael@0 | 4 | <title>Test for Form History Autocomplete</title> |
michael@0 | 5 | <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script> |
michael@0 | 6 | <script type="text/javascript" src="/tests/SimpleTest/EventUtils.js"></script> |
michael@0 | 7 | <script type="text/javascript" src="satchel_common.js"></script> |
michael@0 | 8 | <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" /> |
michael@0 | 9 | </head> |
michael@0 | 10 | <body> |
michael@0 | 11 | Form History test: form field autocomplete |
michael@0 | 12 | <p id="display"></p> |
michael@0 | 13 | |
michael@0 | 14 | <!-- we presumably can't hide the content for this test. --> |
michael@0 | 15 | <div id="content" style="direction: rtl;"> |
michael@0 | 16 | <!-- unused --> |
michael@0 | 17 | <form id="unused" onsubmit="return false;"> |
michael@0 | 18 | <input type="text" name="field1" value="unused"> |
michael@0 | 19 | </form> |
michael@0 | 20 | |
michael@0 | 21 | <!-- normal, basic form --> |
michael@0 | 22 | <form id="form1" onsubmit="return false;"> |
michael@0 | 23 | <input type="text" name="field1"> |
michael@0 | 24 | <button type="submit">Submit</button> |
michael@0 | 25 | </form> |
michael@0 | 26 | </div> |
michael@0 | 27 | |
michael@0 | 28 | <pre id="test"> |
michael@0 | 29 | <script class="testbody" type="text/javascript"> |
michael@0 | 30 | |
michael@0 | 31 | /** Test for Form History autocomplete **/ |
michael@0 | 32 | |
michael@0 | 33 | var autocompletePopup = getAutocompletePopup(); |
michael@0 | 34 | autocompletePopup.style.direction = "ltr"; |
michael@0 | 35 | |
michael@0 | 36 | var input = $_(1, "field1"); |
michael@0 | 37 | |
michael@0 | 38 | // Get the form history service |
michael@0 | 39 | function setupFormHistory(aCallback) { |
michael@0 | 40 | updateFormHistory([ |
michael@0 | 41 | { op : "remove" }, |
michael@0 | 42 | { op : "add", fieldname : "field1", value : "value1" }, |
michael@0 | 43 | { op : "add", fieldname : "field1", value : "value2" }, |
michael@0 | 44 | { op : "add", fieldname : "field1", value : "value3" }, |
michael@0 | 45 | { op : "add", fieldname : "field1", value : "value4" }, |
michael@0 | 46 | { op : "add", fieldname : "field1", value : "value5" }, |
michael@0 | 47 | { op : "add", fieldname : "field1", value : "value6" }, |
michael@0 | 48 | { op : "add", fieldname : "field1", value : "value7" }, |
michael@0 | 49 | { op : "add", fieldname : "field1", value : "value8" }, |
michael@0 | 50 | { op : "add", fieldname : "field1", value : "value9" }, |
michael@0 | 51 | ], aCallback); |
michael@0 | 52 | } |
michael@0 | 53 | |
michael@0 | 54 | function checkForm(expectedValue) { |
michael@0 | 55 | var formID = input.parentNode.id; |
michael@0 | 56 | is(input.value, expectedValue, "Checking " + formID + " input"); |
michael@0 | 57 | } |
michael@0 | 58 | |
michael@0 | 59 | function checkPopupOpen(isOpen, expectedIndex) { |
michael@0 | 60 | var actuallyOpen = autocompletePopup.popupOpen; |
michael@0 | 61 | var actualIndex = autocompletePopup.selectedIndex; |
michael@0 | 62 | is(actuallyOpen, isOpen, "popup should be " + (isOpen ? "open" : "closed")); |
michael@0 | 63 | if (isOpen) |
michael@0 | 64 | is(actualIndex, expectedIndex, "checking selected index"); |
michael@0 | 65 | |
michael@0 | 66 | // Correct state if needed, so following tests run as expected. |
michael@0 | 67 | if (actuallyOpen != isOpen) { |
michael@0 | 68 | if (isOpen) |
michael@0 | 69 | autocompletePopup.openPopup(); |
michael@0 | 70 | else |
michael@0 | 71 | autocompletePopup.closePopup(); |
michael@0 | 72 | } |
michael@0 | 73 | if (isOpen && actualIndex != expectedIndex) |
michael@0 | 74 | autocompletePopup.selectedIndex = expectedIndex; |
michael@0 | 75 | } |
michael@0 | 76 | |
michael@0 | 77 | function doKeyUnprivileged(key) { |
michael@0 | 78 | var keyName = "DOM_VK_" + key.toUpperCase(); |
michael@0 | 79 | var keycode, charcode, alwaysVal; |
michael@0 | 80 | |
michael@0 | 81 | if (key.length == 1) { |
michael@0 | 82 | keycode = 0; |
michael@0 | 83 | charcode = key.charCodeAt(0); |
michael@0 | 84 | alwaysval = charcode; |
michael@0 | 85 | } else { |
michael@0 | 86 | keycode = KeyEvent[keyName]; |
michael@0 | 87 | if (!keycode) |
michael@0 | 88 | throw "invalid keyname in test"; |
michael@0 | 89 | charcode = 0; |
michael@0 | 90 | alwaysval = keycode; |
michael@0 | 91 | } |
michael@0 | 92 | |
michael@0 | 93 | var dnEvent = document.createEvent('KeyboardEvent'); |
michael@0 | 94 | var prEvent = document.createEvent('KeyboardEvent'); |
michael@0 | 95 | var upEvent = document.createEvent('KeyboardEvent'); |
michael@0 | 96 | |
michael@0 | 97 | dnEvent.initKeyEvent("keydown", true, true, null, false, false, false, false, alwaysval, 0); |
michael@0 | 98 | prEvent.initKeyEvent("keypress", true, true, null, false, false, false, false, keycode, charcode); |
michael@0 | 99 | upEvent.initKeyEvent("keyup", true, true, null, false, false, false, false, alwaysval, 0); |
michael@0 | 100 | |
michael@0 | 101 | input.dispatchEvent(dnEvent); |
michael@0 | 102 | input.dispatchEvent(prEvent); |
michael@0 | 103 | input.dispatchEvent(upEvent); |
michael@0 | 104 | } |
michael@0 | 105 | |
michael@0 | 106 | function doClickUnprivileged() { |
michael@0 | 107 | var dnEvent = document.createEvent('MouseEvent'); |
michael@0 | 108 | var upEvent = document.createEvent('MouseEvent'); |
michael@0 | 109 | var ckEvent = document.createEvent('MouseEvent'); |
michael@0 | 110 | |
michael@0 | 111 | dnEvent.initMouseEvent("mousedown", true, true, window, 1, 0, 0, 0, 0, false, false, false, false, 0, null); |
michael@0 | 112 | upEvent.initMouseEvent("mouseup", true, true, window, 1, 0, 0, 0, 0, false, false, false, false, 0, null); |
michael@0 | 113 | ckEvent.initMouseEvent("mouseclick", true, true, window, 1, 0, 0, 0, 0, false, false, false, false, 0, null); |
michael@0 | 114 | |
michael@0 | 115 | input.dispatchEvent(dnEvent); |
michael@0 | 116 | input.dispatchEvent(upEvent); |
michael@0 | 117 | input.dispatchEvent(ckEvent); |
michael@0 | 118 | } |
michael@0 | 119 | |
michael@0 | 120 | var testNum = 0; |
michael@0 | 121 | var expectingPopup = false; |
michael@0 | 122 | |
michael@0 | 123 | function expectPopup() |
michael@0 | 124 | { |
michael@0 | 125 | info("expecting popup for test " + testNum); |
michael@0 | 126 | expectingPopup = true; |
michael@0 | 127 | } |
michael@0 | 128 | |
michael@0 | 129 | function popupShownListener() |
michael@0 | 130 | { |
michael@0 | 131 | info("popup shown for test " + testNum); |
michael@0 | 132 | if (expectingPopup) { |
michael@0 | 133 | expectingPopup = false; |
michael@0 | 134 | SimpleTest.executeSoon(runTest); |
michael@0 | 135 | } |
michael@0 | 136 | else { |
michael@0 | 137 | ok(false, "Autocomplete popup not expected" + testNum); |
michael@0 | 138 | } |
michael@0 | 139 | } |
michael@0 | 140 | |
michael@0 | 141 | SpecialPowers.addAutoCompletePopupEventListener(window, "popupshown", popupShownListener); |
michael@0 | 142 | |
michael@0 | 143 | /* |
michael@0 | 144 | * Main section of test... |
michael@0 | 145 | * |
michael@0 | 146 | * This is a bit hacky, because the events are either being sent or |
michael@0 | 147 | * processes asynchronously, so we need to interrupt our flow with lots of |
michael@0 | 148 | * setTimeout() calls. The case statements are executed in order, one per |
michael@0 | 149 | * timeout. |
michael@0 | 150 | */ |
michael@0 | 151 | function runTest() { |
michael@0 | 152 | testNum++; |
michael@0 | 153 | |
michael@0 | 154 | ok(true, "Starting test #" + testNum); |
michael@0 | 155 | |
michael@0 | 156 | switch(testNum) { |
michael@0 | 157 | // |
michael@0 | 158 | // Check initial state |
michael@0 | 159 | // |
michael@0 | 160 | case 1: |
michael@0 | 161 | input.value = ""; |
michael@0 | 162 | checkForm(""); |
michael@0 | 163 | is(autocompletePopup.popupOpen, false, "popup should be initially closed"); |
michael@0 | 164 | break; |
michael@0 | 165 | |
michael@0 | 166 | // |
michael@0 | 167 | // Try to open the autocomplete popup from untrusted events. |
michael@0 | 168 | // |
michael@0 | 169 | // try a focus() |
michael@0 | 170 | case 2: |
michael@0 | 171 | input.focus(); |
michael@0 | 172 | break; |
michael@0 | 173 | case 3: |
michael@0 | 174 | checkPopupOpen(false); |
michael@0 | 175 | checkForm(""); |
michael@0 | 176 | break; |
michael@0 | 177 | // try a click() |
michael@0 | 178 | case 4: |
michael@0 | 179 | input.click(); |
michael@0 | 180 | break; |
michael@0 | 181 | case 5: |
michael@0 | 182 | checkPopupOpen(false); |
michael@0 | 183 | checkForm(""); |
michael@0 | 184 | break; |
michael@0 | 185 | // try a mouseclick event |
michael@0 | 186 | case 6: |
michael@0 | 187 | doClickUnprivileged(); |
michael@0 | 188 | break; |
michael@0 | 189 | case 7: |
michael@0 | 190 | checkPopupOpen(false); |
michael@0 | 191 | checkForm(""); |
michael@0 | 192 | break; |
michael@0 | 193 | // try a down-arrow |
michael@0 | 194 | case 8: |
michael@0 | 195 | doKeyUnprivileged("down"); |
michael@0 | 196 | break; |
michael@0 | 197 | case 9: |
michael@0 | 198 | checkPopupOpen(false); |
michael@0 | 199 | checkForm(""); |
michael@0 | 200 | break; |
michael@0 | 201 | // try a page-down |
michael@0 | 202 | case 10: |
michael@0 | 203 | doKeyUnprivileged("page_down"); |
michael@0 | 204 | break; |
michael@0 | 205 | case 11: |
michael@0 | 206 | checkPopupOpen(false); |
michael@0 | 207 | checkForm(""); |
michael@0 | 208 | break; |
michael@0 | 209 | // try a return |
michael@0 | 210 | case 12: |
michael@0 | 211 | // XXX this causes later tests to fail for some reason. |
michael@0 | 212 | // doKeyUnprivileged("return"); // not "enter"! |
michael@0 | 213 | break; |
michael@0 | 214 | case 13: |
michael@0 | 215 | checkPopupOpen(false); |
michael@0 | 216 | checkForm(""); |
michael@0 | 217 | break; |
michael@0 | 218 | // try a keypress |
michael@0 | 219 | case 14: |
michael@0 | 220 | doKeyUnprivileged('v'); |
michael@0 | 221 | break; |
michael@0 | 222 | case 15: |
michael@0 | 223 | checkPopupOpen(false); |
michael@0 | 224 | checkForm(""); |
michael@0 | 225 | break; |
michael@0 | 226 | // try a space |
michael@0 | 227 | case 16: |
michael@0 | 228 | doKeyUnprivileged(" "); |
michael@0 | 229 | break; |
michael@0 | 230 | case 17: |
michael@0 | 231 | checkPopupOpen(false); |
michael@0 | 232 | checkForm(""); |
michael@0 | 233 | break; |
michael@0 | 234 | // backspace |
michael@0 | 235 | case 18: |
michael@0 | 236 | doKeyUnprivileged("back_space"); |
michael@0 | 237 | break; |
michael@0 | 238 | case 19: |
michael@0 | 239 | checkPopupOpen(false); |
michael@0 | 240 | checkForm(""); |
michael@0 | 241 | break; |
michael@0 | 242 | case 20: |
michael@0 | 243 | // We're privileged for this test, so open the popup. |
michael@0 | 244 | checkPopupOpen(false); |
michael@0 | 245 | checkForm(""); |
michael@0 | 246 | expectPopup(); |
michael@0 | 247 | doKey("down"); |
michael@0 | 248 | return; |
michael@0 | 249 | break; |
michael@0 | 250 | case 21: |
michael@0 | 251 | checkPopupOpen(true, -1); |
michael@0 | 252 | checkForm(""); |
michael@0 | 253 | testNum = 99; |
michael@0 | 254 | break; |
michael@0 | 255 | |
michael@0 | 256 | // |
michael@0 | 257 | // Try to change the selected autocomplete item from untrusted events |
michael@0 | 258 | // |
michael@0 | 259 | |
michael@0 | 260 | // try a down-arrow |
michael@0 | 261 | case 100: |
michael@0 | 262 | doKeyUnprivileged("down"); |
michael@0 | 263 | break; |
michael@0 | 264 | case 101: |
michael@0 | 265 | checkPopupOpen(true, -1); |
michael@0 | 266 | checkForm(""); |
michael@0 | 267 | break; |
michael@0 | 268 | // try a page-down |
michael@0 | 269 | case 102: |
michael@0 | 270 | doKeyUnprivileged("page_down"); |
michael@0 | 271 | break; |
michael@0 | 272 | case 103: |
michael@0 | 273 | checkPopupOpen(true, -1); |
michael@0 | 274 | checkForm(""); |
michael@0 | 275 | break; |
michael@0 | 276 | // really adjust the index |
michael@0 | 277 | case 104: |
michael@0 | 278 | // (We're privileged for this test.) Try a privileged down-arrow. |
michael@0 | 279 | doKey("down"); |
michael@0 | 280 | break; |
michael@0 | 281 | case 105: |
michael@0 | 282 | checkPopupOpen(true, 0); |
michael@0 | 283 | checkForm(""); |
michael@0 | 284 | break; |
michael@0 | 285 | // try a down-arrow |
michael@0 | 286 | case 106: |
michael@0 | 287 | doKeyUnprivileged("down"); |
michael@0 | 288 | break; |
michael@0 | 289 | case 107: |
michael@0 | 290 | checkPopupOpen(true, 0); |
michael@0 | 291 | checkForm(""); |
michael@0 | 292 | break; |
michael@0 | 293 | // try a page-down |
michael@0 | 294 | case 108: |
michael@0 | 295 | doKeyUnprivileged("page_down"); |
michael@0 | 296 | break; |
michael@0 | 297 | case 109: |
michael@0 | 298 | checkPopupOpen(true, 0); |
michael@0 | 299 | checkForm(""); |
michael@0 | 300 | // try a keypress |
michael@0 | 301 | case 110: |
michael@0 | 302 | // XXX this causes the popup to close, and makes the value "vaa" (sic) |
michael@0 | 303 | //doKeyUnprivileged('a'); |
michael@0 | 304 | break; |
michael@0 | 305 | case 111: |
michael@0 | 306 | checkPopupOpen(true, 0); |
michael@0 | 307 | checkForm(""); |
michael@0 | 308 | testNum = 199; |
michael@0 | 309 | break; |
michael@0 | 310 | |
michael@0 | 311 | // |
michael@0 | 312 | // Try to use the selected autocomplete item from untrusted events |
michael@0 | 313 | // |
michael@0 | 314 | // try a right-arrow |
michael@0 | 315 | case 200: |
michael@0 | 316 | doKeyUnprivileged("right"); |
michael@0 | 317 | break; |
michael@0 | 318 | case 201: |
michael@0 | 319 | checkPopupOpen(true, 0); |
michael@0 | 320 | checkForm(""); |
michael@0 | 321 | break; |
michael@0 | 322 | // try a space |
michael@0 | 323 | case 202: |
michael@0 | 324 | doKeyUnprivileged(" "); |
michael@0 | 325 | break; |
michael@0 | 326 | case 203: |
michael@0 | 327 | // XXX we should ignore this input while popup is open? |
michael@0 | 328 | checkPopupOpen(true, 0); |
michael@0 | 329 | checkForm(""); |
michael@0 | 330 | break; |
michael@0 | 331 | // backspace |
michael@0 | 332 | case 204: |
michael@0 | 333 | doKeyUnprivileged("back_space"); |
michael@0 | 334 | break; |
michael@0 | 335 | case 205: |
michael@0 | 336 | // XXX we should ignore this input while popup is open? |
michael@0 | 337 | checkPopupOpen(true, 0); |
michael@0 | 338 | checkForm(""); |
michael@0 | 339 | break; |
michael@0 | 340 | case 206: |
michael@0 | 341 | // (this space intentionally left blank) |
michael@0 | 342 | break; |
michael@0 | 343 | case 207: |
michael@0 | 344 | checkPopupOpen(true, 0); |
michael@0 | 345 | checkForm(""); |
michael@0 | 346 | break; |
michael@0 | 347 | // try a return |
michael@0 | 348 | case 208: |
michael@0 | 349 | // XXX this seems to cause problems with reliably closing the popup |
michael@0 | 350 | // doKeyUnprivileged("return"); // not "enter"! |
michael@0 | 351 | break; |
michael@0 | 352 | case 209: |
michael@0 | 353 | checkPopupOpen(true, 0); |
michael@0 | 354 | checkForm(""); |
michael@0 | 355 | break; |
michael@0 | 356 | // Send a real Escape to ensure popup closed at end of test. |
michael@0 | 357 | case 210: |
michael@0 | 358 | // Need to use doKey(), even though this test is not privileged. |
michael@0 | 359 | doKey("escape"); |
michael@0 | 360 | break; |
michael@0 | 361 | case 211: |
michael@0 | 362 | checkPopupOpen(false); |
michael@0 | 363 | checkForm(""); |
michael@0 | 364 | is(autocompletePopup.style.direction, "rtl", "direction should have been changed from ltr to rtl"); |
michael@0 | 365 | |
michael@0 | 366 | SpecialPowers.removeAutoCompletePopupEventListener(window, "popupshown", popupShownListener); |
michael@0 | 367 | SimpleTest.finish(); |
michael@0 | 368 | return; |
michael@0 | 369 | |
michael@0 | 370 | default: |
michael@0 | 371 | ok(false, "Unexpected invocation of test #" + testNum); |
michael@0 | 372 | SpecialPowers.removeAutoCompletePopupEventListener(window, "popupshown", popupShownListener); |
michael@0 | 373 | SimpleTest.finish(); |
michael@0 | 374 | return; |
michael@0 | 375 | } |
michael@0 | 376 | |
michael@0 | 377 | SimpleTest.executeSoon(runTest); |
michael@0 | 378 | } |
michael@0 | 379 | |
michael@0 | 380 | function startTest() { |
michael@0 | 381 | setupFormHistory(runTest); |
michael@0 | 382 | } |
michael@0 | 383 | |
michael@0 | 384 | SimpleTest.waitForFocus(startTest); |
michael@0 | 385 | |
michael@0 | 386 | SimpleTest.waitForExplicitFinish(); |
michael@0 | 387 | </script> |
michael@0 | 388 | </pre> |
michael@0 | 389 | </body> |
michael@0 | 390 | </html> |