toolkit/components/satchel/test/test_bug_511615.html

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/components/satchel/test/test_bug_511615.html	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,390 @@
     1.4 +<!DOCTYPE HTML>
     1.5 +<html>
     1.6 +<head>
     1.7 +  <title>Test for Form History Autocomplete</title>
     1.8 +  <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
     1.9 +  <script type="text/javascript" src="/tests/SimpleTest/EventUtils.js"></script>
    1.10 +  <script type="text/javascript" src="satchel_common.js"></script>
    1.11 +  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
    1.12 +</head>
    1.13 +<body>
    1.14 +Form History test: form field autocomplete
    1.15 +<p id="display"></p>
    1.16 +
    1.17 +<!-- we presumably can't hide the content for this test. -->
    1.18 +<div id="content" style="direction: rtl;">
    1.19 +  <!-- unused -->
    1.20 +  <form id="unused" onsubmit="return false;">
    1.21 +    <input  type="text" name="field1" value="unused">
    1.22 +  </form>
    1.23 +
    1.24 +  <!-- normal, basic form -->
    1.25 +  <form id="form1" onsubmit="return false;">
    1.26 +    <input  type="text" name="field1">
    1.27 +    <button type="submit">Submit</button>
    1.28 +  </form>
    1.29 +</div>
    1.30 +
    1.31 +<pre id="test">
    1.32 +<script class="testbody" type="text/javascript">
    1.33 +
    1.34 +/** Test for Form History autocomplete **/
    1.35 +
    1.36 +var autocompletePopup = getAutocompletePopup();
    1.37 +autocompletePopup.style.direction = "ltr";
    1.38 +
    1.39 +var input = $_(1, "field1");
    1.40 +
    1.41 +// Get the form history service
    1.42 +function setupFormHistory(aCallback) {
    1.43 +  updateFormHistory([
    1.44 +    { op : "remove" },
    1.45 +    { op : "add", fieldname : "field1", value : "value1" },
    1.46 +    { op : "add", fieldname : "field1", value : "value2" },
    1.47 +    { op : "add", fieldname : "field1", value : "value3" },
    1.48 +    { op : "add", fieldname : "field1", value : "value4" },
    1.49 +    { op : "add", fieldname : "field1", value : "value5" },
    1.50 +    { op : "add", fieldname : "field1", value : "value6" },
    1.51 +    { op : "add", fieldname : "field1", value : "value7" },
    1.52 +    { op : "add", fieldname : "field1", value : "value8" },
    1.53 +    { op : "add", fieldname : "field1", value : "value9" },
    1.54 +  ], aCallback);
    1.55 +}
    1.56 +
    1.57 +function checkForm(expectedValue) {
    1.58 +  var formID = input.parentNode.id;
    1.59 +  is(input.value, expectedValue, "Checking " + formID + " input");
    1.60 +}
    1.61 +
    1.62 +function checkPopupOpen(isOpen, expectedIndex) {
    1.63 +    var actuallyOpen = autocompletePopup.popupOpen;
    1.64 +    var actualIndex = autocompletePopup.selectedIndex;
    1.65 +    is(actuallyOpen, isOpen, "popup should be " + (isOpen ? "open" : "closed"));
    1.66 +    if (isOpen)
    1.67 +        is(actualIndex, expectedIndex, "checking selected index");
    1.68 +
    1.69 +    // Correct state if needed, so following tests run as expected.
    1.70 +    if (actuallyOpen != isOpen) {
    1.71 +        if (isOpen)
    1.72 +            autocompletePopup.openPopup();
    1.73 +        else
    1.74 +            autocompletePopup.closePopup();
    1.75 +    }
    1.76 +    if (isOpen && actualIndex != expectedIndex)
    1.77 +        autocompletePopup.selectedIndex = expectedIndex;
    1.78 +}
    1.79 +
    1.80 +function doKeyUnprivileged(key) {
    1.81 +    var keyName = "DOM_VK_" + key.toUpperCase();
    1.82 +    var keycode, charcode, alwaysVal;
    1.83 +
    1.84 +    if (key.length == 1) {
    1.85 +        keycode = 0;
    1.86 +        charcode = key.charCodeAt(0);
    1.87 +        alwaysval = charcode;
    1.88 +    } else {
    1.89 +        keycode = KeyEvent[keyName];
    1.90 +        if (!keycode)
    1.91 +            throw "invalid keyname in test";
    1.92 +        charcode = 0;
    1.93 +        alwaysval = keycode;
    1.94 +    }
    1.95 +
    1.96 +    var dnEvent = document.createEvent('KeyboardEvent');
    1.97 +    var prEvent = document.createEvent('KeyboardEvent');
    1.98 +    var upEvent = document.createEvent('KeyboardEvent');
    1.99 +
   1.100 +    dnEvent.initKeyEvent("keydown",  true, true, null, false, false, false, false, alwaysval, 0);
   1.101 +    prEvent.initKeyEvent("keypress", true, true, null, false, false, false, false, keycode, charcode);
   1.102 +    upEvent.initKeyEvent("keyup",    true, true, null, false, false, false, false, alwaysval, 0);
   1.103 +
   1.104 +    input.dispatchEvent(dnEvent);
   1.105 +    input.dispatchEvent(prEvent);
   1.106 +    input.dispatchEvent(upEvent);
   1.107 +}
   1.108 +
   1.109 +function doClickUnprivileged() {
   1.110 +    var dnEvent = document.createEvent('MouseEvent');
   1.111 +    var upEvent = document.createEvent('MouseEvent');
   1.112 +    var ckEvent = document.createEvent('MouseEvent');
   1.113 +
   1.114 +    dnEvent.initMouseEvent("mousedown",  true, true, window, 1, 0, 0, 0, 0, false, false, false, false, 0, null);
   1.115 +    upEvent.initMouseEvent("mouseup",    true, true, window, 1, 0, 0, 0, 0, false, false, false, false, 0, null);
   1.116 +    ckEvent.initMouseEvent("mouseclick", true, true, window, 1, 0, 0, 0, 0, false, false, false, false, 0, null);
   1.117 +
   1.118 +    input.dispatchEvent(dnEvent);
   1.119 +    input.dispatchEvent(upEvent);
   1.120 +    input.dispatchEvent(ckEvent);
   1.121 +}
   1.122 +
   1.123 +var testNum = 0;
   1.124 +var expectingPopup = false;
   1.125 +
   1.126 +function expectPopup()
   1.127 +{
   1.128 +  info("expecting popup for test " + testNum);
   1.129 +  expectingPopup = true;
   1.130 +}
   1.131 +
   1.132 +function popupShownListener()
   1.133 +{
   1.134 +  info("popup shown for test " + testNum);
   1.135 +  if (expectingPopup) {
   1.136 +    expectingPopup = false;
   1.137 +    SimpleTest.executeSoon(runTest);
   1.138 +  }
   1.139 +  else {
   1.140 +    ok(false, "Autocomplete popup not expected" + testNum);
   1.141 +  }
   1.142 +}
   1.143 +
   1.144 +SpecialPowers.addAutoCompletePopupEventListener(window, "popupshown", popupShownListener);
   1.145 +
   1.146 +/*
   1.147 + * Main section of test...
   1.148 + *
   1.149 + * This is a bit hacky, because the events are either being sent or
   1.150 + * processes asynchronously, so we need to interrupt our flow with lots of
   1.151 + * setTimeout() calls. The case statements are executed in order, one per
   1.152 + * timeout.
   1.153 + */
   1.154 +function runTest() {
   1.155 +  testNum++;
   1.156 +
   1.157 +  ok(true, "Starting test #" + testNum);
   1.158 +
   1.159 +  switch(testNum) {
   1.160 +    //
   1.161 +    // Check initial state
   1.162 +    //
   1.163 +    case 1:
   1.164 +        input.value = "";
   1.165 +        checkForm("");
   1.166 +        is(autocompletePopup.popupOpen, false, "popup should be initially closed");
   1.167 +        break;
   1.168 +
   1.169 +    //
   1.170 +    // Try to open the autocomplete popup from untrusted events.
   1.171 +    //
   1.172 +    // try a focus()
   1.173 +    case 2:
   1.174 +        input.focus();
   1.175 +        break;
   1.176 +    case 3:
   1.177 +        checkPopupOpen(false);
   1.178 +        checkForm("");
   1.179 +        break;
   1.180 +    // try a click()
   1.181 +    case 4:
   1.182 +        input.click();
   1.183 +        break;
   1.184 +    case 5:
   1.185 +        checkPopupOpen(false);
   1.186 +        checkForm("");
   1.187 +        break;
   1.188 +    // try a mouseclick event
   1.189 +    case 6:
   1.190 +        doClickUnprivileged();
   1.191 +        break;
   1.192 +    case 7:
   1.193 +        checkPopupOpen(false);
   1.194 +        checkForm("");
   1.195 +        break;
   1.196 +    // try a down-arrow
   1.197 +    case 8:
   1.198 +        doKeyUnprivileged("down");
   1.199 +        break;
   1.200 +    case 9:
   1.201 +        checkPopupOpen(false);
   1.202 +        checkForm("");
   1.203 +        break;
   1.204 +    // try a page-down
   1.205 +    case 10:
   1.206 +        doKeyUnprivileged("page_down");
   1.207 +        break;
   1.208 +    case 11:
   1.209 +        checkPopupOpen(false);
   1.210 +        checkForm("");
   1.211 +        break;
   1.212 +    // try a return
   1.213 +    case 12:
   1.214 +// XXX this causes later tests to fail for some reason.
   1.215 +//        doKeyUnprivileged("return"); // not "enter"!
   1.216 +        break;
   1.217 +    case 13:
   1.218 +        checkPopupOpen(false);
   1.219 +        checkForm("");
   1.220 +        break;
   1.221 +    // try a keypress
   1.222 +    case 14:
   1.223 +        doKeyUnprivileged('v');
   1.224 +        break;
   1.225 +    case 15:
   1.226 +        checkPopupOpen(false);
   1.227 +        checkForm("");
   1.228 +        break;
   1.229 +    // try a space
   1.230 +    case 16:
   1.231 +        doKeyUnprivileged(" ");
   1.232 +        break;
   1.233 +    case 17:
   1.234 +        checkPopupOpen(false);
   1.235 +        checkForm("");
   1.236 +        break;
   1.237 +    // backspace
   1.238 +    case 18:
   1.239 +        doKeyUnprivileged("back_space");
   1.240 +        break;
   1.241 +    case 19:
   1.242 +        checkPopupOpen(false);
   1.243 +        checkForm("");
   1.244 +        break;
   1.245 +    case 20:
   1.246 +        // We're privileged for this test, so open the popup.
   1.247 +        checkPopupOpen(false);
   1.248 +        checkForm("");
   1.249 +        expectPopup();
   1.250 +        doKey("down");
   1.251 +        return;
   1.252 +        break;
   1.253 +    case 21:
   1.254 +        checkPopupOpen(true, -1);
   1.255 +        checkForm("");
   1.256 +        testNum = 99;
   1.257 +        break;
   1.258 +
   1.259 +    //
   1.260 +    // Try to change the selected autocomplete item from untrusted events
   1.261 +    //
   1.262 +
   1.263 +    // try a down-arrow
   1.264 +    case 100:
   1.265 +        doKeyUnprivileged("down");
   1.266 +        break;
   1.267 +    case 101:
   1.268 +        checkPopupOpen(true, -1);
   1.269 +        checkForm("");
   1.270 +        break;
   1.271 +    // try a page-down
   1.272 +    case 102:
   1.273 +        doKeyUnprivileged("page_down");
   1.274 +        break;
   1.275 +    case 103:
   1.276 +        checkPopupOpen(true, -1);
   1.277 +        checkForm("");
   1.278 +        break;
   1.279 +    // really adjust the index
   1.280 +    case 104:
   1.281 +        // (We're privileged for this test.) Try a privileged down-arrow.
   1.282 +        doKey("down");
   1.283 +        break;
   1.284 +    case 105:
   1.285 +        checkPopupOpen(true, 0);
   1.286 +        checkForm("");
   1.287 +        break;
   1.288 +    // try a down-arrow
   1.289 +    case 106:
   1.290 +        doKeyUnprivileged("down");
   1.291 +        break;
   1.292 +    case 107:
   1.293 +        checkPopupOpen(true, 0);
   1.294 +        checkForm("");
   1.295 +        break;
   1.296 +    // try a page-down
   1.297 +    case 108:
   1.298 +        doKeyUnprivileged("page_down");
   1.299 +        break;
   1.300 +    case 109:
   1.301 +        checkPopupOpen(true, 0);
   1.302 +        checkForm("");
   1.303 +    // try a keypress
   1.304 +    case 110:
   1.305 +        // XXX this causes the popup to close, and makes the value "vaa" (sic)
   1.306 +        //doKeyUnprivileged('a');
   1.307 +        break;
   1.308 +    case 111:
   1.309 +        checkPopupOpen(true, 0);
   1.310 +        checkForm("");
   1.311 +        testNum = 199;
   1.312 +        break;
   1.313 +
   1.314 +    //
   1.315 +    // Try to use the selected autocomplete item from untrusted events
   1.316 +    //
   1.317 +    // try a right-arrow
   1.318 +    case 200:
   1.319 +        doKeyUnprivileged("right");
   1.320 +        break;
   1.321 +    case 201:
   1.322 +        checkPopupOpen(true, 0);
   1.323 +        checkForm("");
   1.324 +        break;
   1.325 +    // try a space
   1.326 +    case 202:
   1.327 +        doKeyUnprivileged(" ");
   1.328 +        break;
   1.329 +    case 203:
   1.330 +        // XXX we should ignore this input while popup is open?
   1.331 +        checkPopupOpen(true, 0);
   1.332 +        checkForm("");
   1.333 +        break;
   1.334 +    // backspace
   1.335 +    case 204:
   1.336 +        doKeyUnprivileged("back_space");
   1.337 +        break;
   1.338 +    case 205:
   1.339 +        // XXX we should ignore this input while popup is open?
   1.340 +        checkPopupOpen(true, 0);
   1.341 +        checkForm("");
   1.342 +        break;
   1.343 +    case 206:
   1.344 +        // (this space intentionally left blank)
   1.345 +        break;
   1.346 +    case 207:
   1.347 +        checkPopupOpen(true, 0);
   1.348 +        checkForm("");
   1.349 +        break;
   1.350 +    // try a return
   1.351 +    case 208:
   1.352 +// XXX this seems to cause problems with reliably closing the popup
   1.353 +//        doKeyUnprivileged("return"); // not "enter"!
   1.354 +        break;
   1.355 +    case 209:
   1.356 +        checkPopupOpen(true, 0);
   1.357 +        checkForm("");
   1.358 +        break;
   1.359 +    // Send a real Escape to ensure popup closed at end of test.
   1.360 +    case 210:
   1.361 +        // Need to use doKey(), even though this test is not privileged.
   1.362 +        doKey("escape");
   1.363 +        break;
   1.364 +    case 211:
   1.365 +        checkPopupOpen(false);
   1.366 +        checkForm("");
   1.367 +        is(autocompletePopup.style.direction, "rtl", "direction should have been changed from ltr to rtl");
   1.368 +
   1.369 +        SpecialPowers.removeAutoCompletePopupEventListener(window, "popupshown", popupShownListener);
   1.370 +        SimpleTest.finish();
   1.371 +        return;
   1.372 +
   1.373 +    default:
   1.374 +        ok(false, "Unexpected invocation of test #" + testNum);
   1.375 +        SpecialPowers.removeAutoCompletePopupEventListener(window, "popupshown", popupShownListener);
   1.376 +        SimpleTest.finish();
   1.377 +        return;
   1.378 +  }
   1.379 +
   1.380 +  SimpleTest.executeSoon(runTest);
   1.381 +}
   1.382 +
   1.383 +function startTest() {
   1.384 +  setupFormHistory(runTest);
   1.385 +}
   1.386 +
   1.387 +SimpleTest.waitForFocus(startTest);
   1.388 +
   1.389 +SimpleTest.waitForExplicitFinish();
   1.390 +</script>
   1.391 +</pre>
   1.392 +</body>
   1.393 +</html>

mercurial