File: sync_service_impl_harness.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (730 lines) | stat: -rw-r--r-- 26,812 bytes parent folder | download | duplicates (3)
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
// Copyright 2013 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_service_impl_harness.h"

#include <cstddef>
#include <sstream>
#include <utility>

#include "base/check_deref.h"
#include "base/command_line.h"
#include "base/feature_list.h"
#include "base/json/json_writer.h"
#include "base/logging.h"
#include "base/memory/ptr_util.h"
#include "base/memory/raw_ptr.h"
#include "base/test/bind.h"
#include "build/build_config.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/signin/identity_manager_factory.h"
#include "chrome/browser/sync/sync_service_factory.h"
#include "chrome/browser/sync/test/integration/invalidations/invalidations_status_checker.h"
#include "chrome/browser/sync/test/integration/quiesce_status_change_checker.h"
#include "chrome/browser/sync/test/integration/single_client_status_change_checker.h"
#include "chrome/browser/sync/test/integration/sync_signin_delegate.h"
#include "chrome/common/channel_info.h"
#include "components/signin/public/identity_manager/identity_manager.h"
#include "components/signin/public/identity_manager/identity_test_utils.h"
#include "components/sync/engine/net/url_translator.h"
#include "components/sync/engine/sync_string_conversions.h"
#include "components/sync/engine/traffic_logger.h"
#include "components/sync/protocol/sync.pb.h"
#include "components/sync/service/glue/sync_transport_data_prefs.h"
#include "components/sync/service/local_data_description.h"
#include "components/sync/service/sync_internals_util.h"
#include "content/public/browser/browser_context.h"
#include "content/public/test/simple_url_loader_test_helper.h"
#include "google_apis/google_api_keys.h"
#include "net/base/net_errors.h"
#include "net/traffic_annotation/network_traffic_annotation_test_helper.h"
#include "services/network/public/cpp/resource_request.h"
#include "services/network/public/cpp/shared_url_loader_factory.h"
#include "services/network/public/cpp/simple_url_loader.h"
#include "services/network/public/mojom/fetch_api.mojom-shared.h"
#include "third_party/zlib/google/compression_utils.h"

namespace {

using syncer::SyncCycleSnapshot;
using syncer::SyncServiceImpl;

constexpr char kSyncUrlClearServerDataKey[] = "sync-url-clear-server-data";

bool HasAuthError(SyncServiceImpl* service) {
  return service->GetAuthError().state() ==
             GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS ||
         service->GetAuthError().state() ==
             GoogleServiceAuthError::SERVICE_ERROR ||
         service->GetAuthError().state() ==
             GoogleServiceAuthError::REQUEST_CANCELED;
}

class EngineInitializeChecker : public SingleClientStatusChangeChecker {
 public:
  explicit EngineInitializeChecker(SyncServiceImpl* service)
      : SingleClientStatusChangeChecker(service) {}

  bool IsExitConditionSatisfied(std::ostream* os) override {
    *os << "Waiting for sync engine initialization to complete; actual "
           "transport state: "
        << syncer::sync_ui_util::TransportStateStringToDebugString(
               service()->GetTransportState())
        << ", disable reasons: "
        << syncer::sync_ui_util::GetDisableReasonsDebugString(
               service()->GetDisableReasons());
    if (service()->IsEngineInitialized()) {
      return true;
    }
    // Engine initialization is blocked by an auth error.
    if (HasAuthError(service())) {
      LOG(WARNING) << "Sync engine initialization blocked by auth error";
      return true;
    }
    // Engine initialization is blocked by a failure to fetch Oauth2 tokens.
    if (service()->IsRetryingAccessTokenFetchForTest()) {
      LOG(WARNING) << "Sync engine initialization blocked by failure to fetch "
                      "access tokens";
      return true;
    }
    // Still waiting on engine initialization.
    return false;
  }
};

class SyncSetupChecker : public SingleClientStatusChangeChecker {
 public:
  enum class State { kTransportActive, kFeatureActive };

  SyncSetupChecker(SyncServiceImpl* service, State wait_for_state)
      : SingleClientStatusChangeChecker(service),
        wait_for_state_(wait_for_state) {}

