| OLD | NEW |
| (Empty) |
| 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. | |
| 4 | |
| 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'; | |
| 8 | |
| 9 var testParams = { | |
| 10 initialInputMethod: '', | |
| 11 newInputMethod: '', | |
| 12 dictionaryLoaded: null, | |
| 13 }; | |
| 14 | |
| 15 // The tests needs to be executed in order. | |
| 16 | |
| 17 function initTests() { | |
| 18 console.log('initTest: Getting initial inputMethod'); | |
| 19 chrome.inputMethodPrivate.getCurrentInputMethod(function(inputMethod) { | |
| 20 testParams.initialInputMethod = inputMethod; | |
| 21 | |
| 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(); | |
| 29 }); | |
| 30 } | |
| 31 | |
| 32 function setTest() { | |
| 33 chrome.test.assertTrue(!!testParams.newInputMethod); | |
| 34 console.log( | |
| 35 'setTest: Changing input method to: ' + testParams.newInputMethod); | |
| 36 chrome.inputMethodPrivate.setCurrentInputMethod(testParams.newInputMethod, | |
| 37 function() { | |
| 38 chrome.test.assertTrue( | |
| 39 !chrome.runtime.lastError, | |
| 40 chrome.runtime.lastError ? chrome.runtime.lastError.message : ''); | |
| 41 chrome.test.succeed(); | |
| 42 }); | |
| 43 } | |
| 44 | |
| 45 function getTest() { | |
| 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(); | |
| 51 }); | |
| 52 } | |
| 53 | |
| 54 function observeTest() { | |
| 55 chrome.test.assertTrue(!!testParams.initialInputMethod); | |
| 56 console.log('observeTest: Adding input method event listener.'); | |
| 57 | |
| 58 var listener = function(inputMethod) { | |
| 59 chrome.inputMethodPrivate.onChanged.removeListener(listener); | |
| 60 chrome.test.assertEq(testParams.initialInputMethod, inputMethod); | |
| 61 chrome.test.succeed(); | |
| 62 }; | |
| 63 chrome.inputMethodPrivate.onChanged.addListener(listener); | |
| 64 | |
| 65 console.log('observeTest: Changing input method to: ' + | |
| 66 testParams.initialInputMethod); | |
| 67 chrome.inputMethodPrivate.setCurrentInputMethod( | |
| 68 testParams.initialInputMethod); | |
| 69 } | |
| 70 | |
| 71 | |
| 72 function setInvalidTest() { | |
| 73 console.log( | |
| 74 'setInvalidTest: Changing input method to: ' + kInvalidInputMethod); | |
| 75 chrome.inputMethodPrivate.setCurrentInputMethod(kInvalidInputMethod, | |
| 76 function() { | |
| 77 chrome.test.assertTrue(!!chrome.runtime.lastError); | |
| 78 chrome.test.succeed(); | |
| 79 }); | |
| 80 } | |
| 81 | |
| 82 function getListTest() { | |
| 83 chrome.test.assertTrue(!!testParams.initialInputMethod); | |
| 84 chrome.test.assertTrue(!!testParams.newInputMethod); | |
| 85 console.log('getListTest: Getting input method list.'); | |
| 86 | |
| 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; | |
| 96 } | |
| 97 chrome.test.assertTrue(foundInitialInputMethod); | |
| 98 chrome.test.assertTrue(foundNewInputMethod); | |
| 99 chrome.test.succeed(); | |
| 100 }); | |
| 101 } | |
| 102 | |
| 103 // Helper function | |
| 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)); | |
| 109 } else { | |
| 110 resolve(words); | |
| 111 } | |
| 112 }); | |
| 113 }); | |
| 114 } | |
| 115 | |
| 116 // Helper function | |
| 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)); | |
| 122 } else { | |
| 123 resolve(); | |
| 124 } | |
| 125 }); | |
| 126 }); | |
| 127 } | |
| 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 | |
| 157 function addWordToDictionaryTest() { | |
| 158 var wordToAdd = 'helloworld'; | |
| 159 testParams.dictionaryLoaded | |
| 160 .then(function() { | |
| 161 return getAddPromise(wordToAdd); | |
| 162 }) | |
| 163 // Adding the same word results in an error. | |
| 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 }); | |
| 193 } | |
| 194 | |
| 195 chrome.test.sendMessage('ready'); | |
| 196 chrome.test.runTests( | |
| 197 [initTests, setTest, getTest, observeTest, setInvalidTest, getListTest, | |
| 198 loadDictionaryAsyncTest, fetchDictionaryTest, addWordToDictionaryTest, | |
| 199 dictionaryChangedTest]); | |
| OLD | NEW |