1 // Copyright (c) 2015 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 "chrome/browser/sync/profile_sync_service_factory.h"
6 
7 #include <stddef.h>
8 
9 #include <vector>
10 
11 #include "base/command_line.h"
12 #include "base/feature_list.h"
13 #include "base/task/thread_pool/thread_pool_instance.h"
14 #include "build/build_config.h"
15 #include "chrome/browser/favicon/favicon_service_factory.h"
16 #include "chrome/browser/history/history_service_factory.h"
17 #include "chrome/common/buildflags.h"
18 #include "chrome/test/base/testing_profile.h"
19 #include "components/browser_sync/browser_sync_switches.h"
20 #include "components/sync/base/model_type.h"
21 #include "components/sync/base/sync_base_switches.h"
22 #include "components/sync/driver/data_type_controller.h"
23 #include "components/sync/driver/profile_sync_service.h"
24 #include "components/sync/driver/sync_driver_switches.h"
25 #include "content/public/test/browser_task_environment.h"
26 #include "extensions/buildflags/buildflags.h"
27 #include "testing/gtest/include/gtest/gtest.h"
28 
29 #if defined(OS_CHROMEOS)
30 #include "chrome/browser/chromeos/arc/arc_util.h"
31 #include "chrome/browser/sync/wifi_configuration_sync_service_factory.h"
32 #include "chrome/browser/ui/app_list/app_list_syncable_service_factory.h"
33 #include "chromeos/components/sync_wifi/wifi_configuration_sync_service.h"
34 #include "chromeos/constants/chromeos_features.h"
35 #include "chromeos/dbus/shill/shill_clients.h"
36 #include "chromeos/dbus/shill/shill_manager_client.h"
37 #include "chromeos/network/network_handler.h"
38 #include "chromeos/services/network_config/public/cpp/cros_network_config_test_helper.h"
39 #endif
40 
41 class ProfileSyncServiceFactoryTest : public testing::Test {
42  public:
SetUp()43   void SetUp() override {
44 #if defined(OS_CHROMEOS)
45     app_list::AppListSyncableServiceFactory::SetUseInTesting(true);
46 #endif  // defined(OS_CHROMEOS)
47     TestingProfile::Builder builder;
48     builder.AddTestingFactory(FaviconServiceFactory::GetInstance(),
49                               FaviconServiceFactory::GetDefaultFactory());
50     builder.AddTestingFactory(HistoryServiceFactory::GetInstance(),
51                               HistoryServiceFactory::GetDefaultFactory());
52     profile_ = builder.Build();
53     // Some services will only be created if there is a WebDataService.
54     profile_->CreateWebDataService();
55   }
56 
TearDown()57   void TearDown() override {
58 #if defined(OS_CHROMEOS)
59     app_list::AppListSyncableServiceFactory::SetUseInTesting(false);
60 #endif  // defined(OS_CHROMEOS)
61     base::ThreadPoolInstance::Get()->FlushForTesting();
62   }
63 
64  protected:
65 #if defined(OS_CHROMEOS)
ProfileSyncServiceFactoryTest()66   ProfileSyncServiceFactoryTest() {
67     // Fake network stack is required for WIFI_CONFIGURATIONS datatype.
68     chromeos::NetworkHandler::Initialize();
69   }
~ProfileSyncServiceFactoryTest()70   ~ProfileSyncServiceFactoryTest() override {
71     chromeos::NetworkHandler::Shutdown();
72   }
73 #else
74   ProfileSyncServiceFactoryTest() = default;
75   ~ProfileSyncServiceFactoryTest() override = default;
76 #endif
77 
78   // Returns the collection of default datatypes.
DefaultDatatypes()79   std::vector<syncer::ModelType> DefaultDatatypes() {
80     static_assert(41 == syncer::ModelType::NUM_ENTRIES,
81                   "When adding a new type, you probably want to add it here as "
82                   "well (assuming it is already enabled).");
83 
84     std::vector<syncer::ModelType> datatypes;
85 
86     // These preprocessor conditions and their order should be in sync with
87     // preprocessor conditions in ChromeSyncClient::CreateDataTypeControllers:
88 
89     // ChromeSyncClient types.
90     datatypes.push_back(syncer::SECURITY_EVENTS);
91 
92 #if BUILDFLAG(ENABLE_SUPERVISED_USERS)
93     datatypes.push_back(syncer::SUPERVISED_USER_SETTINGS);
94     datatypes.push_back(syncer::SUPERVISED_USER_ALLOWLISTS);
95 #endif  // BUILDFLAG(ENABLE_SUPERVISED_USERS)
96 
97 #if BUILDFLAG(ENABLE_EXTENSIONS)
98     datatypes.push_back(syncer::APPS);
99     datatypes.push_back(syncer::EXTENSIONS);
100     datatypes.push_back(syncer::EXTENSION_SETTINGS);
101     datatypes.push_back(syncer::APP_SETTINGS);
102     datatypes.push_back(syncer::WEB_APPS);
103 #endif  // BUILDFLAG(ENABLE_EXTENSIONS)
104 
105 #if !defined(OS_ANDROID)
106     datatypes.push_back(syncer::THEMES);
107     datatypes.push_back(syncer::SEARCH_ENGINES);
108 #endif  // !defined(OS_ANDROID)
109 
110 #if defined(OS_LINUX) || defined(OS_CHROMEOS) || defined(OS_WIN)
111     datatypes.push_back(syncer::DICTIONARY);
112 #endif
113 
114 #if defined(OS_CHROMEOS)
115     datatypes.push_back(syncer::APP_LIST);
116     if (arc::IsArcAllowedForProfile(profile()))
117       datatypes.push_back(syncer::ARC_PACKAGE);
118     if (chromeos::features::IsSplitSettingsSyncEnabled()) {
119       datatypes.push_back(syncer::OS_PREFERENCES);
120       datatypes.push_back(syncer::OS_PRIORITY_PREFERENCES);
121     }
122     datatypes.push_back(syncer::PRINTERS);
123     if (base::FeatureList::IsEnabled(switches::kSyncWifiConfigurations)) {
124       datatypes.push_back(syncer::WIFI_CONFIGURATIONS);
125     }
126 #endif  // OS_CHROMEOS
127 
128     // Common types. This excludes PASSWORDS because the password store factory
129     // is null for testing and hence no controller gets instantiated.
130     datatypes.push_back(syncer::AUTOFILL);
131     datatypes.push_back(syncer::AUTOFILL_PROFILE);
132     datatypes.push_back(syncer::AUTOFILL_WALLET_DATA);
133     datatypes.push_back(syncer::AUTOFILL_WALLET_METADATA);
134     datatypes.push_back(syncer::BOOKMARKS);
135     datatypes.push_back(syncer::DEVICE_INFO);
136     datatypes.push_back(syncer::HISTORY_DELETE_DIRECTIVES);
137     datatypes.push_back(syncer::PREFERENCES);
138     datatypes.push_back(syncer::PRIORITY_PREFERENCES);
139     datatypes.push_back(syncer::SESSIONS);
140     datatypes.push_back(syncer::PROXY_TABS);
141     datatypes.push_back(syncer::TYPED_URLS);
142     datatypes.push_back(syncer::USER_EVENTS);
143     datatypes.push_back(syncer::USER_CONSENTS);
144     datatypes.push_back(syncer::SEND_TAB_TO_SELF);
145     datatypes.push_back(syncer::SHARING_MESSAGE);
146     return datatypes;
147   }
148 
149   // Returns the number of default datatypes.
DefaultDatatypesCount()150   size_t DefaultDatatypesCount() { return DefaultDatatypes().size(); }
151 
152   // Asserts that all the default datatypes are in |types|, except
153   // for |exception_types|, which are asserted to not be in |types|.
CheckDefaultDatatypesInSetExcept(syncer::ModelTypeSet types,syncer::ModelTypeSet exception_types)154   void CheckDefaultDatatypesInSetExcept(syncer::ModelTypeSet types,
155                                         syncer::ModelTypeSet exception_types) {
156     std::vector<syncer::ModelType> defaults = DefaultDatatypes();
157     std::vector<syncer::ModelType>::iterator iter;
158     for (iter = defaults.begin(); iter != defaults.end(); ++iter) {
159       if (exception_types.Has(*iter))
160         EXPECT_FALSE(types.Has(*iter))
161             << *iter << " found in dataypes map, shouldn't be there.";
162       else
163         EXPECT_TRUE(types.Has(*iter)) << *iter << " not found in datatypes map";
164     }
165   }
166 
SetDisabledTypes(syncer::ModelTypeSet disabled_types)167   void SetDisabledTypes(syncer::ModelTypeSet disabled_types) {
168     base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(
169         switches::kDisableSyncTypes,
170         syncer::ModelTypeSetToString(disabled_types));
171   }
172 
profile()173   Profile* profile() { return profile_.get(); }
174 
RunUntilIdle()175   void RunUntilIdle() { task_environment_.RunUntilIdle(); }
176 
177  private:
178   content::BrowserTaskEnvironment task_environment_;
179   std::unique_ptr<TestingProfile> profile_;
180 
181 #if defined(OS_CHROMEOS)
182   // Sets up  and  tears down the Chrome OS networking mojo service as needed
183   // for the WIFI_CONFIGURATIONS sync service.
184   chromeos::network_config::CrosNetworkConfigTestHelper network_config_helper_;
185 #endif
186 };
187 
188 // Verify that the disable sync flag disables creation of the sync service.
TEST_F(ProfileSyncServiceFactoryTest,DisableSyncFlag)189 TEST_F(ProfileSyncServiceFactoryTest, DisableSyncFlag) {
190   base::CommandLine::ForCurrentProcess()->AppendSwitch(switches::kDisableSync);
191   EXPECT_EQ(nullptr, ProfileSyncServiceFactory::GetForProfile(profile()));
192 }
193 
194 // Verify that a normal (no command line flags) PSS can be created and
195 // properly initialized.
TEST_F(ProfileSyncServiceFactoryTest,CreatePSSDefault)196 TEST_F(ProfileSyncServiceFactoryTest, CreatePSSDefault) {
197   syncer::ProfileSyncService* pss =
198       ProfileSyncServiceFactory::GetAsProfileSyncServiceForProfile(profile());
199   syncer::ModelTypeSet types = pss->GetRegisteredDataTypesForTest();
200   EXPECT_EQ(DefaultDatatypesCount(), types.Size());
201   CheckDefaultDatatypesInSetExcept(types, syncer::ModelTypeSet());
202 
203   pss->Shutdown();
204   RunUntilIdle();
205 }
206 
207 // Verify that a PSS with a disabled datatype can be created and properly
208 // initialized.
TEST_F(ProfileSyncServiceFactoryTest,CreatePSSDisableOne)209 TEST_F(ProfileSyncServiceFactoryTest, CreatePSSDisableOne) {
210   syncer::ModelTypeSet disabled_types(syncer::AUTOFILL);
211   SetDisabledTypes(disabled_types);
212   syncer::ProfileSyncService* pss =
213       ProfileSyncServiceFactory::GetAsProfileSyncServiceForProfile(profile());
214   syncer::ModelTypeSet types = pss->GetRegisteredDataTypesForTest();
215   EXPECT_EQ(DefaultDatatypesCount() - disabled_types.Size(), types.Size());
216   CheckDefaultDatatypesInSetExcept(types, disabled_types);
217 
218   pss->Shutdown();
219   RunUntilIdle();
220 }
221 
222 // Verify that a PSS with multiple disabled datatypes can be created and
223 // properly initialized.
TEST_F(ProfileSyncServiceFactoryTest,CreatePSSDisableMultiple)224 TEST_F(ProfileSyncServiceFactoryTest, CreatePSSDisableMultiple) {
225   syncer::ModelTypeSet disabled_types(syncer::AUTOFILL_PROFILE,
226                                       syncer::BOOKMARKS);
227   SetDisabledTypes(disabled_types);
228   syncer::ProfileSyncService* pss =
229       ProfileSyncServiceFactory::GetAsProfileSyncServiceForProfile(profile());
230   syncer::ModelTypeSet types = pss->GetRegisteredDataTypesForTest();
231   EXPECT_EQ(DefaultDatatypesCount() - disabled_types.Size(), types.Size());
232   CheckDefaultDatatypesInSetExcept(types, disabled_types);
233 
234   pss->Shutdown();
235   RunUntilIdle();
236 }
237