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> |
| 8 |
7 #include "chrome/browser/profiles/profile.h" | 9 #include "chrome/browser/profiles/profile.h" |
8 #include "chrome/browser/spellchecker/spellcheck_factory.h" | 10 #include "chrome/browser/spellchecker/spellcheck_factory.h" |
9 #include "chrome/browser/spellchecker/spellcheck_service.h" | 11 #include "chrome/browser/spellchecker/spellcheck_service.h" |
10 #include "chrome/browser/sync/profile_sync_service_harness.h" | 12 #include "chrome/browser/sync/profile_sync_service_harness.h" |
11 #include "chrome/browser/sync/test/integration/dictionary_load_observer.h" | 13 #include "chrome/browser/sync/test/integration/dictionary_load_observer.h" |
12 #include "chrome/browser/sync/test/integration/sync_datatype_helper.h" | 14 #include "chrome/browser/sync/test/integration/sync_datatype_helper.h" |
13 #include "chrome/browser/sync/test/integration/sync_test.h" | 15 #include "chrome/browser/sync/test/integration/sync_test.h" |
14 #include "chrome/common/chrome_switches.h" | 16 #include "chrome/common/chrome_switches.h" |
15 #include "chrome/common/spellcheck_common.h" | 17 #include "chrome/common/spellcheck_common.h" |
16 #include "content/public/test/test_utils.h" | 18 #include "content/public/test/test_utils.h" |
17 | 19 |
18 class DictionarySyncIntegrationTestHelper { | 20 class DictionarySyncIntegrationTestHelper { |
19 public: | 21 public: |
20 // Same as SpellcheckCustomDictionary::AddWord/RemoveWord, except does not | 22 // Same as SpellcheckCustomDictionary::AddWord/RemoveWord, except does not |
21 // write to disk. | 23 // write to disk. |
22 static bool ApplyChange( | 24 static bool ApplyChange( |
23 SpellcheckCustomDictionary* dictionary, | 25 SpellcheckCustomDictionary* dictionary, |
24 SpellcheckCustomDictionary::Change& change) { | 26 SpellcheckCustomDictionary::Change& change) { |
25 std::sort(dictionary->words_.begin(), dictionary->words_.end()); | |
26 int result = change.Sanitize(dictionary->GetWords()); | 27 int result = change.Sanitize(dictionary->GetWords()); |
27 dictionary->Apply(change); | 28 dictionary->Apply(change); |
28 dictionary->Notify(change); | 29 dictionary->Notify(change); |
29 dictionary->Sync(change); | 30 dictionary->Sync(change); |
30 return !result; | 31 return !result; |
31 } | 32 } |
32 | 33 |
33 DISALLOW_COPY_AND_ASSIGN(DictionarySyncIntegrationTestHelper); | 34 DISALLOW_COPY_AND_ASSIGN(DictionarySyncIntegrationTestHelper); |
34 }; | 35 }; |
35 | 36 |
36 | 37 |
37 namespace dictionary_helper { | 38 namespace dictionary_helper { |
38 namespace { | 39 namespace { |
39 | 40 |
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) { | 41 SpellcheckCustomDictionary* GetDictionary(int index) { |
50 return SpellcheckServiceFactory::GetForProfile( | 42 return SpellcheckServiceFactory::GetForProfile( |
51 sync_datatype_helper::test()->GetProfile(index))->GetCustomDictionary(); | 43 sync_datatype_helper::test()->GetProfile(index))->GetCustomDictionary(); |
52 } | 44 } |
53 | 45 |
54 SpellcheckCustomDictionary* GetVerifierDictionary() { | 46 SpellcheckCustomDictionary* GetVerifierDictionary() { |
55 return SpellcheckServiceFactory::GetForProfile( | 47 return SpellcheckServiceFactory::GetForProfile( |
56 sync_datatype_helper::test()->verifier())->GetCustomDictionary(); | 48 sync_datatype_helper::test()->verifier())->GetCustomDictionary(); |
57 } | 49 } |
58 | 50 |
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) { | 51 void LoadDictionary(SpellcheckCustomDictionary* dictionary) { |
68 if (dictionary->IsLoaded()) | 52 if (dictionary->IsLoaded()) |
69 return; | 53 return; |
70 base::RunLoop run_loop; | 54 base::RunLoop run_loop; |
71 DictionaryLoadObserver observer(content::GetQuitTaskForRunLoop(&run_loop)); | 55 DictionaryLoadObserver observer(content::GetQuitTaskForRunLoop(&run_loop)); |
72 dictionary->AddObserver(&observer); | 56 dictionary->AddObserver(&observer); |
73 dictionary->Load(); | 57 dictionary->Load(); |
74 content::RunThisRunLoop(&run_loop); | 58 content::RunThisRunLoop(&run_loop); |
75 dictionary->RemoveObserver(&observer); | 59 dictionary->RemoveObserver(&observer); |
76 ASSERT_TRUE(dictionary->IsLoaded()); | 60 ASSERT_TRUE(dictionary->IsLoaded()); |
77 } | 61 } |
78 | 62 |
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 return false; | |
85 } | |
86 } | |
87 return true; | |
88 } | |
89 | |
90 } // namespace | 63 } // namespace |
91 | 64 |
92 | 65 |
93 void LoadDictionaries() { | 66 void LoadDictionaries() { |
94 for (int i = 0; i < sync_datatype_helper::test()->num_clients(); ++i) | 67 for (int i = 0; i < sync_datatype_helper::test()->num_clients(); ++i) |
95 LoadDictionary(GetDictionary(i)); | 68 LoadDictionary(GetDictionary(i)); |
96 if (sync_datatype_helper::test()->use_verifier()) | 69 if (sync_datatype_helper::test()->use_verifier()) |
97 LoadDictionary(GetVerifierDictionary()); | 70 LoadDictionary(GetVerifierDictionary()); |
98 } | 71 } |
99 | 72 |
100 size_t GetDictionarySize(int index) { | 73 size_t GetDictionarySize(int index) { |
101 return GetDictionary(index)->GetWords().size(); | 74 return GetDictionary(index)->GetWords().size(); |
102 } | 75 } |
103 | 76 |
104 size_t GetVerifierDictionarySize() { | 77 size_t GetVerifierDictionarySize() { |
105 return GetVerifierDictionary()->GetWords().size(); | 78 return GetVerifierDictionary()->GetWords().size(); |
106 } | 79 } |
107 | 80 |
108 bool DictionariesMatch() { | 81 bool DictionariesMatch() { |
109 chrome::spellcheck_common::WordList reference = | 82 const chrome::spellcheck_common::WordSet& reference = |
110 sync_datatype_helper::test()->use_verifier() ? | 83 sync_datatype_helper::test()->use_verifier() |
111 GetVerifierDictionary()->GetWords() : GetDictionary(0)->GetWords(); | 84 ? GetVerifierDictionary()->GetWords() |
| 85 : GetDictionary(0)->GetWords(); |
112 for (int i = 0; i < sync_datatype_helper::test()->num_clients(); ++i) { | 86 for (int i = 0; i < sync_datatype_helper::test()->num_clients(); ++i) { |
113 if (reference.size() != GetDictionary(i)->GetWords().size()) | 87 const chrome::spellcheck_common::WordSet& dictionary = |
| 88 GetDictionary(i)->GetWords(); |
| 89 if (reference.size() != dictionary.size() || |
| 90 !std::equal(reference.begin(), reference.end(), dictionary.begin())) { |
114 return false; | 91 return false; |
115 } | 92 } |
116 for (chrome::spellcheck_common::WordList::iterator it = reference.begin(); | |
117 it != reference.end(); | |
118 ++it) { | |
119 if (!HaveWordMatches(*it)) | |
120 return false; | |
121 } | 93 } |
122 return true; | 94 return true; |
123 } | 95 } |
124 | 96 |
125 bool DictionaryMatchesVerifier(int index) { | 97 bool DictionaryMatchesVerifier(int index) { |
126 chrome::spellcheck_common::WordList expected = | 98 const chrome::spellcheck_common::WordSet& expected = |
127 GetVerifierDictionary()->GetWords(); | 99 GetVerifierDictionary()->GetWords(); |
128 chrome::spellcheck_common::WordList actual = GetDictionary(index)->GetWords(); | 100 const chrome::spellcheck_common::WordSet& actual = |
129 if (expected.size() != actual.size()) | 101 GetDictionary(index)->GetWords(); |
130 return false; | 102 return expected.size() == actual.size() && |
131 for (chrome::spellcheck_common::WordList::iterator it = expected.begin(); | 103 std::equal(expected.begin(), expected.end(), actual.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 } | 104 } |
139 | 105 |
140 bool AddWord(int index, const std::string& word) { | 106 bool AddWord(int index, const std::string& word) { |
141 SpellcheckCustomDictionary::Change dictionary_change; | 107 SpellcheckCustomDictionary::Change dictionary_change; |
142 dictionary_change.AddWord(word); | 108 dictionary_change.AddWord(word); |
143 bool result = DictionarySyncIntegrationTestHelper::ApplyChange( | 109 bool result = DictionarySyncIntegrationTestHelper::ApplyChange( |
144 GetDictionary(index), dictionary_change); | 110 GetDictionary(index), dictionary_change); |
145 if (sync_datatype_helper::test()->use_verifier()) { | 111 if (sync_datatype_helper::test()->use_verifier()) { |
146 result &= DictionarySyncIntegrationTestHelper::ApplyChange( | 112 result &= DictionarySyncIntegrationTestHelper::ApplyChange( |
147 GetVerifierDictionary(), dictionary_change); | 113 GetVerifierDictionary(), dictionary_change); |
148 } | 114 } |
149 return result; | 115 return result; |
150 } | 116 } |
151 | 117 |
152 bool RemoveWord(int index, const std::string& word) { | 118 bool RemoveWord(int index, const std::string& word) { |
153 SpellcheckCustomDictionary::Change dictionary_change; | 119 SpellcheckCustomDictionary::Change dictionary_change; |
154 dictionary_change.RemoveWord(word); | 120 dictionary_change.RemoveWord(word); |
155 bool result = DictionarySyncIntegrationTestHelper::ApplyChange( | 121 bool result = DictionarySyncIntegrationTestHelper::ApplyChange( |
156 GetDictionary(index), dictionary_change); | 122 GetDictionary(index), dictionary_change); |
157 if (sync_datatype_helper::test()->use_verifier()) { | 123 if (sync_datatype_helper::test()->use_verifier()) { |
158 result &= DictionarySyncIntegrationTestHelper::ApplyChange( | 124 result &= DictionarySyncIntegrationTestHelper::ApplyChange( |
159 GetVerifierDictionary(), dictionary_change); | 125 GetVerifierDictionary(), dictionary_change); |
160 } | 126 } |
161 return result; | 127 return result; |
162 } | 128 } |
163 | 129 |
164 } // namespace dictionary_helper | 130 } // namespace dictionary_helper |
OLD | NEW |