OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 var kNewInputMethodTemplate = '_comp_ime_{EXT_ID}xkb:fr::fra'; | 5 var kNewInputMethodTemplate = '_comp_ime_{EXT_ID}xkb:fr::fra'; |
6 var kInitialInputMethodRegex = /_comp_ime_([a-z]{32})xkb:us::eng/; | 6 var kInitialInputMethodRegex = /_comp_ime_([a-z]{32})xkb:us::eng/; |
7 var kInvalidInputMethod = 'xx::xxx'; | 7 var kInvalidInputMethod = 'xx::xxx'; |
8 | 8 |
9 var testParams = { | 9 var testParams = { |
10 initialInputMethod: '', | 10 initialInputMethod: '', |
11 newInputMethod: '' | 11 newInputMethod: '', |
| 12 dictionaryLoaded: null, |
12 }; | 13 }; |
13 | 14 |
14 // The tests needs to be executed in order. | 15 // The tests needs to be executed in order. |
15 | 16 |
16 function initTests() { | 17 function initTests() { |
17 console.log('initTest: Getting initial inputMethod'); | 18 console.log('initTest: Getting initial inputMethod'); |
18 chrome.inputMethodPrivate.getCurrentInputMethod(function(inputMethod) { | 19 chrome.inputMethodPrivate.getCurrentInputMethod(function(inputMethod) { |
19 testParams.initialInputMethod = inputMethod; | 20 testParams.initialInputMethod = inputMethod; |
20 | 21 |
21 var match = inputMethod.match(kInitialInputMethodRegex); | 22 var match = inputMethod.match(kInitialInputMethodRegex); |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
92 foundInitialInputMethod = true; | 93 foundInitialInputMethod = true; |
93 if (inputMethods[i].id == testParams.newInputMethod) | 94 if (inputMethods[i].id == testParams.newInputMethod) |
94 foundNewInputMethod = true; | 95 foundNewInputMethod = true; |
95 } | 96 } |
96 chrome.test.assertTrue(foundInitialInputMethod); | 97 chrome.test.assertTrue(foundInitialInputMethod); |
97 chrome.test.assertTrue(foundNewInputMethod); | 98 chrome.test.assertTrue(foundNewInputMethod); |
98 chrome.test.succeed(); | 99 chrome.test.succeed(); |
99 }); | 100 }); |
100 } | 101 } |
101 | 102 |
102 function initDictionaryNotLoadedTest() { | 103 // Helper function |
103 // This test must come first because the dictionary is only lazy loaded after | 104 function getFetchPromise() { |
104 // this call is made. | 105 return new Promise(function(resolve, reject) { |
105 chrome.inputMethodPrivate.fetchAllDictionaryWords(function(words) { | 106 chrome.inputMethodPrivate.fetchAllDictionaryWords(function(words) { |
106 chrome.test.assertTrue(words === undefined); | 107 if (!!chrome.runtime.lastError) { |
107 chrome.test.assertTrue(!!chrome.runtime.lastError); | 108 reject(Error(chrome.runtime.lastError)); |
108 chrome.test.succeed(); | 109 } else { |
| 110 resolve(words); |
| 111 } |
| 112 }); |
109 }); | 113 }); |
110 } | 114 } |
111 | 115 |
112 function initDictionaryTests() { | 116 // Helper function |
113 chrome.inputMethodPrivate.fetchAllDictionaryWords(function(words) { | 117 function getAddPromise(word) { |
114 chrome.test.assertTrue(words !== undefined); | 118 return new Promise(function(resolve, reject) { |
115 chrome.test.assertTrue(words.length === 0); | 119 chrome.inputMethodPrivate.addWordToDictionary(word, function() { |
116 chrome.test.succeed(); | 120 if (!!chrome.runtime.lastError) { |
| 121 reject(Error(chrome.runtime.lastError)); |
| 122 } else { |
| 123 resolve(); |
| 124 } |
| 125 }); |
117 }); | 126 }); |
118 } | 127 } |
119 | 128 |
| 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'); |
| 136 resolve(); |
| 137 }); |
| 138 message = 'after'; |
| 139 }); |
| 140 // We don't need to wait for the promise to resolve before continuing since |
| 141 // promises are async wrappers. |
| 142 chrome.test.succeed(); |
| 143 } |
| 144 |
| 145 function fetchDictionaryTest() { |
| 146 testParams.dictionaryLoaded |
| 147 .then(function () { |
| 148 return getFetchPromise(); |
| 149 }) |
| 150 .then(function confirmFetch(words) { |
| 151 chrome.test.assertTrue(words !== undefined); |
| 152 chrome.test.assertTrue(words.length === 0); |
| 153 chrome.test.succeed(); |
| 154 }); |
| 155 } |
| 156 |
120 function addWordToDictionaryTest() { | 157 function addWordToDictionaryTest() { |
121 chrome.inputMethodPrivate.addWordToDictionary('helloworld', function() { | 158 var wordToAdd = 'helloworld'; |
122 chrome.inputMethodPrivate.fetchAllDictionaryWords(function(words) { | 159 testParams.dictionaryLoaded |
123 chrome.test.assertTrue(words.length === 1); | 160 .then(function() { |
124 chrome.test.assertEq(words[0], 'helloworld'); | 161 return getAddPromise(wordToAdd); |
125 chrome.test.succeed(); | 162 }) |
126 }); | 163 // Adding the same word results in an error. |
127 }); | 164 .then(function() { |
| 165 return getAddPromise(wordToAdd); |
| 166 }) |
| 167 .catch(function(error) { |
| 168 chrome.test.assertTrue(!!error.message); |
| 169 return getFetchPromise(); |
| 170 }) |
| 171 .then(function(words) { |
| 172 chrome.test.assertTrue(words.length === 1); |
| 173 chrome.test.assertEq(words[0], wordToAdd); |
| 174 chrome.test.succeed(); |
| 175 }); |
| 176 } |
| 177 |
| 178 function dictionaryChangedTest() { |
| 179 var wordToAdd = 'helloworld2'; |
| 180 testParams.dictionaryLoaded |
| 181 .then(function() { |
| 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(); |
| 188 }); |
| 189 }) |
| 190 .then(function() { |
| 191 return getAddPromise(wordToAdd); |
| 192 }); |
128 } | 193 } |
129 | 194 |
130 chrome.test.sendMessage('ready'); | 195 chrome.test.sendMessage('ready'); |
131 chrome.test.runTests( | 196 chrome.test.runTests( |
132 [initTests, setTest, getTest, observeTest, setInvalidTest, getListTest, | 197 [initTests, setTest, getTest, observeTest, setInvalidTest, getListTest, |
133 initDictionaryNotLoadedTest, initDictionaryTests, | 198 loadDictionaryAsyncTest, fetchDictionaryTest, addWordToDictionaryTest, |
134 addWordToDictionaryTest]); | 199 dictionaryChangedTest]); |
OLD | NEW |