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/device_info_data_type_controller.h"
10 #include "chrome/browser/sync/glue/local_device_info_provider_mock.h"
11 #include "chrome/browser/sync/profile_sync_components_factory_mock.h"
12 #include "content/public/test/test_browser_thread_bundle.h"
13 #include "testing/gtest/include/gtest/gtest.h"
15 using sync_driver::DataTypeController;
17 namespace browser_sync {
21 class DeviceInfoDataTypeControllerTest : public testing::Test {
23 DeviceInfoDataTypeControllerTest()
24 : load_finished_(false),
25 thread_bundle_(content::TestBrowserThreadBundle::DEFAULT),
26 weak_ptr_factory_(this),
27 last_type_(syncer::UNSPECIFIED) {}
28 virtual ~DeviceInfoDataTypeControllerTest() {}
30 virtual void SetUp() OVERRIDE {
31 local_device_.reset(new LocalDeviceInfoProviderMock(
33 "Wayne Gretzky's Hacking Box",
36 sync_pb::SyncEnums_DeviceType_TYPE_LINUX,
39 controller_ = new DeviceInfoDataTypeController(
40 &profile_sync_factory_,
43 load_finished_ = false;
44 last_type_ = syncer::UNSPECIFIED;
45 last_error_ = syncer::SyncError();
48 virtual void TearDown() OVERRIDE {
50 local_device_.reset();
54 controller_->LoadModels(
55 base::Bind(&DeviceInfoDataTypeControllerTest::OnLoadFinished,
56 weak_ptr_factory_.GetWeakPtr()));
59 void OnLoadFinished(syncer::ModelType type, syncer::SyncError error) {
60 load_finished_ = true;
65 testing::AssertionResult LoadResult() {
66 if (!load_finished_) {
67 return testing::AssertionFailure() << "OnLoadFinished wasn't called";
70 if (last_error_.IsSet()) {
71 return testing::AssertionFailure()
72 << "OnLoadFinished was called with a SyncError: "
73 << last_error_.ToString();
76 if (last_type_ != syncer::DEVICE_INFO) {
77 return testing::AssertionFailure()
78 << "OnLoadFinished was called with a wrong sync type: "
82 return testing::AssertionSuccess();
86 scoped_refptr<DeviceInfoDataTypeController> controller_;
87 scoped_ptr<LocalDeviceInfoProviderMock> local_device_;
91 content::TestBrowserThreadBundle thread_bundle_;
92 ProfileSyncComponentsFactoryMock profile_sync_factory_;
93 base::WeakPtrFactory<DeviceInfoDataTypeControllerTest> weak_ptr_factory_;
94 syncer::ModelType last_type_;
95 syncer::SyncError last_error_;
98 TEST_F(DeviceInfoDataTypeControllerTest, StartModels) {
100 EXPECT_EQ(DataTypeController::MODEL_LOADED, controller_->state());
101 EXPECT_TRUE(LoadResult());
104 TEST_F(DeviceInfoDataTypeControllerTest, StartModelsDelayedByLocalDevice) {
105 local_device_->SetInitialized(false);
107 EXPECT_FALSE(load_finished_);
108 EXPECT_EQ(DataTypeController::MODEL_STARTING, controller_->state());
110 local_device_->SetInitialized(true);
111 EXPECT_EQ(DataTypeController::MODEL_LOADED, controller_->state());
112 EXPECT_TRUE(LoadResult());
117 } // namespace browser_sync