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.
5 #include "testing/gmock/include/gmock/gmock.h"
6 #include "testing/gtest/include/gtest/gtest.h"
8 #include "chrome/browser/sync/glue/fake_data_type_controller.h"
10 #include "testing/gmock/include/gmock/gmock.h"
11 #include "testing/gtest/include/gtest/gtest.h"
13 using syncer::ModelType;
14 namespace browser_sync {
16 FakeDataTypeController::FakeDataTypeController(ModelType type)
17 : state_(NOT_RUNNING), model_load_delayed_(false), type_(type) {}
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 state_ = MODEL_LOADED;
32 model_load_callback.Run(type(), syncer::SyncError());
34 model_load_callback_ = model_load_callback;
35 state_ = MODEL_STARTING;
39 void FakeDataTypeController::OnModelLoaded() {
43 // MODEL_LOADED -> MODEL_STARTING.
44 void FakeDataTypeController::StartAssociating(
45 const StartCallback& start_callback) {
46 last_start_callback_ = start_callback;
50 // MODEL_STARTING | ASSOCIATING -> RUNNING | DISABLED | NOT_RUNNING
51 // (depending on |result|)
52 void FakeDataTypeController::FinishStart(StartResult result) {
53 // We should have a callback from Start().
54 if (last_start_callback_.is_null()) {
59 // Set |state_| first below since the callback may call state().
60 syncer::SyncMergeResult local_merge_result(type());
61 syncer::SyncMergeResult syncer_merge_result(type());
62 if (result <= OK_FIRST_RUN) {
64 } else if (result == ASSOCIATION_FAILED) {
66 local_merge_result.set_error(
67 syncer::SyncError(FROM_HERE,
68 syncer::SyncError::DATATYPE_ERROR,
73 local_merge_result.set_error(
74 syncer::SyncError(FROM_HERE,
75 syncer::SyncError::DATATYPE_ERROR,
79 StartCallback start_callback = last_start_callback_;
80 last_start_callback_.Reset();
81 start_callback.Run(result,
87 void FakeDataTypeController::Stop() {
89 if (!model_load_callback_.is_null()) {
90 // Real data type controllers run the callback and specify "ABORTED" as an
91 // error. We should probably find a way to use the real code and mock out
92 // unnecessary pieces.
93 SimulateModelLoadFinishing();
96 // The DTM still expects |last_start_callback_| to be called back.
97 if (!last_start_callback_.is_null()) {
98 syncer::SyncError error(FROM_HERE,
99 syncer::SyncError::DATATYPE_ERROR,
102 syncer::SyncMergeResult local_merge_result(type_);
103 local_merge_result.set_error(error);
104 last_start_callback_.Run(ABORTED,
106 syncer::SyncMergeResult(type_));
110 ModelType FakeDataTypeController::type() const {
114 std::string FakeDataTypeController::name() const {
115 return ModelTypeToString(type_);
118 // This isn't called by the DTM.
119 syncer::ModelSafeGroup FakeDataTypeController::model_safe_group() const {
121 return syncer::GROUP_PASSIVE;
124 DataTypeController::State FakeDataTypeController::state() const {
128 void FakeDataTypeController::OnSingleDatatypeUnrecoverableError(
129 const tracked_objects::Location& from_here,
130 const std::string& message) {
131 ADD_FAILURE() << message;
134 void FakeDataTypeController::RecordUnrecoverableError(
135 const tracked_objects::Location& from_here,
136 const std::string& message) {
137 ADD_FAILURE() << message;
140 void FakeDataTypeController::SetDelayModelLoad() {
141 model_load_delayed_ = true;
144 void FakeDataTypeController::SimulateModelLoadFinishing() {
145 ModelLoadCallback model_load_callback = model_load_callback_;
146 model_load_callback.Run(type(), syncer::SyncError());
149 } // namespace browser_sync