Thu, 22 Jan 2015 13:21:57 +0100
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 | <!-- |
michael@0 | 4 | https://bugzilla.mozilla.org/show_bug.cgi?id=932145 |
michael@0 | 5 | --> |
michael@0 | 6 | <head> |
michael@0 | 7 | <title>Basic test for InputMethod API.</title> |
michael@0 | 8 | <script type="application/javascript;version=1.7" src="/tests/SimpleTest/SimpleTest.js"></script> |
michael@0 | 9 | <script type="application/javascript;version=1.7" src="inputmethod_common.js"></script> |
michael@0 | 10 | <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/> |
michael@0 | 11 | </head> |
michael@0 | 12 | <body> |
michael@0 | 13 | <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=932145">Mozilla Bug 932145</a> |
michael@0 | 14 | <p id="display"></p> |
michael@0 | 15 | <pre id="test"> |
michael@0 | 16 | <script class="testbody" type="application/javascript;version=1.7"> |
michael@0 | 17 | |
michael@0 | 18 | // The input context. |
michael@0 | 19 | var gContext = null; |
michael@0 | 20 | |
michael@0 | 21 | inputmethod_setup(function() { |
michael@0 | 22 | runTest(); |
michael@0 | 23 | }); |
michael@0 | 24 | |
michael@0 | 25 | function runTest() { |
michael@0 | 26 | let im = navigator.mozInputMethod; |
michael@0 | 27 | |
michael@0 | 28 | im.oninputcontextchange = function() { |
michael@0 | 29 | ok(true, 'inputcontextchange event was fired.'); |
michael@0 | 30 | im.oninputcontextchange = null; |
michael@0 | 31 | |
michael@0 | 32 | gContext = im.inputcontext; |
michael@0 | 33 | if (!gContext) { |
michael@0 | 34 | ok(false, 'Should have a non-null inputcontext.'); |
michael@0 | 35 | inputmethod_cleanup(); |
michael@0 | 36 | return; |
michael@0 | 37 | } |
michael@0 | 38 | |
michael@0 | 39 | todo_is(gContext.type, 'input', 'The input context type should match.'); |
michael@0 | 40 | is(gContext.inputType, 'text', 'The inputType should match.'); |
michael@0 | 41 | is(gContext.inputMode, 'verbatim', 'The input mode should match.'); |
michael@0 | 42 | is(gContext.lang, 'zh', 'The language should match.'); |
michael@0 | 43 | is(gContext.textBeforeCursor + gContext.textAfterCursor, 'Yuan', |
michael@0 | 44 | 'Should get the text around the cursor.'); |
michael@0 | 45 | |
michael@0 | 46 | test_setSelectionRange(); |
michael@0 | 47 | }; |
michael@0 | 48 | |
michael@0 | 49 | // Set current page as an input method. |
michael@0 | 50 | SpecialPowers.wrap(im).setActive(true); |
michael@0 | 51 | |
michael@0 | 52 | let iframe = document.createElement('iframe'); |
michael@0 | 53 | iframe.src = 'file_test_app.html'; |
michael@0 | 54 | iframe.setAttribute('mozbrowser', true); |
michael@0 | 55 | document.body.appendChild(iframe); |
michael@0 | 56 | } |
michael@0 | 57 | |
michael@0 | 58 | function test_setSelectionRange() { |
michael@0 | 59 | // Move cursor position to 2. |
michael@0 | 60 | gContext.setSelectionRange(2, 0).then(function() { |
michael@0 | 61 | is(gContext.selectionStart, 2, 'selectionStart was set successfully.'); |
michael@0 | 62 | is(gContext.selectionEnd, 2, 'selectionEnd was set successfully.'); |
michael@0 | 63 | test_sendKey(); |
michael@0 | 64 | }, function(e) { |
michael@0 | 65 | ok(false, 'setSelectionRange failed:' + e.name); |
michael@0 | 66 | inputmethod_cleanup(); |
michael@0 | 67 | }); |
michael@0 | 68 | } |
michael@0 | 69 | |
michael@0 | 70 | function test_sendKey() { |
michael@0 | 71 | // Add '-' to current cursor posistion and move the cursor position to 3. |
michael@0 | 72 | gContext.sendKey(0, '-'.charCodeAt(0), 0).then(function() { |
michael@0 | 73 | is(gContext.textBeforeCursor + gContext.textAfterCursor, 'Yu-an', |
michael@0 | 74 | 'sendKey should changed the input field correctly.'); |
michael@0 | 75 | test_deleteSurroundingText(); |
michael@0 | 76 | }, function(e) { |
michael@0 | 77 | ok(false, 'sendKey failed:' + e.name); |
michael@0 | 78 | inputmethod_cleanup(); |
michael@0 | 79 | }); |
michael@0 | 80 | } |
michael@0 | 81 | |
michael@0 | 82 | function test_deleteSurroundingText() { |
michael@0 | 83 | // Remove one character before current cursor position and move the cursor |
michael@0 | 84 | // position back to 2. |
michael@0 | 85 | gContext.deleteSurroundingText(-1, 1).then(function() { |
michael@0 | 86 | ok(true, 'deleteSurroundingText finished'); |
michael@0 | 87 | is(gContext.textBeforeCursor + gContext.textAfterCursor, 'Yuan', |
michael@0 | 88 | 'deleteSurroundingText should changed the input field correctly.'); |
michael@0 | 89 | test_replaceSurroundingText(); |
michael@0 | 90 | }, function(e) { |
michael@0 | 91 | ok(false, 'deleteSurroundingText failed:' + e.name); |
michael@0 | 92 | inputmethod_cleanup(); |
michael@0 | 93 | }); |
michael@0 | 94 | } |
michael@0 | 95 | |
michael@0 | 96 | function test_replaceSurroundingText() { |
michael@0 | 97 | // Replace 'Yuan' with 'Xulei'. |
michael@0 | 98 | gContext.replaceSurroundingText('Xulei', -2, 4).then(function() { |
michael@0 | 99 | ok(true, 'replaceSurroundingText finished'); |
michael@0 | 100 | is(gContext.textBeforeCursor + gContext.textAfterCursor, 'Xulei', |
michael@0 | 101 | 'replaceSurroundingText changed the input field correctly.'); |
michael@0 | 102 | test_setComposition(); |
michael@0 | 103 | }, function(e) { |
michael@0 | 104 | ok(false, 'replaceSurroundingText failed: ' + e.name); |
michael@0 | 105 | inputmethod_cleanup(); |
michael@0 | 106 | }); |
michael@0 | 107 | } |
michael@0 | 108 | |
michael@0 | 109 | function test_setComposition() { |
michael@0 | 110 | gContext.setComposition('XXX').then(function() { |
michael@0 | 111 | ok(true, 'setComposition finished'); |
michael@0 | 112 | test_endComposition(); |
michael@0 | 113 | }, function(e) { |
michael@0 | 114 | ok(false, 'setComposition failed: ' + e.name); |
michael@0 | 115 | inputmethod_cleanup(); |
michael@0 | 116 | }); |
michael@0 | 117 | } |
michael@0 | 118 | |
michael@0 | 119 | function test_endComposition() { |
michael@0 | 120 | gContext.endComposition('2013').then(function() { |
michael@0 | 121 | is(gContext.textBeforeCursor + gContext.textAfterCursor, 'Xulei2013', |
michael@0 | 122 | 'endComposition changed the input field correctly.'); |
michael@0 | 123 | test_onSelectionChange(); |
michael@0 | 124 | }, function (e) { |
michael@0 | 125 | ok(false, 'endComposition failed: ' + e.name); |
michael@0 | 126 | inputmethod_cleanup(); |
michael@0 | 127 | }); |
michael@0 | 128 | } |
michael@0 | 129 | |
michael@0 | 130 | function test_onSelectionChange() { |
michael@0 | 131 | var sccTimeout = setTimeout(function() { |
michael@0 | 132 | ok(false, 'selectionchange event not fired'); |
michael@0 | 133 | cleanup(true); |
michael@0 | 134 | }, 3000); |
michael@0 | 135 | |
michael@0 | 136 | function cleanup(failed) { |
michael@0 | 137 | gContext.onselectionchange = null; |
michael@0 | 138 | clearTimeout(sccTimeout); |
michael@0 | 139 | if (failed) { |
michael@0 | 140 | inputmethod_cleanup(); |
michael@0 | 141 | } |
michael@0 | 142 | else { |
michael@0 | 143 | test_onSurroundingTextChange(); |
michael@0 | 144 | } |
michael@0 | 145 | } |
michael@0 | 146 | |
michael@0 | 147 | gContext.onselectionchange = function() { |
michael@0 | 148 | ok(true, 'onselectionchange fired'); |
michael@0 | 149 | cleanup(); |
michael@0 | 150 | }; |
michael@0 | 151 | |
michael@0 | 152 | gContext.sendKey(0, 'j'.charCodeAt(0), 0).then(function() { |
michael@0 | 153 | // do nothing and wait for onselectionchange event |
michael@0 | 154 | }, function(e) { |
michael@0 | 155 | ok(false, 'sendKey failed: ' + e.name); |
michael@0 | 156 | cleanup(true); |
michael@0 | 157 | }); |
michael@0 | 158 | } |
michael@0 | 159 | |
michael@0 | 160 | function test_onSurroundingTextChange() { |
michael@0 | 161 | var sccTimeout = setTimeout(function() { |
michael@0 | 162 | ok(false, 'surroundingtextchange event not fired'); |
michael@0 | 163 | cleanup(true); |
michael@0 | 164 | }, 3000); |
michael@0 | 165 | |
michael@0 | 166 | function cleanup(failed) { |
michael@0 | 167 | gContext.onsurroundingtextchange = null; |
michael@0 | 168 | clearTimeout(sccTimeout); |
michael@0 | 169 | if (failed) { |
michael@0 | 170 | inputmethod_cleanup(); |
michael@0 | 171 | } |
michael@0 | 172 | else { |
michael@0 | 173 | // in case we want more tests leave this |
michael@0 | 174 | inputmethod_cleanup(); |
michael@0 | 175 | } |
michael@0 | 176 | } |
michael@0 | 177 | |
michael@0 | 178 | gContext.onsurroundingtextchange = function() { |
michael@0 | 179 | ok(true, 'onsurroundingtextchange fired'); |
michael@0 | 180 | cleanup(); |
michael@0 | 181 | }; |
michael@0 | 182 | |
michael@0 | 183 | gContext.sendKey(0, 'j'.charCodeAt(0), 0).then(function() { |
michael@0 | 184 | // do nothing and wait for onselectionchange event |
michael@0 | 185 | }, function(e) { |
michael@0 | 186 | ok(false, 'sendKey failed: ' + e.name); |
michael@0 | 187 | cleanup(true); |
michael@0 | 188 | }); |
michael@0 | 189 | } |
michael@0 | 190 | |
michael@0 | 191 | </script> |
michael@0 | 192 | </pre> |
michael@0 | 193 | </body> |
michael@0 | 194 | </html> |
michael@0 | 195 |