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 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/sync/glue/sync_backend_registrar.h"
#include "base/run_loop.h"
#include "chrome/browser/sync/glue/ui_model_worker.h"
#include "chrome/test/base/testing_profile.h"
#include "components/sync_driver/change_processor_mock.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/test/test_browser_thread_bundle.h"
#include "sync/internal_api/public/base/model_type.h"
#include "sync/internal_api/public/test/test_user_share.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace browser_sync {
namespace {
using ::testing::_;
using ::testing::InSequence;
using ::testing::Return;
using ::testing::StrictMock;
using content::BrowserThread;
using syncer::FIRST_REAL_MODEL_TYPE;
using syncer::AUTOFILL;
using syncer::BOOKMARKS;
using syncer::PREFERENCES;
using syncer::THEMES;
using syncer::NIGORI;
using syncer::PASSWORDS;
using syncer::MODEL_TYPE_COUNT;
using syncer::ModelTypeSet;
using syncer::ModelType;
using syncer::ModelTypeFromInt;
void TriggerChanges(SyncBackendRegistrar* registrar, ModelType type) {
registrar->OnChangesApplied(type, 0, NULL,
syncer::ImmutableChangeRecordList());
registrar->OnChangesComplete(type);
}
class SyncBackendRegistrarTest : public testing::Test {
public:
void TestNonUIDataTypeActivationAsync(sync_driver::ChangeProcessor* processor,
base::WaitableEvent* done) {
registrar_->ActivateDataType(AUTOFILL,
syncer::GROUP_DB,
processor,
test_user_share_.user_share());
syncer::ModelSafeRoutingInfo expected_routing_info;
expected_routing_info[AUTOFILL] = syncer::GROUP_DB;
ExpectRoutingInfo(registrar_.get(), expected_routing_info);
ExpectHasProcessorsForTypes(*registrar_, ModelTypeSet(AUTOFILL));
TriggerChanges(registrar_.get(), AUTOFILL);
done->Signal();
}
protected:
SyncBackendRegistrarTest()
: sync_thread_(NULL),
thread_bundle_(content::TestBrowserThreadBundle::REAL_DB_THREAD |
content::TestBrowserThreadBundle::REAL_FILE_THREAD |
content::TestBrowserThreadBundle::REAL_IO_THREAD) {}
~SyncBackendRegistrarTest() override {}
void SetUp() override {
test_user_share_.SetUp();
registrar_.reset(new SyncBackendRegistrar("test", &profile_,
scoped_ptr<base::Thread>()));
sync_thread_ = registrar_->sync_thread();
}
void TearDown() override {
registrar_->RequestWorkerStopOnUIThread();
test_user_share_.TearDown();
sync_thread_->message_loop()->PostTask(
FROM_HERE,
base::Bind(&SyncBackendRegistrar::Shutdown,
base::Unretained(registrar_.release())));
sync_thread_->message_loop()->RunUntilIdle();
}
void ExpectRoutingInfo(
SyncBackendRegistrar* registrar,
const syncer::ModelSafeRoutingInfo& expected_routing_info) {
syncer::ModelSafeRoutingInfo routing_info;
registrar->GetModelSafeRoutingInfo(&routing_info);
EXPECT_EQ(expected_routing_info, routing_info);
}
void ExpectHasProcessorsForTypes(const SyncBackendRegistrar& registrar,
ModelTypeSet types) {
for (int i = FIRST_REAL_MODEL_TYPE; i < MODEL_TYPE_COUNT; ++i) {
ModelType model_type = ModelTypeFromInt(i);
EXPECT_EQ(types.Has(model_type),
registrar_->IsTypeActivatedForTest(model_type));
}
}
syncer::TestUserShare test_user_share_;
TestingProfile profile_;
scoped_ptr<SyncBackendRegistrar> registrar_;
base::Thread* sync_thread_;
content::TestBrowserThreadBundle thread_bundle_;
};
TEST_F(SyncBackendRegistrarTest, ConstructorEmpty) {
registrar_->SetInitialTypes(ModelTypeSet());
EXPECT_FALSE(registrar_->IsNigoriEnabled());
{
std::vector<scoped_refptr<syncer::ModelSafeWorker> > workers;
registrar_->GetWorkers(&workers);
EXPECT_EQ(4u, workers.size());
}
ExpectRoutingInfo(registrar_.get(), syncer::ModelSafeRoutingInfo());
ExpectHasProcessorsForTypes(*registrar_, ModelTypeSet());
}
TEST_F(SyncBackendRegistrarTest, ConstructorNonEmpty) {
const ModelTypeSet initial_types(BOOKMARKS, NIGORI, PASSWORDS);
registrar_->SetInitialTypes(initial_types);
EXPECT_TRUE(registrar_->IsNigoriEnabled());
{
std::vector<scoped_refptr<syncer::ModelSafeWorker> > workers;
registrar_->GetWorkers(&workers);
EXPECT_EQ(4u, workers.size());
}
{
syncer::ModelSafeRoutingInfo expected_routing_info;
expected_routing_info[BOOKMARKS] = syncer::GROUP_PASSIVE;
expected_routing_info[NIGORI] = syncer::GROUP_PASSIVE;
// Passwords dropped because of no password store.
ExpectRoutingInfo(registrar_.get(), expected_routing_info);
}
ExpectHasProcessorsForTypes(*registrar_, ModelTypeSet());
}
TEST_F(SyncBackendRegistrarTest, ConfigureDataTypes) {
registrar_->SetInitialTypes(ModelTypeSet());
// Add.
const ModelTypeSet types1(BOOKMARKS, NIGORI, AUTOFILL);
EXPECT_TRUE(
registrar_->ConfigureDataTypes(types1, ModelTypeSet()).Equals(types1));
{
syncer::ModelSafeRoutingInfo expected_routing_info;
expected_routing_info[BOOKMARKS] = syncer::GROUP_PASSIVE;
expected_routing_info[NIGORI] = syncer::GROUP_PASSIVE;
expected_routing_info[AUTOFILL] = syncer::GROUP_PASSIVE;
ExpectRoutingInfo(registrar_.get(), expected_routing_info);
}
ExpectHasProcessorsForTypes(*registrar_, ModelTypeSet());
EXPECT_TRUE(types1.Equals(registrar_->GetLastConfiguredTypes()));
// Add and remove.
const ModelTypeSet types2(PREFERENCES, THEMES);
EXPECT_TRUE(registrar_->ConfigureDataTypes(types2, types1).Equals(types2));
{
syncer::ModelSafeRoutingInfo expected_routing_info;
expected_routing_info[PREFERENCES] = syncer::GROUP_PASSIVE;
expected_routing_info[THEMES] = syncer::GROUP_PASSIVE;
ExpectRoutingInfo(registrar_.get(), expected_routing_info);
}
ExpectHasProcessorsForTypes(*registrar_, ModelTypeSet());
EXPECT_TRUE(types2.Equals(registrar_->GetLastConfiguredTypes()));
// Remove.
EXPECT_TRUE(registrar_->ConfigureDataTypes(ModelTypeSet(), types2).Empty());
ExpectRoutingInfo(registrar_.get(), syncer::ModelSafeRoutingInfo());
ExpectHasProcessorsForTypes(*registrar_, ModelTypeSet());
EXPECT_TRUE(ModelTypeSet().Equals(registrar_->GetLastConfiguredTypes()));
}
TEST_F(SyncBackendRegistrarTest, ActivateDeactivateUIDataType) {
InSequence in_sequence;
registrar_->SetInitialTypes(ModelTypeSet());
// Should do nothing.
TriggerChanges(registrar_.get(), BOOKMARKS);
StrictMock<sync_driver::ChangeProcessorMock> change_processor_mock;
EXPECT_CALL(change_processor_mock, StartImpl());
EXPECT_CALL(change_processor_mock, IsRunning())
.WillRepeatedly(Return(true));
EXPECT_CALL(change_processor_mock, ApplyChangesFromSyncModel(NULL, _, _));
EXPECT_CALL(change_processor_mock, IsRunning())
.WillRepeatedly(Return(true));
EXPECT_CALL(change_processor_mock, CommitChangesFromSyncModel());
EXPECT_CALL(change_processor_mock, IsRunning())
.WillRepeatedly(Return(false));
const ModelTypeSet types(BOOKMARKS);
EXPECT_TRUE(
registrar_->ConfigureDataTypes(types, ModelTypeSet()).Equals(types));
registrar_->ActivateDataType(BOOKMARKS, syncer::GROUP_UI,
&change_processor_mock,
test_user_share_.user_share());
{
syncer::ModelSafeRoutingInfo expected_routing_info;
expected_routing_info[BOOKMARKS] = syncer::GROUP_UI;
ExpectRoutingInfo(registrar_.get(), expected_routing_info);
}
ExpectHasProcessorsForTypes(*registrar_, types);
TriggerChanges(registrar_.get(), BOOKMARKS);
registrar_->DeactivateDataType(BOOKMARKS);
ExpectRoutingInfo(registrar_.get(), syncer::ModelSafeRoutingInfo());
ExpectHasProcessorsForTypes(*registrar_, ModelTypeSet());
// Should do nothing.
TriggerChanges(registrar_.get(), BOOKMARKS);
}
TEST_F(SyncBackendRegistrarTest, ActivateDeactivateNonUIDataType) {
InSequence in_sequence;
registrar_->SetInitialTypes(ModelTypeSet());
// Should do nothing.
TriggerChanges(registrar_.get(), AUTOFILL);
StrictMock<sync_driver::ChangeProcessorMock> change_processor_mock;
EXPECT_CALL(change_processor_mock, StartImpl());
EXPECT_CALL(change_processor_mock, IsRunning())
.WillRepeatedly(Return(true));
EXPECT_CALL(change_processor_mock, ApplyChangesFromSyncModel(NULL, _, _));
EXPECT_CALL(change_processor_mock, IsRunning())
.WillRepeatedly(Return(true));
EXPECT_CALL(change_processor_mock, CommitChangesFromSyncModel());
EXPECT_CALL(change_processor_mock, IsRunning())
.WillRepeatedly(Return(false));
const ModelTypeSet types(AUTOFILL);
EXPECT_TRUE(
registrar_->ConfigureDataTypes(types, ModelTypeSet()).Equals(types));
base::WaitableEvent done(false, false);
BrowserThread::PostTask(
BrowserThread::DB,
FROM_HERE,
base::Bind(&SyncBackendRegistrarTest::TestNonUIDataTypeActivationAsync,
base::Unretained(this),
&change_processor_mock,
&done));
done.Wait();
registrar_->DeactivateDataType(AUTOFILL);
ExpectRoutingInfo(registrar_.get(), syncer::ModelSafeRoutingInfo());
ExpectHasProcessorsForTypes(*registrar_, ModelTypeSet());
// Should do nothing.
TriggerChanges(registrar_.get(), AUTOFILL);
}
class SyncBackendRegistrarShutdownTest : public testing::Test {
public:
void BlockDBThread() {
EXPECT_FALSE(db_thread_lock_.Try());
db_thread_blocked_.Signal();
base::AutoLock l(db_thread_lock_);
}
protected:
friend class TestRegistrar;
SyncBackendRegistrarShutdownTest()
: thread_bundle_(content::TestBrowserThreadBundle::REAL_DB_THREAD |
content::TestBrowserThreadBundle::REAL_FILE_THREAD |
content::TestBrowserThreadBundle::REAL_IO_THREAD),
db_thread_blocked_(false, false) {
quit_closure_ = run_loop_.QuitClosure();
}
~SyncBackendRegistrarShutdownTest() override {}
void PostQuitOnUIMessageLoop() {
BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, quit_closure_);
}
content::TestBrowserThreadBundle thread_bundle_;
TestingProfile profile_;
base::WaitableEvent db_thread_blocked_;
base::Lock db_thread_lock_;
base::RunLoop run_loop_;
base::Closure quit_closure_;
};
// Wrap SyncBackendRegistrar so that we can monitor its lifetime.
class TestRegistrar : public SyncBackendRegistrar {
public:
explicit TestRegistrar(Profile* profile,
SyncBackendRegistrarShutdownTest* test)
: SyncBackendRegistrar("test", profile, scoped_ptr<base::Thread>()),
test_(test) {}
~TestRegistrar() override { test_->PostQuitOnUIMessageLoop(); }
private:
SyncBackendRegistrarShutdownTest* test_;
};
TEST_F(SyncBackendRegistrarShutdownTest, BlockingShutdown) {
// Take ownership of |db_thread_lock_| so that the DB thread can't acquire it.
db_thread_lock_.Acquire();
// This will block the DB thread by waiting on |db_thread_lock_|.
BrowserThread::PostTask(
BrowserThread::DB,
FROM_HERE,
base::Bind(&SyncBackendRegistrarShutdownTest::BlockDBThread,
base::Unretained(this)));
scoped_ptr<TestRegistrar> registrar(new TestRegistrar(&profile_, this));
base::Thread* sync_thread = registrar->sync_thread();
// Stop here until the DB thread gets a chance to run and block on the lock.
// Please note that since the task above didn't finish, the task to
// initialize the worker on the DB thread hasn't had a chance to run yet too.
// Which means ModelSafeWorker::SetWorkingLoopToCurrent hasn't been called
// for the DB worker.
db_thread_blocked_.Wait();
registrar->SetInitialTypes(ModelTypeSet());
// Start the shutdown.
registrar->RequestWorkerStopOnUIThread();
sync_thread->message_loop()->PostTask(
FROM_HERE,
base::Bind(&SyncBackendRegistrar::Shutdown,
base::Unretained(registrar.release())));
// The test verifies that the sync thread doesn't block because
// of the blocked DB thread and can finish the shutdown.
sync_thread->message_loop()->RunUntilIdle();
db_thread_lock_.Release();
// Run the main thread loop until all workers have been removed and the
// registrar destroyed.
run_loop_.Run();
}
} // namespace
} // namespace browser_sync
|