dictionary_helper.cc revision 2a99a7e74a7f215066514fe81d2bfa6639d9eddd
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 18class 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 37namespace dictionary_helper { 38namespace { 39 40bool 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 49SpellcheckCustomDictionary* GetDictionary(int index) { 50 return SpellcheckServiceFactory::GetForProfile( 51 sync_datatype_helper::test()->GetProfile(index))->GetCustomDictionary(); 52} 53 54SpellcheckCustomDictionary* GetVerifierDictionary() { 55 return SpellcheckServiceFactory::GetForProfile( 56 sync_datatype_helper::test()->verifier())->GetCustomDictionary(); 57} 58 59bool HaveWord(int index, std::string word) { 60 return DictionaryHasWord(GetDictionary(index), word); 61} 62 63bool HaveWordInVerifier(std::string word) { 64 return DictionaryHasWord(GetVerifierDictionary(), word); 65} 66 67void 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 79bool 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 93void LoadDictionaries() { 94 for (int i = 0; i < sync_datatype_helper::test()->num_clients(); ++i) 95 LoadDictionary(GetDictionary(i)); 96 if (sync_datatype_helper::test()->use_verifier()) 97 LoadDictionary(GetVerifierDictionary()); 98} 99 100size_t GetDictionarySize(int index) { 101 return GetDictionary(index)->GetWords().size(); 102} 103 104size_t GetVerifierDictionarySize() { 105 return GetVerifierDictionary()->GetWords().size(); 106} 107 108bool DictionariesMatch() { 109 chrome::spellcheck_common::WordList reference = 110 sync_datatype_helper::test()->use_verifier() ? 111 GetVerifierDictionary()->GetWords() : GetDictionary(0)->GetWords(); 112 for (int i = 0; i < sync_datatype_helper::test()->num_clients(); ++i) { 113 if (reference.size() != GetDictionary(i)->GetWords().size()) 114 return false; 115 } 116 for (chrome::spellcheck_common::WordList::iterator it = reference.begin(); 117 it != reference.end(); 118 ++it) { 119 if (!HaveWordMatches(*it)) 120 return false; 121 } 122 return true; 123} 124 125bool DictionaryMatchesVerifier(int index) { 126 chrome::spellcheck_common::WordList expected = 127 GetVerifierDictionary()->GetWords(); 128 chrome::spellcheck_common::WordList actual = GetDictionary(index)->GetWords(); 129 if (expected.size() != actual.size()) 130 return false; 131 for (chrome::spellcheck_common::WordList::iterator it = expected.begin(); 132 it != expected.end(); 133 ++it) { 134 if (actual.end() == std::find(actual.begin(), actual.end(), *it)) 135 return false; 136 } 137 return true; 138} 139 140bool AddWord(int index, const std::string& word) { 141 SpellcheckCustomDictionary::Change dictionary_change; 142 dictionary_change.AddWord(word); 143 bool result = DictionarySyncIntegrationTestHelper::ApplyChange( 144 GetDictionary(index), dictionary_change); 145 if (sync_datatype_helper::test()->use_verifier()) { 146 result &= DictionarySyncIntegrationTestHelper::ApplyChange( 147 GetVerifierDictionary(), dictionary_change); 148 } 149 return result; 150} 151 152bool RemoveWord(int index, const std::string& word) { 153 SpellcheckCustomDictionary::Change dictionary_change; 154 dictionary_change.RemoveWord(word); 155 bool result = DictionarySyncIntegrationTestHelper::ApplyChange( 156 GetDictionary(index), dictionary_change); 157 if (sync_datatype_helper::test()->use_verifier()) { 158 result &= DictionarySyncIntegrationTestHelper::ApplyChange( 159 GetVerifierDictionary(), dictionary_change); 160 } 161 return result; 162} 163 164} // namespace dictionary_helper 165