1// Copyright (c) 2011 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 "testing/gtest/include/gtest/gtest.h"
8#include "base/command_line.h"
9#include "base/file_path.h"
10#include "base/memory/scoped_ptr.h"
11#include "base/message_loop.h"
12#include "chrome/browser/sync/engine/syncapi.h"
13#include "chrome/browser/sync/glue/data_type_controller.h"
14#include "chrome/browser/sync/profile_sync_service.h"
15#include "chrome/browser/sync/profile_sync_factory_impl.h"
16#include "chrome/common/chrome_switches.h"
17#include "chrome/test/testing_profile.h"
18#include "content/browser/browser_thread.h"
19
20using browser_sync::DataTypeController;
21
22class ProfileSyncFactoryImplTest : public testing::Test {
23 protected:
24  ProfileSyncFactoryImplTest()
25      : ui_thread_(BrowserThread::UI, &message_loop_) {}
26
27  virtual void SetUp() {
28    profile_.reset(new TestingProfile());
29    FilePath program_path(FILE_PATH_LITERAL("chrome.exe"));
30    command_line_.reset(new CommandLine(program_path));
31    profile_sync_service_factory_.reset(
32        new ProfileSyncFactoryImpl(profile_.get(), command_line_.get()));
33  }
34
35  // Returns the collection of default datatypes.
36  static std::vector<syncable::ModelType> DefaultDatatypes() {
37    std::vector<syncable::ModelType> datatypes;
38    datatypes.push_back(syncable::BOOKMARKS);
39    datatypes.push_back(syncable::PREFERENCES);
40    datatypes.push_back(syncable::AUTOFILL);
41    datatypes.push_back(syncable::THEMES);
42    datatypes.push_back(syncable::EXTENSIONS);
43    datatypes.push_back(syncable::APPS);
44    datatypes.push_back(syncable::AUTOFILL_PROFILE);
45    datatypes.push_back(syncable::PASSWORDS);
46    return datatypes;
47  }
48
49  // Returns the number of default datatypes.
50  static size_t DefaultDatatypesCount() {
51    return DefaultDatatypes().size();
52  }
53
54  // Asserts that all the default datatypes are in |map|, except
55  // for |exception_type|, which unless it is UNDEFINED, is asserted to
56  // not be in |map|.
57  static void CheckDefaultDatatypesInMapExcept(
58      DataTypeController::StateMap* map,
59      syncable::ModelType exception_type) {
60    std::vector<syncable::ModelType> defaults = DefaultDatatypes();
61    std::vector<syncable::ModelType>::iterator iter;
62    for (iter = defaults.begin(); iter != defaults.end(); ++iter) {
63      if (exception_type != syncable::UNSPECIFIED && exception_type == *iter)
64        EXPECT_EQ(0U, map->count(*iter))
65            << *iter << " found in dataypes map, shouldn't be there.";
66      else
67        EXPECT_EQ(1U, map->count(*iter))
68            << *iter << " not found in datatypes map";
69    }
70  }
71
72  // Asserts that if you apply the command line switch |cmd_switch|,
73  // all types are enabled except for |type|, which is disabled.
74  void TestSwitchDisablesType(const char* cmd_switch,
75                              syncable::ModelType type) {
76    command_line_->AppendSwitch(cmd_switch);
77    scoped_ptr<ProfileSyncService> pss(
78        profile_sync_service_factory_->CreateProfileSyncService(""));
79    DataTypeController::StateMap controller_states;
80    pss->GetDataTypeControllerStates(&controller_states);
81    EXPECT_EQ(DefaultDatatypesCount() - 1, controller_states.size());
82    CheckDefaultDatatypesInMapExcept(&controller_states, type);
83  }
84
85  MessageLoop message_loop_;
86  BrowserThread ui_thread_;
87  scoped_ptr<Profile> profile_;
88  scoped_ptr<CommandLine> command_line_;
89  scoped_ptr<ProfileSyncFactoryImpl> profile_sync_service_factory_;
90};
91
92TEST_F(ProfileSyncFactoryImplTest, CreatePSSDefault) {
93  scoped_ptr<ProfileSyncService> pss(
94      profile_sync_service_factory_->CreateProfileSyncService(""));
95  DataTypeController::StateMap controller_states;
96  pss->GetDataTypeControllerStates(&controller_states);
97  EXPECT_EQ(DefaultDatatypesCount(), controller_states.size());
98  CheckDefaultDatatypesInMapExcept(&controller_states, syncable::UNSPECIFIED);
99}
100
101TEST_F(ProfileSyncFactoryImplTest, CreatePSSDisableAutofill) {
102  TestSwitchDisablesType(switches::kDisableSyncAutofill,
103                         syncable::AUTOFILL);
104}
105
106TEST_F(ProfileSyncFactoryImplTest, CreatePSSDisableBookmarks) {
107  TestSwitchDisablesType(switches::kDisableSyncBookmarks,
108                         syncable::BOOKMARKS);
109}
110
111TEST_F(ProfileSyncFactoryImplTest, CreatePSSDisablePreferences) {
112  TestSwitchDisablesType(switches::kDisableSyncPreferences,
113                         syncable::PREFERENCES);
114}
115
116TEST_F(ProfileSyncFactoryImplTest, CreatePSSDisableThemes) {
117  TestSwitchDisablesType(switches::kDisableSyncThemes,
118                         syncable::THEMES);
119}
120
121TEST_F(ProfileSyncFactoryImplTest, CreatePSSDisableExtensions) {
122  TestSwitchDisablesType(switches::kDisableSyncExtensions,
123                         syncable::EXTENSIONS);
124}
125
126TEST_F(ProfileSyncFactoryImplTest, CreatePSSDisableApps) {
127  TestSwitchDisablesType(switches::kDisableSyncApps,
128                         syncable::APPS);
129}
130
131TEST_F(ProfileSyncFactoryImplTest, CreatePSSDisableAutofillProfile) {
132  TestSwitchDisablesType(switches::kDisableSyncAutofillProfile,
133                         syncable::AUTOFILL_PROFILE);
134}
135
136TEST_F(ProfileSyncFactoryImplTest, CreatePSSDisablePasswords) {
137  TestSwitchDisablesType(switches::kDisableSyncPasswords,
138                         syncable::PASSWORDS);
139}
140