Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / sync / glue / device_info_data_type_controller_unittest.cc
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/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"
14
15 using sync_driver::DataTypeController;
16
17 namespace browser_sync {
18
19 namespace {
20
21 class DeviceInfoDataTypeControllerTest : public testing::Test {
22  public:
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() {}
29
30   virtual void SetUp() OVERRIDE {
31     local_device_.reset(new LocalDeviceInfoProviderMock(
32         "cache_guid",
33         "Wayne Gretzky's Hacking Box",
34         "Chromium 10k",
35         "Chrome 10k",
36         sync_pb::SyncEnums_DeviceType_TYPE_LINUX,
37         "device_id"));
38
39     controller_ = new DeviceInfoDataTypeController(
40         &profile_sync_factory_,
41         local_device_.get());
42
43     load_finished_ = false;
44     last_type_ = syncer::UNSPECIFIED;
45     last_error_ = syncer::SyncError();
46   }
47
48   virtual void TearDown() OVERRIDE {
49     controller_ = NULL;
50     local_device_.reset();
51   }
52
53   void Start() {
54     controller_->LoadModels(
55         base::Bind(&DeviceInfoDataTypeControllerTest::OnLoadFinished,
56                    weak_ptr_factory_.GetWeakPtr()));
57   }
58
59   void OnLoadFinished(syncer::ModelType type, syncer::SyncError error) {
60     load_finished_ = true;
61     last_type_ = type;
62     last_error_ = error;
63   }
64
65   testing::AssertionResult LoadResult() {
66     if (!load_finished_) {
67       return testing::AssertionFailure() << "OnLoadFinished wasn't called";
68     }
69
70     if (last_error_.IsSet()) {
71       return testing::AssertionFailure()
72              << "OnLoadFinished was called with a SyncError: "
73              << last_error_.ToString();
74     }
75
76     if (last_type_ != syncer::DEVICE_INFO) {
77       return testing::AssertionFailure()
78              << "OnLoadFinished was called with a wrong sync type: "
79              << last_type_;
80     }
81
82     return testing::AssertionSuccess();
83   }
84
85  protected:
86   scoped_refptr<DeviceInfoDataTypeController> controller_;
87   scoped_ptr<LocalDeviceInfoProviderMock> local_device_;
88   bool load_finished_;
89
90  private:
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_;
96 };
97
98 TEST_F(DeviceInfoDataTypeControllerTest, StartModels) {
99   Start();
100   EXPECT_EQ(DataTypeController::MODEL_LOADED, controller_->state());
101   EXPECT_TRUE(LoadResult());
102 }
103
104 TEST_F(DeviceInfoDataTypeControllerTest, StartModelsDelayedByLocalDevice) {
105   local_device_->SetInitialized(false);
106   Start();
107   EXPECT_FALSE(load_finished_);
108   EXPECT_EQ(DataTypeController::MODEL_STARTING, controller_->state());
109
110   local_device_->SetInitialized(true);
111   EXPECT_EQ(DataTypeController::MODEL_LOADED, controller_->state());
112   EXPECT_TRUE(LoadResult());
113 }
114
115 }  // namespace
116
117 }  // namespace browser_sync