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 int result = dictionary->Apply(&change); |
| 26 dictionary->Notify(&change); |
| 27 dictionary->Sync(&change); |
| 28 return !result; |
| 29 } |
| 30 |
| 31 DISALLOW_COPY_AND_ASSIGN(DictionarySyncIntegrationTestHelper); |
| 32 }; |
| 33 |
| 34 |
| 35 namespace dictionary_helper { |
| 36 namespace { |
| 37 |
| 38 bool DictionaryHasWord( |
| 39 const SpellcheckCustomDictionary* dictionary, |
| 40 const std::string& word) { |
| 41 return dictionary->GetWords().end() != std::find( |
| 42 dictionary->GetWords().begin(), |
| 43 dictionary->GetWords().end(), |
| 44 word); |
| 45 } |
| 46 |
| 47 SpellcheckCustomDictionary* GetDictionary(int index) { |
| 48 return SpellcheckServiceFactory::GetForProfile( |
| 49 sync_datatype_helper::test()->GetProfile(index))->GetCustomDictionary(); |
| 50 } |
| 51 |
| 52 SpellcheckCustomDictionary* GetVerifierDictionary() { |
| 53 return SpellcheckServiceFactory::GetForProfile( |
| 54 sync_datatype_helper::test()->verifier())->GetCustomDictionary(); |
| 55 } |
| 56 |
| 57 bool HaveWord(int index, std::string word) { |
| 58 return DictionaryHasWord(GetDictionary(index), word); |
| 59 } |
| 60 |
| 61 bool HaveWordInVerifier(std::string word) { |
| 62 return DictionaryHasWord(GetVerifierDictionary(), word); |
| 63 } |
| 64 |
| 65 void LoadDictionary(SpellcheckCustomDictionary* dictionary) { |
| 66 if (dictionary->IsLoaded()) |
| 67 return; |
| 68 base::RunLoop run_loop; |
| 69 DictionaryLoadObserver observer(content::GetQuitTaskForRunLoop(&run_loop)); |
| 70 dictionary->AddObserver(&observer); |
| 71 dictionary->Load(); |
| 72 content::RunThisRunLoop(&run_loop); |
| 73 dictionary->RemoveObserver(&observer); |
| 74 ASSERT_TRUE(dictionary->IsLoaded()); |
| 75 } |
| 76 |
| 77 bool HaveWordMatches(const std::string& word) { |
| 78 bool reference = sync_datatype_helper::test()->use_verifier() ? |
| 79 HaveWordInVerifier(word) : HaveWord(0, word); |
| 80 for (int i = 0; i < sync_datatype_helper::test()->num_clients(); ++i) { |
| 81 if (reference != HaveWord(i, word)) { |
| 82 LOG(ERROR) |
| 83 << "Word " << word << " is " << (reference ? "missing " : "present ") |
| 84 << "in profile " << i << "."; |
| 85 return false; |
| 86 } |
| 87 } |
| 88 return true; |
| 89 } |
| 90 |
| 91 } // namespace |
| 92 |
| 93 |
| 94 void EnableDictionarySync(CommandLine* cl) { |
| 95 CommandLine::ForCurrentProcess()->AppendSwitch( |
| 96 switches::kEnableSyncDictionary); |
| 97 } |
| 98 |
| 99 void LoadDictionaries() { |
| 100 for (int i = 0; i < sync_datatype_helper::test()->num_clients(); ++i) { |
| 101 LOG(ERROR) << "Loading dictionary for profile " << i; |
| 102 LoadDictionary(GetDictionary(i)); |
| 103 } |
| 104 if (sync_datatype_helper::test()->use_verifier()) { |
| 105 LOG(ERROR) << "Loading dictionary for verifier profile"; |
| 106 LoadDictionary(GetVerifierDictionary()); |
| 107 } |
| 108 } |
| 109 |
| 110 size_t GetDictionarySize(int index) { |
| 111 return GetDictionary(index)->GetWords().size(); |
| 112 } |
| 113 |
| 114 size_t GetVerifierDictionarySize() { |
| 115 return GetVerifierDictionary()->GetWords().size(); |
| 116 } |
| 117 |
| 118 bool DictionariesMatch() { |
| 119 chrome::spellcheck_common::WordList reference = |
| 120 sync_datatype_helper::test()->use_verifier() ? |
| 121 GetVerifierDictionary()->GetWords() : GetDictionary(0)->GetWords(); |
| 122 for (int i = 0; i < sync_datatype_helper::test()->num_clients(); ++i) { |
| 123 if (reference.size() != GetDictionary(i)->GetWords().size()) { |
| 124 LOG(ERROR) |
| 125 << "Dictionary in profile " << i << " has " |
| 126 << GetDictionary(i)->GetWords().size() << " words, but should have " |
| 127 << reference.size() << "."; |
| 128 return false; |
| 129 } |
| 130 } |
| 131 for (chrome::spellcheck_common::WordList::iterator it = reference.begin(); |
| 132 it != reference.end(); |
| 133 ++it) { |
| 134 if (!HaveWordMatches(*it)) |
| 135 return false; |
| 136 } |
| 137 return true; |
| 138 } |
| 139 |
| 140 bool DictionaryMatchesVerifier(int index) { |
| 141 chrome::spellcheck_common::WordList expected = |
| 142 GetVerifierDictionary()->GetWords(); |
| 143 chrome::spellcheck_common::WordList actual = GetDictionary(index)->GetWords(); |
| 144 if (expected.size() != actual.size()) |
| 145 return false; |
| 146 for (chrome::spellcheck_common::WordList::iterator it = expected.begin(); |
| 147 it != expected.end(); |
| 148 ++it) { |
| 149 if (actual.end() == std::find(actual.begin(), actual.end(), *it)) |
| 150 return false; |
| 151 } |
| 152 return true; |
| 153 } |
| 154 |
| 155 bool AddWord(int index, const std::string& word) { |
| 156 SpellcheckCustomDictionary::Change dictionary_change; |
| 157 dictionary_change.AddWord(word); |
| 158 bool result = DictionarySyncIntegrationTestHelper::ApplyChange( |
| 159 GetDictionary(index), dictionary_change); |
| 160 if (sync_datatype_helper::test()->use_verifier()) { |
| 161 result &= DictionarySyncIntegrationTestHelper::ApplyChange( |
| 162 GetVerifierDictionary(), dictionary_change); |
| 163 } |
| 164 return result; |
| 165 } |
| 166 |
| 167 bool RemoveWord(int index, const std::string& word) { |
| 168 SpellcheckCustomDictionary::Change dictionary_change; |
| 169 dictionary_change.RemoveWord(word); |
| 170 bool result = DictionarySyncIntegrationTestHelper::ApplyChange( |
| 171 GetDictionary(index), dictionary_change); |
| 172 if (sync_datatype_helper::test()->use_verifier()) { |
| 173 result &= DictionarySyncIntegrationTestHelper::ApplyChange( |
| 174 GetVerifierDictionary(), dictionary_change); |
| 175 } |
| 176 return result; |
| 177 } |
| 178 |
| 179 } // namespace dictionary_helper |
OLD | NEW |