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..d5330b62f78971daff6a6d5ace08fa7e7711e0f2 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,74 @@ TEST_F(SpellcheckCustomDictionaryTest, MultiProfile) { |
// TearDown(); |
MessageLoop::current()->RunUntilIdle(); |
} |
+ |
+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.
|
+ 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"; |
+ 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); |
+ |
+ // Words with spaces are illegal and should be removed. |
+ content = "foo\nfoo bar\nbar\nfoo bar"; |
+ file_util::WriteFile(dictionary_path, content.c_str(), content.length()); |
+ custom_dictionary->LoadDictionaryIntoCustomWordList(&loaded_custom_words); |
+ 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(); |
+} |