  bool IsExitConditionSatisfied(std::ostream* os) override {
    *os << "Waiting for sync setup to complete";

    syncer::SyncService::TransportState transport_state =
        service()->GetTransportState();
    if (transport_state == syncer::SyncService::TransportState::ACTIVE &&
        (wait_for_state_ != State::kFeatureActive ||
         service()->IsSyncFeatureActive())) {
      return true;
    }
    // Sync is blocked by an auth error.
    if (HasAuthError(service())) {
      return true;
    }

    // Still waiting on sync setup.
    return false;
  }

 private:
  const State wait_for_state_;
};

class SyncTransportStateChecker : public SingleClientStatusChangeChecker {
 public:
  SyncTransportStateChecker(SyncServiceImpl* service,
                            syncer::SyncService::TransportState state)
      : SingleClientStatusChangeChecker(service), state_(state) {}

  bool IsExitConditionSatisfied(std::ostream* os) override {
    *os << "Waiting for sync transport state to change";
    return service()->GetTransportState() == state_;
  }

 private:
  const syncer::SyncService::TransportState state_;
};

// Same as reset on chrome.google.com/sync.
// This function will wait until the reset is done. If error occurs,
// it will log error messages.
bool ResetAccount(network::SharedURLLoaderFactory* url_loader_factory,
                  const std::string& access_token,
                  const GURL& url,
                  const std::string& username,
                  const std::string& birthday) {
  // Generate https POST payload.
  sync_pb::ClientToServerMessage message;
  message.set_share(username);
  message.set_message_contents(
      sync_pb::ClientToServerMessage::CLEAR_SERVER_DATA);
  message.set_store_birthday(birthday);
  message.set_api_key(google_apis::GetAPIKey());
  syncer::LogClientToServerMessage(message);
  std::string payload;
  message.SerializeToString(&payload);
  std::string request_to_send;
  compression::GzipCompress(payload, &request_to_send);

  auto resource_request = std::make_unique<network::ResourceRequest>();
  resource_request->url = url;
  resource_request->method = "POST";
  resource_request->headers.SetHeader("Authorization",
                                      "Bearer " + access_token);
  resource_request->headers.SetHeader("Content-Encoding", "gzip");
  resource_request->headers.SetHeader("Accept-Language", "en-US,en");
  resource_request->credentials_mode = network::mojom::CredentialsMode::kOmit;
  std::unique_ptr<network::SimpleURLLoader> simple_loader =
      network::SimpleURLLoader::Create(std::move(resource_request),
                                       TRAFFIC_ANNOTATION_FOR_TESTS);
  simple_loader->AttachStringForUpload(request_to_send,
                                       "application/octet-stream");
  simple_loader->SetTimeoutDuration(base::Seconds(10));
  content::SimpleURLLoaderTestHelper url_loader_helper;
  simple_loader->DownloadToStringOfUnboundedSizeUntilCrashAndDie(
      url_loader_factory, url_loader_helper.GetCallbackDeprecated());
  url_loader_helper.WaitForCallback();
  if (simple_loader->NetError() != 0) {
    LOG(ERROR) << "Reset account failed with error "
               << net::ErrorToString(simple_loader->NetError())
               << ". The account will remain dirty and may cause test fail.";
    return false;
  }
  return true;
}

std::unique_ptr<SyncSigninDelegate> CreateSyncSigninDelegateForType(
    SyncServiceImplHarness::SigninType signin_type,
    Profile* profile) {
  CHECK(profile);
  switch (signin_type) {
    case SyncServiceImplHarness::SigninType::UI_SIGNIN:
      return CreateSyncSigninDelegateWithLiveSignin(profile);
    case SyncServiceImplHarness::SigninType::FAKE_SIGNIN:
      return CreateSyncSigninDelegateWithFakeSignin(profile);
  }
  NOTREACHED();
}

}  // namespace

// static
std::unique_ptr<SyncServiceImplHarness> SyncServiceImplHarness::Create(
    Profile* profile,
    SigninType signin_type) {
  CHECK(profile);
  return base::WrapUnique(new SyncServiceImplHarness(
      profile, CreateSyncSigninDelegateForType(signin_type, profile)));
}

