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;
12 namespace sync_driver {
14 FakeDataTypeController::FakeDataTypeController(ModelType type)
15 : DataTypeController(base::MessageLoopProxy::current(), base::Closure()),
17 model_load_delayed_(false),
19 ready_for_start_(true) {}
21 FakeDataTypeController::~FakeDataTypeController() {
24 // NOT_RUNNING ->MODEL_LOADED |MODEL_STARTING.
25 void FakeDataTypeController::LoadModels(
26 const ModelLoadCallback& model_load_callback) {
27 model_load_callback_ = model_load_callback;
28 if (state_ != NOT_RUNNING) {
33 if (model_load_delayed_ == false) {
34 if (load_error_.IsSet())
37 state_ = MODEL_LOADED;
38 model_load_callback.Run(type(), load_error_);
40 state_ = MODEL_STARTING;
44 void FakeDataTypeController::OnModelLoaded() {
48 // MODEL_LOADED -> MODEL_STARTING.
49 void FakeDataTypeController::StartAssociating(
50 const StartCallback& start_callback) {
51 last_start_callback_ = start_callback;
55 // MODEL_STARTING | ASSOCIATING -> RUNNING | DISABLED | NOT_RUNNING
56 // (depending on |result|)
57 void FakeDataTypeController::FinishStart(ConfigureResult result) {
58 // We should have a callback from Start().
59 if (last_start_callback_.is_null()) {
64 // Set |state_| first below since the callback may call state().
65 syncer::SyncMergeResult local_merge_result(type());
66 syncer::SyncMergeResult syncer_merge_result(type());
67 if (result <= OK_FIRST_RUN) {
69 } else if (result == ASSOCIATION_FAILED) {
71 local_merge_result.set_error(
72 syncer::SyncError(FROM_HERE,
73 syncer::SyncError::DATATYPE_ERROR,
76 } else if (result == UNRECOVERABLE_ERROR) {
78 local_merge_result.set_error(
79 syncer::SyncError(FROM_HERE,
80 syncer::SyncError::UNRECOVERABLE_ERROR,
81 "Unrecoverable error",
83 } else if (result == NEEDS_CRYPTO) {
85 local_merge_result.set_error(
86 syncer::SyncError(FROM_HERE,
87 syncer::SyncError::CRYPTO_ERROR,
93 last_start_callback_.Run(result, local_merge_result, syncer_merge_result);
97 void FakeDataTypeController::Stop() {
99 if (!model_load_callback_.is_null()) {
100 // Real data type controllers run the callback and specify "ABORTED" as an
101 // error. We should probably find a way to use the real code and mock out
102 // unnecessary pieces.
103 SimulateModelLoadFinishing();
107 ModelType FakeDataTypeController::type() const {
111 std::string FakeDataTypeController::name() const {
112 return ModelTypeToString(type_);
115 syncer::ModelSafeGroup FakeDataTypeController::model_safe_group() const {
116 return syncer::GROUP_PASSIVE;
119 ChangeProcessor* FakeDataTypeController::GetChangeProcessor() const {
123 DataTypeController::State FakeDataTypeController::state() const {
127 void FakeDataTypeController::OnSingleDataTypeUnrecoverableError(
128 const syncer::SyncError& error) {
129 if (!model_load_callback_.is_null())
130 model_load_callback_.Run(type(), error);
133 bool FakeDataTypeController::ReadyForStart() const {
134 return ready_for_start_;
137 void FakeDataTypeController::SetDelayModelLoad() {
138 model_load_delayed_ = true;
141 void FakeDataTypeController::SetModelLoadError(syncer::SyncError error) {
145 void FakeDataTypeController::SimulateModelLoadFinishing() {
146 model_load_callback_.Run(type(), load_error_);
149 void FakeDataTypeController::SetReadyForStart(bool ready) {
150 ready_for_start_ = ready;
153 } // namespace sync_driver