File: single_client_sessions_sync_test.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 (1051 lines) | stat: -rw-r--r-- 39,939 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
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
// 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 "base/base64.h"
#include "base/containers/to_vector.h"
#include "base/memory/raw_ptr.h"
#include "base/run_loop.h"
#include "base/strings/stringprintf.h"
#include "base/task/single_thread_task_runner.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/test_timeouts.h"
#include "base/threading/thread_restrictions.h"
#include "build/build_config.h"
#include "chrome/browser/browser_features.h"
#include "chrome/browser/favicon/favicon_service_factory.h"
#include "chrome/browser/favicon/large_icon_service_factory.h"
#include "chrome/browser/history/history_service_factory.h"
#include "chrome/browser/signin/identity_manager_factory.h"
#include "chrome/browser/sync/session_sync_service_factory.h"
#include "chrome/browser/sync/test/integration/history_helper.h"
#include "chrome/browser/sync/test/integration/session_hierarchy_match_checker.h"
#include "chrome/browser/sync/test/integration/sessions_helper.h"
#include "chrome/browser/sync/test/integration/sync_service_impl_harness.h"
#include "chrome/browser/sync/test/integration/sync_test.h"
#include "chrome/browser/sync/test/integration/updated_progress_marker_checker.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_commands.h"
#include "chrome/common/webui_url_constants.h"
#include "components/favicon/core/favicon_service.h"
#include "components/favicon/core/large_icon_service_impl.h"
#include "components/favicon_base/favicon_types.h"
#include "components/history/core/browser/history_service.h"
#include "components/history/core/browser/history_types.h"
#include "components/sessions/core/session_types.h"
#include "components/signin/public/identity_manager/account_info.h"
#include "components/signin/public/identity_manager/accounts_in_cookie_jar_info.h"
#include "components/signin/public/identity_manager/identity_test_utils.h"
#include "components/sync/base/time.h"
#include "components/sync/engine/cycle/entity_change_metric_recording.h"
#include "components/sync/protocol/entity_specifics.pb.h"
#include "components/sync/protocol/proto_value_conversions.h"
#include "components/sync/protocol/session_specifics.pb.h"
#include "components/sync/protocol/sync.pb.h"
#include "components/sync/protocol/sync_entity.pb.h"
#include "components/sync/service/glue/sync_transport_data_prefs.h"
#include "components/sync/test/sessions_hierarchy.h"
#include "components/sync_sessions/session_store.h"
#include "components/sync_sessions/session_sync_service.h"
#include "components/sync_sessions/session_sync_test_helper.h"
#include "content/public/test/browser_test.h"
#include "google_apis/gaia/gaia_auth_util.h"
#include "net/dns/mock_host_resolver.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
#include "net/test/embedded_test_server/http_request.h"
#include "net/test/embedded_test_server/http_response.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace {

using fake_server::SessionsHierarchy;
using history_helper::GetUrlFromClient;
using sessions_helper::CheckInitialState;
using sessions_helper::CloseTab;
using sessions_helper::GetLocalWindows;
using sessions_helper::GetSessionData;
using sessions_helper::MoveTab;
using sessions_helper::NavigateTab;
using sessions_helper::NavigateTabBack;
using sessions_helper::NavigateTabForward;
using sessions_helper::OpenTab;
using sessions_helper::OpenTabAtIndex;
using sessions_helper::ScopedWindowMap;
using sessions_helper::SessionEntitiesChecker;
using sessions_helper::SyncedSessionVector;
using sessions_helper::WaitForTabsToLoad;
using sessions_helper::WindowsMatch;
using sync_sessions::SessionSyncTestHelper;
using testing::_;
using testing::UnorderedElementsAre;

MATCHER_P(SessionHeader, session_tag, "") {
  return arg.has_header() && arg.session_tag() == session_tag;
}

static const char* kBaseFragmentURL =
    "data:text/html,<html><title>Fragment</title><body></body></html>";
static const char* kSpecifiedFragmentURL =
    "data:text/html,<html><title>Fragment</title><body></body></html>#fragment";

std::unique_ptr<net::test_server::HttpResponse> FaviconServerRequestHandler(
    const net::test_server::HttpRequest& request) {
  // An arbitrary 16x16 png (opaque black square), Base64 encoded.
  const std::string kTestPngBase64 =
      "iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAQAAAC1+jfqAAAAE0lEQVR42mNk+M+"
      "AFzCOKhhJCgBrLxABLz0PwwAAAABJRU5ErkJggg==";
  std::string content;
  base::Base64Decode(kTestPngBase64, &content);

  std::unique_ptr<net::test_server::BasicHttpResponse> http_response(
      new net::test_server::BasicHttpResponse);
  http_response->set_content(content);
  http_response->set_code(net::HTTP_OK);
  return std::move(http_response);
}

class IsIconURLSyncedChecker : public SingleClientStatusChangeChecker {
 public:
  IsIconURLSyncedChecker(const std::string& page_url,
                         const std::string& icon_url,
                         fake_server::FakeServer* fake_server,
                         syncer::SyncServiceImpl* service)
      : SingleClientStatusChangeChecker(service),
        page_url_(page_url),
        icon_url_(icon_url),
        fake_server_(fake_server) {}

  // StatusChangeChecker implementation.
  bool IsExitConditionSatisfied(std::ostream* os) override {
    *os << "Waiting for URLs to be commited to the server";
    std::vector<sync_pb::SyncEntity> sessions =
        fake_server_->GetSyncEntitiesByDataType(syncer::SESSIONS);
    for (const sync_pb::SyncEntity& entity : sessions) {
      const sync_pb::SessionSpecifics& session_specifics =
          entity.specifics().session();
      if (!session_specifics.has_tab()) {
        continue;
      }
      for (int i = 0; i < session_specifics.tab().navigation_size(); i++) {
        const sync_pb::TabNavigation& nav =
            session_specifics.tab().navigation(i);
        if (nav.has_virtual_url() && nav.has_favicon_url() &&
            nav.virtual_url() == page_url_ && nav.favicon_url() == icon_url_) {
          return true;
        }
      }
    }
    return false;
  }

