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.
5 #include "chrome/browser/sync/test/integration/dictionary_helper.h"
9 #include "base/format_macros.h"
10 #include "base/strings/stringprintf.h"
11 #include "chrome/browser/profiles/profile.h"
12 #include "chrome/browser/spellchecker/spellcheck_factory.h"
13 #include "chrome/browser/spellchecker/spellcheck_service.h"
14 #include "chrome/browser/sync/test/integration/dictionary_load_observer.h"
15 #include "chrome/browser/sync/test/integration/multi_client_status_change_checker.h"
16 #include "chrome/browser/sync/test/integration/profile_sync_service_harness.h"
17 #include "chrome/browser/sync/test/integration/single_client_status_change_checker.h"
18 #include "chrome/browser/sync/test/integration/sync_datatype_helper.h"
19 #include "chrome/browser/sync/test/integration/sync_test.h"
20 #include "chrome/common/chrome_switches.h"
21 #include "chrome/common/spellcheck_common.h"
22 #include "content/public/test/test_utils.h"
24 class DictionarySyncIntegrationTestHelper
{
26 // Same as SpellcheckCustomDictionary::AddWord/RemoveWord, except does not
28 static bool ApplyChange(
29 SpellcheckCustomDictionary
* dictionary
,
30 SpellcheckCustomDictionary::Change
& change
) {
31 int result
= change
.Sanitize(dictionary
->GetWords());
32 dictionary
->Apply(change
);
33 dictionary
->Notify(change
);
34 dictionary
->Sync(change
);
38 DISALLOW_COPY_AND_ASSIGN(DictionarySyncIntegrationTestHelper
);
42 namespace dictionary_helper
{
45 SpellcheckCustomDictionary
* GetDictionary(int index
) {
46 return SpellcheckServiceFactory::GetForContext(
47 sync_datatype_helper::test()->GetProfile(index
))->GetCustomDictionary();
50 SpellcheckCustomDictionary
* GetVerifierDictionary() {
51 return SpellcheckServiceFactory::GetForContext(
52 sync_datatype_helper::test()->verifier())->GetCustomDictionary();
55 void LoadDictionary(SpellcheckCustomDictionary
* dictionary
) {
56 if (dictionary
->IsLoaded())
58 base::RunLoop run_loop
;
59 DictionaryLoadObserver
observer(content::GetQuitTaskForRunLoop(&run_loop
));
60 dictionary
->AddObserver(&observer
);
62 content::RunThisRunLoop(&run_loop
);
63 dictionary
->RemoveObserver(&observer
);
64 ASSERT_TRUE(dictionary
->IsLoaded());
70 void LoadDictionaries() {
71 for (int i
= 0; i
< sync_datatype_helper::test()->num_clients(); ++i
)
72 LoadDictionary(GetDictionary(i
));
73 if (sync_datatype_helper::test()->use_verifier())
74 LoadDictionary(GetVerifierDictionary());
77 size_t GetDictionarySize(int index
) {
78 return GetDictionary(index
)->GetWords().size();
81 size_t GetVerifierDictionarySize() {
82 return GetVerifierDictionary()->GetWords().size();
85 bool DictionariesMatch() {
86 const chrome::spellcheck_common::WordSet
& reference
=
87 sync_datatype_helper::test()->use_verifier()
88 ? GetVerifierDictionary()->GetWords()
89 : GetDictionary(0)->GetWords();
90 for (int i
= 0; i
< sync_datatype_helper::test()->num_clients(); ++i
) {
91 const chrome::spellcheck_common::WordSet
& dictionary
=
92 GetDictionary(i
)->GetWords();
93 if (reference
.size() != dictionary
.size() ||
94 !std::equal(reference
.begin(), reference
.end(), dictionary
.begin())) {
103 // Helper class used in the implementation of AwaitDictionariesMatch.
104 class DictionaryMatchStatusChecker
: public MultiClientStatusChangeChecker
{
106 DictionaryMatchStatusChecker();
107 ~DictionaryMatchStatusChecker() override
;
109 bool IsExitConditionSatisfied() override
;
110 std::string
GetDebugMessage() const override
;
113 DictionaryMatchStatusChecker::DictionaryMatchStatusChecker()
114 : MultiClientStatusChangeChecker(
115 sync_datatype_helper::test()->GetSyncServices()) {}
117 DictionaryMatchStatusChecker::~DictionaryMatchStatusChecker() {}
119 bool DictionaryMatchStatusChecker::IsExitConditionSatisfied() {
120 return DictionariesMatch();
123 std::string
DictionaryMatchStatusChecker::GetDebugMessage() const {
124 return "Waiting for matching dictionaries";
127 // Helper class used in the implementation of AwaitNumDictionaryEntries.
128 class NumDictionaryEntriesStatusChecker
129 : public SingleClientStatusChangeChecker
{
131 NumDictionaryEntriesStatusChecker(int index
, size_t num_words
);
132 ~NumDictionaryEntriesStatusChecker() override
;
134 bool IsExitConditionSatisfied() override
;
135 std::string
GetDebugMessage() const override
;
142 NumDictionaryEntriesStatusChecker::NumDictionaryEntriesStatusChecker(
143 int index
, size_t num_words
)
144 : SingleClientStatusChangeChecker(
145 sync_datatype_helper::test()->GetSyncService(index
)),
147 num_words_(num_words
) {}
149 NumDictionaryEntriesStatusChecker::~NumDictionaryEntriesStatusChecker() {}
151 bool NumDictionaryEntriesStatusChecker::IsExitConditionSatisfied() {
152 return GetDictionarySize(index_
) == num_words_
;
155 std::string
NumDictionaryEntriesStatusChecker::GetDebugMessage() const {
156 return base::StringPrintf(
157 "Waiting for client %d: %" PRIuS
" / %" PRIuS
" words downloaded",
158 index_
, GetDictionarySize(index_
), num_words_
);
163 bool AwaitDictionariesMatch() {
164 DictionaryMatchStatusChecker checker
;
166 return !checker
.TimedOut();
169 bool AwaitNumDictionaryEntries(int index
, size_t num_words
) {
170 NumDictionaryEntriesStatusChecker
checker(index
, num_words
);
172 return !checker
.TimedOut();
175 bool DictionaryMatchesVerifier(int index
) {
176 const chrome::spellcheck_common::WordSet
& expected
=
177 GetVerifierDictionary()->GetWords();
178 const chrome::spellcheck_common::WordSet
& actual
=
179 GetDictionary(index
)->GetWords();
180 return expected
.size() == actual
.size() &&
181 std::equal(expected
.begin(), expected
.end(), actual
.begin());
184 bool AddWord(int index
, const std::string
& word
) {
185 SpellcheckCustomDictionary::Change dictionary_change
;
186 dictionary_change
.AddWord(word
);
187 bool result
= DictionarySyncIntegrationTestHelper::ApplyChange(
188 GetDictionary(index
), dictionary_change
);
189 if (sync_datatype_helper::test()->use_verifier()) {
190 result
&= DictionarySyncIntegrationTestHelper::ApplyChange(
191 GetVerifierDictionary(), dictionary_change
);
196 bool RemoveWord(int index
, const std::string
& word
) {
197 SpellcheckCustomDictionary::Change dictionary_change
;
198 dictionary_change
.RemoveWord(word
);
199 bool result
= DictionarySyncIntegrationTestHelper::ApplyChange(
200 GetDictionary(index
), dictionary_change
);
201 if (sync_datatype_helper::test()->use_verifier()) {
202 result
&= DictionarySyncIntegrationTestHelper::ApplyChange(
203 GetVerifierDictionary(), dictionary_change
);
208 } // namespace dictionary_helper