Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / sync / test / integration / two_client_dictionary_sync_test.cc
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 "base/strings/string_number_conversions.h"
6 #include "chrome/browser/sync/test/integration/dictionary_helper.h"
7 #include "chrome/browser/sync/test/integration/profile_sync_service_harness.h"
8 #include "chrome/browser/sync/test/integration/sync_test.h"
9 #include "chrome/common/spellcheck_common.h"
10
11 class TwoClientDictionarySyncTest : public SyncTest {
12  public:
13   TwoClientDictionarySyncTest() : SyncTest(TWO_CLIENT) {}
14   virtual ~TwoClientDictionarySyncTest() {}
15
16  private:
17   DISALLOW_COPY_AND_ASSIGN(TwoClientDictionarySyncTest);
18 };
19
20 IN_PROC_BROWSER_TEST_F(TwoClientDictionarySyncTest, Sanity) {
21   ASSERT_TRUE(SetupSync()) << "SetupSync() failed.";
22   dictionary_helper::LoadDictionaries();
23   ASSERT_TRUE(dictionary_helper::DictionariesMatch());
24
25   std::vector<std::string> words;
26   words.push_back("foo");
27   words.push_back("bar");
28   ASSERT_EQ(num_clients(), static_cast<int>(words.size()));
29
30   for (int i = 0; i < num_clients(); ++i) {
31     ASSERT_TRUE(dictionary_helper::AddWord(i, words[i]));
32     ASSERT_TRUE(GetClient(i)->AwaitMutualSyncCycleCompletion(
33         GetClient((i + 1) % 2)));
34   }
35   ASSERT_TRUE(dictionary_helper::DictionariesMatch());
36   ASSERT_EQ(words.size(), dictionary_helper::GetDictionarySize(0));
37
38   for (int i = 0; i < num_clients(); ++i) {
39     ASSERT_TRUE(dictionary_helper::RemoveWord(i, words[i]));
40     ASSERT_TRUE(GetClient(i)->AwaitMutualSyncCycleCompletion(
41         GetClient((i + 1) % 2)));
42   }
43   ASSERT_TRUE(dictionary_helper::DictionariesMatch());
44   ASSERT_EQ(0UL, dictionary_helper::GetDictionarySize(0));
45
46   DisableVerifier();
47   for (int i = 0; i < num_clients(); ++i)
48     ASSERT_TRUE(dictionary_helper::AddWord(i, words[i]));
49   ASSERT_TRUE(AwaitQuiescence());
50   ASSERT_TRUE(dictionary_helper::DictionariesMatch());
51   ASSERT_EQ(words.size(), dictionary_helper::GetDictionarySize(0));
52 }
53
54 IN_PROC_BROWSER_TEST_F(TwoClientDictionarySyncTest, SimultaneousAdd) {
55   ASSERT_TRUE(SetupSync()) << "SetupSync() failed.";
56   dictionary_helper::LoadDictionaries();
57   ASSERT_TRUE(dictionary_helper::DictionariesMatch());
58
59   for (int i = 0; i < num_clients(); ++i)
60     dictionary_helper::AddWord(i, "foo");
61   ASSERT_TRUE(AwaitQuiescence());
62   ASSERT_TRUE(dictionary_helper::DictionariesMatch());
63   ASSERT_EQ(1UL, dictionary_helper::GetDictionarySize(0));
64 }
65
66 IN_PROC_BROWSER_TEST_F(TwoClientDictionarySyncTest, SimultaneousRemove) {
67   ASSERT_TRUE(SetupSync()) << "SetupSync() failed.";
68   dictionary_helper::LoadDictionaries();
69   ASSERT_TRUE(dictionary_helper::DictionariesMatch());
70
71   for (int i = 0; i < num_clients(); ++i)
72     dictionary_helper::AddWord(i, "foo");
73   ASSERT_TRUE(AwaitQuiescence());
74   ASSERT_TRUE(dictionary_helper::DictionariesMatch());
75   ASSERT_EQ(1UL, dictionary_helper::GetDictionarySize(0));
76
77   for (int i = 0; i < num_clients(); ++i)
78     dictionary_helper::RemoveWord(i, "foo");
79   ASSERT_TRUE(AwaitQuiescence());
80   ASSERT_TRUE(dictionary_helper::DictionariesMatch());
81   ASSERT_EQ(0UL, dictionary_helper::GetDictionarySize(0));
82 }
83
84 IN_PROC_BROWSER_TEST_F(TwoClientDictionarySyncTest, RemoveOnAAddOnB) {
85   ASSERT_TRUE(SetupSync()) << "SetupSync() failed.";
86   dictionary_helper::LoadDictionaries();
87   ASSERT_TRUE(dictionary_helper::DictionariesMatch());
88
89   std::string word = "foo";
90   // Add on client A
91   ASSERT_TRUE(dictionary_helper::AddWord(0, word));
92   // Remove on client A
93   ASSERT_TRUE(dictionary_helper::RemoveWord(0, word));
94   ASSERT_TRUE(AwaitQuiescence());
95   // Add on client B
96   ASSERT_TRUE(dictionary_helper::AddWord(1, word));
97   ASSERT_TRUE(AwaitQuiescence());
98   ASSERT_TRUE(dictionary_helper::DictionariesMatch());
99   ASSERT_EQ(1UL, dictionary_helper::GetDictionarySize(0));
100 }
101
102 IN_PROC_BROWSER_TEST_F(TwoClientDictionarySyncTest, DisableSync) {
103   ASSERT_TRUE(SetupSync()) << "SetupSync() failed.";
104   dictionary_helper::LoadDictionaries();
105   ASSERT_TRUE(dictionary_helper::DictionariesMatch());
106
107   ASSERT_TRUE(GetClient(1)->DisableSyncForAllDatatypes());
108   ASSERT_TRUE(dictionary_helper::AddWord(0, "foo"));
109   ASSERT_TRUE(GetClient(0)->AwaitCommitActivityCompletion());
110   ASSERT_TRUE(dictionary_helper::DictionaryMatchesVerifier(0));
111   ASSERT_FALSE(dictionary_helper::DictionaryMatchesVerifier(1));
112 }
113
114 IN_PROC_BROWSER_TEST_F(TwoClientDictionarySyncTest, Limit) {
115   ASSERT_TRUE(SetupSync()) << "SetupSync() failed.";
116   dictionary_helper::LoadDictionaries();
117   ASSERT_TRUE(dictionary_helper::DictionariesMatch());
118
119   ASSERT_TRUE(GetClient(0)->DisableSyncForAllDatatypes());
120   for (size_t i = 0;
121        i < chrome::spellcheck_common::MAX_SYNCABLE_DICTIONARY_WORDS;
122        ++i) {
123     ASSERT_TRUE(dictionary_helper::AddWord(
124         0, "foo" + base::Uint64ToString(i)));
125     ASSERT_TRUE(dictionary_helper::AddWord(
126         1, "bar" + base::Uint64ToString(i)));
127   }
128   ASSERT_TRUE(AwaitQuiescence());
129   ASSERT_FALSE(dictionary_helper::DictionariesMatch());
130
131   // Client #0 should have only "foo" set of words.
132   ASSERT_EQ(chrome::spellcheck_common::MAX_SYNCABLE_DICTIONARY_WORDS,
133             dictionary_helper::GetDictionarySize(0));
134
135   // Client #1 should have only "bar" set of words.
136   ASSERT_EQ(chrome::spellcheck_common::MAX_SYNCABLE_DICTIONARY_WORDS,
137             dictionary_helper::GetDictionarySize(1));
138
139   ASSERT_TRUE(GetClient(0)->EnableSyncForAllDatatypes());
140   ASSERT_TRUE(AwaitQuiescence());
141
142   // Client #0 should have both "foo" and "bar" sets of words.
143   ASSERT_EQ(chrome::spellcheck_common::MAX_SYNCABLE_DICTIONARY_WORDS * 2,
144             dictionary_helper::GetDictionarySize(0));
145
146   // The sync server and client #1 should have only "bar" set of words.
147   ASSERT_EQ(chrome::spellcheck_common::MAX_SYNCABLE_DICTIONARY_WORDS,
148             dictionary_helper::GetDictionarySize(1));
149 }