1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
|
// Copyright 2011 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/sync/test/integration/dictionary_helper.h"
#include <set>
#include "base/format_macros.h"
#include "base/run_loop.h"
#include "base/strings/string_number_conversions.h"
#include "chrome/browser/spellchecker/spellcheck_custom_dictionary.h"
#include "chrome/browser/spellchecker/spellcheck_factory.h"
#include "chrome/browser/spellchecker/spellcheck_service.h"
#include "chrome/browser/sync/test/integration/dictionary_load_observer.h"
#include "chrome/browser/sync/test/integration/sync_datatype_helper.h"
#include "chrome/browser/sync/test/integration/sync_test.h"
#include "content/public/test/test_utils.h"
#include "testing/gtest/include/gtest/gtest.h"
class DictionarySyncIntegrationTestHelper {
public:
DictionarySyncIntegrationTestHelper(
const DictionarySyncIntegrationTestHelper&) = delete;
DictionarySyncIntegrationTestHelper& operator=(
const DictionarySyncIntegrationTestHelper&) = delete;
// Same as SpellcheckCustomDictionary::AddWord/RemoveWord, except does not
// write to disk.
static bool ApplyChange(SpellcheckCustomDictionary* dictionary,
SpellcheckCustomDictionary::Change* change) {
int result = change->Sanitize(dictionary->GetWords());
dictionary->Apply(*change);
dictionary->Notify(*change);
dictionary->Sync(*change);
return !result;
}
};
namespace dictionary_helper {
namespace {
SpellcheckCustomDictionary* GetDictionary(int index) {
return SpellcheckServiceFactory::GetForContext(
sync_datatype_helper::test()->GetProfile(index))
->GetCustomDictionary();
}
void LoadDictionary(SpellcheckCustomDictionary* dictionary) {
if (dictionary->IsLoaded()) {
return;
}
base::RunLoop run_loop;
DictionaryLoadObserver observer(
content::GetDeferredQuitTaskForRunLoop(&run_loop));
dictionary->AddObserver(&observer);
dictionary->Load();
run_loop.Run();
dictionary->RemoveObserver(&observer);
ASSERT_TRUE(dictionary->IsLoaded());
}
} // namespace
const std::set<std::string>& GetDictionaryWords(int profile_index) {
return GetDictionary(profile_index)->GetWords();
}
void LoadDictionaries() {
for (int i = 0; i < sync_datatype_helper::test()->num_clients(); ++i) {
LoadDictionary(GetDictionary(i));
}
}
size_t GetDictionarySize(int index) {
return GetDictionary(index)->GetWords().size();
}
bool AddWord(int index, const std::string& word) {
SpellcheckCustomDictionary::Change dictionary_change;
dictionary_change.AddWord(word);
bool result = DictionarySyncIntegrationTestHelper::ApplyChange(
GetDictionary(index), &dictionary_change);
return result;
}
bool AddWords(int index, int n, const std::string& prefix) {
bool result = true;
for (int i = 0; i < n; ++i) {
result &= AddWord(index, prefix + base::NumberToString(i));
}
return result;
}
bool RemoveWord(int index, const std::string& word) {
SpellcheckCustomDictionary::Change dictionary_change;
dictionary_change.RemoveWord(word);
bool result = DictionarySyncIntegrationTestHelper::ApplyChange(
GetDictionary(index), &dictionary_change);
return result;
}
DictionaryChecker::DictionaryChecker(
const std::vector<std::string>& expected_words)
: MultiClientStatusChangeChecker(
sync_datatype_helper::test()->GetSyncServices()),
expected_words_(expected_words.begin(), expected_words.end()) {}
DictionaryChecker::~DictionaryChecker() = default;
bool DictionaryChecker::IsExitConditionSatisfied(std::ostream* os) {
*os << "Waiting for matching dictionaries";
for (int i = 0; i < sync_datatype_helper::test()->num_clients(); ++i) {
if (GetDictionaryWords(/*profile_index=*/i) != expected_words_) {
return false;
}
}
return true;
}
NumDictionaryEntriesChecker::NumDictionaryEntriesChecker(int index,
size_t num_words)
: SingleClientStatusChangeChecker(
sync_datatype_helper::test()->GetSyncService(index)),
index_(index),
num_words_(num_words) {}
bool NumDictionaryEntriesChecker::IsExitConditionSatisfied(std::ostream* os) {
size_t actual_size = GetDictionarySize(index_);
*os << "Waiting for client " << index_ << ": " << actual_size << " / "
<< num_words_ << " words downloaded";
return actual_size == num_words_;
}
} // namespace dictionary_helper
|