1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 var kNewInputMethodTemplate
= '_comp_ime_{EXT_ID}xkb:fr::fra';
6 var kInitialInputMethodRegex
= /_comp_ime_([a-z]{32})xkb:us::eng/;
7 var kInvalidInputMethod
= 'xx::xxx';
10 initialInputMethod
: '',
12 dictionaryLoaded
: null,
15 // The tests needs to be executed in order.
17 function initTests() {
18 console
.log('initTest: Getting initial inputMethod');
19 chrome
.inputMethodPrivate
.getCurrentInputMethod(function(inputMethod
) {
20 testParams
.initialInputMethod
= inputMethod
;
22 var match
= inputMethod
.match(kInitialInputMethodRegex
);
23 chrome
.test
.assertTrue(!!match
);
24 chrome
.test
.assertEq(2, match
.length
);
25 var extensionId
= match
[1];
26 testParams
.newInputMethod
=
27 kNewInputMethodTemplate
.replace('{EXT_ID}', extensionId
);
28 chrome
.test
.succeed();
33 chrome
.test
.assertTrue(!!testParams
.newInputMethod
);
35 'setTest: Changing input method to: ' + testParams
.newInputMethod
);
36 chrome
.inputMethodPrivate
.setCurrentInputMethod(testParams
.newInputMethod
,
38 chrome
.test
.assertTrue(
39 !chrome
.runtime
.lastError
,
40 chrome
.runtime
.lastError
? chrome
.runtime
.lastError
.message
: '');
41 chrome
.test
.succeed();
46 chrome
.test
.assertTrue(!!testParams
.newInputMethod
);
47 console
.log('getTest: Getting current input method.');
48 chrome
.inputMethodPrivate
.getCurrentInputMethod(function(inputMethod
) {
49 chrome
.test
.assertEq(testParams
.newInputMethod
, inputMethod
);
50 chrome
.test
.succeed();
54 function observeTest() {
55 chrome
.test
.assertTrue(!!testParams
.initialInputMethod
);
56 console
.log('observeTest: Adding input method event listener.');
58 var listener = function(subfix
) {
59 chrome
.inputMethodPrivate
.onChanged
.removeListener(listener
);
60 chrome
.test
.assertEq('us::eng', subfix
);
61 chrome
.test
.succeed();
63 chrome
.inputMethodPrivate
.onChanged
.addListener(listener
);
65 console
.log('observeTest: Changing input method to: ' +
66 testParams
.initialInputMethod
);
67 chrome
.inputMethodPrivate
.setCurrentInputMethod(
68 testParams
.initialInputMethod
);
72 function setInvalidTest() {
74 'setInvalidTest: Changing input method to: ' + kInvalidInputMethod
);
75 chrome
.inputMethodPrivate
.setCurrentInputMethod(kInvalidInputMethod
,
77 chrome
.test
.assertTrue(!!chrome
.runtime
.lastError
);
78 chrome
.test
.succeed();
82 function getListTest() {
83 chrome
.test
.assertTrue(!!testParams
.initialInputMethod
);
84 chrome
.test
.assertTrue(!!testParams
.newInputMethod
);
85 console
.log('getListTest: Getting input method list.');
87 chrome
.inputMethodPrivate
.getInputMethods(function(inputMethods
) {
88 chrome
.test
.assertEq(6, inputMethods
.length
);
89 var foundInitialInputMethod
= false;
90 var foundNewInputMethod
= false;
91 for (var i
= 0; i
< inputMethods
.length
; ++i
) {
92 if (inputMethods
[i
].id
== testParams
.initialInputMethod
)
93 foundInitialInputMethod
= true;
94 if (inputMethods
[i
].id
== testParams
.newInputMethod
)
95 foundNewInputMethod
= true;
97 chrome
.test
.assertTrue(foundInitialInputMethod
);
98 chrome
.test
.assertTrue(foundNewInputMethod
);
99 chrome
.test
.succeed();
104 function getFetchPromise() {
105 return new Promise(function(resolve
, reject
) {
106 chrome
.inputMethodPrivate
.fetchAllDictionaryWords(function(words
) {
107 if (!!chrome
.runtime
.lastError
) {
108 reject(Error(chrome
.runtime
.lastError
));
117 function getAddPromise(word
) {
118 return new Promise(function(resolve
, reject
) {
119 chrome
.inputMethodPrivate
.addWordToDictionary(word
, function() {
120 if (!!chrome
.runtime
.lastError
) {
121 reject(Error(chrome
.runtime
.lastError
));
129 function loadDictionaryAsyncTest() {
130 testParams
.dictionaryLoaded
= new Promise(function(resolve
, reject
) {
131 var message
= 'before';
132 chrome
.inputMethodPrivate
.onDictionaryLoaded
.addListener(
133 function listener() {
134 chrome
.inputMethodPrivate
.onDictionaryLoaded
.removeListener(listener
);
135 chrome
.test
.assertEq(message
, 'after');
140 // We don't need to wait for the promise to resolve before continuing since
141 // promises are async wrappers.
142 chrome
.test
.succeed();
145 function fetchDictionaryTest() {
146 testParams
.dictionaryLoaded
148 return getFetchPromise();
150 .then(function confirmFetch(words
) {
151 chrome
.test
.assertTrue(words
!== undefined);
152 chrome
.test
.assertTrue(words
.length
=== 0);
153 chrome
.test
.succeed();
157 function addWordToDictionaryTest() {
158 var wordToAdd
= 'helloworld';
159 testParams
.dictionaryLoaded
161 return getAddPromise(wordToAdd
);
163 // Adding the same word results in an error.
165 return getAddPromise(wordToAdd
);
167 .catch(function(error
) {
168 chrome
.test
.assertTrue(!!error
.message
);
169 return getFetchPromise();
171 .then(function(words
) {
172 chrome
.test
.assertTrue(words
.length
=== 1);
173 chrome
.test
.assertEq(words
[0], wordToAdd
);
174 chrome
.test
.succeed();
178 function dictionaryChangedTest() {
179 var wordToAdd
= 'helloworld2';
180 testParams
.dictionaryLoaded
182 chrome
.inputMethodPrivate
.onDictionaryChanged
.addListener(
183 function(added
, removed
) {
184 chrome
.test
.assertTrue(added
.length
=== 1);
185 chrome
.test
.assertTrue(removed
.length
=== 0);
186 chrome
.test
.assertEq(added
[0], wordToAdd
);
187 chrome
.test
.succeed();
191 return getAddPromise(wordToAdd
);
195 chrome
.test
.sendMessage('ready');
196 chrome
.test
.runTests(
197 [initTests
, setTest
, getTest
, observeTest
, setInvalidTest
, getListTest
,
198 loadDictionaryAsyncTest
, fetchDictionaryTest
, addWordToDictionaryTest
,
199 dictionaryChangedTest
]);