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