-
Notifications
You must be signed in to change notification settings - Fork 6.6k
/
sync_test.cc
1275 lines (1112 loc) · 48.4 KB
/
sync_test.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/sync/test/integration/sync_test.h"
#include <utility>
#include "base/command_line.h"
#include "base/files/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/memory/ptr_util.h"
#include "base/memory/ref_counted.h"
#include "base/memory/weak_ptr.h"
#include "base/path_service.h"
#include "base/rand_util.h"
#include "base/run_loop.h"
#include "base/strings/string_split.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "base/task/sequenced_task_runner.h"
#include "base/task/task_traits.h"
#include "base/task/thread_pool.h"
#include "base/test/test_timeouts.h"
#include "build/build_config.h"
#include "build/chromeos_buildflags.h"
#include "chrome/browser/bookmarks/bookmark_model_factory.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/browser_process_platform_part.h"
#include "chrome/browser/gcm/gcm_profile_service_factory.h"
#include "chrome/browser/gcm/instance_id/instance_id_profile_service_factory.h"
#include "chrome/browser/invalidation/profile_invalidation_provider_factory.h"
#include "chrome/browser/net/system_network_context_manager.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/browser/profiles/profile_test_util.h"
#include "chrome/browser/profiles/profiles_state.h"
#include "chrome/browser/signin/chrome_signin_client_factory.h"
#include "chrome/browser/signin/identity_manager_factory.h"
#include "chrome/browser/sync/sync_service_factory.h"
#include "chrome/browser/sync/test/integration/committed_all_nudged_changes_checker.h"
#include "chrome/browser/sync/test/integration/device_info_helper.h"
#include "chrome/browser/sync/test/integration/fake_sync_gcm_driver_for_instance_id.h"
#include "chrome/browser/sync/test/integration/session_hierarchy_match_checker.h"
#include "chrome/browser/sync/test/integration/sync_datatype_helper.h"
#include "chrome/browser/sync/test/integration/sync_disabled_checker.h"
#include "chrome/browser/sync/test/integration/sync_integration_test_util.h"
#include "chrome/browser/sync/test/integration/sync_service_impl_harness.h"
#include "chrome/common/chrome_constants.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/common/chrome_switches.h"
#include "components/bookmarks/test/bookmark_test_helpers.h"
#include "components/gcm_driver/fake_gcm_profile_service.h"
#include "components/gcm_driver/gcm_profile_service.h"
#include "components/gcm_driver/instance_id/instance_id.h"
#include "components/gcm_driver/instance_id/instance_id_driver.h"
#include "components/gcm_driver/instance_id/instance_id_profile_service.h"
#include "components/invalidation/impl/fake_invalidation_service.h"
#include "components/invalidation/impl/fcm_invalidation_service.h"
#include "components/invalidation/impl/fcm_network_handler.h"
#include "components/invalidation/impl/invalidation_prefs.h"
#include "components/invalidation/impl/invalidation_switches.h"
#include "components/invalidation/impl/per_user_topic_subscription_manager.h"
#include "components/invalidation/impl/profile_identity_provider.h"
#include "components/invalidation/impl/profile_invalidation_provider.h"
#include "components/invalidation/public/invalidation_service.h"
#include "components/keyed_service/content/browser_context_dependency_manager.h"
#include "components/keyed_service/core/keyed_service.h"
#include "components/os_crypt/sync/os_crypt_mocker.h"
#include "components/prefs/scoped_user_pref_update.h"
#include "components/signin/public/base/consent_level.h"
#include "components/sync/base/command_line_switches.h"
#include "components/sync/base/invalidation_helper.h"
#include "components/sync/base/model_type.h"
#include "components/sync/driver/glue/sync_transport_data_prefs.h"
#include "components/sync/driver/sync_service_impl.h"
#include "components/sync/driver/sync_user_settings.h"
#include "components/sync/engine/sync_scheduler_impl.h"
#include "components/sync/invalidations/sync_invalidations_service_impl.h"
#include "components/sync/test/fake_server_network_resources.h"
#include "content/public/browser/navigation_entry.h"
#include "content/public/browser/network_service_instance.h"
#include "content/public/browser/storage_partition.h"
#include "content/public/browser/web_contents.h"
#include "google_apis/gaia/gaia_urls.h"
#include "net/base/port_util.h"
#include "net/dns/mock_host_resolver.h"
#include "net/traffic_annotation/network_traffic_annotation_test_helper.h"
#include "services/network/public/cpp/features.h"
#include "services/network/public/cpp/weak_wrapper_shared_url_loader_factory.h"
#include "services/network/public/mojom/url_response_head.mojom.h"
#include "url/gurl.h"
#include "url/url_constants.h"
#if BUILDFLAG(IS_CHROMEOS_ASH)
#include "ash/components/arc/test/arc_util_test_support.h"
#include "ash/constants/ash_features.h"
#include "ash/constants/ash_switches.h"
#include "chrome/browser/ash/app_list/arc/arc_app_list_prefs_factory.h"
#include "chrome/browser/ash/app_list/test/fake_app_list_model_updater.h"
#include "chrome/browser/sync/test/integration/printers_helper.h"
#include "chrome/browser/sync/test/integration/sync_arc_package_helper.h"
#include "chromeos/ash/components/account_manager/account_manager_factory.h"
#include "components/account_manager_core/chromeos/account_manager.h"
#endif // BUILDFLAG(IS_CHROMEOS_ASH)
#if BUILDFLAG(IS_CHROMEOS_LACROS)
#include "components/account_manager_core/chromeos/account_manager.h"
#include "components/account_manager_core/chromeos/account_manager_facade_factory.h"
#endif // BUILDFLAG(IS_CHROMEOS_LACROS)
#if BUILDFLAG(IS_ANDROID)
#include "chrome/browser/sync/test/integration/sync_test_utils_android.h"
#else // BUILDFLAG(IS_ANDROID)
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_finder.h"
#include "chrome/browser/ui/browser_list.h"
#include "chrome/browser/ui/browser_list_observer.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/browser/ui/webui/signin/login_ui_service.h"
#include "chrome/browser/ui/webui/signin/login_ui_service_factory.h"
#endif // BUILDFLAG(IS_ANDROID)
using syncer::SyncServiceImpl;
namespace {
// Sender ID coming from the Firebase console.
const char kInvalidationGCMSenderId[] = "8181035976";
void SetURLLoaderFactoryForTest(
Profile* profile,
scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory) {
ChromeSigninClient* signin_client = static_cast<ChromeSigninClient*>(
ChromeSigninClientFactory::GetForProfile(profile));
signin_client->SetURLLoaderFactoryForTest(url_loader_factory);
#if BUILDFLAG(IS_CHROMEOS_ASH)
ash::AccountManagerFactory* factory =
g_browser_process->platform_part()->GetAccountManagerFactory();
account_manager::AccountManager* account_manager =
factory->GetAccountManager(profile->GetPath().value());
account_manager->SetUrlLoaderFactoryForTests(url_loader_factory);
#elif BUILDFLAG(IS_CHROMEOS_LACROS)
account_manager::AccountManager* account_manager =
MaybeGetAshAccountManagerForTests();
if (account_manager)
account_manager->SetUrlLoaderFactoryForTests(url_loader_factory);
#endif // BUILDFLAG(IS_CHROMEOS_ASH)
}
class FakePerUserTopicSubscriptionManager
: public invalidation::PerUserTopicSubscriptionManager {
public:
explicit FakePerUserTopicSubscriptionManager(PrefService* local_state)
: invalidation::PerUserTopicSubscriptionManager(
/*identity_provider=*/nullptr,
/*pref_service=*/local_state,
/*url_loader_factory=*/nullptr,
/*project_id*/ kInvalidationGCMSenderId,
/*migrate_prefs=*/false) {}
FakePerUserTopicSubscriptionManager(
const FakePerUserTopicSubscriptionManager&) = delete;
FakePerUserTopicSubscriptionManager& operator=(
const FakePerUserTopicSubscriptionManager&) = delete;
~FakePerUserTopicSubscriptionManager() override = default;
void UpdateSubscribedTopics(const invalidation::Topics& topics,
const std::string& instance_id_token) override {}
};
std::unique_ptr<invalidation::FCMNetworkHandler> CreateFCMNetworkHandler(
Profile* profile,
std::map<const Profile*, invalidation::FCMNetworkHandler*>*
profile_to_fcm_network_handler_map,
gcm::GCMDriver* gcm_driver,
instance_id::InstanceIDDriver* instance_id_driver,
const std::string& sender_id,
const std::string& app_id) {
auto handler = std::make_unique<invalidation::FCMNetworkHandler>(
gcm_driver, instance_id_driver, sender_id, app_id);
(*profile_to_fcm_network_handler_map)[profile] = handler.get();
return handler;
}
std::unique_ptr<invalidation::PerUserTopicSubscriptionManager>
CreatePerUserTopicSubscriptionManager(PrefService* local_state,
const std::string& project_id,
bool migrate_prefs) {
return std::make_unique<FakePerUserTopicSubscriptionManager>(local_state);
}
invalidation::FCMNetworkHandler* GetFCMNetworkHandler(
Profile* profile,
std::map<const Profile*, invalidation::FCMNetworkHandler*>*
profile_to_fcm_network_handler_map) {
// Delivering FCM notifications does not work if explicitly signed-out.
signin::IdentityManager* identity_manager =
IdentityManagerFactory::GetForProfile(profile);
if (!identity_manager ||
!identity_manager->HasPrimaryAccount(signin::ConsentLevel::kSignin)) {
return nullptr;
}
auto it = profile_to_fcm_network_handler_map->find(profile);
return it != profile_to_fcm_network_handler_map->end() ? it->second : nullptr;
}
} // namespace
#if !BUILDFLAG(IS_ANDROID)
class SyncTest::ClosedBrowserObserver : public BrowserListObserver {
public:
using OnBrowserRemovedCallback =
base::RepeatingCallback<void(Browser* browser)>;
explicit ClosedBrowserObserver(OnBrowserRemovedCallback callback)
: browser_remove_callback_(std::move(callback)) {
BrowserList::AddObserver(this);
}
~ClosedBrowserObserver() override { BrowserList::RemoveObserver(this); }
// BrowserListObserver overrides.
void OnBrowserRemoved(Browser* browser) override {
browser_remove_callback_.Run(browser);
}
private:
OnBrowserRemovedCallback browser_remove_callback_;
};
#endif
SyncTest::SyncTest(TestType test_type)
: test_type_(test_type),
server_type_(base::CommandLine::ForCurrentProcess()->HasSwitch(
syncer::kSyncServiceURL)
? EXTERNAL_LIVE_SERVER
: IN_PROCESS_FAKE_SERVER),
test_construction_time_(base::Time::Now()),
sync_run_loop_timeout(FROM_HERE, TestTimeouts::action_max_timeout()),
previous_profile_(nullptr),
num_clients_(-1) {
// Any RunLoop timeout will by default result in test failure.
sync_run_loop_timeout.SetAddGTestFailureOnTimeout();
sync_datatype_helper::AssociateWithTest(this);
switch (test_type_) {
case SINGLE_CLIENT: {
num_clients_ = 1;
break;
}
case TWO_CLIENT: {
num_clients_ = 2;
break;
}
}
#if !BUILDFLAG(IS_ANDROID)
browser_list_observer_ = std::make_unique<ClosedBrowserObserver>(
base::BindRepeating(&SyncTest::OnBrowserRemoved, base::Unretained(this)));
#endif
}
SyncTest::~SyncTest() = default;
void SyncTest::SetUp() {
#if BUILDFLAG(IS_ANDROID)
sync_test_utils_android::SetUpAuthForTesting();
#endif
base::CommandLine* cl = base::CommandLine::ForCurrentProcess();
if (cl->HasSwitch(switches::kPasswordFileForTest)) {
ReadPasswordFile();
} else {
// Decide on username to use or create one.
if (cl->HasSwitch(switches::kSyncUserForTest)) {
username_ = cl->GetSwitchValueASCII(switches::kSyncUserForTest);
} else if (server_type_ != EXTERNAL_LIVE_SERVER) {
username_ = "user@gmail.com";
}
// Decide on password to use.
password_ = cl->HasSwitch(switches::kSyncPasswordForTest)
? cl->GetSwitchValueASCII(switches::kSyncPasswordForTest)
: "password";
}
if (username_.empty() || password_.empty()) {
LOG(FATAL) << "Cannot run sync tests without GAIA credentials.";
}
// Mock the Mac Keychain service. The real Keychain can block on user input.
OSCryptMocker::SetUp();
// Yield control back to the PlatformBrowserTest framework.
PlatformBrowserTest::SetUp();
LOG(INFO) << "SyncTest::SetUp() completed; elapsed time since construction: "
<< (base::Time::Now() - test_construction_time_);
}
void SyncTest::TearDown() {
// Clear any mock gaia responses that might have been set.
ClearMockGaiaResponses();
// Allow the PlatformBrowserTest framework to perform its tear down.
PlatformBrowserTest::TearDown();
// Return OSCrypt to its real behaviour
OSCryptMocker::TearDown();
}
void SyncTest::PostRunTestOnMainThread() {
PlatformBrowserTest::PostRunTestOnMainThread();
#if BUILDFLAG(IS_ANDROID)
sync_test_utils_android::TearDownAuthForTesting();
#endif
}
void SyncTest::SetUpCommandLine(base::CommandLine* cl) {
// Disable non-essential access of external network resources.
if (!cl->HasSwitch(switches::kDisableBackgroundNetworking)) {
cl->AppendSwitch(switches::kDisableBackgroundNetworking);
}
if (!cl->HasSwitch(syncer::kSyncShortInitialRetryOverride)) {
cl->AppendSwitch(syncer::kSyncShortInitialRetryOverride);
}
if (!cl->HasSwitch(syncer::kSyncShortNudgeDelayForTest)) {
cl->AppendSwitch(syncer::kSyncShortNudgeDelayForTest);
}
// TODO(crbug.com/1060366): This is a temporary switch to allow having two
// profiles syncing the same account. Having a profile outside of the user
// directory isn't supported in Chrome.
if (!cl->HasSwitch(switches::kAllowProfilesOutsideUserDir)) {
cl->AppendSwitch(switches::kAllowProfilesOutsideUserDir);
}
if (cl->HasSwitch(syncer::kSyncServiceURL)) {
// TODO(crbug.com/1243653): setup real SecurityDomainService if
// server_type_ == EXTERNAL_LIVE_SERVER.
// Effectively disables interaction with SecurityDomainService for E2E
// tests.
cl->AppendSwitchASCII(syncer::kTrustedVaultServiceURL, "broken_url");
}
#if BUILDFLAG(IS_CHROMEOS_ASH)
cl->AppendSwitch(ash::switches::kIgnoreUserProfileMappingForTests);
cl->AppendSwitch(ash::switches::kDisableArcOptInVerification);
cl->AppendSwitch(ash::switches::kDisableLacrosKeepAliveForTesting);
arc::SetArcAvailableCommandLineForTesting(cl);
#endif
}
void SyncTest::BeforeSetupClient(int index,
const base::FilePath& profile_path) {}
base::FilePath SyncTest::GetProfileBaseName(int index) {
return base::FilePath(base::StringPrintf(
FILE_PATH_LITERAL("SyncIntegrationTestClient%d"), index));
}
bool SyncTest::CreateProfile(int index) {
base::FilePath profile_path;
// For Android, we don't create profile because Clank doesn't support
// multiple profiles.
#if !BUILDFLAG(IS_ANDROID)
base::ScopedAllowBlockingForTesting allow_blocking;
if (server_type_ == EXTERNAL_LIVE_SERVER &&
(num_clients_ > 1 || use_new_user_data_dir_)) {
scoped_temp_dirs_.push_back(std::make_unique<base::ScopedTempDir>());
// For multi profile UI signin, profile paths should be outside user data
// dir to allow signing-in multiple profiles to same account. Otherwise, we
// get an error that the profile has already signed in on this device.
// Note: Various places in Chrome assume that all profiles are within the
// user data dir. We violate that assumption here, which can lead to weird
// issues, see https://crbug.com/801569 and the workaround in
// TearDownOnMainThread.
if (!scoped_temp_dirs_.back()->CreateUniqueTempDir()) {
ADD_FAILURE();
return false;
}
profile_path = scoped_temp_dirs_.back()->GetPath();
} else {
base::FilePath user_data_dir;
base::PathService::Get(chrome::DIR_USER_DATA, &user_data_dir);
// Create new profiles in user data dir so that other profiles can know
// about it. This is needed in tests such as supervised user cases which
// assume browser->profile() as the custodian profile. Instead of creating
// a new directory, we use a deterministic name such that PRE_ tests (i.e.
// test that span browser restarts) can reuse the same directory and carry
// over state.
profile_path = user_data_dir.Append(GetProfileBaseName(index));
}
#endif
BeforeSetupClient(index, profile_path);
if (server_type_ == EXTERNAL_LIVE_SERVER) {
// If running against an EXTERNAL_LIVE_SERVER, we signin profiles using real
// GAIA server. This requires creating profiles with no test hooks.
InitializeProfile(index, MakeProfileForUISignin(profile_path));
} else {
// Use default profile for Android.
#if BUILDFLAG(IS_ANDROID)
DCHECK(index == 0);
Profile* profile = ProfileManager::GetLastUsedProfile();
#else
// Without need of real GAIA authentication, we create new test profiles.
Profile* profile =
g_browser_process->profile_manager()->GetProfile(profile_path);
#endif
SetupMockGaiaResponsesForProfile(profile);
InitializeProfile(index, profile);
}
// Once profile initialization has kicked off, wait for it to finish.
WaitForDataModels(GetProfile(index));
return true;
}
// TODO(shadi): Ideally creating a new profile should not depend on signin
// process. We should try to consolidate MakeProfileForUISignin() and
// MakeProfile(). Major differences are profile paths and creation methods. For
// UI signin we need profiles in unique user data dir's and we need to use
// ProfileManager::CreateProfileAsync() for proper profile creation.
// static
Profile* SyncTest::MakeProfileForUISignin(base::FilePath profile_path) {
ProfileManager* profile_manager = g_browser_process->profile_manager();
return profiles::testing::CreateProfileSync(profile_manager, profile_path);
}
Profile* SyncTest::GetProfile(int index) const {
DCHECK(!profiles_.empty()) << "SetupClients() has not yet been called.";
DCHECK(index >= 0 && index < static_cast<int>(profiles_.size()))
<< "GetProfile(): Index is out of bounds: " << index;
Profile* profile = profiles_[index];
DCHECK(profile) << "No profile found at index: " << index;
return profile;
}
std::vector<Profile*> SyncTest::GetAllProfiles() {
std::vector<Profile*> profiles;
if (UseVerifier()) {
profiles.push_back(verifier());
}
for (int i = 0; i < num_clients(); ++i) {
profiles.push_back(GetProfile(i));
}
return profiles;
}
#if !BUILDFLAG(IS_ANDROID)
Browser* SyncTest::GetBrowser(int index) {
DCHECK(!browsers_.empty()) << "SetupClients() has not yet been called.";
DCHECK(index >= 0 && index < static_cast<int>(browsers_.size()))
<< "GetBrowser(): Index is out of bounds: " << index;
Browser* browser = browsers_[index];
DCHECK(browser);
return browser;
}
Browser* SyncTest::AddBrowser(int profile_index) {
Profile* profile = GetProfile(profile_index);
browsers_.push_back(Browser::Create(Browser::CreateParams(profile, true)));
profiles_.push_back(profile);
DCHECK_EQ(browsers_.size(), profiles_.size());
return browsers_[browsers_.size() - 1];
}
void SyncTest::OnBrowserRemoved(Browser* browser) {
for (size_t i = 0; i < browsers_.size(); ++i) {
if (browsers_[i] == browser) {
browsers_[i] = nullptr;
break;
}
}
}
#endif
SyncServiceImplHarness* SyncTest::GetClient(int index) {
return const_cast<SyncServiceImplHarness*>(
std::as_const(*this).GetClient(index));
}
const SyncServiceImplHarness* SyncTest::GetClient(int index) const {
if (clients_.empty()) {
LOG(FATAL) << "SetupClients() has not yet been called.";
}
if (index < 0 || index >= static_cast<int>(clients_.size())) {
LOG(FATAL) << "GetClient(): Index is out of bounds.";
}
return clients_[index].get();
}
std::vector<SyncServiceImplHarness*> SyncTest::GetSyncClients() {
std::vector<SyncServiceImplHarness*> clients(clients_.size());
for (size_t i = 0; i < clients_.size(); ++i) {
clients[i] = clients_[i].get();
}
return clients;
}
SyncServiceImpl* SyncTest::GetSyncService(int index) const {
return SyncServiceFactory::GetAsSyncServiceImplForProfileForTesting(
GetProfile(index));
}
syncer::UserSelectableTypeSet SyncTest::GetRegisteredSelectableTypes(
int index) {
return GetSyncService(index)
->GetUserSettings()
->GetRegisteredSelectableTypes();
}
std::vector<SyncServiceImpl*> SyncTest::GetSyncServices() {
std::vector<SyncServiceImpl*> services;
for (int i = 0; i < num_clients(); ++i) {
services.push_back(GetSyncService(i));
}
return services;
}
Profile* SyncTest::verifier() {
if (!UseVerifier()) {
LOG(FATAL) << "Verifier account is disabled.";
}
if (verifier_ == nullptr) {
LOG(FATAL) << "SetupClients() has not yet been called.";
}
return verifier_;
}
bool SyncTest::UseVerifier() {
return false;
}
bool SyncTest::UseArcPackage() {
#if BUILDFLAG(IS_CHROMEOS_ASH)
// ARC_PACKAGE do not support supervised users, switches::kSupervisedUserId
// need to be set in SetUpCommandLine() when a test will use supervise users.
return !base::CommandLine::ForCurrentProcess()->HasSwitch(
switches::kSupervisedUserId);
#else // BUILDFLAG(IS_CHROMEOS_ASH)
return false;
#endif // BUILDFLAG(IS_CHROMEOS_ASH)
}
bool SyncTest::SetupClients() {
previous_profile_ =
g_browser_process->profile_manager()->GetLastUsedProfile();
base::ScopedAllowBlockingForTesting allow_blocking;
if (num_clients_ <= 0) {
LOG(FATAL) << "num_clients_ incorrectly initialized.";
}
bool has_any_browser = false;
#if !BUILDFLAG(IS_ANDROID)
has_any_browser = !browsers_.empty();
#endif
if (!profiles_.empty() || has_any_browser || !clients_.empty()) {
LOG(FATAL) << "SetupClients() has already been called.";
}
// Create the required number of sync profiles, browsers and clients.
profiles_.resize(num_clients_);
clients_.resize(num_clients_);
fake_server_invalidation_observers_.resize(num_clients_);
auto* cl = base::CommandLine::ForCurrentProcess();
if (!cl->HasSwitch(syncer::kSyncDeferredStartupTimeoutSeconds)) {
cl->AppendSwitchASCII(syncer::kSyncDeferredStartupTimeoutSeconds, "0");
}
#if BUILDFLAG(IS_CHROMEOS_ASH)
if (UseArcPackage()) {
// Sets Arc flags, need to be called before create test profiles.
ArcAppListPrefsFactory::SetFactoryForSyncTest();
}
// Uses a fake app list model updater to avoid interacting with Ash.
model_updater_factory_ = std::make_unique<
app_list::AppListSyncableService::ScopedModelUpdaterFactoryForTest>(
base::BindRepeating(
[](app_list::reorder::AppListReorderDelegate* reorder_delegate)
-> std::unique_ptr<AppListModelUpdater> {
return std::make_unique<FakeAppListModelUpdater>(
/*profile=*/nullptr, reorder_delegate);
}));
#endif
for (int i = 0; i < num_clients_; ++i) {
if (!CreateProfile(i)) {
return false;
}
LOG(INFO) << "SyncTest::SetupClients() created profile " << i
<< "; elapsed time since construction: "
<< (base::Time::Now() - test_construction_time_);
}
// Verifier account is not useful when running against external servers.
DCHECK(server_type_ != EXTERNAL_LIVE_SERVER || !UseVerifier());
// Verifier needs to create a test profile. But Clank doesn't support multiple
// profiles.
#if BUILDFLAG(IS_ANDROID)
DCHECK(!UseVerifier());
#endif
// Create the verifier profile.
if (UseVerifier()) {
base::FilePath user_data_dir;
base::PathService::Get(chrome::DIR_USER_DATA, &user_data_dir);
verifier_ = g_browser_process->profile_manager()->GetProfile(
user_data_dir.Append(FILE_PATH_LITERAL("Verifier")));
WaitForDataModels(verifier());
}
#if BUILDFLAG(IS_CHROMEOS_ASH)
if (ArcAppListPrefsFactory::IsFactorySetForSyncTest()) {
// Init SyncArcPackageHelper to ensure that the arc services are initialized
// for each Profile, only can be called after test profiles are created.
if (!sync_arc_helper()) {
return false;
}
}
#endif
LOG(INFO)
<< "SyncTest::SetupClients() completed; elapsed time since construction: "
<< (base::Time::Now() - test_construction_time_);
return true;
}
void SyncTest::InitializeProfile(int index, Profile* profile) {
DCHECK(profile);
profiles_[index] = profile;
profile->AddObserver(this);
SetUpInvalidations(index);
#if !BUILDFLAG(IS_ANDROID)
browsers_.push_back(Browser::Create(Browser::CreateParams(profile, true)));
DCHECK_EQ(static_cast<size_t>(index), browsers_.size() - 1);
#endif
// Make sure the SyncServiceImpl has been created before creating the
// SyncServiceImplHarness - some tests expect the SyncServiceImpl to
// already exist.
SyncServiceImpl* sync_service_impl =
SyncServiceFactory::GetAsSyncServiceImplForProfileForTesting(
GetProfile(index));
if (server_type_ == IN_PROCESS_FAKE_SERVER) {
sync_service_impl->OverrideNetworkForTest(
fake_server::CreateFakeServerHttpPostProviderFactory(
GetFakeServer()->AsWeakPtr()));
// Make sure that an instance of GCMProfileService has been created. This is
// required for some tests which only call SetupClients().
gcm::GCMProfileServiceFactory::GetForProfile(profile);
DCHECK(base::Contains(profile_to_fake_gcm_driver_, profile));
fake_server_sync_invalidation_sender_->AddFakeGCMDriver(
profile_to_fake_gcm_driver_[profile]);
}
SyncServiceImplHarness::SigninType signin_type =
server_type_ == EXTERNAL_LIVE_SERVER
? SyncServiceImplHarness::SigninType::UI_SIGNIN
: SyncServiceImplHarness::SigninType::FAKE_SIGNIN;
DCHECK(!clients_[index]);
clients_[index] = SyncServiceImplHarness::Create(GetProfile(index), username_,
password_, signin_type);
EXPECT_NE(nullptr, GetClient(index)) << "Could not create Client " << index;
}
void SyncTest::DisableNotificationsForClient(int index) {
fake_server_->RemoveObserver(
fake_server_invalidation_observers_[index].get());
}
void SyncTest::SetupMockGaiaResponsesForProfile(Profile* profile) {
SetURLLoaderFactoryForTest(profile,
test_url_loader_factory_.GetSafeWeakWrapper());
}
void SyncTest::SetUpInvalidations(int index) {
switch (server_type_) {
case EXTERNAL_LIVE_SERVER:
// DO NOTHING. External live sync servers use GCM to notify profiles of
// any invalidations in sync'ed data. In this case, to notify other
// profiles of invalidations, we use sync refresh notifications instead.
break;
case IN_PROCESS_FAKE_SERVER: {
const std::string client_id = "Client " + base::NumberToString(index);
// Listen for fake server changes.
fake_server_invalidation_observers_[index] =
std::make_unique<fake_server::FakeServerInvalidationSender>(
client_id, TestUsesSelfNotifications(),
base::BindRepeating(&GetFCMNetworkHandler, GetProfile(index),
&profile_to_fcm_network_handler_map_));
fake_server_->AddObserver(
fake_server_invalidation_observers_[index].get());
// Store in prefs the mapping between public and private topics names. In
// real clients, those are stored upon subscription with the
// per-user-topic server. The pref name is defined in
// per_user_topic_subscription_manager.cc.
ScopedDictPrefUpdate update(
GetProfile(index)->GetPrefs(),
"invalidation.per_sender_registered_for_invalidation");
update->Set(kInvalidationGCMSenderId, base::Value::Dict());
for (syncer::ModelType model_type :
GetSyncService(index)->GetPreferredDataTypes()) {
std::string notification_type;
if (!RealModelTypeToNotificationType(model_type, ¬ification_type)) {
continue;
}
update->FindDict(kInvalidationGCMSenderId)
->Set(notification_type,
"/private/" + notification_type + "-topic_server_user_id");
}
ScopedDictPrefUpdate update_client_id(
GetProfile(index)->GetPrefs(),
invalidation::prefs::kInvalidationClientIDCache);
update_client_id->Set(kInvalidationGCMSenderId, client_id);
break;
}
}
}
void SyncTest::SetupSyncInternal(SetupSyncMode setup_mode) {
// Create sync profiles and clients if they haven't already been created.
if (profiles_.empty()) {
if (!SetupClients()) {
LOG(FATAL) << "SetupClients() failed.";
}
}
// TODO(crbug.com/801482): If we ever start running tests against external
// servers again, we might have to find a way to clear any pre-existing data
// from the test account.
if (server_type_ == EXTERNAL_LIVE_SERVER) {
LOG(ERROR) << "WARNING: Running against external servers with an existing "
"account. If there is any pre-existing data in the account, "
"things will likely break.";
}
// Sync each of the profiles.
for (int client_index = 0; client_index < num_clients_; client_index++) {
SyncServiceImplHarness* client = GetClient(client_index);
DVLOG(1) << "Setting up " << client_index << " client";
ASSERT_TRUE(client->SetupSyncNoWaitForCompletion())
<< "SetupSync() failed.";
if (TestUsesSelfNotifications()) {
// On Android, invalidations for Session data type are disabled by
// default. This may result in test flakiness when using when using
// AwaitQuiescence() because Android commits Session for "about:blank"
// page, hence AwaitQuiescence() would wait for downloading updates
// forever.
// TODO(crbug.com/1188034): remove this workaround once SetupSync doesn't
// rely on self-notifications.
DCHECK(GetSyncService(client_index)->IsEngineInitialized());
GetSyncService(client_index)->SetInvalidationsForSessionsEnabled(true);
}
// It's important to wait for each client before setting up the next one,
// otherwise multi-client tests get flaky.
// TODO(crbug.com/956043): It would be nice to figure out why.
switch (setup_mode) {
case NO_WAITING:
break;
case WAIT_FOR_SYNC_SETUP_TO_COMPLETE:
ASSERT_TRUE(client->AwaitSyncSetupCompletion());
ASSERT_TRUE(client->AwaitInvalidationsStatus(/*expected_status=*/true));
break;
case WAIT_FOR_COMMITS_TO_COMPLETE:
ASSERT_TRUE(client->AwaitSyncSetupCompletion());
ASSERT_TRUE(client->AwaitInvalidationsStatus(/*expected_status=*/true));
ASSERT_TRUE(WaitForAsyncChangesToBeCommitted(client_index));
break;
}
}
}
void SyncTest::ClearProfiles() {
// This method is called for only a live server, so it shouldn't use
// FakeGCMDriver.
DCHECK(profile_to_fake_gcm_driver_.empty());
profiles_.clear();
scoped_temp_dirs_.clear();
#if !BUILDFLAG(IS_ANDROID)
browsers_.clear();
#endif
clients_.clear();
}
bool SyncTest::SetupSync(SetupSyncMode setup_mode) {
#if BUILDFLAG(IS_ANDROID)
// For Android, currently the framework only supports one client.
// The client uses the default profile.
DCHECK(num_clients_ == 1) << "For Android, currently it only supports "
<< "one client.";
#endif
base::ScopedAllowBlockingForTesting allow_blocking;
SetupSyncInternal(setup_mode);
// Because clients may modify sync data as part of startup (for example
// local session-releated data is rewritten), we need to ensure all
// startup-based changes have propagated between the clients.
//
// Tests that don't use self-notifications can't await quiescense. They'll
// have to find their own way of waiting for an initial state if they really
// need such guarantees.
if (setup_mode != NO_WAITING && TestUsesSelfNotifications()) {
if (!AwaitQuiescence()) {
LOG(FATAL) << "AwaitQuiescence() failed.";
return false;
}
}
#if !BUILDFLAG(IS_ANDROID)
if (server_type_ == EXTERNAL_LIVE_SERVER) {
// OneClickSigninSyncStarter observer is created with a real user sign in.
// It is deleted on certain conditions which are not satisfied by our tests,
// and this causes the SigninTracker observer to stay hanging at shutdown.
// Calling LoginUIService::SyncConfirmationUIClosed forces the observer to
// be removed. http://crbug.com/484388
for (int i = 0; i < num_clients_; ++i) {
LoginUIServiceFactory::GetForProfile(GetProfile(i))
->SyncConfirmationUIClosed(
LoginUIService::SYNC_WITH_DEFAULT_SETTINGS);
}
}
#endif
return true;
}
void SyncTest::TearDownOnMainThread() {
// Verify that there are no data type failures after the test.
for (size_t client_index = 0; client_index < clients_.size();
++client_index) {
if (!GetClient(client_index)) {
// This may happen if the last tab and hence a browser has been closed.
continue;
}
CheckForDataTypeFailures(client_index);
}
// Workaround for https://crbug.com/801569: |prefs::kProfileLastUsed| stores
// the profile path relative to the user dir, but our testing profiles are
// outside the user dir (see CreateProfile). So code trying to access the last
// used profile by path will fail. To work around that, set the last used
// profile back to the originally created default profile (which does live in
// the user data dir, and which we don't use otherwise).
if (previous_profile_) {
profiles::SetLastUsedProfile(previous_profile_->GetBaseName());
previous_profile_ = nullptr;
}
if (fake_server_.get()) {
for (const std::unique_ptr<fake_server::FakeServerInvalidationSender>&
observer : fake_server_invalidation_observers_) {
fake_server_->RemoveObserver(observer.get());
}
fake_server_sync_invalidation_sender_.reset();
fake_server_.reset();
}
for (Profile* profile : profiles_) {
// Profile could be removed earlier.
if (profile) {
profile->RemoveObserver(this);
}
}
// Note: Closing all the browsers (see above) may destroy the Profiles, if
// kDestroyProfileOnBrowserClose is enabled. So clear them out here, to make
// sure they're not used anymore.
profiles_.clear();
clients_.clear();
profile_to_fake_gcm_driver_.clear();
// TODO(crbug.com/1260897): There are various other Profile-related members
// around like profile_to_*_map_ - those should probably be cleaned up too.
#if !BUILDFLAG(IS_ANDROID)
// Closing all browsers created by this test. The calls here block until
// they are closed. Other browsers created outside SyncTest setup should be
// closed by the creator of that browser.
for (Browser* browser : browsers_) {
if (browser) {
CloseBrowserSynchronously(browser);
}
}
browsers_.clear();
#endif
PlatformBrowserTest::TearDownOnMainThread();
}
void SyncTest::SetUpInProcessBrowserTestFixture() {
create_services_subscription_ =
BrowserContextDependencyManager::GetInstance()
->RegisterCreateServicesCallbackForTesting(
base::BindRepeating(&SyncTest::OnWillCreateBrowserContextServices,
base::Unretained(this)));
}
void SyncTest::OnProfileWillBeDestroyed(Profile* profile) {
profile->RemoveObserver(this);
for (size_t index = 0; index < profiles_.size(); ++index) {
if (profiles_[index] != profile) {
continue;
}
CheckForDataTypeFailures(/*client_index=*/index);
// |profile_to_fake_gcm_driver_| may be empty when using an external server.
if (base::Contains(profile_to_fake_gcm_driver_, profile)) {
fake_server_sync_invalidation_sender_->RemoveFakeGCMDriver(
profile_to_fake_gcm_driver_[profile]);
profile_to_fake_gcm_driver_.erase(profile);
}
profiles_[index] = nullptr;
clients_[index].reset();
#if !BUILDFLAG(IS_ANDROID)
DCHECK(!browsers_[index]);
#endif // !BUILDFLAG(IS_ANDROID)
}
}
void SyncTest::OnWillCreateBrowserContextServices(
content::BrowserContext* context) {
if (server_type_ == EXTERNAL_LIVE_SERVER) {
// DO NOTHING. External live sync servers use GCM to notify profiles of
// any invalidations in sync'ed data. No need to provide a testing
// factory for ProfileInvalidationProvider and SyncInvalidationsService.
return;
}
invalidation::ProfileInvalidationProviderFactory::GetInstance()
->SetTestingFactory(
context,
base::BindRepeating(&SyncTest::CreateProfileInvalidationProvider,
&profile_to_fcm_network_handler_map_));
gcm::GCMProfileServiceFactory::GetInstance()->SetTestingFactory(
context, base::BindRepeating(&SyncTest::CreateGCMProfileService,
base::Unretained(this)));
}
// static
std::unique_ptr<KeyedService> SyncTest::CreateProfileInvalidationProvider(
std::map<const Profile*, invalidation::FCMNetworkHandler*>*
profile_to_fcm_network_handler_map,
content::BrowserContext* context) {
Profile* profile = Profile::FromBrowserContext(context);
gcm::GCMProfileService* gcm_profile_service =
gcm::GCMProfileServiceFactory::GetForProfile(profile);
instance_id::InstanceIDDriver* instance_id_driver =
instance_id::InstanceIDProfileServiceFactory::GetForProfile(profile)
->driver();
auto profile_identity_provider =
std::make_unique<invalidation::ProfileIdentityProvider>(
IdentityManagerFactory::GetForProfile(profile));
auto fcm_invalidation_service =
std::make_unique<invalidation::FCMInvalidationService>(
profile_identity_provider.get(),
base::BindRepeating(&CreateFCMNetworkHandler, profile,
profile_to_fcm_network_handler_map,
gcm_profile_service->driver(),
instance_id_driver),
base::BindRepeating(&CreatePerUserTopicSubscriptionManager,
profile->GetPrefs()),
instance_id_driver, profile->GetPrefs(), kInvalidationGCMSenderId);