 private:
  const std::string page_url_;
  const std::string icon_url_;
  const raw_ptr<fake_server::FakeServer> fake_server_;
};

// Checker to block until the history DB for |profile| does / does not have a
// favicon for |page_url| (depending on |should_be_available|).
class FaviconForPageUrlAvailableChecker : public StatusChangeChecker {
 public:
  FaviconForPageUrlAvailableChecker(Profile* profile,
                                    const GURL& page_url,
                                    bool should_be_available)
      : profile_(profile),
        page_url_(page_url),
        should_be_available_(should_be_available) {
    history::HistoryService* history_service =
        HistoryServiceFactory::GetForProfile(
            profile_, ServiceAccessType::EXPLICIT_ACCESS);
    callback_subscription_ =
        history_service->AddFaviconsChangedCallback(base::BindRepeating(
            &FaviconForPageUrlAvailableChecker::OnFaviconsChanged,
            base::Unretained(this)));

    // Load the state asynchronously to figure out if further waiting is needed.
    CheckExitConditionAsync();
  }
  ~FaviconForPageUrlAvailableChecker() override = default;

 protected:
  // StatusChangeChecker implementation.
  bool IsExitConditionSatisfied(std::ostream* os) override {
    return exit_condition_satisfied_;
  }

 private:
  void OnFaviconsChanged(const std::set<GURL>& page_urls,
                         const GURL& icon_url) {
    for (const GURL& page_url : page_urls) {
      if (page_url == page_url_) {
        CheckExitConditionAsync();
      }
    }
  }

  void CheckExitConditionAsync() {
    favicon::FaviconService* favicon_service =
        FaviconServiceFactory::GetForProfile(
            profile_, ServiceAccessType::EXPLICIT_ACCESS);
    favicon_service->GetFaviconImageForPageURL(
        page_url_,
        base::BindOnce(&FaviconForPageUrlAvailableChecker::OnFaviconLoaded,
                       base::Unretained(this)),
        &tracker_);
  }

  void OnFaviconLoaded(const favicon_base::FaviconImageResult& result) {
    bool is_available = !result.image.IsEmpty();
    exit_condition_satisfied_ = (is_available == should_be_available_);
    CheckExitCondition();
  }

  const raw_ptr<Profile> profile_;
  const GURL page_url_;
  const bool should_be_available_;
  base::CallbackListSubscription callback_subscription_;
  bool exit_condition_satisfied_ = false;
  base::CancelableTaskTracker tracker_;
};

class SingleClientSessionsSyncTest : public SyncTest {
 public:
  SingleClientSessionsSyncTest() : SyncTest(SINGLE_CLIENT) {}

  SingleClientSessionsSyncTest(const SingleClientSessionsSyncTest&) = delete;
  SingleClientSessionsSyncTest& operator=(const SingleClientSessionsSyncTest&) =
      delete;

  ~SingleClientSessionsSyncTest() override = default;

  void ExpectNavigationChain(const std::vector<GURL>& urls) {
    ScopedWindowMap windows;
    ASSERT_TRUE(GetLocalWindows(0, &windows));
    ASSERT_EQ(windows.begin()->second->wrapped_window.tabs.size(), 1u);
    sessions::SessionTab* tab =
        windows.begin()->second->wrapped_window.tabs[0].get();

    int index = 0;
    EXPECT_EQ(urls.size(), tab->navigations.size());
    for (const sessions::SerializedNavigationEntry& navigation :
         tab->navigations) {
      EXPECT_EQ(urls[index], navigation.virtual_url());
      index++;
    }
  }

  // Block until the expected hierarchy is recorded on the FakeServer for
  // profile 0. This will time out if the hierarchy is never recorded.
  void WaitForHierarchyOnServer(
      const fake_server::SessionsHierarchy& hierarchy) {
    SessionHierarchyMatchChecker checker(hierarchy, GetSyncService(0),
                                         GetFakeServer());
    EXPECT_TRUE(checker.Wait());
  }

  // Shortcut to call WaitForHierarchyOnServer for only |url| in a single
  // window.
  void WaitForURLOnServer(const GURL& url) {
    WaitForHierarchyOnServer({{url.spec()}});
  }

  // Simulates receiving list of accounts in the cookie jar from ListAccounts
  // endpoint. Adds |account_ids| into signed in accounts, notifies
  // SyncServiceImpl and waits for change to propagate to sync engine.
  void UpdateCookieJarAccountsAndWait(std::vector<CoreAccountInfo> accounts,
                                      bool expected_cookie_jar_mismatch) {
    std::vector<gaia::ListedAccount> signed_in_accounts =
        base::ToVector(accounts, [](const CoreAccountInfo& account) {
          gaia::ListedAccount listed_account;
          listed_account.id = account.account_id;
          listed_account.gaia_id = account.gaia;
          listed_account.email = account.email;
          return listed_account;
        });
    signin::AccountsInCookieJarInfo cookies(/*accounts_are_fresh=*/true,
                                            signed_in_accounts);
    base::RunLoop run_loop;
    EXPECT_EQ(expected_cookie_jar_mismatch,
              GetClient(0)->service()->HasCookieJarMismatch(
                  cookies.GetPotentiallyInvalidSignedInAccounts()));
    GetClient(0)->service()->OnAccountsInCookieUpdatedWithCallback(
        cookies, run_loop.QuitClosure());
    run_loop.Run();
  }

