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 EXPECT_TRUE(shared_change_processor
->Connect(
115 base::WeakPtr
<syncer::SyncMergeResult
>()));
118 base::MessageLoopForUI ui_loop_
;
119 content::TestBrowserThread ui_thread_
;
120 content::TestBrowserThread db_thread_
;
122 scoped_refptr
<SharedChangeProcessor
> shared_change_processor_
;
123 NiceMock
<ProfileSyncComponentsFactoryMock
> sync_factory_
;
124 TestingProfile profile_
;
125 NiceMock
<ProfileSyncServiceMock
> sync_service_
;
126 StrictMock
<DataTypeErrorHandlerMock
> error_handler_
;
128 GenericChangeProcessorFactory processor_factory_
;
130 // Used only on DB thread.
131 scoped_ptr
<syncer::FakeSyncableService
> db_syncable_service_
;
134 // Simply connect the shared change processor. It should succeed, and
135 // nothing further should happen.
136 TEST_F(SyncSharedChangeProcessorTest
, Basic
) {
142 } // namespace browser_sync