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 "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"
11 using dictionary_helper::AwaitNumDictionaryEntries
;
12 using chrome::spellcheck_common::MAX_SYNCABLE_DICTIONARY_WORDS
;
14 class MultipleClientDictionarySyncTest
: public SyncTest
{
16 MultipleClientDictionarySyncTest() : SyncTest(MULTIPLE_CLIENT
) {}
17 ~MultipleClientDictionarySyncTest() override
{}
19 bool TestUsesSelfNotifications() override
{ return false; }
22 DISALLOW_COPY_AND_ASSIGN(MultipleClientDictionarySyncTest
);
25 IN_PROC_BROWSER_TEST_F(MultipleClientDictionarySyncTest
, AddToOne
) {
26 ASSERT_TRUE(SetupSync()) << "SetupSync() failed.";
27 dictionary_helper::LoadDictionaries();
28 ASSERT_TRUE(dictionary_helper::AwaitDictionariesMatch());
30 ASSERT_TRUE(dictionary_helper::AddWord(0, "foo"));
31 ASSERT_TRUE(dictionary_helper::AwaitDictionariesMatch());
34 IN_PROC_BROWSER_TEST_F(MultipleClientDictionarySyncTest
, AddSameToAll
) {
35 ASSERT_TRUE(SetupSync()) << "SetupSync() failed.";
36 dictionary_helper::LoadDictionaries();
37 ASSERT_TRUE(dictionary_helper::AwaitDictionariesMatch());
39 for (int i
= 0; i
< num_clients(); ++i
)
40 dictionary_helper::AddWord(i
, "foo");
41 ASSERT_TRUE(dictionary_helper::AwaitDictionariesMatch());
42 ASSERT_EQ(1UL, dictionary_helper::GetDictionarySize(0));
45 IN_PROC_BROWSER_TEST_F(MultipleClientDictionarySyncTest
, AddDifferentToAll
) {
46 ASSERT_TRUE(SetupSync()) << "SetupSync() failed.";
47 dictionary_helper::LoadDictionaries();
48 ASSERT_TRUE(dictionary_helper::AwaitDictionariesMatch());
50 for (int i
= 0; i
< num_clients(); ++i
)
51 dictionary_helper::AddWord(i
, "foo" + base::IntToString(i
));
52 ASSERT_TRUE(dictionary_helper::AwaitDictionariesMatch());
53 ASSERT_EQ(num_clients(),
54 static_cast<int>(dictionary_helper::GetDictionarySize(0)));
57 // Tests the case where the Nth client pushes the server beyond its
58 // MAX_SYNCABLE_DICTIONARY_WORDS limit.
59 // Crashes on Windows. crbug.com/431354
61 #define MAYBE_Limit DISABLED_Limit
63 #define MAYBE_Limit Limit
65 IN_PROC_BROWSER_TEST_F(MultipleClientDictionarySyncTest
, MAYBE_Limit
) {
66 ASSERT_TRUE(SetupSync()) << "SetupSync() failed.";
67 dictionary_helper::LoadDictionaries();
68 ASSERT_TRUE(dictionary_helper::AwaitDictionariesMatch());
70 const int n
= num_clients();
72 // Pick a number of initial words per client such that
73 // (num-clients()-1) * initial_words
74 // < MAX_SYNCABLE_DICTIONARY_WORDS
75 // < num_clients() * initial_words
76 size_t initial_words
=
77 (MAX_SYNCABLE_DICTIONARY_WORDS
+ n
) / n
;
79 // Add |initial_words| words to each of the clients before sync.
80 for (int i
= 0; i
< n
; ++i
) {
81 GetClient(i
)->DisableSyncForAllDatatypes();
82 for (size_t j
= 0; j
< initial_words
; ++j
) {
83 ASSERT_TRUE(dictionary_helper::AddWord(
84 i
, "foo-" + base::IntToString(i
) + "-" + base::Uint64ToString(j
)));
86 ASSERT_EQ(initial_words
, dictionary_helper::GetDictionarySize(i
));
89 // As long as we don't get involved in any race conditions where two clients
90 // are committing at once, we should be able to guarantee that the server has
91 // at most MAX_SYNCABLE_DICTIONARY_WORDS words. Every client will be able to
92 // sync these items. Clients are allowed to have more words if they're
93 // available locally, but they won't be able to commit any words once the
96 // As we enable clients one-by-one, all but the (N-1)th client should be able
97 // to commit all of their items. The last one will have some local data left
100 // Open the floodgates. Allow N-1 clients to sync their items.
101 for (int i
= 0; i
< n
-1; ++i
) {
104 // Client #i has |initial_words| words before sync.
105 ASSERT_EQ(initial_words
, dictionary_helper::GetDictionarySize(i
));
106 ASSERT_TRUE(GetClient(i
)->EnableSyncForAllDatatypes());
109 // Wait for clients to catch up. All should be in sync with the server
110 // and have exactly (initial_words * (N-1)) words in their dictionaries.
111 for (int i
= 0; i
< n
-1; ++i
) {
113 ASSERT_TRUE(AwaitNumDictionaryEntries(i
, initial_words
*(n
-1)));
116 // Add the client that has engough new words to cause an overflow.
117 ASSERT_EQ(initial_words
, dictionary_helper::GetDictionarySize(n
-1));
118 ASSERT_TRUE(GetClient(n
-1)->EnableSyncForAllDatatypes());
120 // The Nth client will receive the initial_words * (n-1) entries that were on
121 // the server. It will commit some of the entries it had locally. And it
122 // will have a few uncommittable items left over.
123 ASSERT_TRUE(AwaitNumDictionaryEntries(n
-1, initial_words
*n
));
125 // Everyone else should be at the limit.
126 for (int i
= 0; i
< n
-1; ++i
) {
128 ASSERT_TRUE(AwaitNumDictionaryEntries(i
, MAX_SYNCABLE_DICTIONARY_WORDS
));