 protected:
  void SetUpOnMainThread() override {
    host_resolver()->AddRule("*", "127.0.0.1");
    ASSERT_TRUE(embedded_test_server()->Start());
    SyncTest::SetUpOnMainThread();
  }
};

IN_PROC_BROWSER_TEST_F(SingleClientSessionsSyncTest,
                       RequireUserSelectableTypeTabsForUiDelegate) {
  ASSERT_TRUE(SetupSync());
  ASSERT_TRUE(CheckInitialState(0));

  sync_sessions::SessionSyncService* service =
      SessionSyncServiceFactory::GetForProfile(GetProfile(0));

  EXPECT_NE(nullptr, service->GetOpenTabsUIDelegate());
  ASSERT_TRUE(
      GetClient(0)->DisableSyncForType(syncer::UserSelectableType::kTabs));
  EXPECT_EQ(nullptr, service->GetOpenTabsUIDelegate());
}

IN_PROC_BROWSER_TEST_F(SingleClientSessionsSyncTest, Sanity) {
  ASSERT_TRUE(SetupSync());

  ASSERT_TRUE(CheckInitialState(0));

  // Add a new session to client 0 and wait for it to sync.
  ScopedWindowMap old_windows;
  GURL url = embedded_test_server()->GetURL("/sync/simple.html");
  ASSERT_TRUE(OpenTab(0, url));
  EXPECT_TRUE(GetLocalWindows(0, &old_windows));
  EXPECT_TRUE(UpdatedProgressMarkerChecker(GetSyncService(0)).Wait());

  // Get foreign session data from client 0.
  SyncedSessionVector sessions;
  EXPECT_FALSE(GetSessionData(0, &sessions));
  EXPECT_EQ(0U, sessions.size());

  // Verify client didn't change.
  ScopedWindowMap new_windows;
  ASSERT_TRUE(GetLocalWindows(0, &new_windows));
  EXPECT_TRUE(WindowsMatch(old_windows, new_windows));

  WaitForURLOnServer(url);
}

IN_PROC_BROWSER_TEST_F(SingleClientSessionsSyncTest, PRE_SessionStartTime) {
  const base::Time initial_time = base::Time::Now();
  ASSERT_TRUE(SetupSync());

  ASSERT_TRUE(CheckInitialState(0));

  // Add a tab and wait for it to sync.
  GURL url =
      embedded_test_server()->GetURL("www.host1.com", "/sync/simple.html");
  ASSERT_TRUE(OpenTab(0, url));
  EXPECT_TRUE(UpdatedProgressMarkerChecker(GetSyncService(0)).Wait());

  WaitForURLOnServer(url);

  // Ensure the session start time was properly populated in the header.
  std::vector<sync_pb::SyncEntity> entities =
      fake_server_->GetSyncEntitiesByDataType(syncer::SESSIONS);
  EXPECT_EQ(entities.size(), 2u);
  bool found_header = false;
  for (const sync_pb::SyncEntity& entity : entities) {
    const sync_pb::SessionSpecifics session = entity.specifics().session();
    if (session.has_header()) {
      found_header = true;
      EXPECT_TRUE(session.header().has_session_start_time_unix_epoch_millis());
      EXPECT_GE(base::Time::FromMillisecondsSinceUnixEpoch(
                    session.header().session_start_time_unix_epoch_millis()),
                initial_time);
    }
  }
  EXPECT_TRUE(found_header);
}

IN_PROC_BROWSER_TEST_F(SingleClientSessionsSyncTest, SessionStartTime) {
  const base::Time initial_time = base::Time::Now();
  ASSERT_TRUE(SetupClients());

  // Open another tab and wait for it to sync, just to ensure everything's up
  // to date.
  GURL url =
      embedded_test_server()->GetURL("www.host2.com", "/sync/simple.html");
  ASSERT_TRUE(OpenTab(0, url));
  EXPECT_TRUE(UpdatedProgressMarkerChecker(GetSyncService(0)).Wait());

  WaitForURLOnServer(url);

  // Ensure the session start time in the header was *not* updated.
  std::vector<sync_pb::SyncEntity> entities =
      fake_server_->GetSyncEntitiesByDataType(syncer::SESSIONS);
  EXPECT_EQ(entities.size(), 2u);
  bool found_header = false;
  for (const sync_pb::SyncEntity& entity : entities) {
    const sync_pb::SessionSpecifics session = entity.specifics().session();
    if (session.has_header()) {
      found_header = true;
      EXPECT_TRUE(session.header().has_session_start_time_unix_epoch_millis());
      // `initial_time` is after the browser restart, so the session start time
      // should be before that.
      EXPECT_LT(base::Time::FromMillisecondsSinceUnixEpoch(
                    session.header().session_start_time_unix_epoch_millis()),
                initial_time);
    }
  }
  EXPECT_TRUE(found_header);
}

