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