widget/tests/test_input_events_on_deactive_window.xul

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 <?xml version="1.0"?>
michael@0 2 <?xml-stylesheet href="chrome://global/skin" type="text/css"?>
michael@0 3 <?xml-stylesheet href="chrome://mochikit/content/tests/SimpleTest/test.css"
michael@0 4 type="text/css"?>
michael@0 5 <window title="Testing composition, text and query content events"
michael@0 6 xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
michael@0 7
michael@0 8 <script type="application/javascript"
michael@0 9 src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js" />
michael@0 10 <script type="application/javascript"
michael@0 11 src="chrome://mochikit/content/tests/SimpleTest/EventUtils.js" />
michael@0 12 <script type="text/javascript" src="chrome://mochikit/content/tests/SimpleTest/ChromeUtils.js"></script>
michael@0 13
michael@0 14 <body xmlns="http://www.w3.org/1999/xhtml">
michael@0 15 <div id="content" style="display: none">
michael@0 16 </div>
michael@0 17 <p id="display">
michael@0 18 <textarea id="textarea"></textarea>
michael@0 19 </p>
michael@0 20 <pre id="test">
michael@0 21 </pre>
michael@0 22 </body>
michael@0 23
michael@0 24 <script class="testbody" type="application/javascript">
michael@0 25 <![CDATA[
michael@0 26
michael@0 27 SimpleTest.waitForExplicitFinish();
michael@0 28 SimpleTest.waitForFocus(runTests, window);
michael@0 29
michael@0 30 var fm = Components.classes["@mozilla.org/focus-manager;1"].
michael@0 31 getService(Components.interfaces.nsIFocusManager);
michael@0 32 var textarea = document.getElementById("textarea");
michael@0 33 var otherWindow;
michael@0 34 var timer;
michael@0 35
michael@0 36 function runTests()
michael@0 37 {
michael@0 38 textarea.focus();
michael@0 39 is(fm.focusedElement, textarea, "we're deactive");
michael@0 40 if (fm.focusedElement != textarea) {
michael@0 41 SimpleTest.finish();
michael@0 42 return;
michael@0 43 }
michael@0 44
michael@0 45 otherWindow =
michael@0 46 window.open("data:text/plain,this is an active window.", "_blank",
michael@0 47 "chrome,width=100,height=100");
michael@0 48 ok(otherWindow, "failed to open other window");
michael@0 49 if (!otherWindow) {
michael@0 50 SimpleTest.finish();
michael@0 51 return;
michael@0 52 }
michael@0 53
michael@0 54 SimpleTest.waitForFocus(startTests, otherWindow);
michael@0 55 otherWindow.focus();
michael@0 56 }
michael@0 57
michael@0 58 function startTests()
michael@0 59 {
michael@0 60 clearTimeout(timer);
michael@0 61 isnot(fm.focusedWindow, window, "we're not deactive");
michael@0 62 if (fm.focusedWindow == window) {
michael@0 63 otherWindow.close();
michael@0 64 SimpleTest.finish();
michael@0 65 return;
michael@0 66 }
michael@0 67
michael@0 68 var keydownHandled, keypressHandled, keyupHandled, compositionstartHandled,
michael@0 69 compositionendHandled, compositionupdateHandled, inputHandled;
michael@0 70
michael@0 71 function clear()
michael@0 72 {
michael@0 73 keydownHandled = false;
michael@0 74 keypressHandled = false;
michael@0 75 keyupHandled = false;
michael@0 76 compositionstartHandled = false;
michael@0 77 compositionendHandled = false;
michael@0 78 compositionupdateHandled = false;
michael@0 79 inputHandled = false;
michael@0 80 }
michael@0 81
michael@0 82 function onEvent(aEvent)
michael@0 83 {
michael@0 84 if (aEvent.type == "keydown") {
michael@0 85 keydownHandled = true;
michael@0 86 } else if (aEvent.type == "keypress") {
michael@0 87 keypressHandled = true;
michael@0 88 } else if (aEvent.type == "keyup") {
michael@0 89 keyupHandled = true;
michael@0 90 } else if (aEvent.type == "compositionstart") {
michael@0 91 compositionstartHandled = true;
michael@0 92 } else if (aEvent.type == "compositionend") {
michael@0 93 compositionendHandled = true;
michael@0 94 } else if (aEvent.type == "compositionupdate") {
michael@0 95 compositionupdateHandled = true;
michael@0 96 } else if (aEvent.type == "input") {
michael@0 97 inputHandled = true;
michael@0 98 } else {
michael@0 99 ok(false, "handled unknown event: " + aEvent.type);
michael@0 100 }
michael@0 101 }
michael@0 102
michael@0 103 textarea.addEventListener("keydown", onEvent, false);
michael@0 104 textarea.addEventListener("keypress", onEvent, false);
michael@0 105 textarea.addEventListener("keyup", onEvent, false);
michael@0 106 textarea.addEventListener("compositionstart", onEvent, false);
michael@0 107 textarea.addEventListener("compositionend", onEvent, false);
michael@0 108 textarea.addEventListener("compositionupdate", onEvent, false);
michael@0 109 textarea.addEventListener("input", onEvent, false);
michael@0 110
michael@0 111 startTestsInternal();
michael@0 112
michael@0 113 function startTestsInternal()
michael@0 114 {
michael@0 115 // key events
michael@0 116 function checkKeyEvents(aKeydown, aKeypress, aKeyup, aInput, aDescription)
michael@0 117 {
michael@0 118 is(keydownHandled, aKeydown,
michael@0 119 "keydown event is (not) handled: " + aDescription);
michael@0 120 is(keypressHandled, aKeypress,
michael@0 121 "keypress event is (not) handled: " + aDescription);
michael@0 122 is(keyupHandled, aKeyup,
michael@0 123 "keyup event is (not) handled: " + aDescription);
michael@0 124 is(inputHandled, aInput,
michael@0 125 "input event is (not) handled: " + aDescription);
michael@0 126 }
michael@0 127
michael@0 128 function checkCompositionEvents(aStart, aEnd, aUpdate, aInput, aDescription)
michael@0 129 {
michael@0 130 is(compositionstartHandled, aStart,
michael@0 131 "compositionstart event is (not) handled: " + aDescription);
michael@0 132 is(compositionendHandled, aEnd,
michael@0 133 "compositionend event is (not) handled: " + aDescription);
michael@0 134 is(compositionupdateHandled, aUpdate,
michael@0 135 "compositionupdate event is (not) handled: " + aDescription);
michael@0 136 is(inputHandled, aInput,
michael@0 137 "input event is (not) handled: " + aDescription);
michael@0 138 }
michael@0 139
michael@0 140 clear();
michael@0 141 synthesizeKey("a", { type: "keydown" });
michael@0 142 checkKeyEvents(true, false, false, false, "a keydown");
michael@0 143 clear();
michael@0 144 synthesizeKey("a", { type: "keypress" });
michael@0 145 checkKeyEvents(false, true, false, true, "a keypress");
michael@0 146 is(textarea.value, "a", "textarea value isn't 'a'");
michael@0 147 clear();
michael@0 148 synthesizeKey("a", { type: "keyup" });
michael@0 149 checkKeyEvents(false, false, true, false, "a keyup");
michael@0 150 clear();
michael@0 151 synthesizeKey("VK_BACK_SPACE", {});
michael@0 152 checkKeyEvents(true, true, true, true, "VK_BACK_SPACE key events");
michael@0 153 is(textarea.value, "", "textarea value isn't empty");
michael@0 154
michael@0 155 // IME events
michael@0 156 clear();
michael@0 157 // start composition
michael@0 158 synthesizeComposition({ type: "compositionstart" });
michael@0 159 checkCompositionEvents(true, false, false, false, "compositionstart");
michael@0 160 clear();
michael@0 161 // input first character
michael@0 162 synthesizeComposition({ type: "compositionupdate", data: "\u3089" });
michael@0 163 synthesizeText(
michael@0 164 { "composition":
michael@0 165 { "string": "\u3089",
michael@0 166 "clauses":
michael@0 167 [
michael@0 168 { "length": 1, "attr": COMPOSITION_ATTR_RAWINPUT }
michael@0 169 ]
michael@0 170 },
michael@0 171 "caret": { "start": 1, "length": 0 }
michael@0 172 });
michael@0 173 checkCompositionEvents(false, false, true, true, "composing");
michael@0 174 var queryText = synthesizeQueryTextContent(0, 100);
michael@0 175 ok(queryText, "query text event result is null");
michael@0 176 if (!queryText) {
michael@0 177 return;
michael@0 178 }
michael@0 179 ok(queryText.succeeded, "query text event failed");
michael@0 180 if (!queryText.succeeded) {
michael@0 181 return;
michael@0 182 }
michael@0 183 is(queryText.text, "\u3089", "composing text is incorrect");
michael@0 184 var querySelectedText = synthesizeQuerySelectedText();
michael@0 185 ok(querySelectedText, "query selected text event result is null");
michael@0 186 if (!querySelectedText) {
michael@0 187 return;
michael@0 188 }
michael@0 189 ok(querySelectedText.succeeded, "query selected text event failed");
michael@0 190 if (!querySelectedText.succeeded) {
michael@0 191 return;
michael@0 192 }
michael@0 193 is(querySelectedText.offset, 1,
michael@0 194 "query selected text event returns wrong offset");
michael@0 195 is(querySelectedText.text, "",
michael@0 196 "query selected text event returns wrong selected text");
michael@0 197 clear();
michael@0 198 // commit composition
michael@0 199 synthesizeText(
michael@0 200 { "composition":
michael@0 201 { "string": "\u3089",
michael@0 202 "clauses":
michael@0 203 [
michael@0 204 { "length": 0, "attr": 0 }
michael@0 205 ]
michael@0 206 },
michael@0 207 "caret": { "start": 1, "length": 0 }
michael@0 208 });
michael@0 209 checkCompositionEvents(false, false, false, false, "commit composition");
michael@0 210 queryText = synthesizeQueryTextContent(0, 100);
michael@0 211 ok(queryText, "query text event result is null after commit");
michael@0 212 if (!queryText) {
michael@0 213 return;
michael@0 214 }
michael@0 215 ok(queryText.succeeded, "query text event failed after commit");
michael@0 216 if (!queryText.succeeded) {
michael@0 217 return;
michael@0 218 }
michael@0 219 is(queryText.text, "\u3089", "composing text is incorrect after commit");
michael@0 220 querySelectedText = synthesizeQuerySelectedText();
michael@0 221 ok(querySelectedText,
michael@0 222 "query selected text event result is null after commit");
michael@0 223 if (!querySelectedText) {
michael@0 224 return;
michael@0 225 }
michael@0 226 ok(querySelectedText.succeeded,
michael@0 227 "query selected text event failed after commit");
michael@0 228 if (!querySelectedText.succeeded) {
michael@0 229 return;
michael@0 230 }
michael@0 231 is(querySelectedText.offset, 1,
michael@0 232 "query selected text event returns wrong offset after commit");
michael@0 233 is(querySelectedText.text, "",
michael@0 234 "query selected text event returns wrong selected text after commit");
michael@0 235 clear();
michael@0 236 // end composition
michael@0 237 synthesizeComposition({ type: "compositionend", data: "\u3089" });
michael@0 238 checkCompositionEvents(false, true, false, true, "compositionend");
michael@0 239 }
michael@0 240
michael@0 241 textarea.removeEventListener("keydown", onEvent, false);
michael@0 242 textarea.removeEventListener("keypress", onEvent, false);
michael@0 243 textarea.removeEventListener("keyup", onEvent, false);
michael@0 244 textarea.removeEventListener("compositionstart", onEvent, false);
michael@0 245 textarea.removeEventListener("compositionupdate", onEvent, false);
michael@0 246 textarea.removeEventListener("compositionend", onEvent, false);
michael@0 247 textarea.removeEventListener("input", onEvent, false);
michael@0 248
michael@0 249 otherWindow.close();
michael@0 250
michael@0 251 SimpleTest.finish();
michael@0 252 }
michael@0 253
michael@0 254
michael@0 255 ]]>
michael@0 256 </script>
michael@0 257 </window>

mercurial