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.
5 #include "components/sync_driver/fake_data_type_controller.h"
7 #include "testing/gmock/include/gmock/gmock.h"
8 #include "testing/gtest/include/gtest/gtest.h"
10 using syncer::ModelType;
11 namespace browser_sync {
13 FakeDataTypeController::FakeDataTypeController(ModelType type)
14 : DataTypeController(base::MessageLoopProxy::current(), base::Closure()),
16 model_load_delayed_(false),
19 FakeDataTypeController::~FakeDataTypeController() {
22 // NOT_RUNNING ->MODEL_LOADED |MODEL_STARTING.
23 void FakeDataTypeController::LoadModels(
24 const ModelLoadCallback& model_load_callback) {
25 if (state_ != NOT_RUNNING) {
30 if (model_load_delayed_ == false) {
31 if (load_error_.IsSet())
34 state_ = MODEL_LOADED;
35 model_load_callback.Run(type(), load_error_);
37 model_load_callback_ = model_load_callback;
38 state_ = MODEL_STARTING;
42 void FakeDataTypeController::OnModelLoaded() {
46 // MODEL_LOADED -> MODEL_STARTING.
47 void FakeDataTypeController::StartAssociating(
48 const StartCallback& start_callback) {
49 last_start_callback_ = start_callback;
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()) {
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) {
67 } else if (result == ASSOCIATION_FAILED) {
69 local_merge_result.set_error(
70 syncer::SyncError(FROM_HERE,
71 syncer::SyncError::DATATYPE_ERROR,
76 local_merge_result.set_error(
77 syncer::SyncError(FROM_HERE,
78 syncer::SyncError::DATATYPE_ERROR,
82 StartCallback start_callback = last_start_callback_;
83 last_start_callback_.Reset();
84 start_callback.Run(result,
90 void FakeDataTypeController::Stop() {
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();
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,
105 syncer::SyncMergeResult local_merge_result(type_);
106 local_merge_result.set_error(error);
107 last_start_callback_.Run(ABORTED,
109 syncer::SyncMergeResult(type_));
113 ModelType FakeDataTypeController::type() const {
117 std::string FakeDataTypeController::name() const {
118 return ModelTypeToString(type_);
121 // This isn't called by the DTM.
122 syncer::ModelSafeGroup FakeDataTypeController::model_safe_group() const {
124 return syncer::GROUP_PASSIVE;
127 DataTypeController::State FakeDataTypeController::state() const {
131 void FakeDataTypeController::OnSingleDatatypeUnrecoverableError(
132 const tracked_objects::Location& from_here,
133 const std::string& message) {
134 ADD_FAILURE() << message;
137 void FakeDataTypeController::RecordUnrecoverableError(
138 const tracked_objects::Location& from_here,
139 const std::string& message) {
140 ADD_FAILURE() << message;
143 void FakeDataTypeController::SetDelayModelLoad() {
144 model_load_delayed_ = true;
147 void FakeDataTypeController::SetModelLoadError(syncer::SyncError error) {
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();
157 } // namespace browser_sync