OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 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/sync/test/integration/dictionary_helper.h" | 5 #include "chrome/browser/sync/test/integration/dictionary_helper.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <set> | 8 #include <set> |
9 | 9 |
10 #include "base/format_macros.h" | 10 #include "base/format_macros.h" |
11 #include "base/macros.h" | 11 #include "base/macros.h" |
12 #include "base/run_loop.h" | 12 #include "base/run_loop.h" |
13 #include "base/strings/string_number_conversions.h" | 13 #include "base/strings/string_number_conversions.h" |
14 #include "base/strings/stringprintf.h" | 14 #include "base/strings/stringprintf.h" |
15 #include "chrome/browser/spellchecker/spellcheck_custom_dictionary.h" | 15 #include "chrome/browser/spellchecker/spellcheck_custom_dictionary.h" |
16 #include "chrome/browser/spellchecker/spellcheck_factory.h" | 16 #include "chrome/browser/spellchecker/spellcheck_factory.h" |
17 #include "chrome/browser/spellchecker/spellcheck_service.h" | 17 #include "chrome/browser/spellchecker/spellcheck_service.h" |
18 #include "chrome/browser/sync/test/integration/dictionary_load_observer.h" | 18 #include "chrome/browser/sync/test/integration/dictionary_load_observer.h" |
19 #include "chrome/browser/sync/test/integration/multi_client_status_change_checke
r.h" | |
20 #include "chrome/browser/sync/test/integration/single_client_status_change_check
er.h" | |
21 #include "chrome/browser/sync/test/integration/sync_datatype_helper.h" | 19 #include "chrome/browser/sync/test/integration/sync_datatype_helper.h" |
22 #include "chrome/browser/sync/test/integration/sync_test.h" | 20 #include "chrome/browser/sync/test/integration/sync_test.h" |
23 #include "content/public/test/test_utils.h" | 21 #include "content/public/test/test_utils.h" |
24 #include "testing/gtest/include/gtest/gtest.h" | 22 #include "testing/gtest/include/gtest/gtest.h" |
25 | 23 |
26 class DictionarySyncIntegrationTestHelper { | 24 class DictionarySyncIntegrationTestHelper { |
27 public: | 25 public: |
28 // Same as SpellcheckCustomDictionary::AddWord/RemoveWord, except does not | 26 // Same as SpellcheckCustomDictionary::AddWord/RemoveWord, except does not |
29 // write to disk. | 27 // write to disk. |
30 static bool ApplyChange(SpellcheckCustomDictionary* dictionary, | 28 static bool ApplyChange(SpellcheckCustomDictionary* dictionary, |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
92 for (int i = 0; i < sync_datatype_helper::test()->num_clients(); ++i) { | 90 for (int i = 0; i < sync_datatype_helper::test()->num_clients(); ++i) { |
93 const std::set<std::string>& dictionary = GetDictionary(i)->GetWords(); | 91 const std::set<std::string>& dictionary = GetDictionary(i)->GetWords(); |
94 if (reference.size() != dictionary.size() || | 92 if (reference.size() != dictionary.size() || |
95 !std::equal(reference.begin(), reference.end(), dictionary.begin())) { | 93 !std::equal(reference.begin(), reference.end(), dictionary.begin())) { |
96 return false; | 94 return false; |
97 } | 95 } |
98 } | 96 } |
99 return true; | 97 return true; |
100 } | 98 } |
101 | 99 |
102 namespace { | |
103 | |
104 // Helper class used in the implementation of AwaitDictionariesMatch. | |
105 class DictionaryMatchStatusChecker : public MultiClientStatusChangeChecker { | |
106 public: | |
107 DictionaryMatchStatusChecker(); | |
108 ~DictionaryMatchStatusChecker() override; | |
109 | |
110 bool IsExitConditionSatisfied() override; | |
111 std::string GetDebugMessage() const override; | |
112 }; | |
113 | |
114 DictionaryMatchStatusChecker::DictionaryMatchStatusChecker() | |
115 : MultiClientStatusChangeChecker( | |
116 sync_datatype_helper::test()->GetSyncServices()) {} | |
117 | |
118 DictionaryMatchStatusChecker::~DictionaryMatchStatusChecker() {} | |
119 | |
120 bool DictionaryMatchStatusChecker::IsExitConditionSatisfied() { | |
121 return DictionariesMatch(); | |
122 } | |
123 | |
124 std::string DictionaryMatchStatusChecker::GetDebugMessage() const { | |
125 return "Waiting for matching dictionaries"; | |
126 } | |
127 | |
128 // Helper class used in the implementation of AwaitNumDictionaryEntries. | |
129 class NumDictionaryEntriesStatusChecker | |
130 : public SingleClientStatusChangeChecker { | |
131 public: | |
132 NumDictionaryEntriesStatusChecker(int index, size_t num_words); | |
133 ~NumDictionaryEntriesStatusChecker() override; | |
134 | |
135 bool IsExitConditionSatisfied() override; | |
136 std::string GetDebugMessage() const override; | |
137 | |
138 private: | |
139 int index_; | |
140 size_t num_words_; | |
141 }; | |
142 | |
143 NumDictionaryEntriesStatusChecker::NumDictionaryEntriesStatusChecker( | |
144 int index, size_t num_words) | |
145 : SingleClientStatusChangeChecker( | |
146 sync_datatype_helper::test()->GetSyncService(index)), | |
147 index_(index), | |
148 num_words_(num_words) {} | |
149 | |
150 NumDictionaryEntriesStatusChecker::~NumDictionaryEntriesStatusChecker() {} | |
151 | |
152 bool NumDictionaryEntriesStatusChecker::IsExitConditionSatisfied() { | |
153 return GetDictionarySize(index_) == num_words_; | |
154 } | |
155 | |
156 std::string NumDictionaryEntriesStatusChecker::GetDebugMessage() const { | |
157 return base::StringPrintf( | |
158 "Waiting for client %d: %" PRIuS " / %" PRIuS " words downloaded", | |
159 index_, GetDictionarySize(index_), num_words_); | |
160 } | |
161 | |
162 } // namespace | |
163 | |
164 bool AwaitDictionariesMatch() { | |
165 DictionaryMatchStatusChecker checker; | |
166 checker.Wait(); | |
167 return !checker.TimedOut(); | |
168 } | |
169 | |
170 bool AwaitNumDictionaryEntries(int index, size_t num_words) { | |
171 NumDictionaryEntriesStatusChecker checker(index, num_words); | |
172 checker.Wait(); | |
173 return !checker.TimedOut(); | |
174 } | |
175 | |
176 bool DictionaryMatchesVerifier(int index) { | 100 bool DictionaryMatchesVerifier(int index) { |
177 const std::set<std::string>& expected = GetVerifierDictionary()->GetWords(); | 101 const std::set<std::string>& expected = GetVerifierDictionary()->GetWords(); |
178 const std::set<std::string>& actual = GetDictionary(index)->GetWords(); | 102 const std::set<std::string>& actual = GetDictionary(index)->GetWords(); |
179 return expected.size() == actual.size() && | 103 return expected.size() == actual.size() && |
180 std::equal(expected.begin(), expected.end(), actual.begin()); | 104 std::equal(expected.begin(), expected.end(), actual.begin()); |
181 } | 105 } |
182 | 106 |
183 bool AddWord(int index, const std::string& word) { | 107 bool AddWord(int index, const std::string& word) { |
184 SpellcheckCustomDictionary::Change dictionary_change; | 108 SpellcheckCustomDictionary::Change dictionary_change; |
185 dictionary_change.AddWord(word); | 109 dictionary_change.AddWord(word); |
(...skipping 20 matching lines...) Expand all Loading... |
206 bool result = DictionarySyncIntegrationTestHelper::ApplyChange( | 130 bool result = DictionarySyncIntegrationTestHelper::ApplyChange( |
207 GetDictionary(index), &dictionary_change); | 131 GetDictionary(index), &dictionary_change); |
208 if (sync_datatype_helper::test()->use_verifier()) { | 132 if (sync_datatype_helper::test()->use_verifier()) { |
209 result &= DictionarySyncIntegrationTestHelper::ApplyChange( | 133 result &= DictionarySyncIntegrationTestHelper::ApplyChange( |
210 GetVerifierDictionary(), &dictionary_change); | 134 GetVerifierDictionary(), &dictionary_change); |
211 } | 135 } |
212 return result; | 136 return result; |
213 } | 137 } |
214 | 138 |
215 } // namespace dictionary_helper | 139 } // namespace dictionary_helper |
| 140 |
| 141 DictionaryMatchChecker::DictionaryMatchChecker() |
| 142 : MultiClientStatusChangeChecker( |
| 143 sync_datatype_helper::test()->GetSyncServices()) {} |
| 144 |
| 145 bool DictionaryMatchChecker::IsExitConditionSatisfied() { |
| 146 return dictionary_helper::DictionariesMatch(); |
| 147 } |
| 148 |
| 149 std::string DictionaryMatchChecker::GetDebugMessage() const { |
| 150 return "Waiting for matching dictionaries"; |
| 151 } |
| 152 |
| 153 NumDictionaryEntriesChecker::NumDictionaryEntriesChecker(int index, |
| 154 size_t num_words) |
| 155 : SingleClientStatusChangeChecker( |
| 156 sync_datatype_helper::test()->GetSyncService(index)), |
| 157 index_(index), |
| 158 num_words_(num_words) {} |
| 159 |
| 160 bool NumDictionaryEntriesChecker::IsExitConditionSatisfied() { |
| 161 return dictionary_helper::GetDictionarySize(index_) == num_words_; |
| 162 } |
| 163 |
| 164 std::string NumDictionaryEntriesChecker::GetDebugMessage() const { |
| 165 return base::StringPrintf( |
| 166 "Waiting for client %d: %" PRIuS " / %" PRIuS " words downloaded", index_, |
| 167 dictionary_helper::GetDictionarySize(index_), num_words_); |
| 168 } |
OLD | NEW |