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