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. 4 5 #include <vector> 6 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_factory.h" 12 #include "chrome/browser/sync/managed_user_signin_manager_wrapper.h" 13 #include "chrome/browser/sync/profile_sync_components_factory_impl.h" 14 #include "chrome/browser/sync/profile_sync_service.h" 15 #include "chrome/browser/sync/profile_sync_service_factory.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 "components/signin/core/browser/profile_oauth2_token_service.h" 20 #include "components/sync_driver/data_type_controller.h" 21 #include "content/public/test/test_browser_thread_bundle.h" 22 #include "google_apis/gaia/gaia_constants.h" 23 #include "google_apis/gaia/oauth2_token_service.h" 24 #include "testing/gtest/include/gtest/gtest.h" 25 #include "ui/app_list/app_list_switches.h" 26 27 using browser_sync::DataTypeController; 28 29 const char kAccountId[] = "testuser (at) test.com"; 30 31 class ProfileSyncComponentsFactoryImplTest : public testing::Test { 32 protected: 33 ProfileSyncComponentsFactoryImplTest() 34 : thread_bundle_(content::TestBrowserThreadBundle::DEFAULT) {} 35 36 virtual void SetUp() { 37 profile_.reset(new TestingProfile()); 38 base::FilePath program_path(FILE_PATH_LITERAL("chrome.exe")); 39 command_line_.reset(new CommandLine(program_path)); 40 scope_set_.insert(GaiaConstants::kChromeSyncOAuth2Scope); 41 } 42 43 // Returns the collection of default datatypes. 44 static std::vector<syncer::ModelType> DefaultDatatypes() { 45 std::vector<syncer::ModelType> datatypes; 46 datatypes.push_back(syncer::APPS); 47 #if defined(ENABLE_APP_LIST) 48 if (app_list::switches::IsAppListSyncEnabled()) 49 datatypes.push_back(syncer::APP_LIST); 50 #endif 51 datatypes.push_back(syncer::APP_SETTINGS); 52 datatypes.push_back(syncer::AUTOFILL); 53 datatypes.push_back(syncer::AUTOFILL_PROFILE); 54 datatypes.push_back(syncer::BOOKMARKS); 55 #if defined(OS_LINUX) || defined(OS_WIN) || defined(OS_CHROMEOS) 56 datatypes.push_back(syncer::DICTIONARY); 57 #endif 58 datatypes.push_back(syncer::EXTENSIONS); 59 datatypes.push_back(syncer::EXTENSION_SETTINGS); 60 datatypes.push_back(syncer::HISTORY_DELETE_DIRECTIVES); 61 datatypes.push_back(syncer::PASSWORDS); 62 datatypes.push_back(syncer::PREFERENCES); 63 datatypes.push_back(syncer::PRIORITY_PREFERENCES); 64 datatypes.push_back(syncer::SEARCH_ENGINES); 65 datatypes.push_back(syncer::SESSIONS); 66 datatypes.push_back(syncer::PROXY_TABS); 67 datatypes.push_back(syncer::THEMES); 68 datatypes.push_back(syncer::TYPED_URLS); 69 datatypes.push_back(syncer::FAVICON_TRACKING); 70 datatypes.push_back(syncer::FAVICON_IMAGES); 71 datatypes.push_back(syncer::SUPERVISED_USERS); 72 datatypes.push_back(syncer::SUPERVISED_USER_SHARED_SETTINGS); 73 74 return datatypes; 75 } 76 77 // Returns the number of default datatypes. 78 static size_t DefaultDatatypesCount() { 79 return DefaultDatatypes().size(); 80 } 81 82 // Asserts that all the default datatypes are in |map|, except 83 // for |exception_type|, which unless it is UNDEFINED, is asserted to 84 // not be in |map|. 85 static void CheckDefaultDatatypesInMapExcept( 86 DataTypeController::StateMap* map, 87 syncer::ModelTypeSet exception_types) { 88 std::vector<syncer::ModelType> defaults = DefaultDatatypes(); 89 std::vector<syncer::ModelType>::iterator iter; 90 for (iter = defaults.begin(); iter != defaults.end(); ++iter) { 91 if (exception_types.Has(*iter)) 92 EXPECT_EQ(0U, map->count(*iter)) 93 << *iter << " found in dataypes map, shouldn't be there."; 94 else 95 EXPECT_EQ(1U, map->count(*iter)) 96 << *iter << " not found in datatypes map"; 97 } 98 } 99 100 // Asserts that if you disable types via the command line, all other types 101 // are enabled. 102 void TestSwitchDisablesType(syncer::ModelTypeSet types) { 103 command_line_->AppendSwitchASCII(switches::kDisableSyncTypes, 104 syncer::ModelTypeSetToString(types)); 105 GURL sync_service_url = 106 ProfileSyncService::GetSyncServiceURL(*command_line_); 107 ProfileOAuth2TokenService* token_service = 108 ProfileOAuth2TokenServiceFactory::GetForProfile(profile_.get()); 109 scoped_ptr<ProfileSyncService> pss(new ProfileSyncService( 110 new ProfileSyncComponentsFactoryImpl( 111 profile_.get(), 112 command_line_.get(), 113 ProfileSyncService::GetSyncServiceURL(*command_line_), 114 kAccountId, 115 scope_set_, 116 token_service, 117 profile_->GetRequestContext()), 118 profile_.get(), 119 make_scoped_ptr<ManagedUserSigninManagerWrapper>(NULL), 120 token_service, 121 browser_sync::MANUAL_START)); 122 pss->factory()->RegisterDataTypes(pss.get()); 123 DataTypeController::StateMap controller_states; 124 pss->GetDataTypeControllerStates(&controller_states); 125 EXPECT_EQ(DefaultDatatypesCount() - types.Size(), controller_states.size()); 126 CheckDefaultDatatypesInMapExcept(&controller_states, types); 127 } 128 129 content::TestBrowserThreadBundle thread_bundle_; 130 scoped_ptr<Profile> profile_; 131 scoped_ptr<CommandLine> command_line_; 132 OAuth2TokenService::ScopeSet scope_set_; 133 }; 134 135 TEST_F(ProfileSyncComponentsFactoryImplTest, CreatePSSDefault) { 136 ProfileOAuth2TokenService* token_service = 137 ProfileOAuth2TokenServiceFactory::GetForProfile(profile_.get()); 138 scoped_ptr<ProfileSyncService> pss(new ProfileSyncService( 139 new ProfileSyncComponentsFactoryImpl( 140 profile_.get(), 141 command_line_.get(), 142 ProfileSyncService::GetSyncServiceURL(*command_line_), 143 kAccountId, 144 scope_set_, 145 token_service, 146 profile_->GetRequestContext()), 147 profile_.get(), 148 make_scoped_ptr<ManagedUserSigninManagerWrapper>(NULL), 149 token_service, 150 browser_sync::MANUAL_START)); 151 pss->factory()->RegisterDataTypes(pss.get()); 152 DataTypeController::StateMap controller_states; 153 pss->GetDataTypeControllerStates(&controller_states); 154 EXPECT_EQ(DefaultDatatypesCount(), controller_states.size()); 155 CheckDefaultDatatypesInMapExcept(&controller_states, syncer::ModelTypeSet()); 156 } 157 158 TEST_F(ProfileSyncComponentsFactoryImplTest, CreatePSSDisableOne) { 159 TestSwitchDisablesType(syncer::ModelTypeSet(syncer::AUTOFILL)); 160 } 161 162 TEST_F(ProfileSyncComponentsFactoryImplTest, CreatePSSDisableMultiple) { 163 TestSwitchDisablesType( 164 syncer::ModelTypeSet(syncer::AUTOFILL_PROFILE, syncer::BOOKMARKS)); 165 } 166