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.
7 #include "base/command_line.h"
8 #include "base/files/file_path.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/message_loop/message_loop.h"
11 #include "chrome/browser/signin/profile_oauth2_token_service.h"
12 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
13 #include "chrome/browser/sync/glue/data_type_controller.h"
14 #include "chrome/browser/sync/profile_sync_components_factory_impl.h"
15 #include "chrome/browser/sync/profile_sync_service.h"
16 #include "chrome/common/chrome_switches.h"
17 #include "chrome/common/chrome_version_info.h"
18 #include "chrome/test/base/testing_profile.h"
19 #include "content/public/test/test_browser_thread.h"
20 #include "testing/gtest/include/gtest/gtest.h"
22 using browser_sync::DataTypeController
;
23 using content::BrowserThread
;
25 class ProfileSyncComponentsFactoryImplTest
: public testing::Test
{
27 ProfileSyncComponentsFactoryImplTest()
28 : ui_thread_(BrowserThread::UI
, &message_loop_
) {}
30 virtual void SetUp() {
31 profile_
.reset(new TestingProfile());
32 base::FilePath
program_path(FILE_PATH_LITERAL("chrome.exe"));
33 command_line_
.reset(new CommandLine(program_path
));
36 // Returns the collection of default datatypes.
37 static std::vector
<syncer::ModelType
> DefaultDatatypes() {
38 std::vector
<syncer::ModelType
> datatypes
;
39 datatypes
.push_back(syncer::APPS
);
40 datatypes
.push_back(syncer::APP_SETTINGS
);
41 datatypes
.push_back(syncer::AUTOFILL
);
42 datatypes
.push_back(syncer::AUTOFILL_PROFILE
);
43 datatypes
.push_back(syncer::BOOKMARKS
);
44 #if defined(OS_LINUX) || defined(OS_WIN) || defined(OS_CHROMEOS)
45 datatypes
.push_back(syncer::DICTIONARY
);
47 datatypes
.push_back(syncer::EXTENSIONS
);
48 datatypes
.push_back(syncer::EXTENSION_SETTINGS
);
49 datatypes
.push_back(syncer::HISTORY_DELETE_DIRECTIVES
);
50 datatypes
.push_back(syncer::PASSWORDS
);
51 datatypes
.push_back(syncer::PREFERENCES
);
52 datatypes
.push_back(syncer::PRIORITY_PREFERENCES
);
53 datatypes
.push_back(syncer::SEARCH_ENGINES
);
54 datatypes
.push_back(syncer::SESSIONS
);
55 datatypes
.push_back(syncer::PROXY_TABS
);
56 datatypes
.push_back(syncer::THEMES
);
57 datatypes
.push_back(syncer::TYPED_URLS
);
58 datatypes
.push_back(syncer::FAVICON_TRACKING
);
59 datatypes
.push_back(syncer::FAVICON_IMAGES
);
60 datatypes
.push_back(syncer::SYNCED_NOTIFICATIONS
);
61 datatypes
.push_back(syncer::MANAGED_USERS
);
62 // TODO(bauerb): Enable once the Sync server has been updated to know about
64 // datatypes.push_back(syncer::MANAGED_USER_SHARED_SETTINGS);
69 // Returns the number of default datatypes.
70 static size_t DefaultDatatypesCount() {
71 return DefaultDatatypes().size();
74 // Asserts that all the default datatypes are in |map|, except
75 // for |exception_type|, which unless it is UNDEFINED, is asserted to
77 static void CheckDefaultDatatypesInMapExcept(
78 DataTypeController::StateMap
* map
,
79 syncer::ModelType exception_type
) {
80 std::vector
<syncer::ModelType
> defaults
= DefaultDatatypes();
81 std::vector
<syncer::ModelType
>::iterator iter
;
82 for (iter
= defaults
.begin(); iter
!= defaults
.end(); ++iter
) {
83 if (exception_type
!= syncer::UNSPECIFIED
&& exception_type
== *iter
)
84 EXPECT_EQ(0U, map
->count(*iter
))
85 << *iter
<< " found in dataypes map, shouldn't be there.";
87 EXPECT_EQ(1U, map
->count(*iter
))
88 << *iter
<< " not found in datatypes map";
92 // Asserts that if you apply the command line switch |cmd_switch|,
93 // all types are enabled except for |type|, which is disabled.
94 void TestSwitchDisablesType(const char* cmd_switch
,
95 syncer::ModelType type
) {
96 command_line_
->AppendSwitch(cmd_switch
);
97 scoped_ptr
<ProfileSyncService
> pss(
98 new ProfileSyncService(
99 new ProfileSyncComponentsFactoryImpl(profile_
.get(),
100 command_line_
.get()),
103 ProfileOAuth2TokenServiceFactory::GetForProfile(profile_
.get()),
104 ProfileSyncService::MANUAL_START
));
105 pss
->factory()->RegisterDataTypes(pss
.get());
106 DataTypeController::StateMap controller_states
;
107 pss
->GetDataTypeControllerStates(&controller_states
);
108 EXPECT_EQ(DefaultDatatypesCount() - 1, controller_states
.size());
109 CheckDefaultDatatypesInMapExcept(&controller_states
, type
);
112 base::MessageLoop message_loop_
;
113 content::TestBrowserThread ui_thread_
;
114 scoped_ptr
<Profile
> profile_
;
115 scoped_ptr
<CommandLine
> command_line_
;
118 TEST_F(ProfileSyncComponentsFactoryImplTest
, CreatePSSDefault
) {
119 scoped_ptr
<ProfileSyncService
> pss(new ProfileSyncService(
120 new ProfileSyncComponentsFactoryImpl(profile_
.get(), command_line_
.get()),
123 ProfileOAuth2TokenServiceFactory::GetForProfile(profile_
.get()),
124 ProfileSyncService::MANUAL_START
));
125 pss
->factory()->RegisterDataTypes(pss
.get());
126 DataTypeController::StateMap controller_states
;
127 pss
->GetDataTypeControllerStates(&controller_states
);
128 EXPECT_EQ(DefaultDatatypesCount(), controller_states
.size());
129 CheckDefaultDatatypesInMapExcept(&controller_states
, syncer::UNSPECIFIED
);
132 TEST_F(ProfileSyncComponentsFactoryImplTest
, CreatePSSDisableAutofill
) {
133 TestSwitchDisablesType(switches::kDisableSyncAutofill
,
137 TEST_F(ProfileSyncComponentsFactoryImplTest
, CreatePSSDisableBookmarks
) {
138 TestSwitchDisablesType(switches::kDisableSyncBookmarks
,
142 TEST_F(ProfileSyncComponentsFactoryImplTest
, CreatePSSDisablePreferences
) {
143 TestSwitchDisablesType(switches::kDisableSyncPreferences
,
144 syncer::PREFERENCES
);
147 TEST_F(ProfileSyncComponentsFactoryImplTest
, CreatePSSDisableThemes
) {
148 TestSwitchDisablesType(switches::kDisableSyncThemes
,
152 TEST_F(ProfileSyncComponentsFactoryImplTest
, CreatePSSDisableExtensions
) {
153 TestSwitchDisablesType(switches::kDisableSyncExtensions
,
157 TEST_F(ProfileSyncComponentsFactoryImplTest
, CreatePSSDisableApps
) {
158 TestSwitchDisablesType(switches::kDisableSyncApps
,
162 TEST_F(ProfileSyncComponentsFactoryImplTest
, CreatePSSDisableAutofillProfile
) {
163 TestSwitchDisablesType(switches::kDisableSyncAutofillProfile
,
164 syncer::AUTOFILL_PROFILE
);
167 TEST_F(ProfileSyncComponentsFactoryImplTest
, CreatePSSDisablePasswords
) {
168 TestSwitchDisablesType(switches::kDisableSyncPasswords
,