| Index: chrome/browser/spellchecker/spellcheck_custom_dictionary_unittest.cc
|
| diff --git a/chrome/browser/spellchecker/spellcheck_custom_dictionary_unittest.cc b/chrome/browser/spellchecker/spellcheck_custom_dictionary_unittest.cc
|
| index 1073440e3fa73bf8c75ab05ce0d1493e56f66ebd..30ac46f55270a5ca77e170bae1c2bc6de9488130 100644
|
| --- a/chrome/browser/spellchecker/spellcheck_custom_dictionary_unittest.cc
|
| +++ b/chrome/browser/spellchecker/spellcheck_custom_dictionary_unittest.cc
|
| @@ -4,9 +4,11 @@
|
|
|
| #include <vector>
|
|
|
| +#include "base/file_util.h"
|
| #include "chrome/browser/spellchecker/spellcheck_custom_dictionary.h"
|
| #include "chrome/browser/spellchecker/spellcheck_factory.h"
|
| #include "chrome/browser/spellchecker/spellcheck_service.h"
|
| +#include "chrome/common/chrome_constants.h"
|
| #include "chrome/common/spellcheck_common.h"
|
| #include "chrome/test/base/testing_profile.h"
|
| #include "content/public/test/test_browser_thread.h"
|
| @@ -164,3 +166,68 @@ TEST_F(SpellcheckCustomDictionaryTest, MultiProfile) {
|
| // TearDown();
|
| MessageLoop::current()->RunUntilIdle();
|
| }
|
| +
|
| +TEST_F(SpellcheckCustomDictionaryTest, Reliability) {
|
| + FilePath dictionary_path(
|
| + profile_->GetPath().Append(chrome::kCustomDictionaryFileName));
|
| + SpellcheckService* spellcheck_service =
|
| + SpellcheckServiceFactory::GetForProfile(profile_.get());
|
| + SpellcheckCustomDictionary* custom_dictionary =
|
| + spellcheck_service->GetCustomDictionary();
|
| + WordList loaded_custom_words;
|
| + WordList expected;
|
| + std::string content = "";
|
| +
|
| + // Legacy empty dictionary should be converted to new format empty dicitonary.
|
| + content = "";
|
| + file_util::WriteFile(dictionary_path, content.c_str(), content.length());
|
| + custom_dictionary->LoadDictionaryIntoCustomWordList(&loaded_custom_words);
|
| + EXPECT_EQ(loaded_custom_words, expected);
|
| +
|
| + // Legacy dictionary with two words should be converted to new format
|
| + // dictionary with two words.
|
| + content = "foo\nbar\n";
|
| + file_util::WriteFile(dictionary_path, content.c_str(), content.length());
|
| + custom_dictionary->LoadDictionaryIntoCustomWordList(&loaded_custom_words);
|
| + expected.push_back("bar");
|
| + expected.push_back("foo");
|
| + EXPECT_EQ(expected, loaded_custom_words);
|
| +
|
| + // Write to dicitonary should backup previous version and write the word to
|
| + // the end of the dictionary. The dicitonary is now unsorted.
|
| + custom_dictionary->WriteWordToCustomDictionary("baz");
|
| +
|
| + // Reloading the custom dicitonary will backup the unsorted version of the
|
| + // dictionary and write a sorted version to disk as part of the cleanup that
|
| + // happens on load.
|
| + custom_dictionary->LoadDictionaryIntoCustomWordList(&loaded_custom_words);
|
| + expected.insert(expected.begin() + 1, "baz");
|
| + EXPECT_EQ(expected, loaded_custom_words);
|
| +
|
| + // If the dictionary file is corrupted on disk, the previous version should be
|
| + // reloaded. The previous version is unsorted. The load process sorts it as
|
| + // part of cleanup.
|
| + content.clear();
|
| + file_util::ReadFileToString(dictionary_path, &content);
|
| + content.append("corruption");
|
| + file_util::WriteFile(dictionary_path, content.c_str(), content.length());
|
| + custom_dictionary->LoadDictionaryIntoCustomWordList(&loaded_custom_words);
|
| + EXPECT_EQ(expected, loaded_custom_words);
|
| +
|
| + // Write "fun" successfully, but corrupt the write of "zoo". Reloading the
|
| + // dicitonary should undo the "zoo" write and see only "fun" word at the end.
|
| + custom_dictionary->WriteWordToCustomDictionary("fun");
|
| + custom_dictionary->WriteWordToCustomDictionary("zoo");
|
| + content.clear();
|
| + file_util::ReadFileToString(dictionary_path, &content);
|
| + content.append("corruption");
|
| + file_util::WriteFile(dictionary_path, content.c_str(), content.length());
|
| + custom_dictionary->LoadDictionaryIntoCustomWordList(&loaded_custom_words);
|
| + expected.push_back("fun");
|
| + // The write of "zoo" was corrupted, so not expecting to see it.
|
| + EXPECT_EQ(expected, loaded_custom_words);
|
| +
|
| + // Flush the loop now to prevent service init tasks from being run during
|
| + // TearDown();
|
| + MessageLoop::current()->RunUntilIdle();
|
| +}
|
|
|