| 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/message_loop.h" | 8 #include "base/message_loop.h" |
| 9 #include "base/metrics/histogram.h" | 9 #include "base/metrics/histogram.h" |
| 10 #include "base/metrics/histogram_samples.h" | 10 #include "base/metrics/histogram_samples.h" |
| (...skipping 13 matching lines...) Expand all Loading... |
| 24 #include "sync/api/sync_error_factory_mock.h" | 24 #include "sync/api/sync_error_factory_mock.h" |
| 25 #include "sync/protocol/sync.pb.h" | 25 #include "sync/protocol/sync.pb.h" |
| 26 #include "testing/gmock/include/gmock/gmock.h" | 26 #include "testing/gmock/include/gmock/gmock.h" |
| 27 #include "testing/gtest/include/gtest/gtest.h" | 27 #include "testing/gtest/include/gtest/gtest.h" |
| 28 | 28 |
| 29 using base::HistogramBase; | 29 using base::HistogramBase; |
| 30 using base::HistogramSamples; | 30 using base::HistogramSamples; |
| 31 using base::StatisticsRecorder; | 31 using base::StatisticsRecorder; |
| 32 using content::BrowserThread; | 32 using content::BrowserThread; |
| 33 using chrome::spellcheck_common::WordList; | 33 using chrome::spellcheck_common::WordList; |
| 34 using chrome::spellcheck_common::WordSet; |
| 34 | 35 |
| 35 namespace { | 36 namespace { |
| 36 | 37 |
| 37 // Get all sync data for the custom dictionary without limiting to maximum | 38 // Get all sync data for the custom dictionary without limiting to maximum |
| 38 // number of syncable words. | 39 // number of syncable words. |
| 39 syncer::SyncDataList GetAllSyncDataNoLimit( | 40 syncer::SyncDataList GetAllSyncDataNoLimit( |
| 40 const SpellcheckCustomDictionary* dictionary) { | 41 const SpellcheckCustomDictionary* dictionary) { |
| 41 syncer::SyncDataList data; | 42 syncer::SyncDataList data; |
| 42 std::string word; | 43 std::string word; |
| 43 for (WordList::const_iterator it = dictionary->GetWords().begin(); | 44 const WordSet& words = dictionary->GetWords(); |
| 44 it != dictionary->GetWords().end(); | 45 for (WordSet::const_iterator it = words.begin(); it != words.end(); ++it) { |
| 45 ++it) { | |
| 46 word = *it; | 46 word = *it; |
| 47 sync_pb::EntitySpecifics specifics; | 47 sync_pb::EntitySpecifics specifics; |
| 48 specifics.mutable_dictionary()->set_word(word); | 48 specifics.mutable_dictionary()->set_word(word); |
| 49 data.push_back(syncer::SyncData::CreateLocalData(word, word, specifics)); | 49 data.push_back(syncer::SyncData::CreateLocalData(word, word, specifics)); |
| 50 } | 50 } |
| 51 return data; | 51 return data; |
| 52 } | 52 } |
| 53 | 53 |
| 54 } // namespace | 54 } // namespace |
| 55 | 55 |
| (...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 218 SpellcheckCustomDictionary* custom_dictionary = | 218 SpellcheckCustomDictionary* custom_dictionary = |
| 219 spellcheck_service->GetCustomDictionary(); | 219 spellcheck_service->GetCustomDictionary(); |
| 220 TestingProfile profile2; | 220 TestingProfile profile2; |
| 221 SpellcheckService* spellcheck_service2 = | 221 SpellcheckService* spellcheck_service2 = |
| 222 static_cast<SpellcheckService*>( | 222 static_cast<SpellcheckService*>( |
| 223 SpellcheckServiceFactory::GetInstance()->SetTestingFactoryAndUse( | 223 SpellcheckServiceFactory::GetInstance()->SetTestingFactoryAndUse( |
| 224 &profile2, &BuildSpellcheckService)); | 224 &profile2, &BuildSpellcheckService)); |
| 225 SpellcheckCustomDictionary* custom_dictionary2 = | 225 SpellcheckCustomDictionary* custom_dictionary2 = |
| 226 spellcheck_service2->GetCustomDictionary(); | 226 spellcheck_service2->GetCustomDictionary(); |
| 227 | 227 |
| 228 WordList expected1; | 228 WordSet expected1; |
| 229 WordList expected2; | 229 WordSet expected2; |
| 230 | 230 |
| 231 custom_dictionary->AddWord("foo"); | 231 custom_dictionary->AddWord("foo"); |
| 232 custom_dictionary->AddWord("bar"); | 232 custom_dictionary->AddWord("bar"); |
| 233 expected1.push_back("foo"); | 233 expected1.insert("foo"); |
| 234 expected1.push_back("bar"); | 234 expected1.insert("bar"); |
| 235 | 235 |
| 236 custom_dictionary2->AddWord("hoge"); | 236 custom_dictionary2->AddWord("hoge"); |
| 237 custom_dictionary2->AddWord("fuga"); | 237 custom_dictionary2->AddWord("fuga"); |
| 238 expected2.push_back("hoge"); | 238 expected2.insert("hoge"); |
| 239 expected2.push_back("fuga"); | 239 expected2.insert("fuga"); |
| 240 | 240 |
| 241 WordList actual1 = custom_dictionary->GetWords(); | 241 WordSet actual1 = custom_dictionary->GetWords(); |
| 242 std::sort(actual1.begin(), actual1.end()); | |
| 243 std::sort(expected1.begin(), expected1.end()); | |
| 244 EXPECT_EQ(actual1, expected1); | 242 EXPECT_EQ(actual1, expected1); |
| 245 | 243 |
| 246 WordList actual2 = custom_dictionary2->GetWords(); | 244 WordSet actual2 = custom_dictionary2->GetWords(); |
| 247 std::sort(actual2.begin(), actual2.end()); | |
| 248 std::sort(expected2.begin(), expected2.end()); | |
| 249 EXPECT_EQ(actual2, expected2); | 245 EXPECT_EQ(actual2, expected2); |
| 250 } | 246 } |
| 251 | 247 |
| 252 // Legacy empty dictionary should be converted to new format empty dictionary. | 248 // Legacy empty dictionary should be converted to new format empty dictionary. |
| 253 TEST_F(SpellcheckCustomDictionaryTest, LegacyEmptyDictionaryShouldBeConverted) { | 249 TEST_F(SpellcheckCustomDictionaryTest, LegacyEmptyDictionaryShouldBeConverted) { |
| 254 base::FilePath path = | 250 base::FilePath path = |
| 255 profile_->GetPath().Append(chrome::kCustomDictionaryFileName); | 251 profile_->GetPath().Append(chrome::kCustomDictionaryFileName); |
| 256 | 252 |
| 257 std::string content; | 253 std::string content; |
| 258 file_util::WriteFile(path, content.c_str(), content.length()); | 254 file_util::WriteFile(path, content.c_str(), content.length()); |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 323 | 319 |
| 324 TEST_F(SpellcheckCustomDictionaryTest, | 320 TEST_F(SpellcheckCustomDictionaryTest, |
| 325 GetAllSyncDataAccuratelyReflectsDictionaryState) { | 321 GetAllSyncDataAccuratelyReflectsDictionaryState) { |
| 326 SpellcheckCustomDictionary* dictionary = | 322 SpellcheckCustomDictionary* dictionary = |
| 327 SpellcheckServiceFactory::GetForProfile( | 323 SpellcheckServiceFactory::GetForProfile( |
| 328 profile_.get())->GetCustomDictionary(); | 324 profile_.get())->GetCustomDictionary(); |
| 329 | 325 |
| 330 syncer::SyncDataList data = dictionary->GetAllSyncData(syncer::DICTIONARY); | 326 syncer::SyncDataList data = dictionary->GetAllSyncData(syncer::DICTIONARY); |
| 331 EXPECT_TRUE(data.empty()); | 327 EXPECT_TRUE(data.empty()); |
| 332 | 328 |
| 329 EXPECT_TRUE(dictionary->AddWord("bar")); |
| 333 EXPECT_TRUE(dictionary->AddWord("foo")); | 330 EXPECT_TRUE(dictionary->AddWord("foo")); |
| 334 EXPECT_TRUE(dictionary->AddWord("bar")); | |
| 335 | 331 |
| 336 data = dictionary->GetAllSyncData(syncer::DICTIONARY); | 332 data = dictionary->GetAllSyncData(syncer::DICTIONARY); |
| 337 EXPECT_EQ(2UL, data.size()); | 333 EXPECT_EQ(2UL, data.size()); |
| 338 std::vector<std::string> words; | 334 std::vector<std::string> words; |
| 335 words.push_back("bar"); |
| 339 words.push_back("foo"); | 336 words.push_back("foo"); |
| 340 words.push_back("bar"); | |
| 341 for (size_t i = 0; i < data.size(); i++) { | 337 for (size_t i = 0; i < data.size(); i++) { |
| 342 EXPECT_TRUE(data[i].GetSpecifics().has_dictionary()); | 338 EXPECT_TRUE(data[i].GetSpecifics().has_dictionary()); |
| 343 EXPECT_EQ(syncer::DICTIONARY, data[i].GetDataType()); | 339 EXPECT_EQ(syncer::DICTIONARY, data[i].GetDataType()); |
| 344 EXPECT_EQ(words[i], data[i].GetTag()); | 340 EXPECT_EQ(words[i], data[i].GetTag()); |
| 345 EXPECT_EQ(words[i], data[i].GetSpecifics().dictionary().word()); | 341 EXPECT_EQ(words[i], data[i].GetSpecifics().dictionary().word()); |
| 346 } | 342 } |
| 347 | 343 |
| 344 EXPECT_TRUE(dictionary->RemoveWord("bar")); |
| 348 EXPECT_TRUE(dictionary->RemoveWord("foo")); | 345 EXPECT_TRUE(dictionary->RemoveWord("foo")); |
| 349 EXPECT_TRUE(dictionary->RemoveWord("bar")); | |
| 350 | 346 |
| 351 data = dictionary->GetAllSyncData(syncer::DICTIONARY); | 347 data = dictionary->GetAllSyncData(syncer::DICTIONARY); |
| 352 EXPECT_TRUE(data.empty()); | 348 EXPECT_TRUE(data.empty()); |
| 353 } | 349 } |
| 354 | 350 |
| 355 TEST_F(SpellcheckCustomDictionaryTest, GetAllSyncDataHasLimit) { | 351 TEST_F(SpellcheckCustomDictionaryTest, GetAllSyncDataHasLimit) { |
| 356 SpellcheckCustomDictionary* dictionary = | 352 SpellcheckCustomDictionary* dictionary = |
| 357 SpellcheckServiceFactory::GetForProfile( | 353 SpellcheckServiceFactory::GetForProfile( |
| 358 profile_.get())->GetCustomDictionary(); | 354 profile_.get())->GetCustomDictionary(); |
| 359 | 355 |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 445 sync_pb::EntitySpecifics specifics; | 441 sync_pb::EntitySpecifics specifics; |
| 446 specifics.mutable_dictionary()->set_word(word); | 442 specifics.mutable_dictionary()->set_word(word); |
| 447 changes.push_back(syncer::SyncChange( | 443 changes.push_back(syncer::SyncChange( |
| 448 FROM_HERE, | 444 FROM_HERE, |
| 449 syncer::SyncChange::ACTION_DELETE, | 445 syncer::SyncChange::ACTION_DELETE, |
| 450 syncer::SyncData::CreateLocalData(word, word, specifics))); | 446 syncer::SyncData::CreateLocalData(word, word, specifics))); |
| 451 } | 447 } |
| 452 | 448 |
| 453 EXPECT_FALSE(dictionary->ProcessSyncChanges(FROM_HERE, changes).IsSet()); | 449 EXPECT_FALSE(dictionary->ProcessSyncChanges(FROM_HERE, changes).IsSet()); |
| 454 | 450 |
| 455 const chrome::spellcheck_common::WordList& words = dictionary->GetWords(); | 451 const WordSet& words = dictionary->GetWords(); |
| 456 EXPECT_EQ(2UL, words.size()); | 452 EXPECT_EQ(2UL, words.size()); |
| 457 EXPECT_EQ(words.end(), std::find(words.begin(), words.end(), "bar")); | 453 EXPECT_EQ(0UL, words.count("bar")); |
| 458 EXPECT_NE(words.end(), std::find(words.begin(), words.end(), "foo")); | 454 EXPECT_EQ(1UL, words.count("foo")); |
| 459 EXPECT_NE(words.end(), std::find(words.begin(), words.end(), "baz")); | 455 EXPECT_EQ(1UL, words.count("baz")); |
| 460 } | 456 } |
| 461 | 457 |
| 462 TEST_F(SpellcheckCustomDictionaryTest, MergeDataAndStartSyncing) { | 458 TEST_F(SpellcheckCustomDictionaryTest, MergeDataAndStartSyncing) { |
| 463 SpellcheckService* spellcheck_service = | 459 SpellcheckService* spellcheck_service = |
| 464 SpellcheckServiceFactory::GetForProfile(profile_.get()); | 460 SpellcheckServiceFactory::GetForProfile(profile_.get()); |
| 465 SpellcheckCustomDictionary* custom_dictionary = | 461 SpellcheckCustomDictionary* custom_dictionary = |
| 466 spellcheck_service->GetCustomDictionary(); | 462 spellcheck_service->GetCustomDictionary(); |
| 467 TestingProfile profile2; | 463 TestingProfile profile2; |
| 468 SpellcheckService* spellcheck_service2 = | 464 SpellcheckService* spellcheck_service2 = |
| 469 static_cast<SpellcheckService*>( | 465 static_cast<SpellcheckService*>( |
| (...skipping 22 matching lines...) Expand all Loading... |
| 492 EXPECT_FALSE(custom_dictionary->MergeDataAndStartSyncing( | 488 EXPECT_FALSE(custom_dictionary->MergeDataAndStartSyncing( |
| 493 syncer::DICTIONARY, | 489 syncer::DICTIONARY, |
| 494 custom_dictionary2->GetAllSyncData(syncer::DICTIONARY), | 490 custom_dictionary2->GetAllSyncData(syncer::DICTIONARY), |
| 495 scoped_ptr<syncer::SyncChangeProcessor>( | 491 scoped_ptr<syncer::SyncChangeProcessor>( |
| 496 new SyncChangeProcessorDelegate(custom_dictionary2)), | 492 new SyncChangeProcessorDelegate(custom_dictionary2)), |
| 497 scoped_ptr<syncer::SyncErrorFactory>( | 493 scoped_ptr<syncer::SyncErrorFactory>( |
| 498 new SyncErrorFactoryStub(&error_counter))).error().IsSet()); | 494 new SyncErrorFactoryStub(&error_counter))).error().IsSet()); |
| 499 EXPECT_EQ(0, error_counter); | 495 EXPECT_EQ(0, error_counter); |
| 500 EXPECT_TRUE(custom_dictionary->IsSyncing()); | 496 EXPECT_TRUE(custom_dictionary->IsSyncing()); |
| 501 | 497 |
| 502 WordList words = custom_dictionary->GetWords(); | 498 WordSet words = custom_dictionary->GetWords(); |
| 503 WordList words2 = custom_dictionary2->GetWords(); | 499 WordSet words2 = custom_dictionary2->GetWords(); |
| 504 EXPECT_EQ(words.size(), words2.size()); | 500 EXPECT_EQ(words.size(), words2.size()); |
| 505 | |
| 506 std::sort(words.begin(), words.end()); | |
| 507 std::sort(words2.begin(), words2.end()); | |
| 508 EXPECT_EQ(words, words2); | 501 EXPECT_EQ(words, words2); |
| 509 } | 502 } |
| 510 | 503 |
| 511 TEST_F(SpellcheckCustomDictionaryTest, DictionaryTooBigBeforeSyncing) { | 504 TEST_F(SpellcheckCustomDictionaryTest, DictionaryTooBigBeforeSyncing) { |
| 512 SpellcheckService* spellcheck_service = | 505 SpellcheckService* spellcheck_service = |
| 513 SpellcheckServiceFactory::GetForProfile(profile_.get()); | 506 SpellcheckServiceFactory::GetForProfile(profile_.get()); |
| 514 SpellcheckCustomDictionary* custom_dictionary = | 507 SpellcheckCustomDictionary* custom_dictionary = |
| 515 spellcheck_service->GetCustomDictionary(); | 508 spellcheck_service->GetCustomDictionary(); |
| 516 TestingProfile profile2; | 509 TestingProfile profile2; |
| 517 SpellcheckService* spellcheck_service2 = | 510 SpellcheckService* spellcheck_service2 = |
| (...skipping 601 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1119 EXPECT_EQ(2u, loaded_custom_words.size()); | 1112 EXPECT_EQ(2u, loaded_custom_words.size()); |
| 1120 | 1113 |
| 1121 histogram = | 1114 histogram = |
| 1122 StatisticsRecorder::FindHistogram("SpellCheck.CustomWords"); | 1115 StatisticsRecorder::FindHistogram("SpellCheck.CustomWords"); |
| 1123 ASSERT_TRUE(histogram != NULL); | 1116 ASSERT_TRUE(histogram != NULL); |
| 1124 scoped_ptr<HistogramSamples> samples2 = histogram->SnapshotSamples(); | 1117 scoped_ptr<HistogramSamples> samples2 = histogram->SnapshotSamples(); |
| 1125 | 1118 |
| 1126 samples2->Subtract(*baseline); | 1119 samples2->Subtract(*baseline); |
| 1127 EXPECT_EQ(2,samples2->sum()); | 1120 EXPECT_EQ(2,samples2->sum()); |
| 1128 } | 1121 } |
| 1122 |
| 1123 TEST_F(SpellcheckCustomDictionaryTest, HasWord) { |
| 1124 SpellcheckService* spellcheck_service = |
| 1125 SpellcheckServiceFactory::GetForProfile(profile_.get()); |
| 1126 SpellcheckCustomDictionary* custom_dictionary = |
| 1127 spellcheck_service->GetCustomDictionary(); |
| 1128 OnLoaded(*custom_dictionary, WordList()); |
| 1129 EXPECT_FALSE(custom_dictionary->HasWord("foo")); |
| 1130 EXPECT_FALSE(custom_dictionary->HasWord("bar")); |
| 1131 custom_dictionary->AddWord("foo"); |
| 1132 EXPECT_TRUE(custom_dictionary->HasWord("foo")); |
| 1133 EXPECT_FALSE(custom_dictionary->HasWord("bar")); |
| 1134 } |
| OLD | NEW |