File: saved_tab_group_sync_bridge_unittest.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 (1856 lines) | stat: -rw-r--r-- 81,819 bytes parent folder | download | duplicates (5)
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
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/saved_tab_groups/internal/saved_tab_group_sync_bridge.h"

#include <algorithm>
#include <cstddef>
#include <memory>
#include <optional>
#include <string>
#include <vector>

#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/memory/raw_ref.h"
#include "base/run_loop.h"
#include "base/scoped_observation.h"
#include "base/test/bind.h"
#include "base/test/protobuf_matchers.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/task_environment.h"
#include "base/test/test_file_util.h"
#include "base/time/time.h"
#include "base/uuid.h"
#include "components/pref_registry/pref_registry_syncable.h"
#include "components/prefs/testing_pref_service.h"
#include "components/saved_tab_groups/internal/saved_tab_group_model.h"
#include "components/saved_tab_groups/internal/saved_tab_group_model_observer.h"
#include "components/saved_tab_groups/internal/sync_bridge_tab_group_model_wrapper.h"
#include "components/saved_tab_groups/proto/saved_tab_group_data.pb.h"
#include "components/saved_tab_groups/public/features.h"
#include "components/saved_tab_groups/public/pref_names.h"
#include "components/saved_tab_groups/public/saved_tab_group.h"
#include "components/saved_tab_groups/public/saved_tab_group_tab.h"
#include "components/saved_tab_groups/public/types.h"
#include "components/saved_tab_groups/public/utils.h"
#include "components/saved_tab_groups/test_support/extended_saved_tab_group_specifics.pb.h"
#include "components/saved_tab_groups/test_support/saved_tab_group_test_utils.h"
#include "components/sync/base/data_type.h"
#include "components/sync/engine/commit_queue.h"
#include "components/sync/model/conflict_resolution.h"
#include "components/sync/model/data_batch.h"
#include "components/sync/model/data_type_store.h"
#include "components/sync/model/data_type_store_service_impl.h"
#include "components/sync/model/entity_change.h"
#include "components/sync/model/metadata_batch.h"
#include "components/sync/model/metadata_change_list.h"
#include "components/sync/model/model_error.h"
#include "components/sync/model/mutable_data_batch.h"
#include "components/sync/protocol/entity_data.h"
#include "components/sync/protocol/entity_specifics.pb.h"
#include "components/sync/protocol/saved_tab_group_specifics.pb.h"
#include "components/sync/test/data_type_store_test_util.h"
#include "components/sync/test/mock_data_type_local_change_processor.h"
#include "components/tab_groups/tab_group_color.h"
#include "components/tab_groups/tab_group_id.h"
#include "components/tab_groups/tab_group_visual_data.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"

using base::test::EqualsProto;
using syncer::ConflictResolution;
using syncer::EntityData;
using testing::_;
using testing::ElementsAre;
using testing::Invoke;
using testing::Not;
using testing::Pointee;
using testing::ReturnRef;

namespace tab_groups {
namespace {

// Returns the extra (unsupported) field from `specifics` which don't have a
// corresponding field in proto.
std::string GetGroupExtraFieldFromSpecifics(
    const sync_pb::SavedTabGroupSpecifics& specifics) {
  sync_pb::test_utils::SavedTabGroupSpecifics extended_specifics;
  bool success =
      extended_specifics.ParseFromString(specifics.SerializeAsString());
  CHECK(success);
  return extended_specifics.group().extra_field_for_testing();
}

MATCHER_P(EntityDataHasGroupUnsupportedFields, extra_field, "") {
  const sync_pb::SavedTabGroupSpecifics& arg_specifics =
      arg.specifics.saved_tab_group();
  return GetGroupExtraFieldFromSpecifics(arg_specifics) == extra_field;
}

// Discard orphaned tabs after 30 days if the associated group cannot be found.
constexpr base::TimeDelta kDiscardOrphanedTabsThreshold = base::Days(30);

// Forwards SavedTabGroupModel's observer notifications to the bridge.
class ModelObserverForwarder : public SavedTabGroupModelObserver {
 public:
  ModelObserverForwarder(SavedTabGroupModel& model,
                         SavedTabGroupSyncBridge& bridge)
      : model_(model), bridge_(bridge) {
    observation_.Observe(&model);
  }

  ~ModelObserverForwarder() override = default;

  // SavedTabGroupModelObserver overrides.
  void SavedTabGroupAddedLocally(const base::Uuid& guid) override {
    bridge_->SavedTabGroupAddedLocally(guid);
  }

  void SavedTabGroupRemovedLocally(
      const SavedTabGroup& removed_group) override {
    bridge_->SavedTabGroupRemovedLocally(removed_group);
  }

  void SavedTabGroupUpdatedLocally(
      const base::Uuid& group_guid,
      const std::optional<base::Uuid>& tab_guid) override {
    bridge_->SavedTabGroupUpdatedLocally(group_guid, tab_guid);
  }

  void SavedTabGroupTabMovedLocally(const base::Uuid& group_guid,
                                    const base::Uuid& tab_guid) override {
    bridge_->SavedTabGroupTabsReorderedLocally(group_guid);
  }

  void SavedTabGroupReorderedLocally() override {
    bridge_->SavedTabGroupReorderedLocally();
  }

  void SavedTabGroupLocalIdChanged(const base::Uuid& group_guid) override {
    bridge_->SavedTabGroupLocalIdChanged(group_guid);
  }

  void SavedTabGroupLastUserInteractionTimeUpdated(
      const base::Uuid& group_guid) override {
    bridge_->SavedTabGroupLastUserInteractionTimeUpdated(group_guid);
  }

 private:
  raw_ref<SavedTabGroupModel> model_;
  raw_ref<SavedTabGroupSyncBridge> bridge_;

  base::ScopedObservation<SavedTabGroupModel, SavedTabGroupModelObserver>
      observation_{this};
};

class MockTabGroupModelObserver : public SavedTabGroupModelObserver {
 public:
  MockTabGroupModelObserver() = default;

  void ObserveModel(SavedTabGroupModel* model) { observation_.Observe(model); }

  MOCK_METHOD(void, SavedTabGroupAddedFromSync, (const base::Uuid&));

 private:
  base::ScopedObservation<SavedTabGroupModel, SavedTabGroupModelObserver>
      observation_{this};
};

// Do not check update times for specifics as adding tabs to a group through the
// bridge will change the update times for the group object.
bool AreGroupSpecificsEqual(const sync_pb::SavedTabGroupSpecifics& sp1,
                            const sync_pb::SavedTabGroupSpecifics& sp2) {
  if (sp1.guid() != sp2.guid()) {
    return false;
  }
  if (sp1.group().title() != sp2.group().title()) {
    return false;
  }
  if (sp1.group().color() != sp2.group().color()) {
    return false;
  }
  if (sp1.group().position() != sp2.group().position()) {
    return false;
  }
  if (sp1.group().pinned_position() != sp2.group().pinned_position()) {
    return false;
  }
  if (sp1.creation_time_windows_epoch_micros() !=
      sp2.creation_time_windows_epoch_micros()) {
    return false;
  }
  return true;
}

bool AreSavedTabGroupsEqual(const SavedTabGroup& group1,
                            const SavedTabGroup& group2) {
  return AreGroupSpecificsEqual(
      SavedTabGroupSyncBridge::SavedTabGroupToSpecificsForTest(group1),
      SavedTabGroupSyncBridge::SavedTabGroupToSpecificsForTest(group2));
}

bool AreTabSpecificsEqual(const sync_pb::SavedTabGroupSpecifics& sp1,
                          const sync_pb::SavedTabGroupSpecifics& sp2) {
  if (sp1.guid() != sp2.guid()) {
    return false;
  }
  if (sp1.tab().url() != sp2.tab().url()) {
    return false;
  }
  if (sp1.tab().title() != sp2.tab().title()) {
    return false;
  }
  if (sp1.tab().group_guid() != sp2.tab().group_guid()) {
    return false;
  }
  if (sp1.creation_time_windows_epoch_micros() !=
      sp2.creation_time_windows_epoch_micros()) {
    return false;
  }
  return true;
}

bool AreSavedTabGroupTabsEqual(const SavedTabGroupTab& tab1,
                               const SavedTabGroupTab& tab2) {
  return AreTabSpecificsEqual(
      SavedTabGroupSyncBridge::SavedTabGroupTabToSpecificsForTest(tab1),
      SavedTabGroupSyncBridge::SavedTabGroupTabToSpecificsForTest(tab2));
}

syncer::EntityData CreateEntityData(sync_pb::SavedTabGroupSpecifics specific) {
  syncer::EntityData entity_data;
  entity_data.name = specific.guid();
  entity_data.specifics.mutable_saved_tab_group()->Swap(&specific);
  return entity_data;
}

std::unique_ptr<syncer::EntityChange> CreateEntityChange(
    sync_pb::SavedTabGroupSpecifics specific,
    syncer::EntityChange::ChangeType change_type) {
  std::string guid = specific.guid();

  switch (change_type) {
    case syncer::EntityChange::ACTION_ADD:
      return syncer::EntityChange::CreateAdd(guid, CreateEntityData(specific));
    case syncer::EntityChange::ACTION_UPDATE:
      return syncer::EntityChange::CreateUpdate(guid,
                                                CreateEntityData(specific));
    case syncer::EntityChange::ACTION_DELETE:
      return syncer::EntityChange::CreateDelete(guid, syncer::EntityData());
  }
}

syncer::EntityChangeList CreateEntityChangeListFromGroup(
    const SavedTabGroup& group,
    syncer::EntityChange::ChangeType change_type) {
  syncer::EntityChangeList entity_change_list;
  entity_change_list.push_back(CreateEntityChange(
      SavedTabGroupSyncBridge::SavedTabGroupToSpecificsForTest(group),
      change_type));

  for (const SavedTabGroupTab& tab : group.saved_tabs()) {
    entity_change_list.push_back(CreateEntityChange(
        SavedTabGroupSyncBridge::SavedTabGroupTabToSpecificsForTest(tab),
        change_type));
  }

  return entity_change_list;
}

sync_pb::SavedTabGroupSpecifics MakeTabGroupSpecificsWithUnknownFields(
    const base::Uuid& group_guid,
    const std::string& title,
    const std::string& extra_field) {
  sync_pb::SavedTabGroupSpecifics specifics;
  specifics.set_guid(group_guid.AsLowercaseString());

  sync_pb::test_utils::SavedTabGroupSpecifics extended_specifics;
  extended_specifics.mutable_group()->set_title(title);
  extended_specifics.mutable_group()->set_extra_field_for_testing(extra_field);
  sync_pb::SavedTabGroupSpecifics specifics_with_unknown_fields;
  bool success = specifics_with_unknown_fields.ParseFromString(
      extended_specifics.SerializeAsString());
  CHECK(success);
  specifics.MergeFrom(specifics_with_unknown_fields);
  return specifics;
}

}  // anonymous namespace

// // Verifies the sync bridge correctly passes/merges data in to the model.
class SavedTabGroupSyncBridgeTest : public ::testing::Test {
 public:
  SavedTabGroupSyncBridgeTest()
      : sync_bridge_model_wrapper_(
            syncer::SAVED_TAB_GROUP,
            &saved_tab_group_model_,
            base::BindOnce(&SavedTabGroupModel::LoadStoredEntries,
                           base::Unretained(&saved_tab_group_model_))),
        store_(syncer::DataTypeStoreTestUtil::CreateInMemoryStoreForTest()) {}
  ~SavedTabGroupSyncBridgeTest() override = default;

