1 // Copyright (c) 2012 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 "chrome/browser/sync/glue/shared_change_processor.h"
10 #include "base/bind_helpers.h"
11 #include "base/compiler_specific.h"
12 #include "base/message_loop/message_loop.h"
13 #include "chrome/browser/sync/profile_sync_components_factory_impl.h"
14 #include "chrome/browser/sync/profile_sync_components_factory_mock.h"
15 #include "chrome/browser/sync/profile_sync_service_mock.h"
16 #include "components/sync_driver/data_type_error_handler_mock.h"
17 #include "components/sync_driver/generic_change_processor.h"
18 #include "components/sync_driver/generic_change_processor_factory.h"
19 #include "content/public/test/test_browser_thread.h"
20 #include "sync/api/fake_syncable_service.h"
21 #include "testing/gmock/include/gmock/gmock.h"
22 #include "testing/gtest/include/gtest/gtest.h"
24 namespace browser_sync {
28 using content::BrowserThread;
29 using ::testing::NiceMock;
30 using ::testing::StrictMock;
32 ACTION_P(GetWeakPtrToSyncableService, syncable_service) {
33 // Have to do this within an Action to ensure it's not evaluated on the wrong
35 return syncable_service->AsWeakPtr();
38 class SyncSharedChangeProcessorTest : public testing::Test {
40 SyncSharedChangeProcessorTest()
41 : ui_thread_(BrowserThread::UI, &ui_loop_),
42 db_thread_(BrowserThread::DB),
43 sync_service_(&profile_) {}
45 virtual ~SyncSharedChangeProcessorTest() {
46 EXPECT_FALSE(db_syncable_service_.get());
50 virtual void SetUp() OVERRIDE {
51 shared_change_processor_ = new SharedChangeProcessor();
53 EXPECT_TRUE(BrowserThread::PostTask(
56 base::Bind(&SyncSharedChangeProcessorTest::SetUpDBSyncableService,
57 base::Unretained(this))));
60 virtual void TearDown() OVERRIDE {
61 EXPECT_TRUE(BrowserThread::PostTask(
64 base::Bind(&SyncSharedChangeProcessorTest::TearDownDBSyncableService,
65 base::Unretained(this))));
66 // This must happen before the DB thread is stopped since
67 // |shared_change_processor_| may post tasks to delete its members
68 // on the correct thread.
70 // TODO(akalin): Write deterministic tests for the destruction of
71 // |shared_change_processor_| on the UI and DB threads.
72 shared_change_processor_ = NULL;
76 // Connect |shared_change_processor_| on the DB thread.
78 EXPECT_TRUE(BrowserThread::PostTask(
81 base::Bind(&SyncSharedChangeProcessorTest::ConnectOnDBThread,
82 base::Unretained(this),
83 shared_change_processor_)));
88 void SetUpDBSyncableService() {
89 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB));
90 DCHECK(!db_syncable_service_.get());
91 db_syncable_service_.reset(new syncer::FakeSyncableService());
94 // Used by TearDown().
95 void TearDownDBSyncableService() {
96 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB));
97 DCHECK(db_syncable_service_.get());
98 db_syncable_service_.reset();
101 // Used by Connect(). The SharedChangeProcessor is passed in
102 // because we modify |shared_change_processor_| on the main thread
104 void ConnectOnDBThread(
105 const scoped_refptr<SharedChangeProcessor>& shared_change_processor) {
106 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB));
107 EXPECT_CALL(sync_factory_, GetSyncableServiceForType(syncer::AUTOFILL)).
108 WillOnce(GetWeakPtrToSyncableService(db_syncable_service_.get()));
109 syncer::UserShare share;
110 EXPECT_CALL(sync_service_, GetUserShare()).WillOnce(
111 ::testing::Return(&share));
112 EXPECT_TRUE(shared_change_processor->Connect(
115 sync_service_.GetUserShare(),
118 base::WeakPtr<syncer::SyncMergeResult>()));
121 base::MessageLoopForUI ui_loop_;
122 content::TestBrowserThread ui_thread_;
123 content::TestBrowserThread db_thread_;
125 scoped_refptr<SharedChangeProcessor> shared_change_processor_;
126 NiceMock<ProfileSyncComponentsFactoryMock> sync_factory_;
127 TestingProfile profile_;
128 NiceMock<ProfileSyncServiceMock> sync_service_;
129 StrictMock<DataTypeErrorHandlerMock> error_handler_;
131 GenericChangeProcessorFactory processor_factory_;
133 // Used only on DB thread.
134 scoped_ptr<syncer::FakeSyncableService> db_syncable_service_;
137 // Simply connect the shared change processor. It should succeed, and
138 // nothing further should happen.
139 TEST_F(SyncSharedChangeProcessorTest, Basic) {
145 } // namespace browser_sync