SyncServiceImplHarness::SyncServiceImplHarness(
    Profile* profile,
    std::unique_ptr<SyncSigninDelegate> signin_delegate)
    : profile_(CHECK_DEREF(profile).GetWeakPtr()),
      service_(SyncServiceFactory::GetAsSyncServiceImplForProfileForTesting(
          profile)),
      profile_debug_name_(profile->GetDebugName()),
      signin_delegate_(std::move(signin_delegate)) {
  CHECK(profile_);
  CHECK(service_);
  CHECK(signin_delegate_);
}

SyncServiceImplHarness::~SyncServiceImplHarness() = default;

signin::GaiaIdHash SyncServiceImplHarness::GetGaiaIdHashForPrimaryAccount()
    const {
  signin::IdentityManager* identity_manager =
      IdentityManagerFactory::GetForProfile(profile_.get());
  return signin::GaiaIdHash::FromGaiaId(
      identity_manager->GetPrimaryAccountInfo(signin::ConsentLevel::kSignin)
          .gaia);
}

GaiaId SyncServiceImplHarness::GetGaiaIdForAccount(
    SyncTestAccount account) const {
  return signin_delegate_->GetGaiaIdForAccount(account);
}

std::string SyncServiceImplHarness::GetEmailForAccount(
    SyncTestAccount account) const {
  return signin_delegate_->GetEmailForAccount(account);
}

bool SyncServiceImplHarness::SignInPrimaryAccount(SyncTestAccount account) {
  if (!signin_delegate_->SignIn(account, signin::ConsentLevel::kSignin)) {
    return false;
  }

  signin::IdentityManager* identity_manager =
      IdentityManagerFactory::GetForProfile(profile_.get());
  CHECK(identity_manager->HasPrimaryAccount(signin::ConsentLevel::kSignin));
  CHECK(identity_manager->HasPrimaryAccountWithRefreshToken(
      signin::ConsentLevel::kSignin));
  CHECK(!service()->GetAccountInfo().IsEmpty());

  return true;
}

bool SyncServiceImplHarness::ResetSyncForPrimaryAccount() {
  syncer::SyncTransportDataPrefs transport_data_prefs(
      profile_->GetPrefs(), GetGaiaIdHashForPrimaryAccount());
  // Generate the https url.
  // CLEAR_SERVER_DATA isn't enabled on the prod Sync server,
  // so --sync-url-clear-server-data can be used to specify an
  // alternative endpoint.
  // Note: Any OTA(Owned Test Account) tries to clear data need to be
  // whitelisted.
  auto* cmd_line = base::CommandLine::ForCurrentProcess();
  DCHECK(cmd_line->HasSwitch(kSyncUrlClearServerDataKey))
      << "Missing switch " << kSyncUrlClearServerDataKey;
  GURL base_url(cmd_line->GetSwitchValueASCII(kSyncUrlClearServerDataKey) +
                "/command/?");
  GURL url = syncer::AppendSyncQueryString(base_url,
                                           transport_data_prefs.GetCacheGuid());

  // Call sync server to clear sync data.
  const std::string access_token = service()->GetAccessTokenForTest();
  if (access_token.empty()) {
    LOG(ERROR) << "Access token is not available.";
    return false;
  }
  return ResetAccount(profile_->GetURLLoaderFactory().get(), access_token, url,
                      service()->GetAccountInfo().email,
                      transport_data_prefs.GetBirthday());
}

#if !BUILDFLAG(IS_CHROMEOS)
void SyncServiceImplHarness::SignOutPrimaryAccount() {
  signin_delegate_->SignOut();
}
#endif  // !BUILDFLAG(IS_CHROMEOS)

#if !BUILDFLAG(IS_ANDROID)
void SyncServiceImplHarness::EnterSyncPausedStateForPrimaryAccount() {
  DCHECK(service_->IsSyncFeatureActive());
  signin::SetInvalidRefreshTokenForPrimaryAccount(
      IdentityManagerFactory::GetForProfile(profile_.get()));
}

bool SyncServiceImplHarness::ExitSyncPausedStateForPrimaryAccount() {
  signin::SetRefreshTokenForPrimaryAccount(
      IdentityManagerFactory::GetForProfile(profile_.get()));
  // The engine was off in the sync-paused state, so wait for it to start.
  return AwaitSyncSetupCompletion();
}

