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 "base/bind.h" 6 #include "base/callback.h" 7 #include "base/memory/weak_ptr.h" 8 #include "base/run_loop.h" 9 #include "chrome/browser/sync/glue/local_device_info_provider_mock.h" 10 #include "chrome/browser/sync/profile_sync_components_factory_mock.h" 11 #include "components/sync_driver/device_info_data_type_controller.h" 12 #include "content/public/test/test_browser_thread_bundle.h" 13 #include "testing/gtest/include/gtest/gtest.h" 14 15 using sync_driver::DataTypeController; 16 using sync_driver::DeviceInfoDataTypeController; 17 18 namespace browser_sync { 19 20 namespace { 21 22 class DeviceInfoDataTypeControllerTest : public testing::Test { 23 public: 24 DeviceInfoDataTypeControllerTest() 25 : load_finished_(false), 26 thread_bundle_(content::TestBrowserThreadBundle::DEFAULT), 27 weak_ptr_factory_(this), 28 last_type_(syncer::UNSPECIFIED) {} 29 virtual ~DeviceInfoDataTypeControllerTest() {} 30 31 virtual void SetUp() OVERRIDE { 32 local_device_.reset(new LocalDeviceInfoProviderMock( 33 "cache_guid", 34 "Wayne Gretzky's Hacking Box", 35 "Chromium 10k", 36 "Chrome 10k", 37 sync_pb::SyncEnums_DeviceType_TYPE_LINUX, 38 "device_id")); 39 40 controller_ = new DeviceInfoDataTypeController( 41 base::MessageLoopProxy::current(), 42 base::Closure(), 43 &profile_sync_factory_, 44 local_device_.get()); 45 46 load_finished_ = false; 47 last_type_ = syncer::UNSPECIFIED; 48 last_error_ = syncer::SyncError(); 49 } 50 51 virtual void TearDown() OVERRIDE { 52 controller_ = NULL; 53 local_device_.reset(); 54 } 55 56 void Start() { 57 controller_->LoadModels( 58 base::Bind(&DeviceInfoDataTypeControllerTest::OnLoadFinished, 59 weak_ptr_factory_.GetWeakPtr())); 60 } 61 62 void OnLoadFinished(syncer::ModelType type, syncer::SyncError error) { 63 load_finished_ = true; 64 last_type_ = type; 65 last_error_ = error; 66 } 67 68 testing::AssertionResult LoadResult() { 69 if (!load_finished_) { 70 return testing::AssertionFailure() << "OnLoadFinished wasn't called"; 71 } 72 73 if (last_error_.IsSet()) { 74 return testing::AssertionFailure() 75 << "OnLoadFinished was called with a SyncError: " 76 << last_error_.ToString(); 77 } 78 79 if (last_type_ != syncer::DEVICE_INFO) { 80 return testing::AssertionFailure() 81 << "OnLoadFinished was called with a wrong sync type: " 82 << last_type_; 83 } 84 85 return testing::AssertionSuccess(); 86 } 87 88 protected: 89 scoped_refptr<DeviceInfoDataTypeController> controller_; 90 scoped_ptr<LocalDeviceInfoProviderMock> local_device_; 91 bool load_finished_; 92 93 private: 94 content::TestBrowserThreadBundle thread_bundle_; 95 ProfileSyncComponentsFactoryMock profile_sync_factory_; 96 base::WeakPtrFactory<DeviceInfoDataTypeControllerTest> weak_ptr_factory_; 97 syncer::ModelType last_type_; 98 syncer::SyncError last_error_; 99 }; 100 101 TEST_F(DeviceInfoDataTypeControllerTest, StartModels) { 102 Start(); 103 EXPECT_EQ(DataTypeController::MODEL_LOADED, controller_->state()); 104 EXPECT_TRUE(LoadResult()); 105 } 106 107 TEST_F(DeviceInfoDataTypeControllerTest, StartModelsDelayedByLocalDevice) { 108 local_device_->SetInitialized(false); 109 Start(); 110 EXPECT_FALSE(load_finished_); 111 EXPECT_EQ(DataTypeController::MODEL_STARTING, controller_->state()); 112 113 local_device_->SetInitialized(true); 114 EXPECT_EQ(DataTypeController::MODEL_LOADED, controller_->state()); 115 EXPECT_TRUE(LoadResult()); 116 } 117 118 } // namespace 119 120 } // namespace browser_sync 121