Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(758)

Side by Side Diff: chrome/browser/spellchecker/spellcheck_custom_dictionary_unittest.cc

Issue 11414282: Improve reliability of custom spelling dictionary file. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Created 8 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 "chrome/browser/spellchecker/spellcheck_custom_dictionary.h" 8 #include "chrome/browser/spellchecker/spellcheck_custom_dictionary.h"
8 #include "chrome/browser/spellchecker/spellcheck_factory.h" 9 #include "chrome/browser/spellchecker/spellcheck_factory.h"
9 #include "chrome/browser/spellchecker/spellcheck_service.h" 10 #include "chrome/browser/spellchecker/spellcheck_service.h"
11 #include "chrome/common/chrome_constants.h"
10 #include "chrome/common/spellcheck_common.h" 12 #include "chrome/common/spellcheck_common.h"
11 #include "chrome/test/base/testing_profile.h" 13 #include "chrome/test/base/testing_profile.h"
12 #include "content/public/test/test_browser_thread.h" 14 #include "content/public/test/test_browser_thread.h"
13 #include "testing/gmock/include/gmock/gmock.h" 15 #include "testing/gmock/include/gmock/gmock.h"
14 #include "testing/gtest/include/gtest/gtest.h" 16 #include "testing/gtest/include/gtest/gtest.h"
15 17
16 using content::BrowserThread; 18 using content::BrowserThread;
17 using chrome::spellcheck_common::WordList; 19 using chrome::spellcheck_common::WordList;
18 20
19 static ProfileKeyedService* BuildSpellcheckService(Profile* profile) { 21 static ProfileKeyedService* BuildSpellcheckService(Profile* profile) {
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after
157 159
158 WordList actual2; 160 WordList actual2;
159 custom_dictionary2->LoadDictionaryIntoCustomWordList(&actual2); 161 custom_dictionary2->LoadDictionaryIntoCustomWordList(&actual2);
160 std::sort(expected2.begin(), expected2.end()); 162 std::sort(expected2.begin(), expected2.end());
161 EXPECT_EQ(actual2, expected2); 163 EXPECT_EQ(actual2, expected2);
162 164
163 // Flush the loop now to prevent service init tasks from being run during 165 // Flush the loop now to prevent service init tasks from being run during
164 // TearDown(); 166 // TearDown();
165 MessageLoop::current()->RunUntilIdle(); 167 MessageLoop::current()->RunUntilIdle();
166 } 168 }
169
170 TEST_F(SpellcheckCustomDictionaryTest, Reliability) {
groby-ooo-7-16 2012/12/11 19:25:48 nit: Can you break this into individual test cases
please use gerrit instead 2012/12/11 22:32:58 Done.
171 FilePath dictionary_path(
172 profile_->GetPath().Append(chrome::kCustomDictionaryFileName));
173 SpellcheckService* spellcheck_service =
174 SpellcheckServiceFactory::GetForProfile(profile_.get());
175 SpellcheckCustomDictionary* custom_dictionary =
176 spellcheck_service->GetCustomDictionary();
177 WordList loaded_custom_words;
178 WordList expected;
179 std::string content = "";
180
181 // Legacy empty dictionary should be converted to new format empty dicitonary.
182 content = "";
183 file_util::WriteFile(dictionary_path, content.c_str(), content.length());
184 custom_dictionary->LoadDictionaryIntoCustomWordList(&loaded_custom_words);
185 EXPECT_EQ(loaded_custom_words, expected);
186
187 // Legacy dictionary with two words should be converted to new format
188 // dictionary with two words.
189 content = "foo\nbar";
190 file_util::WriteFile(dictionary_path, content.c_str(), content.length());
191 custom_dictionary->LoadDictionaryIntoCustomWordList(&loaded_custom_words);
192 expected.push_back("bar");
193 expected.push_back("foo");
194 EXPECT_EQ(expected, loaded_custom_words);
195
196 // Words with spaces are illegal and should be removed.
197 content = "foo\nfoo bar\nbar\nfoo bar";
198 file_util::WriteFile(dictionary_path, content.c_str(), content.length());
199 custom_dictionary->LoadDictionaryIntoCustomWordList(&loaded_custom_words);
200 EXPECT_EQ(expected, loaded_custom_words);
201
202 // Write to dicitonary should backup previous version and write the word to
203 // the end of the dictionary. The dicitonary is now unsorted.
204 custom_dictionary->WriteWordToCustomDictionary("baz");
205
206 // Reloading the custom dicitonary will backup the unsorted version of the
207 // dictionary and write a sorted version to disk as part of the cleanup that
208 // happens on load.
209 custom_dictionary->LoadDictionaryIntoCustomWordList(&loaded_custom_words);
210 expected.insert(expected.begin() + 1, "baz");
211 EXPECT_EQ(expected, loaded_custom_words);
212
213 // If the dictionary file is corrupted on disk, the previous version should be
214 // reloaded. The previous version is unsorted. The load process sorts it as
215 // part of cleanup.
216 content.clear();
217 file_util::ReadFileToString(dictionary_path, &content);
218 content.append("corruption");
219 file_util::WriteFile(dictionary_path, content.c_str(), content.length());
220 custom_dictionary->LoadDictionaryIntoCustomWordList(&loaded_custom_words);
221 EXPECT_EQ(expected, loaded_custom_words);
222
223 // Write "fun" successfully, but corrupt the write of "zoo". Reloading the
224 // dicitonary should undo the "zoo" write and see only "fun" word at the end.
225 custom_dictionary->WriteWordToCustomDictionary("fun");
226 custom_dictionary->WriteWordToCustomDictionary("zoo");
227 content.clear();
228 file_util::ReadFileToString(dictionary_path, &content);
229 content.append("corruption");
230 file_util::WriteFile(dictionary_path, content.c_str(), content.length());
231 custom_dictionary->LoadDictionaryIntoCustomWordList(&loaded_custom_words);
232 expected.push_back("fun");
233 // The write of "zoo" was corrupted, so not expecting to see it.
234 EXPECT_EQ(expected, loaded_custom_words);
235
236 // Flush the loop now to prevent service init tasks from being run during
237 // TearDown();
238 MessageLoop::current()->RunUntilIdle();
239 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698