bool SyncServiceImplHarness::EnterSignInPendingStateForPrimaryAccount() {
  CHECK_EQ(service_->GetTransportState(),
           syncer::SyncServiceImpl::TransportState::ACTIVE);
  signin::IdentityManager* identity_manager =
      IdentityManagerFactory::GetForProfile(profile_.get());
  CHECK(identity_manager->HasPrimaryAccount(signin::ConsentLevel::kSignin));
  signin::SetInvalidRefreshTokenForPrimaryAccount(identity_manager);
  return AwaitSyncTransportPaused();
}

bool SyncServiceImplHarness::ExitSignInPendingStateForPrimaryAccount() {
  CHECK_EQ(service_->GetTransportState(),
           syncer::SyncService::TransportState::PAUSED);
  signin::SetRefreshTokenForPrimaryAccount(
      IdentityManagerFactory::GetForProfile(profile_.get()));
  return AwaitSyncTransportActive();
}
#endif  // !BUILDFLAG(IS_ANDROID)

bool SyncServiceImplHarness::SetupSync(SyncTestAccount account) {
  bool result =
      SetupSyncNoWaitForCompletion(account) && AwaitSyncSetupCompletion();
  if (!result) {
    LOG(ERROR) << profile_debug_name_ << ": SetupSync failed. Syncer status:\n"
               << GetServiceStatus();
  } else {
    DVLOG(1) << profile_debug_name_ << ": SetupSync successful.";
  }
  return result;
}

bool SyncServiceImplHarness::SetupSyncWithCustomSettings(
    SetUserSettingsCallback user_settings_callback,
    SyncTestAccount account) {
  bool result = SetupSyncWithCustomSettingsNoWaitForCompletion(
                    std::move(user_settings_callback), account) &&
                AwaitSyncSetupCompletion();
  if (!result) {
    LOG(ERROR) << profile_debug_name_ << ": SetupSync failed. Syncer status:\n"
               << GetServiceStatus();
  } else {
    DVLOG(1) << profile_debug_name_ << ": SetupSync successful.";
  }
  return result;
}

bool SyncServiceImplHarness::SetupSyncNoWaitForCompletion(
    SyncTestAccount account) {
  // By default, mimic the user confirming the default settings.
  return SetupSyncWithCustomSettingsNoWaitForCompletion(
      base::BindLambdaForTesting([](syncer::SyncUserSettings* user_settings) {
#if !BUILDFLAG(IS_CHROMEOS)
        user_settings->SetInitialSyncFeatureSetupComplete(
            syncer::SyncFirstSetupCompleteSource::BASIC_FLOW);
#endif  // !BUILDFLAG(IS_CHROMEOS)
      }),
      account);
}

bool SyncServiceImplHarness::SetupSyncWithCustomSettingsNoWaitForCompletion(
    SetUserSettingsCallback user_settings_callback,
    SyncTestAccount account) {
  if (service() == nullptr) {
    LOG(ERROR) << "SetupSync(): service() is null.";
    return false;
  }

  // Tell the sync service that setup is in progress so we don't start syncing
  // until we've finished configuration.
  sync_blocker_ = service()->GetSetupInProgressHandle();

  if (!signin_delegate_->SignIn(account, signin::ConsentLevel::kSync)) {
    return false;
  }

  signin::IdentityManager* identity_manager =
      IdentityManagerFactory::GetForProfile(profile_.get());
  // Note that `ConsentLevel::kSync` is not actually guaranteed at this stage.
  // Namely, live tests require closing the sync confirmation dialog before
  // `ConsentLevel::kSync` is granted. This is achieved later below with
  // `ConfirmSyncUI()`.
  CHECK(identity_manager->HasPrimaryAccount(signin::ConsentLevel::kSignin));

  if (!AwaitEngineInitialization()) {
    return false;
  }

  // Now give the caller a chance to configure settings (in particular, the
  // selected data types) before actually starting to sync. This callback
  // usually (but not necessarily) invokes SetInitialSyncFeatureSetupComplete().
  std::move(user_settings_callback).Run(service()->GetUserSettings());

  // Notify SyncServiceImpl that we are done with configuration.
  sync_blocker_.reset();

  if (!signin_delegate_->ConfirmSync()) {
    return false;
  }

  CHECK(identity_manager->HasPrimaryAccount(signin::ConsentLevel::kSync));
  CHECK(identity_manager->HasPrimaryAccountWithRefreshToken(
      signin::ConsentLevel::kSync));
  CHECK(!service()->GetAccountInfo().IsEmpty());

  return true;
}

