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.
6 #include "base/bind_helpers.h"
7 #include "base/callback.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "base/run_loop.h"
10 #include "base/tracked_objects.h"
11 #include "chrome/browser/search_engines/template_url_service_factory.h"
12 #include "chrome/browser/search_engines/template_url_service_test_util.h"
13 #include "chrome/browser/sync/glue/search_engine_data_type_controller.h"
14 #include "chrome/browser/sync/profile_sync_components_factory_mock.h"
15 #include "chrome/browser/sync/profile_sync_service_mock.h"
16 #include "chrome/test/base/profile_mock.h"
17 #include "components/sync_driver/data_type_controller_mock.h"
18 #include "components/sync_driver/fake_generic_change_processor.h"
19 #include "sync/api/fake_syncable_service.h"
20 #include "testing/gtest/include/gtest/gtest.h"
24 using testing::InvokeWithoutArgs;
25 using testing::Return;
26 using testing::SetArgumentPointee;
28 namespace browser_sync {
31 class SyncSearchEngineDataTypeControllerTest : public testing::Test {
33 SyncSearchEngineDataTypeControllerTest() { }
35 virtual void SetUp() {
37 service_.reset(new ProfileSyncServiceMock(test_util_.profile()));
38 profile_sync_factory_.reset(new ProfileSyncComponentsFactoryMock());
39 // Feed the DTC test_util_'s profile so it is reused later.
40 // This allows us to control the associated TemplateURLService.
42 new SearchEngineDataTypeController(
43 profile_sync_factory_.get(), test_util_.profile(),
44 DataTypeController::DisableTypeCallback());
47 virtual void TearDown() {
48 // Must be done before we pump the loop.
49 syncable_service_.StopSyncing(syncer::SEARCH_ENGINES);
50 search_engine_dtc_ = NULL;
52 test_util_.TearDown();
56 // Pre-emptively get the TURL Service and make sure it is loaded.
57 void PreloadTemplateURLService() {
58 test_util_.VerifyLoad();
61 void SetStartExpectations() {
62 search_engine_dtc_->SetGenericChangeProcessorFactoryForTest(
63 make_scoped_ptr<GenericChangeProcessorFactory>(
64 new FakeGenericChangeProcessorFactory(make_scoped_ptr(
65 new FakeGenericChangeProcessor(profile_sync_factory_.get())))));
66 EXPECT_CALL(model_load_callback_, Run(_, _));
67 EXPECT_CALL(*profile_sync_factory_,
68 GetSyncableServiceForType(syncer::SEARCH_ENGINES)).
69 WillOnce(Return(syncable_service_.AsWeakPtr()));
73 search_engine_dtc_->LoadModels(
74 base::Bind(&ModelLoadCallbackMock::Run,
75 base::Unretained(&model_load_callback_)));
76 search_engine_dtc_->StartAssociating(
77 base::Bind(&StartCallbackMock::Run,
78 base::Unretained(&start_callback_)));
81 // This also manages a BrowserThread and MessageLoop for us. Note that this
82 // must be declared here as the destruction order of the BrowserThread
83 // matters - we could leak if this is declared below.
84 TemplateURLServiceTestUtil test_util_;
85 scoped_refptr<SearchEngineDataTypeController> search_engine_dtc_;
86 scoped_ptr<ProfileSyncComponentsFactoryMock> profile_sync_factory_;
87 scoped_ptr<ProfileSyncServiceMock> service_;
88 syncer::FakeSyncableService syncable_service_;
89 StartCallbackMock start_callback_;
90 ModelLoadCallbackMock model_load_callback_;
93 TEST_F(SyncSearchEngineDataTypeControllerTest, StartURLServiceReady) {
94 SetStartExpectations();
95 // We want to start ready.
96 PreloadTemplateURLService();
97 EXPECT_CALL(start_callback_, Run(DataTypeController::OK, _, _));
99 EXPECT_EQ(DataTypeController::NOT_RUNNING, search_engine_dtc_->state());
100 EXPECT_FALSE(syncable_service_.syncing());
102 EXPECT_EQ(DataTypeController::RUNNING, search_engine_dtc_->state());
103 EXPECT_TRUE(syncable_service_.syncing());
106 TEST_F(SyncSearchEngineDataTypeControllerTest, StartURLServiceNotReady) {
107 EXPECT_CALL(model_load_callback_, Run(_, _));
108 EXPECT_FALSE(syncable_service_.syncing());
109 search_engine_dtc_->LoadModels(
110 base::Bind(&ModelLoadCallbackMock::Run,
111 base::Unretained(&model_load_callback_)));
112 EXPECT_EQ(DataTypeController::MODEL_STARTING, search_engine_dtc_->state());
113 EXPECT_FALSE(syncable_service_.syncing());
115 // Send the notification that the TemplateURLService has started.
116 PreloadTemplateURLService();
117 EXPECT_EQ(DataTypeController::MODEL_LOADED, search_engine_dtc_->state());
119 // Wait until WebDB is loaded before we shut it down.
120 base::RunLoop().RunUntilIdle();
123 TEST_F(SyncSearchEngineDataTypeControllerTest, StartAssociationFailed) {
124 SetStartExpectations();
125 PreloadTemplateURLService();
126 EXPECT_CALL(start_callback_,
127 Run(DataTypeController::ASSOCIATION_FAILED, _, _));
128 syncable_service_.set_merge_data_and_start_syncing_error(
129 syncer::SyncError(FROM_HERE,
130 syncer::SyncError::DATATYPE_ERROR,
132 syncer::SEARCH_ENGINES));
135 EXPECT_EQ(DataTypeController::DISABLED, search_engine_dtc_->state());
136 EXPECT_FALSE(syncable_service_.syncing());
137 search_engine_dtc_->Stop();
138 EXPECT_EQ(DataTypeController::NOT_RUNNING, search_engine_dtc_->state());
139 EXPECT_FALSE(syncable_service_.syncing());
142 TEST_F(SyncSearchEngineDataTypeControllerTest, Stop) {
143 SetStartExpectations();
144 PreloadTemplateURLService();
145 EXPECT_CALL(start_callback_, Run(DataTypeController::OK, _, _));
147 EXPECT_EQ(DataTypeController::NOT_RUNNING, search_engine_dtc_->state());
148 EXPECT_FALSE(syncable_service_.syncing());
150 EXPECT_EQ(DataTypeController::RUNNING, search_engine_dtc_->state());
151 EXPECT_TRUE(syncable_service_.syncing());
152 search_engine_dtc_->Stop();
153 EXPECT_EQ(DataTypeController::NOT_RUNNING, search_engine_dtc_->state());
154 EXPECT_FALSE(syncable_service_.syncing());
158 } // namespace browser_sync