Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / components / sync_driver / shared_change_processor_unittest.cc
1 // Copyright 2014 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.
4
5 #include "components/sync_driver/shared_change_processor.h"
6
7 #include <cstddef>
8
9 #include "base/bind.h"
10 #include "base/bind_helpers.h"
11 #include "base/compiler_specific.h"
12 #include "base/message_loop/message_loop.h"
13 #include "base/threading/thread.h"
14 #include "components/sync_driver/data_type_error_handler_mock.h"
15 #include "components/sync_driver/generic_change_processor.h"
16 #include "components/sync_driver/generic_change_processor_factory.h"
17 #include "components/sync_driver/sync_api_component_factory.h"
18 #include "sync/api/fake_syncable_service.h"
19 #include "sync/internal_api/public/attachments/attachment_service_impl.h"
20 #include "testing/gmock/include/gmock/gmock.h"
21 #include "testing/gtest/include/gtest/gtest.h"
22
23 namespace sync_driver {
24
25 namespace {
26
27 using ::testing::NiceMock;
28 using ::testing::StrictMock;
29
30 class SyncSharedChangeProcessorTest :
31     public testing::Test,
32     public SyncApiComponentFactory {
33  public:
34   SyncSharedChangeProcessorTest() : backend_thread_("dbthread"),
35                                     did_connect_(false) {}
36
37   virtual ~SyncSharedChangeProcessorTest() {
38     EXPECT_FALSE(db_syncable_service_.get());
39   }
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       const syncer::UserShare& user_share,
48       syncer::AttachmentService::Delegate* delegate) OVERRIDE {
49     return syncer::AttachmentServiceImpl::CreateForTest();
50   }
51
52  protected:
53   virtual void SetUp() OVERRIDE {
54     shared_change_processor_ = new SharedChangeProcessor();
55     ASSERT_TRUE(backend_thread_.Start());
56     ASSERT_TRUE(backend_thread_.message_loop_proxy()->PostTask(
57         FROM_HERE,
58         base::Bind(&SyncSharedChangeProcessorTest::SetUpDBSyncableService,
59                    base::Unretained(this))));
60   }
61
62   virtual void TearDown() OVERRIDE {
63     EXPECT_TRUE(backend_thread_.message_loop_proxy()->PostTask(
64         FROM_HERE,
65         base::Bind(&SyncSharedChangeProcessorTest::TearDownDBSyncableService,
66                    base::Unretained(this))));
67     // This must happen before the DB thread is stopped since
68     // |shared_change_processor_| may post tasks to delete its members
69     // on the correct thread.
70     //
71     // TODO(akalin): Write deterministic tests for the destruction of
72     // |shared_change_processor_| on the UI and DB threads.
73     shared_change_processor_ = NULL;
74     backend_thread_.Stop();
75
76     // Note: Stop() joins the threads, and that barrier prevents this read
77     // from being moved (e.g by compiler optimization) in such a way that it
78     // would race with the write in ConnectOnDBThread (because by this time,
79     // everything that could have run on |backend_thread_| has done so).
80     ASSERT_TRUE(did_connect_);
81   }
82
83   // Connect |shared_change_processor_| on the DB thread.
84   void Connect() {
85     EXPECT_TRUE(backend_thread_.message_loop_proxy()->PostTask(
86         FROM_HERE,
87         base::Bind(&SyncSharedChangeProcessorTest::ConnectOnDBThread,
88                    base::Unretained(this),
89                    shared_change_processor_)));
90   }
91
92  private:
93   // Used by SetUp().
94   void SetUpDBSyncableService() {
95     DCHECK(backend_thread_.message_loop_proxy()->BelongsToCurrentThread());
96     DCHECK(!db_syncable_service_.get());
97     db_syncable_service_.reset(new syncer::FakeSyncableService());
98   }
99
100   // Used by TearDown().
101   void TearDownDBSyncableService() {
102     DCHECK(backend_thread_.message_loop_proxy()->BelongsToCurrentThread());
103     DCHECK(db_syncable_service_.get());
104     db_syncable_service_.reset();
105   }
106
107   // Used by Connect().  The SharedChangeProcessor is passed in
108   // because we modify |shared_change_processor_| on the main thread
109   // (in TearDown()).
110   void ConnectOnDBThread(
111       const scoped_refptr<SharedChangeProcessor>& shared_change_processor) {
112     DCHECK(backend_thread_.message_loop_proxy()->BelongsToCurrentThread());
113     syncer::UserShare share;
114     EXPECT_TRUE(shared_change_processor->Connect(
115         this,
116         &processor_factory_,
117         &share,
118         &error_handler_,
119         syncer::AUTOFILL,
120         base::WeakPtr<syncer::SyncMergeResult>()));
121     did_connect_ = true;
122   }
123
124   base::MessageLoop frontend_loop_;
125   base::Thread backend_thread_;
126
127   scoped_refptr<SharedChangeProcessor> shared_change_processor_;
128   StrictMock<DataTypeErrorHandlerMock> error_handler_;
129
130   GenericChangeProcessorFactory processor_factory_;
131   bool did_connect_;
132
133   // Used only on DB thread.
134   scoped_ptr<syncer::FakeSyncableService> db_syncable_service_;
135 };
136
137 // Simply connect the shared change processor.  It should succeed, and
138 // nothing further should happen.
139 TEST_F(SyncSharedChangeProcessorTest, Basic) {
140   Connect();
141 }
142
143 }  // namespace
144
145 }  // namespace sync_driver