void SyncServiceImplHarness::FinishSyncSetup() {
#if !BUILDFLAG(IS_CHROMEOS)
  service()->GetUserSettings()->SetInitialSyncFeatureSetupComplete(
      syncer::SyncFirstSetupCompleteSource::BASIC_FLOW);
#endif  // !BUILDFLAG(IS_CHROMEOS)
  sync_blocker_.reset();
}

bool SyncServiceImplHarness::AwaitMutualSyncCycleCompletion(
    SyncServiceImplHarness* partner) {
  std::vector<SyncServiceImplHarness*> harnesses;
  harnesses.push_back(this);
  harnesses.push_back(partner);
  return AwaitQuiescence(harnesses);
}

// static
bool SyncServiceImplHarness::AwaitQuiescence(
    const std::vector<SyncServiceImplHarness*>& clients) {
  if (clients.empty()) {
    return true;
  }

  std::vector<raw_ptr<SyncServiceImpl, VectorExperimental>> services;
  for (SyncServiceImplHarness* harness : clients) {
    services.push_back(harness->service());
  }
  return QuiesceStatusChangeChecker(services).Wait();
}

bool SyncServiceImplHarness::AwaitEngineInitialization() {
  if (!EngineInitializeChecker(service()).Wait()) {
    LOG(ERROR) << "EngineInitializeChecker timed out.";
    return false;
  }

  if (HasAuthError(service())) {
    LOG(ERROR) << "Credentials were rejected. Sync cannot proceed.";
    return false;
  }

  if (service()->IsRetryingAccessTokenFetchForTest()) {
    LOG(ERROR) << "Failed to fetch access token. Sync cannot proceed.";
    return false;
  }

  if (!service()->IsEngineInitialized()) {
    LOG(ERROR) << "Service engine not initialized.";
    return false;
  }

  return true;
}

bool SyncServiceImplHarness::AwaitSyncSetupCompletion() {
  CHECK(service()->GetUserSettings()->IsInitialSyncFeatureSetupComplete())
      << "Waiting for setup completion can only succeed after the first setup "
      << "got marked complete. Did you call SetupSync on this client?";
  if (!SyncSetupChecker(service(), SyncSetupChecker::State::kFeatureActive)
           .Wait()) {
    LOG(ERROR) << "SyncSetupChecker timed out.";
    return false;
  }
  // Signal an error if the initial sync wasn't successful.
  if (HasAuthError(service())) {
    LOG(ERROR) << "Credentials were rejected. Sync cannot proceed.";
    return false;
  }

  return true;
}

bool SyncServiceImplHarness::AwaitSyncTransportActive() {
  if (!SyncSetupChecker(service(), SyncSetupChecker::State::kTransportActive)
           .Wait()) {
    LOG(ERROR) << "SyncSetupChecker timed out.";
    return false;
  }
  // Signal an error if the initial sync wasn't successful.
  if (HasAuthError(service())) {
    LOG(ERROR) << "Credentials were rejected. Sync cannot proceed.";
    return false;
  }

  return true;
}

bool SyncServiceImplHarness::AwaitSyncTransportPaused() {
  if (!SyncTransportStateChecker(service(),
                                 syncer::SyncService::TransportState::PAUSED)
           .Wait()) {
    LOG(ERROR) << "SyncTransportStateChecker timed out.";
    return false;
  }
  return true;
}

bool SyncServiceImplHarness::AwaitInvalidationsStatus(bool expected_status) {
  return InvalidationsStatusChecker(service(), expected_status).Wait();
}

