dom/base/test/test_sendQueryContentAndSelectionSetEvent.html

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 <!DOCTYPE HTML>
michael@0 2 <html>
michael@0 3 <head>
michael@0 4 <title>Test for nsIDOMWindowUtils.sendQueryContentEvent() and .sendSelectionSetEvent()</title>
michael@0 5 <script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
michael@0 6 <link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css"/>
michael@0 7 </head>
michael@0 8 <body>
michael@0 9 <p id="display"></p>
michael@0 10 <div id="content" style="display: none">
michael@0 11
michael@0 12 </div>
michael@0 13 <div id="editor" contenteditable>abc<br>def</div>
michael@0 14 <pre id="test">
michael@0 15 <script type="application/javascript">
michael@0 16
michael@0 17 var editor = document.getElementById("editor");
michael@0 18
michael@0 19 SimpleTest.waitForExplicitFinish();
michael@0 20
michael@0 21 const kIsWin = navigator.platform.indexOf("Win") == 0;
michael@0 22 const kIsMac = navigator.platform.indexOf("Mac") == 0;
michael@0 23
michael@0 24 const kLineBreak = kIsWin ? "\r\n" : kIsMac ? "\r" : "\n";
michael@0 25
michael@0 26 var gUtils = window.QueryInterface(Components.interfaces.nsIInterfaceRequestor)
michael@0 27 .getInterface(Components.interfaces.nsIDOMWindowUtils);
michael@0 28
michael@0 29 function escape(aStr)
michael@0 30 {
michael@0 31 var result = aStr.replace("\n", "\\n");
michael@0 32 return result.replace("\r", "\\r");
michael@0 33 }
michael@0 34
michael@0 35 function runTests()
michael@0 36 {
michael@0 37 editor.focus();
michael@0 38
michael@0 39 // NOTE: For compatibility, calling without flags should work as with QUERY_CONTENT_FLAG_USE_NATIVE_LINE_BREAK.
michael@0 40
michael@0 41 // QueryTextContent
michael@0 42 var expectedStr = escape(("abc" + kLineBreak + "def").substr(2, 4));
michael@0 43 var result = gUtils.sendQueryContentEvent(gUtils.QUERY_TEXT_CONTENT, 2, 4, 0, 0,
michael@0 44 gUtils.QUERY_CONTENT_FLAG_USE_NATIVE_LINE_BREAK);
michael@0 45 ok(result.succeeded,
michael@0 46 "sendQueryContentEvent(QUERY_TEXT_CONTENT, QUERY_CONTENT_FLAG_USE_NATIVE_LINE_BREAK) should succeed");
michael@0 47 is(escape(result.text), expectedStr,
michael@0 48 "sendQueryContentEvent(QUERY_TEXT_CONTENT, QUERY_CONTENT_FLAG_USE_NATIVE_LINE_BREAK) got unexpected string");
michael@0 49
michael@0 50 result = gUtils.sendQueryContentEvent(gUtils.QUERY_TEXT_CONTENT, 2, 4, 0, 0);
michael@0 51 ok(result.succeeded,
michael@0 52 "sendQueryContentEvent(QUERY_TEXT_CONTENT) should succeed");
michael@0 53 is(escape(result.text), expectedStr,
michael@0 54 "sendQueryContentEvent(QUERY_TEXT_CONTENT) should return same string as calling with QUERY_CONTENT_FLAG_USE_NATIVE_LINE_BREAK");
michael@0 55
michael@0 56 expectedStr = escape(("abc\ndef").substr(2, 4));
michael@0 57 result = gUtils.sendQueryContentEvent(gUtils.QUERY_TEXT_CONTENT, 2, 4, 0, 0,
michael@0 58 gUtils.QUERY_CONTENT_FLAG_USE_XP_LINE_BREAK);
michael@0 59 ok(result.succeeded,
michael@0 60 "sendQueryContentEvent(QUERY_TEXT_CONTENT, QUERY_CONTENT_FLAG_USE_XP_LINE_BREAK) should succeed");
michael@0 61 is(escape(result.text), expectedStr,
michael@0 62 "sendQueryContentEvent(QUERY_TEXT_CONTENT, QUERY_CONTENT_FLAG_USE_XP_LINE_BREAK) got unexpected string");
michael@0 63
michael@0 64 // QueryCaretRect
michael@0 65 window.getSelection().collapse(editor.firstChild, 0);
michael@0 66
michael@0 67 var caretNative = gUtils.sendQueryContentEvent(gUtils.QUERY_CARET_RECT, 6, 0, 0, 0,
michael@0 68 gUtils.QUERY_CONTENT_FLAG_USE_NATIVE_LINE_BREAK);
michael@0 69 ok(caretNative.succeeded,
michael@0 70 "sendQueryContentEvent(QUERY_CARET_RECT, QUERY_CONTENT_FLAG_USE_NATIVE_LINE_BREAK) should succeed");
michael@0 71 var caretXP = gUtils.sendQueryContentEvent(gUtils.QUERY_CARET_RECT, 6 - kLineBreak.length + 1, 0, 0, 0,
michael@0 72 gUtils.QUERY_CONTENT_FLAG_USE_XP_LINE_BREAK);
michael@0 73 ok(caretXP.succeeded,
michael@0 74 "sendQueryContentEvent(QUERY_CARET_RECT, QUERY_CONTENT_FLAG_USE_XP_LINE_BREAK) should succeed");
michael@0 75
michael@0 76 is(caretXP.top, caretNative.top,
michael@0 77 "The caret top should be same");
michael@0 78 is(caretXP.left, caretNative.left,
michael@0 79 "The caret left should be same");
michael@0 80
michael@0 81 result = gUtils.sendQueryContentEvent(gUtils.QUERY_CARET_RECT, 6, 0, 0, 0);
michael@0 82 ok(result.succeeded,
michael@0 83 "sendQueryContentEvent(QUERY_CARET_RECT) should succeed");
michael@0 84 is(result.top, caretNative.top,
michael@0 85 "sendQueryContentEvent(QUERY_CARET_RECT) should return same top as calling with QUERY_CONTENT_FLAG_USE_NATIVE_LINE_BREAK");
michael@0 86 is(result.left, caretNative.left,
michael@0 87 "sendQueryContentEvent(QUERY_CARET_RECT) should return same left as calling with QUERY_CONTENT_FLAG_USE_NATIVE_LINE_BREAK");
michael@0 88
michael@0 89 // QueryTextRect
michael@0 90 var textRectNative = gUtils.sendQueryContentEvent(gUtils.QUERY_TEXT_RECT, 6, 1, 0, 0,
michael@0 91 gUtils.QUERY_CONTENT_FLAG_USE_NATIVE_LINE_BREAK);
michael@0 92 ok(textRectNative.succeeded,
michael@0 93 "sendQueryContentEvent(QUERY_TEXT_RECT, QUERY_CONTENT_FLAG_USE_NATIVE_LINE_BREAK) should succeed");
michael@0 94 var textRectXP = gUtils.sendQueryContentEvent(gUtils.QUERY_TEXT_RECT, 6 - kLineBreak.length + 1, 1, 0, 0,
michael@0 95 gUtils.QUERY_CONTENT_FLAG_USE_XP_LINE_BREAK);
michael@0 96 ok(textRectXP.succeeded,
michael@0 97 "sendQueryContentEvent(QUERY_TEXT_RECT, QUERY_CONTENT_FLAG_USE_XP_LINE_BREAK) should succeed");
michael@0 98
michael@0 99 is(textRectXP.top, textRectNative.top,
michael@0 100 "The text top should be same");
michael@0 101 is(textRectXP.left, textRectNative.left,
michael@0 102 "The text left should be same");
michael@0 103 is(textRectXP.height, textRectNative.height,
michael@0 104 "The text height should be same");
michael@0 105 is(textRectXP.width, textRectNative.width,
michael@0 106 "The text width should be same");
michael@0 107
michael@0 108 result = gUtils.sendQueryContentEvent(gUtils.QUERY_TEXT_RECT, 6, 1, 0, 0);
michael@0 109 ok(result.succeeded,
michael@0 110 "sendQueryContentEvent(QUERY_TEXT_RECT) should succeed");
michael@0 111 is(result.top, textRectNative.top,
michael@0 112 "sendQueryContentEvent(QUERY_TEXT_RECT) should return same top as calling with QUERY_CONTENT_FLAG_USE_NATIVE_LINE_BREAK");
michael@0 113 is(result.left, textRectNative.left,
michael@0 114 "sendQueryContentEvent(QUERY_TEXT_RECT) should return same left as calling with QUERY_CONTENT_FLAG_USE_NATIVE_LINE_BREAK");
michael@0 115 is(result.height, textRectNative.height,
michael@0 116 "sendQueryContentEvent(QUERY_TEXT_RECT) should return same height as calling with QUERY_CONTENT_FLAG_USE_NATIVE_LINE_BREAK");
michael@0 117 is(result.width, textRectNative.width,
michael@0 118 "sendQueryContentEvent(QUERY_TEXT_RECT) should return same width as calling with QUERY_CONTENT_FLAG_USE_NATIVE_LINE_BREAK");
michael@0 119
michael@0 120 // QueryCharacterAtOffset
michael@0 121 result = gUtils.sendQueryContentEvent(gUtils.QUERY_CHARACTER_AT_POINT, 0, 0, textRectNative.left + 1, textRectNative.top + 1,
michael@0 122 gUtils.QUERY_CONTENT_FLAG_USE_NATIVE_LINE_BREAK);
michael@0 123 ok(result.succeeded,
michael@0 124 "sendQueryContentEvent(QUERY_CHARACTER_AT_POINT, QUERY_CONTENT_FLAG_USE_NATIVE_LINE_BREAK) should succeed");
michael@0 125 is(result.top, textRectNative.top,
michael@0 126 "The character top is wrong");
michael@0 127 is(result.left, textRectNative.left,
michael@0 128 "The character left is wrong");
michael@0 129 is(result.height, textRectNative.height,
michael@0 130 "The character height is wrong");
michael@0 131 is(result.width, textRectNative.width,
michael@0 132 "The character width is wrong");
michael@0 133 is(result.offset, 6,
michael@0 134 "The character offset is wrong");
michael@0 135
michael@0 136 result = gUtils.sendQueryContentEvent(gUtils.QUERY_CHARACTER_AT_POINT, 0, 0, textRectNative.left + 1, textRectNative.top + 1);
michael@0 137 ok(result.succeeded,
michael@0 138 "sendQueryContentEvent(QUERY_CHARACTER_AT_POINT) should succeed");
michael@0 139 is(result.top, textRectNative.top,
michael@0 140 "The character top should be same as calling with QUERY_CONTENT_FLAG_USE_NATIVE_LINE_BREAK");
michael@0 141 is(result.left, textRectNative.left,
michael@0 142 "The character left should be same as calling with QUERY_CONTENT_FLAG_USE_NATIVE_LINE_BREAK");
michael@0 143 is(result.height, textRectNative.height,
michael@0 144 "The character height should be same as calling with QUERY_CONTENT_FLAG_USE_NATIVE_LINE_BREAK");
michael@0 145 is(result.width, textRectNative.width,
michael@0 146 "The character width should be same as calling with QUERY_CONTENT_FLAG_USE_NATIVE_LINE_BREAK");
michael@0 147 is(result.offset, 6,
michael@0 148 "The character offset should be same as calling with QUERY_CONTENT_FLAG_USE_NATIVE_LINE_BREAK");
michael@0 149
michael@0 150 result = gUtils.sendQueryContentEvent(gUtils.QUERY_CHARACTER_AT_POINT, 0, 0, textRectXP.left + 1, textRectXP.top + 1,
michael@0 151 gUtils.QUERY_CONTENT_FLAG_USE_XP_LINE_BREAK);
michael@0 152 ok(result.succeeded,
michael@0 153 "sendQueryContentEvent(QUERY_CHARACTER_AT_POINT, QUERY_CONTENT_FLAG_USE_XP_LINE_BREAK) should succeed");
michael@0 154 is(result.top, textRectXP.top,
michael@0 155 "The character top is wrong");
michael@0 156 is(result.left, textRectXP.left,
michael@0 157 "The character left is wrong");
michael@0 158 is(result.height, textRectXP.height,
michael@0 159 "The character height is wrong");
michael@0 160 is(result.width, textRectXP.width,
michael@0 161 "The character width is wrong");
michael@0 162 is(result.offset, 6 - kLineBreak.length + 1,
michael@0 163 "The character offset is wrong");
michael@0 164
michael@0 165 // SelectionSet and QuerySelectedText
michael@0 166 var selectionSet = gUtils.sendSelectionSetEvent(0, 6, gUtils.SELECTION_SET_FLAG_USE_NATIVE_LINE_BREAK);
michael@0 167 ok(selectionSet,
michael@0 168 "sendSelectionSetEvent(0, 6, SELECTION_SET_FLAG_USE_NATIVE_LINE_BREAK) should succeed");
michael@0 169 expectedStr = escape(("abc" + kLineBreak + "def").substr(0, 6));
michael@0 170
michael@0 171 result = gUtils.sendQueryContentEvent(gUtils.QUERY_SELECTED_TEXT, 0, 0, 0, 0,
michael@0 172 gUtils.QUERY_CONTENT_FLAG_USE_NATIVE_LINE_BREAK);
michael@0 173 ok(result.succeeded,
michael@0 174 "sendQueryContentEvent(QUERY_SELECTED_TEXT, QUERY_CONTENT_FLAG_USE_NATIVE_LINE_BREAK) should succeed");
michael@0 175 ok(!result.reversed,
michael@0 176 "sendSelectionSetEvent(0, 6, SELECTION_SET_FLAG_USE_NATIVE_LINE_BREAK) should set non-reversed selection");
michael@0 177 is(escape(result.text), expectedStr,
michael@0 178 "sendQueryContentEvent(QUERY_SELECTED_TEXT, QUERY_CONTENT_FLAG_USE_NATIVE_LINE_BREAK) got unexpected string");
michael@0 179
michael@0 180 selectionSet = gUtils.sendSelectionSetEvent(0, 6, gUtils.SELECTION_SET_FLAG_USE_XP_LINE_BREAK);
michael@0 181 ok(selectionSet,
michael@0 182 "sendSelectionSetEvent(0, 6, SELECTION_SET_FLAG_USE_XP_LINE_BREAK) should succeed");
michael@0 183 expectedStr = escape(("abc\ndef").substr(0, 6));
michael@0 184
michael@0 185 result = gUtils.sendQueryContentEvent(gUtils.QUERY_SELECTED_TEXT, 0, 0, 0, 0,
michael@0 186 gUtils.QUERY_CONTENT_FLAG_USE_XP_LINE_BREAK);
michael@0 187 ok(result.succeeded,
michael@0 188 "sendQueryContentEvent(QUERY_SELECTED_TEXT, QUERY_CONTENT_FLAG_USE_XP_LINE_BREAK) should succeed");
michael@0 189 ok(!result.reversed,
michael@0 190 "sendSelectionSetEvent(0, 6, SELECTION_SET_FLAG_USE_XP_LINE_BREAK) should set non-reversed selection");
michael@0 191 is(escape(result.text), expectedStr,
michael@0 192 "sendQueryContentEvent(QUERY_SELECTED_TEXT, QUERY_CONTENT_FLAG_USE_XP_LINE_BREAK) got unexpected string");
michael@0 193
michael@0 194 var selectionSet = gUtils.sendSelectionSetEvent(0, 6, gUtils.SELECTION_SET_FLAG_USE_NATIVE_LINE_BREAK | gUtils.SELECTION_SET_FLAG_REVERSE);
michael@0 195 ok(selectionSet,
michael@0 196 "sendSelectionSetEvent(0, 6, SELECTION_SET_FLAG_USE_NATIVE_LINE_BREAK) should succeed");
michael@0 197
michael@0 198 result = gUtils.sendQueryContentEvent(gUtils.QUERY_SELECTED_TEXT, 0, 0, 0, 0,
michael@0 199 gUtils.QUERY_CONTENT_FLAG_USE_NATIVE_LINE_BREAK);
michael@0 200 ok(result.succeeded,
michael@0 201 "sendQueryContentEvent(QUERY_SELECTED_TEXT, QUERY_CONTENT_FLAG_USE_NATIVE_LINE_BREAK) should succeed");
michael@0 202 ok(result.reversed,
michael@0 203 "sendSelectionSetEvent(0, 6, SELECTION_SET_FLAG_USE_NATIVE_LINE_BREAK | SELECTION_SET_FLAG_REVERSE) should set reversed selection");
michael@0 204
michael@0 205 selectionSet = gUtils.sendSelectionSetEvent(0, 6, gUtils.SELECTION_SET_FLAG_USE_XP_LINE_BREAK | gUtils.SELECTION_SET_FLAG_REVERSE);
michael@0 206 ok(selectionSet,
michael@0 207 "sendSelectionSetEvent(0, 6, SELECTION_SET_FLAG_USE_XP_LINE_BREAK | SELECTION_SET_FLAG_REVERSE) should succeed");
michael@0 208
michael@0 209 result = gUtils.sendQueryContentEvent(gUtils.QUERY_SELECTED_TEXT, 0, 0, 0, 0,
michael@0 210 gUtils.QUERY_CONTENT_FLAG_USE_XP_LINE_BREAK);
michael@0 211 ok(result.succeeded,
michael@0 212 "sendQueryContentEvent(QUERY_SELECTED_TEXT, QUERY_CONTENT_FLAG_USE_XP_LINE_BREAK) should succeed");
michael@0 213 ok(result.reversed,
michael@0 214 "sendSelectionSetEvent(0, 6, SELECTION_SET_FLAG_USE_XP_LINE_BREAK | SELECTION_SET_FLAG_REVERSE) should set reversed selection");
michael@0 215
michael@0 216 SimpleTest.finish();
michael@0 217 }
michael@0 218
michael@0 219 SimpleTest.waitForFocus(runTests);
michael@0 220
michael@0 221 </script>
michael@0 222 </pre>
michael@0 223 </body>
michael@0 224 </html>

mercurial