#if !BUILDFLAG(IS_CHROMEOS)
// Regression test for crbug.com/361256057.
IN_PROC_BROWSER_TEST_F(SingleClientSessionsSyncTest, UpdateSessionTag) {
  ASSERT_TRUE(SetupSync(SyncTestAccount::kConsumerAccount1));

  ASSERT_TRUE(CheckInitialState(0));

  std::string first_cache_guid;
  {
    syncer::SyncTransportDataPrefs prefs(
        GetProfile(0)->GetPrefs(),
        GetClient(0)->GetGaiaIdHashForPrimaryAccount());
    first_cache_guid = prefs.GetCacheGuid();
  }

  // Note: The only tab that exists is an NTP, which is not interesting and thus
  // not uploaded. So the only SESSIONS entity on the server is now a header.
  ASSERT_TRUE(SessionEntitiesChecker(
                  UnorderedElementsAre(SessionHeader(first_cache_guid)))
                  .Wait());

  // Disable Sync, then turn it on again with a different account.
  GetClient(0)->SignOutPrimaryAccount();
  ASSERT_TRUE(GetClient(0)->SetupSync(SyncTestAccount::kConsumerAccount2));

  std::string second_cache_guid;
  {
    syncer::SyncTransportDataPrefs prefs(
        GetProfile(0)->GetPrefs(),
        GetClient(0)->GetGaiaIdHashForPrimaryAccount());
    second_cache_guid = prefs.GetCacheGuid();
  }
  ASSERT_NE(first_cache_guid, second_cache_guid);

  // Ensure that a session header with the new cache GUID was uploaded.
  EXPECT_TRUE(SessionEntitiesChecker(
                  UnorderedElementsAre(SessionHeader(first_cache_guid),
                                       SessionHeader(second_cache_guid)))
                  .Wait());
}
#endif  // !BUILDFLAG(IS_CHROMEOS)

IN_PROC_BROWSER_TEST_F(SingleClientSessionsSyncTest, NavigateInTab) {
  ASSERT_TRUE(SetupSync());
  ASSERT_TRUE(CheckInitialState(0));

  GURL url1 =
      embedded_test_server()->GetURL("www.host1.com", "/sync/simple.html");
  GURL url2 =
      embedded_test_server()->GetURL("www.host2.com", "/sync/simple.html");

  ASSERT_TRUE(OpenTab(0, url1));
  WaitForHierarchyOnServer(SessionsHierarchy({{url1.spec()}}));

  NavigateTab(0, url2);
  WaitForHierarchyOnServer(SessionsHierarchy({{url2.spec()}}));
}

IN_PROC_BROWSER_TEST_F(SingleClientSessionsSyncTest,
                       SessionsWithoutHistorySync) {
  ASSERT_TRUE(SetupSync());
  // If the user disables history sync on settings, but still enables tab sync,
  // then sessions should be synced but the server should be able to tell the
  // difference based on active datatypes.
  ASSERT_TRUE(
      GetClient(0)->DisableSyncForType(syncer::UserSelectableType::kHistory));
  ASSERT_TRUE(CheckInitialState(0));

  GURL url1 =
      embedded_test_server()->GetURL("www.host1.com", "/sync/simple.html");
  GURL url2 =
      embedded_test_server()->GetURL("www.host2.com", "/sync/simple.html");

  ASSERT_TRUE(OpenTab(0, url1));
  WaitForHierarchyOnServer(SessionsHierarchy({{url1.spec()}}));

  NavigateTab(0, url2);
  WaitForHierarchyOnServer(SessionsHierarchy({{url2.spec()}}));
}

IN_PROC_BROWSER_TEST_F(SingleClientSessionsSyncTest, NoSessions) {
  ASSERT_TRUE(SetupSync());

  WaitForHierarchyOnServer(SessionsHierarchy());
}

IN_PROC_BROWSER_TEST_F(SingleClientSessionsSyncTest, TimestampMatchesHistory) {
  ASSERT_TRUE(SetupSync());

  ASSERT_TRUE(CheckInitialState(0));

  GURL url = embedded_test_server()->GetURL("/sync/simple.html");

  ScopedWindowMap windows;
  ASSERT_TRUE(OpenTab(0, url));
  ASSERT_TRUE(GetLocalWindows(0, &windows));

  int found_navigations = 0;
  for (const auto& [window_id, window] : windows) {
    for (const std::unique_ptr<sessions::SessionTab>& tab :
         window->wrapped_window.tabs) {
      for (const sessions::SerializedNavigationEntry& navigation :
           tab->navigations) {
        history::URLRow virtual_row;
        ASSERT_TRUE(
            GetUrlFromClient(0, navigation.virtual_url(), &virtual_row));
        const base::Time history_timestamp = virtual_row.last_visit();
        // Propagated timestamps have millisecond-level resolution, so we avoid
        // exact comparison here (i.e. usecs might differ).
        ASSERT_EQ(
            0, (navigation.timestamp() - history_timestamp).InMilliseconds());
        ++found_navigations;
      }
    }
  }
  ASSERT_EQ(1, found_navigations);
}

IN_PROC_BROWSER_TEST_F(SingleClientSessionsSyncTest, ResponseCodeIsPreserved) {
  ASSERT_TRUE(SetupSync());

  ASSERT_TRUE(CheckInitialState(0));

  GURL url = embedded_test_server()->GetURL("/sync/simple.html");

  ScopedWindowMap windows;
  ASSERT_TRUE(OpenTab(0, url));
  ASSERT_TRUE(GetLocalWindows(0, &windows));

  int found_navigations = 0;
  for (const auto& [window_id, window] : windows) {
    for (const std::unique_ptr<sessions::SessionTab>& tab :
         window->wrapped_window.tabs) {
      for (const sessions::SerializedNavigationEntry& navigation :
           tab->navigations) {
        EXPECT_EQ(200, navigation.http_status_code());
        ++found_navigations;
      }
    }
  }
  ASSERT_EQ(1, found_navigations);
}

IN_PROC_BROWSER_TEST_F(SingleClientSessionsSyncTest, FragmentURLNavigation) {
  ASSERT_TRUE(SetupSync());
  ASSERT_TRUE(CheckInitialState(0));

  const GURL url(kBaseFragmentURL);
  ASSERT_TRUE(OpenTab(0, url));
  WaitForURLOnServer(url);

  const GURL fragment_url(kSpecifiedFragmentURL);
  NavigateTab(0, fragment_url);
  WaitForURLOnServer(fragment_url);
}