bool SyncServiceImplHarness::EnableSyncForType(
    syncer::UserSelectableType type) {
  DVLOG(1) << GetClientInfoString(
      "EnableSyncForType(" +
      std::string(syncer::GetUserSelectableTypeName(type)) + ")");

  if (!IsSyncEnabledByUser()) {
    bool result = SetupSyncWithCustomSettings(base::BindLambdaForTesting(
        [type](syncer::SyncUserSettings* user_settings) {
          user_settings->SetSelectedTypes(false, {type});
#if !BUILDFLAG(IS_CHROMEOS)
          user_settings->SetInitialSyncFeatureSetupComplete(
              syncer::SyncFirstSetupCompleteSource::ADVANCED_FLOW_CONFIRM);
#endif  // !BUILDFLAG(IS_CHROMEOS)
        }));
    // If SetupSync() succeeded, then Sync must now be enabled.
    DCHECK(!result || IsSyncEnabledByUser());
    return result;
  }

  if (service() == nullptr) {
    LOG(ERROR) << "EnableSyncForType(): service() is null.";
    return false;
  }

  syncer::UserSelectableTypeSet selected_types =
      service()->GetUserSettings()->GetSelectedTypes();
  if (selected_types.Has(type)) {
    DVLOG(1) << "EnableSyncForType(): Sync already enabled for type "
             << syncer::GetUserSelectableTypeName(type) << " on "
             << profile_debug_name_ << ".";
    return true;
  }

  selected_types.Put(type);
  service()->GetUserSettings()->SetSelectedTypes(false, selected_types);
  if (AwaitSyncSetupCompletion()) {
    DVLOG(1) << "EnableSyncForType(): Enabled sync for type "
             << syncer::GetUserSelectableTypeName(type) << " on "
             << profile_debug_name_ << ".";
    return true;
  }

  DVLOG(0) << GetClientInfoString("EnableSyncForType failed");
  return false;
}

bool SyncServiceImplHarness::DisableSyncForType(
    syncer::UserSelectableType type) {
  DVLOG(1) << GetClientInfoString(
      "DisableSyncForType(" +
      std::string(syncer::GetUserSelectableTypeName(type)) + ")");

  if (service() == nullptr) {
    LOG(ERROR) << "DisableSyncForType(): service() is null.";
    return false;
  }

  syncer::UserSelectableTypeSet selected_types =
      service()->GetUserSettings()->GetSelectedTypes();
  if (!selected_types.Has(type)) {
    DVLOG(1) << "DisableSyncForType(): Sync already disabled for type "
             << syncer::GetUserSelectableTypeName(type) << " on "
             << profile_debug_name_ << ".";
    return true;
  }

  selected_types.Remove(type);
  service()->GetUserSettings()->SetSelectedTypes(false, selected_types);
  if (AwaitSyncSetupCompletion()) {
    DVLOG(1) << "DisableSyncForType(): Disabled sync for type "
             << syncer::GetUserSelectableTypeName(type) << " on "
             << profile_debug_name_ << ".";
    return true;
  }

  DVLOG(0) << GetClientInfoString("DisableSyncForDatatype failed");
  return false;
}

bool SyncServiceImplHarness::EnableSyncForRegisteredDatatypes() {
  DVLOG(1) << GetClientInfoString("EnableSyncForRegisteredDatatypes");

  if (!IsSyncEnabledByUser()) {
    bool result = SetupSync();
    // If SetupSync() succeeded, then Sync must now be enabled.
    DCHECK(!result || IsSyncEnabledByUser());
    return result;
  }

  if (service() == nullptr) {
    LOG(ERROR) << "EnableSyncForRegisteredDatatypes(): service() is null.";
    return false;
  }

  service()->GetUserSettings()->SetSelectedTypes(
      /*sync_everything=*/true,
      service()->GetUserSettings()->GetRegisteredSelectableTypes());

  if (AwaitSyncSetupCompletion()) {
    DVLOG(1)
        << "EnableSyncForRegisteredDatatypes(): Enabled sync for all datatypes "
        << "on " << profile_debug_name_ << ".";
    return true;
  }

  DVLOG(0) << GetClientInfoString("EnableSyncForRegisteredDatatypes failed");
  return false;
}

