| 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 "chrome/browser/spellchecker/spellcheck_custom_dictionary.h" | 5 #include "chrome/browser/spellchecker/spellcheck_custom_dictionary.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 #include <utility> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "base/files/file_util.h" | 11 #include "base/files/file_util.h" |
| 12 #include "base/macros.h" | 12 #include "base/macros.h" |
| 13 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
| 14 #include "base/metrics/histogram_samples.h" | 14 #include "base/metrics/histogram_samples.h" |
| 15 #include "base/metrics/statistics_recorder.h" | 15 #include "base/metrics/statistics_recorder.h" |
| 16 #include "base/strings/string_number_conversions.h" | 16 #include "base/strings/string_number_conversions.h" |
| 17 #include "build/build_config.h" | 17 #include "build/build_config.h" |
| 18 #include "chrome/browser/spellchecker/spellcheck_factory.h" | 18 #include "chrome/browser/spellchecker/spellcheck_factory.h" |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 80 const base::FilePath& path) { | 80 const base::FilePath& path) { |
| 81 return SpellcheckCustomDictionary::LoadDictionaryFile(path); | 81 return SpellcheckCustomDictionary::LoadDictionaryFile(path); |
| 82 } | 82 } |
| 83 | 83 |
| 84 // A wrapper around SpellcheckCustomDictionary::UpdateDictionaryFile private | 84 // A wrapper around SpellcheckCustomDictionary::UpdateDictionaryFile private |
| 85 // function to avoid a large number of FRIEND_TEST declarations in | 85 // function to avoid a large number of FRIEND_TEST declarations in |
| 86 // SpellcheckCustomDictionary. | 86 // SpellcheckCustomDictionary. |
| 87 void UpdateDictionaryFile( | 87 void UpdateDictionaryFile( |
| 88 scoped_ptr<SpellcheckCustomDictionary::Change> dictionary_change, | 88 scoped_ptr<SpellcheckCustomDictionary::Change> dictionary_change, |
| 89 const base::FilePath& path) { | 89 const base::FilePath& path) { |
| 90 SpellcheckCustomDictionary::UpdateDictionaryFile(dictionary_change.Pass(), | 90 SpellcheckCustomDictionary::UpdateDictionaryFile( |
| 91 path); | 91 std::move(dictionary_change), path); |
| 92 } | 92 } |
| 93 | 93 |
| 94 // A wrapper around SpellcheckCustomDictionary::OnLoaded private method to | 94 // A wrapper around SpellcheckCustomDictionary::OnLoaded private method to |
| 95 // avoid a large number of FRIEND_TEST declarations in | 95 // avoid a large number of FRIEND_TEST declarations in |
| 96 // SpellcheckCustomDictionary. | 96 // SpellcheckCustomDictionary. |
| 97 void OnLoaded(SpellcheckCustomDictionary& dictionary, | 97 void OnLoaded(SpellcheckCustomDictionary& dictionary, |
| 98 scoped_ptr<std::set<std::string>> words) { | 98 scoped_ptr<std::set<std::string>> words) { |
| 99 scoped_ptr<SpellcheckCustomDictionary::LoadFileResult> result( | 99 scoped_ptr<SpellcheckCustomDictionary::LoadFileResult> result( |
| 100 new SpellcheckCustomDictionary::LoadFileResult); | 100 new SpellcheckCustomDictionary::LoadFileResult); |
| 101 result->is_valid_file = true; | 101 result->is_valid_file = true; |
| 102 result->words = *words; | 102 result->words = *words; |
| 103 dictionary.OnLoaded(result.Pass()); | 103 dictionary.OnLoaded(std::move(result)); |
| 104 } | 104 } |
| 105 | 105 |
| 106 // A wrapper around SpellcheckCustomDictionary::Apply private method to avoid | 106 // A wrapper around SpellcheckCustomDictionary::Apply private method to avoid |
| 107 // a large number of FRIEND_TEST declarations in SpellcheckCustomDictionary. | 107 // a large number of FRIEND_TEST declarations in SpellcheckCustomDictionary. |
| 108 void Apply( | 108 void Apply( |
| 109 SpellcheckCustomDictionary& dictionary, | 109 SpellcheckCustomDictionary& dictionary, |
| 110 const SpellcheckCustomDictionary::Change& change) { | 110 const SpellcheckCustomDictionary::Change& change) { |
| 111 return dictionary.Apply(change); | 111 return dictionary.Apply(change); |
| 112 } | 112 } |
| 113 | 113 |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 169 profile_.GetPath().Append(chrome::kCustomDictionaryFileName); | 169 profile_.GetPath().Append(chrome::kCustomDictionaryFileName); |
| 170 | 170 |
| 171 // The custom word list should be empty now. | 171 // The custom word list should be empty now. |
| 172 EXPECT_TRUE(LoadDictionaryFile(path)->words.empty()); | 172 EXPECT_TRUE(LoadDictionaryFile(path)->words.empty()); |
| 173 | 173 |
| 174 scoped_ptr<SpellcheckCustomDictionary::Change> change( | 174 scoped_ptr<SpellcheckCustomDictionary::Change> change( |
| 175 new SpellcheckCustomDictionary::Change); | 175 new SpellcheckCustomDictionary::Change); |
| 176 change->AddWord("bar"); | 176 change->AddWord("bar"); |
| 177 change->AddWord("foo"); | 177 change->AddWord("foo"); |
| 178 | 178 |
| 179 UpdateDictionaryFile(change.Pass(), path); | 179 UpdateDictionaryFile(std::move(change), path); |
| 180 std::set<std::string> expected; | 180 std::set<std::string> expected; |
| 181 expected.insert("bar"); | 181 expected.insert("bar"); |
| 182 expected.insert("foo"); | 182 expected.insert("foo"); |
| 183 | 183 |
| 184 // The custom word list should include written words. | 184 // The custom word list should include written words. |
| 185 EXPECT_EQ(expected, LoadDictionaryFile(path)->words); | 185 EXPECT_EQ(expected, LoadDictionaryFile(path)->words); |
| 186 | 186 |
| 187 scoped_ptr<SpellcheckCustomDictionary::Change> change2( | 187 scoped_ptr<SpellcheckCustomDictionary::Change> change2( |
| 188 new SpellcheckCustomDictionary::Change); | 188 new SpellcheckCustomDictionary::Change); |
| 189 change2->RemoveWord("bar"); | 189 change2->RemoveWord("bar"); |
| 190 change2->RemoveWord("foo"); | 190 change2->RemoveWord("foo"); |
| 191 UpdateDictionaryFile(change2.Pass(), path); | 191 UpdateDictionaryFile(std::move(change2), path); |
| 192 EXPECT_TRUE(LoadDictionaryFile(path)->words.empty()); | 192 EXPECT_TRUE(LoadDictionaryFile(path)->words.empty()); |
| 193 } | 193 } |
| 194 | 194 |
| 195 TEST_F(SpellcheckCustomDictionaryTest, MultiProfile) { | 195 TEST_F(SpellcheckCustomDictionaryTest, MultiProfile) { |
| 196 SpellcheckService* spellcheck_service = | 196 SpellcheckService* spellcheck_service = |
| 197 SpellcheckServiceFactory::GetForContext(&profile_); | 197 SpellcheckServiceFactory::GetForContext(&profile_); |
| 198 SpellcheckCustomDictionary* custom_dictionary = | 198 SpellcheckCustomDictionary* custom_dictionary = |
| 199 spellcheck_service->GetCustomDictionary(); | 199 spellcheck_service->GetCustomDictionary(); |
| 200 TestingProfile profile2; | 200 TestingProfile profile2; |
| 201 SpellcheckService* spellcheck_service2 = | 201 SpellcheckService* spellcheck_service2 = |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 278 std::string content = "foo\nbar"; | 278 std::string content = "foo\nbar"; |
| 279 base::WriteFile(path, content.c_str(), content.length()); | 279 base::WriteFile(path, content.c_str(), content.length()); |
| 280 std::set<std::string> expected; | 280 std::set<std::string> expected; |
| 281 expected.insert("bar"); | 281 expected.insert("bar"); |
| 282 expected.insert("foo"); | 282 expected.insert("foo"); |
| 283 EXPECT_EQ(expected, LoadDictionaryFile(path)->words); | 283 EXPECT_EQ(expected, LoadDictionaryFile(path)->words); |
| 284 | 284 |
| 285 scoped_ptr<SpellcheckCustomDictionary::Change> change( | 285 scoped_ptr<SpellcheckCustomDictionary::Change> change( |
| 286 new SpellcheckCustomDictionary::Change); | 286 new SpellcheckCustomDictionary::Change); |
| 287 change->AddWord("baz"); | 287 change->AddWord("baz"); |
| 288 UpdateDictionaryFile(change.Pass(), path); | 288 UpdateDictionaryFile(std::move(change), path); |
| 289 content.clear(); | 289 content.clear(); |
| 290 base::ReadFileToString(path, &content); | 290 base::ReadFileToString(path, &content); |
| 291 content.append("corruption"); | 291 content.append("corruption"); |
| 292 base::WriteFile(path, content.c_str(), content.length()); | 292 base::WriteFile(path, content.c_str(), content.length()); |
| 293 EXPECT_EQ(expected, LoadDictionaryFile(path)->words); | 293 EXPECT_EQ(expected, LoadDictionaryFile(path)->words); |
| 294 } | 294 } |
| 295 | 295 |
| 296 TEST_F(SpellcheckCustomDictionaryTest, | 296 TEST_F(SpellcheckCustomDictionaryTest, |
| 297 GetAllSyncDataAccuratelyReflectsDictionaryState) { | 297 GetAllSyncDataAccuratelyReflectsDictionaryState) { |
| 298 SpellcheckCustomDictionary* dictionary = | 298 SpellcheckCustomDictionary* dictionary = |
| (...skipping 194 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 493 &profile2, &BuildSpellcheckService)) | 493 &profile2, &BuildSpellcheckService)) |
| 494 ->GetCustomDictionary(); | 494 ->GetCustomDictionary(); |
| 495 | 495 |
| 496 scoped_ptr<SpellcheckCustomDictionary::Change> change( | 496 scoped_ptr<SpellcheckCustomDictionary::Change> change( |
| 497 new SpellcheckCustomDictionary::Change); | 497 new SpellcheckCustomDictionary::Change); |
| 498 change->AddWord("foo"); | 498 change->AddWord("foo"); |
| 499 Apply(*custom_dictionary2, *change); | 499 Apply(*custom_dictionary2, *change); |
| 500 | 500 |
| 501 base::FilePath path = | 501 base::FilePath path = |
| 502 profile_.GetPath().Append(chrome::kCustomDictionaryFileName); | 502 profile_.GetPath().Append(chrome::kCustomDictionaryFileName); |
| 503 UpdateDictionaryFile(change.Pass(), path); | 503 UpdateDictionaryFile(std::move(change), path); |
| 504 EXPECT_TRUE(custom_dictionary->GetWords().empty()); | 504 EXPECT_TRUE(custom_dictionary->GetWords().empty()); |
| 505 | 505 |
| 506 int error_counter = 0; | 506 int error_counter = 0; |
| 507 EXPECT_FALSE( | 507 EXPECT_FALSE( |
| 508 custom_dictionary->MergeDataAndStartSyncing( | 508 custom_dictionary->MergeDataAndStartSyncing( |
| 509 syncer::DICTIONARY, | 509 syncer::DICTIONARY, |
| 510 custom_dictionary2->GetAllSyncData( | 510 custom_dictionary2->GetAllSyncData( |
| 511 syncer::DICTIONARY), | 511 syncer::DICTIONARY), |
| 512 scoped_ptr<syncer::SyncChangeProcessor>( | 512 scoped_ptr<syncer::SyncChangeProcessor>( |
| 513 new syncer::SyncChangeProcessorWrapperForTest( | 513 new syncer::SyncChangeProcessorWrapperForTest( |
| (...skipping 313 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 827 custom_dictionary2)), | 827 custom_dictionary2)), |
| 828 scoped_ptr<syncer::SyncErrorFactory>( | 828 scoped_ptr<syncer::SyncErrorFactory>( |
| 829 new SyncErrorFactoryStub(&error_counter))) | 829 new SyncErrorFactoryStub(&error_counter))) |
| 830 .error() | 830 .error() |
| 831 .IsSet()); | 831 .IsSet()); |
| 832 EXPECT_EQ(0, error_counter); | 832 EXPECT_EQ(0, error_counter); |
| 833 EXPECT_TRUE(custom_dictionary->IsSyncing()); | 833 EXPECT_TRUE(custom_dictionary->IsSyncing()); |
| 834 | 834 |
| 835 scoped_ptr<std::set<std::string>> custom_words(new std::set<std::string>); | 835 scoped_ptr<std::set<std::string>> custom_words(new std::set<std::string>); |
| 836 custom_words->insert("bar"); | 836 custom_words->insert("bar"); |
| 837 OnLoaded(*custom_dictionary, custom_words.Pass()); | 837 OnLoaded(*custom_dictionary, std::move(custom_words)); |
| 838 EXPECT_TRUE(custom_dictionary->IsSyncing()); | 838 EXPECT_TRUE(custom_dictionary->IsSyncing()); |
| 839 | 839 |
| 840 EXPECT_EQ(2UL, custom_dictionary->GetWords().size()); | 840 EXPECT_EQ(2UL, custom_dictionary->GetWords().size()); |
| 841 EXPECT_EQ(2UL, custom_dictionary2->GetWords().size()); | 841 EXPECT_EQ(2UL, custom_dictionary2->GetWords().size()); |
| 842 | 842 |
| 843 EXPECT_EQ(2UL, custom_dictionary->GetAllSyncData(syncer::DICTIONARY).size()); | 843 EXPECT_EQ(2UL, custom_dictionary->GetAllSyncData(syncer::DICTIONARY).size()); |
| 844 EXPECT_EQ(2UL, custom_dictionary2->GetAllSyncData(syncer::DICTIONARY).size()); | 844 EXPECT_EQ(2UL, custom_dictionary2->GetAllSyncData(syncer::DICTIONARY).size()); |
| 845 } | 845 } |
| 846 | 846 |
| 847 TEST_F(SpellcheckCustomDictionaryTest, LoadAfterSyncStartTooBigToSync) { | 847 TEST_F(SpellcheckCustomDictionaryTest, LoadAfterSyncStartTooBigToSync) { |
| (...skipping 26 matching lines...) Expand all Loading... |
| 874 .IsSet()); | 874 .IsSet()); |
| 875 EXPECT_EQ(0, error_counter); | 875 EXPECT_EQ(0, error_counter); |
| 876 EXPECT_TRUE(custom_dictionary->IsSyncing()); | 876 EXPECT_TRUE(custom_dictionary->IsSyncing()); |
| 877 | 877 |
| 878 scoped_ptr<std::set<std::string>> custom_words(new std::set<std::string>); | 878 scoped_ptr<std::set<std::string>> custom_words(new std::set<std::string>); |
| 879 for (size_t i = 0; | 879 for (size_t i = 0; |
| 880 i < chrome::spellcheck_common::MAX_SYNCABLE_DICTIONARY_WORDS; | 880 i < chrome::spellcheck_common::MAX_SYNCABLE_DICTIONARY_WORDS; |
| 881 ++i) { | 881 ++i) { |
| 882 custom_words->insert(custom_words->end(), "foo" + base::Uint64ToString(i)); | 882 custom_words->insert(custom_words->end(), "foo" + base::Uint64ToString(i)); |
| 883 } | 883 } |
| 884 OnLoaded(*custom_dictionary, custom_words.Pass()); | 884 OnLoaded(*custom_dictionary, std::move(custom_words)); |
| 885 EXPECT_EQ(0, error_counter); | 885 EXPECT_EQ(0, error_counter); |
| 886 EXPECT_FALSE(custom_dictionary->IsSyncing()); | 886 EXPECT_FALSE(custom_dictionary->IsSyncing()); |
| 887 | 887 |
| 888 EXPECT_EQ(chrome::spellcheck_common::MAX_SYNCABLE_DICTIONARY_WORDS + 1, | 888 EXPECT_EQ(chrome::spellcheck_common::MAX_SYNCABLE_DICTIONARY_WORDS + 1, |
| 889 custom_dictionary->GetWords().size()); | 889 custom_dictionary->GetWords().size()); |
| 890 EXPECT_EQ(chrome::spellcheck_common::MAX_SYNCABLE_DICTIONARY_WORDS, | 890 EXPECT_EQ(chrome::spellcheck_common::MAX_SYNCABLE_DICTIONARY_WORDS, |
| 891 custom_dictionary2->GetWords().size()); | 891 custom_dictionary2->GetWords().size()); |
| 892 | 892 |
| 893 EXPECT_EQ(chrome::spellcheck_common::MAX_SYNCABLE_DICTIONARY_WORDS, | 893 EXPECT_EQ(chrome::spellcheck_common::MAX_SYNCABLE_DICTIONARY_WORDS, |
| 894 custom_dictionary->GetAllSyncData(syncer::DICTIONARY).size()); | 894 custom_dictionary->GetAllSyncData(syncer::DICTIONARY).size()); |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 954 SpellcheckServiceFactory::GetForContext(&profile_); | 954 SpellcheckServiceFactory::GetForContext(&profile_); |
| 955 SpellcheckCustomDictionary* custom_dictionary = | 955 SpellcheckCustomDictionary* custom_dictionary = |
| 956 spellcheck_service->GetCustomDictionary(); | 956 spellcheck_service->GetCustomDictionary(); |
| 957 | 957 |
| 958 DictionaryObserverCounter observer; | 958 DictionaryObserverCounter observer; |
| 959 custom_dictionary->AddObserver(&observer); | 959 custom_dictionary->AddObserver(&observer); |
| 960 | 960 |
| 961 scoped_ptr<std::set<std::string>> custom_words(new std::set<std::string>); | 961 scoped_ptr<std::set<std::string>> custom_words(new std::set<std::string>); |
| 962 custom_words->insert("foo"); | 962 custom_words->insert("foo"); |
| 963 custom_words->insert("bar"); | 963 custom_words->insert("bar"); |
| 964 OnLoaded(*custom_dictionary, custom_words.Pass()); | 964 OnLoaded(*custom_dictionary, std::move(custom_words)); |
| 965 | 965 |
| 966 EXPECT_GE(observer.loads(), 1); | 966 EXPECT_GE(observer.loads(), 1); |
| 967 EXPECT_LE(observer.loads(), 2); | 967 EXPECT_LE(observer.loads(), 2); |
| 968 EXPECT_EQ(0, observer.changes()); | 968 EXPECT_EQ(0, observer.changes()); |
| 969 | 969 |
| 970 custom_dictionary->RemoveObserver(&observer); | 970 custom_dictionary->RemoveObserver(&observer); |
| 971 } | 971 } |
| 972 | 972 |
| 973 TEST_F(SpellcheckCustomDictionaryTest, DictionaryAddWordNotification) { | 973 TEST_F(SpellcheckCustomDictionaryTest, DictionaryAddWordNotification) { |
| 974 SpellcheckService* spellcheck_service = | 974 SpellcheckService* spellcheck_service = |
| (...skipping 220 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1195 ASSERT_TRUE(histogram != NULL); | 1195 ASSERT_TRUE(histogram != NULL); |
| 1196 scoped_ptr<HistogramSamples> samples = histogram->SnapshotSamples(); | 1196 scoped_ptr<HistogramSamples> samples = histogram->SnapshotSamples(); |
| 1197 | 1197 |
| 1198 samples->Subtract(*baseline); | 1198 samples->Subtract(*baseline); |
| 1199 EXPECT_EQ(0,samples->sum()); | 1199 EXPECT_EQ(0,samples->sum()); |
| 1200 | 1200 |
| 1201 scoped_ptr<SpellcheckCustomDictionary::Change> change( | 1201 scoped_ptr<SpellcheckCustomDictionary::Change> change( |
| 1202 new SpellcheckCustomDictionary::Change); | 1202 new SpellcheckCustomDictionary::Change); |
| 1203 change->AddWord("bar"); | 1203 change->AddWord("bar"); |
| 1204 change->AddWord("foo"); | 1204 change->AddWord("foo"); |
| 1205 UpdateDictionaryFile(change.Pass(), path); | 1205 UpdateDictionaryFile(std::move(change), path); |
| 1206 | 1206 |
| 1207 // Load the dictionary again and it should have 2 entries. | 1207 // Load the dictionary again and it should have 2 entries. |
| 1208 EXPECT_EQ(2u, LoadDictionaryFile(path)->words.size()); | 1208 EXPECT_EQ(2u, LoadDictionaryFile(path)->words.size()); |
| 1209 | 1209 |
| 1210 histogram = | 1210 histogram = |
| 1211 StatisticsRecorder::FindHistogram("SpellCheck.CustomWords"); | 1211 StatisticsRecorder::FindHistogram("SpellCheck.CustomWords"); |
| 1212 ASSERT_TRUE(histogram != NULL); | 1212 ASSERT_TRUE(histogram != NULL); |
| 1213 scoped_ptr<HistogramSamples> samples2 = histogram->SnapshotSamples(); | 1213 scoped_ptr<HistogramSamples> samples2 = histogram->SnapshotSamples(); |
| 1214 | 1214 |
| 1215 samples2->Subtract(*baseline); | 1215 samples2->Subtract(*baseline); |
| 1216 EXPECT_EQ(2,samples2->sum()); | 1216 EXPECT_EQ(2,samples2->sum()); |
| 1217 } | 1217 } |
| 1218 | 1218 |
| 1219 TEST_F(SpellcheckCustomDictionaryTest, HasWord) { | 1219 TEST_F(SpellcheckCustomDictionaryTest, HasWord) { |
| 1220 SpellcheckService* spellcheck_service = | 1220 SpellcheckService* spellcheck_service = |
| 1221 SpellcheckServiceFactory::GetForContext(&profile_); | 1221 SpellcheckServiceFactory::GetForContext(&profile_); |
| 1222 SpellcheckCustomDictionary* custom_dictionary = | 1222 SpellcheckCustomDictionary* custom_dictionary = |
| 1223 spellcheck_service->GetCustomDictionary(); | 1223 spellcheck_service->GetCustomDictionary(); |
| 1224 OnLoaded(*custom_dictionary, make_scoped_ptr(new std::set<std::string>)); | 1224 OnLoaded(*custom_dictionary, make_scoped_ptr(new std::set<std::string>)); |
| 1225 EXPECT_FALSE(custom_dictionary->HasWord("foo")); | 1225 EXPECT_FALSE(custom_dictionary->HasWord("foo")); |
| 1226 EXPECT_FALSE(custom_dictionary->HasWord("bar")); | 1226 EXPECT_FALSE(custom_dictionary->HasWord("bar")); |
| 1227 custom_dictionary->AddWord("foo"); | 1227 custom_dictionary->AddWord("foo"); |
| 1228 EXPECT_TRUE(custom_dictionary->HasWord("foo")); | 1228 EXPECT_TRUE(custom_dictionary->HasWord("foo")); |
| 1229 EXPECT_FALSE(custom_dictionary->HasWord("bar")); | 1229 EXPECT_FALSE(custom_dictionary->HasWord("bar")); |
| 1230 } | 1230 } |
| OLD | NEW |