IN_PROC_BROWSER_TEST_F(SingleClientSessionsSyncTest,
                       NavigationChainForwardBack) {
  ASSERT_TRUE(SetupSync());
  ASSERT_TRUE(CheckInitialState(0));

  GURL first_url =
      embedded_test_server()->GetURL("www.host1.com", "/sync/simple.html");
  ASSERT_TRUE(OpenTab(0, first_url));
  WaitForURLOnServer(first_url);

  GURL second_url =
      embedded_test_server()->GetURL("www.host2.com", "/sync/simple.html");
  NavigateTab(0, second_url);
  WaitForURLOnServer(second_url);

  NavigateTabBack(0);
  WaitForURLOnServer(first_url);

  ExpectNavigationChain({first_url, second_url});

  NavigateTabForward(0);
  WaitForURLOnServer(second_url);

  ExpectNavigationChain({first_url, second_url});
}

IN_PROC_BROWSER_TEST_F(SingleClientSessionsSyncTest,
                       NavigationChainAlteredDestructively) {
  ASSERT_TRUE(SetupSync());
  ASSERT_TRUE(CheckInitialState(0));

  GURL base_url =
      embedded_test_server()->GetURL("www.base.com", "/sync/simple.html");
  ASSERT_TRUE(OpenTab(0, base_url));
  WaitForURLOnServer(base_url);

  GURL first_url =
      embedded_test_server()->GetURL("www.host1.com", "/sync/simple.html");
  NavigateTab(0, first_url);
  WaitForURLOnServer(first_url);

  // Check that the navigation chain matches the above sequence of {base_url,
  // first_url}.
  ExpectNavigationChain({base_url, first_url});

  NavigateTabBack(0);
  WaitForURLOnServer(base_url);

  GURL second_url =
      embedded_test_server()->GetURL("www.host2.com", "/sync/simple.html");
  NavigateTab(0, second_url);
  WaitForURLOnServer(second_url);

  NavigateTabBack(0);
  WaitForURLOnServer(base_url);

  // Check that the navigation chain contains second_url where first_url was
  // before.
  ExpectNavigationChain({base_url, second_url});
}

IN_PROC_BROWSER_TEST_F(SingleClientSessionsSyncTest, OpenNewTab) {
  ASSERT_TRUE(SetupSync());
  ASSERT_TRUE(CheckInitialState(0));

  GURL base_url =
      embedded_test_server()->GetURL("www.host1.com", "/sync/simple.html");
  ASSERT_TRUE(OpenTabAtIndex(0, 0, base_url));

  WaitForURLOnServer(base_url);

  GURL new_tab_url =
      embedded_test_server()->GetURL("www.host2.com", "/sync/simple.html");
  ASSERT_TRUE(OpenTabAtIndex(0, 1, new_tab_url));

  WaitForHierarchyOnServer(
      SessionsHierarchy({{base_url.spec(), new_tab_url.spec()}}));
}

IN_PROC_BROWSER_TEST_F(SingleClientSessionsSyncTest, OpenNewWindow) {
  ASSERT_TRUE(SetupSync());
  ASSERT_TRUE(CheckInitialState(0));

  GURL base_url =
      embedded_test_server()->GetURL("www.host1.com", "/sync/simple.html");
  ASSERT_TRUE(OpenTab(0, base_url));

  WaitForURLOnServer(base_url);

  GURL new_window_url =
      embedded_test_server()->GetURL("www.host2.com", "/sync/simple.html");
  AddBrowser(0);
  ASSERT_TRUE(OpenTab(1, new_window_url));

  WaitForHierarchyOnServer(
      SessionsHierarchy({{base_url.spec()}, {new_window_url.spec()}}));
}

IN_PROC_BROWSER_TEST_F(SingleClientSessionsSyncTest,
                       GarbageCollectionOfForeignSessions) {
  const std::string kForeignSessionTag = "ForeignSessionTag";
  const std::string kForeignClientName = "ForeignClientName";
  const SessionID kWindowId = SessionID::FromSerializedValue(5);
  const SessionID kTabId1 = SessionID::FromSerializedValue(1);
  const SessionID kTabId2 = SessionID::FromSerializedValue(2);
  const base::Time kLastModifiedTime = base::Time::Now() - base::Days(100);

  SessionSyncTestHelper helper;
  sync_pb::SessionSpecifics tab1 =
      helper.BuildTabSpecifics(kForeignSessionTag, kWindowId, kTabId1);
  sync_pb::SessionSpecifics tab2 =
      helper.BuildTabSpecifics(kForeignSessionTag, kWindowId, kTabId2);

  // |tab2| is orphan, i.e. not referenced by the header. We do this to verify
  // that such tabs are also subject to garbage collection.
  sync_pb::SessionSpecifics header =
      SessionSyncTestHelper::BuildHeaderSpecificsWithoutWindows(
          kForeignSessionTag, kForeignClientName);
  SessionSyncTestHelper::AddWindowSpecifics(kWindowId, {kTabId1}, &header);

  for (const sync_pb::SessionSpecifics& specifics : {tab1, tab2, header}) {
    sync_pb::EntitySpecifics entity;
    *entity.mutable_session() = specifics;
    GetFakeServer()->InjectEntity(
        syncer::PersistentUniqueClientEntity::CreateFromSpecificsForTesting(
            /*non_unique_name=*/"",
            sync_sessions::SessionStore::GetClientTag(entity.session()), entity,
            /*creation_time=*/syncer::TimeToProtoTime(kLastModifiedTime),
            /*last_modified_time=*/syncer::TimeToProtoTime(kLastModifiedTime)));
  }

  base::HistogramTester histogram_tester;
  ASSERT_TRUE(SetupSync());

  // Verify that all entities have been deleted.
  WaitForHierarchyOnServer(SessionsHierarchy());

  std::vector<sync_pb::SyncEntity> entities =
      fake_server_->GetSyncEntitiesByDataType(syncer::SESSIONS);
  for (const sync_pb::SyncEntity& entity : entities) {
    EXPECT_NE(kForeignSessionTag, entity.specifics().session().session_tag());
  }

  EXPECT_EQ(3, histogram_tester.GetBucketCount(
                   "Sync.DataTypeEntityChange.SESSION",
                   syncer::DataTypeEntityChange::kLocalDeletion));
}