bool SyncServiceImplHarness::DisableSyncForAllDatatypes() {
  DVLOG(1) << GetClientInfoString("DisableSyncForAllDatatypes");

  if (service() == nullptr) {
    LOG(ERROR) << "DisableSyncForAllDatatypes(): service() is null.";
    return false;
  }

  service()->GetUserSettings()->SetSelectedTypes(
      /*sync_everything=*/false, syncer::UserSelectableTypeSet());

  DVLOG(1) << "DisableSyncForAllDatatypes(): Disabled sync for all "
           << "datatypes on " << profile_debug_name_;
  return true;
}

SyncCycleSnapshot SyncServiceImplHarness::GetLastCycleSnapshot() const {
  DCHECK(service() != nullptr) << "Sync service has not yet been set up.";
  if (service()->IsSyncFeatureActive()) {
    return service()->GetLastCycleSnapshotForDebugging();
  }
  return SyncCycleSnapshot();
}

absl::flat_hash_map<syncer::DataType, size_t>
SyncServiceImplHarness::GetTypesWithUnsyncedDataAndWait(
    syncer::DataTypeSet requested_types) const {
  base::test::TestFuture<absl::flat_hash_map<syncer::DataType, size_t>> future;
  service()->GetTypesWithUnsyncedData(requested_types, future.GetCallback());
  return future.Get();
}

syncer::LocalDataDescription
SyncServiceImplHarness::GetLocalDataDescriptionAndWait(
    syncer::DataType data_type) {
  base::test::TestFuture<
      std::map<syncer::DataType, syncer::LocalDataDescription>>
      descriptions;
  service()->GetLocalDataDescriptions({data_type}, descriptions.GetCallback());

  if (descriptions.Get().size() != 1u) {
    ADD_FAILURE()
        << "The expected size of local data description map is 1. Found "
        << descriptions.Get().size() << '.';
    return syncer::LocalDataDescription();
  }

  if (descriptions.Get().begin()->first != data_type) {
    ADD_FAILURE()
        << DataTypeToDebugString(data_type)
        << " is the only expected key in the local data description map. Found "
        << DataTypeToDebugString(descriptions.Get().begin()->first) << '.';
    return syncer::LocalDataDescription();
  }

  return descriptions.Get().begin()->second;
}

std::string SyncServiceImplHarness::GetServiceStatus() {
  // This method is only used in test code for debugging purposes, so it's fine
  // to include sensitive data in ConstructAboutInformation().
  base::Value::Dict value = syncer::sync_ui_util::ConstructAboutInformation(
      syncer::sync_ui_util::IncludeSensitiveData(true), service(),
      chrome::GetChannelName(chrome::WithExtendedStable(true)));
  std::string service_status;
  base::JSONWriter::WriteWithOptions(
      value, base::JSONWriter::OPTIONS_PRETTY_PRINT, &service_status);
  return service_status;
}

// TODO(sync): Clean up this method in a separate CL. Remove all snapshot fields
// and log shorter, more meaningful messages.
std::string SyncServiceImplHarness::GetClientInfoString(
    const std::string& message) const {
  std::stringstream os;
  os << profile_debug_name_ << ": " << message << ": ";
  if (service()) {
    const SyncCycleSnapshot& snap = GetLastCycleSnapshot();
    syncer::SyncStatus status;
    service()->QueryDetailedSyncStatusForDebugging(&status);
    // Capture select info from the sync session snapshot and syncer status.
    os << ", has_unsynced_items: " << snap.has_remaining_local_changes()
       << ", did_commit: "
       << (snap.model_neutral_state().num_successful_commits == 0 &&
           snap.model_neutral_state().commit_result.type() ==
               syncer::SyncerError::Type::kSuccess)
       << ", server conflicts: " << snap.num_server_conflicts()
       << ", num_updates_downloaded : "
       << snap.model_neutral_state().num_updates_downloaded_total
       << ", passphrase_required: "
       << service()->GetUserSettings()->IsPassphraseRequired()
       << ", notifications_enabled: " << status.notifications_enabled
       << ", service_is_active: " << service()->IsSyncFeatureActive();
  } else {
    os << "Sync service not available";
  }
  return os.str();
}

bool SyncServiceImplHarness::IsSyncEnabledByUser() const {
  return service()->GetUserSettings()->IsInitialSyncFeatureSetupComplete();
}