OLD | NEW |
| (Empty) |
1 // Copyright (c) 2011 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 #ifndef CHROME_TEST_SYNC_ENGINE_SYNCER_COMMAND_TEST_H_ | |
6 #define CHROME_TEST_SYNC_ENGINE_SYNCER_COMMAND_TEST_H_ | |
7 #pragma once | |
8 | |
9 #include <algorithm> | |
10 #include <string> | |
11 #include <vector> | |
12 | |
13 #include "base/compiler_specific.h" | |
14 #include "chrome/browser/sync/engine/model_safe_worker.h" | |
15 #include "chrome/browser/sync/sessions/sync_session.h" | |
16 #include "chrome/browser/sync/sessions/sync_session_context.h" | |
17 #include "chrome/test/sync/engine/mock_connection_manager.h" | |
18 #include "chrome/test/sync/engine/test_directory_setter_upper.h" | |
19 #include "testing/gtest/include/gtest/gtest.h" | |
20 | |
21 namespace browser_sync { | |
22 | |
23 // A test fixture that simplifies writing unit tests for individual | |
24 // SyncerCommands, providing convenient access to a test directory | |
25 // and a syncer session. | |
26 template<typename T> | |
27 class SyncerCommandTestWithParam : public testing::TestWithParam<T>, | |
28 public sessions::SyncSession::Delegate, | |
29 public ModelSafeWorkerRegistrar { | |
30 public: | |
31 enum UseMockDirectory { | |
32 USE_MOCK_DIRECTORY | |
33 }; | |
34 | |
35 // SyncSession::Delegate implementation. | |
36 virtual void OnSilencedUntil( | |
37 const base::TimeTicks& silenced_until) OVERRIDE { | |
38 FAIL() << "Should not get silenced."; | |
39 } | |
40 virtual bool IsSyncingCurrentlySilenced() OVERRIDE { | |
41 ADD_FAILURE() << "No requests for silenced state should be made."; | |
42 return false; | |
43 } | |
44 virtual void OnReceivedLongPollIntervalUpdate( | |
45 const base::TimeDelta& new_interval) OVERRIDE { | |
46 FAIL() << "Should not get poll interval update."; | |
47 } | |
48 virtual void OnReceivedShortPollIntervalUpdate( | |
49 const base::TimeDelta& new_interval) OVERRIDE { | |
50 FAIL() << "Should not get poll interval update."; | |
51 } | |
52 virtual void OnReceivedSessionsCommitDelay( | |
53 const base::TimeDelta& new_delay) OVERRIDE { | |
54 FAIL() << "Should not get sessions commit delay."; | |
55 } | |
56 virtual void OnShouldStopSyncingPermanently() OVERRIDE { | |
57 FAIL() << "Shouldn't be called."; | |
58 } | |
59 virtual void OnSyncProtocolError( | |
60 const sessions::SyncSessionSnapshot& session) OVERRIDE { | |
61 FAIL() << "Shouldn't be called."; | |
62 } | |
63 | |
64 // ModelSafeWorkerRegistrar implementation. | |
65 virtual void GetWorkers(std::vector<ModelSafeWorker*>* out) OVERRIDE { | |
66 std::vector<scoped_refptr<ModelSafeWorker> >::iterator it; | |
67 for (it = workers_.begin(); it != workers_.end(); ++it) | |
68 out->push_back(*it); | |
69 } | |
70 virtual void GetModelSafeRoutingInfo(ModelSafeRoutingInfo* out) OVERRIDE { | |
71 ModelSafeRoutingInfo copy(routing_info_); | |
72 out->swap(copy); | |
73 } | |
74 | |
75 protected: | |
76 SyncerCommandTestWithParam() : syncdb_(new TestDirectorySetterUpper()) {} | |
77 | |
78 explicit SyncerCommandTestWithParam(UseMockDirectory use_mock) | |
79 : syncdb_(new MockDirectorySetterUpper()) {} | |
80 | |
81 virtual ~SyncerCommandTestWithParam() {} | |
82 virtual void SetUp() { | |
83 syncdb_->SetUp(); | |
84 ResetContext(); | |
85 } | |
86 virtual void TearDown() { | |
87 syncdb_->TearDown(); | |
88 } | |
89 | |
90 TestDirectorySetterUpper* syncdb() { return syncdb_.get(); } | |
91 sessions::SyncSessionContext* context() const { return context_.get(); } | |
92 sessions::SyncSession::Delegate* delegate() { return this; } | |
93 ModelSafeWorkerRegistrar* registrar() { return this; } | |
94 | |
95 // Lazily create a session requesting all datatypes with no payload. | |
96 sessions::SyncSession* session() { | |
97 syncable::ModelTypePayloadMap types = | |
98 syncable::ModelTypePayloadMapFromRoutingInfo(routing_info_, | |
99 std::string()); | |
100 return session(sessions::SyncSourceInfo(types)); | |
101 } | |
102 | |
103 // Create a session with the provided source. | |
104 sessions::SyncSession* session(const sessions::SyncSourceInfo& source) { | |
105 if (!session_.get()) { | |
106 std::vector<ModelSafeWorker*> workers; | |
107 GetWorkers(&workers); | |
108 session_.reset(new sessions::SyncSession(context(), delegate(), source, | |
109 routing_info_, workers)); | |
110 } | |
111 return session_.get(); | |
112 } | |
113 | |
114 void ClearSession() { | |
115 session_.reset(); | |
116 } | |
117 | |
118 void ResetContext() { | |
119 context_.reset(new sessions::SyncSessionContext( | |
120 mock_server_.get(), syncdb_->manager(), registrar(), | |
121 std::vector<SyncEngineEventListener*>())); | |
122 context_->set_account_name(syncdb_->name()); | |
123 ClearSession(); | |
124 } | |
125 | |
126 // Install a MockServerConnection. Resets the context. By default, | |
127 // the context does not have a MockServerConnection attached. | |
128 void ConfigureMockServerConnection() { | |
129 mock_server_.reset( | |
130 new MockConnectionManager(syncdb_->manager(), syncdb_->name())); | |
131 ResetContext(); | |
132 } | |
133 | |
134 std::vector<scoped_refptr<ModelSafeWorker> >* workers() { | |
135 return &workers_; | |
136 } | |
137 | |
138 const ModelSafeRoutingInfo& routing_info() { return routing_info_; } | |
139 ModelSafeRoutingInfo* mutable_routing_info() { return &routing_info_; } | |
140 | |
141 MockConnectionManager* mock_server() { | |
142 return mock_server_.get(); | |
143 } | |
144 | |
145 private: | |
146 scoped_ptr<TestDirectorySetterUpper> syncdb_; | |
147 scoped_ptr<sessions::SyncSessionContext> context_; | |
148 scoped_ptr<MockConnectionManager> mock_server_; | |
149 scoped_ptr<sessions::SyncSession> session_; | |
150 std::vector<scoped_refptr<ModelSafeWorker> > workers_; | |
151 ModelSafeRoutingInfo routing_info_; | |
152 DISALLOW_COPY_AND_ASSIGN(SyncerCommandTestWithParam); | |
153 }; | |
154 | |
155 class SyncerCommandTest : public SyncerCommandTestWithParam<void*> {}; | |
156 | |
157 class MockDirectorySyncerCommandTest | |
158 : public SyncerCommandTestWithParam<void*> { | |
159 public: | |
160 MockDirectorySyncerCommandTest() | |
161 : SyncerCommandTestWithParam<void*>( | |
162 SyncerCommandTestWithParam<void*>::USE_MOCK_DIRECTORY) {} | |
163 MockDirectorySetterUpper::MockDirectory* mock_directory() { | |
164 return static_cast<MockDirectorySetterUpper*>(syncdb())->directory(); | |
165 } | |
166 }; | |
167 | |
168 } // namespace browser_sync | |
169 | |
170 #endif // CHROME_TEST_SYNC_ENGINE_SYNCER_COMMAND_TEST_H_ | |
OLD | NEW |