IN_PROC_BROWSER_TEST_F(SingleClientSessionsSyncTest,
                       GarbageCollectionOfForeignOrphanTabWithoutHeader) {
  const std::string kForeignSessionTag = "ForeignSessionTag";
  const SessionID kWindowId = SessionID::FromSerializedValue(5);
  const SessionID kTabId1 = SessionID::FromSerializedValue(1);
  const SessionID kTabId2 = SessionID::FromSerializedValue(2);
  const base::Time kLastModifiedTime = base::Time::Now() - base::Days(100);

  SessionSyncTestHelper helper;

  // There are two orphan tab entities without a header entity.
  sync_pb::EntitySpecifics tab1;
  *tab1.mutable_session() =
      helper.BuildTabSpecifics(kForeignSessionTag, kWindowId, kTabId1);

  sync_pb::EntitySpecifics tab2;
  *tab2.mutable_session() =
      helper.BuildTabSpecifics(kForeignSessionTag, kWindowId, kTabId2);

  for (const sync_pb::EntitySpecifics& specifics : {tab1, tab2}) {
    GetFakeServer()->InjectEntity(
        syncer::PersistentUniqueClientEntity::CreateFromSpecificsForTesting(
            /*non_unique_name=*/"",
            sync_sessions::SessionStore::GetClientTag(specifics.session()),
            specifics,
            /*creation_time=*/syncer::TimeToProtoTime(kLastModifiedTime),
            /*last_modified_time=*/syncer::TimeToProtoTime(kLastModifiedTime)));
  }

  base::HistogramTester histogram_tester;
  ASSERT_TRUE(SetupSync());

  // Verify that all entities have been deleted.
  WaitForHierarchyOnServer(SessionsHierarchy());

  std::vector<sync_pb::SyncEntity> entities =
      fake_server_->GetSyncEntitiesByDataType(syncer::SESSIONS);
  for (const sync_pb::SyncEntity& entity : entities) {
    EXPECT_NE(kForeignSessionTag, entity.specifics().session().session_tag());
  }

  EXPECT_EQ(2, histogram_tester.GetBucketCount(
                   "Sync.DataTypeEntityChange.SESSION",
                   syncer::DataTypeEntityChange::kLocalDeletion));
}

// Regression test for crbug.com/915133 that verifies the browser doesn't crash
// if the server sends corrupt data during initial merge.
IN_PROC_BROWSER_TEST_F(SingleClientSessionsSyncTest, CorruptInitialForeignTab) {
  // Tabs with a negative node ID should be ignored.
  sync_pb::EntitySpecifics specifics;
  specifics.mutable_session()->mutable_tab();
  specifics.mutable_session()->set_tab_node_id(-1);

  GetFakeServer()->InjectEntity(
      syncer::PersistentUniqueClientEntity::CreateFromSpecificsForTesting(
          "somename", "someclienttag", specifics,
          /*creation_time=*/0,
          /*last_modified_time=*/0));

  ASSERT_TRUE(SetupSync());

  // Foreign data should be empty.
  SyncedSessionVector sessions;
  ASSERT_FALSE(GetSessionData(0, &sessions));
  EXPECT_EQ(0U, sessions.size());
}

// Regression test for crbug.com/915133 that verifies the browser doesn't crash
// if the server sends corrupt data as incremental update.
IN_PROC_BROWSER_TEST_F(SingleClientSessionsSyncTest, CorruptForeignTabUpdate) {
  ASSERT_TRUE(SetupSync());

  // Tabs with a negative node ID should be ignored.
  sync_pb::EntitySpecifics specifics;
  specifics.mutable_session()->mutable_tab();
  specifics.mutable_session()->set_tab_node_id(-1);

  GetFakeServer()->InjectEntity(
      syncer::PersistentUniqueClientEntity::CreateFromSpecificsForTesting(
          "somename", "someclienttag", specifics,
          /*creation_time=*/0,
          /*last_modified_time=*/0));

  // Mimic a browser restart by forcing a refresh to get updates.
  GetSyncService(0)->TriggerRefresh({syncer::SESSIONS});
  EXPECT_TRUE(UpdatedProgressMarkerChecker(GetSyncService(0)).Wait());

  // Foreign data should be empty.
  SyncedSessionVector sessions;
  ASSERT_FALSE(GetSessionData(0, &sessions));
  EXPECT_EQ(0U, sessions.size());
}

IN_PROC_BROWSER_TEST_F(SingleClientSessionsSyncTest, TabMovedToOtherWindow) {
  ASSERT_TRUE(SetupSync());
  ASSERT_TRUE(CheckInitialState(0));

  GURL base_url =
      embedded_test_server()->GetURL("www.base.com", "/sync/simple.html");
  GURL moved_tab_url =
      embedded_test_server()->GetURL("www.host1.com", "/sync/simple.html");

  ASSERT_TRUE(OpenTab(0, base_url));
  ASSERT_TRUE(OpenTabAtIndex(0, 1, moved_tab_url));

  GURL new_window_url =
      embedded_test_server()->GetURL("www.host2.com", "/sync/simple.html");
  AddBrowser(0);
  ASSERT_TRUE(OpenTab(1, new_window_url));

  WaitForHierarchyOnServer(SessionsHierarchy(
      {{base_url.spec(), moved_tab_url.spec()}, {new_window_url.spec()}}));

  // Move tab 1 in browser 0 to browser 1.
  MoveTab(0, 1, 1);

  WaitForHierarchyOnServer(SessionsHierarchy(
      {{base_url.spec()}, {new_window_url.spec(), moved_tab_url.spec()}}));
}

