| 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/metrics/histogram_samples.h" | 8 #include "base/metrics/histogram_samples.h" |
| 9 #include "base/metrics/statistics_recorder.h" | 9 #include "base/metrics/statistics_recorder.h" |
| 10 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
| (...skipping 208 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 219 WordSet actual2 = custom_dictionary2->GetWords(); | 219 WordSet actual2 = custom_dictionary2->GetWords(); |
| 220 EXPECT_EQ(actual2, expected2); | 220 EXPECT_EQ(actual2, expected2); |
| 221 } | 221 } |
| 222 | 222 |
| 223 // Legacy empty dictionary should be converted to new format empty dictionary. | 223 // Legacy empty dictionary should be converted to new format empty dictionary. |
| 224 TEST_F(SpellcheckCustomDictionaryTest, LegacyEmptyDictionaryShouldBeConverted) { | 224 TEST_F(SpellcheckCustomDictionaryTest, LegacyEmptyDictionaryShouldBeConverted) { |
| 225 base::FilePath path = | 225 base::FilePath path = |
| 226 profile_.GetPath().Append(chrome::kCustomDictionaryFileName); | 226 profile_.GetPath().Append(chrome::kCustomDictionaryFileName); |
| 227 | 227 |
| 228 std::string content; | 228 std::string content; |
| 229 file_util::WriteFile(path, content.c_str(), content.length()); | 229 base::WriteFile(path, content.c_str(), content.length()); |
| 230 WordList loaded_custom_words = LoadDictionaryFile(path); | 230 WordList loaded_custom_words = LoadDictionaryFile(path); |
| 231 EXPECT_TRUE(loaded_custom_words.empty()); | 231 EXPECT_TRUE(loaded_custom_words.empty()); |
| 232 } | 232 } |
| 233 | 233 |
| 234 // Legacy dictionary with two words should be converted to new format dictionary | 234 // Legacy dictionary with two words should be converted to new format dictionary |
| 235 // with two words. | 235 // with two words. |
| 236 TEST_F(SpellcheckCustomDictionaryTest, | 236 TEST_F(SpellcheckCustomDictionaryTest, |
| 237 LegacyDictionaryWithTwoWordsShouldBeConverted) { | 237 LegacyDictionaryWithTwoWordsShouldBeConverted) { |
| 238 base::FilePath path = | 238 base::FilePath path = |
| 239 profile_.GetPath().Append(chrome::kCustomDictionaryFileName); | 239 profile_.GetPath().Append(chrome::kCustomDictionaryFileName); |
| 240 | 240 |
| 241 std::string content = "foo\nbar\nfoo\n"; | 241 std::string content = "foo\nbar\nfoo\n"; |
| 242 file_util::WriteFile(path, content.c_str(), content.length()); | 242 base::WriteFile(path, content.c_str(), content.length()); |
| 243 WordList loaded_custom_words = LoadDictionaryFile(path); | 243 WordList loaded_custom_words = LoadDictionaryFile(path); |
| 244 WordList expected; | 244 WordList expected; |
| 245 expected.push_back("bar"); | 245 expected.push_back("bar"); |
| 246 expected.push_back("foo"); | 246 expected.push_back("foo"); |
| 247 EXPECT_EQ(expected, loaded_custom_words); | 247 EXPECT_EQ(expected, loaded_custom_words); |
| 248 } | 248 } |
| 249 | 249 |
| 250 // Illegal words should be removed. Leading and trailing whitespace should be | 250 // Illegal words should be removed. Leading and trailing whitespace should be |
| 251 // trimmed. | 251 // trimmed. |
| 252 TEST_F(SpellcheckCustomDictionaryTest, | 252 TEST_F(SpellcheckCustomDictionaryTest, |
| 253 IllegalWordsShouldBeRemovedFromDictionary) { | 253 IllegalWordsShouldBeRemovedFromDictionary) { |
| 254 base::FilePath path = | 254 base::FilePath path = |
| 255 profile_.GetPath().Append(chrome::kCustomDictionaryFileName); | 255 profile_.GetPath().Append(chrome::kCustomDictionaryFileName); |
| 256 | 256 |
| 257 std::string content = "foo\n foo bar \n\n \nbar\n" | 257 std::string content = "foo\n foo bar \n\n \nbar\n" |
| 258 "01234567890123456789012345678901234567890123456789" | 258 "01234567890123456789012345678901234567890123456789" |
| 259 "01234567890123456789012345678901234567890123456789"; | 259 "01234567890123456789012345678901234567890123456789"; |
| 260 file_util::WriteFile(path, content.c_str(), content.length()); | 260 base::WriteFile(path, content.c_str(), content.length()); |
| 261 WordList loaded_custom_words = LoadDictionaryFile(path); | 261 WordList loaded_custom_words = LoadDictionaryFile(path); |
| 262 WordList expected; | 262 WordList expected; |
| 263 expected.push_back("bar"); | 263 expected.push_back("bar"); |
| 264 expected.push_back("foo"); | 264 expected.push_back("foo"); |
| 265 expected.push_back("foo bar"); | 265 expected.push_back("foo bar"); |
| 266 EXPECT_EQ(expected, loaded_custom_words); | 266 EXPECT_EQ(expected, loaded_custom_words); |
| 267 } | 267 } |
| 268 | 268 |
| 269 // Write to dictionary should backup previous version and write the word to the | 269 // Write to dictionary should backup previous version and write the word to the |
| 270 // end of the dictionary. If the dictionary file is corrupted on disk, the | 270 // end of the dictionary. If the dictionary file is corrupted on disk, the |
| 271 // previous version should be reloaded. | 271 // previous version should be reloaded. |
| 272 TEST_F(SpellcheckCustomDictionaryTest, CorruptedWriteShouldBeRecovered) { | 272 TEST_F(SpellcheckCustomDictionaryTest, CorruptedWriteShouldBeRecovered) { |
| 273 base::FilePath path = | 273 base::FilePath path = |
| 274 profile_.GetPath().Append(chrome::kCustomDictionaryFileName); | 274 profile_.GetPath().Append(chrome::kCustomDictionaryFileName); |
| 275 | 275 |
| 276 std::string content = "foo\nbar"; | 276 std::string content = "foo\nbar"; |
| 277 file_util::WriteFile(path, content.c_str(), content.length()); | 277 base::WriteFile(path, content.c_str(), content.length()); |
| 278 WordList loaded_custom_words = LoadDictionaryFile(path); | 278 WordList loaded_custom_words = LoadDictionaryFile(path); |
| 279 WordList expected; | 279 WordList expected; |
| 280 expected.push_back("bar"); | 280 expected.push_back("bar"); |
| 281 expected.push_back("foo"); | 281 expected.push_back("foo"); |
| 282 EXPECT_EQ(expected, loaded_custom_words); | 282 EXPECT_EQ(expected, loaded_custom_words); |
| 283 | 283 |
| 284 SpellcheckCustomDictionary::Change change; | 284 SpellcheckCustomDictionary::Change change; |
| 285 change.AddWord("baz"); | 285 change.AddWord("baz"); |
| 286 UpdateDictionaryFile(change, path); | 286 UpdateDictionaryFile(change, path); |
| 287 content.clear(); | 287 content.clear(); |
| 288 base::ReadFileToString(path, &content); | 288 base::ReadFileToString(path, &content); |
| 289 content.append("corruption"); | 289 content.append("corruption"); |
| 290 file_util::WriteFile(path, content.c_str(), content.length()); | 290 base::WriteFile(path, content.c_str(), content.length()); |
| 291 loaded_custom_words = LoadDictionaryFile(path); | 291 loaded_custom_words = LoadDictionaryFile(path); |
| 292 EXPECT_EQ(expected, loaded_custom_words); | 292 EXPECT_EQ(expected, loaded_custom_words); |
| 293 } | 293 } |
| 294 | 294 |
| 295 TEST_F(SpellcheckCustomDictionaryTest, | 295 TEST_F(SpellcheckCustomDictionaryTest, |
| 296 GetAllSyncDataAccuratelyReflectsDictionaryState) { | 296 GetAllSyncDataAccuratelyReflectsDictionaryState) { |
| 297 SpellcheckCustomDictionary* dictionary = | 297 SpellcheckCustomDictionary* dictionary = |
| 298 SpellcheckServiceFactory::GetForContext( | 298 SpellcheckServiceFactory::GetForContext( |
| 299 &profile_)->GetCustomDictionary(); | 299 &profile_)->GetCustomDictionary(); |
| 300 | 300 |
| (...skipping 867 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1168 SpellcheckServiceFactory::GetForContext(&profile_); | 1168 SpellcheckServiceFactory::GetForContext(&profile_); |
| 1169 SpellcheckCustomDictionary* custom_dictionary = | 1169 SpellcheckCustomDictionary* custom_dictionary = |
| 1170 spellcheck_service->GetCustomDictionary(); | 1170 spellcheck_service->GetCustomDictionary(); |
| 1171 OnLoaded(*custom_dictionary, WordList()); | 1171 OnLoaded(*custom_dictionary, WordList()); |
| 1172 EXPECT_FALSE(custom_dictionary->HasWord("foo")); | 1172 EXPECT_FALSE(custom_dictionary->HasWord("foo")); |
| 1173 EXPECT_FALSE(custom_dictionary->HasWord("bar")); | 1173 EXPECT_FALSE(custom_dictionary->HasWord("bar")); |
| 1174 custom_dictionary->AddWord("foo"); | 1174 custom_dictionary->AddWord("foo"); |
| 1175 EXPECT_TRUE(custom_dictionary->HasWord("foo")); | 1175 EXPECT_TRUE(custom_dictionary->HasWord("foo")); |
| 1176 EXPECT_FALSE(custom_dictionary->HasWord("bar")); | 1176 EXPECT_FALSE(custom_dictionary->HasWord("bar")); |
| 1177 } | 1177 } |
| OLD | NEW |