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.
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"
15 using sync_driver::DataTypeController;
16 using sync_driver::DeviceInfoDataTypeController;
18 namespace browser_sync {
22 class DeviceInfoDataTypeControllerTest : public testing::Test {
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() {}
31 virtual void SetUp() OVERRIDE {
32 local_device_.reset(new LocalDeviceInfoProviderMock(
34 "Wayne Gretzky's Hacking Box",
37 sync_pb::SyncEnums_DeviceType_TYPE_LINUX,
40 controller_ = new DeviceInfoDataTypeController(
41 base::MessageLoopProxy::current(),
43 &profile_sync_factory_,
46 load_finished_ = false;
47 last_type_ = syncer::UNSPECIFIED;
48 last_error_ = syncer::SyncError();
51 virtual void TearDown() OVERRIDE {
53 local_device_.reset();
57 controller_->LoadModels(
58 base::Bind(&DeviceInfoDataTypeControllerTest::OnLoadFinished,
59 weak_ptr_factory_.GetWeakPtr()));
62 void OnLoadFinished(syncer::ModelType type, syncer::SyncError error) {
63 load_finished_ = true;
68 testing::AssertionResult LoadResult() {
69 if (!load_finished_) {
70 return testing::AssertionFailure() << "OnLoadFinished wasn't called";
73 if (last_error_.IsSet()) {
74 return testing::AssertionFailure()
75 << "OnLoadFinished was called with a SyncError: "
76 << last_error_.ToString();
79 if (last_type_ != syncer::DEVICE_INFO) {
80 return testing::AssertionFailure()
81 << "OnLoadFinished was called with a wrong sync type: "
85 return testing::AssertionSuccess();
89 scoped_refptr<DeviceInfoDataTypeController> controller_;
90 scoped_ptr<LocalDeviceInfoProviderMock> local_device_;
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_;
101 TEST_F(DeviceInfoDataTypeControllerTest, StartModels) {
103 EXPECT_EQ(DataTypeController::MODEL_LOADED, controller_->state());
104 EXPECT_TRUE(LoadResult());
107 TEST_F(DeviceInfoDataTypeControllerTest, StartModelsDelayedByLocalDevice) {
108 local_device_->SetInitialized(false);
110 EXPECT_FALSE(load_finished_);
111 EXPECT_EQ(DataTypeController::MODEL_STARTING, controller_->state());
113 local_device_->SetInitialized(true);
114 EXPECT_EQ(DataTypeController::MODEL_LOADED, controller_->state());
115 EXPECT_TRUE(LoadResult());
120 } // namespace browser_sync