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( | |
Nicolas Zea
2013/01/17 20:14:52
Does this need the class around it? Can you just h
please use gerrit instead
2013/01/17 22:25:29
This function is accessing private methods in Spel
Nicolas Zea
2013/01/17 22:40:15
I see. You could just declare the function as a fr
| |
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 LOG(ERROR) | |
85 << "Word " << word << " is " << (reference ? "missing " : "present ") | |
86 << "in profile " << i << "."; | |
87 return false; | |
88 } | |
89 } | |
90 return true; | |
91 } | |
92 | |
93 } // namespace | |
94 | |
95 | |
96 void EnableDictionarySync(CommandLine* cl) { | |
97 CommandLine::ForCurrentProcess()->AppendSwitch( | |
98 switches::kEnableSyncDictionary); | |
99 } | |
100 | |
101 void LoadDictionaries() { | |
102 for (int i = 0; i < sync_datatype_helper::test()->num_clients(); ++i) { | |
103 LOG(ERROR) << "Loading dictionary for profile " << i; | |
Nicolas Zea
2013/01/17 20:14:52
These aren't actually errors right?
please use gerrit instead
2013/01/17 22:25:29
These are debug statements. I've removed them now.
| |
104 LoadDictionary(GetDictionary(i)); | |
105 } | |
106 if (sync_datatype_helper::test()->use_verifier()) { | |
107 LOG(ERROR) << "Loading dictionary for verifier profile"; | |
108 LoadDictionary(GetVerifierDictionary()); | |
109 } | |
110 } | |
111 | |
112 size_t GetDictionarySize(int index) { | |
113 return GetDictionary(index)->GetWords().size(); | |
114 } | |
115 | |
116 size_t GetVerifierDictionarySize() { | |
117 return GetVerifierDictionary()->GetWords().size(); | |
118 } | |
119 | |
120 bool DictionariesMatch() { | |
121 chrome::spellcheck_common::WordList reference = | |
122 sync_datatype_helper::test()->use_verifier() ? | |
123 GetVerifierDictionary()->GetWords() : GetDictionary(0)->GetWords(); | |
124 for (int i = 0; i < sync_datatype_helper::test()->num_clients(); ++i) { | |
125 if (reference.size() != GetDictionary(i)->GetWords().size()) { | |
126 LOG(ERROR) | |
127 << "Dictionary in profile " << i << " has " | |
128 << GetDictionary(i)->GetWords().size() << " words, but should have " | |
129 << reference.size() << "."; | |
130 return false; | |
131 } | |
132 } | |
133 for (chrome::spellcheck_common::WordList::iterator it = reference.begin(); | |
134 it != reference.end(); | |
135 ++it) { | |
136 if (!HaveWordMatches(*it)) | |
137 return false; | |
138 } | |
139 return true; | |
140 } | |
141 | |
142 bool DictionaryMatchesVerifier(int index) { | |
143 chrome::spellcheck_common::WordList expected = | |
144 GetVerifierDictionary()->GetWords(); | |
145 chrome::spellcheck_common::WordList actual = GetDictionary(index)->GetWords(); | |
146 if (expected.size() != actual.size()) | |
147 return false; | |
148 for (chrome::spellcheck_common::WordList::iterator it = expected.begin(); | |
149 it != expected.end(); | |
150 ++it) { | |
151 if (actual.end() == std::find(actual.begin(), actual.end(), *it)) | |
152 return false; | |
153 } | |
154 return true; | |
155 } | |
156 | |
157 bool AddWord(int index, const std::string& word) { | |
158 SpellcheckCustomDictionary::Change dictionary_change; | |
159 dictionary_change.AddWord(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 bool RemoveWord(int index, const std::string& word) { | |
170 SpellcheckCustomDictionary::Change dictionary_change; | |
171 dictionary_change.RemoveWord(word); | |
172 bool result = DictionarySyncIntegrationTestHelper::ApplyChange( | |
173 GetDictionary(index), dictionary_change); | |
174 if (sync_datatype_helper::test()->use_verifier()) { | |
175 result &= DictionarySyncIntegrationTestHelper::ApplyChange( | |
176 GetVerifierDictionary(), dictionary_change); | |
177 } | |
178 return result; | |
179 } | |
180 | |
181 } // namespace dictionary_helper | |
OLD | NEW |