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/glue/data_type_error_handler_mock.h"
14 #include "chrome/browser/sync/profile_sync_components_factory_impl.h"
15 #include "chrome/browser/sync/profile_sync_components_factory_mock.h"
16 #include "chrome/browser/sync/profile_sync_service_mock.h"
17 #include "content/public/test/test_browser_thread.h"
18 #include "sync/api/fake_syncable_service.h"
19 #include "testing/gmock/include/gmock/gmock.h"
20 #include "testing/gtest/include/gtest/gtest.h"
22 namespace browser_sync
{
26 using content::BrowserThread
;
27 using ::testing::NiceMock
;
28 using ::testing::StrictMock
;
30 ACTION_P(GetWeakPtrToSyncableService
, syncable_service
) {
31 // Have to do this within an Action to ensure it's not evaluated on the wrong
33 return syncable_service
->AsWeakPtr();
36 class SyncSharedChangeProcessorTest
: public testing::Test
{
38 SyncSharedChangeProcessorTest()
39 : ui_thread_(BrowserThread::UI
, &ui_loop_
),
40 db_thread_(BrowserThread::DB
),
41 sync_service_(&profile_
) {}
43 virtual ~SyncSharedChangeProcessorTest() {
44 EXPECT_FALSE(db_syncable_service_
.get());
48 virtual void SetUp() OVERRIDE
{
49 shared_change_processor_
= new SharedChangeProcessor();
51 EXPECT_TRUE(BrowserThread::PostTask(
54 base::Bind(&SyncSharedChangeProcessorTest::SetUpDBSyncableService
,
55 base::Unretained(this))));
58 virtual void TearDown() OVERRIDE
{
59 EXPECT_TRUE(BrowserThread::PostTask(
62 base::Bind(&SyncSharedChangeProcessorTest::TearDownDBSyncableService
,
63 base::Unretained(this))));
64 // This must happen before the DB thread is stopped since
65 // |shared_change_processor_| may post tasks to delete its members
66 // on the correct thread.
68 // TODO(akalin): Write deterministic tests for the destruction of
69 // |shared_change_processor_| on the UI and DB threads.
70 shared_change_processor_
= NULL
;
74 // Connect |shared_change_processor_| on the DB thread.
76 EXPECT_TRUE(BrowserThread::PostTask(
79 base::Bind(&SyncSharedChangeProcessorTest::ConnectOnDBThread
,
80 base::Unretained(this),
81 shared_change_processor_
)));
86 void SetUpDBSyncableService() {
87 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB
));
88 DCHECK(!db_syncable_service_
.get());
89 db_syncable_service_
.reset(new syncer::FakeSyncableService());
92 // Used by TearDown().
93 void TearDownDBSyncableService() {
94 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB
));
95 DCHECK(db_syncable_service_
.get());
96 db_syncable_service_
.reset();
99 // Used by Connect(). The SharedChangeProcessor is passed in
100 // because we modify |shared_change_processor_| on the main thread
102 void ConnectOnDBThread(
103 const scoped_refptr
<SharedChangeProcessor
>& shared_change_processor
) {
104 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB
));
105 EXPECT_CALL(sync_factory_
, GetSyncableServiceForType(syncer::AUTOFILL
)).
106 WillOnce(GetWeakPtrToSyncableService(db_syncable_service_
.get()));
107 EXPECT_TRUE(shared_change_processor
->Connect(
112 base::WeakPtr
<syncer::SyncMergeResult
>()));
115 base::MessageLoopForUI ui_loop_
;
116 content::TestBrowserThread ui_thread_
;
117 content::TestBrowserThread db_thread_
;
119 scoped_refptr
<SharedChangeProcessor
> shared_change_processor_
;
120 NiceMock
<ProfileSyncComponentsFactoryMock
> sync_factory_
;
121 TestingProfile profile_
;
122 NiceMock
<ProfileSyncServiceMock
> sync_service_
;
123 StrictMock
<DataTypeErrorHandlerMock
> error_handler_
;
125 // Used only on DB thread.
126 scoped_ptr
<syncer::FakeSyncableService
> db_syncable_service_
;
129 // Simply connect the shared change processor. It should succeed, and
130 // nothing further should happen.
131 TEST_F(SyncSharedChangeProcessorTest
, Basic
) {
137 } // namespace browser_sync