- add sources.
[platform/framework/web/crosswalk.git] / src / sync / test / engine / syncer_command_test.h
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 OnShouldStopSyncingPermanently() OVERRIDE;
54   virtual void OnSyncProtocolError(
55       const sessions::SyncSessionSnapshot& session) OVERRIDE;
56
57   std::vector<ModelSafeWorker*> GetWorkers() {
58     std::vector<ModelSafeWorker*> workers;
59     std::vector<scoped_refptr<ModelSafeWorker> >::iterator it;
60     for (it = workers_.begin(); it != workers_.end(); ++it)
61       workers.push_back(it->get());
62     return workers;
63   }
64   void GetModelSafeRoutingInfo(ModelSafeRoutingInfo* out) {
65     ModelSafeRoutingInfo copy(routing_info_);
66     out->swap(copy);
67   }
68
69  protected:
70   SyncerCommandTestBase();
71
72   virtual ~SyncerCommandTestBase();
73   virtual void SetUp();
74   virtual void TearDown();
75
76   sessions::SyncSessionContext* context() const { return context_.get(); }
77   sessions::SyncSession::Delegate* delegate() { return this; }
78
79   // Lazily create a session requesting all datatypes with no state.
80   sessions::SyncSession* session() {
81     if (!session_.get())
82       session_.reset(sessions::SyncSession::Build(context(), delegate()));
83     return session_.get();
84   }
85
86   void ClearSession() {
87     session_.reset();
88   }
89
90   void ResetContext() {
91     context_.reset(new sessions::SyncSessionContext(
92             mock_server_.get(), directory(),
93             GetWorkers(), extensions_activity_.get(),
94             std::vector<SyncEngineEventListener*>(),
95             &debug_info_event_listener_,
96             &traffic_recorder_,
97             true,  // enable keystore encryption
98             false,  // force enable pre-commit GU avoidance experiment
99             "fake_invalidator_client_id"));
100     context_->set_routing_info(routing_info_);
101     context_->set_account_name(directory()->name());
102     ClearSession();
103   }
104
105   // Install a MockServerConnection.  Resets the context.  By default,
106   // the context does not have a MockServerConnection attached.
107   void ConfigureMockServerConnection() {
108     mock_server_.reset(new MockConnectionManager(directory(),
109                                                  &cancelation_signal_));
110     ResetContext();
111   }
112
113   virtual syncable::Directory* directory() = 0;
114
115   std::vector<scoped_refptr<ModelSafeWorker> >* workers() {
116     return &workers_;
117   }
118
119   const ModelSafeRoutingInfo& routing_info() { return routing_info_; }
120   ModelSafeRoutingInfo* mutable_routing_info() { return &routing_info_; }
121
122   MockConnectionManager* mock_server() {
123     return mock_server_.get();
124   }
125
126   DebugInfoEventListener* debug_info_event_listener() {
127     return &debug_info_event_listener_;
128   }
129   // Helper functions to check command.GetGroupsToChange().
130
131   void ExpectNoGroupsToChange(const ModelChangingSyncerCommand& command) {
132     EXPECT_TRUE(command.GetGroupsToChangeForTest(*session()).empty());
133   }
134
135   void ExpectGroupToChange(
136       const ModelChangingSyncerCommand& command, ModelSafeGroup group) {
137     std::set<ModelSafeGroup> expected_groups_to_change;
138     expected_groups_to_change.insert(group);
139     EXPECT_EQ(expected_groups_to_change,
140               command.GetGroupsToChangeForTest(*session()));
141   }
142
143   void ExpectGroupsToChange(
144       const ModelChangingSyncerCommand& command,
145       ModelSafeGroup group1, ModelSafeGroup group2) {
146     std::set<ModelSafeGroup> expected_groups_to_change;
147     expected_groups_to_change.insert(group1);
148     expected_groups_to_change.insert(group2);
149     EXPECT_EQ(expected_groups_to_change,
150               command.GetGroupsToChangeForTest(*session()));
151   }
152
153   void ExpectGroupsToChange(
154       const ModelChangingSyncerCommand& command,
155       ModelSafeGroup group1, ModelSafeGroup group2, ModelSafeGroup group3) {
156     std::set<ModelSafeGroup> expected_groups_to_change;
157     expected_groups_to_change.insert(group1);
158     expected_groups_to_change.insert(group2);
159     expected_groups_to_change.insert(group3);
160     EXPECT_EQ(expected_groups_to_change,
161               command.GetGroupsToChangeForTest(*session()));
162   }
163
164  private:
165   base::MessageLoop message_loop_;
166   scoped_ptr<sessions::SyncSessionContext> context_;
167   scoped_ptr<MockConnectionManager> mock_server_;
168   scoped_ptr<sessions::SyncSession> session_;
169   std::vector<scoped_refptr<ModelSafeWorker> > workers_;
170   ModelSafeRoutingInfo routing_info_;
171   DebugInfoEventListener debug_info_event_listener_;
172   scoped_refptr<ExtensionsActivity> extensions_activity_;
173   TrafficRecorder traffic_recorder_;
174   CancelationSignal cancelation_signal_;
175   DISALLOW_COPY_AND_ASSIGN(SyncerCommandTestBase);
176 };
177
178 class SyncerCommandTest : public SyncerCommandTestBase {
179  public:
180   virtual void SetUp() OVERRIDE;
181   virtual void TearDown() OVERRIDE;
182   virtual syncable::Directory* directory() OVERRIDE;
183
184  private:
185   TestDirectorySetterUpper dir_maker_;
186 };
187
188 }  // namespace syncer
189
190 #endif  // SYNC_TEST_ENGINE_SYNCER_COMMAND_TEST_H_