OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/sync/glue/shared_change_processor.h" | 5 #include "components/sync_driver/shared_change_processor.h" |
6 | 6 |
7 #include <cstddef> | 7 #include <cstddef> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
11 #include "base/compiler_specific.h" | 11 #include "base/compiler_specific.h" |
12 #include "base/message_loop/message_loop.h" | 12 #include "base/message_loop/message_loop.h" |
13 #include "chrome/browser/sync/profile_sync_components_factory_impl.h" | 13 #include "base/threading/thread.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" | 14 #include "components/sync_driver/data_type_error_handler_mock.h" |
17 #include "components/sync_driver/generic_change_processor.h" | 15 #include "components/sync_driver/generic_change_processor.h" |
18 #include "components/sync_driver/generic_change_processor_factory.h" | 16 #include "components/sync_driver/generic_change_processor_factory.h" |
19 #include "content/public/test/test_browser_thread.h" | 17 #include "components/sync_driver/sync_api_component_factory.h" |
18 #include "sync/api/attachments/attachment_service_impl.h" | |
20 #include "sync/api/fake_syncable_service.h" | 19 #include "sync/api/fake_syncable_service.h" |
21 #include "testing/gmock/include/gmock/gmock.h" | 20 #include "testing/gmock/include/gmock/gmock.h" |
22 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
23 | 22 |
24 namespace browser_sync { | 23 namespace browser_sync { |
25 | 24 |
26 namespace { | 25 namespace { |
27 | 26 |
28 using content::BrowserThread; | |
29 using ::testing::NiceMock; | 27 using ::testing::NiceMock; |
30 using ::testing::StrictMock; | 28 using ::testing::StrictMock; |
31 | 29 |
32 ACTION_P(GetWeakPtrToSyncableService, syncable_service) { | 30 class SyncSharedChangeProcessorTest : |
33 // Have to do this within an Action to ensure it's not evaluated on the wrong | 31 public testing::Test, |
34 // thread. | 32 public browser_sync::SyncApiComponentFactory { |
35 return syncable_service->AsWeakPtr(); | |
36 } | |
37 | |
38 class SyncSharedChangeProcessorTest : public testing::Test { | |
39 public: | 33 public: |
40 SyncSharedChangeProcessorTest() | 34 SyncSharedChangeProcessorTest() : backend_thread_("dbthread"), |
41 : ui_thread_(BrowserThread::UI, &ui_loop_), | 35 did_connect_(false) {} |
42 db_thread_(BrowserThread::DB), | |
43 sync_service_(&profile_) {} | |
44 | 36 |
45 virtual ~SyncSharedChangeProcessorTest() { | 37 virtual ~SyncSharedChangeProcessorTest() { |
46 EXPECT_FALSE(db_syncable_service_.get()); | 38 EXPECT_FALSE(db_syncable_service_.get()); |
47 } | 39 } |
48 | 40 |
41 virtual base::WeakPtr<syncer::SyncableService> GetSyncableServiceForType( | |
42 syncer::ModelType type) OVERRIDE { | |
43 return db_syncable_service_->AsWeakPtr(); | |
44 } | |
45 | |
46 virtual scoped_ptr<syncer::AttachmentService> CreateAttachmentService( | |
47 syncer::AttachmentService::Delegate* delegate) { | |
maniscalco
2014/05/29 23:04:43
OVERRIDE?
tim (not reviewing)
2014/05/30 17:43:07
Done.
| |
48 return syncer::AttachmentServiceImpl::CreateForTest(); | |
49 } | |
50 | |
49 protected: | 51 protected: |
50 virtual void SetUp() OVERRIDE { | 52 virtual void SetUp() OVERRIDE { |
51 shared_change_processor_ = new SharedChangeProcessor(); | 53 shared_change_processor_ = new SharedChangeProcessor(); |
52 db_thread_.Start(); | 54 ASSERT_TRUE(backend_thread_.Start()); |
53 EXPECT_TRUE(BrowserThread::PostTask( | 55 ASSERT_TRUE(backend_thread_.message_loop_proxy()->PostTask( |
54 BrowserThread::DB, | |
55 FROM_HERE, | 56 FROM_HERE, |
56 base::Bind(&SyncSharedChangeProcessorTest::SetUpDBSyncableService, | 57 base::Bind(&SyncSharedChangeProcessorTest::SetUpDBSyncableService, |
57 base::Unretained(this)))); | 58 base::Unretained(this)))); |
58 } | 59 } |
59 | 60 |
60 virtual void TearDown() OVERRIDE { | 61 virtual void TearDown() OVERRIDE { |
61 EXPECT_TRUE(BrowserThread::PostTask( | 62 EXPECT_TRUE(backend_thread_.message_loop_proxy()->PostTask( |
62 BrowserThread::DB, | |
63 FROM_HERE, | 63 FROM_HERE, |
64 base::Bind(&SyncSharedChangeProcessorTest::TearDownDBSyncableService, | 64 base::Bind(&SyncSharedChangeProcessorTest::TearDownDBSyncableService, |
65 base::Unretained(this)))); | 65 base::Unretained(this)))); |
66 // This must happen before the DB thread is stopped since | 66 // This must happen before the DB thread is stopped since |
67 // |shared_change_processor_| may post tasks to delete its members | 67 // |shared_change_processor_| may post tasks to delete its members |
68 // on the correct thread. | 68 // on the correct thread. |
69 // | 69 // |
70 // TODO(akalin): Write deterministic tests for the destruction of | 70 // TODO(akalin): Write deterministic tests for the destruction of |
71 // |shared_change_processor_| on the UI and DB threads. | 71 // |shared_change_processor_| on the UI and DB threads. |
72 shared_change_processor_ = NULL; | 72 shared_change_processor_ = NULL; |
73 db_thread_.Stop(); | 73 backend_thread_.Stop(); |
74 | |
75 // Note: Stop() joins the threads, and that barrier prevents this read | |
76 // from being moved (e.g by compiler optimization) in such a way that it | |
77 // would race with the write in ConnectOnDBThread (because by this time, | |
78 // everything that could have run on |backend_thread_| has done so). | |
79 ASSERT_TRUE(did_connect_); | |
74 } | 80 } |
75 | 81 |
76 // Connect |shared_change_processor_| on the DB thread. | 82 // Connect |shared_change_processor_| on the DB thread. |
77 void Connect() { | 83 void Connect() { |
78 EXPECT_TRUE(BrowserThread::PostTask( | 84 EXPECT_TRUE(backend_thread_.message_loop_proxy()->PostTask( |
79 BrowserThread::DB, | |
80 FROM_HERE, | 85 FROM_HERE, |
81 base::Bind(&SyncSharedChangeProcessorTest::ConnectOnDBThread, | 86 base::Bind(&SyncSharedChangeProcessorTest::ConnectOnDBThread, |
82 base::Unretained(this), | 87 base::Unretained(this), |
83 shared_change_processor_))); | 88 shared_change_processor_))); |
84 } | 89 } |
85 | 90 |
86 private: | 91 private: |
87 // Used by SetUp(). | 92 // Used by SetUp(). |
88 void SetUpDBSyncableService() { | 93 void SetUpDBSyncableService() { |
89 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); | 94 DCHECK(backend_thread_.message_loop_proxy()->BelongsToCurrentThread()); |
90 DCHECK(!db_syncable_service_.get()); | 95 DCHECK(!db_syncable_service_.get()); |
91 db_syncable_service_.reset(new syncer::FakeSyncableService()); | 96 db_syncable_service_.reset(new syncer::FakeSyncableService()); |
92 } | 97 } |
93 | 98 |
94 // Used by TearDown(). | 99 // Used by TearDown(). |
95 void TearDownDBSyncableService() { | 100 void TearDownDBSyncableService() { |
96 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); | 101 DCHECK(backend_thread_.message_loop_proxy()->BelongsToCurrentThread()); |
97 DCHECK(db_syncable_service_.get()); | 102 DCHECK(db_syncable_service_.get()); |
98 db_syncable_service_.reset(); | 103 db_syncable_service_.reset(); |
99 } | 104 } |
100 | 105 |
101 // Used by Connect(). The SharedChangeProcessor is passed in | 106 // Used by Connect(). The SharedChangeProcessor is passed in |
102 // because we modify |shared_change_processor_| on the main thread | 107 // because we modify |shared_change_processor_| on the main thread |
103 // (in TearDown()). | 108 // (in TearDown()). |
104 void ConnectOnDBThread( | 109 void ConnectOnDBThread( |
105 const scoped_refptr<SharedChangeProcessor>& shared_change_processor) { | 110 const scoped_refptr<SharedChangeProcessor>& shared_change_processor) { |
106 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); | 111 DCHECK(backend_thread_.message_loop_proxy()->BelongsToCurrentThread()); |
107 EXPECT_CALL(sync_factory_, GetSyncableServiceForType(syncer::AUTOFILL)). | |
108 WillOnce(GetWeakPtrToSyncableService(db_syncable_service_.get())); | |
109 syncer::UserShare share; | 112 syncer::UserShare share; |
110 EXPECT_CALL(sync_service_, GetUserShare()).WillOnce( | |
111 ::testing::Return(&share)); | |
112 EXPECT_TRUE(shared_change_processor->Connect( | 113 EXPECT_TRUE(shared_change_processor->Connect( |
113 &sync_factory_, | 114 this, |
114 &processor_factory_, | 115 &processor_factory_, |
115 sync_service_.GetUserShare(), | 116 &share, |
116 &error_handler_, | 117 &error_handler_, |
117 syncer::AUTOFILL, | 118 syncer::AUTOFILL, |
118 base::WeakPtr<syncer::SyncMergeResult>())); | 119 base::WeakPtr<syncer::SyncMergeResult>())); |
120 did_connect_ = true; | |
119 } | 121 } |
120 | 122 |
121 base::MessageLoopForUI ui_loop_; | 123 base::MessageLoop frontend_loop_; |
122 content::TestBrowserThread ui_thread_; | 124 base::Thread backend_thread_; |
123 content::TestBrowserThread db_thread_; | |
124 | 125 |
125 scoped_refptr<SharedChangeProcessor> shared_change_processor_; | 126 scoped_refptr<SharedChangeProcessor> shared_change_processor_; |
126 NiceMock<ProfileSyncComponentsFactoryMock> sync_factory_; | |
127 TestingProfile profile_; | |
128 NiceMock<ProfileSyncServiceMock> sync_service_; | |
129 StrictMock<DataTypeErrorHandlerMock> error_handler_; | 127 StrictMock<DataTypeErrorHandlerMock> error_handler_; |
130 | 128 |
131 GenericChangeProcessorFactory processor_factory_; | 129 GenericChangeProcessorFactory processor_factory_; |
130 bool did_connect_; | |
132 | 131 |
133 // Used only on DB thread. | 132 // Used only on DB thread. |
134 scoped_ptr<syncer::FakeSyncableService> db_syncable_service_; | 133 scoped_ptr<syncer::FakeSyncableService> db_syncable_service_; |
135 }; | 134 }; |
136 | 135 |
137 // Simply connect the shared change processor. It should succeed, and | 136 // Simply connect the shared change processor. It should succeed, and |
138 // nothing further should happen. | 137 // nothing further should happen. |
139 TEST_F(SyncSharedChangeProcessorTest, Basic) { | 138 TEST_F(SyncSharedChangeProcessorTest, Basic) { |
140 Connect(); | 139 Connect(); |
141 } | 140 } |
142 | 141 |
143 } // namespace | 142 } // namespace |
144 | 143 |
145 } // namespace browser_sync | 144 } // namespace browser_sync |
OLD | NEW |