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 "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 Loading... |
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 // Legacy empty dictionary should be converted to new format empty dicitonary. |
| 171 TEST_F(SpellcheckCustomDictionaryTest, LegacyEmptyDictionaryShouldBeConverted) { |
| 172 FilePath dictionary_path( |
| 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; |
| 179 |
| 180 std::string content; |
| 181 file_util::WriteFile(dictionary_path, content.c_str(), content.length()); |
| 182 custom_dictionary->LoadDictionaryIntoCustomWordList(&loaded_custom_words); |
| 183 EXPECT_TRUE(loaded_custom_words.empty()); |
| 184 |
| 185 // Flush the loop now to prevent service init tasks from being run during |
| 186 // TearDown(); |
| 187 MessageLoop::current()->RunUntilIdle(); |
| 188 } |
| 189 |
| 190 // Legacy dictionary with two words should be converted to new format dictionary |
| 191 // with two words. |
| 192 TEST_F(SpellcheckCustomDictionaryTest, |
| 193 LegacyDictionaryWithTwoWordsShouldBeConverted) { |
| 194 FilePath dictionary_path( |
| 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; |
| 201 WordList expected; |
| 202 |
| 203 std::string content = "foo\nbar"; |
| 204 file_util::WriteFile(dictionary_path, content.c_str(), content.length()); |
| 205 custom_dictionary->LoadDictionaryIntoCustomWordList(&loaded_custom_words); |
| 206 expected.push_back("bar"); |
| 207 expected.push_back("foo"); |
| 208 EXPECT_EQ(expected, loaded_custom_words); |
| 209 |
| 210 // Flush the loop now to prevent service init tasks from being run during |
| 211 // TearDown(); |
| 212 MessageLoop::current()->RunUntilIdle(); |
| 213 } |
| 214 |
| 215 // Words with spaces are illegal and should be removed. |
| 216 TEST_F(SpellcheckCustomDictionaryTest, |
| 217 IllegalWordsShouldBeRemovedFromDictionary) { |
| 218 FilePath dictionary_path( |
| 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; |
| 225 WordList expected; |
| 226 |
| 227 std::string content = "foo\nfoo bar\nbar\nfoo bar"; |
| 228 file_util::WriteFile(dictionary_path, content.c_str(), content.length()); |
| 229 custom_dictionary->LoadDictionaryIntoCustomWordList(&loaded_custom_words); |
| 230 expected.push_back("bar"); |
| 231 expected.push_back("foo"); |
| 232 EXPECT_EQ(expected, loaded_custom_words); |
| 233 |
| 234 // Flush the loop now to prevent service init tasks from being run during |
| 235 // TearDown(); |
| 236 MessageLoop::current()->RunUntilIdle(); |
| 237 } |
| 238 |
| 239 // Write to dicitonary should backup previous version and write the word to the |
| 240 // end of the dictionary. If the dictionary file is corrupted on disk, the |
| 241 // previous version should be reloaded. |
| 242 TEST_F(SpellcheckCustomDictionaryTest, CorruptedWriteShouldBeRecovered) { |
| 243 FilePath dictionary_path( |
| 244 profile_->GetPath().Append(chrome::kCustomDictionaryFileName)); |
| 245 SpellcheckService* spellcheck_service = |
| 246 SpellcheckServiceFactory::GetForProfile(profile_.get()); |
| 247 SpellcheckCustomDictionary* custom_dictionary = |
| 248 spellcheck_service->GetCustomDictionary(); |
| 249 WordList loaded_custom_words; |
| 250 WordList expected; |
| 251 |
| 252 std::string content = "foo\nbar"; |
| 253 file_util::WriteFile(dictionary_path, content.c_str(), content.length()); |
| 254 custom_dictionary->LoadDictionaryIntoCustomWordList(&loaded_custom_words); |
| 255 expected.push_back("bar"); |
| 256 expected.push_back("foo"); |
| 257 EXPECT_EQ(expected, loaded_custom_words); |
| 258 |
| 259 custom_dictionary->WriteWordToCustomDictionary("baz"); |
| 260 content.clear(); |
| 261 file_util::ReadFileToString(dictionary_path, &content); |
| 262 content.append("corruption"); |
| 263 file_util::WriteFile(dictionary_path, content.c_str(), content.length()); |
| 264 custom_dictionary->LoadDictionaryIntoCustomWordList(&loaded_custom_words); |
| 265 EXPECT_EQ(expected, loaded_custom_words); |
| 266 |
| 267 // Flush the loop now to prevent service init tasks from being run during |
| 268 // TearDown(); |
| 269 MessageLoop::current()->RunUntilIdle(); |
| 270 } |
OLD | NEW |