IN_PROC_BROWSER_TEST_F(SingleClientSessionsSyncTest, CookieJarMismatch) {
  ASSERT_TRUE(SetupSync());

  ASSERT_TRUE(CheckInitialState(0));

  // Simulate empty list of accounts in the cookie jar.
  UpdateCookieJarAccountsAndWait({},
                                 /*expected_cookie_jar_mismatch=*/true);

  // Add a new session to client 0 and wait for it to sync.
  GURL url1 =
      embedded_test_server()->GetURL("www.host1.com", "/sync/simple.html");
  ASSERT_TRUE(OpenTab(0, url1));
  WaitForURLOnServer(url1);

  // Verify the cookie jar mismatch bool is set to true.
  sync_pb::ClientToServerMessage first_commit;
  ASSERT_TRUE(GetFakeServer()->GetLastCommitMessage(&first_commit));
  EXPECT_TRUE(first_commit.commit().config_params().cookie_jar_mismatch())
      << syncer::ClientToServerMessageToValue(first_commit, /*options=*/{});

  // Avoid interferences from actual IdentityManager trying to fetch gaia
  // account information, which would exercise
  // SyncServiceImpl::OnAccountsInCookieUpdated().
  signin::CancelAllOngoingGaiaCookieOperations(
      IdentityManagerFactory::GetForProfile(GetProfile(0)));

  // Trigger a cookie jar change (user signing in to content area).
  // Updating the cookie jar has to travel to the sync engine. It is possible
  // something is already running or scheduled to run on the sync thread. We
  // want to block here until we know the cookie jar stats have been updated.
  UpdateCookieJarAccountsAndWait({GetClient(0)->service()->GetAccountInfo()},
                                 /*expected_cookie_jar_mismatch=*/false);

  // Trigger a sync and wait for it.
  GURL url2 =
      embedded_test_server()->GetURL("www.host2.com", "/sync/simple.html");
  NavigateTab(0, url2);
  WaitForURLOnServer(url2);

  // Verify the cookie jar mismatch bool is set to false.
  sync_pb::ClientToServerMessage second_commit;
  ASSERT_TRUE(GetFakeServer()->GetLastCommitMessage(&second_commit));
  EXPECT_FALSE(second_commit.commit().config_params().cookie_jar_mismatch())
      << syncer::ClientToServerMessageToValue(second_commit, /*options=*/{});
}

IN_PROC_BROWSER_TEST_F(SingleClientSessionsSyncTest,
                       ShouldNotifyLoadedIconUrl) {
  ASSERT_TRUE(SetupSync());
  ASSERT_TRUE(CheckInitialState(0));

  // Url with endoded 1 pixel icon.
  std::string icon_url =
      "data:image/png;base64,"
      "R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7";
  std::string page_url =
      "data:text/html,<html><title>TestWithFavicon</title><link rel=icon "
      "href=" +
      icon_url + " /></html>";

  ASSERT_TRUE(OpenTab(0, GURL(page_url)));

  IsIconURLSyncedChecker checker(page_url, icon_url, GetFakeServer(),
                                 GetSyncService(0));
  EXPECT_TRUE(checker.Wait());
}

class SingleClientSessionsSyncTestWithFaviconTestServer
    : public SingleClientSessionsSyncTest {
 public:
  SingleClientSessionsSyncTestWithFaviconTestServer() = default;

  SingleClientSessionsSyncTestWithFaviconTestServer(
      const SingleClientSessionsSyncTestWithFaviconTestServer&) = delete;
  SingleClientSessionsSyncTestWithFaviconTestServer& operator=(
      const SingleClientSessionsSyncTestWithFaviconTestServer&) = delete;

  ~SingleClientSessionsSyncTestWithFaviconTestServer() override = default;

 protected:
  void SetUpOnMainThread() override {
    // Mock favicon server response.
    embedded_test_server()->RegisterRequestHandler(
        base::BindRepeating(&FaviconServerRequestHandler));
    SingleClientSessionsSyncTest::SetUpOnMainThread();
  }
};

