Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / sync / glue / shared_change_processor_unittest.cc
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.
4
5 #include "chrome/browser/sync/glue/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 "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"
23
24 namespace browser_sync {
25
26 namespace {
27
28 using content::BrowserThread;
29 using ::testing::NiceMock;
30 using ::testing::StrictMock;
31
32 ACTION_P(GetWeakPtrToSyncableService, syncable_service) {
33   // Have to do this within an Action to ensure it's not evaluated on the wrong
34   // thread.
35   return syncable_service->AsWeakPtr();
36 }
37
38 class SyncSharedChangeProcessorTest : public testing::Test {
39  public:
40   SyncSharedChangeProcessorTest()
41       : ui_thread_(BrowserThread::UI, &ui_loop_),
42         db_thread_(BrowserThread::DB),
43         sync_service_(&profile_) {}
44
45   virtual ~SyncSharedChangeProcessorTest() {
46     EXPECT_FALSE(db_syncable_service_.get());
47   }
48
49  protected:
50   virtual void SetUp() OVERRIDE {
51     shared_change_processor_ = new SharedChangeProcessor();
52     db_thread_.Start();
53     EXPECT_TRUE(BrowserThread::PostTask(
54         BrowserThread::DB,
55         FROM_HERE,
56         base::Bind(&SyncSharedChangeProcessorTest::SetUpDBSyncableService,
57                    base::Unretained(this))));
58   }
59
60   virtual void TearDown() OVERRIDE {
61     EXPECT_TRUE(BrowserThread::PostTask(
62         BrowserThread::DB,
63         FROM_HERE,
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.
69     //
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;
73     db_thread_.Stop();
74   }
75
76   // Connect |shared_change_processor_| on the DB thread.
77   void Connect() {
78     EXPECT_TRUE(BrowserThread::PostTask(
79         BrowserThread::DB,
80         FROM_HERE,
81         base::Bind(&SyncSharedChangeProcessorTest::ConnectOnDBThread,
82                    base::Unretained(this),
83                    shared_change_processor_)));
84   }
85
86  private:
87   // Used by SetUp().
88   void SetUpDBSyncableService() {
89     DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB));
90     DCHECK(!db_syncable_service_.get());
91     db_syncable_service_.reset(new syncer::FakeSyncableService());
92   }
93
94   // Used by TearDown().
95   void TearDownDBSyncableService() {
96     DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB));
97     DCHECK(db_syncable_service_.get());
98     db_syncable_service_.reset();
99   }
100
101   // Used by Connect().  The SharedChangeProcessor is passed in
102   // because we modify |shared_change_processor_| on the main thread
103   // (in TearDown()).
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(
113         &sync_factory_,
114         &processor_factory_,
115         sync_service_.GetUserShare(),
116         &error_handler_,
117         syncer::AUTOFILL,
118         base::WeakPtr<syncer::SyncMergeResult>()));
119   }
120
121   base::MessageLoopForUI ui_loop_;
122   content::TestBrowserThread ui_thread_;
123   content::TestBrowserThread db_thread_;
124
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_;
130
131   GenericChangeProcessorFactory processor_factory_;
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 browser_sync