1 /* vim:set et ts=4 sts=4:
2 *
3 * libpyzy - The Chinese PinYin and Bopomofo conversion library.
4 *
5 * Copyright (c) 2008-2010 Peng Huang <shawn.p.huang@gmail.com>
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
20 * USA
21 */
22 #include <glib/gstdio.h>
23
24 #include <iostream>
25 #include <algorithm>
26
27 #include "Config.h"
28 #include "InputContext.h"
29 #include "Util.h" // for unique_ptr
30 #include "Variant.h"
31
32
33 using namespace std;
34 using namespace PyZy;
35
36 class DummyObserver : public PyZy::InputContext::Observer {
37 public:
commitText(const InputContext * context,const std::string & commit_text)38 void commitText (const InputContext *context, const std::string &commit_text) {
39 m_commited_text = commit_text;
40 }
inputTextChanged(const InputContext * context)41 void inputTextChanged (const InputContext *context) {}
preeditTextChanged(const InputContext * context)42 void preeditTextChanged (const InputContext *context) {}
auxiliaryTextChanged(const InputContext * context)43 void auxiliaryTextChanged (const InputContext *context) {}
candidatesChanged(const InputContext * context)44 void candidatesChanged (const InputContext *context) {}
cursorChanged(const InputContext * context)45 void cursorChanged (const InputContext *context) {}
46
commitedText()47 string commitedText () { return m_commited_text; }
48
clear()49 void clear () {
50 m_commited_text.clear ();
51 }
52 private:
53 string m_commited_text;
54 };
55
insertKeys(InputContext * context,const string & keys)56 void insertKeys (InputContext *context, const string &keys) {
57 for (size_t i = 0; i < keys.size (); ++i) {
58 context->insert (keys[i]);
59 }
60 }
61
62 #define g_assert_cmpstring(s1, cmp, s2) \
63 g_assert_cmpstr (s1.c_str(), cmp, s2)
64
testFullPinyin()65 int testFullPinyin ()
66 {
67 DummyObserver observer;
68 unique_ptr<InputContext> context;
69 context.reset (InputContext::create (InputContext::FULL_PINYIN, &observer));
70
71 { // Reset
72 context->reset ();
73
74 observer.clear ();
75 insertKeys (context.get (), "nihao");
76 g_assert_cmpint (context->cursor (), ==, 5);
77 g_assert_cmpstring (context->inputText (), ==, "nihao");
78 g_assert_cmpstring (context->selectedText (), ==, "");
79 g_assert_cmpstring (context->conversionText (), ==, "你好");
80 g_assert_cmpstring (context->restText (), ==, "");
81 g_assert_cmpstring (context->auxiliaryText (), ==, "ni hao|");
82 g_assert (context->hasCandidate (0));
83 g_assert_cmpstring (observer.commitedText (), ==, "");
84
85 observer.clear ();
86 context->reset ();
87 g_assert_cmpint (context->cursor (), ==, 0);
88 g_assert_cmpstring (context->inputText (), ==, "");
89 g_assert_cmpstring (context->selectedText (), ==, "");
90 g_assert_cmpstring (context->conversionText (), ==, "");
91 g_assert_cmpstring (context->restText (), ==, "");
92 g_assert_cmpstring (context->auxiliaryText (), ==, "");
93 g_assert (!context->hasCandidate (0));
94 g_assert_cmpstring (observer.commitedText (), ==, "");
95 }
96
97 { // Commit directly
98 context->reset ();
99
100 observer.clear ();
101 insertKeys (context.get (), "nihao");
102 g_assert_cmpint (context->cursor (), ==, 5);
103 g_assert_cmpstring (context->inputText (), ==, "nihao");
104 g_assert_cmpstring (context->selectedText (), ==, "");
105 g_assert_cmpstring (context->conversionText (), ==, "你好");
106 g_assert_cmpstring (context->restText (), ==, "");
107 g_assert_cmpstring (context->auxiliaryText (), ==, "ni hao|");
108 g_assert (context->hasCandidate (0));
109 g_assert_cmpstring (observer.commitedText (), ==, "");
110
111 observer.clear ();
112 context->commit (InputContext::TYPE_CONVERTED);
113 g_assert_cmpint (context->cursor (), ==, 0);
114 g_assert_cmpstring (context->inputText (), ==, "");
115 g_assert_cmpstring (context->selectedText (), ==, "");
116 g_assert_cmpstring (context->conversionText (), ==, "");
117 g_assert_cmpstring (context->restText (), ==, "");
118 g_assert_cmpstring (context->auxiliaryText (), ==, "");
119 g_assert (!context->hasCandidate (0));
120 g_assert_cmpstring (observer.commitedText (), ==, "nihao");
121 }
122
123 { // Select candidate
124 context->reset ();
125
126 observer.clear ();
127 insertKeys (context.get (), "nihao");
128 g_assert_cmpint (context->cursor (), ==, 5);
129 g_assert_cmpstring (context->inputText (), ==, "nihao");
130 g_assert_cmpstring (context->selectedText (), ==, "");
131 g_assert_cmpstring (context->conversionText (), ==, "你好");
132 g_assert_cmpstring (context->restText (), ==, "");
133 g_assert_cmpstring (context->auxiliaryText (), ==, "ni hao|");
134 g_assert (context->hasCandidate (0));
135 g_assert_cmpstring (observer.commitedText (), ==, "");
136
137 observer.clear ();
138 context->selectCandidate (0);
139 g_assert_cmpint (context->cursor (), ==, 0);
140 g_assert_cmpstring (context->inputText (), ==, "");
141 g_assert_cmpstring (context->selectedText (), ==, "");
142 g_assert_cmpstring (context->conversionText (), ==, "");
143 g_assert_cmpstring (context->restText (), ==, "");
144 g_assert_cmpstring (context->auxiliaryText (), ==, "");
145 g_assert (!context->hasCandidate (0));
146 g_assert_cmpstring (observer.commitedText (), ==, "你好");
147 }
148
149 { // Select special phrases.
150 context->reset ();
151
152 observer.clear ();
153 insertKeys (context.get (), "aazhi");
154 g_assert_cmpint (context->cursor (), ==, 5);
155 g_assert_cmpstring (context->inputText (), ==, "aazhi");
156 g_assert_cmpstring (context->selectedText (), ==, "");
157 g_assert_cmpstring (context->conversionText (), ==, "AA制");
158 g_assert_cmpstring (context->restText (), ==, "");
159 g_assert_cmpstring (context->auxiliaryText (), ==, "aazhi|");
160 g_assert (context->hasCandidate (0));
161 g_assert_cmpstring (observer.commitedText (), ==, "");
162
163 observer.clear ();
164 context->selectCandidate (0);
165 g_assert_cmpint (context->cursor (), ==, 0);
166 g_assert_cmpstring (context->inputText (), ==, "");
167 g_assert_cmpstring (context->selectedText (), ==, "");
168 g_assert_cmpstring (context->conversionText (), ==, "");
169 g_assert_cmpstring (context->restText (), ==, "");
170 g_assert_cmpstring (context->auxiliaryText (), ==, "");
171 g_assert (!context->hasCandidate (0));
172 g_assert_cmpstring (observer.commitedText (), ==, "AA制");
173 }
174
175 { // Select special phrases with some operations.
176 context->reset ();
177
178 observer.clear ();
179 insertKeys (context.get (), "aazhii");
180 g_assert_cmpint (context->cursor (), ==, 6);
181 g_assert_cmpstring (context->inputText (), ==, "aazhii");
182 g_assert_cmpstring (context->selectedText (), ==, "");
183 g_assert_cmpstring (context->conversionText (), ==, "啊啊之");
184 g_assert_cmpstring (context->restText (), ==, "i");
185 g_assert_cmpstring (context->auxiliaryText (), ==, "a a zhi i|");
186 g_assert (context->hasCandidate (0));
187 g_assert_cmpstring (observer.commitedText (), ==, "");
188
189 observer.clear ();
190 context->moveCursorLeft ();
191 g_assert_cmpint (context->cursor (), ==, 5);
192 g_assert_cmpstring (context->inputText (), ==, "aazhii");
193 g_assert_cmpstring (context->selectedText (), ==, "");
194 g_assert_cmpstring (context->conversionText (), ==, "AA制");
195 g_assert_cmpstring (context->restText (), ==, "i");
196 g_assert_cmpstring (context->auxiliaryText (), ==, "aazhi|i");
197 g_assert (context->hasCandidate (0));
198 g_assert_cmpstring (observer.commitedText (), ==, "");
199
200 observer.clear ();
201 context->selectCandidate (0);
202 g_assert_cmpint (context->cursor (), ==, 5);
203 g_assert_cmpstring (context->inputText (), ==, "aazhii");
204 g_assert_cmpstring (context->selectedText (), ==, "AA制");
205 g_assert_cmpstring (context->conversionText (), ==, "");
206 g_assert_cmpstring (context->restText (), ==, "i");
207 g_assert_cmpstring (context->auxiliaryText (), ==, "");
208 g_assert (!context->hasCandidate (0));
209 g_assert_cmpstring (observer.commitedText (), ==, "");
210
211 observer.clear ();
212 context->commit (InputContext::TYPE_CONVERTED);
213 g_assert_cmpint (context->cursor (), ==, 0);
214 g_assert_cmpstring (context->inputText (), ==, "");
215 g_assert_cmpstring (context->selectedText (), ==, "");
216 g_assert_cmpstring (context->conversionText (), ==, "");
217 g_assert_cmpstring (context->restText (), ==, "");
218 g_assert_cmpstring (context->auxiliaryText (), ==, "");
219 g_assert (!context->hasCandidate (0));
220 g_assert_cmpstring (observer.commitedText (), ==, "AA制i");
221 }
222
223 { // Many operations
224 context->reset ();
225
226 observer.clear ();
227 insertKeys (context.get (), "aazhi");
228 g_assert_cmpint (context->cursor (), ==, 5);
229 g_assert_cmpstring (context->inputText (), ==, "aazhi");
230 g_assert_cmpstring (context->selectedText (), ==, "");
231 g_assert_cmpstring (context->conversionText (), ==, "AA制");
232 g_assert_cmpstring (context->restText (), ==, "");
233 g_assert_cmpstring (context->auxiliaryText (), ==, "aazhi|");
234 g_assert (context->hasCandidate (0));
235 g_assert_cmpstring (observer.commitedText (), ==, "");
236
237 observer.clear ();
238 context->focusCandidate (1);
239 g_assert_cmpint (context->cursor (), ==, 5);
240 g_assert_cmpstring (context->inputText (), ==, "aazhi");
241 g_assert_cmpstring (context->selectedText (), ==, "");
242 g_assert_cmpstring (context->conversionText (), ==, "啊啊之");
243 g_assert_cmpstring (context->restText (), ==, "");
244 g_assert_cmpstring (context->auxiliaryText (), ==, "aazhi|");
245 g_assert (context->hasCandidate (0));
246 g_assert_cmpstring (observer.commitedText (), ==, "");
247
248 observer.clear ();
249 context->selectCandidate (4);
250 g_assert_cmpint (context->cursor (), ==, 5);
251 g_assert_cmpstring (context->inputText (), ==, "aazhi");
252 g_assert_cmpstring (context->selectedText (), ==, "阿");
253 g_assert_cmpstring (context->conversionText (), ==, "\xE9\x98\xBF\xE7\xB4\xAB");
254 g_assert_cmpstring (context->restText (), ==, "");
255 g_assert_cmpstring (context->auxiliaryText (), ==, "a zhi|");
256 g_assert (context->hasCandidate (0));
257 g_assert_cmpstring (observer.commitedText (), ==, "");
258
259 observer.clear ();
260 context->unselectCandidates ();
261 g_assert_cmpint (context->cursor (), ==, 5);
262 g_assert_cmpstring (context->inputText (), ==, "aazhi");
263 g_assert_cmpstring (context->selectedText (), ==, "");
264 g_assert_cmpstring (context->conversionText (), ==, "AA制");
265 g_assert_cmpstring (context->restText (), ==, "");
266 g_assert_cmpstring (context->auxiliaryText (), ==, "aazhi|");
267 g_assert (context->hasCandidate (0));
268 g_assert_cmpstring (observer.commitedText (), ==, "");
269
270 observer.clear ();
271 context->moveCursorLeft ();
272 g_assert_cmpint (context->cursor (), ==, 4);
273 g_assert_cmpstring (context->inputText (), ==, "aazhi");
274 g_assert_cmpstring (context->selectedText (), ==, "");
275 g_assert_cmpstring (context->conversionText (), ==, "a a zh|i");
276 g_assert_cmpstring (context->restText (), ==, "");
277 g_assert_cmpstring (context->auxiliaryText (), ==, "a a zh|i");
278 g_assert (context->hasCandidate (0));
279 g_assert_cmpstring (observer.commitedText (), ==, "");
280
281 observer.clear ();
282 context->insert ('i');
283 g_assert_cmpint (context->cursor (), ==, 5);
284 g_assert_cmpstring (context->inputText (), ==, "aazhii");
285 g_assert_cmpstring (context->selectedText (), ==, "");
286 g_assert_cmpstring (context->conversionText (), ==, "AA制");
287 g_assert_cmpstring (context->restText (), ==, "i");
288 g_assert_cmpstring (context->auxiliaryText (), ==, "aazhi|i");
289 g_assert (context->hasCandidate (0));
290 g_assert_cmpstring (observer.commitedText (), ==, "");
291
292 observer.clear ();
293 context->removeCharBefore ();
294 g_assert_cmpint (context->cursor (), ==, 4);
295 g_assert_cmpstring (context->inputText (), ==, "aazhi");
296 g_assert_cmpstring (context->selectedText (), ==, "");
297 g_assert_cmpstring (context->conversionText (), ==, "a a zh|i");
298 g_assert_cmpstring (context->restText (), ==, "");
299 g_assert_cmpstring (context->auxiliaryText (), ==, "a a zh|i");
300 g_assert (context->hasCandidate (0));
301 g_assert_cmpstring (observer.commitedText (), ==, "");
302
303 observer.clear ();
304 context->moveCursorRight ();
305 g_assert_cmpint (context->cursor (), ==, 5);
306 g_assert_cmpstring (context->inputText (), ==, "aazhi");
307 g_assert_cmpstring (context->selectedText (), ==, "");
308 g_assert_cmpstring (context->conversionText (), ==, "AA制");
309 g_assert_cmpstring (context->restText (), ==, "");
310 g_assert_cmpstring (context->auxiliaryText (), ==, "aazhi|");
311 g_assert (context->hasCandidate (0));
312 g_assert_cmpstring (observer.commitedText (), ==, "");
313
314 observer.clear ();
315 context->removeWordBefore ();
316 g_assert_cmpint (context->cursor (), ==, 2);
317 g_assert_cmpstring (context->inputText (), ==, "aa");
318 g_assert_cmpstring (context->selectedText (), ==, "");
319 g_assert_cmpstring (context->conversionText (), ==, "啊啊");
320 g_assert_cmpstring (context->restText (), ==, "");
321 g_assert_cmpstring (context->auxiliaryText (), ==, "a a|");
322 g_assert (context->hasCandidate (0));
323 g_assert_cmpstring (observer.commitedText (), ==, "");
324
325 observer.clear ();
326 insertKeys (context.get (), "nihao");
327 g_assert_cmpint (context->cursor (), ==, 7);
328 g_assert_cmpstring (context->inputText (), ==, "aanihao");
329 g_assert_cmpstring (context->selectedText (), ==, "");
330 g_assert_cmpstring (context->conversionText (), ==, "啊啊你好");
331 g_assert_cmpstring (context->restText (), ==, "");
332 g_assert_cmpstring (context->auxiliaryText (), ==, "a a ni hao|");
333 g_assert (context->hasCandidate (0));
334 g_assert_cmpstring (observer.commitedText (), ==, "");
335
336 observer.clear ();
337 context->selectCandidate (1);
338 g_assert_cmpint (context->cursor (), ==, 7);
339 g_assert_cmpstring (context->inputText (), ==, "aanihao");
340 g_assert_cmpstring (context->selectedText (), ==, "啊啊");
341 g_assert_cmpstring (context->conversionText (), ==, "你好");
342 g_assert_cmpstring (context->restText (), ==, "");
343 g_assert_cmpstring (context->auxiliaryText (), ==, "ni hao|");
344 g_assert (context->hasCandidate (0));
345 g_assert_cmpstring (observer.commitedText (), ==, "");
346
347 observer.clear ();
348 context->commit (InputContext::TYPE_CONVERTED);
349 g_assert_cmpint (context->cursor (), ==, 0);
350 g_assert_cmpstring (context->inputText (), ==, "");
351 g_assert_cmpstring (context->selectedText (), ==, "");
352 g_assert_cmpstring (context->conversionText (), ==, "");
353 g_assert_cmpstring (context->restText (), ==, "");
354 g_assert_cmpstring (context->auxiliaryText (), ==, "");
355 g_assert (!context->hasCandidate (0));
356 g_assert_cmpstring (observer.commitedText (), ==, "啊啊nihao");
357 }
358 }
359
testDoublePinyin()360 void testDoublePinyin()
361 {
362 DummyObserver observer;
363 unique_ptr<InputContext> context;
364 context.reset (
365 InputContext::create (InputContext::DOUBLE_PINYIN, &observer));
366
367 { // Reset
368 context->reset ();
369
370 observer.clear ();
371 insertKeys (context.get (), "nihk");
372 g_assert_cmpint (context->cursor (), ==, 4);
373 g_assert_cmpstring (context->inputText (), ==, "nihk");
374 g_assert_cmpstring (context->selectedText (), ==, "");
375 g_assert_cmpstring (context->conversionText (), ==, "你好");
376 g_assert_cmpstring (context->restText (), ==, "");
377 g_assert_cmpstring (context->auxiliaryText (), ==, "ni hao|");
378 g_assert (context->hasCandidate (0));
379 g_assert_cmpstring (observer.commitedText (), ==, "");
380
381 observer.clear ();
382 context->reset ();
383 g_assert_cmpint (context->cursor (), ==, 0);
384 g_assert_cmpstring (context->inputText (), ==, "");
385 g_assert_cmpstring (context->selectedText (), ==, "");
386 g_assert_cmpstring (context->conversionText (), ==, "");
387 g_assert_cmpstring (context->restText (), ==, "");
388 g_assert_cmpstring (context->auxiliaryText (), ==, "");
389 g_assert (!context->hasCandidate (0));
390 g_assert_cmpstring (observer.commitedText (), ==, "");
391 }
392
393 { // Commit directly
394 context->reset ();
395
396 observer.clear ();
397 insertKeys (context.get (), "nihk");
398 g_assert_cmpint (context->cursor (), ==, 4);
399 g_assert_cmpstring (context->inputText (), ==, "nihk");
400 g_assert_cmpstring (context->selectedText (), ==, "");
401 g_assert_cmpstring (context->conversionText (), ==, "你好");
402 g_assert_cmpstring (context->restText (), ==, "");
403 g_assert_cmpstring (context->auxiliaryText (), ==, "ni hao|");
404 g_assert (context->hasCandidate (0));
405 g_assert_cmpstring (observer.commitedText (), ==, "");
406
407 observer.clear ();
408 context->commit (InputContext::TYPE_CONVERTED);
409 g_assert_cmpint (context->cursor (), ==, 0);
410 g_assert_cmpstring (context->inputText (), ==, "");
411 g_assert_cmpstring (context->selectedText (), ==, "");
412 g_assert_cmpstring (context->conversionText (), ==, "");
413 g_assert_cmpstring (context->restText (), ==, "");
414 g_assert_cmpstring (context->auxiliaryText (), ==, "");
415 g_assert (!context->hasCandidate (0));
416 g_assert_cmpstring (observer.commitedText (), ==, "nihk");
417 }
418
419 { // Select candidate
420 context->reset ();
421
422 observer.clear ();
423 insertKeys (context.get (), "nihk");
424 g_assert_cmpint (context->cursor (), ==, 4);
425 g_assert_cmpstring (context->inputText (), ==, "nihk");
426 g_assert_cmpstring (context->selectedText (), ==, "");
427 g_assert_cmpstring (context->conversionText (), ==, "你好");
428 g_assert_cmpstring (context->restText (), ==, "");
429 g_assert_cmpstring (context->auxiliaryText (), ==, "ni hao|");
430 g_assert (context->hasCandidate (0));
431 g_assert_cmpstring (observer.commitedText (), ==, "");
432
433 observer.clear ();
434 context->selectCandidate (0);
435 g_assert_cmpint (context->cursor (), ==, 0);
436 g_assert_cmpstring (context->inputText (), ==, "");
437 g_assert_cmpstring (context->selectedText (), ==, "");
438 g_assert_cmpstring (context->conversionText (), ==, "");
439 g_assert_cmpstring (context->restText (), ==, "");
440 g_assert_cmpstring (context->auxiliaryText (), ==, "");
441 g_assert (!context->hasCandidate (0));
442 g_assert_cmpstring (observer.commitedText (), ==, "你好");
443 }
444
445 { // Select special phrases.
446 context->reset ();
447
448 observer.clear ();
449 insertKeys (context.get (), "aazhi");
450 g_assert_cmpint (context->cursor (), ==, 5);
451 g_assert_cmpstring (context->inputText (), ==, "aazhi");
452 g_assert_cmpstring (context->selectedText (), ==, "");
453 g_assert_cmpstring (context->conversionText (), ==, "AA制");
454 g_assert_cmpstring (context->restText (), ==, "");
455 g_assert_cmpstring (context->auxiliaryText (), ==, "aazhi|");
456 g_assert (context->hasCandidate (0));
457 g_assert_cmpstring (observer.commitedText (), ==, "");
458
459 observer.clear ();
460 context->selectCandidate (0);
461 g_assert_cmpint (context->cursor (), ==, 0);
462 g_assert_cmpstring (context->inputText (), ==, "");
463 g_assert_cmpstring (context->selectedText (), ==, "");
464 g_assert_cmpstring (context->conversionText (), ==, "");
465 g_assert_cmpstring (context->restText (), ==, "");
466 g_assert_cmpstring (context->auxiliaryText (), ==, "");
467 g_assert (!context->hasCandidate (0));
468 g_assert_cmpstring (observer.commitedText (), ==, "AA制");
469 }
470
471 { // Select special phrases with some operations.
472 context->reset ();
473
474 observer.clear ();
475 insertKeys (context.get (), "aazhii");
476 g_assert_cmpint (context->cursor (), ==, 6);
477 g_assert_cmpstring (context->inputText (), ==, "aazhii");
478 g_assert_cmpstring (context->selectedText (), ==, "");
479 g_assert_cmpstring (context->conversionText (), ==, "啊展翅");
480 g_assert_cmpstring (context->restText (), ==, "");
481 g_assert_cmpstring (context->auxiliaryText (), ==, "a zang chi|");
482 g_assert (context->hasCandidate (0));
483 g_assert_cmpstring (observer.commitedText (), ==, "");
484
485 observer.clear ();
486 context->moveCursorLeft ();
487 g_assert_cmpint (context->cursor (), ==, 5);
488 g_assert_cmpstring (context->inputText (), ==, "aazhii");
489 g_assert_cmpstring (context->selectedText (), ==, "");
490 g_assert_cmpstring (context->conversionText (), ==, "AA制");
491 g_assert_cmpstring (context->restText (), ==, "i");
492 g_assert_cmpstring (context->auxiliaryText (), ==, "aazhi|i");
493 g_assert (context->hasCandidate (0));
494 g_assert_cmpstring (observer.commitedText (), ==, "");
495
496 observer.clear ();
497 context->selectCandidate (0);
498 g_assert_cmpint (context->cursor (), ==, 5);
499 g_assert_cmpstring (context->inputText (), ==, "aazhii");
500 g_assert_cmpstring (context->selectedText (), ==, "AA制");
501 g_assert_cmpstring (context->conversionText (), ==, "");
502 g_assert_cmpstring (context->restText (), ==, "i");
503 g_assert_cmpstring (context->auxiliaryText (), ==, "");
504 g_assert (!context->hasCandidate (0));
505 g_assert_cmpstring (observer.commitedText (), ==, "");
506
507 observer.clear ();
508 context->commit (InputContext::TYPE_CONVERTED);
509 g_assert_cmpint (context->cursor (), ==, 0);
510 g_assert_cmpstring (context->inputText (), ==, "");
511 g_assert_cmpstring (context->selectedText (), ==, "");
512 g_assert_cmpstring (context->conversionText (), ==, "");
513 g_assert_cmpstring (context->restText (), ==, "");
514 g_assert_cmpstring (context->auxiliaryText (), ==, "");
515 g_assert (!context->hasCandidate (0));
516 g_assert_cmpstring (observer.commitedText (), ==, "AA制i");
517 }
518
519 { // Many operations
520 context->reset ();
521
522 observer.clear ();
523 insertKeys (context.get (), "aazhi");
524 g_assert_cmpint (context->cursor (), ==, 5);
525 g_assert_cmpstring (context->inputText (), ==, "aazhi");
526 g_assert_cmpstring (context->selectedText (), ==, "");
527 g_assert_cmpstring (context->conversionText (), ==, "AA制");
528 g_assert_cmpstring (context->restText (), ==, "");
529 g_assert_cmpstring (context->auxiliaryText (), ==, "aazhi|");
530 g_assert (context->hasCandidate (0));
531 g_assert_cmpstring (observer.commitedText (), ==, "");
532
533 observer.clear ();
534 context->focusCandidate (1);
535 g_assert_cmpint (context->cursor (), ==, 5);
536 g_assert_cmpstring (context->inputText (), ==, "aazhi");
537 g_assert_cmpstring (context->selectedText (), ==, "");
538 g_assert_cmpstring (context->conversionText (), ==, "啊战场");
539 g_assert_cmpstring (context->restText (), ==, "");
540 g_assert_cmpstring (context->auxiliaryText (), ==, "aazhi|");
541 g_assert (context->hasCandidate (0));
542 g_assert_cmpstring (observer.commitedText (), ==, "");
543
544 observer.clear ();
545 context->selectCandidate (3);
546 g_assert_cmpint (context->cursor (), ==, 5);
547 g_assert_cmpstring (context->inputText (), ==, "aazhi");
548 g_assert_cmpstring (context->selectedText (), ==, "阿");
549 g_assert_cmpstring (context->conversionText (), ==, "战场");
550 g_assert_cmpstring (context->restText (), ==, "");
551 g_assert_cmpstring (context->auxiliaryText (), ==, "zang ch|");
552 g_assert (context->hasCandidate (0));
553 g_assert_cmpstring (observer.commitedText (), ==, "");
554
555 observer.clear ();
556 context->unselectCandidates ();
557 g_assert_cmpint (context->cursor (), ==, 5);
558 g_assert_cmpstring (context->inputText (), ==, "aazhi");
559 g_assert_cmpstring (context->selectedText (), ==, "");
560 g_assert_cmpstring (context->conversionText (), ==, "AA制");
561 g_assert_cmpstring (context->restText (), ==, "");
562 g_assert_cmpstring (context->auxiliaryText (), ==, "aazhi|");
563 g_assert (context->hasCandidate (0));
564 g_assert_cmpstring (observer.commitedText (), ==, "");
565
566 observer.clear ();
567 context->moveCursorLeft ();
568 g_assert_cmpint (context->cursor (), ==, 4);
569 g_assert_cmpstring (context->inputText (), ==, "aazhi");
570 g_assert_cmpstring (context->selectedText (), ==, "");
571 g_assert_cmpstring (context->conversionText (), ==, "a zang|i");
572 g_assert_cmpstring (context->restText (), ==, "");
573 g_assert_cmpstring (context->auxiliaryText (), ==, "a zang|i");
574 g_assert (context->hasCandidate (0));
575 g_assert_cmpstring (observer.commitedText (), ==, "");
576
577 observer.clear ();
578 context->insert ('i');
579 g_assert_cmpint (context->cursor (), ==, 5);
580 g_assert_cmpstring (context->inputText (), ==, "aazhii");
581 g_assert_cmpstring (context->selectedText (), ==, "");
582 g_assert_cmpstring (context->conversionText (), ==, "AA制");
583 g_assert_cmpstring (context->restText (), ==, "i");
584 g_assert_cmpstring (context->auxiliaryText (), ==, "aazhi|i");
585 g_assert (context->hasCandidate (0));
586 g_assert_cmpstring (observer.commitedText (), ==, "");
587
588 observer.clear ();
589 context->removeCharBefore ();
590 g_assert_cmpint (context->cursor (), ==, 4);
591 g_assert_cmpstring (context->inputText (), ==, "aazhi");
592 g_assert_cmpstring (context->selectedText (), ==, "");
593 g_assert_cmpstring (context->conversionText (), ==, "a zang|i");
594 g_assert_cmpstring (context->restText (), ==, "");
595 g_assert_cmpstring (context->auxiliaryText (), ==, "a zang|i");
596 g_assert (context->hasCandidate (0));
597 g_assert_cmpstring (observer.commitedText (), ==, "");
598
599 observer.clear ();
600 context->moveCursorRight ();
601 g_assert_cmpint (context->cursor (), ==, 5);
602 g_assert_cmpstring (context->inputText (), ==, "aazhi");
603 g_assert_cmpstring (context->selectedText (), ==, "");
604 g_assert_cmpstring (context->conversionText (), ==, "AA制");
605 g_assert_cmpstring (context->restText (), ==, "");
606 g_assert_cmpstring (context->auxiliaryText (), ==, "aazhi|");
607 g_assert (context->hasCandidate (0));
608 g_assert_cmpstring (observer.commitedText (), ==, "");
609
610 observer.clear ();
611 context->removeWordBefore ();
612 g_assert_cmpint (context->cursor (), ==, 4);
613 g_assert_cmpstring (context->inputText (), ==, "aazh");
614 g_assert_cmpstring (context->selectedText (), ==, "");
615 g_assert_cmpstring (context->conversionText (), ==, "啊张");
616 g_assert_cmpstring (context->restText (), ==, "");
617 g_assert_cmpstring (context->auxiliaryText (), ==, "a zang|");
618 g_assert (context->hasCandidate (0));
619 g_assert_cmpstring (observer.commitedText (), ==, "");
620
621 observer.clear ();
622 insertKeys (context.get (), "nihk");
623 g_assert_cmpint (context->cursor (), ==, 8);
624 g_assert_cmpstring (context->inputText (), ==, "aazhnihk");
625 g_assert_cmpstring (context->selectedText (), ==, "");
626 g_assert_cmpstring (context->conversionText (), ==, "啊站立好");
627 g_assert_cmpstring (context->restText (), ==, "");
628 g_assert_cmpstring (context->auxiliaryText (), ==, "a zang ni hao|");
629 g_assert (context->hasCandidate (0));
630 g_assert_cmpstring (observer.commitedText (), ==, "");
631
632 observer.clear ();
633 context->selectCandidate (1);
634 g_assert_cmpint (context->cursor (), ==, 8);
635 g_assert_cmpstring (context->inputText (), ==, "aazhnihk");
636 g_assert_cmpstring (context->selectedText (), ==, "啊");
637 g_assert_cmpstring (context->conversionText (), ==, "站立好");
638 g_assert_cmpstring (context->restText (), ==, "");
639 g_assert_cmpstring (context->auxiliaryText (), ==, "zang ni hao|");
640 g_assert (context->hasCandidate (0));
641 g_assert_cmpstring (observer.commitedText (), ==, "");
642
643 observer.clear ();
644 context->commit (InputContext::TYPE_CONVERTED);
645 g_assert_cmpint (context->cursor (), ==, 0);
646 g_assert_cmpstring (context->inputText (), ==, "");
647 g_assert_cmpstring (context->selectedText (), ==, "");
648 g_assert_cmpstring (context->conversionText (), ==, "");
649 g_assert_cmpstring (context->restText (), ==, "");
650 g_assert_cmpstring (context->auxiliaryText (), ==, "");
651 g_assert (!context->hasCandidate (0));
652 g_assert_cmpstring (observer.commitedText (), ==, "啊zhnihk");
653 }
654 }
655
testBopomofo()656 void testBopomofo ()
657 {
658 DummyObserver observer;
659 unique_ptr<InputContext> context;
660 context.reset (InputContext::create (InputContext::BOPOMOFO, &observer));
661 context->setProperty (InputContext::PROPERTY_SPECIAL_PHRASE,
662 Variant::fromBool (false));
663
664 { // Reset
665 context->reset ();
666
667 observer.clear ();
668 insertKeys (context.get (), "sucl");
669 g_assert_cmpint (context->cursor (), ==, 4);
670 g_assert_cmpstring (context->inputText (), ==, "sucl");
671 g_assert_cmpstring (context->selectedText (), ==, "");
672 g_assert_cmpstring (context->conversionText (), ==, "你好");
673 g_assert_cmpstring (context->restText (), ==, "");
674 g_assert_cmpstring (context->auxiliaryText (), ==, "ㄋㄧ,ㄏㄠ|");
675 g_assert (context->hasCandidate (0));
676 g_assert_cmpstring (observer.commitedText (), ==, "");
677
678 observer.clear ();
679 context->reset ();
680 g_assert_cmpint (context->cursor (), ==, 0);
681 g_assert_cmpstring (context->inputText (), ==, "");
682 g_assert_cmpstring (context->selectedText (), ==, "");
683 g_assert_cmpstring (context->conversionText (), ==, "");
684 g_assert_cmpstring (context->restText (), ==, "");
685 g_assert_cmpstring (context->auxiliaryText (), ==, "");
686 g_assert (!context->hasCandidate (0));
687 g_assert_cmpstring (observer.commitedText (), ==, "");
688 }
689
690 { // Commit directly
691 context->reset ();
692
693 observer.clear ();
694 insertKeys (context.get (), "sucl");
695 g_assert_cmpint (context->cursor (), ==, 4);
696 g_assert_cmpstring (context->inputText (), ==, "sucl");
697 g_assert_cmpstring (context->selectedText (), ==, "");
698 g_assert_cmpstring (context->conversionText (), ==, "你好");
699 g_assert_cmpstring (context->restText (), ==, "");
700 g_assert_cmpstring (context->auxiliaryText (), ==, "ㄋㄧ,ㄏㄠ|");
701 g_assert (context->hasCandidate (0));
702 g_assert_cmpstring (observer.commitedText (), ==, "");
703
704 observer.clear ();
705 context->commit (InputContext::TYPE_RAW);
706 g_assert_cmpint (context->cursor (), ==, 0);
707 g_assert_cmpstring (context->inputText (), ==, "");
708 g_assert_cmpstring (context->selectedText (), ==, "");
709 g_assert_cmpstring (context->conversionText (), ==, "");
710 g_assert_cmpstring (context->restText (), ==, "");
711 g_assert_cmpstring (context->auxiliaryText (), ==, "");
712 g_assert (!context->hasCandidate (0));
713 g_assert_cmpstring (observer.commitedText (), ==, "sucl");
714 }
715
716 { // Select candidate
717 context->reset ();
718
719 observer.clear ();
720 insertKeys (context.get (), "sucl");
721 g_assert_cmpint (context->cursor (), ==, 4);
722 g_assert_cmpstring (context->inputText (), ==, "sucl");
723 g_assert_cmpstring (context->selectedText (), ==, "");
724 g_assert_cmpstring (context->conversionText (), ==, "你好");
725 g_assert_cmpstring (context->restText (), ==, "");
726 g_assert_cmpstring (context->auxiliaryText (), ==, "ㄋㄧ,ㄏㄠ|");
727 g_assert (context->hasCandidate (0));
728 g_assert_cmpstring (observer.commitedText (), ==, "");
729
730 observer.clear ();
731 context->selectCandidate (0);
732 g_assert_cmpint (context->cursor (), ==, 0);
733 g_assert_cmpstring (context->inputText (), ==, "");
734 g_assert_cmpstring (context->selectedText (), ==, "");
735 g_assert_cmpstring (context->conversionText (), ==, "");
736 g_assert_cmpstring (context->restText (), ==, "");
737 g_assert_cmpstring (context->auxiliaryText (), ==, "");
738 g_assert (!context->hasCandidate (0));
739 g_assert_cmpstring (observer.commitedText (), ==, "你好");
740 }
741
742 { // Don't use special phrases for bopomofo.
743 context->reset ();
744
745 observer.clear ();
746 insertKeys (context.get (), "aazhi");
747 g_assert_cmpint (context->cursor (), ==, 5);
748 g_assert_cmpstring (context->inputText (), ==, "aazhi");
749 g_assert_cmpstring (context->selectedText (), ==, "");
750 g_assert_cmpstring (context->conversionText (), ==, "妈妈好吃哦");
751 g_assert_cmpstring (context->restText (), ==, "");
752 g_assert_cmpstring (context->auxiliaryText (), ==, "ㄇ,ㄇ,ㄈ,ㄘ,ㄛ|");
753 g_assert (context->hasCandidate (0));
754 g_assert_cmpstring (observer.commitedText (), ==, "");
755
756 observer.clear ();
757 context->selectCandidate (0);
758 g_assert_cmpint (context->cursor (), ==, 0);
759 g_assert_cmpstring (context->inputText (), ==, "");
760 g_assert_cmpstring (context->selectedText (), ==, "");
761 g_assert_cmpstring (context->conversionText (), ==, "");
762 g_assert_cmpstring (context->restText (), ==, "");
763 g_assert_cmpstring (context->auxiliaryText (), ==, "");
764 g_assert (!context->hasCandidate (0));
765 g_assert_cmpstring (observer.commitedText (), ==, "妈妈好吃哦");
766 }
767
768 { // Select special phrases with some operations.
769 context->reset ();
770
771 observer.clear ();
772 insertKeys (context.get (), "aazhii");
773 g_assert_cmpint (context->cursor (), ==, 6);
774 g_assert_cmpstring (context->inputText (), ==, "aazhii");
775 g_assert_cmpstring (context->selectedText (), ==, "");
776 g_assert_cmpstring (context->conversionText (), ==, "妈妈好吃哦哦");
777 g_assert_cmpstring (context->restText (), ==, "");
778 g_assert_cmpstring (context->auxiliaryText (), ==, "ㄇ,ㄇ,ㄈ,ㄘ,ㄛ,ㄛ|");
779 g_assert (context->hasCandidate (0));
780 g_assert_cmpstring (observer.commitedText (), ==, "");
781
782 observer.clear ();
783 context->moveCursorLeft ();
784 g_assert_cmpint (context->cursor (), ==, 5);
785 g_assert_cmpstring (context->inputText (), ==, "aazhii");
786 g_assert_cmpstring (context->selectedText (), ==, "");
787 g_assert_cmpstring (context->conversionText (), ==, "ㄇㄇㄈㄘㄛ ㄛ");
788 g_assert_cmpstring (context->restText (), ==, "");
789 g_assert_cmpstring (context->auxiliaryText (), ==, "ㄇ,ㄇ,ㄈ,ㄘ,ㄛ|ㄛ");
790 g_assert (context->hasCandidate (0));
791 g_assert_cmpstring (observer.commitedText (), ==, "");
792
793 observer.clear ();
794 context->selectCandidate (0);
795 g_assert_cmpint (context->cursor (), ==, 5);
796 g_assert_cmpstring (context->inputText (), ==, "aazhii");
797 g_assert_cmpstring (context->selectedText (), ==, "妈妈好吃哦");
798 g_assert_cmpstring (context->conversionText (), ==, "");
799 g_assert_cmpstring (context->restText (), ==, "ㄛ");
800 g_assert_cmpstring (context->auxiliaryText (), ==, "");
801 g_assert (!context->hasCandidate (0));
802 g_assert_cmpstring (observer.commitedText (), ==, "");
803
804 observer.clear ();
805 context->commit (InputContext::TYPE_CONVERTED);
806 g_assert_cmpint (context->cursor (), ==, 0);
807 g_assert_cmpstring (context->inputText (), ==, "");
808 g_assert_cmpstring (context->selectedText (), ==, "");
809 g_assert_cmpstring (context->conversionText (), ==, "");
810 g_assert_cmpstring (context->restText (), ==, "");
811 g_assert_cmpstring (context->auxiliaryText (), ==, "");
812 g_assert (!context->hasCandidate (0));
813 g_assert_cmpstring (observer.commitedText (), ==, "妈妈好吃哦ㄛ");
814 }
815
816 { // Many operations
817 context->reset ();
818
819 observer.clear ();
820 insertKeys (context.get (), "aazhi");
821 g_assert_cmpint (context->cursor (), ==, 5);
822 g_assert_cmpstring (context->inputText (), ==, "aazhi");
823 g_assert_cmpstring (context->selectedText (), ==, "");
824 g_assert_cmpstring (context->conversionText (), ==, "妈妈好吃哦");
825 g_assert_cmpstring (context->restText (), ==, "");
826 g_assert_cmpstring (context->auxiliaryText (), ==, "ㄇ,ㄇ,ㄈ,ㄘ,ㄛ|");
827 g_assert (context->hasCandidate (0));
828 g_assert_cmpstring (observer.commitedText (), ==, "");
829
830 observer.clear ();
831 context->focusCandidate (1);
832 g_assert_cmpint (context->cursor (), ==, 5);
833 g_assert_cmpstring (context->inputText (), ==, "aazhi");
834 g_assert_cmpstring (context->selectedText (), ==, "");
835 g_assert_cmpstring (context->conversionText (), ==, "妈妈");
836 g_assert_cmpstring (context->restText (), ==, "");
837 g_assert_cmpstring (context->auxiliaryText (), ==, "ㄇ,ㄇ,ㄈ,ㄘ,ㄛ|");
838 g_assert (context->hasCandidate (0));
839 g_assert_cmpstring (observer.commitedText (), ==, "");
840
841 observer.clear ();
842 context->selectCandidate (3);
843 g_assert_cmpint (context->cursor (), ==, 5);
844 g_assert_cmpstring (context->inputText (), ==, "aazhi");
845 g_assert_cmpstring (context->selectedText (), ==, "慢慢");
846 g_assert_cmpstring (context->conversionText (), ==, "好吃哦");
847 g_assert_cmpstring (context->restText (), ==, "");
848 g_assert_cmpstring (context->auxiliaryText (), ==, "ㄈ,ㄘ,ㄛ|");
849 g_assert (context->hasCandidate (0));
850 g_assert_cmpstring (observer.commitedText (), ==, "");
851
852 observer.clear ();
853 context->unselectCandidates ();
854 g_assert_cmpint (context->cursor (), ==, 5);
855 g_assert_cmpstring (context->inputText (), ==, "aazhi");
856 g_assert_cmpstring (context->selectedText (), ==, "");
857 g_assert_cmpstring (context->conversionText (), ==, "妈妈好吃哦");
858 g_assert_cmpstring (context->restText (), ==, "");
859 g_assert_cmpstring (context->auxiliaryText (), ==, "ㄇ,ㄇ,ㄈ,ㄘ,ㄛ|");
860 g_assert (context->hasCandidate (0));
861 g_assert_cmpstring (observer.commitedText (), ==, "");
862
863 observer.clear ();
864 context->moveCursorLeft ();
865 g_assert_cmpint (context->cursor (), ==, 4);
866 g_assert_cmpstring (context->inputText (), ==, "aazhi");
867 g_assert_cmpstring (context->selectedText (), ==, "");
868 g_assert_cmpstring (context->conversionText (), ==, "ㄇㄇㄈㄘ ㄛ");
869 g_assert_cmpstring (context->restText (), ==, "");
870 g_assert_cmpstring (context->auxiliaryText (), ==, "ㄇ,ㄇ,ㄈ,ㄘ|ㄛ");
871 g_assert (context->hasCandidate (0));
872 g_assert_cmpstring (observer.commitedText (), ==, "");
873
874 observer.clear ();
875 context->insert ('i');
876 g_assert_cmpint (context->cursor (), ==, 5);
877 g_assert_cmpstring (context->inputText (), ==, "aazhii");
878 g_assert_cmpstring (context->selectedText (), ==, "");
879 g_assert_cmpstring (context->conversionText (), ==, "ㄇㄇㄈㄘㄛ ㄛ");
880 g_assert_cmpstring (context->restText (), ==, "");
881 g_assert_cmpstring (context->auxiliaryText (), ==, "ㄇ,ㄇ,ㄈ,ㄘ,ㄛ|ㄛ");
882 g_assert (context->hasCandidate (0));
883 g_assert_cmpstring (observer.commitedText (), ==, "");
884
885 observer.clear ();
886 context->removeCharBefore ();
887 g_assert_cmpint (context->cursor (), ==, 4);
888 g_assert_cmpstring (context->inputText (), ==, "aazhi");
889 g_assert_cmpstring (context->selectedText (), ==, "");
890 g_assert_cmpstring (context->conversionText (), ==, "ㄇㄇㄈㄘ ㄛ");
891 g_assert_cmpstring (context->restText (), ==, "");
892 g_assert_cmpstring (context->auxiliaryText (), ==, "ㄇ,ㄇ,ㄈ,ㄘ|ㄛ");
893 g_assert (context->hasCandidate (0));
894 g_assert_cmpstring (observer.commitedText (), ==, "");
895
896 observer.clear ();
897 context->moveCursorRight ();
898 g_assert_cmpint (context->cursor (), ==, 5);
899 g_assert_cmpstring (context->inputText (), ==, "aazhi");
900 g_assert_cmpstring (context->selectedText (), ==, "");
901 g_assert_cmpstring (context->conversionText (), ==, "妈妈好吃哦");
902 g_assert_cmpstring (context->restText (), ==, "");
903 g_assert_cmpstring (context->auxiliaryText (), ==, "ㄇ,ㄇ,ㄈ,ㄘ,ㄛ|");
904 g_assert (context->hasCandidate (0));
905 g_assert_cmpstring (observer.commitedText (), ==, "");
906
907 observer.clear ();
908 context->removeWordBefore ();
909 g_assert_cmpint (context->cursor (), ==, 4);
910 g_assert_cmpstring (context->inputText (), ==, "aazh");
911 g_assert_cmpstring (context->selectedText (), ==, "");
912 g_assert_cmpstring (context->conversionText (), ==, "妈妈好吃");
913 g_assert_cmpstring (context->restText (), ==, "");
914 g_assert_cmpstring (context->auxiliaryText (), ==, "ㄇ,ㄇ,ㄈ,ㄘ|");
915 g_assert (context->hasCandidate (0));
916 g_assert_cmpstring (observer.commitedText (), ==, "");
917
918 observer.clear ();
919 insertKeys (context.get (), "nihk");
920 g_assert_cmpint (context->cursor (), ==, 8);
921 g_assert_cmpstring (context->inputText (), ==, "aazhnihk");
922 g_assert_cmpstring (context->selectedText (), ==, "");
923 g_assert_cmpstring (context->conversionText (), ==, "妈妈好吃是哦车");
924 g_assert_cmpstring (context->restText (), ==, "");
925 g_assert_cmpstring (context->auxiliaryText (), ==, "ㄇ,ㄇ,ㄈ,ㄘ,ㄙ,ㄛ,ㄘㄜ|");
926 g_assert (context->hasCandidate (0));
927 g_assert_cmpstring (observer.commitedText (), ==, "");
928
929 observer.clear ();
930 context->selectCandidate (1);
931 g_assert_cmpint (context->cursor (), ==, 8);
932 g_assert_cmpstring (context->inputText (), ==, "aazhnihk");
933 g_assert_cmpstring (context->selectedText (), ==, "妈妈");
934 g_assert_cmpstring (context->conversionText (), ==, "好吃是哦车");
935 g_assert_cmpstring (context->restText (), ==, "");
936 g_assert_cmpstring (context->auxiliaryText (), ==, "ㄈ,ㄘ,ㄙ,ㄛ,ㄘㄜ|");
937 g_assert (context->hasCandidate (0));
938 g_assert_cmpstring (observer.commitedText (), ==, "");
939
940 observer.clear ();
941 context->commit (InputContext::TYPE_CONVERTED);
942 g_assert_cmpint (context->cursor (), ==, 0);
943 g_assert_cmpstring (context->inputText (), ==, "");
944 g_assert_cmpstring (context->selectedText (), ==, "");
945 g_assert_cmpstring (context->conversionText (), ==, "");
946 g_assert_cmpstring (context->restText (), ==, "");
947 g_assert_cmpstring (context->auxiliaryText (), ==, "");
948 g_assert (!context->hasCandidate (0));
949 g_assert_cmpstring (observer.commitedText (), ==, "妈妈ㄈㄘㄙㄛㄘㄜ");
950 }
951 }
952
testCommit()953 void testCommit ()
954 {
955 { // Pinyin commit
956 DummyObserver observer;
957 unique_ptr<InputContext> context;
958 context.reset (
959 InputContext::create (InputContext::FULL_PINYIN, &observer));
960
961 observer.clear ();
962 insertKeys (context.get (), "nihao");
963 g_assert_cmpstring (context->inputText (), ==, "nihao");
964 g_assert_cmpstring (context->conversionText (), ==, "你好");
965 g_assert_cmpstring (context->auxiliaryText (), ==, "ni hao|");
966
967 observer.clear ();
968 context->commit (InputContext::TYPE_RAW);
969 g_assert_cmpstring (observer.commitedText (), ==, "nihao");
970
971 context->reset ();
972 observer.clear ();
973 insertKeys (context.get (), "nihao");
974 g_assert_cmpstring (context->inputText (), ==, "nihao");
975 g_assert_cmpstring (context->conversionText (), ==, "你好");
976 g_assert_cmpstring (context->auxiliaryText (), ==, "ni hao|");
977
978 observer.clear ();
979 context->commit (InputContext::TYPE_PHONETIC);
980 g_assert_cmpstring (observer.commitedText (), ==, "nihao");
981
982 context->reset ();
983 observer.clear ();
984 insertKeys (context.get (), "nihao");
985 g_assert_cmpstring (context->inputText (), ==, "nihao");
986 g_assert_cmpstring (context->conversionText (), ==, "你好");
987 g_assert_cmpstring (context->auxiliaryText (), ==, "ni hao|");
988
989 // To get "你好", we should call selectCandidate().
990 observer.clear ();
991 context->commit (InputContext::TYPE_CONVERTED);
992 g_assert_cmpstring (observer.commitedText (), ==, "nihao");
993 }
994
995 { // Bopomofo commit
996 DummyObserver observer;
997 unique_ptr<InputContext> context;
998 context.reset (
999 InputContext::create (InputContext::BOPOMOFO, &observer));
1000
1001 observer.clear ();
1002 insertKeys (context.get (), "sucl");
1003 g_assert_cmpstring (context->inputText (), ==, "sucl");
1004 g_assert_cmpstring (context->conversionText (), ==, "你好");
1005 g_assert_cmpstring (context->auxiliaryText (), ==, "ㄋㄧ,ㄏㄠ|");
1006
1007 observer.clear ();
1008 context->commit (InputContext::TYPE_RAW);
1009 g_assert_cmpstring (observer.commitedText (), ==, "sucl");
1010
1011 context->reset ();
1012 observer.clear ();
1013 insertKeys (context.get (), "sucl");
1014 g_assert_cmpstring (context->inputText (), ==, "sucl");
1015 g_assert_cmpstring (context->conversionText (), ==, "你好");
1016 g_assert_cmpstring (context->auxiliaryText (), ==, "ㄋㄧ,ㄏㄠ|");
1017
1018 observer.clear ();
1019 context->commit (InputContext::TYPE_PHONETIC);
1020 g_assert_cmpstring (observer.commitedText (), ==, "ㄋㄧㄏㄠ");
1021
1022 context->reset ();
1023 observer.clear ();
1024 insertKeys (context.get (), "sucl");
1025 g_assert_cmpstring (context->inputText (), ==, "sucl");
1026 g_assert_cmpstring (context->conversionText (), ==, "你好");
1027 g_assert_cmpstring (context->auxiliaryText (), ==, "ㄋㄧ,ㄏㄠ|");
1028
1029 // To get "你好", we should call selectCandidate().
1030 observer.clear ();
1031 context->commit (InputContext::TYPE_CONVERTED);
1032 g_assert_cmpstring (observer.commitedText (), ==, "ㄋㄧㄏㄠ");
1033 }
1034 }
1035
getTestDir()1036 string getTestDir ()
1037 {
1038 const char *kPyZyTestDirName = "__pyzy_test_dir__";
1039
1040 gchar *path = g_build_filename (g_get_tmp_dir(), kPyZyTestDirName, NULL);
1041 const string result = path;
1042 g_free (path);
1043 return result;
1044 }
1045
removeDirectory(const string & path)1046 bool removeDirectory (const string &path) {
1047 GDir *dir = g_dir_open (path.c_str (), 0, NULL);
1048 if (dir == NULL) {
1049 return false;
1050 }
1051
1052 const gchar *entry_name = NULL;
1053 while ((entry_name = g_dir_read_name (dir)) != NULL) {
1054 gchar *entry_path = g_build_filename (path.c_str (), entry_name, NULL);
1055
1056 if (g_file_test (entry_path, G_FILE_TEST_IS_DIR)) {
1057 removeDirectory (entry_path);
1058 } else {
1059 g_unlink (entry_path);
1060 }
1061
1062 g_free (entry_path);
1063 }
1064
1065 g_dir_close (dir);
1066 int ret = g_rmdir (path.c_str ());
1067
1068 return ret == 0;
1069 }
1070
setUp()1071 void setUp ()
1072 {
1073 const string test_dir = getTestDir ();
1074 InputContext::init (test_dir, test_dir);
1075 }
1076
tearDown()1077 void tearDown ()
1078 {
1079 InputContext::finalize ();
1080 removeDirectory (getTestDir ());
1081 }
1082
main(int argc,char ** argv)1083 int main (int argc, char **argv)
1084 {
1085 setUp();
1086 testFullPinyin();
1087 tearDown();
1088
1089 setUp();
1090 testDoublePinyin();
1091 tearDown();
1092
1093 setUp();
1094 testBopomofo();
1095 tearDown();
1096
1097 setUp();
1098 testCommit();
1099 tearDown();
1100
1101 return 0;
1102 }
1103