IN_PROC_BROWSER_TEST_F(SingleClientSessionsSyncTestWithFaviconTestServer,
                       ShouldDeleteOnDemandIconsOnSessionsDisabled) {
  const std::string kForeignSessionTag = "ForeignSessionTag";
  const std::string kForeignClientName = "ForeignClientName";
  const SessionID kWindowId = SessionID::FromSerializedValue(5);
  const SessionID kTabId = SessionID::FromSerializedValue(1);
  const base::Time kLastModifiedTime = base::Time::Now();

  // Inject fake data on the server.
  SessionSyncTestHelper helper;
  sync_pb::SessionSpecifics tab =
      helper.BuildTabSpecifics(kForeignSessionTag, kWindowId, kTabId);
  sync_pb::SessionSpecifics header =
      SessionSyncTestHelper::BuildHeaderSpecificsWithoutWindows(
          kForeignSessionTag, kForeignClientName);
  SessionSyncTestHelper::AddWindowSpecifics(kWindowId, {kTabId}, &header);
  for (const sync_pb::SessionSpecifics& specifics : {tab, header}) {
    sync_pb::EntitySpecifics entity;
    *entity.mutable_session() = specifics;
    GetFakeServer()->InjectEntity(
        syncer::PersistentUniqueClientEntity::CreateFromSpecificsForTesting(
            "somename",
            sync_sessions::SessionStore::GetClientTag(entity.session()), entity,
            /*creation_time=*/syncer::TimeToProtoTime(kLastModifiedTime),
            /*last_modified_time=*/syncer::TimeToProtoTime(kLastModifiedTime)));
  }

  ASSERT_TRUE(SetupClients());

  // Override large icon service to talk to the mock server.
  favicon::LargeIconServiceImpl* large_icon_service =
      static_cast<favicon::LargeIconServiceImpl*>(
          LargeIconServiceFactory::GetForBrowserContext(GetProfile(0)));
  large_icon_service->SetServerUrlForTesting(
      embedded_test_server()->GetURL("/"));

  ASSERT_TRUE(SetupSync());

  // Expect injected foreign sessions to be synced down.
  SyncedSessionVector sessions;
  ASSERT_TRUE(GetSessionData(0, &sessions));
  ASSERT_EQ(1U, sessions.size());

  // Force creation of RecentTabsSubMenuModel which as a result fetches the
  // favicon for the injected fake recent tab.
  chrome::ShowAppMenu(GetBrowser(0));

  EXPECT_TRUE(FaviconForPageUrlAvailableChecker(GetProfile(0),
                                                GURL("http://foo/1"),
                                                /*should_be_available=*/true)
                  .Wait());

  // Disable tabs and history toggles.
  ASSERT_TRUE(
      GetClient(0)->DisableSyncForType(syncer::UserSelectableType::kTabs));
  ASSERT_TRUE(
      GetClient(0)->DisableSyncForType(syncer::UserSelectableType::kHistory));

  EXPECT_TRUE(FaviconForPageUrlAvailableChecker(GetProfile(0),
                                                GURL("http://foo/1"),
                                                /*should_be_available=*/false)
                  .Wait());
}

class SingleClientSessionsWithoutDestroyProfileSyncTest
    : public SingleClientSessionsSyncTest {
 public:
  SingleClientSessionsWithoutDestroyProfileSyncTest() {
    features_.InitAndDisableFeature(features::kDestroyProfileOnBrowserClose);
  }

 private:
  base::test::ScopedFeatureList features_;
};

IN_PROC_BROWSER_TEST_F(SingleClientSessionsWithoutDestroyProfileSyncTest,
                       ShouldDeleteLastClosedTab) {
  ASSERT_TRUE(SetupSync());
  ASSERT_TRUE(CheckInitialState(0));

  GURL url1 =
      embedded_test_server()->GetURL("www.host1.com", "/sync/simple.html");
  GURL url2 =
      embedded_test_server()->GetURL("www.host2.com", "/sync/simple.html");

  ASSERT_TRUE(OpenTab(0, url1));
  ASSERT_TRUE(OpenTab(0, url2));
  WaitForHierarchyOnServer(SessionsHierarchy({{url1.spec(), url2.spec()}}));

  CloseTab(/*browser_index=*/0, /*tab_index=*/0);
  WaitForHierarchyOnServer(SessionsHierarchy({{url2.spec()}}));
  CloseTab(/*browser_index=*/0, /*tab_index=*/0);
  WaitForHierarchyOnServer(SessionsHierarchy());
}

#if !BUILDFLAG(IS_CHROMEOS)
class SingleClientSessionsWithDestroyProfileSyncTest
    : public SingleClientSessionsSyncTest {
 public:
  SingleClientSessionsWithDestroyProfileSyncTest() {
    features_.InitAndEnableFeature(features::kDestroyProfileOnBrowserClose);
  }

 private:
  base::test::ScopedFeatureList features_;
};

IN_PROC_BROWSER_TEST_F(SingleClientSessionsWithDestroyProfileSyncTest,
                       ShouldNotDeleteLastClosedTab) {
  ASSERT_TRUE(SetupSync());
  ASSERT_TRUE(CheckInitialState(0));

  GURL url1 =
      embedded_test_server()->GetURL("www.host1.com", "/sync/simple.html");
  GURL url2 =
      embedded_test_server()->GetURL("www.host2.com", "/sync/simple.html");

  ASSERT_TRUE(OpenTab(0, url1));
  ASSERT_TRUE(OpenTab(0, url2));
  WaitForHierarchyOnServer(SessionsHierarchy({{url1.spec(), url2.spec()}}));

  CloseTab(/*browser_index=*/0, /*tab_index=*/0);
  WaitForHierarchyOnServer(SessionsHierarchy({{url2.spec()}}));

  {
    // Closing the last tab results in profile destruction and hence may require
    // running blocking tasks which are normally disallowed during tests.
    // TODO(crbug.com/40846214): remove once it's clear why it results in
    // blocking tasks.
    base::ScopedAllowUnresponsiveTasksForTesting scoped_allow_sync_primitives;
    CloseTab(/*browser_index=*/0, /*tab_index=*/0);
    // TODO(crbug.com/40113507): When DestroyProfileOnBrowserClose is enabled,
    // the last CloseTab() triggers Profile deletion (and SyncService deletion).
    // This means the last tab close never gets synced. We should fix this
    // regression eventually. Once that's done, merge this test with the
    // WithoutDestroyProfile version.
    base::RunLoop run_loop;
    base::SingleThreadTaskRunner::GetCurrentDefault()->PostDelayedTask(
        FROM_HERE, run_loop.QuitClosure(), TestTimeouts::action_timeout());
    run_loop.Run();
  }

  // Even after several seconds, state didn't change on the server.
  fake_server::FakeServerVerifier verifier(GetFakeServer());
  EXPECT_TRUE(verifier.VerifySessions(SessionsHierarchy({{url2.spec()}})));
}
#endif  // !BUILDFLAG(IS_CHROMEOS)

}  // namespace