OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/sync/test/integration/dictionary_helper.h" |
| 6 |
| 7 #include "chrome/browser/profiles/profile.h" |
| 8 #include "chrome/browser/spellchecker/spellcheck_factory.h" |
| 9 #include "chrome/browser/spellchecker/spellcheck_service.h" |
| 10 #include "chrome/browser/sync/profile_sync_service_harness.h" |
| 11 #include "chrome/browser/sync/test/integration/dictionary_load_observer.h" |
| 12 #include "chrome/browser/sync/test/integration/sync_datatype_helper.h" |
| 13 #include "chrome/browser/sync/test/integration/sync_test.h" |
| 14 #include "chrome/common/chrome_switches.h" |
| 15 #include "chrome/common/spellcheck_common.h" |
| 16 #include "content/public/test/test_utils.h" |
| 17 |
| 18 class DictionarySyncIntegrationTestHelper { |
| 19 public: |
| 20 // Same as SpellcheckCustomDictionary::AddWord/RemoveWord, except does not |
| 21 // write to disk. |
| 22 static bool ApplyChange( |
| 23 SpellcheckCustomDictionary* dictionary, |
| 24 SpellcheckCustomDictionary::Change& change) { |
| 25 std::sort(dictionary->words_.begin(), dictionary->words_.end()); |
| 26 int result = change.Sanitize(dictionary->GetWords()); |
| 27 dictionary->Apply(change); |
| 28 dictionary->Notify(change); |
| 29 dictionary->Sync(change); |
| 30 return !result; |
| 31 } |
| 32 |
| 33 DISALLOW_COPY_AND_ASSIGN(DictionarySyncIntegrationTestHelper); |
| 34 }; |
| 35 |
| 36 |
| 37 namespace dictionary_helper { |
| 38 namespace { |
| 39 |
| 40 bool DictionaryHasWord( |
| 41 const SpellcheckCustomDictionary* dictionary, |
| 42 const std::string& word) { |
| 43 return dictionary->GetWords().end() != std::find( |
| 44 dictionary->GetWords().begin(), |
| 45 dictionary->GetWords().end(), |
| 46 word); |
| 47 } |
| 48 |
| 49 SpellcheckCustomDictionary* GetDictionary(int index) { |
| 50 return SpellcheckServiceFactory::GetForProfile( |
| 51 sync_datatype_helper::test()->GetProfile(index))->GetCustomDictionary(); |
| 52 } |
| 53 |
| 54 SpellcheckCustomDictionary* GetVerifierDictionary() { |
| 55 return SpellcheckServiceFactory::GetForProfile( |
| 56 sync_datatype_helper::test()->verifier())->GetCustomDictionary(); |
| 57 } |
| 58 |
| 59 bool HaveWord(int index, std::string word) { |
| 60 return DictionaryHasWord(GetDictionary(index), word); |
| 61 } |
| 62 |
| 63 bool HaveWordInVerifier(std::string word) { |
| 64 return DictionaryHasWord(GetVerifierDictionary(), word); |
| 65 } |
| 66 |
| 67 void LoadDictionary(SpellcheckCustomDictionary* dictionary) { |
| 68 if (dictionary->IsLoaded()) |
| 69 return; |
| 70 base::RunLoop run_loop; |
| 71 DictionaryLoadObserver observer(content::GetQuitTaskForRunLoop(&run_loop)); |
| 72 dictionary->AddObserver(&observer); |
| 73 dictionary->Load(); |
| 74 content::RunThisRunLoop(&run_loop); |
| 75 dictionary->RemoveObserver(&observer); |
| 76 ASSERT_TRUE(dictionary->IsLoaded()); |
| 77 } |
| 78 |
| 79 bool HaveWordMatches(const std::string& word) { |
| 80 bool reference = sync_datatype_helper::test()->use_verifier() ? |
| 81 HaveWordInVerifier(word) : HaveWord(0, word); |
| 82 for (int i = 0; i < sync_datatype_helper::test()->num_clients(); ++i) { |
| 83 if (reference != HaveWord(i, word)) { |
| 84 return false; |
| 85 } |
| 86 } |
| 87 return true; |
| 88 } |
| 89 |
| 90 } // namespace |
| 91 |
| 92 |
| 93 void EnableDictionarySync(CommandLine* cl) { |
| 94 CommandLine::ForCurrentProcess()->AppendSwitch( |
| 95 switches::kEnableSyncDictionary); |
| 96 } |
| 97 |
| 98 void LoadDictionaries() { |
| 99 for (int i = 0; i < sync_datatype_helper::test()->num_clients(); ++i) |
| 100 LoadDictionary(GetDictionary(i)); |
| 101 if (sync_datatype_helper::test()->use_verifier()) |
| 102 LoadDictionary(GetVerifierDictionary()); |
| 103 } |
| 104 |
| 105 size_t GetDictionarySize(int index) { |
| 106 return GetDictionary(index)->GetWords().size(); |
| 107 } |
| 108 |
| 109 size_t GetVerifierDictionarySize() { |
| 110 return GetVerifierDictionary()->GetWords().size(); |
| 111 } |
| 112 |
| 113 bool DictionariesMatch() { |
| 114 chrome::spellcheck_common::WordList reference = |
| 115 sync_datatype_helper::test()->use_verifier() ? |
| 116 GetVerifierDictionary()->GetWords() : GetDictionary(0)->GetWords(); |
| 117 for (int i = 0; i < sync_datatype_helper::test()->num_clients(); ++i) { |
| 118 if (reference.size() != GetDictionary(i)->GetWords().size()) |
| 119 return false; |
| 120 } |
| 121 for (chrome::spellcheck_common::WordList::iterator it = reference.begin(); |
| 122 it != reference.end(); |
| 123 ++it) { |
| 124 if (!HaveWordMatches(*it)) |
| 125 return false; |
| 126 } |
| 127 return true; |
| 128 } |
| 129 |
| 130 bool DictionaryMatchesVerifier(int index) { |
| 131 chrome::spellcheck_common::WordList expected = |
| 132 GetVerifierDictionary()->GetWords(); |
| 133 chrome::spellcheck_common::WordList actual = GetDictionary(index)->GetWords(); |
| 134 if (expected.size() != actual.size()) |
| 135 return false; |
| 136 for (chrome::spellcheck_common::WordList::iterator it = expected.begin(); |
| 137 it != expected.end(); |
| 138 ++it) { |
| 139 if (actual.end() == std::find(actual.begin(), actual.end(), *it)) |
| 140 return false; |
| 141 } |
| 142 return true; |
| 143 } |
| 144 |
| 145 bool AddWord(int index, const std::string& word) { |
| 146 SpellcheckCustomDictionary::Change dictionary_change; |
| 147 dictionary_change.AddWord(word); |
| 148 bool result = DictionarySyncIntegrationTestHelper::ApplyChange( |
| 149 GetDictionary(index), dictionary_change); |
| 150 if (sync_datatype_helper::test()->use_verifier()) { |
| 151 result &= DictionarySyncIntegrationTestHelper::ApplyChange( |
| 152 GetVerifierDictionary(), dictionary_change); |
| 153 } |
| 154 return result; |
| 155 } |
| 156 |
| 157 bool RemoveWord(int index, const std::string& word) { |
| 158 SpellcheckCustomDictionary::Change dictionary_change; |
| 159 dictionary_change.RemoveWord(word); |
| 160 bool result = DictionarySyncIntegrationTestHelper::ApplyChange( |
| 161 GetDictionary(index), dictionary_change); |
| 162 if (sync_datatype_helper::test()->use_verifier()) { |
| 163 result &= DictionarySyncIntegrationTestHelper::ApplyChange( |
| 164 GetVerifierDictionary(), dictionary_change); |
| 165 } |
| 166 return result; |
| 167 } |
| 168 |
| 169 } // namespace dictionary_helper |
OLD | NEW |