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