OLD | NEW |
| (Empty) |
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 #ifndef SYNC_TEST_ENGINE_SYNCER_COMMAND_TEST_H_ | |
6 #define SYNC_TEST_ENGINE_SYNCER_COMMAND_TEST_H_ | |
7 | |
8 #include <algorithm> | |
9 #include <string> | |
10 #include <vector> | |
11 | |
12 #include "base/compiler_specific.h" | |
13 #include "base/memory/ref_counted.h" | |
14 #include "base/message_loop/message_loop.h" | |
15 #include "sync/engine/model_changing_syncer_command.h" | |
16 #include "sync/engine/traffic_recorder.h" | |
17 #include "sync/internal_api/debug_info_event_listener.h" | |
18 #include "sync/internal_api/public/base/cancelation_signal.h" | |
19 #include "sync/internal_api/public/engine/model_safe_worker.h" | |
20 #include "sync/sessions/debug_info_getter.h" | |
21 #include "sync/sessions/sync_session.h" | |
22 #include "sync/sessions/sync_session_context.h" | |
23 #include "sync/syncable/directory.h" | |
24 #include "sync/test/engine/fake_model_worker.h" | |
25 #include "sync/test/engine/mock_connection_manager.h" | |
26 #include "sync/test/engine/test_directory_setter_upper.h" | |
27 #include "sync/util/extensions_activity.h" | |
28 #include "testing/gmock/include/gmock/gmock.h" | |
29 #include "testing/gtest/include/gtest/gtest.h" | |
30 | |
31 namespace syncer { | |
32 | |
33 // A test fixture that simplifies writing unit tests for individual | |
34 // SyncerCommands, providing convenient access to a test directory | |
35 // and a syncer session. | |
36 class SyncerCommandTestBase : public testing::Test, | |
37 public sessions::SyncSession::Delegate { | |
38 public: | |
39 // SyncSession::Delegate implementation. | |
40 virtual void OnThrottled( | |
41 const base::TimeDelta& throttle_duration) OVERRIDE; | |
42 virtual void OnTypesThrottled( | |
43 ModelTypeSet types, | |
44 const base::TimeDelta& throttle_duration) OVERRIDE; | |
45 virtual bool IsCurrentlyThrottled() OVERRIDE; | |
46 virtual void OnReceivedLongPollIntervalUpdate( | |
47 const base::TimeDelta& new_interval) OVERRIDE; | |
48 virtual void OnReceivedShortPollIntervalUpdate( | |
49 const base::TimeDelta& new_interval) OVERRIDE; | |
50 virtual void OnReceivedSessionsCommitDelay( | |
51 const base::TimeDelta& new_delay) OVERRIDE; | |
52 virtual void OnReceivedClientInvalidationHintBufferSize(int size) OVERRIDE; | |
53 virtual void OnSyncProtocolError( | |
54 const sessions::SyncSessionSnapshot& session) OVERRIDE; | |
55 | |
56 std::vector<ModelSafeWorker*> GetWorkers() { | |
57 std::vector<ModelSafeWorker*> workers; | |
58 std::vector<scoped_refptr<ModelSafeWorker> >::iterator it; | |
59 for (it = workers_.begin(); it != workers_.end(); ++it) | |
60 workers.push_back(it->get()); | |
61 return workers; | |
62 } | |
63 void GetModelSafeRoutingInfo(ModelSafeRoutingInfo* out) { | |
64 ModelSafeRoutingInfo copy(routing_info_); | |
65 out->swap(copy); | |
66 } | |
67 | |
68 protected: | |
69 SyncerCommandTestBase(); | |
70 | |
71 virtual ~SyncerCommandTestBase(); | |
72 virtual void SetUp(); | |
73 virtual void TearDown(); | |
74 | |
75 sessions::SyncSessionContext* context() const { return context_.get(); } | |
76 sessions::SyncSession::Delegate* delegate() { return this; } | |
77 | |
78 // Lazily create a session requesting all datatypes with no state. | |
79 sessions::SyncSession* session() { | |
80 if (!session_.get()) | |
81 session_.reset(sessions::SyncSession::Build(context(), delegate())); | |
82 return session_.get(); | |
83 } | |
84 | |
85 void ClearSession() { | |
86 session_.reset(); | |
87 } | |
88 | |
89 void ResetContext() { | |
90 context_.reset(new sessions::SyncSessionContext( | |
91 mock_server_.get(), directory(), | |
92 GetWorkers(), extensions_activity_.get(), | |
93 std::vector<SyncEngineEventListener*>(), | |
94 &debug_info_event_listener_, | |
95 &traffic_recorder_, | |
96 true, // enable keystore encryption | |
97 false, // force enable pre-commit GU avoidance experiment | |
98 "fake_invalidator_client_id")); | |
99 context_->set_routing_info(routing_info_); | |
100 context_->set_account_name(directory()->name()); | |
101 ClearSession(); | |
102 } | |
103 | |
104 // Install a MockServerConnection. Resets the context. By default, | |
105 // the context does not have a MockServerConnection attached. | |
106 void ConfigureMockServerConnection() { | |
107 mock_server_.reset(new MockConnectionManager(directory(), | |
108 &cancelation_signal_)); | |
109 ResetContext(); | |
110 } | |
111 | |
112 virtual syncable::Directory* directory() = 0; | |
113 | |
114 std::vector<scoped_refptr<ModelSafeWorker> >* workers() { | |
115 return &workers_; | |
116 } | |
117 | |
118 const ModelSafeRoutingInfo& routing_info() { return routing_info_; } | |
119 ModelSafeRoutingInfo* mutable_routing_info() { return &routing_info_; } | |
120 | |
121 MockConnectionManager* mock_server() { | |
122 return mock_server_.get(); | |
123 } | |
124 | |
125 DebugInfoEventListener* debug_info_event_listener() { | |
126 return &debug_info_event_listener_; | |
127 } | |
128 // Helper functions to check command.GetGroupsToChange(). | |
129 | |
130 void ExpectNoGroupsToChange(const ModelChangingSyncerCommand& command) { | |
131 EXPECT_TRUE(command.GetGroupsToChangeForTest(*session()).empty()); | |
132 } | |
133 | |
134 void ExpectGroupToChange( | |
135 const ModelChangingSyncerCommand& command, ModelSafeGroup group) { | |
136 std::set<ModelSafeGroup> expected_groups_to_change; | |
137 expected_groups_to_change.insert(group); | |
138 EXPECT_EQ(expected_groups_to_change, | |
139 command.GetGroupsToChangeForTest(*session())); | |
140 } | |
141 | |
142 void ExpectGroupsToChange( | |
143 const ModelChangingSyncerCommand& command, | |
144 ModelSafeGroup group1, ModelSafeGroup group2) { | |
145 std::set<ModelSafeGroup> expected_groups_to_change; | |
146 expected_groups_to_change.insert(group1); | |
147 expected_groups_to_change.insert(group2); | |
148 EXPECT_EQ(expected_groups_to_change, | |
149 command.GetGroupsToChangeForTest(*session())); | |
150 } | |
151 | |
152 void ExpectGroupsToChange( | |
153 const ModelChangingSyncerCommand& command, | |
154 ModelSafeGroup group1, ModelSafeGroup group2, ModelSafeGroup group3) { | |
155 std::set<ModelSafeGroup> expected_groups_to_change; | |
156 expected_groups_to_change.insert(group1); | |
157 expected_groups_to_change.insert(group2); | |
158 expected_groups_to_change.insert(group3); | |
159 EXPECT_EQ(expected_groups_to_change, | |
160 command.GetGroupsToChangeForTest(*session())); | |
161 } | |
162 | |
163 private: | |
164 base::MessageLoop message_loop_; | |
165 scoped_ptr<sessions::SyncSessionContext> context_; | |
166 scoped_ptr<MockConnectionManager> mock_server_; | |
167 scoped_ptr<sessions::SyncSession> session_; | |
168 std::vector<scoped_refptr<ModelSafeWorker> > workers_; | |
169 ModelSafeRoutingInfo routing_info_; | |
170 DebugInfoEventListener debug_info_event_listener_; | |
171 scoped_refptr<ExtensionsActivity> extensions_activity_; | |
172 TrafficRecorder traffic_recorder_; | |
173 CancelationSignal cancelation_signal_; | |
174 DISALLOW_COPY_AND_ASSIGN(SyncerCommandTestBase); | |
175 }; | |
176 | |
177 class SyncerCommandTest : public SyncerCommandTestBase { | |
178 public: | |
179 virtual void SetUp() OVERRIDE; | |
180 virtual void TearDown() OVERRIDE; | |
181 virtual syncable::Directory* directory() OVERRIDE; | |
182 | |
183 private: | |
184 TestDirectorySetterUpper dir_maker_; | |
185 }; | |
186 | |
187 } // namespace syncer | |
188 | |
189 #endif // SYNC_TEST_ENGINE_SYNCER_COMMAND_TEST_H_ | |
OLD | NEW |