Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / components / sync_driver / fake_data_type_controller.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/fake_data_type_controller.h"
6
7 #include "testing/gmock/include/gmock/gmock.h"
8 #include "testing/gtest/include/gtest/gtest.h"
9
10 using syncer::ModelType;
11 namespace browser_sync {
12
13 FakeDataTypeController::FakeDataTypeController(ModelType type)
14       : DataTypeController(base::MessageLoopProxy::current(), base::Closure()),
15         state_(NOT_RUNNING),
16         model_load_delayed_(false),
17         type_(type) {}
18
19 FakeDataTypeController::~FakeDataTypeController() {
20 }
21
22 // NOT_RUNNING ->MODEL_LOADED |MODEL_STARTING.
23 void FakeDataTypeController::LoadModels(
24     const ModelLoadCallback& model_load_callback) {
25   if (state_ != NOT_RUNNING) {
26     ADD_FAILURE();
27     return;
28   }
29
30   if (model_load_delayed_ == false) {
31     if (load_error_.IsSet())
32       state_ = DISABLED;
33     else
34       state_ = MODEL_LOADED;
35     model_load_callback.Run(type(), load_error_);
36   } else {
37     model_load_callback_ = model_load_callback;
38     state_ = MODEL_STARTING;
39   }
40 }
41
42 void FakeDataTypeController::OnModelLoaded() {
43   NOTREACHED();
44 }
45
46 // MODEL_LOADED -> MODEL_STARTING.
47 void FakeDataTypeController::StartAssociating(
48    const StartCallback& start_callback) {
49   last_start_callback_ = start_callback;
50   state_ = ASSOCIATING;
51 }
52
53 // MODEL_STARTING | ASSOCIATING -> RUNNING | DISABLED | NOT_RUNNING
54 // (depending on |result|)
55 void FakeDataTypeController::FinishStart(StartResult result) {
56   // We should have a callback from Start().
57   if (last_start_callback_.is_null()) {
58     ADD_FAILURE();
59     return;
60   }
61
62   // Set |state_| first below since the callback may call state().
63   syncer::SyncMergeResult local_merge_result(type());
64   syncer::SyncMergeResult syncer_merge_result(type());
65   if (result <= OK_FIRST_RUN) {
66     state_ = RUNNING;
67   } else if (result == ASSOCIATION_FAILED) {
68     state_ = DISABLED;
69     local_merge_result.set_error(
70         syncer::SyncError(FROM_HERE,
71                           syncer::SyncError::DATATYPE_ERROR,
72                           "Association failed",
73                           type()));
74   } else {
75     state_ = NOT_RUNNING;
76     local_merge_result.set_error(
77         syncer::SyncError(FROM_HERE,
78                           syncer::SyncError::DATATYPE_ERROR,
79                           "Fake error",
80                           type()));
81   }
82   StartCallback start_callback = last_start_callback_;
83   last_start_callback_.Reset();
84   start_callback.Run(result,
85                      local_merge_result,
86                      syncer_merge_result);
87 }
88
89 // * -> NOT_RUNNING
90 void FakeDataTypeController::Stop() {
91   state_ = NOT_RUNNING;
92   if (!model_load_callback_.is_null()) {
93     // Real data type controllers run the callback and specify "ABORTED" as an
94     // error.  We should probably find a way to use the real code and mock out
95     // unnecessary pieces.
96     SimulateModelLoadFinishing();
97   }
98
99   // The DTM still expects |last_start_callback_| to be called back.
100   if (!last_start_callback_.is_null()) {
101     syncer::SyncError error(FROM_HERE,
102                             syncer::SyncError::DATATYPE_ERROR,
103                             "Fake error",
104                             type_);
105     syncer::SyncMergeResult local_merge_result(type_);
106     local_merge_result.set_error(error);
107     last_start_callback_.Run(ABORTED,
108                              local_merge_result,
109                              syncer::SyncMergeResult(type_));
110   }
111 }
112
113 ModelType FakeDataTypeController::type() const {
114   return type_;
115 }
116
117 std::string FakeDataTypeController::name() const {
118   return ModelTypeToString(type_);
119 }
120
121 // This isn't called by the DTM.
122 syncer::ModelSafeGroup FakeDataTypeController::model_safe_group() const {
123   ADD_FAILURE();
124   return syncer::GROUP_PASSIVE;
125 }
126
127 DataTypeController::State FakeDataTypeController::state() const {
128   return state_;
129 }
130
131 void FakeDataTypeController::OnSingleDatatypeUnrecoverableError(
132     const tracked_objects::Location& from_here,
133     const std::string& message) {
134   ADD_FAILURE() << message;
135 }
136
137 void FakeDataTypeController::RecordUnrecoverableError(
138     const tracked_objects::Location& from_here,
139     const std::string& message) {
140   ADD_FAILURE() << message;
141 }
142
143 void FakeDataTypeController::SetDelayModelLoad() {
144   model_load_delayed_ = true;
145 }
146
147 void FakeDataTypeController::SetModelLoadError(syncer::SyncError error) {
148   load_error_ = error;
149 }
150
151 void FakeDataTypeController::SimulateModelLoadFinishing() {
152   ModelLoadCallback model_load_callback = model_load_callback_;
153   model_load_callback.Run(type(), load_error_);
154   model_load_callback_.Reset();
155 }
156
157 }  // namespace browser_sync