Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(143)

Side by Side Diff: chrome/browser/sync/test/integration/dictionary_helper.cc

Issue 11445002: Sync user's custom spellcheck dictionary (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Add browser tests for dictionary change notifications in settings Created 7 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 namespace dictionary_helper {
19 namespace {
20
21 bool DictionaryHasWord(
22 const SpellcheckCustomDictionary* dictionary,
23 const std::string& word) {
24 return dictionary->GetWords().end() != std::find(
25 dictionary->GetWords().begin(),
26 dictionary->GetWords().end(),
27 word);
28 }
29
30 SpellcheckCustomDictionary* GetDictionary(int index) {
31 return SpellcheckServiceFactory::GetForProfile(
32 sync_datatype_helper::test()->GetProfile(index))->GetCustomDictionary();
33 }
34
35 SpellcheckCustomDictionary* GetVerifierDictionary() {
36 return SpellcheckServiceFactory::GetForProfile(
37 sync_datatype_helper::test()->verifier())->GetCustomDictionary();
38 }
39
40 bool HaveWord(int index, std::string word) {
41 return DictionaryHasWord(GetDictionary(index), word);
42 }
43
44 bool HaveWordInVerifier(std::string word) {
45 return DictionaryHasWord(GetVerifierDictionary(), word);
46 }
47
48 void LoadDictionary(SpellcheckCustomDictionary* dictionary) {
49 if (dictionary->IsLoaded())
50 return;
51 base::RunLoop run_loop;
52 DictionaryLoadObserver observer(content::GetQuitTaskForRunLoop(&run_loop));
53 dictionary->AddObserver(&observer);
54 dictionary->Load();
55 content::RunThisRunLoop(&run_loop);
56 dictionary->RemoveObserver(&observer);
57 ASSERT_TRUE(dictionary->IsLoaded());
58 }
59
60 bool HaveWordMatches(const std::string& word) {
61 bool reference = sync_datatype_helper::test()->use_verifier() ?
62 HaveWordInVerifier(word) : HaveWord(0, word);
63 for (int i = 0; i < sync_datatype_helper::test()->num_clients(); ++i) {
64 if (reference != HaveWord(i, word)) {
65 LOG(ERROR)
66 << "Word " << word << " is " << (reference ? "missing " : "present ")
67 << "in profile " << i << ".";
68 return false;
69 }
70 }
71 return true;
72 }
73
74 } // namespace
75
76 void EnableDictionarySync(CommandLine* cl) {
77 CommandLine::ForCurrentProcess()->AppendSwitch(
78 switches::kEnableSyncDictionary);
79 }
80
81 void LoadDictionaries() {
82 for (int i = 0; i < sync_datatype_helper::test()->num_clients(); ++i) {
83 LOG(ERROR) << "Loading dictionary for profile " << i;
84 LoadDictionary(GetDictionary(i));
85 }
86 if (sync_datatype_helper::test()->use_verifier()) {
87 LOG(ERROR) << "Loading dictionary for verifier profile";
88 LoadDictionary(GetVerifierDictionary());
89 }
90 }
91
92 size_t GetDictionarySize(int index) {
93 return GetDictionary(index)->GetWords().size();
94 }
95
96 size_t GetVerifierDictionarySize() {
97 return GetVerifierDictionary()->GetWords().size();
98 }
99
100 bool DictionariesMatch() {
101 chrome::spellcheck_common::WordList reference =
102 sync_datatype_helper::test()->use_verifier() ?
103 GetVerifierDictionary()->GetWords() : GetDictionary(0)->GetWords();
104 for (int i = 0; i < sync_datatype_helper::test()->num_clients(); ++i) {
105 if (reference.size() != GetDictionary(i)->GetWords().size()) {
106 LOG(ERROR)
107 << "Dictionary in profile " << i << " has "
108 << GetDictionary(i)->GetWords().size() << " words, but should have "
109 << reference.size() << ".";
110 return false;
111 }
112 }
113 for (chrome::spellcheck_common::WordList::iterator it = reference.begin();
114 it != reference.end();
115 ++it) {
116 if (!HaveWordMatches(*it))
117 return false;
118 }
119 return true;
120 }
121
122 bool DictionaryMatchesVerifier(int index) {
123 chrome::spellcheck_common::WordList expected =
124 GetVerifierDictionary()->GetWords();
125 chrome::spellcheck_common::WordList actual = GetDictionary(index)->GetWords();
126 if (expected.size() != actual.size())
127 return false;
128 for (chrome::spellcheck_common::WordList::iterator it = expected.begin();
129 it != expected.end();
130 ++it) {
131 if (actual.end() == std::find(actual.begin(), actual.end(), *it))
132 return false;
133 }
134 return true;
135 }
136
137 bool AddWord(int index, const std::string& word) {
138 SpellcheckCustomDictionary::Change::Result result1;
139 SpellcheckCustomDictionary::Change::Result result2;
140 result1 = GetDictionary(index)->AddWord(word);
141 if (sync_datatype_helper::test()->use_verifier())
142 result2 = GetVerifierDictionary()->AddWord(word);
143 return !result1.detected_invalid_words() &&
144 !result1.detected_duplicate_words() &&
145 !result2.detected_invalid_words() &&
146 !result2.detected_duplicate_words();
147 }
148
149 bool CustomWordAddedLocally(int index, const std::string& word) {
150 SpellcheckCustomDictionary::Change::Result result1;
151 SpellcheckCustomDictionary::Change::Result result2;
152 result1 = GetDictionary(index)->CustomWordAddedLocally(word);
153 if (sync_datatype_helper::test()->use_verifier())
154 result2 = GetVerifierDictionary()->CustomWordAddedLocally(word);
155 return !result1.detected_invalid_words() &&
156 !result1.detected_duplicate_words() &&
157 !result2.detected_invalid_words() &&
158 !result2.detected_duplicate_words();
159 }
160
161 bool RemoveWord(int index, const std::string& word) {
162 SpellcheckCustomDictionary::Change::Result result1;
163 SpellcheckCustomDictionary::Change::Result result2;
164 result1 = GetDictionary(index)->RemoveWord(word);
165 if (sync_datatype_helper::test()->use_verifier())
166 result2 = GetVerifierDictionary()->RemoveWord(word);
167 return !result1.detected_missing_words() && !result2.detected_missing_words();
168 }
169
170 bool CustomWordRemovedLocally(int index, const std::string& word) {
171 SpellcheckCustomDictionary::Change::Result result1;
172 SpellcheckCustomDictionary::Change::Result result2;
173 result1 = GetDictionary(index)->CustomWordRemovedLocally(word);
174 if (sync_datatype_helper::test()->use_verifier())
175 result2 = GetVerifierDictionary()->CustomWordRemovedLocally(word);
176 return !result1.detected_missing_words() && !result2.detected_missing_words();
177 }
178
179 } // namespace dictionary_helper
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698