1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/dom/base/test/test_sendQueryContentAndSelectionSetEvent.html Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,224 @@ 1.4 +<!DOCTYPE HTML> 1.5 +<html> 1.6 +<head> 1.7 + <title>Test for nsIDOMWindowUtils.sendQueryContentEvent() and .sendSelectionSetEvent()</title> 1.8 + <script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script> 1.9 + <link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css"/> 1.10 +</head> 1.11 +<body> 1.12 +<p id="display"></p> 1.13 +<div id="content" style="display: none"> 1.14 + 1.15 +</div> 1.16 +<div id="editor" contenteditable>abc<br>def</div> 1.17 +<pre id="test"> 1.18 +<script type="application/javascript"> 1.19 + 1.20 +var editor = document.getElementById("editor"); 1.21 + 1.22 +SimpleTest.waitForExplicitFinish(); 1.23 + 1.24 +const kIsWin = navigator.platform.indexOf("Win") == 0; 1.25 +const kIsMac = navigator.platform.indexOf("Mac") == 0; 1.26 + 1.27 +const kLineBreak = kIsWin ? "\r\n" : kIsMac ? "\r" : "\n"; 1.28 + 1.29 +var gUtils = window.QueryInterface(Components.interfaces.nsIInterfaceRequestor) 1.30 + .getInterface(Components.interfaces.nsIDOMWindowUtils); 1.31 + 1.32 +function escape(aStr) 1.33 +{ 1.34 + var result = aStr.replace("\n", "\\n"); 1.35 + return result.replace("\r", "\\r"); 1.36 +} 1.37 + 1.38 +function runTests() 1.39 +{ 1.40 + editor.focus(); 1.41 + 1.42 + // NOTE: For compatibility, calling without flags should work as with QUERY_CONTENT_FLAG_USE_NATIVE_LINE_BREAK. 1.43 + 1.44 + // QueryTextContent 1.45 + var expectedStr = escape(("abc" + kLineBreak + "def").substr(2, 4)); 1.46 + var result = gUtils.sendQueryContentEvent(gUtils.QUERY_TEXT_CONTENT, 2, 4, 0, 0, 1.47 + gUtils.QUERY_CONTENT_FLAG_USE_NATIVE_LINE_BREAK); 1.48 + ok(result.succeeded, 1.49 + "sendQueryContentEvent(QUERY_TEXT_CONTENT, QUERY_CONTENT_FLAG_USE_NATIVE_LINE_BREAK) should succeed"); 1.50 + is(escape(result.text), expectedStr, 1.51 + "sendQueryContentEvent(QUERY_TEXT_CONTENT, QUERY_CONTENT_FLAG_USE_NATIVE_LINE_BREAK) got unexpected string"); 1.52 + 1.53 + result = gUtils.sendQueryContentEvent(gUtils.QUERY_TEXT_CONTENT, 2, 4, 0, 0); 1.54 + ok(result.succeeded, 1.55 + "sendQueryContentEvent(QUERY_TEXT_CONTENT) should succeed"); 1.56 + is(escape(result.text), expectedStr, 1.57 + "sendQueryContentEvent(QUERY_TEXT_CONTENT) should return same string as calling with QUERY_CONTENT_FLAG_USE_NATIVE_LINE_BREAK"); 1.58 + 1.59 + expectedStr = escape(("abc\ndef").substr(2, 4)); 1.60 + result = gUtils.sendQueryContentEvent(gUtils.QUERY_TEXT_CONTENT, 2, 4, 0, 0, 1.61 + gUtils.QUERY_CONTENT_FLAG_USE_XP_LINE_BREAK); 1.62 + ok(result.succeeded, 1.63 + "sendQueryContentEvent(QUERY_TEXT_CONTENT, QUERY_CONTENT_FLAG_USE_XP_LINE_BREAK) should succeed"); 1.64 + is(escape(result.text), expectedStr, 1.65 + "sendQueryContentEvent(QUERY_TEXT_CONTENT, QUERY_CONTENT_FLAG_USE_XP_LINE_BREAK) got unexpected string"); 1.66 + 1.67 + // QueryCaretRect 1.68 + window.getSelection().collapse(editor.firstChild, 0); 1.69 + 1.70 + var caretNative = gUtils.sendQueryContentEvent(gUtils.QUERY_CARET_RECT, 6, 0, 0, 0, 1.71 + gUtils.QUERY_CONTENT_FLAG_USE_NATIVE_LINE_BREAK); 1.72 + ok(caretNative.succeeded, 1.73 + "sendQueryContentEvent(QUERY_CARET_RECT, QUERY_CONTENT_FLAG_USE_NATIVE_LINE_BREAK) should succeed"); 1.74 + var caretXP = gUtils.sendQueryContentEvent(gUtils.QUERY_CARET_RECT, 6 - kLineBreak.length + 1, 0, 0, 0, 1.75 + gUtils.QUERY_CONTENT_FLAG_USE_XP_LINE_BREAK); 1.76 + ok(caretXP.succeeded, 1.77 + "sendQueryContentEvent(QUERY_CARET_RECT, QUERY_CONTENT_FLAG_USE_XP_LINE_BREAK) should succeed"); 1.78 + 1.79 + is(caretXP.top, caretNative.top, 1.80 + "The caret top should be same"); 1.81 + is(caretXP.left, caretNative.left, 1.82 + "The caret left should be same"); 1.83 + 1.84 + result = gUtils.sendQueryContentEvent(gUtils.QUERY_CARET_RECT, 6, 0, 0, 0); 1.85 + ok(result.succeeded, 1.86 + "sendQueryContentEvent(QUERY_CARET_RECT) should succeed"); 1.87 + is(result.top, caretNative.top, 1.88 + "sendQueryContentEvent(QUERY_CARET_RECT) should return same top as calling with QUERY_CONTENT_FLAG_USE_NATIVE_LINE_BREAK"); 1.89 + is(result.left, caretNative.left, 1.90 + "sendQueryContentEvent(QUERY_CARET_RECT) should return same left as calling with QUERY_CONTENT_FLAG_USE_NATIVE_LINE_BREAK"); 1.91 + 1.92 + // QueryTextRect 1.93 + var textRectNative = gUtils.sendQueryContentEvent(gUtils.QUERY_TEXT_RECT, 6, 1, 0, 0, 1.94 + gUtils.QUERY_CONTENT_FLAG_USE_NATIVE_LINE_BREAK); 1.95 + ok(textRectNative.succeeded, 1.96 + "sendQueryContentEvent(QUERY_TEXT_RECT, QUERY_CONTENT_FLAG_USE_NATIVE_LINE_BREAK) should succeed"); 1.97 + var textRectXP = gUtils.sendQueryContentEvent(gUtils.QUERY_TEXT_RECT, 6 - kLineBreak.length + 1, 1, 0, 0, 1.98 + gUtils.QUERY_CONTENT_FLAG_USE_XP_LINE_BREAK); 1.99 + ok(textRectXP.succeeded, 1.100 + "sendQueryContentEvent(QUERY_TEXT_RECT, QUERY_CONTENT_FLAG_USE_XP_LINE_BREAK) should succeed"); 1.101 + 1.102 + is(textRectXP.top, textRectNative.top, 1.103 + "The text top should be same"); 1.104 + is(textRectXP.left, textRectNative.left, 1.105 + "The text left should be same"); 1.106 + is(textRectXP.height, textRectNative.height, 1.107 + "The text height should be same"); 1.108 + is(textRectXP.width, textRectNative.width, 1.109 + "The text width should be same"); 1.110 + 1.111 + result = gUtils.sendQueryContentEvent(gUtils.QUERY_TEXT_RECT, 6, 1, 0, 0); 1.112 + ok(result.succeeded, 1.113 + "sendQueryContentEvent(QUERY_TEXT_RECT) should succeed"); 1.114 + is(result.top, textRectNative.top, 1.115 + "sendQueryContentEvent(QUERY_TEXT_RECT) should return same top as calling with QUERY_CONTENT_FLAG_USE_NATIVE_LINE_BREAK"); 1.116 + is(result.left, textRectNative.left, 1.117 + "sendQueryContentEvent(QUERY_TEXT_RECT) should return same left as calling with QUERY_CONTENT_FLAG_USE_NATIVE_LINE_BREAK"); 1.118 + is(result.height, textRectNative.height, 1.119 + "sendQueryContentEvent(QUERY_TEXT_RECT) should return same height as calling with QUERY_CONTENT_FLAG_USE_NATIVE_LINE_BREAK"); 1.120 + is(result.width, textRectNative.width, 1.121 + "sendQueryContentEvent(QUERY_TEXT_RECT) should return same width as calling with QUERY_CONTENT_FLAG_USE_NATIVE_LINE_BREAK"); 1.122 + 1.123 + // QueryCharacterAtOffset 1.124 + result = gUtils.sendQueryContentEvent(gUtils.QUERY_CHARACTER_AT_POINT, 0, 0, textRectNative.left + 1, textRectNative.top + 1, 1.125 + gUtils.QUERY_CONTENT_FLAG_USE_NATIVE_LINE_BREAK); 1.126 + ok(result.succeeded, 1.127 + "sendQueryContentEvent(QUERY_CHARACTER_AT_POINT, QUERY_CONTENT_FLAG_USE_NATIVE_LINE_BREAK) should succeed"); 1.128 + is(result.top, textRectNative.top, 1.129 + "The character top is wrong"); 1.130 + is(result.left, textRectNative.left, 1.131 + "The character left is wrong"); 1.132 + is(result.height, textRectNative.height, 1.133 + "The character height is wrong"); 1.134 + is(result.width, textRectNative.width, 1.135 + "The character width is wrong"); 1.136 + is(result.offset, 6, 1.137 + "The character offset is wrong"); 1.138 + 1.139 + result = gUtils.sendQueryContentEvent(gUtils.QUERY_CHARACTER_AT_POINT, 0, 0, textRectNative.left + 1, textRectNative.top + 1); 1.140 + ok(result.succeeded, 1.141 + "sendQueryContentEvent(QUERY_CHARACTER_AT_POINT) should succeed"); 1.142 + is(result.top, textRectNative.top, 1.143 + "The character top should be same as calling with QUERY_CONTENT_FLAG_USE_NATIVE_LINE_BREAK"); 1.144 + is(result.left, textRectNative.left, 1.145 + "The character left should be same as calling with QUERY_CONTENT_FLAG_USE_NATIVE_LINE_BREAK"); 1.146 + is(result.height, textRectNative.height, 1.147 + "The character height should be same as calling with QUERY_CONTENT_FLAG_USE_NATIVE_LINE_BREAK"); 1.148 + is(result.width, textRectNative.width, 1.149 + "The character width should be same as calling with QUERY_CONTENT_FLAG_USE_NATIVE_LINE_BREAK"); 1.150 + is(result.offset, 6, 1.151 + "The character offset should be same as calling with QUERY_CONTENT_FLAG_USE_NATIVE_LINE_BREAK"); 1.152 + 1.153 + result = gUtils.sendQueryContentEvent(gUtils.QUERY_CHARACTER_AT_POINT, 0, 0, textRectXP.left + 1, textRectXP.top + 1, 1.154 + gUtils.QUERY_CONTENT_FLAG_USE_XP_LINE_BREAK); 1.155 + ok(result.succeeded, 1.156 + "sendQueryContentEvent(QUERY_CHARACTER_AT_POINT, QUERY_CONTENT_FLAG_USE_XP_LINE_BREAK) should succeed"); 1.157 + is(result.top, textRectXP.top, 1.158 + "The character top is wrong"); 1.159 + is(result.left, textRectXP.left, 1.160 + "The character left is wrong"); 1.161 + is(result.height, textRectXP.height, 1.162 + "The character height is wrong"); 1.163 + is(result.width, textRectXP.width, 1.164 + "The character width is wrong"); 1.165 + is(result.offset, 6 - kLineBreak.length + 1, 1.166 + "The character offset is wrong"); 1.167 + 1.168 + // SelectionSet and QuerySelectedText 1.169 + var selectionSet = gUtils.sendSelectionSetEvent(0, 6, gUtils.SELECTION_SET_FLAG_USE_NATIVE_LINE_BREAK); 1.170 + ok(selectionSet, 1.171 + "sendSelectionSetEvent(0, 6, SELECTION_SET_FLAG_USE_NATIVE_LINE_BREAK) should succeed"); 1.172 + expectedStr = escape(("abc" + kLineBreak + "def").substr(0, 6)); 1.173 + 1.174 + result = gUtils.sendQueryContentEvent(gUtils.QUERY_SELECTED_TEXT, 0, 0, 0, 0, 1.175 + gUtils.QUERY_CONTENT_FLAG_USE_NATIVE_LINE_BREAK); 1.176 + ok(result.succeeded, 1.177 + "sendQueryContentEvent(QUERY_SELECTED_TEXT, QUERY_CONTENT_FLAG_USE_NATIVE_LINE_BREAK) should succeed"); 1.178 + ok(!result.reversed, 1.179 + "sendSelectionSetEvent(0, 6, SELECTION_SET_FLAG_USE_NATIVE_LINE_BREAK) should set non-reversed selection"); 1.180 + is(escape(result.text), expectedStr, 1.181 + "sendQueryContentEvent(QUERY_SELECTED_TEXT, QUERY_CONTENT_FLAG_USE_NATIVE_LINE_BREAK) got unexpected string"); 1.182 + 1.183 + selectionSet = gUtils.sendSelectionSetEvent(0, 6, gUtils.SELECTION_SET_FLAG_USE_XP_LINE_BREAK); 1.184 + ok(selectionSet, 1.185 + "sendSelectionSetEvent(0, 6, SELECTION_SET_FLAG_USE_XP_LINE_BREAK) should succeed"); 1.186 + expectedStr = escape(("abc\ndef").substr(0, 6)); 1.187 + 1.188 + result = gUtils.sendQueryContentEvent(gUtils.QUERY_SELECTED_TEXT, 0, 0, 0, 0, 1.189 + gUtils.QUERY_CONTENT_FLAG_USE_XP_LINE_BREAK); 1.190 + ok(result.succeeded, 1.191 + "sendQueryContentEvent(QUERY_SELECTED_TEXT, QUERY_CONTENT_FLAG_USE_XP_LINE_BREAK) should succeed"); 1.192 + ok(!result.reversed, 1.193 + "sendSelectionSetEvent(0, 6, SELECTION_SET_FLAG_USE_XP_LINE_BREAK) should set non-reversed selection"); 1.194 + is(escape(result.text), expectedStr, 1.195 + "sendQueryContentEvent(QUERY_SELECTED_TEXT, QUERY_CONTENT_FLAG_USE_XP_LINE_BREAK) got unexpected string"); 1.196 + 1.197 + var selectionSet = gUtils.sendSelectionSetEvent(0, 6, gUtils.SELECTION_SET_FLAG_USE_NATIVE_LINE_BREAK | gUtils.SELECTION_SET_FLAG_REVERSE); 1.198 + ok(selectionSet, 1.199 + "sendSelectionSetEvent(0, 6, SELECTION_SET_FLAG_USE_NATIVE_LINE_BREAK) should succeed"); 1.200 + 1.201 + result = gUtils.sendQueryContentEvent(gUtils.QUERY_SELECTED_TEXT, 0, 0, 0, 0, 1.202 + gUtils.QUERY_CONTENT_FLAG_USE_NATIVE_LINE_BREAK); 1.203 + ok(result.succeeded, 1.204 + "sendQueryContentEvent(QUERY_SELECTED_TEXT, QUERY_CONTENT_FLAG_USE_NATIVE_LINE_BREAK) should succeed"); 1.205 + ok(result.reversed, 1.206 + "sendSelectionSetEvent(0, 6, SELECTION_SET_FLAG_USE_NATIVE_LINE_BREAK | SELECTION_SET_FLAG_REVERSE) should set reversed selection"); 1.207 + 1.208 + selectionSet = gUtils.sendSelectionSetEvent(0, 6, gUtils.SELECTION_SET_FLAG_USE_XP_LINE_BREAK | gUtils.SELECTION_SET_FLAG_REVERSE); 1.209 + ok(selectionSet, 1.210 + "sendSelectionSetEvent(0, 6, SELECTION_SET_FLAG_USE_XP_LINE_BREAK | SELECTION_SET_FLAG_REVERSE) should succeed"); 1.211 + 1.212 + result = gUtils.sendQueryContentEvent(gUtils.QUERY_SELECTED_TEXT, 0, 0, 0, 0, 1.213 + gUtils.QUERY_CONTENT_FLAG_USE_XP_LINE_BREAK); 1.214 + ok(result.succeeded, 1.215 + "sendQueryContentEvent(QUERY_SELECTED_TEXT, QUERY_CONTENT_FLAG_USE_XP_LINE_BREAK) should succeed"); 1.216 + ok(result.reversed, 1.217 + "sendSelectionSetEvent(0, 6, SELECTION_SET_FLAG_USE_XP_LINE_BREAK | SELECTION_SET_FLAG_REVERSE) should set reversed selection"); 1.218 + 1.219 + SimpleTest.finish(); 1.220 +} 1.221 + 1.222 +SimpleTest.waitForFocus(runTests); 1.223 + 1.224 +</script> 1.225 +</pre> 1.226 +</body> 1.227 +</html>