Home | History | Annotate | Download | only in glue
      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 #include "testing/gmock/include/gmock/gmock.h"
      6 #include "testing/gtest/include/gtest/gtest.h"
      7 
      8 #include "chrome/browser/sync/glue/fake_data_type_controller.h"
      9 
     10 #include "testing/gmock/include/gmock/gmock.h"
     11 #include "testing/gtest/include/gtest/gtest.h"
     12 
     13 using syncer::ModelType;
     14 namespace browser_sync {
     15 
     16 FakeDataTypeController::FakeDataTypeController(ModelType type)
     17       : state_(NOT_RUNNING), model_load_delayed_(false), 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     state_ = MODEL_LOADED;
     32     model_load_callback.Run(type(), syncer::SyncError());
     33   } else {
     34     model_load_callback_ = model_load_callback;
     35     state_ = MODEL_STARTING;
     36   }
     37 }
     38 
     39 void FakeDataTypeController::OnModelLoaded() {
     40   NOTREACHED();
     41 }
     42 
     43 // MODEL_LOADED -> MODEL_STARTING.
     44 void FakeDataTypeController::StartAssociating(
     45    const StartCallback& start_callback) {
     46   last_start_callback_ = start_callback;
     47   state_ = ASSOCIATING;
     48 }
     49 
     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()) {
     55     ADD_FAILURE();
     56     return;
     57   }
     58 
     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) {
     63     state_ = RUNNING;
     64   } else if (result == ASSOCIATION_FAILED) {
     65     state_ = DISABLED;
     66     local_merge_result.set_error(
     67         syncer::SyncError(FROM_HERE,
     68                           syncer::SyncError::DATATYPE_ERROR,
     69                           "Association failed",
     70                           type()));
     71   } else {
     72     state_ = NOT_RUNNING;
     73     local_merge_result.set_error(
     74         syncer::SyncError(FROM_HERE,
     75                           syncer::SyncError::DATATYPE_ERROR,
     76                           "Fake error",
     77                           type()));
     78   }
     79   StartCallback start_callback = last_start_callback_;
     80   last_start_callback_.Reset();
     81   start_callback.Run(result,
     82                      local_merge_result,
     83                      syncer_merge_result);
     84 }
     85 
     86 // * -> NOT_RUNNING
     87 void FakeDataTypeController::Stop() {
     88   state_ = NOT_RUNNING;
     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();
     94   }
     95 
     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,
    100                             "Fake error",
    101                             type_);
    102     syncer::SyncMergeResult local_merge_result(type_);
    103     local_merge_result.set_error(error);
    104     last_start_callback_.Run(ABORTED,
    105                              local_merge_result,
    106                              syncer::SyncMergeResult(type_));
    107   }
    108 }
    109 
    110 ModelType FakeDataTypeController::type() const {
    111   return type_;
    112 }
    113 
    114 std::string FakeDataTypeController::name() const {
    115   return ModelTypeToString(type_);
    116 }
    117 
    118 // This isn't called by the DTM.
    119 syncer::ModelSafeGroup FakeDataTypeController::model_safe_group() const {
    120   ADD_FAILURE();
    121   return syncer::GROUP_PASSIVE;
    122 }
    123 
    124 DataTypeController::State FakeDataTypeController::state() const {
    125   return state_;
    126 }
    127 
    128 void FakeDataTypeController::OnSingleDatatypeUnrecoverableError(
    129     const tracked_objects::Location& from_here,
    130     const std::string& message) {
    131   ADD_FAILURE() << message;
    132 }
    133 
    134 void FakeDataTypeController::RecordUnrecoverableError(
    135     const tracked_objects::Location& from_here,
    136     const std::string& message) {
    137   ADD_FAILURE() << message;
    138 }
    139 
    140 void FakeDataTypeController::SetDelayModelLoad() {
    141   model_load_delayed_ = true;
    142 }
    143 
    144 void FakeDataTypeController::SimulateModelLoadFinishing() {
    145   ModelLoadCallback model_load_callback = model_load_callback_;
    146   model_load_callback.Run(type(), syncer::SyncError());
    147 }
    148 
    149 }  // namespace browser_sync
    150