OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 #include <vector> | 5 #include <vector> |
6 | 6 |
7 #include "base/file_util.h" | 7 #include "base/file_util.h" |
8 #include "base/string_number_conversions.h" | |
8 #include "chrome/browser/spellchecker/spellcheck_custom_dictionary.h" | 9 #include "chrome/browser/spellchecker/spellcheck_custom_dictionary.h" |
9 #include "chrome/browser/spellchecker/spellcheck_factory.h" | 10 #include "chrome/browser/spellchecker/spellcheck_factory.h" |
10 #include "chrome/browser/spellchecker/spellcheck_service.h" | 11 #include "chrome/browser/spellchecker/spellcheck_service.h" |
11 #include "chrome/common/chrome_constants.h" | 12 #include "chrome/common/chrome_constants.h" |
12 #include "chrome/common/spellcheck_common.h" | 13 #include "chrome/common/spellcheck_common.h" |
13 #include "chrome/test/base/testing_profile.h" | 14 #include "chrome/test/base/testing_profile.h" |
14 #include "content/public/test/test_browser_thread.h" | 15 #include "content/public/test/test_browser_thread.h" |
16 #include "sync/api/sync_change.h" | |
17 #include "sync/api/sync_data.h" | |
18 #include "sync/api/sync_error_factory.h" | |
19 #include "sync/api/sync_error_factory_mock.h" | |
20 #include "sync/protocol/sync.pb.h" | |
15 #include "testing/gmock/include/gmock/gmock.h" | 21 #include "testing/gmock/include/gmock/gmock.h" |
16 #include "testing/gtest/include/gtest/gtest.h" | 22 #include "testing/gtest/include/gtest/gtest.h" |
17 | 23 |
18 using content::BrowserThread; | 24 using content::BrowserThread; |
19 using chrome::spellcheck_common::WordList; | 25 using chrome::spellcheck_common::WordList; |
20 | 26 |
27 namespace { | |
28 const size_t LIMIT = 1300; | |
groby-ooo-7-16
2012/12/19 22:16:00
Please document. Or use a shared constant.
please use gerrit instead
2012/12/22 03:20:19
Done. Using constant in chrome::spellcheck_common.
| |
29 } | |
30 | |
21 static ProfileKeyedService* BuildSpellcheckService(Profile* profile) { | 31 static ProfileKeyedService* BuildSpellcheckService(Profile* profile) { |
22 return new SpellcheckService(profile); | 32 return new SpellcheckService(profile); |
23 } | 33 } |
24 | 34 |
25 class SpellcheckCustomDictionaryTest : public testing::Test { | 35 class SpellcheckCustomDictionaryTest : public testing::Test { |
26 protected: | 36 protected: |
27 SpellcheckCustomDictionaryTest() | 37 SpellcheckCustomDictionaryTest() |
28 : ui_thread_(BrowserThread::UI, &message_loop_), | 38 : ui_thread_(BrowserThread::UI, &message_loop_), |
29 file_thread_(BrowserThread::FILE, &message_loop_), | 39 file_thread_(BrowserThread::FILE, &message_loop_), |
30 profile_(new TestingProfile()) { | 40 profile_(new TestingProfile) { |
31 } | 41 } |
32 | 42 |
33 void SetUp() OVERRIDE { | 43 void SetUp() OVERRIDE { |
34 // Use SetTestingFactoryAndUse to force creation and initialization. | 44 // Use SetTestingFactoryAndUse to force creation and initialization. |
35 SpellcheckServiceFactory::GetInstance()->SetTestingFactoryAndUse( | 45 SpellcheckServiceFactory::GetInstance()->SetTestingFactoryAndUse( |
36 profile_.get(), &BuildSpellcheckService); | 46 profile_.get(), &BuildSpellcheckService); |
37 } | 47 } |
38 | 48 |
39 void TearDown() OVERRIDE { | 49 void TearDown() OVERRIDE { |
40 MessageLoop::current()->RunUntilIdle(); | 50 MessageLoop::current()->RunUntilIdle(); |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
77 expected.push_back("bar"); | 87 expected.push_back("bar"); |
78 EXPECT_EQ(custom_dictionary->GetWords(), expected); | 88 EXPECT_EQ(custom_dictionary->GetWords(), expected); |
79 | 89 |
80 custom_dictionary->CustomWordRemovedLocally("foo"); | 90 custom_dictionary->CustomWordRemovedLocally("foo"); |
81 custom_dictionary->CustomWordRemovedLocally("bar"); | 91 custom_dictionary->CustomWordRemovedLocally("bar"); |
82 expected.clear(); | 92 expected.clear(); |
83 EXPECT_EQ(custom_dictionary->GetWords(), expected); | 93 EXPECT_EQ(custom_dictionary->GetWords(), expected); |
84 } | 94 } |
85 | 95 |
86 TEST_F(SpellcheckCustomDictionaryTest, SaveAndLoad) { | 96 TEST_F(SpellcheckCustomDictionaryTest, SaveAndLoad) { |
87 SpellcheckService* spellcheck_service = | 97 FilePath path = profile_->GetPath().Append(chrome::kCustomDictionaryFileName); |
88 SpellcheckServiceFactory::GetForProfile(profile_.get()); | |
89 SpellcheckCustomDictionary* custom_dictionary = | |
90 spellcheck_service->GetCustomDictionary(); | |
91 | |
92 WordList loaded_custom_words; | 98 WordList loaded_custom_words; |
93 custom_dictionary->LoadDictionaryIntoCustomWordList(&loaded_custom_words); | 99 SpellcheckCustomDictionary::LoadDictionaryIntoCustomWordList( |
100 &loaded_custom_words, path); | |
94 | 101 |
95 // The custom word list should be empty now. | 102 // The custom word list should be empty now. |
96 WordList expected; | 103 WordList expected; |
97 EXPECT_EQ(loaded_custom_words, expected); | 104 EXPECT_EQ(loaded_custom_words, expected); |
98 | 105 |
99 custom_dictionary->WriteWordToCustomDictionary("foo"); | 106 SpellcheckCustomDictionary::WriteWordToCustomDictionary("foo", path); |
100 expected.push_back("foo"); | 107 expected.push_back("foo"); |
101 | 108 |
102 custom_dictionary->WriteWordToCustomDictionary("bar"); | 109 SpellcheckCustomDictionary::WriteWordToCustomDictionary("bar", path); |
103 expected.push_back("bar"); | 110 expected.push_back("bar"); |
104 | 111 |
105 // The custom word list should include written words. | 112 // The custom word list should include written words. |
106 custom_dictionary->LoadDictionaryIntoCustomWordList(&loaded_custom_words); | 113 SpellcheckCustomDictionary::LoadDictionaryIntoCustomWordList( |
114 &loaded_custom_words, path); | |
107 std::sort(expected.begin(), expected.end()); | 115 std::sort(expected.begin(), expected.end()); |
108 EXPECT_EQ(loaded_custom_words, expected); | 116 EXPECT_EQ(loaded_custom_words, expected); |
109 | 117 |
110 // Load in another instance of SpellCheckService. | 118 SpellcheckCustomDictionary::EraseWordFromCustomDictionary("foo", path); |
111 // The result should be the same. | 119 SpellcheckCustomDictionary::EraseWordFromCustomDictionary("bar", path); |
112 SpellcheckService spellcheck_service2(profile_.get()); | 120 SpellcheckCustomDictionary::LoadDictionaryIntoCustomWordList( |
113 WordList loaded_custom_words2; | 121 &loaded_custom_words, path); |
114 spellcheck_service2.GetCustomDictionary()-> | |
115 LoadDictionaryIntoCustomWordList(&loaded_custom_words2); | |
116 EXPECT_EQ(loaded_custom_words2, expected); | |
117 | |
118 custom_dictionary->EraseWordFromCustomDictionary("foo"); | |
119 custom_dictionary->EraseWordFromCustomDictionary("bar"); | |
120 custom_dictionary->LoadDictionaryIntoCustomWordList(&loaded_custom_words); | |
121 expected.clear(); | 122 expected.clear(); |
122 EXPECT_EQ(loaded_custom_words, expected); | 123 EXPECT_EQ(loaded_custom_words, expected); |
123 | 124 |
124 // Flush the loop now to prevent service init tasks from being run during | 125 // Flush the loop now to prevent service init tasks from being run during |
125 // TearDown(); | 126 // TearDown(); |
126 MessageLoop::current()->RunUntilIdle(); | 127 MessageLoop::current()->RunUntilIdle(); |
127 } | 128 } |
128 | 129 |
129 TEST_F(SpellcheckCustomDictionaryTest, MultiProfile) { | 130 TEST_F(SpellcheckCustomDictionaryTest, MultiProfile) { |
130 SpellcheckService* spellcheck_service = | 131 SpellcheckService* spellcheck_service = |
131 SpellcheckServiceFactory::GetForProfile(profile_.get()); | 132 SpellcheckServiceFactory::GetForProfile(profile_.get()); |
132 SpellcheckCustomDictionary* custom_dictionary = | 133 SpellcheckCustomDictionary* custom_dictionary = |
133 spellcheck_service->GetCustomDictionary(); | 134 spellcheck_service->GetCustomDictionary(); |
134 TestingProfile profile2; | 135 TestingProfile profile2; |
135 SpellcheckService* spellcheck_service2 = | 136 SpellcheckService* spellcheck_service2 = |
136 static_cast<SpellcheckService*>( | 137 static_cast<SpellcheckService*>( |
137 SpellcheckServiceFactory::GetInstance()->SetTestingFactoryAndUse( | 138 SpellcheckServiceFactory::GetInstance()->SetTestingFactoryAndUse( |
138 &profile2, &BuildSpellcheckService)); | 139 &profile2, &BuildSpellcheckService)); |
139 SpellcheckCustomDictionary* custom_dictionary2 = | 140 SpellcheckCustomDictionary* custom_dictionary2 = |
140 spellcheck_service2->GetCustomDictionary(); | 141 spellcheck_service2->GetCustomDictionary(); |
141 | 142 |
142 WordList expected1; | 143 WordList expected1; |
143 WordList expected2; | 144 WordList expected2; |
144 | 145 |
145 custom_dictionary->WriteWordToCustomDictionary("foo"); | 146 custom_dictionary->CustomWordAddedLocally("foo"); |
146 custom_dictionary->WriteWordToCustomDictionary("bar"); | 147 custom_dictionary->CustomWordAddedLocally("bar"); |
147 expected1.push_back("foo"); | 148 expected1.push_back("foo"); |
148 expected1.push_back("bar"); | 149 expected1.push_back("bar"); |
149 | 150 |
150 custom_dictionary2->WriteWordToCustomDictionary("hoge"); | 151 custom_dictionary2->CustomWordAddedLocally("hoge"); |
151 custom_dictionary2->WriteWordToCustomDictionary("fuga"); | 152 custom_dictionary2->CustomWordAddedLocally("fuga"); |
152 expected2.push_back("hoge"); | 153 expected2.push_back("hoge"); |
153 expected2.push_back("fuga"); | 154 expected2.push_back("fuga"); |
154 | 155 |
155 WordList actual1; | 156 WordList actual1 = custom_dictionary->GetWords(); |
156 custom_dictionary->LoadDictionaryIntoCustomWordList(&actual1); | 157 std::sort(actual1.begin(), actual1.end()); |
157 std::sort(expected1.begin(), expected1.end()); | 158 std::sort(expected1.begin(), expected1.end()); |
158 EXPECT_EQ(actual1, expected1); | 159 EXPECT_EQ(actual1, expected1); |
159 | 160 |
160 WordList actual2; | 161 WordList actual2 = custom_dictionary2->GetWords(); |
161 custom_dictionary2->LoadDictionaryIntoCustomWordList(&actual2); | 162 std::sort(actual2.begin(), actual2.end()); |
162 std::sort(expected2.begin(), expected2.end()); | 163 std::sort(expected2.begin(), expected2.end()); |
163 EXPECT_EQ(actual2, expected2); | 164 EXPECT_EQ(actual2, expected2); |
164 | 165 |
165 // Flush the loop now to prevent service init tasks from being run during | 166 // Flush the loop now to prevent service init tasks from being run during |
166 // TearDown(); | 167 // TearDown(); |
167 MessageLoop::current()->RunUntilIdle(); | 168 MessageLoop::current()->RunUntilIdle(); |
168 } | 169 } |
169 | 170 |
170 // Legacy empty dictionary should be converted to new format empty dicitonary. | 171 // Legacy empty dictionary should be converted to new format empty dictionary. |
171 TEST_F(SpellcheckCustomDictionaryTest, LegacyEmptyDictionaryShouldBeConverted) { | 172 TEST_F(SpellcheckCustomDictionaryTest, LegacyEmptyDictionaryShouldBeConverted) { |
172 FilePath dictionary_path( | 173 FilePath path = profile_->GetPath().Append(chrome::kCustomDictionaryFileName); |
173 profile_->GetPath().Append(chrome::kCustomDictionaryFileName)); | |
174 SpellcheckService* spellcheck_service = | |
175 SpellcheckServiceFactory::GetForProfile(profile_.get()); | |
176 SpellcheckCustomDictionary* custom_dictionary = | |
177 spellcheck_service->GetCustomDictionary(); | |
178 WordList loaded_custom_words; | 174 WordList loaded_custom_words; |
179 | 175 |
180 std::string content; | 176 std::string content; |
181 file_util::WriteFile(dictionary_path, content.c_str(), content.length()); | 177 file_util::WriteFile(path, content.c_str(), content.length()); |
182 custom_dictionary->LoadDictionaryIntoCustomWordList(&loaded_custom_words); | 178 SpellcheckCustomDictionary::LoadDictionaryIntoCustomWordList( |
179 &loaded_custom_words, path); | |
183 EXPECT_TRUE(loaded_custom_words.empty()); | 180 EXPECT_TRUE(loaded_custom_words.empty()); |
184 | 181 |
185 // Flush the loop now to prevent service init tasks from being run during | 182 // Flush the loop now to prevent service init tasks from being run during |
186 // TearDown(); | 183 // TearDown(); |
187 MessageLoop::current()->RunUntilIdle(); | 184 MessageLoop::current()->RunUntilIdle(); |
188 } | 185 } |
189 | 186 |
190 // Legacy dictionary with two words should be converted to new format dictionary | 187 // Legacy dictionary with two words should be converted to new format dictionary |
191 // with two words. | 188 // with two words. |
192 TEST_F(SpellcheckCustomDictionaryTest, | 189 TEST_F(SpellcheckCustomDictionaryTest, |
193 LegacyDictionaryWithTwoWordsShouldBeConverted) { | 190 LegacyDictionaryWithTwoWordsShouldBeConverted) { |
194 FilePath dictionary_path( | 191 FilePath path = profile_->GetPath().Append(chrome::kCustomDictionaryFileName); |
195 profile_->GetPath().Append(chrome::kCustomDictionaryFileName)); | |
196 SpellcheckService* spellcheck_service = | |
197 SpellcheckServiceFactory::GetForProfile(profile_.get()); | |
198 SpellcheckCustomDictionary* custom_dictionary = | |
199 spellcheck_service->GetCustomDictionary(); | |
200 WordList loaded_custom_words; | 192 WordList loaded_custom_words; |
201 WordList expected; | 193 WordList expected; |
202 | 194 |
203 std::string content = "foo\nbar"; | 195 std::string content = "foo\nbar"; |
204 file_util::WriteFile(dictionary_path, content.c_str(), content.length()); | 196 file_util::WriteFile(path, content.c_str(), content.length()); |
205 custom_dictionary->LoadDictionaryIntoCustomWordList(&loaded_custom_words); | 197 SpellcheckCustomDictionary::LoadDictionaryIntoCustomWordList( |
198 &loaded_custom_words, path); | |
206 expected.push_back("bar"); | 199 expected.push_back("bar"); |
207 expected.push_back("foo"); | 200 expected.push_back("foo"); |
208 EXPECT_EQ(expected, loaded_custom_words); | 201 EXPECT_EQ(expected, loaded_custom_words); |
209 | 202 |
210 // Flush the loop now to prevent service init tasks from being run during | 203 // Flush the loop now to prevent service init tasks from being run during |
211 // TearDown(); | 204 // TearDown(); |
212 MessageLoop::current()->RunUntilIdle(); | 205 MessageLoop::current()->RunUntilIdle(); |
213 } | 206 } |
214 | 207 |
215 // Words with spaces are illegal and should be removed. | 208 // Words with spaces are illegal and should be removed. |
216 TEST_F(SpellcheckCustomDictionaryTest, | 209 TEST_F(SpellcheckCustomDictionaryTest, |
217 IllegalWordsShouldBeRemovedFromDictionary) { | 210 IllegalWordsShouldBeRemovedFromDictionary) { |
218 FilePath dictionary_path( | 211 FilePath path = profile_->GetPath().Append(chrome::kCustomDictionaryFileName); |
219 profile_->GetPath().Append(chrome::kCustomDictionaryFileName)); | |
220 SpellcheckService* spellcheck_service = | |
221 SpellcheckServiceFactory::GetForProfile(profile_.get()); | |
222 SpellcheckCustomDictionary* custom_dictionary = | |
223 spellcheck_service->GetCustomDictionary(); | |
224 WordList loaded_custom_words; | 212 WordList loaded_custom_words; |
225 WordList expected; | 213 WordList expected; |
226 | 214 |
227 std::string content = "foo\nfoo bar\nbar\nfoo bar"; | 215 std::string content = "foo\nfoo bar\nbar\nfoo bar"; |
228 file_util::WriteFile(dictionary_path, content.c_str(), content.length()); | 216 file_util::WriteFile(path, content.c_str(), content.length()); |
229 custom_dictionary->LoadDictionaryIntoCustomWordList(&loaded_custom_words); | 217 SpellcheckCustomDictionary::LoadDictionaryIntoCustomWordList( |
218 &loaded_custom_words, path); | |
230 expected.push_back("bar"); | 219 expected.push_back("bar"); |
231 expected.push_back("foo"); | 220 expected.push_back("foo"); |
232 EXPECT_EQ(expected, loaded_custom_words); | 221 EXPECT_EQ(expected, loaded_custom_words); |
233 | 222 |
234 // Flush the loop now to prevent service init tasks from being run during | 223 // Flush the loop now to prevent service init tasks from being run during |
235 // TearDown(); | 224 // TearDown(); |
236 MessageLoop::current()->RunUntilIdle(); | 225 MessageLoop::current()->RunUntilIdle(); |
237 } | 226 } |
238 | 227 |
239 // Write to dicitonary should backup previous version and write the word to the | 228 // Write to dictionary should backup previous version and write the word to the |
240 // end of the dictionary. If the dictionary file is corrupted on disk, the | 229 // end of the dictionary. If the dictionary file is corrupted on disk, the |
241 // previous version should be reloaded. | 230 // previous version should be reloaded. |
242 TEST_F(SpellcheckCustomDictionaryTest, CorruptedWriteShouldBeRecovered) { | 231 TEST_F(SpellcheckCustomDictionaryTest, CorruptedWriteShouldBeRecovered) { |
243 FilePath dictionary_path( | 232 FilePath path = profile_->GetPath().Append(chrome::kCustomDictionaryFileName); |
244 profile_->GetPath().Append(chrome::kCustomDictionaryFileName)); | 233 WordList loaded_custom_words; |
234 WordList expected; | |
235 | |
236 std::string content = "foo\nbar"; | |
237 file_util::WriteFile(path, content.c_str(), content.length()); | |
238 SpellcheckCustomDictionary::LoadDictionaryIntoCustomWordList( | |
239 &loaded_custom_words, path); | |
240 expected.push_back("bar"); | |
241 expected.push_back("foo"); | |
242 EXPECT_EQ(expected, loaded_custom_words); | |
243 | |
244 SpellcheckCustomDictionary::WriteWordToCustomDictionary("baz", path); | |
245 content.clear(); | |
246 file_util::ReadFileToString(path, &content); | |
247 content.append("corruption"); | |
248 file_util::WriteFile(path, content.c_str(), content.length()); | |
249 SpellcheckCustomDictionary::LoadDictionaryIntoCustomWordList( | |
250 &loaded_custom_words, path); | |
251 EXPECT_EQ(expected, loaded_custom_words); | |
252 | |
253 // Flush the loop now to prevent service init tasks from being run during | |
254 // TearDown(); | |
255 MessageLoop::current()->RunUntilIdle(); | |
256 } | |
257 | |
258 TEST_F(SpellcheckCustomDictionaryTest, | |
259 GetAllSyncDataAccuratelyReflectsDictionaryState) { | |
260 SpellcheckCustomDictionary* dictionary = | |
261 SpellcheckServiceFactory::GetForProfile( | |
262 profile_.get())->GetCustomDictionary(); | |
263 | |
264 syncer::SyncDataList data = dictionary->GetAllSyncData(syncer::DICTIONARY); | |
265 EXPECT_TRUE(data.empty()); | |
266 | |
267 EXPECT_TRUE(dictionary->CustomWordAddedLocally("foo")); | |
268 EXPECT_TRUE(dictionary->CustomWordAddedLocally("bar")); | |
269 data = dictionary->GetAllSyncData(syncer::DICTIONARY); | |
270 EXPECT_EQ(2UL, data.size()); | |
271 std::vector<std::string> words; | |
272 words.push_back("foo"); | |
273 words.push_back("bar"); | |
274 for (size_t i = 0; i < data.size(); i++) { | |
275 EXPECT_TRUE(data[i].GetSpecifics().has_dictionary()); | |
276 EXPECT_EQ(syncer::DICTIONARY, data[i].GetDataType()); | |
277 EXPECT_EQ(words[i], data[i].GetTag()); | |
278 EXPECT_EQ(words[i], data[i].GetSpecifics().dictionary().word()); | |
279 } | |
280 | |
281 EXPECT_TRUE(dictionary->CustomWordRemovedLocally("foo")); | |
282 EXPECT_TRUE(dictionary->CustomWordRemovedLocally("bar")); | |
283 data = dictionary->GetAllSyncData(syncer::DICTIONARY); | |
284 EXPECT_TRUE(data.empty()); | |
285 | |
286 // Flush the loop now to prevent service init tasks from being run during | |
287 // TearDown(); | |
288 MessageLoop::current()->RunUntilIdle(); | |
groby-ooo-7-16
2012/12/19 22:16:00
Why not run them during TearDown()? It seems _ever
please use gerrit instead
2012/12/22 03:20:19
We need to run RunUntilIdle here before the spellc
| |
289 } | |
290 | |
291 TEST_F(SpellcheckCustomDictionaryTest, GetAllSyncDataHasLimit) { | |
292 SpellcheckCustomDictionary* dictionary = | |
293 SpellcheckServiceFactory::GetForProfile( | |
294 profile_.get())->GetCustomDictionary(); | |
295 | |
296 for (size_t i = 0; i < LIMIT - 1; i++) { | |
297 EXPECT_TRUE(dictionary->CustomWordAddedLocally( | |
298 "foo" + base::Uint64ToString(i))); | |
299 } | |
300 EXPECT_EQ(LIMIT - 1, dictionary->GetWords().size()); | |
301 EXPECT_EQ(LIMIT - 1, dictionary->GetAllSyncData(syncer::DICTIONARY).size()); | |
302 | |
303 EXPECT_TRUE(dictionary->CustomWordAddedLocally("baz")); | |
304 EXPECT_EQ(LIMIT, dictionary->GetWords().size()); | |
305 EXPECT_EQ(LIMIT, dictionary->GetAllSyncData(syncer::DICTIONARY).size()); | |
306 | |
307 EXPECT_TRUE(dictionary->CustomWordAddedLocally("bar")); | |
308 EXPECT_EQ(LIMIT + 1, dictionary->GetWords().size()); | |
309 EXPECT_EQ(LIMIT, dictionary->GetAllSyncData(syncer::DICTIONARY).size()); | |
310 | |
311 EXPECT_TRUE(dictionary->CustomWordAddedLocally("snafoo")); | |
312 EXPECT_EQ(LIMIT + 2, dictionary->GetWords().size()); | |
313 EXPECT_EQ(LIMIT, dictionary->GetAllSyncData(syncer::DICTIONARY).size()); | |
314 | |
315 // Flush the loop now to prevent service init tasks from being run during | |
316 // TearDown(); | |
317 MessageLoop::current()->RunUntilIdle(); | |
318 } | |
319 | |
320 TEST_F(SpellcheckCustomDictionaryTest, ProcessSyncChanges) { | |
321 SpellcheckService* spellcheck_service = | |
322 SpellcheckServiceFactory::GetForProfile(profile_.get()); | |
323 SpellcheckCustomDictionary* dictionary = | |
324 spellcheck_service->GetCustomDictionary(); | |
325 | |
326 dictionary->CustomWordAddedLocally("foo"); | |
327 dictionary->CustomWordAddedLocally("bar"); | |
328 | |
329 syncer::SyncChangeList changes; | |
330 { | |
331 // Add existing word. | |
332 std::string word = "foo"; | |
333 sync_pb::EntitySpecifics specifics; | |
334 specifics.mutable_dictionary()->set_word(word); | |
335 changes.push_back(syncer::SyncChange( | |
336 FROM_HERE, | |
337 syncer::SyncChange::ACTION_ADD, | |
338 syncer::SyncData::CreateLocalData(word, word, specifics))); | |
339 } | |
340 { | |
341 // Add invalid word. | |
342 std::string word = "foo bar"; | |
343 sync_pb::EntitySpecifics specifics; | |
344 specifics.mutable_dictionary()->set_word(word); | |
345 changes.push_back(syncer::SyncChange( | |
346 FROM_HERE, | |
347 syncer::SyncChange::ACTION_ADD, | |
348 syncer::SyncData::CreateLocalData(word, word, specifics))); | |
349 } | |
350 { | |
351 // Add valid word. | |
352 std::string word = "baz"; | |
353 sync_pb::EntitySpecifics specifics; | |
354 specifics.mutable_dictionary()->set_word(word); | |
355 changes.push_back(syncer::SyncChange( | |
356 FROM_HERE, | |
357 syncer::SyncChange::ACTION_ADD, | |
358 syncer::SyncData::CreateLocalData(word, word, specifics))); | |
359 } | |
360 { | |
361 // Remove missing word. | |
362 std::string word = "snafoo"; | |
363 sync_pb::EntitySpecifics specifics; | |
364 specifics.mutable_dictionary()->set_word(word); | |
365 changes.push_back(syncer::SyncChange( | |
366 FROM_HERE, | |
367 syncer::SyncChange::ACTION_DELETE, | |
368 syncer::SyncData::CreateLocalData(word, word, specifics))); | |
369 } | |
370 { | |
371 // Remove existing word. | |
372 std::string word = "bar"; | |
373 sync_pb::EntitySpecifics specifics; | |
374 specifics.mutable_dictionary()->set_word(word); | |
375 changes.push_back(syncer::SyncChange( | |
376 FROM_HERE, | |
377 syncer::SyncChange::ACTION_DELETE, | |
378 syncer::SyncData::CreateLocalData(word, word, specifics))); | |
379 } | |
380 | |
381 EXPECT_FALSE(dictionary->ProcessSyncChanges(FROM_HERE, changes).IsSet()); | |
382 | |
383 const chrome::spellcheck_common::WordList& words = dictionary->GetWords(); | |
384 EXPECT_EQ(2UL, words.size()); | |
385 EXPECT_EQ(words.end(), std::find(words.begin(), words.end(), "bar")); | |
386 EXPECT_NE(words.end(), std::find(words.begin(), words.end(), "foo")); | |
387 EXPECT_NE(words.end(), std::find(words.begin(), words.end(), "baz")); | |
388 | |
389 // Flush the loop now to prevent service init tasks from being run during | |
390 // TearDown(); | |
391 MessageLoop::current()->RunUntilIdle(); | |
392 } | |
393 | |
394 TEST_F(SpellcheckCustomDictionaryTest, MergeDataAndStartSyncing) { | |
245 SpellcheckService* spellcheck_service = | 395 SpellcheckService* spellcheck_service = |
246 SpellcheckServiceFactory::GetForProfile(profile_.get()); | 396 SpellcheckServiceFactory::GetForProfile(profile_.get()); |
247 SpellcheckCustomDictionary* custom_dictionary = | 397 SpellcheckCustomDictionary* custom_dictionary = |
248 spellcheck_service->GetCustomDictionary(); | 398 spellcheck_service->GetCustomDictionary(); |
249 WordList loaded_custom_words; | 399 TestingProfile profile2; |
250 WordList expected; | 400 SpellcheckService* spellcheck_service2 = |
251 | 401 static_cast<SpellcheckService*>( |
252 std::string content = "foo\nbar"; | 402 SpellcheckServiceFactory::GetInstance()->SetTestingFactoryAndUse( |
253 file_util::WriteFile(dictionary_path, content.c_str(), content.length()); | 403 &profile2, &BuildSpellcheckService)); |
254 custom_dictionary->LoadDictionaryIntoCustomWordList(&loaded_custom_words); | 404 SpellcheckCustomDictionary* custom_dictionary2 = |
255 expected.push_back("bar"); | 405 spellcheck_service2->GetCustomDictionary(); |
256 expected.push_back("foo"); | 406 |
257 EXPECT_EQ(expected, loaded_custom_words); | 407 custom_dictionary->AddWord("foo"); |
258 | 408 custom_dictionary2->AddWord("bar"); |
259 custom_dictionary->WriteWordToCustomDictionary("baz"); | 409 custom_dictionary2->AddWord("baz"); |
260 content.clear(); | 410 |
261 file_util::ReadFileToString(dictionary_path, &content); | 411 EXPECT_FALSE(custom_dictionary->MergeDataAndStartSyncing( |
262 content.append("corruption"); | 412 syncer::DICTIONARY, |
263 file_util::WriteFile(dictionary_path, content.c_str(), content.length()); | 413 custom_dictionary2->GetAllSyncData(syncer::DICTIONARY), |
264 custom_dictionary->LoadDictionaryIntoCustomWordList(&loaded_custom_words); | 414 scoped_ptr<syncer::SyncChangeProcessor>(custom_dictionary2), |
265 EXPECT_EQ(expected, loaded_custom_words); | 415 scoped_ptr<syncer::SyncErrorFactory>(new syncer::SyncErrorFactoryMock)). |
266 | 416 error().IsSet()); |
267 // Flush the loop now to prevent service init tasks from being run during | 417 custom_dictionary->StopSyncingForTesting(); |
268 // TearDown(); | 418 |
269 MessageLoop::current()->RunUntilIdle(); | 419 WordList words = custom_dictionary->GetWords(); |
270 } | 420 WordList words2 = custom_dictionary2->GetWords(); |
421 EXPECT_EQ(words.size(), words2.size()); | |
422 | |
423 std::sort(words.begin(), words.end()); | |
424 std::sort(words2.begin(), words2.end()); | |
425 for (size_t i = 0; i < words.size(); ++i) | |
426 EXPECT_EQ(words[i], words2[i]); | |
427 | |
428 // Flush the loop now to prevent service init tasks from being run during | |
429 // TearDown(); | |
430 MessageLoop::current()->RunUntilIdle(); | |
431 } | |
OLD | NEW |