Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / sync / profile_sync_components_factory_impl_unittest.cc
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/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/browser/sync/supervised_user_signin_manager_wrapper.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 sync_driver::DataTypeController;
28
29 class ProfileSyncComponentsFactoryImplTest : public testing::Test {
30  protected:
31   ProfileSyncComponentsFactoryImplTest()
32       : thread_bundle_(content::TestBrowserThreadBundle::DEFAULT) {}
33
34   virtual void SetUp() {
35     profile_.reset(new TestingProfile());
36     base::FilePath program_path(FILE_PATH_LITERAL("chrome.exe"));
37     command_line_.reset(new CommandLine(program_path));
38     scope_set_.insert(GaiaConstants::kChromeSyncOAuth2Scope);
39   }
40
41   // Returns the collection of default datatypes.
42   static std::vector<syncer::ModelType> DefaultDatatypes() {
43     std::vector<syncer::ModelType> datatypes;
44     datatypes.push_back(syncer::APPS);
45 #if defined(ENABLE_APP_LIST)
46     if (app_list::switches::IsAppListSyncEnabled())
47       datatypes.push_back(syncer::APP_LIST);
48 #endif
49     datatypes.push_back(syncer::APP_SETTINGS);
50     datatypes.push_back(syncer::AUTOFILL);
51     datatypes.push_back(syncer::AUTOFILL_PROFILE);
52     datatypes.push_back(syncer::BOOKMARKS);
53     datatypes.push_back(syncer::DEVICE_INFO);
54 #if defined(OS_LINUX) || defined(OS_WIN) || defined(OS_CHROMEOS)
55     datatypes.push_back(syncer::DICTIONARY);
56 #endif
57     datatypes.push_back(syncer::EXTENSIONS);
58     datatypes.push_back(syncer::EXTENSION_SETTINGS);
59     datatypes.push_back(syncer::HISTORY_DELETE_DIRECTIVES);
60     datatypes.push_back(syncer::PASSWORDS);
61     datatypes.push_back(syncer::PREFERENCES);
62     datatypes.push_back(syncer::PRIORITY_PREFERENCES);
63     datatypes.push_back(syncer::SEARCH_ENGINES);
64     datatypes.push_back(syncer::SESSIONS);
65     datatypes.push_back(syncer::PROXY_TABS);
66     datatypes.push_back(syncer::THEMES);
67     datatypes.push_back(syncer::TYPED_URLS);
68     datatypes.push_back(syncer::FAVICON_TRACKING);
69     datatypes.push_back(syncer::FAVICON_IMAGES);
70     datatypes.push_back(syncer::SUPERVISED_USERS);
71     datatypes.push_back(syncer::SUPERVISED_USER_SETTINGS);
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         scoped_ptr<ProfileSyncComponentsFactory>(
111             new ProfileSyncComponentsFactoryImpl(
112                 profile_.get(),
113                 command_line_.get(),
114                 ProfileSyncService::GetSyncServiceURL(*command_line_),
115                 token_service,
116                 profile_->GetRequestContext())),
117         profile_.get(),
118         make_scoped_ptr<SupervisedUserSigninManagerWrapper>(NULL),
119         token_service,
120         browser_sync::MANUAL_START));
121     pss->factory()->RegisterDataTypes(pss.get());
122     DataTypeController::StateMap controller_states;
123     pss->GetDataTypeControllerStates(&controller_states);
124     EXPECT_EQ(DefaultDatatypesCount() - types.Size(), controller_states.size());
125     CheckDefaultDatatypesInMapExcept(&controller_states, types);
126   }
127
128   content::TestBrowserThreadBundle thread_bundle_;
129   scoped_ptr<Profile> profile_;
130   scoped_ptr<CommandLine> command_line_;
131   OAuth2TokenService::ScopeSet scope_set_;
132 };
133
134 TEST_F(ProfileSyncComponentsFactoryImplTest, CreatePSSDefault) {
135   ProfileOAuth2TokenService* token_service =
136       ProfileOAuth2TokenServiceFactory::GetForProfile(profile_.get());
137   scoped_ptr<ProfileSyncService> pss(new ProfileSyncService(
138       scoped_ptr<ProfileSyncComponentsFactory>(
139           new ProfileSyncComponentsFactoryImpl(
140               profile_.get(),
141               command_line_.get(),
142               ProfileSyncService::GetSyncServiceURL(*command_line_),
143               token_service,
144               profile_->GetRequestContext())),
145       profile_.get(),
146       make_scoped_ptr<SupervisedUserSigninManagerWrapper>(NULL),
147       token_service,
148       browser_sync::MANUAL_START));
149   pss->factory()->RegisterDataTypes(pss.get());
150   DataTypeController::StateMap controller_states;
151   pss->GetDataTypeControllerStates(&controller_states);
152   EXPECT_EQ(DefaultDatatypesCount(), controller_states.size());
153   CheckDefaultDatatypesInMapExcept(&controller_states, syncer::ModelTypeSet());
154 }
155
156 TEST_F(ProfileSyncComponentsFactoryImplTest, CreatePSSDisableOne) {
157   TestSwitchDisablesType(syncer::ModelTypeSet(syncer::AUTOFILL));
158 }
159
160 TEST_F(ProfileSyncComponentsFactoryImplTest, CreatePSSDisableMultiple) {
161   TestSwitchDisablesType(
162       syncer::ModelTypeSet(syncer::AUTOFILL_PROFILE, syncer::BOOKMARKS));
163 }