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/sync/glue/data_type_controller.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 "content/public/test/test_browser_thread.h"
18#include "testing/gtest/include/gtest/gtest.h"
19
20using browser_sync::DataTypeController;
21using content::BrowserThread;
22
23class ProfileSyncComponentsFactoryImplTest : public testing::Test {
24 protected:
25  ProfileSyncComponentsFactoryImplTest()
26      : ui_thread_(BrowserThread::UI, &message_loop_) {}
27
28  virtual void SetUp() {
29    profile_.reset(new TestingProfile());
30    base::FilePath program_path(FILE_PATH_LITERAL("chrome.exe"));
31    command_line_.reset(new CommandLine(program_path));
32  }
33
34  // Returns the collection of default datatypes.
35  static std::vector<syncer::ModelType> DefaultDatatypes() {
36    std::vector<syncer::ModelType> datatypes;
37    datatypes.push_back(syncer::APPS);
38    datatypes.push_back(syncer::APP_SETTINGS);
39    datatypes.push_back(syncer::AUTOFILL);
40    datatypes.push_back(syncer::AUTOFILL_PROFILE);
41    datatypes.push_back(syncer::BOOKMARKS);
42#if defined(OS_LINUX) || defined(OS_WIN) || defined(OS_CHROMEOS)
43    datatypes.push_back(syncer::DICTIONARY);
44#endif
45    datatypes.push_back(syncer::EXTENSIONS);
46    datatypes.push_back(syncer::EXTENSION_SETTINGS);
47    datatypes.push_back(syncer::HISTORY_DELETE_DIRECTIVES);
48    datatypes.push_back(syncer::PASSWORDS);
49    datatypes.push_back(syncer::PREFERENCES);
50    datatypes.push_back(syncer::PRIORITY_PREFERENCES);
51    datatypes.push_back(syncer::SEARCH_ENGINES);
52    datatypes.push_back(syncer::SESSIONS);
53    datatypes.push_back(syncer::PROXY_TABS);
54    datatypes.push_back(syncer::THEMES);
55    datatypes.push_back(syncer::TYPED_URLS);
56    datatypes.push_back(syncer::FAVICON_TRACKING);
57    datatypes.push_back(syncer::FAVICON_IMAGES);
58    datatypes.push_back(syncer::SYNCED_NOTIFICATIONS);
59
60  return datatypes;
61  }
62
63  // Returns the number of default datatypes.
64  static size_t DefaultDatatypesCount() {
65    return DefaultDatatypes().size();
66  }
67
68  // Asserts that all the default datatypes are in |map|, except
69  // for |exception_type|, which unless it is UNDEFINED, is asserted to
70  // not be in |map|.
71  static void CheckDefaultDatatypesInMapExcept(
72      DataTypeController::StateMap* map,
73      syncer::ModelType exception_type) {
74    std::vector<syncer::ModelType> defaults = DefaultDatatypes();
75    std::vector<syncer::ModelType>::iterator iter;
76    for (iter = defaults.begin(); iter != defaults.end(); ++iter) {
77      if (exception_type != syncer::UNSPECIFIED && exception_type == *iter)
78        EXPECT_EQ(0U, map->count(*iter))
79            << *iter << " found in dataypes map, shouldn't be there.";
80      else
81        EXPECT_EQ(1U, map->count(*iter))
82            << *iter << " not found in datatypes map";
83    }
84  }
85
86  // Asserts that if you apply the command line switch |cmd_switch|,
87  // all types are enabled except for |type|, which is disabled.
88  void TestSwitchDisablesType(const char* cmd_switch,
89                              syncer::ModelType type) {
90    command_line_->AppendSwitch(cmd_switch);
91    scoped_ptr<ProfileSyncService> pss(
92        new ProfileSyncService(
93            new ProfileSyncComponentsFactoryImpl(profile_.get(),
94                                                 command_line_.get()),
95            profile_.get(),
96            NULL,
97            ProfileSyncService::MANUAL_START));
98    pss->factory()->RegisterDataTypes(pss.get());
99    DataTypeController::StateMap controller_states;
100    pss->GetDataTypeControllerStates(&controller_states);
101    EXPECT_EQ(DefaultDatatypesCount() - 1, controller_states.size());
102    CheckDefaultDatatypesInMapExcept(&controller_states, type);
103  }
104
105  base::MessageLoop message_loop_;
106  content::TestBrowserThread ui_thread_;
107  scoped_ptr<Profile> profile_;
108  scoped_ptr<CommandLine> command_line_;
109};
110
111TEST_F(ProfileSyncComponentsFactoryImplTest, CreatePSSDefault) {
112  scoped_ptr<ProfileSyncService> pss(
113      new ProfileSyncService(
114          new ProfileSyncComponentsFactoryImpl(profile_.get(),
115                                               command_line_.get()),
116      profile_.get(),
117      NULL,
118      ProfileSyncService::MANUAL_START));
119  pss->factory()->RegisterDataTypes(pss.get());
120  DataTypeController::StateMap controller_states;
121  pss->GetDataTypeControllerStates(&controller_states);
122  EXPECT_EQ(DefaultDatatypesCount(), controller_states.size());
123  CheckDefaultDatatypesInMapExcept(&controller_states, syncer::UNSPECIFIED);
124}
125
126TEST_F(ProfileSyncComponentsFactoryImplTest, CreatePSSDisableAutofill) {
127  TestSwitchDisablesType(switches::kDisableSyncAutofill,
128                         syncer::AUTOFILL);
129}
130
131TEST_F(ProfileSyncComponentsFactoryImplTest, CreatePSSDisableBookmarks) {
132  TestSwitchDisablesType(switches::kDisableSyncBookmarks,
133                         syncer::BOOKMARKS);
134}
135
136TEST_F(ProfileSyncComponentsFactoryImplTest, CreatePSSDisablePreferences) {
137  TestSwitchDisablesType(switches::kDisableSyncPreferences,
138                         syncer::PREFERENCES);
139}
140
141TEST_F(ProfileSyncComponentsFactoryImplTest, CreatePSSDisableThemes) {
142  TestSwitchDisablesType(switches::kDisableSyncThemes,
143                         syncer::THEMES);
144}
145
146TEST_F(ProfileSyncComponentsFactoryImplTest, CreatePSSDisableExtensions) {
147  TestSwitchDisablesType(switches::kDisableSyncExtensions,
148                         syncer::EXTENSIONS);
149}
150
151TEST_F(ProfileSyncComponentsFactoryImplTest, CreatePSSDisableApps) {
152  TestSwitchDisablesType(switches::kDisableSyncApps,
153                         syncer::APPS);
154}
155
156TEST_F(ProfileSyncComponentsFactoryImplTest, CreatePSSDisableAutofillProfile) {
157  TestSwitchDisablesType(switches::kDisableSyncAutofillProfile,
158                         syncer::AUTOFILL_PROFILE);
159}
160
161TEST_F(ProfileSyncComponentsFactoryImplTest, CreatePSSDisablePasswords) {
162  TestSwitchDisablesType(switches::kDisableSyncPasswords,
163                         syncer::PASSWORDS);
164}
165