  void SetUp() override {
    pref_service_.registry()->RegisterBooleanPref(
        prefs::kSavedTabGroupSpecificsToDataMigration, false);
    ON_CALL(processor_, IsTrackingMetadata())
        .WillByDefault(testing::Return(true));
    ON_CALL(processor_, GetPossiblyTrimmedRemoteSpecifics(_))
        .WillByDefault(ReturnRef(sync_pb::EntitySpecifics::default_instance()));
    bridge_ = std::make_unique<SavedTabGroupSyncBridge>(
        &sync_bridge_model_wrapper_,
        syncer::DataTypeStoreTestUtil::FactoryForForwardingStore(store_.get()),
        processor_.CreateForwardingProcessor(), &pref_service_);
    observer_forwarder_ = std::make_unique<ModelObserverForwarder>(
        saved_tab_group_model_, *bridge_);
    mock_model_observer_.ObserveModel(&saved_tab_group_model_);
    task_environment_.RunUntilIdle();
  }

  void VerifyEntriesCount(size_t expected_count) {
    const syncer::DataTypeStore::RecordList records =
        syncer::DataTypeStoreTestUtil::ReadAllDataAndWait(*store_);

    EXPECT_EQ(expected_count, records.size());
  }

  std::optional<proto::SavedTabGroupData> ReadSavedTabGroupDataFromStore(
      const base::Uuid& guid) {
    const std::string storage_key = guid.AsLowercaseString();
    base::RunLoop run_loop;
    std::optional<std::string> read_value;
    store_->ReadData(
        {storage_key},
        base::BindLambdaForTesting(
            [&run_loop, &read_value](
                const std::optional<syncer::ModelError>& error,
                std::unique_ptr<syncer::DataTypeStore::RecordList> data_records,
                std::unique_ptr<syncer::DataTypeStore::IdList>
                    missing_id_list) {
              if (data_records->size() == 1) {
                read_value = data_records->front().value;
              }
              run_loop.Quit();
            }));
    run_loop.Run();

    proto::SavedTabGroupData data;
    if (!read_value.has_value() || !data.ParseFromString(read_value.value())) {
      return std::nullopt;
    }
    return data;
  }

  SavedTabGroupModel* model() { return &saved_tab_group_model_; }
  SavedTabGroupSyncBridge* bridge() { return bridge_.get(); }
  testing::NiceMock<syncer::MockDataTypeLocalChangeProcessor>&
  mock_processor() {
    return processor_;
  }
  const testing::NiceMock<syncer::MockDataTypeLocalChangeProcessor>&
  mock_processor() const {
    return processor_;
  }

