widget/tests/test_input_events_on_deactive_window.xul

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/widget/tests/test_input_events_on_deactive_window.xul	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,257 @@
     1.4 +<?xml version="1.0"?>
     1.5 +<?xml-stylesheet href="chrome://global/skin" type="text/css"?>
     1.6 +<?xml-stylesheet href="chrome://mochikit/content/tests/SimpleTest/test.css"
     1.7 +                 type="text/css"?>
     1.8 +<window title="Testing composition, text and query content events"
     1.9 +  xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
    1.10 +
    1.11 +  <script type="application/javascript"
    1.12 +          src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js" />
    1.13 +  <script type="application/javascript"
    1.14 +          src="chrome://mochikit/content/tests/SimpleTest/EventUtils.js" />
    1.15 +  <script type="text/javascript" src="chrome://mochikit/content/tests/SimpleTest/ChromeUtils.js"></script>
    1.16 +
    1.17 +<body  xmlns="http://www.w3.org/1999/xhtml">
    1.18 +<div id="content" style="display: none">
    1.19 +</div>
    1.20 +<p id="display">
    1.21 +  <textarea id="textarea"></textarea>
    1.22 +</p>
    1.23 +<pre id="test">
    1.24 +</pre>
    1.25 +</body>
    1.26 +
    1.27 +<script class="testbody" type="application/javascript">
    1.28 +<![CDATA[
    1.29 +
    1.30 +SimpleTest.waitForExplicitFinish();
    1.31 +SimpleTest.waitForFocus(runTests, window);
    1.32 +
    1.33 +var fm = Components.classes["@mozilla.org/focus-manager;1"].
    1.34 +           getService(Components.interfaces.nsIFocusManager);
    1.35 +var textarea = document.getElementById("textarea");
    1.36 +var otherWindow;
    1.37 +var timer;
    1.38 +
    1.39 +function runTests()
    1.40 +{
    1.41 +  textarea.focus();
    1.42 +  is(fm.focusedElement, textarea, "we're deactive");
    1.43 +  if (fm.focusedElement != textarea) {
    1.44 +    SimpleTest.finish();
    1.45 +    return;
    1.46 +  }
    1.47 +
    1.48 +  otherWindow =
    1.49 +    window.open("data:text/plain,this is an active window.", "_blank",
    1.50 +                "chrome,width=100,height=100");
    1.51 +  ok(otherWindow, "failed to open other window");
    1.52 +  if (!otherWindow) {
    1.53 +    SimpleTest.finish();
    1.54 +    return;
    1.55 +  }
    1.56 +
    1.57 +  SimpleTest.waitForFocus(startTests, otherWindow);
    1.58 +  otherWindow.focus();
    1.59 +}
    1.60 +
    1.61 +function startTests()
    1.62 +{
    1.63 +  clearTimeout(timer);
    1.64 +  isnot(fm.focusedWindow, window, "we're not deactive");
    1.65 +  if (fm.focusedWindow == window) {
    1.66 +    otherWindow.close();
    1.67 +    SimpleTest.finish();
    1.68 +    return;
    1.69 +  }
    1.70 +
    1.71 +  var keydownHandled, keypressHandled, keyupHandled, compositionstartHandled,
    1.72 +      compositionendHandled, compositionupdateHandled, inputHandled;
    1.73 +
    1.74 +  function clear()
    1.75 +  {
    1.76 +    keydownHandled = false;
    1.77 +    keypressHandled = false;
    1.78 +    keyupHandled = false;
    1.79 +    compositionstartHandled = false;
    1.80 +    compositionendHandled = false;
    1.81 +    compositionupdateHandled = false;
    1.82 +    inputHandled = false;
    1.83 +  }
    1.84 +
    1.85 +  function onEvent(aEvent)
    1.86 +  {
    1.87 +    if (aEvent.type == "keydown") {
    1.88 +      keydownHandled = true;
    1.89 +    } else if (aEvent.type == "keypress") {
    1.90 +      keypressHandled = true;
    1.91 +    } else if (aEvent.type == "keyup") {
    1.92 +      keyupHandled = true;
    1.93 +    } else if (aEvent.type == "compositionstart") {
    1.94 +      compositionstartHandled = true;
    1.95 +    } else if (aEvent.type == "compositionend") {
    1.96 +      compositionendHandled = true;
    1.97 +    } else if (aEvent.type == "compositionupdate") {
    1.98 +      compositionupdateHandled = true;
    1.99 +    } else if (aEvent.type == "input") {
   1.100 +      inputHandled = true;
   1.101 +    } else {
   1.102 +      ok(false, "handled unknown event: " + aEvent.type);
   1.103 +    }
   1.104 +  }
   1.105 +
   1.106 +  textarea.addEventListener("keydown", onEvent, false);
   1.107 +  textarea.addEventListener("keypress", onEvent, false);
   1.108 +  textarea.addEventListener("keyup", onEvent, false);
   1.109 +  textarea.addEventListener("compositionstart", onEvent, false);
   1.110 +  textarea.addEventListener("compositionend", onEvent, false);
   1.111 +  textarea.addEventListener("compositionupdate", onEvent, false);
   1.112 +  textarea.addEventListener("input", onEvent, false);
   1.113 +
   1.114 +  startTestsInternal();
   1.115 +
   1.116 +  function startTestsInternal()
   1.117 +  {
   1.118 +    // key events
   1.119 +    function checkKeyEvents(aKeydown, aKeypress, aKeyup, aInput, aDescription)
   1.120 +    {
   1.121 +      is(keydownHandled, aKeydown,
   1.122 +         "keydown event is (not) handled: " + aDescription);
   1.123 +      is(keypressHandled, aKeypress,
   1.124 +         "keypress event is (not) handled: " + aDescription);
   1.125 +      is(keyupHandled, aKeyup,
   1.126 +         "keyup event is (not) handled: " + aDescription);
   1.127 +      is(inputHandled, aInput,
   1.128 +         "input event is (not) handled: " + aDescription);
   1.129 +    }
   1.130 +
   1.131 +    function checkCompositionEvents(aStart, aEnd, aUpdate, aInput, aDescription)
   1.132 +    {
   1.133 +      is(compositionstartHandled, aStart,
   1.134 +         "compositionstart event is (not) handled: " + aDescription);
   1.135 +      is(compositionendHandled, aEnd,
   1.136 +         "compositionend event is (not) handled: " + aDescription);
   1.137 +      is(compositionupdateHandled, aUpdate,
   1.138 +         "compositionupdate event is (not) handled: " + aDescription);
   1.139 +      is(inputHandled, aInput,
   1.140 +         "input event is (not) handled: " + aDescription);
   1.141 +    }
   1.142 +
   1.143 +    clear();
   1.144 +    synthesizeKey("a", { type: "keydown" });
   1.145 +    checkKeyEvents(true, false, false, false, "a keydown");
   1.146 +    clear();
   1.147 +    synthesizeKey("a", { type: "keypress" });
   1.148 +    checkKeyEvents(false, true, false, true, "a keypress");
   1.149 +    is(textarea.value, "a", "textarea value isn't 'a'");
   1.150 +    clear();
   1.151 +    synthesizeKey("a", { type: "keyup" });
   1.152 +    checkKeyEvents(false, false, true, false, "a keyup");
   1.153 +    clear();
   1.154 +    synthesizeKey("VK_BACK_SPACE", {});
   1.155 +    checkKeyEvents(true, true, true, true, "VK_BACK_SPACE key events");
   1.156 +    is(textarea.value, "", "textarea value isn't empty");
   1.157 +
   1.158 +    // IME events
   1.159 +    clear();
   1.160 +    // start composition
   1.161 +    synthesizeComposition({ type: "compositionstart" });
   1.162 +    checkCompositionEvents(true, false, false, false, "compositionstart");
   1.163 +    clear();
   1.164 +    // input first character
   1.165 +    synthesizeComposition({ type: "compositionupdate", data: "\u3089" });
   1.166 +    synthesizeText(
   1.167 +      { "composition":
   1.168 +        { "string": "\u3089",
   1.169 +          "clauses":
   1.170 +          [
   1.171 +            { "length": 1, "attr": COMPOSITION_ATTR_RAWINPUT }
   1.172 +          ]
   1.173 +        },
   1.174 +        "caret": { "start": 1, "length": 0 }
   1.175 +      });
   1.176 +    checkCompositionEvents(false, false, true, true, "composing");
   1.177 +    var queryText = synthesizeQueryTextContent(0, 100);
   1.178 +    ok(queryText, "query text event result is null");
   1.179 +    if (!queryText) {
   1.180 +      return;
   1.181 +    }
   1.182 +    ok(queryText.succeeded, "query text event failed");
   1.183 +    if (!queryText.succeeded) {
   1.184 +      return;
   1.185 +    }
   1.186 +    is(queryText.text, "\u3089", "composing text is incorrect");
   1.187 +    var querySelectedText = synthesizeQuerySelectedText();
   1.188 +    ok(querySelectedText, "query selected text event result is null");
   1.189 +    if (!querySelectedText) {
   1.190 +      return;
   1.191 +    }
   1.192 +    ok(querySelectedText.succeeded, "query selected text event failed");
   1.193 +    if (!querySelectedText.succeeded) {
   1.194 +      return;
   1.195 +    }
   1.196 +    is(querySelectedText.offset, 1,
   1.197 +       "query selected text event returns wrong offset");
   1.198 +    is(querySelectedText.text, "",
   1.199 +       "query selected text event returns wrong selected text");
   1.200 +    clear();
   1.201 +    // commit composition
   1.202 +    synthesizeText(
   1.203 +      { "composition":
   1.204 +        { "string": "\u3089",
   1.205 +          "clauses":
   1.206 +          [
   1.207 +            { "length": 0, "attr": 0 }
   1.208 +          ]
   1.209 +        },
   1.210 +        "caret": { "start": 1, "length": 0 }
   1.211 +      });
   1.212 +    checkCompositionEvents(false, false, false, false, "commit composition");
   1.213 +    queryText = synthesizeQueryTextContent(0, 100);
   1.214 +    ok(queryText, "query text event result is null after commit");
   1.215 +    if (!queryText) {
   1.216 +      return;
   1.217 +    }
   1.218 +    ok(queryText.succeeded, "query text event failed after commit");
   1.219 +    if (!queryText.succeeded) {
   1.220 +      return;
   1.221 +    }
   1.222 +    is(queryText.text, "\u3089", "composing text is incorrect after commit");
   1.223 +    querySelectedText = synthesizeQuerySelectedText();
   1.224 +    ok(querySelectedText,
   1.225 +       "query selected text event result is null after commit");
   1.226 +    if (!querySelectedText) {
   1.227 +      return;
   1.228 +    }
   1.229 +    ok(querySelectedText.succeeded,
   1.230 +       "query selected text event failed after commit");
   1.231 +    if (!querySelectedText.succeeded) {
   1.232 +      return;
   1.233 +    }
   1.234 +    is(querySelectedText.offset, 1,
   1.235 +       "query selected text event returns wrong offset after commit");
   1.236 +    is(querySelectedText.text, "",
   1.237 +       "query selected text event returns wrong selected text after commit");
   1.238 +    clear();
   1.239 +    // end composition
   1.240 +    synthesizeComposition({ type: "compositionend", data: "\u3089" });
   1.241 +    checkCompositionEvents(false, true, false, true, "compositionend");
   1.242 +  }
   1.243 +
   1.244 +  textarea.removeEventListener("keydown", onEvent, false);
   1.245 +  textarea.removeEventListener("keypress", onEvent, false);
   1.246 +  textarea.removeEventListener("keyup", onEvent, false);
   1.247 +  textarea.removeEventListener("compositionstart", onEvent, false);
   1.248 +  textarea.removeEventListener("compositionupdate", onEvent, false);
   1.249 +  textarea.removeEventListener("compositionend", onEvent, false);
   1.250 +  textarea.removeEventListener("input", onEvent, false);
   1.251 +
   1.252 +  otherWindow.close();
   1.253 +
   1.254 +  SimpleTest.finish();
   1.255 +}
   1.256 +
   1.257 +
   1.258 +]]>
   1.259 +</script>
   1.260 +</window>

mercurial