Upstream version 6.35.121.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/common/chrome_switches.h"
15 #include "chrome/common/chrome_version_info.h"
16 #include "chrome/test/base/testing_profile.h"
17 #include "components/signin/core/browser/profile_oauth2_token_service.h"
18 #include "components/sync_driver/data_type_controller.h"
19 #include "content/public/test/test_browser_thread.h"
20 #include "testing/gtest/include/gtest/gtest.h"
21
22 using browser_sync::DataTypeController;
23 using content::BrowserThread;
24
25 class ProfileSyncComponentsFactoryImplTest : public testing::Test {
26  protected:
27   ProfileSyncComponentsFactoryImplTest()
28       : ui_thread_(BrowserThread::UI, &message_loop_) {}
29
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));
34   }
35
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 #if defined(ENABLE_APP_LIST)
41     datatypes.push_back(syncer::APP_LIST);
42 #endif
43     datatypes.push_back(syncer::APP_SETTINGS);
44     datatypes.push_back(syncer::AUTOFILL);
45     datatypes.push_back(syncer::AUTOFILL_PROFILE);
46     datatypes.push_back(syncer::BOOKMARKS);
47 #if defined(OS_LINUX) || defined(OS_WIN) || defined(OS_CHROMEOS)
48     datatypes.push_back(syncer::DICTIONARY);
49 #endif
50     datatypes.push_back(syncer::EXTENSIONS);
51     datatypes.push_back(syncer::EXTENSION_SETTINGS);
52     datatypes.push_back(syncer::HISTORY_DELETE_DIRECTIVES);
53     datatypes.push_back(syncer::PASSWORDS);
54     datatypes.push_back(syncer::PREFERENCES);
55     datatypes.push_back(syncer::PRIORITY_PREFERENCES);
56     datatypes.push_back(syncer::SEARCH_ENGINES);
57     datatypes.push_back(syncer::SESSIONS);
58     datatypes.push_back(syncer::PROXY_TABS);
59     datatypes.push_back(syncer::THEMES);
60     datatypes.push_back(syncer::TYPED_URLS);
61     datatypes.push_back(syncer::FAVICON_TRACKING);
62     datatypes.push_back(syncer::FAVICON_IMAGES);
63     datatypes.push_back(syncer::SYNCED_NOTIFICATIONS);
64     datatypes.push_back(syncer::MANAGED_USERS);
65     datatypes.push_back(syncer::MANAGED_USER_SHARED_SETTINGS);
66
67     return datatypes;
68   }
69
70   // Returns the number of default datatypes.
71   static size_t DefaultDatatypesCount() {
72     return DefaultDatatypes().size();
73   }
74
75   // Asserts that all the default datatypes are in |map|, except
76   // for |exception_type|, which unless it is UNDEFINED, is asserted to
77   // not be in |map|.
78   static void CheckDefaultDatatypesInMapExcept(
79       DataTypeController::StateMap* map,
80       syncer::ModelTypeSet exception_types) {
81     std::vector<syncer::ModelType> defaults = DefaultDatatypes();
82     std::vector<syncer::ModelType>::iterator iter;
83     for (iter = defaults.begin(); iter != defaults.end(); ++iter) {
84       if (exception_types.Has(*iter))
85         EXPECT_EQ(0U, map->count(*iter))
86             << *iter << " found in dataypes map, shouldn't be there.";
87       else
88         EXPECT_EQ(1U, map->count(*iter))
89             << *iter << " not found in datatypes map";
90     }
91   }
92
93   // Asserts that if you disable types via the command line, all other types
94   // are enabled.
95   void TestSwitchDisablesType(syncer::ModelTypeSet types) {
96     command_line_->AppendSwitchASCII(switches::kDisableSyncTypes,
97                                      syncer::ModelTypeSetToString(types));
98     scoped_ptr<ProfileSyncService> pss(
99         new ProfileSyncService(
100             new ProfileSyncComponentsFactoryImpl(profile_.get(),
101                                                  command_line_.get()),
102             profile_.get(),
103             NULL,
104             ProfileOAuth2TokenServiceFactory::GetForProfile(profile_.get()),
105             browser_sync::MANUAL_START));
106     pss->factory()->RegisterDataTypes(pss.get());
107     DataTypeController::StateMap controller_states;
108     pss->GetDataTypeControllerStates(&controller_states);
109     EXPECT_EQ(DefaultDatatypesCount() - types.Size(), controller_states.size());
110     CheckDefaultDatatypesInMapExcept(&controller_states, types);
111   }
112
113   base::MessageLoop message_loop_;
114   content::TestBrowserThread ui_thread_;
115   scoped_ptr<Profile> profile_;
116   scoped_ptr<CommandLine> command_line_;
117 };
118
119 TEST_F(ProfileSyncComponentsFactoryImplTest, CreatePSSDefault) {
120   scoped_ptr<ProfileSyncService> pss(new ProfileSyncService(
121       new ProfileSyncComponentsFactoryImpl(profile_.get(), command_line_.get()),
122       profile_.get(),
123       NULL,
124       ProfileOAuth2TokenServiceFactory::GetForProfile(profile_.get()),
125       browser_sync::MANUAL_START));
126   pss->factory()->RegisterDataTypes(pss.get());
127   DataTypeController::StateMap controller_states;
128   pss->GetDataTypeControllerStates(&controller_states);
129   EXPECT_EQ(DefaultDatatypesCount(), controller_states.size());
130   CheckDefaultDatatypesInMapExcept(&controller_states, syncer::ModelTypeSet());
131 }
132
133 TEST_F(ProfileSyncComponentsFactoryImplTest, CreatePSSDisableOne) {
134   TestSwitchDisablesType(syncer::ModelTypeSet(syncer::AUTOFILL));
135 }
136
137 TEST_F(ProfileSyncComponentsFactoryImplTest, CreatePSSDisableMultiple) {
138   TestSwitchDisablesType(
139       syncer::ModelTypeSet(syncer::AUTOFILL_PROFILE, syncer::BOOKMARKS));
140 }