 protected:
  base::test::TaskEnvironment task_environment_;
  SavedTabGroupModel saved_tab_group_model_;
  SyncBridgeTabGroupModelWrapper sync_bridge_model_wrapper_;
  testing::NiceMock<syncer::MockDataTypeLocalChangeProcessor> processor_;
  std::unique_ptr<syncer::DataTypeStore> store_;
  TestingPrefServiceSimple pref_service_;
  std::unique_ptr<SavedTabGroupSyncBridge> bridge_;
  std::unique_ptr<ModelObserverForwarder> observer_forwarder_;
  testing::NiceMock<MockTabGroupModelObserver> mock_model_observer_;
};

// Verify that when we add data into the sync bridge the SavedTabGroupModel will
// reflect those changes.
TEST_F(SavedTabGroupSyncBridgeTest, MergeFullSyncData) {
  EXPECT_TRUE(saved_tab_group_model_.saved_tab_groups().empty());

  SavedTabGroup group(u"Test Title", tab_groups::TabGroupColorId::kBlue, {}, 0);
  SavedTabGroupTab tab_1(GURL("https://website.com"), u"Website Title",
                         group.saved_guid(), /*position=*/std::nullopt);
  SavedTabGroupTab tab_2(GURL("https://google.com"), u"Google",
                         group.saved_guid(), /*position=*/std::nullopt);
  group.AddTabLocally(tab_1).AddTabLocally(tab_2);

  // Note: Here the change type does not matter. The initial merge will add
  // all elements in the change list into the model resolving any conflicts if
  // necessary.
  bridge_->MergeFullSyncData(
      bridge_->CreateMetadataChangeList(),
      CreateEntityChangeListFromGroup(
          group, syncer::EntityChange::ChangeType::ACTION_ADD));

  // Ensure all data passed by the bridge is the same.
  EXPECT_TRUE(saved_tab_group_model_.Contains(group.saved_guid()));
  EXPECT_EQ(saved_tab_group_model_.saved_tab_groups().size(), 1u);

  const SavedTabGroup* group_from_model =
      saved_tab_group_model_.Get(group.saved_guid());

  EXPECT_EQ(group_from_model->saved_tabs().size(), 2u);
  EXPECT_TRUE(AreSavedTabGroupsEqual(group, *group_from_model));

  for (const SavedTabGroupTab& tab : group.saved_tabs()) {
    ASSERT_TRUE(group_from_model->ContainsTab(tab.saved_tab_guid()));
    EXPECT_TRUE(AreSavedTabGroupTabsEqual(
        tab, *group_from_model->GetTab(tab.saved_tab_guid())));
  }
}

TEST_F(SavedTabGroupSyncBridgeTest, ConflictResolutionForTabGroup) {
  ASSERT_TRUE(saved_tab_group_model_.saved_tab_groups().empty());

  SavedTabGroup group(u"Test Title`", tab_groups::TabGroupColorId::kCyan, {},
                      1);
  SavedTabGroupTab tab(GURL("https://website1.com"), u"Website Title1",
                       group.saved_guid(), /*position=*/std::nullopt);
  group.AddTabLocally(tab);
  base::Uuid group_id = group.saved_guid();
  saved_tab_group_model_.AddedLocally(std::move(group));
  ASSERT_EQ(saved_tab_group_model_.saved_tab_groups().size(), 1u);
  const SavedTabGroup* group_from_model = saved_tab_group_model_.Get(group_id);

  sync_pb::SavedTabGroupSpecifics group_specific =
      SavedTabGroupSyncBridge::SavedTabGroupToSpecificsForTest(
          *group_from_model);
  syncer::EntityData remote_data = CreateEntityData(group_specific);
  ASSERT_FALSE(remote_data.is_deleted());

  // Remote is old.
  group_specific.set_update_time_windows_epoch_micros(
      group_specific.update_time_windows_epoch_micros() - 10);
  remote_data = CreateEntityData(group_specific);
  EXPECT_THAT(bridge_->ResolveConflict("storagekey1", remote_data),
              testing::Eq(syncer::ConflictResolution::kUseLocal));

  // Remote is more recent.
  group_specific.set_update_time_windows_epoch_micros(
      group_specific.update_time_windows_epoch_micros() + 10);
  remote_data = CreateEntityData(group_specific);
  EXPECT_THAT(bridge_->ResolveConflict("storagekey1", remote_data),
              testing::Eq(syncer::ConflictResolution::kUseRemote));

  // Remote is deleted.
  syncer::EntityData remote_data2;
  remote_data2.name = group_specific.guid();
  ASSERT_TRUE(remote_data2.is_deleted());
  EXPECT_THAT(bridge_->ResolveConflict("storagekey1", remote_data2),
              testing::Eq(syncer::ConflictResolution::kUseLocal));

  // Local doesn't exist.
  saved_tab_group_model_.RemovedLocally(group_id);
  EXPECT_EQ(saved_tab_group_model_.saved_tab_groups().size(), 0u);
  remote_data = CreateEntityData(group_specific);
  EXPECT_THAT(bridge_->ResolveConflict("storagekey1", remote_data),
              testing::Eq(syncer::ConflictResolution::kUseRemote));
}

TEST_F(SavedTabGroupSyncBridgeTest, ConflictResolutionForTab) {
  ASSERT_TRUE(saved_tab_group_model_.saved_tab_groups().empty());

  SavedTabGroup group(u"Test Title`", tab_groups::TabGroupColorId::kCyan, {},
                      1);
  SavedTabGroupTab tab(GURL("https://website1.com"), u"Website Title1",
                       group.saved_guid(), /*position=*/std::nullopt);
  group.AddTabLocally(tab);
  base::Uuid group_id = group.saved_guid();
  base::Uuid tab_id = tab.saved_tab_guid();
  saved_tab_group_model_.AddedLocally(std::move(group));
  EXPECT_EQ(saved_tab_group_model_.saved_tab_groups().size(), 1u);
  const SavedTabGroup* group_from_model = saved_tab_group_model_.Get(group_id);

  const SavedTabGroupTab* tab_from_model = group_from_model->GetTab(tab_id);
  ASSERT_TRUE(tab_from_model);
  sync_pb::SavedTabGroupSpecifics tab_specific =
      SavedTabGroupSyncBridge::SavedTabGroupTabToSpecificsForTest(
          *tab_from_model);

  // Remote is old.
  tab_specific.set_update_time_windows_epoch_micros(
      tab_specific.update_time_windows_epoch_micros() - 10);
  syncer::EntityData remote_data = CreateEntityData(tab_specific);
  ASSERT_FALSE(remote_data.is_deleted());
  EXPECT_THAT(bridge_->ResolveConflict("storagekey1", remote_data),
              testing::Eq(syncer::ConflictResolution::kUseLocal));

  // Remote is more recent.
  tab_specific.set_update_time_windows_epoch_micros(
      tab_specific.update_time_windows_epoch_micros() + 10);
  remote_data = CreateEntityData(tab_specific);
  EXPECT_THAT(bridge_->ResolveConflict("storagekey1", remote_data),
              testing::Eq(syncer::ConflictResolution::kUseRemote));

  // Remote is deleted.
  syncer::EntityData remote_data2;
  remote_data2.name = tab_specific.guid();
  ASSERT_TRUE(remote_data2.is_deleted());
  EXPECT_THAT(bridge_->ResolveConflict("storagekey1", remote_data2),
              testing::Eq(syncer::ConflictResolution::kUseLocal));

  // Local doesn't exist.
  saved_tab_group_model_.RemovedLocally(group_id);
  EXPECT_EQ(saved_tab_group_model_.saved_tab_groups().size(), 0u);
  remote_data = CreateEntityData(tab_specific);
  EXPECT_THAT(bridge_->ResolveConflict("storagekey1", remote_data),
              testing::Eq(syncer::ConflictResolution::kUseRemote));
}

// Verify merging with preexisting data in the model merges the correct
// elements.
TEST_F(SavedTabGroupSyncBridgeTest, MergeFullSyncDataWithExistingData) {
  // Force the cache guid to return the same value as the groups, reflecting
  // that the processor cache guid is the "creator".
  std::string cache_guid = "cache_guid";
  ON_CALL(processor_, TrackedCacheGuid())
      .WillByDefault(testing::Return(cache_guid));

  SavedTabGroup group(u"Test Title", tab_groups::TabGroupColorId::kBlue, {},
                      /*position=*/std::nullopt, /*saved_guid=*/std::nullopt,
                      /*local_group_guid=*/std::nullopt,
                      /*creator_cache_guid=*/cache_guid);
  SavedTabGroupTab tab_1(GURL("https://website.com"), u"Website Title",
                         group.saved_guid(), /*position=*/std::nullopt);
  SavedTabGroupTab tab_2(GURL("https://google.com"), u"Google",
                         group.saved_guid(), /*position=*/std::nullopt);
  group.AddTabLocally(tab_1).AddTabLocally(tab_2);

  base::Uuid group_guid = group.saved_guid();
  base::Uuid tab_1_guid = tab_1.saved_tab_guid();
  base::Uuid tab_2_guid = tab_2.saved_tab_guid();
  base::Time group_creation_time = group.creation_time();
  base::Time tab_1_creation_time = tab_1.creation_time();
  base::Time tab_2_creation_time = tab_2.creation_time();

  saved_tab_group_model_.AddedLocally(std::move(group));

  const SavedTabGroup* group_from_model =
      saved_tab_group_model_.Get(group_guid);

  // Create an updated version of `group` using the same creation time and 1
  // less tab.
  SavedTabGroup updated_group(
      u"New Title", tab_groups::TabGroupColorId::kPink, {}, /*position=*/0,
      /*saved_guid=*/group_guid,
      /*local_group_guid=*/std::nullopt, cache_guid, cache_guid,
      /*created_before_syncing_tab_groups=*/false, group_creation_time);
  SavedTabGroupTab updated_tab_1(GURL("https://support.google.com"), u"Support",
                                 group_guid, /*position=*/0, tab_1_guid,
                                 std::nullopt, cache_guid, cache_guid,
                                 tab_1_creation_time);
  updated_group.AddTabLocally(updated_tab_1);

  syncer::EntityChangeList entity_change_list = CreateEntityChangeListFromGroup(
      updated_group, syncer::EntityChange::ChangeType::ACTION_UPDATE);

  bridge_->MergeFullSyncData(bridge_->CreateMetadataChangeList(),
                             std::move(entity_change_list));

  // Ensure that tab 1 and 2 are still in the group. Data can only be removed
  // when ApplyIncrementalSyncChanges is called.
  EXPECT_EQ(group_from_model->saved_tabs().size(), 2u);
  EXPECT_TRUE(group_from_model->ContainsTab(tab_1_guid));
  EXPECT_TRUE(group_from_model->ContainsTab(tab_2_guid));

  // Ensure tab_2 was left untouched.
  SavedTabGroupTab tab_2_replica(
      GURL("https://google.com"), u"Google", group_guid, /*position=*/1,
      tab_2_guid, std::nullopt, cache_guid, cache_guid, tab_2_creation_time);
  EXPECT_TRUE(AreSavedTabGroupTabsEqual(tab_2_replica,
                                        *group_from_model->GetTab(tab_2_guid)));

  // Ensure the updated group and tab have been merged into the original group
  // in the model.
  EXPECT_TRUE(AreSavedTabGroupsEqual(*group_from_model, updated_group));
  EXPECT_TRUE(AreSavedTabGroupTabsEqual(updated_tab_1,
                                        *group_from_model->GetTab(tab_1_guid)));
}

// Verify that on sign-out, the groups created before sign-in are locally
// deleted.
TEST_F(SavedTabGroupSyncBridgeTest,
       DisableSyncLocallyRemovesGroupsCreatedBeforeSignIn) {
  EXPECT_TRUE(saved_tab_group_model_.saved_tab_groups().empty());

  // Create two groups: group1 before sign in and group2 after sign in.
  SavedTabGroup group1(u"Test Title1", tab_groups::TabGroupColorId::kBlue, {},
                       0, std::nullopt, std::nullopt, std::nullopt,
                       std::nullopt,
                       /*created_before_syncing_tab_groups=*/true);
  SavedTabGroupTab tab_1(GURL("https://website1.com"), u"Website Title1",
                         group1.saved_guid(), /*position=*/std::nullopt);
  group1.AddTabLocally(tab_1);
  group1.SetCreatorCacheGuid("cache_guid_local");

  SavedTabGroup group2(u"Test Title2", tab_groups::TabGroupColorId::kCyan, {},
                       1, std::nullopt, std::nullopt, std::nullopt,
                       std::nullopt,
                       /*created_before_syncing_tab_groups=*/false);
  SavedTabGroupTab tab_2(GURL("https://website2.com"), u"Website Title2",
                         group2.saved_guid(), /*position=*/std::nullopt);
  group2.AddTabLocally(tab_2);
  group2.SetCreatorCacheGuid("cache_guid_local");

  base::Uuid group_id1 = group1.saved_guid();
  base::Uuid group_id2 = group2.saved_guid();
  saved_tab_group_model_.AddedLocally(std::move(group1));
  saved_tab_group_model_.AddedLocally(std::move(group2));
  VerifyEntriesCount(4u);

  EXPECT_EQ(saved_tab_group_model_.saved_tab_groups().size(), 2u);

  const SavedTabGroup* group1_from_model =
      saved_tab_group_model_.Get(group_id1);
  EXPECT_EQ(group1_from_model->saved_tabs().size(), 1u);

  const SavedTabGroup* group2_from_model =
      saved_tab_group_model_.Get(group_id2);
  EXPECT_EQ(group2_from_model->saved_tabs().size(), 1u);

  EXPECT_TRUE(group1_from_model->created_before_syncing_tab_groups());
  EXPECT_FALSE(group2_from_model->created_before_syncing_tab_groups());
  EXPECT_EQ("cache_guid_local", group1_from_model->creator_cache_guid());
  EXPECT_EQ("cache_guid_local", group2_from_model->creator_cache_guid());

  // Disable sync. Expect group 2 to be removed from model, and group 1 should
  // still be in the model. None of them should be deleted from sync.
  EXPECT_CALL(processor_, Delete(_, _, _)).Times(0);
  bridge_->ApplyDisableSyncChanges(bridge_->CreateMetadataChangeList());
  EXPECT_EQ(saved_tab_group_model_.saved_tab_groups().size(), 1u);
  EXPECT_TRUE(saved_tab_group_model_.Contains(group_id1));
  EXPECT_FALSE(saved_tab_group_model_.Contains(group_id2));

  // DB should have only the two entries from group 1.
  VerifyEntriesCount(2u);

  group1_from_model = saved_tab_group_model_.Get(group_id1);
  group2_from_model = saved_tab_group_model_.Get(group_id2);
  EXPECT_EQ("cache_guid_local", group1_from_model->creator_cache_guid());
}

// Verify orphaned tabs (tabs missing their group) are added into the correct
// group in the model once the group arrives.
TEST_F(SavedTabGroupSyncBridgeTest, OrphanedTabAddedIntoGroupWhenFound) {
  // Merge an orphaned tab. Then merge its missing group. This aims to
  // simulate data spread out over multiple changes.
  base::Uuid orphaned_guid = base::Uuid::GenerateRandomV4();
  SavedTabGroupTab orphaned_tab(GURL("https://mail.google.com"), u"Mail",
                                orphaned_guid, /*position=*/0);

  syncer::EntityChangeList orphaned_tab_change_list;
  orphaned_tab_change_list.push_back(CreateEntityChange(
      SavedTabGroupSyncBridge::SavedTabGroupTabToSpecificsForTest(orphaned_tab),
      syncer::EntityChange::ChangeType::ACTION_ADD));
  bridge_->MergeFullSyncData(bridge_->CreateMetadataChangeList(),
                             std::move(orphaned_tab_change_list));

  EXPECT_FALSE(
      saved_tab_group_model_.Contains(orphaned_tab.saved_group_guid()));
  EXPECT_TRUE(saved_tab_group_model_.saved_tab_groups().empty());

  SavedTabGroup missing_group(u"New Group Title",
                              tab_groups::TabGroupColorId::kOrange, {},
                              /*position=*/0, orphaned_guid);
  syncer::EntityChangeList missing_group_change_list;
  missing_group_change_list.push_back(CreateEntityChange(
      SavedTabGroupSyncBridge::SavedTabGroupToSpecificsForTest(missing_group),
      syncer::EntityChange::ChangeType::ACTION_ADD));
  bridge_->ApplyIncrementalSyncChanges(bridge_->CreateMetadataChangeList(),
                                       std::move(missing_group_change_list));

  EXPECT_TRUE(saved_tab_group_model_.Contains(orphaned_guid));
  EXPECT_EQ(saved_tab_group_model_.saved_tab_groups().size(), 1u);

  const SavedTabGroup* orphaned_group_from_model =
      saved_tab_group_model_.Get(orphaned_guid);

  EXPECT_EQ(orphaned_group_from_model->saved_tabs().size(), 1u);
  EXPECT_TRUE(
      orphaned_group_from_model->ContainsTab(orphaned_tab.saved_tab_guid()));
  EXPECT_TRUE(
      AreSavedTabGroupsEqual(missing_group, *orphaned_group_from_model));
  EXPECT_TRUE(AreSavedTabGroupTabsEqual(
      orphaned_tab,
      *orphaned_group_from_model->GetTab(orphaned_tab.saved_tab_guid())));
}

// Verify orphaned tabs (tabs missing their group) that have not been updated
// for 30 days are discarded and not added into the model.
TEST_F(SavedTabGroupSyncBridgeTest, OprhanedTabDiscardedAfter30Days) {
  // Merge an orphaned tab. Then merge its missing group. This aims to
  // simulate data spread out over multiple changes.
  base::Uuid orphaned_guid = base::Uuid::GenerateRandomV4();
  SavedTabGroupTab orphaned_tab(GURL("https://mail.google.com"), u"Mail",
                                orphaned_guid, /*position=*/0);
  orphaned_tab.SetUpdateTime(base::Time::Now() - kDiscardOrphanedTabsThreshold);

  syncer::EntityChangeList orphaned_tab_change_list;
  orphaned_tab_change_list.push_back(CreateEntityChange(
      SavedTabGroupSyncBridge::SavedTabGroupTabToSpecificsForTest(orphaned_tab),
      syncer::EntityChange::ChangeType::ACTION_ADD));
  bridge_->MergeFullSyncData(bridge_->CreateMetadataChangeList(),
                             std::move(orphaned_tab_change_list));

  EXPECT_FALSE(
      saved_tab_group_model_.Contains(orphaned_tab.saved_group_guid()));
  EXPECT_TRUE(saved_tab_group_model_.saved_tab_groups().empty());

  SavedTabGroup missing_group(u"New Group Title",
                              tab_groups::TabGroupColorId::kOrange, {},
                              /*position=*/0, orphaned_guid);
  syncer::EntityChangeList missing_group_change_list;
  missing_group_change_list.push_back(CreateEntityChange(
      SavedTabGroupSyncBridge::SavedTabGroupToSpecificsForTest(missing_group),
      syncer::EntityChange::ChangeType::ACTION_ADD));
  bridge_->ApplyIncrementalSyncChanges(bridge_->CreateMetadataChangeList(),
                                       std::move(missing_group_change_list));

  EXPECT_TRUE(saved_tab_group_model_.Contains(orphaned_guid));
  EXPECT_EQ(saved_tab_group_model_.saved_tab_groups().size(), 1u);

  const SavedTabGroup* orphaned_group_from_model =
      saved_tab_group_model_.Get(orphaned_guid);
  EXPECT_TRUE(orphaned_group_from_model->saved_tabs().empty());
  EXPECT_FALSE(
      orphaned_group_from_model->ContainsTab(orphaned_tab.saved_tab_guid()));
}

// Verify orphaned tabs (tabs missing their group) that have not been updated
// for 30 days and have a group are not discarded.
TEST_F(SavedTabGroupSyncBridgeTest, OprhanedTabGroupFoundAfter30Days) {
  // Merge an orphaned tab. Then merge its missing group. This aims to
  // simulate data spread out over multiple changes.
  base::Uuid orphaned_guid = base::Uuid::GenerateRandomV4();

  SavedTabGroup missing_group(u"New Group Title",
                              tab_groups::TabGroupColorId::kOrange, {},
                              /*position=*/0, orphaned_guid);
  syncer::EntityChangeList missing_group_change_list;
  missing_group_change_list.push_back(CreateEntityChange(
      SavedTabGroupSyncBridge::SavedTabGroupToSpecificsForTest(missing_group),
      syncer::EntityChange::ChangeType::ACTION_ADD));
  bridge_->MergeFullSyncData(bridge_->CreateMetadataChangeList(),
                             std::move(missing_group_change_list));

  EXPECT_TRUE(saved_tab_group_model_.Contains(orphaned_guid));
  EXPECT_EQ(saved_tab_group_model_.saved_tab_groups().size(), 1u);

  SavedTabGroupTab orphaned_tab(GURL("https://mail.google.com"), u"Mail",
                                orphaned_guid, /*position=*/0);
  orphaned_tab.SetUpdateTime(base::Time::Now() - kDiscardOrphanedTabsThreshold);
  syncer::EntityChangeList orphaned_tab_change_list;
  orphaned_tab_change_list.push_back(CreateEntityChange(
      SavedTabGroupSyncBridge::SavedTabGroupTabToSpecificsForTest(orphaned_tab),
      syncer::EntityChange::ChangeType::ACTION_ADD));

  bridge_->ApplyIncrementalSyncChanges(bridge_->CreateMetadataChangeList(),
                                       std::move(orphaned_tab_change_list));

  EXPECT_TRUE(saved_tab_group_model_.Contains(orphaned_guid));
  EXPECT_EQ(saved_tab_group_model_.saved_tab_groups().size(), 1u);

  const SavedTabGroup* orphaned_group_from_model =
      saved_tab_group_model_.Get(orphaned_guid);
  EXPECT_EQ(orphaned_group_from_model->saved_tabs().size(), 1u);
  EXPECT_TRUE(
      orphaned_group_from_model->ContainsTab(orphaned_tab.saved_tab_guid()));
  EXPECT_TRUE(
      AreSavedTabGroupsEqual(missing_group, *orphaned_group_from_model));
  EXPECT_TRUE(AreSavedTabGroupTabsEqual(
      orphaned_tab,
      *orphaned_group_from_model->GetTab(orphaned_tab.saved_tab_guid())));
}

// Verify that when we add data into the sync bridge the SavedTabGroupModel
// will reflect those changes.
TEST_F(SavedTabGroupSyncBridgeTest, AddSyncData) {
  syncer::EntityChangeList empty_change_list;
  bridge_->MergeFullSyncData(bridge_->CreateMetadataChangeList(),
                             std::move(empty_change_list));

  SavedTabGroup group(u"Test Title", tab_groups::TabGroupColorId::kBlue, {},
                      /*position=*/0);
  SavedTabGroupTab tab_1(GURL("https://website.com"), u"Website Title",
                         group.saved_guid(), /*position=*/std::nullopt);
  SavedTabGroupTab tab_2(GURL("https://google.com"), u"Google",
                         group.saved_guid(), /*position=*/std::nullopt);
  group.SetCreatorCacheGuid("remote_cache_guid");
  group.AddTabLocally(tab_1).AddTabLocally(tab_2);
  group.SetCreatorCacheGuid("remote_cache_guid");

  bridge_->ApplyIncrementalSyncChanges(
      bridge_->CreateMetadataChangeList(),
      CreateEntityChangeListFromGroup(
          group, syncer::EntityChange::ChangeType::ACTION_ADD));

  // Ensure all data passed by the bridge is the same.
  ASSERT_TRUE(saved_tab_group_model_.Contains(group.saved_guid()));
  const SavedTabGroup* group_from_model =
      saved_tab_group_model_.Get(group.saved_guid());

  EXPECT_TRUE(AreSavedTabGroupsEqual(group, *group_from_model));

  for (const SavedTabGroupTab& tab : group.saved_tabs()) {
    ASSERT_TRUE(group_from_model->ContainsTab(tab.saved_tab_guid()));
    EXPECT_TRUE(AreSavedTabGroupTabsEqual(
        tab, *group_from_model->GetTab(tab.saved_tab_guid())));
  }

  // Ensure a tab added to an existing group in the bridge is added into the
  // model correctly.
  SavedTabGroupTab additional_tab(GURL("https://maps.google.com"), u"Maps",
                                  group.saved_guid(), /*position=*/2);

  // Orphaned tabs are tabs that do not have a respective group stored in the
  // model. As such, these tabs are kept in local storage but not the model.
  SavedTabGroupTab orphaned_tab(GURL("https://mail.google.com"), u"Mail",
                                base::Uuid::GenerateRandomV4(), /*position=*/0);

  syncer::EntityChangeList entity_change_list;
  entity_change_list.push_back(CreateEntityChange(
      SavedTabGroupSyncBridge::SavedTabGroupTabToSpecificsForTest(
          additional_tab),
      syncer::EntityChange::ChangeType::ACTION_ADD));
  entity_change_list.push_back((CreateEntityChange(
      SavedTabGroupSyncBridge::SavedTabGroupTabToSpecificsForTest(orphaned_tab),
      syncer::EntityChange::ChangeType::ACTION_ADD)));

  bridge_->ApplyIncrementalSyncChanges(bridge_->CreateMetadataChangeList(),
                                       std::move(entity_change_list));

  ASSERT_TRUE(group_from_model->ContainsTab(additional_tab.saved_tab_guid()));
  EXPECT_EQ(group_from_model->saved_tabs().size(), 3u);
  for (const SavedTabGroupTab& tab : group.saved_tabs()) {
    ASSERT_TRUE(group_from_model->ContainsTab(tab.saved_tab_guid()));
    EXPECT_TRUE(AreSavedTabGroupTabsEqual(
        tab, *group_from_model->GetTab(tab.saved_tab_guid())));
  }
}

// Verify that ACTION_UPDATE performs the same as ACTION_ADD initially and that
// the model reflects the updated group data after subsequent calls.
TEST_F(SavedTabGroupSyncBridgeTest, UpdateSyncData) {
  syncer::EntityChangeList empty_change_list;
  bridge_->MergeFullSyncData(bridge_->CreateMetadataChangeList(),
                             std::move(empty_change_list));

  SavedTabGroup group(u"Test Title", tab_groups::TabGroupColorId::kBlue, {},
                      /*position=*/std::nullopt);
  SavedTabGroupTab tab_1(GURL("https://website.com"), u"Title",
                         group.saved_guid(), /*position=*/std::nullopt);
  SavedTabGroupTab tab_2(GURL("https://google.com"), u"Google",
                         group.saved_guid(), /*position=*/std::nullopt);
  group.AddTabLocally(tab_1).AddTabLocally(tab_2);
  group.SetPosition(0);

  bridge_->ApplyIncrementalSyncChanges(
      bridge_->CreateMetadataChangeList(),
      CreateEntityChangeListFromGroup(
          group, syncer::EntityChange::ChangeType::ACTION_ADD));

  const SavedTabGroup* group_from_model =
      saved_tab_group_model_.Get(group.saved_guid());

  group.SetTitle(u"A new title");
  group.SetColor(tab_groups::TabGroupColorId::kRed);
  group.saved_tabs()[0].SetURL(GURL("https://youtube.com"));

  bridge_->ApplyIncrementalSyncChanges(
      bridge_->CreateMetadataChangeList(),
      CreateEntityChangeListFromGroup(
          group, syncer::EntityChange::ChangeType::ACTION_UPDATE));

  EXPECT_TRUE(AreSavedTabGroupsEqual(group, *group_from_model));

  for (const SavedTabGroupTab& tab : group.saved_tabs()) {
    ASSERT_TRUE(group_from_model->ContainsTab(tab.saved_tab_guid()));
    EXPECT_TRUE(AreSavedTabGroupTabsEqual(
        tab, *group_from_model->GetTab(tab.saved_tab_guid())));
  }
}

// Verify that the correct elements are removed when ACTION_DELETE is called.
TEST_F(SavedTabGroupSyncBridgeTest, DeleteSyncData) {
  syncer::EntityChangeList empty_change_list;
  bridge_->MergeFullSyncData(bridge_->CreateMetadataChangeList(),
                             std::move(empty_change_list));

  SavedTabGroup group(u"Test Title", tab_groups::TabGroupColorId::kBlue, {},
                      /*position=*/0);
  SavedTabGroupTab tab_1(GURL("https://website.com"), u"Website Title",
                         group.saved_guid(), /*position=*/std::nullopt);
  SavedTabGroupTab tab_2(GURL("https://google.com"), u"Google",
                         group.saved_guid(), /*position=*/std::nullopt);
  group.AddTabLocally(tab_1).AddTabLocally(tab_2);

  EXPECT_EQ(group.saved_tabs().size(), 2u);

  bridge_->ApplyIncrementalSyncChanges(
      bridge_->CreateMetadataChangeList(),
      CreateEntityChangeListFromGroup(
          group, syncer::EntityChange::ChangeType::ACTION_ADD));

  ASSERT_TRUE(saved_tab_group_model_.Contains(group.saved_guid()));
  const SavedTabGroup* group_from_model =
      saved_tab_group_model_.Get(group.saved_guid());

  // Ensure a deleted tab is deleted from the group correctly in the model.
  base::Uuid tab_to_remove = group.saved_tabs()[0].saved_tab_guid();

  syncer::EntityChangeList delete_tab_change_list;
  delete_tab_change_list.push_back(CreateEntityChange(
      SavedTabGroupSyncBridge::SavedTabGroupTabToSpecificsForTest(
          group.saved_tabs()[0]),
      syncer::EntityChange::ChangeType::ACTION_DELETE));
  bridge_->ApplyIncrementalSyncChanges(bridge_->CreateMetadataChangeList(),
                                       std::move(delete_tab_change_list));

  EXPECT_EQ(group_from_model->saved_tabs().size(), 1u);
  EXPECT_TRUE(AreSavedTabGroupTabsEqual(group.saved_tabs()[1],
                                        group_from_model->saved_tabs()[0]));
  EXPECT_FALSE(group_from_model->ContainsTab(tab_to_remove));

  // Ensure deleting a group deletes all the tabs in the group as well.
  syncer::EntityChangeList delete_group_change_list;

  delete_group_change_list.push_back(CreateEntityChange(
      SavedTabGroupSyncBridge::SavedTabGroupToSpecificsForTest(group),
      syncer::EntityChange::ChangeType::ACTION_DELETE));
  bridge_->ApplyIncrementalSyncChanges(bridge_->CreateMetadataChangeList(),
                                       std::move(delete_group_change_list));

  EXPECT_EQ(saved_tab_group_model_.saved_tab_groups().size(), 0u);
  EXPECT_FALSE(saved_tab_group_model_.Contains(group.saved_guid()));
}

// Verify that the deleted elements are processed last. We process deleted
// elements last for consistency since the ordering of messages is not
// guaranteed.
TEST_F(SavedTabGroupSyncBridgeTest, DeleteSyncDataProcessedLast) {
  syncer::EntityChangeList empty_change_list;
  bridge_->MergeFullSyncData(bridge_->CreateMetadataChangeList(),
                             std::move(empty_change_list));

  SavedTabGroup group(u"Test Title", tab_groups::TabGroupColorId::kBlue, {},
                      /*position=*/0);
  SavedTabGroupTab tab_1(GURL("https://website.com"), u"Website Title",
                         group.saved_guid(), /*position=*/std::nullopt);
  SavedTabGroupTab tab_2(GURL("https://google.com"), u"Google",
                         group.saved_guid(), /*position=*/std::nullopt);
  SavedTabGroupTab tab_3(GURL("https://youtube.com"), u"Youtube",
                         group.saved_guid(), /*position=*/2);
  group.AddTabLocally(tab_1).AddTabLocally(tab_2);
  EXPECT_EQ(group.saved_tabs().size(), 2u);

  bridge_->ApplyIncrementalSyncChanges(
      bridge_->CreateMetadataChangeList(),
      CreateEntityChangeListFromGroup(
          group, syncer::EntityChange::ChangeType::ACTION_ADD));

  ASSERT_TRUE(saved_tab_group_model_.Contains(group.saved_guid()));
  const SavedTabGroup* group_from_model =
      saved_tab_group_model_.Get(group.saved_guid());

  // Ensure the deleted tabs are removed from the group correctly.
  base::Uuid removed_tab_1 = group.saved_tabs()[0].saved_tab_guid();
  base::Uuid removed_tab_2 = group.saved_tabs()[1].saved_tab_guid();

  // Remove both tabs in the group first, then add the new tab.
  syncer::EntityChangeList change_list;
  change_list.push_back(CreateEntityChange(
      SavedTabGroupSyncBridge::SavedTabGroupTabToSpecificsForTest(
          group.saved_tabs()[0]),
      syncer::EntityChange::ChangeType::ACTION_DELETE));
  change_list.push_back(CreateEntityChange(
      SavedTabGroupSyncBridge::SavedTabGroupTabToSpecificsForTest(
          group.saved_tabs()[1]),
      syncer::EntityChange::ChangeType::ACTION_DELETE));
  change_list.push_back(CreateEntityChange(
      SavedTabGroupSyncBridge::SavedTabGroupTabToSpecificsForTest(tab_3),
      syncer::EntityChange::ChangeType::ACTION_ADD));
  bridge_->ApplyIncrementalSyncChanges(bridge_->CreateMetadataChangeList(),
                                       std::move(change_list));

  // The group should still exist with only `tab_3`.
  ASSERT_TRUE(saved_tab_group_model_.Contains(group.saved_guid()));
  EXPECT_EQ(group_from_model->saved_tabs().size(), 1u);
  EXPECT_TRUE(
      AreSavedTabGroupTabsEqual(tab_3, group_from_model->saved_tabs()[0]));

  EXPECT_FALSE(group_from_model->ContainsTab(removed_tab_1));
  EXPECT_FALSE(group_from_model->ContainsTab(removed_tab_2));
  EXPECT_TRUE(group_from_model->ContainsTab(tab_3.saved_tab_guid()));
}

// Verify that locally added groups call add all group data to the processor.
TEST_F(SavedTabGroupSyncBridgeTest, AddGroupLocally) {
  EXPECT_TRUE(saved_tab_group_model_.saved_tab_groups().empty());

  SavedTabGroup group(u"Test Title", tab_groups::TabGroupColorId::kBlue, {},
                      /*position=*/std::nullopt);
  SavedTabGroupTab tab_1(GURL("https://website.com"), u"Website Title",
                         group.saved_guid(), /*position=*/std::nullopt);
  SavedTabGroupTab tab_2(GURL("https://google.com"), u"Google",
                         group.saved_guid(), /*position=*/std::nullopt);
  group.AddTabLocally(tab_1).AddTabLocally(tab_2);

  base::Uuid group_guid = group.saved_guid();
  base::Uuid tab_1_guid = tab_1.saved_tab_guid();
  base::Uuid tab_2_guid = tab_2.saved_tab_guid();

  EXPECT_CALL(processor_, Put(tab_1_guid.AsLowercaseString(), _, _));
  EXPECT_CALL(processor_, Put(tab_2_guid.AsLowercaseString(), _, _));
  EXPECT_CALL(processor_, Put(group_guid.AsLowercaseString(), _, _));

  saved_tab_group_model_.AddedLocally(std::move(group));
}

// Verify that local ID change events aren't passed to the processor.
TEST_F(SavedTabGroupSyncBridgeTest, LocalIdChanged) {
  EXPECT_TRUE(saved_tab_group_model_.saved_tab_groups().empty());

  SavedTabGroup group(u"Test Title", tab_groups::TabGroupColorId::kBlue, {},
                      /*position=*/std::nullopt);
  SavedTabGroupTab tab_1(GURL("https://website.com"), u"Website Title",
                         group.saved_guid(), /*position=*/std::nullopt);
  group.AddTabLocally(tab_1);
  saved_tab_group_model_.AddedLocally(group);

  // Local ID change events on tabs or groups shouldn't propagate to the
  // processor.
  EXPECT_CALL(processor_, Put(_, _, _)).Times(0);
  // Open the group.
  group.SetLocalGroupId(test::GenerateRandomTabGroupID());
  tab_1.SetLocalTabID(test::GenerateRandomTabID());

  // Close the group.
  group.SetLocalGroupId(std::nullopt);
  tab_1.SetLocalTabID(std::nullopt);
}

// Verify that locally removed groups removes the group from the processor
// and leaves the tabs without an associated group.
TEST_F(SavedTabGroupSyncBridgeTest, RemoveGroupLocally) {
  EXPECT_TRUE(saved_tab_group_model_.saved_tab_groups().empty());

  SavedTabGroup group(u"Test Title", tab_groups::TabGroupColorId::kBlue, {},
                      /*position=*/std::nullopt);
  SavedTabGroupTab tab_1(GURL("https://website.com"), u"Website Title",
                         group.saved_guid(), /*position=*/std::nullopt);
  SavedTabGroupTab tab_2(GURL("https://google.com"), u"Google",
                         group.saved_guid(), /*position=*/std::nullopt);
  group.AddTabLocally(tab_1).AddTabLocally(tab_2);

  base::Uuid group_guid = group.saved_guid();
  base::Uuid tab_1_guid = tab_1.saved_tab_guid();
  base::Uuid tab_2_guid = tab_2.saved_tab_guid();
  saved_tab_group_model_.AddedLocally(std::move(group));

  EXPECT_CALL(processor_, Delete(group_guid.AsLowercaseString(), _, _));
  EXPECT_CALL(processor_, Delete(tab_1_guid.AsLowercaseString(), _, _))
      .Times(0);
  EXPECT_CALL(processor_, Delete(tab_2_guid.AsLowercaseString(), _, _))
      .Times(0);

  saved_tab_group_model_.RemovedLocally(group_guid);

  // Verify that the orphaned tabs are still stored locally in the sync bridge.
  const std::vector<proto::SavedTabGroupData>& tabs_missing_groups =
      bridge_->GetTabsMissingGroupsForTesting();

  auto it_1 = std::ranges::find_if(
      tabs_missing_groups, [&](proto::SavedTabGroupData data) {
        return data.specifics().guid() == tab_1_guid.AsLowercaseString();
      });
  auto it_2 = std::ranges::find_if(
      tabs_missing_groups, [&](proto::SavedTabGroupData data) {
        return data.specifics().guid() == tab_2_guid.AsLowercaseString();
      });

  EXPECT_TRUE(it_1 != tabs_missing_groups.end());
  EXPECT_TRUE(it_2 != tabs_missing_groups.end());
}

// Verify that locally updated groups add all group data to the processor.
TEST_F(SavedTabGroupSyncBridgeTest, UpdateGroupLocally) {
  EXPECT_TRUE(saved_tab_group_model_.saved_tab_groups().empty());

  SavedTabGroup group(u"Test Title", tab_groups::TabGroupColorId::kBlue, {},
                      /*position=*/std::nullopt,
                      /*saved_guid=*/base::Uuid::GenerateRandomV4(),
                      /*local_group_id=*/test::GenerateRandomTabGroupID());
  SavedTabGroupTab tab_1(GURL("https://website.com"), u"Website Title",
                         group.saved_guid(), /*position=*/std::nullopt);
  SavedTabGroupTab tab_2(GURL("https://google.com"), u"Google",
                         group.saved_guid(), /*position=*/std::nullopt);
  group.AddTabLocally(tab_1).AddTabLocally(tab_2);

  base::Uuid group_guid = group.saved_guid();
  base::Uuid tab_1_guid = tab_1.saved_tab_guid();
  base::Uuid tab_2_guid = tab_2.saved_tab_guid();
  saved_tab_group_model_.AddedLocally(std::move(group));

  EXPECT_CALL(processor_, Put(group_guid.AsLowercaseString(), _, _));
  EXPECT_CALL(processor_, Put(tab_1_guid.AsLowercaseString(), _, _)).Times(0);
  EXPECT_CALL(processor_, Put(tab_2_guid.AsLowercaseString(), _, _)).Times(0);

  tab_groups::TabGroupVisualData visual_data(
      u"New Title", tab_groups::TabGroupColorId::kYellow);
  saved_tab_group_model_.UpdateVisualDataLocally(group.local_group_id().value(),
                                                 &visual_data);
}

// Verify duplicate tab added from sync is merged with the correct tab and not
// added again to the model.
TEST_F(SavedTabGroupSyncBridgeTest, AddTabFromSync) {
  EXPECT_TRUE(saved_tab_group_model_.saved_tab_groups().empty());

  SavedTabGroup group(u"Test Title", tab_groups::TabGroupColorId::kBlue, {},
                      /*position=*/std::nullopt);
  SavedTabGroupTab tab_1(GURL("https://website.com"), u"Website Title",
                         group.saved_guid(), /*position=*/std::nullopt);
  SavedTabGroupTab tab_2(GURL("https://google.com"), u"Google",
                         group.saved_guid(), /*position=*/std::nullopt);
  SavedTabGroupTab tab_3(tab_2);
  tab_3.SetPosition(0);

  group.AddTabLocally(tab_1).AddTabLocally(tab_2);

  base::Uuid group_guid = group.saved_guid();
  base::Uuid tab_1_guid = tab_1.saved_tab_guid();
  base::Uuid tab_2_guid = tab_2.saved_tab_guid();
  base::Uuid tab_3_guid = tab_3.saved_tab_guid();
  saved_tab_group_model_.AddedLocally(std::move(group));
  EXPECT_CALL(processor_, Put(tab_3_guid.AsLowercaseString(), _, _)).Times(0);
  EXPECT_CALL(processor_, Put(tab_1_guid.AsLowercaseString(), _, _)).Times(0);
  EXPECT_CALL(processor_, Put(tab_2_guid.AsLowercaseString(), _, _)).Times(0);
  EXPECT_CALL(processor_, Put(group_guid.AsLowercaseString(), _, _)).Times(0);

  saved_tab_group_model_.AddTabToGroupFromSync(group_guid, tab_3);

  EXPECT_EQ(tab_2_guid, tab_3_guid);
  EXPECT_EQ(
      saved_tab_group_model_.Get(group_guid)->GetTab(tab_2_guid)->position(),
      saved_tab_group_model_.Get(group_guid)->GetTab(tab_3_guid)->position());
}

// Verify that locally added tabs call put on the processor.
TEST_F(SavedTabGroupSyncBridgeTest, AddTabLocally) {
  EXPECT_TRUE(saved_tab_group_model_.saved_tab_groups().empty());

  SavedTabGroup group(u"Test Title", tab_groups::TabGroupColorId::kBlue, {},
                      /*position=*/std::nullopt);
  SavedTabGroupTab tab_1(GURL("https://website.com"), u"Website Title",
                         group.saved_guid(), /*position=*/std::nullopt);
  SavedTabGroupTab tab_2(GURL("https://google.com"), u"Google",
                         group.saved_guid(), /*position=*/std::nullopt);
  SavedTabGroupTab tab_3(GURL("https://youtube.com"), u"Youtube",
                         group.saved_guid(), /*position=*/std::nullopt);
  group.AddTabLocally(tab_1).AddTabLocally(tab_2);

  base::Uuid group_guid = group.saved_guid();
  base::Uuid tab_1_guid = tab_1.saved_tab_guid();
  base::Uuid tab_2_guid = tab_2.saved_tab_guid();
  base::Uuid tab_3_guid = tab_3.saved_tab_guid();
  saved_tab_group_model_.AddedLocally(std::move(group));

  EXPECT_CALL(processor_, Put(tab_3_guid.AsLowercaseString(), _, _));
  EXPECT_CALL(processor_, Put(tab_1_guid.AsLowercaseString(), _, _)).Times(0);
  EXPECT_CALL(processor_, Put(tab_2_guid.AsLowercaseString(), _, _)).Times(0);
  EXPECT_CALL(processor_, Put(group_guid.AsLowercaseString(), _, _)).Times(0);

  saved_tab_group_model_.AddTabToGroupLocally(group_guid, tab_3);
}

// Verify that locally removed tabs remove the correct tabs from the processor.
TEST_F(SavedTabGroupSyncBridgeTest, RemoveTabLocally) {
  EXPECT_TRUE(saved_tab_group_model_.saved_tab_groups().empty());

  SavedTabGroup group(u"Test Title", tab_groups::TabGroupColorId::kBlue, {},
                      /*position=*/std::nullopt);
  SavedTabGroupTab tab_1(GURL("https://website.com"), u"Website Title",
                         group.saved_guid(), /*position=*/std::nullopt);
  SavedTabGroupTab tab_2(GURL("https://google.com"), u"Goole",
                         group.saved_guid(), /*position=*/std::nullopt);
  group.AddTabLocally(tab_1).AddTabLocally(tab_2);

  base::Uuid group_guid = group.saved_guid();
  base::Uuid tab_1_guid = tab_1.saved_tab_guid();
  base::Uuid tab_2_guid = tab_2.saved_tab_guid();
  saved_tab_group_model_.AddedLocally(std::move(group));

  EXPECT_CALL(processor_, Delete(tab_1_guid.AsLowercaseString(), _, _));
  EXPECT_CALL(processor_, Put(tab_2_guid.AsLowercaseString(), _, _)).Times(0);
  EXPECT_CALL(processor_, Put(group_guid.AsLowercaseString(), _, _)).Times(0);

  saved_tab_group_model_.RemoveTabFromGroupLocally(group_guid, tab_1_guid);
}

// Verify that locally updated tabs update the correct tabs in the processor.
TEST_F(SavedTabGroupSyncBridgeTest, UpdateTabLocally) {
  EXPECT_TRUE(saved_tab_group_model_.saved_tab_groups().empty());

  SavedTabGroup group(u"Test Title", tab_groups::TabGroupColorId::kBlue, {},
                      /*position=*/std::nullopt);
  SavedTabGroupTab tab_1(GURL("https://website.com"), u"Website Title",
                         group.saved_guid(), /*position=*/std::nullopt);
  SavedTabGroupTab tab_2(GURL("https://google.com"), u"Google",
                         group.saved_guid(), /*position=*/std::nullopt);
  group.AddTabLocally(tab_1).AddTabLocally(tab_2);

  SavedTabGroupTab updated_tab_1(group.saved_tabs()[0]);
  updated_tab_1.SetURL(GURL("https://youtube.com"));
  updated_tab_1.SetTitle(u"Youtube");

  base::Uuid group_guid = group.saved_guid();
  base::Uuid tab_1_guid = tab_1.saved_tab_guid();
  base::Uuid tab_2_guid = tab_2.saved_tab_guid();
  saved_tab_group_model_.AddedLocally(std::move(group));

  EXPECT_CALL(processor_, Put(tab_1_guid.AsLowercaseString(), _, _));
  EXPECT_CALL(processor_, Put(tab_2_guid.AsLowercaseString(), _, _)).Times(0);
  EXPECT_CALL(processor_, Put(group_guid.AsLowercaseString(), _, _)).Times(0);

  saved_tab_group_model_.UpdateTabInGroup(group_guid, updated_tab_1,
                                          /*notify_observers=*/true);

  EXPECT_CALL(processor_, Put(tab_1_guid.AsLowercaseString(), _, _)).Times(0);
  saved_tab_group_model_.UpdateTabInGroup(group_guid, updated_tab_1,
                                          /*notify_observers=*/false);
}

// Verify that locally reordered tabs updates all tabs in the group.
TEST_F(SavedTabGroupSyncBridgeTest, ReorderTabsInGroupLocally) {
  EXPECT_TRUE(saved_tab_group_model_.saved_tab_groups().empty());

  SavedTabGroup group(u"Test Title", tab_groups::TabGroupColorId::kBlue, {},
                      /*position=*/std::nullopt);
  SavedTabGroupTab tab_1(GURL("https://website.com"), u"Website Title",
                         group.saved_guid(), /*position=*/std::nullopt);
  SavedTabGroupTab tab_2(GURL("https://google.com"), u"Google",
                         group.saved_guid(), /*position=*/std::nullopt);
  group.AddTabLocally(tab_1).AddTabLocally(tab_2);

  SavedTabGroupTab updated_tab_1(group.saved_tabs()[0]);
  updated_tab_1.SetURL(GURL("https://youtube.com"));
  updated_tab_1.SetTitle(u"Youtube");

  base::Uuid group_guid = group.saved_guid();
  base::Uuid tab_1_guid = tab_1.saved_tab_guid();
  base::Uuid tab_2_guid = tab_2.saved_tab_guid();
  saved_tab_group_model_.AddedLocally(std::move(group));

  EXPECT_CALL(processor_, Put(tab_1_guid.AsLowercaseString(), _, _));
  EXPECT_CALL(processor_, Put(tab_2_guid.AsLowercaseString(), _, _));
  EXPECT_CALL(processor_, Put(group_guid.AsLowercaseString(), _, _)).Times(0);

  saved_tab_group_model_.MoveTabInGroupTo(group_guid, tab_1_guid, 1);
}

// Verify that locally reordered tabs updates all tabs in the group.
TEST_F(SavedTabGroupSyncBridgeTest, ReorderGroupLocally) {
  EXPECT_TRUE(saved_tab_group_model_.saved_tab_groups().empty());

  SavedTabGroup group(u"Test Title", tab_groups::TabGroupColorId::kBlue, {},
                      /*position=*/std::nullopt);
  SavedTabGroup group_2(u"Test Title 2", tab_groups::TabGroupColorId::kRed, {},
                        /*position=*/std::nullopt);
  SavedTabGroupTab tab_1(GURL("https://website.com"), u"Website Title",
                         group.saved_guid(), /*position=*/std::nullopt);
  SavedTabGroupTab tab_2(GURL("https://google.com"), u"Google",
                         group.saved_guid(), /*position=*/std::nullopt);
  group.AddTabLocally(tab_1).AddTabLocally(tab_2);

  SavedTabGroupTab updated_tab_1(group.saved_tabs()[0]);
  updated_tab_1.SetURL(GURL("https://youtube.com"));
  updated_tab_1.SetTitle(u"Youtube");

  base::Uuid group_guid = group.saved_guid();
  base::Uuid group_2_guid = group_2.saved_guid();
  base::Uuid tab_1_guid = tab_1.saved_tab_guid();
  base::Uuid tab_2_guid = tab_2.saved_tab_guid();
  saved_tab_group_model_.AddedLocally(std::move(group));
  saved_tab_group_model_.AddedLocally(std::move(group_2));

  EXPECT_CALL(processor_, Put(tab_1_guid.AsLowercaseString(), _, _)).Times(0);
  EXPECT_CALL(processor_, Put(tab_2_guid.AsLowercaseString(), _, _)).Times(0);
  EXPECT_CALL(processor_, Put(group_guid.AsLowercaseString(), _, _));
  EXPECT_CALL(processor_, Put(group_2_guid.AsLowercaseString(), _, _));

  saved_tab_group_model_.ReorderGroupLocally(group_guid, 1);
}

// Verify that pulling the cache guid works.
TEST_F(SavedTabGroupSyncBridgeTest, Group) {
  const std::string expected_guid = "cache_guid";
  ON_CALL(processor_, TrackedCacheGuid())
      .WillByDefault(testing::Return(expected_guid));

  ASSERT_TRUE(processor_.IsTrackingMetadata());
  ASSERT_EQ(processor_.TrackedCacheGuid(), expected_guid);

  std::optional<std::string> maybe_cache_guid = bridge_->GetLocalCacheGuid();
  EXPECT_EQ(maybe_cache_guid, expected_guid);
}

class SavedTabGroupSyncBridgeMigrationTest
    : public SavedTabGroupSyncBridgeTest {
 public:
  void SetUp() override {
    pref_service_.registry()->RegisterBooleanPref(
        prefs::kSavedTabGroupSpecificsToDataMigration, false);
    ON_CALL(processor_, IsTrackingMetadata())
        .WillByDefault(testing::Return(true));
    ON_CALL(processor_, TrackedCacheGuid())
        .WillByDefault(testing::Return("local_cache_guid"));
  }

  void CreateBridge(bool has_specifics_migrated) {
    pref_service_.SetBoolean(prefs::kSavedTabGroupSpecificsToDataMigration,
                             has_specifics_migrated);
    bridge_ = std::make_unique<SavedTabGroupSyncBridge>(
        &sync_bridge_model_wrapper_,
        syncer::DataTypeStoreTestUtil::FactoryForForwardingStore(store_.get()),
        processor_.CreateForwardingProcessor(), &pref_service_);
    task_environment_.RunUntilIdle();
  }
};

TEST_F(
    SavedTabGroupSyncBridgeMigrationTest,
    MigrateSpecificsToSavedTabGroupData_OldToNewFormat_Success_OneGroup_VerifyNewRecord) {
  // Create a SavedTabGroup and serialize in the old format.
  SavedTabGroup group(u"Test Group", tab_groups::TabGroupColorId::kBlue, {}, 0,
                      base::Uuid::GenerateRandomV4());
  sync_pb::SavedTabGroupSpecifics old_specifics =
      SavedTabGroupSyncBridge::SavedTabGroupToSpecificsForTest(group);

  std::unique_ptr<syncer::DataTypeStore::WriteBatch> batch =
      store_->CreateWriteBatch();
  batch->WriteData(old_specifics.guid(), old_specifics.SerializeAsString());
  store_->CommitWriteBatch(std::move(batch), base::DoNothing());

  // Create the bridge. That should trigger migration.
  CreateBridge(/*has_specifics_migrated=*/false);
  task_environment_.RunUntilIdle();

  // Read the migrated data from the store.
  const std::map<std::string, proto::SavedTabGroupData> data =
      syncer::DataTypeStoreTestUtil::ReadAllDataAsProtoAndWait<
          proto::SavedTabGroupData>(*store_);

  // Verify the migrated data
  ASSERT_EQ(data.size(), 1u);
  const proto::SavedTabGroupData& migrated_data = data.begin()->second;

  EXPECT_TRUE(AreGroupSpecificsEqual(migrated_data.specifics(), old_specifics));

  // Verify that the migration pref is set to true.
  EXPECT_TRUE(
      pref_service_.GetBoolean(prefs::kSavedTabGroupSpecificsToDataMigration));
}

TEST_F(
    SavedTabGroupSyncBridgeMigrationTest,
    MigrateSpecificsToSavedTabGroupData_OldToNewFormat_Success_OneGroupWithOneTab) {
  // Create a SavedTabGroup with one tab and serialize in the old format.
  SavedTabGroup group(u"Test Group", tab_groups::TabGroupColorId::kBlue, {}, 0,
                      base::Uuid::GenerateRandomV4());
  SavedTabGroupTab tab_1(GURL("https://website.com"), u"Website Title",
                         group.saved_guid(), 0);
  group.AddTabLocally(tab_1);

  sync_pb::SavedTabGroupSpecifics old_specifics =
      SavedTabGroupSyncBridge::SavedTabGroupToSpecificsForTest(group);
  sync_pb::SavedTabGroupSpecifics old_tab_specifics =
      SavedTabGroupSyncBridge::SavedTabGroupTabToSpecificsForTest(tab_1);

  std::unique_ptr<syncer::DataTypeStore::WriteBatch> batch =
      store_->CreateWriteBatch();
  batch->WriteData(old_specifics.guid(), old_specifics.SerializeAsString());
  batch->WriteData(old_tab_specifics.guid(),
                   old_tab_specifics.SerializeAsString());
  store_->CommitWriteBatch(std::move(batch), base::DoNothing());

  // Create the bridge. That should trigger migration.
  CreateBridge(/*has_specifics_migrated=*/false);
  task_environment_.RunUntilIdle();

  // Read the migrated data from the store.
  const syncer::DataTypeStore::RecordList entries =
      syncer::DataTypeStoreTestUtil::ReadAllDataAndWait(*store_);

  // Verify the migrated data
  ASSERT_EQ(entries.size(), 2u);
  EXPECT_EQ(1u, saved_tab_group_model_.saved_tab_groups().size());

  // Verify the migrated data in the model.
  const SavedTabGroup* migrated_group =
      saved_tab_group_model_.Get(group.saved_guid());
  EXPECT_TRUE(migrated_group);  // The group should exist in the model

  // Compare the migrated group with the original group (excluding
  // local_group_id).
  EXPECT_EQ(migrated_group->title(), group.title());
  EXPECT_EQ(migrated_group->color(), group.color());
  EXPECT_EQ(migrated_group->position(), group.position());

  EXPECT_FALSE(migrated_group->local_group_id().has_value());

  // Verify the migrated tabs.
  const SavedTabGroupTab* migrated_tab =
      migrated_group->GetTab(tab_1.saved_tab_guid());
  EXPECT_TRUE(migrated_tab);
  EXPECT_EQ(migrated_tab->url(), tab_1.url());
  EXPECT_EQ(migrated_tab->title(), tab_1.title());

  // Verify that the migration pref is set to true.
  EXPECT_TRUE(
      pref_service_.GetBoolean(prefs::kSavedTabGroupSpecificsToDataMigration));
}

TEST_F(SavedTabGroupSyncBridgeMigrationTest,
       MigrateSpecificsToSavedTabGroupData_CorruptedData) {
  // 1. Create invalid data.
  std::string invalid_data = "this is not a valid protobuf";

  // 2. Write the invalid data to the DataTypeStore.
  std::unique_ptr<syncer::DataTypeStore::WriteBatch> batch =
      store_->CreateWriteBatch();
  batch->WriteData(base::Uuid::GenerateRandomV4().AsLowercaseString(),
                   invalid_data);
  store_->CommitWriteBatch(std::move(batch), base::DoNothing());

  // 3. Create the bridge (triggering migration).
  CreateBridge(/*has_specifics_migrated=*/false);
  task_environment_.RunUntilIdle();

  // 4. Verify that the migration didn't crash and the model is empty.
  EXPECT_TRUE(saved_tab_group_model_.saved_tab_groups().empty());
}

TEST_F(SavedTabGroupSyncBridgeMigrationTest,
       SavedTabGroupSyncBridgeMigrationTest_AlreadyMigrated) {
  // Create a SavedTabGroup and serialize in the new format.
  SavedTabGroup group(u"Test Group", tab_groups::TabGroupColorId::kBlue, {}, 0,
                      base::Uuid::GenerateRandomV4());

  proto::SavedTabGroupData group_data =
      SavedTabGroupSyncBridge::SavedTabGroupToDataForTest(group);
  std::unique_ptr<syncer::DataTypeStore::WriteBatch> batch =
      store_->CreateWriteBatch();
  batch->WriteData(group_data.specifics().guid(),
                   group_data.SerializeAsString());
  store_->CommitWriteBatch(std::move(batch), base::DoNothing());

  // Create the bridge with pref set to true. That should not trigger migration.
  CreateBridge(/*has_specifics_migrated=*/true);
  task_environment_.RunUntilIdle();

  // Read the migrated data from the store.
  const syncer::DataTypeStore::RecordList entries =
      syncer::DataTypeStoreTestUtil::ReadAllDataAndWait(*store_);

  // Verify the migrated data. It should match the original.
  ASSERT_EQ(entries.size(), 1u);
  const syncer::DataTypeStore::Record& record = entries.at(0);
  proto::SavedTabGroupData migrated_data;
  EXPECT_EQ(group_data.SerializeAsString(), record.value);
  ASSERT_TRUE(migrated_data.ParseFromString(record.value));
  EXPECT_TRUE(AreGroupSpecificsEqual(migrated_data.specifics(),
                                     group_data.specifics()));

  EXPECT_TRUE(
      pref_service_.GetBoolean(prefs::kSavedTabGroupSpecificsToDataMigration));
}

TEST_F(SavedTabGroupSyncBridgeMigrationTest,
       SavedTabGroupSyncBridgeMigrationTest_NewFormatBeforeMigration) {
  // Create a SavedTabGroup and serialize in the new format.
  SavedTabGroup group(u"Test Group", tab_groups::TabGroupColorId::kBlue, {}, 0,
                      base::Uuid::GenerateRandomV4());

  proto::SavedTabGroupData group_data =
      SavedTabGroupSyncBridge::SavedTabGroupToDataForTest(group);
  std::unique_ptr<syncer::DataTypeStore::WriteBatch> batch =
      store_->CreateWriteBatch();
  batch->WriteData(group_data.specifics().guid(),
                   group_data.SerializeAsString());
  store_->CommitWriteBatch(std::move(batch), base::DoNothing());

  // Create the bridge with pref set to true. That should not trigger migration.
  CreateBridge(/*has_migrated=*/false);
  task_environment_.RunUntilIdle();

  // Read the migrated data from the store.
  const syncer::DataTypeStore::RecordList entries =
      syncer::DataTypeStoreTestUtil::ReadAllDataAndWait(*store_);

  // Verify the migrated data. It should match the original.
  ASSERT_EQ(entries.size(), 1u);
  const syncer::DataTypeStore::Record& record = entries.at(0);
  proto::SavedTabGroupData migrated_data;
  EXPECT_EQ(group_data.SerializeAsString(), record.value);
  ASSERT_TRUE(migrated_data.ParseFromString(record.value));
  EXPECT_TRUE(AreGroupSpecificsEqual(migrated_data.specifics(),
                                     group_data.specifics()));

  EXPECT_TRUE(
      pref_service_.GetBoolean(prefs::kSavedTabGroupSpecificsToDataMigration));
}

TEST_F(
    SavedTabGroupSyncBridgeMigrationTest,
    MigrateSpecificsToSavedTabGroupData_AlreadyNewFormatBeforeMigration_Success_OneGroupWithOneTab) {
  // Create a SavedTabGroup with one tab and serialize in the old format.
  SavedTabGroup group(u"Test Group", tab_groups::TabGroupColorId::kBlue, {}, 0,
                      base::Uuid::GenerateRandomV4());
  SavedTabGroupTab tab_1(GURL("https://website.com"), u"Website Title",
                         group.saved_guid(), 0);
  group.AddTabLocally(tab_1);

  proto::SavedTabGroupData group_data =
      SavedTabGroupSyncBridge::SavedTabGroupToDataForTest(group);
  proto::SavedTabGroupData tab_data =
      SavedTabGroupSyncBridge::SavedTabGroupTabToDataForTest(tab_1);

  std::unique_ptr<syncer::DataTypeStore::WriteBatch> batch =
      store_->CreateWriteBatch();
  batch->WriteData(group_data.specifics().guid(),
                   group_data.SerializeAsString());
  batch->WriteData(tab_data.specifics().guid(), tab_data.SerializeAsString());
  store_->CommitWriteBatch(std::move(batch), base::DoNothing());

  // Create the bridge. That should trigger migration.
  CreateBridge(/*has_migrated=*/false);
  task_environment_.RunUntilIdle();

  // Read the migrated data from the store.
  const syncer::DataTypeStore::RecordList entries =
      syncer::DataTypeStoreTestUtil::ReadAllDataAndWait(*store_);

  // Verify the migrated data
  ASSERT_EQ(entries.size(), 2u);
  EXPECT_EQ(1u, saved_tab_group_model_.saved_tab_groups().size());

  // Verify the migrated data in the model.
  const SavedTabGroup* migrated_group =
      saved_tab_group_model_.Get(group.saved_guid());
  EXPECT_TRUE(migrated_group);  // The group should exist in the model

  // Compare the migrated group with the original group (excluding
  // local_group_id).
  EXPECT_EQ(migrated_group->title(), group.title());
  EXPECT_EQ(migrated_group->color(), group.color());
  EXPECT_EQ(migrated_group->position(), group.position());

  EXPECT_FALSE(migrated_group->local_group_id().has_value());

  // Verify the migrated tabs.
  const SavedTabGroupTab* migrated_tab =
      migrated_group->GetTab(tab_1.saved_tab_guid());
  EXPECT_TRUE(migrated_tab);
  EXPECT_EQ(migrated_tab->url(), tab_1.url());
  EXPECT_EQ(migrated_tab->title(), tab_1.title());

  // Verify that the migration pref is set to true.
  EXPECT_TRUE(
      pref_service_.GetBoolean(prefs::kSavedTabGroupSpecificsToDataMigration));
}

TEST_F(SavedTabGroupSyncBridgeTest, NewlyOrphanedGroupsDontGetDestroyed) {
  SavedTabGroup group(u"Test Title", tab_groups::TabGroupColorId::kBlue, {});
  // position must be set or the update time will be overridden during model
  // save.
  group.SetPosition(0);
  group.SetUpdateTime(base::Time::Now());

  saved_tab_group_model_.AddedLocally(std::move(group));
  EXPECT_EQ(1u, saved_tab_group_model_.saved_tab_groups().size());

  bridge_->MergeFullSyncData(bridge_->CreateMetadataChangeList(), {});

  EXPECT_EQ(1u, saved_tab_group_model_.saved_tab_groups().size());
}

TEST_F(SavedTabGroupSyncBridgeTest, OldOrphanedGroupsGetDestroyed) {
  auto id = base::Uuid::GenerateRandomV4();
  SavedTabGroup group(u"Test Title", tab_groups::TabGroupColorId::kBlue, {});
  // position must be set or the update time will be overridden during model
  // save.
  group.SetPosition(0);

  group.SetUpdateTime((base::Time::Now() - kDiscardOrphanedTabsThreshold) -
                      base::Days(1));

  saved_tab_group_model_.AddedLocally(std::move(group));
  EXPECT_EQ(1u, saved_tab_group_model_.saved_tab_groups().size());

  bridge_->MergeFullSyncData(bridge_->CreateMetadataChangeList(), {});

  EXPECT_EQ(0u, saved_tab_group_model_.saved_tab_groups().size());
}

TEST_F(SavedTabGroupSyncBridgeTest, StoreLocalIdOnRemoteUpdate) {
  if (!AreLocalIdsPersisted()) {
    // This test is only relevant if local IDs are persisted.
    return;
  }
  const LocalTabGroupID kLocalGroupId = test::GenerateRandomTabGroupID();

  // Initialize the bridge.
  syncer::EntityChangeList empty_change_list;
  bridge_->MergeFullSyncData(bridge_->CreateMetadataChangeList(),
                             std::move(empty_change_list));

  // Simulate a reentrant call during applying remote updates.
  EXPECT_CALL(mock_model_observer_, SavedTabGroupAddedFromSync)
      .WillOnce(Invoke([this, &kLocalGroupId](const base::Uuid& group_guid) {
        saved_tab_group_model_.OnGroupOpenedInTabStrip(group_guid,
                                                       kLocalGroupId);
      }));
  SavedTabGroup group(u"Test Title", tab_groups::TabGroupColorId::kBlue, {},
                      /*position=*/std::nullopt);
  bridge_->ApplyIncrementalSyncChanges(
      bridge_->CreateMetadataChangeList(),
      CreateEntityChangeListFromGroup(
          group, syncer::EntityChange::ChangeType::ACTION_ADD));
  testing::Mock::VerifyAndClearExpectations(&mock_model_observer_);

  // Verify that the local group ID is persisted.
  std::optional<proto::SavedTabGroupData> stored_saved_tab_group =
      ReadSavedTabGroupDataFromStore(group.saved_guid());
  ASSERT_TRUE(stored_saved_tab_group.has_value());
  EXPECT_EQ(
      LocalTabGroupIDFromString(
          stored_saved_tab_group->local_tab_group_data().local_group_id()),
      kLocalGroupId);
}

// Verify that deleting the last tab from sync creates a pending NTP which is
// stored to DB correctly.
TEST_F(SavedTabGroupSyncBridgeTest,
       PendingNtpIsCreatedOnDeletionAndStoredToDB) {
  syncer::EntityChangeList empty_change_list;
  bridge_->MergeFullSyncData(bridge_->CreateMetadataChangeList(),
                             std::move(empty_change_list));

  // Create a group with a single tab.
  SavedTabGroup group(u"Test Title", tab_groups::TabGroupColorId::kBlue, {},
                      /*position=*/0);
  SavedTabGroupTab tab_1(GURL("https://website.com"), u"Website Title",
                         group.saved_guid(), /*position=*/std::nullopt);
  group.AddTabLocally(tab_1);

  ASSERT_EQ(group.saved_tabs().size(), 1u);

  bridge_->ApplyIncrementalSyncChanges(
      bridge_->CreateMetadataChangeList(),
      CreateEntityChangeListFromGroup(
          group, syncer::EntityChange::ChangeType::ACTION_ADD));

  ASSERT_TRUE(saved_tab_group_model_.Contains(group.saved_guid()));
  const SavedTabGroup* group_from_model =
      saved_tab_group_model_.Get(group.saved_guid());

  // Delete the tab from sync. It should result in creating the pending
  base::Uuid last_tab_id = group.saved_tabs()[0].saved_tab_guid();

  syncer::EntityChangeList delete_tab_change_list;
  delete_tab_change_list.push_back(CreateEntityChange(
      SavedTabGroupSyncBridge::SavedTabGroupTabToSpecificsForTest(
          group.saved_tabs()[0]),
      syncer::EntityChange::ChangeType::ACTION_DELETE));
  bridge_->ApplyIncrementalSyncChanges(bridge_->CreateMetadataChangeList(),
                                       std::move(delete_tab_change_list));

  ASSERT_FALSE(group_from_model->ContainsTab(last_tab_id));
  EXPECT_EQ(group_from_model->saved_tabs().size(), 1u);

  const SavedTabGroupTab& pending_ntp = group_from_model->saved_tabs()[0];
  base::Uuid pending_ntp_guid = pending_ntp.saved_tab_guid();
  EXPECT_TRUE(pending_ntp.is_pending_ntp());

  // Read the pending NTP from storage and verify that it's written correctly.
  std::optional<proto::SavedTabGroupData> stored_saved_tab_group_data =
      ReadSavedTabGroupDataFromStore(pending_ntp_guid);
  ASSERT_TRUE(stored_saved_tab_group_data.has_value());
}

TEST_F(SavedTabGroupSyncBridgeTest,
       ShouldTrimAllSupportedFieldsFromRemoteTabSpecifics) {
  sync_pb::EntitySpecifics remote_tab_specifics;
  sync_pb::SavedTabGroupSpecifics* tab_specifics =
      remote_tab_specifics.mutable_saved_tab_group();
  tab_specifics->set_guid("guid");
  tab_specifics->set_update_time_windows_epoch_micros(1234567890);
  tab_specifics->mutable_tab()->set_url("http://google.com/1");
  tab_specifics->mutable_tab()->set_title("title");
  tab_specifics->mutable_tab()->set_group_guid("group_guid");
  tab_specifics->mutable_tab()->set_position(20);

  tab_specifics->mutable_attribution_metadata()
      ->mutable_created()
      ->mutable_device_info()
      ->set_cache_guid("cache_guid");
  tab_specifics->mutable_attribution_metadata()
      ->mutable_updated()
      ->mutable_device_info()
      ->set_cache_guid("cache_guid");

  EXPECT_THAT(
      bridge_->TrimAllSupportedFieldsFromRemoteSpecifics(remote_tab_specifics),
      EqualsProto(sync_pb::EntitySpecifics()));
}

TEST_F(SavedTabGroupSyncBridgeTest,
       ShouldTrimAllSupportedFieldsFromRemoteGroupSpecifics) {
  sync_pb::EntitySpecifics remote_group_specifics;
  sync_pb::SavedTabGroupSpecifics* group_specifics =
      remote_group_specifics.mutable_saved_tab_group();
  group_specifics->set_guid("guid");
  group_specifics->set_creation_time_windows_epoch_micros(2345678901);
  group_specifics->set_update_time_windows_epoch_micros(1234567890);
  group_specifics->mutable_group()->set_position(2);
  group_specifics->mutable_group()->set_title("title");
  group_specifics->mutable_group()->set_color(
      sync_pb::SavedTabGroup_SavedTabGroupColor_SAVED_TAB_GROUP_COLOR_CYAN);
  group_specifics->mutable_group()->set_pinned_position(3);

  group_specifics->mutable_attribution_metadata()
      ->mutable_created()
      ->mutable_device_info()
      ->set_cache_guid("cache_guid");
  group_specifics->mutable_attribution_metadata()
      ->mutable_updated()
      ->mutable_device_info()
      ->set_cache_guid("cache_guid");

  EXPECT_THAT(bridge_->TrimAllSupportedFieldsFromRemoteSpecifics(
                  remote_group_specifics),
              EqualsProto(sync_pb::EntitySpecifics()));
}

TEST_F(SavedTabGroupSyncBridgeTest,
       ShouldKeepUnknownFieldsFromRemoteTabSpecifics) {
  sync_pb::test_utils::SavedTabGroupSpecifics extended_tab_specifics;
  extended_tab_specifics.set_guid("guid");
  extended_tab_specifics.set_update_time_windows_epoch_micros(1234567890);
  extended_tab_specifics.mutable_tab()->set_url("http://google.com/1");
  extended_tab_specifics.mutable_tab()->set_title("title");
  extended_tab_specifics.mutable_tab()->set_group_guid("group_guid");
  extended_tab_specifics.mutable_tab()->set_position(20);
  extended_tab_specifics.mutable_tab()->set_extra_field_for_testing(
      "extra_field_for_testing");

  // Serialize and deserialize the proto to get unknown fields.
  sync_pb::EntitySpecifics remote_specifics;
  ASSERT_TRUE(remote_specifics.mutable_saved_tab_group()->ParseFromString(
      extended_tab_specifics.SerializeAsString()));

  sync_pb::EntitySpecifics trimmed_specifics =
      bridge_->TrimAllSupportedFieldsFromRemoteSpecifics(remote_specifics);

  EXPECT_THAT(trimmed_specifics, Not(EqualsProto(sync_pb::EntitySpecifics())));

  // Verify that deserialized proto keeps unknown fields.
  sync_pb::test_utils::SavedTabGroupSpecifics deserialized_extended_specifics;
  ASSERT_TRUE(deserialized_extended_specifics.ParseFromString(
      trimmed_specifics.saved_tab_group().SerializeAsString()));
  EXPECT_EQ(deserialized_extended_specifics.tab().extra_field_for_testing(),
            "extra_field_for_testing");
}

TEST_F(SavedTabGroupSyncBridgeTest,
       ShouldKeepUnknownFieldsFromRemoteGroupSpecifics) {
  sync_pb::test_utils::SavedTabGroupSpecifics extended_group_specifics;

  extended_group_specifics.set_guid("guid");
  extended_group_specifics.set_creation_time_windows_epoch_micros(2345678901);
  extended_group_specifics.set_update_time_windows_epoch_micros(1234567890);
  extended_group_specifics.mutable_group()->set_position(2);
  extended_group_specifics.mutable_group()->set_title("title");
  extended_group_specifics.mutable_group()->set_color(
      sync_pb::test_utils::
          SavedTabGroup_SavedTabGroupColor_SAVED_TAB_GROUP_COLOR_CYAN);
  extended_group_specifics.mutable_group()->set_pinned_position(3);
  extended_group_specifics.mutable_group()->set_extra_field_for_testing(
      "extra_field_for_testing");

  // Serialize and deserialize the proto to get unknown fields.
  sync_pb::EntitySpecifics remote_specifics;
  ASSERT_TRUE(remote_specifics.mutable_saved_tab_group()->ParseFromString(
      extended_group_specifics.SerializeAsString()));

  sync_pb::EntitySpecifics trimmed_specifics =
      bridge_->TrimAllSupportedFieldsFromRemoteSpecifics(remote_specifics);

  EXPECT_THAT(trimmed_specifics, Not(EqualsProto(sync_pb::EntitySpecifics())));

  // Verify that deserialized proto keeps unknown fields.
  sync_pb::test_utils::SavedTabGroupSpecifics deserialized_extended_specifics;
  ASSERT_TRUE(deserialized_extended_specifics.ParseFromString(
      trimmed_specifics.saved_tab_group().SerializeAsString()));
  EXPECT_EQ(deserialized_extended_specifics.group().extra_field_for_testing(),
            "extra_field_for_testing");
}

TEST_F(SavedTabGroupSyncBridgeTest, ShouldPopulateUnknownFieldsOnLocalChanges) {
  base::Uuid group_guid = base::Uuid::GenerateRandomV4();
  sync_pb::EntitySpecifics remote_tab_group_specifics;
  *remote_tab_group_specifics.mutable_saved_tab_group() =
      MakeTabGroupSpecificsWithUnknownFields(group_guid, "title",
                                             "extra_field");
  remote_tab_group_specifics.mutable_saved_tab_group()
      ->mutable_group()
      ->set_color(
          sync_pb::SavedTabGroup_SavedTabGroupColor_SAVED_TAB_GROUP_COLOR_CYAN);
  sync_pb::EntitySpecifics trimmed_specifics =
      bridge_->TrimAllSupportedFieldsFromRemoteSpecifics(
          remote_tab_group_specifics);
  ON_CALL(mock_processor(), GetPossiblyTrimmedRemoteSpecifics(_))
      .WillByDefault(ReturnRef(trimmed_specifics));

  ASSERT_EQ(0u, model()->saved_tab_groups().size());

  // Mimic remote addition of 1 group with 1 tab.
  syncer::EntityChangeList change_list;
  change_list.push_back(
      CreateEntityChange(remote_tab_group_specifics.saved_tab_group(),
                         syncer::EntityChange::ChangeType::ACTION_ADD));

  SavedTabGroupTab tab(GURL("https://youtube.com"), u"Youtube", group_guid,
                       /*position=*/5);
  change_list.push_back(CreateEntityChange(
      SavedTabGroupSyncBridge::SavedTabGroupTabToSpecificsForTest(tab),
      syncer::EntityChange::ChangeType::ACTION_ADD));

  bridge_->ApplyIncrementalSyncChanges(bridge_->CreateMetadataChangeList(),
                                       std::move(change_list));
  ASSERT_EQ(1u, model()->saved_tab_groups().size());
  ASSERT_THAT(model()->saved_tab_groups(),
              ElementsAre(test::HasSavedGroupMetadata(
                  "title", tab_groups::TabGroupColorId::kCyan)));

  // Simulate opening the group in the tab strip to make local changes.
  ASSERT_EQ(group_guid, model()->saved_tab_groups().front().saved_guid());
  const tab_groups::LocalTabGroupID local_tab_group_id =
      test::GenerateRandomTabGroupID();
  model()->OnGroupOpenedInTabStrip(group_guid, local_tab_group_id);

  // Make local changes to the group. The bridge should make a local change
  // with the unknown fields populated.
  EXPECT_CALL(
      mock_processor(),
      Put(_, Pointee(EntityDataHasGroupUnsupportedFields("extra_field")), _));
  tab_groups::TabGroupVisualData visual_data(
      u"new title", tab_groups::TabGroupColorId::kYellow);
  model()->UpdateVisualDataLocally(local_tab_group_id, &visual_data);
}

}  // namespace tab_groups