File: saved_tab_group_sync_bridge_unittest.cc

package info (click to toggle)
chromium 120.0.6099.224-1~deb11u1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 6,112,112 kB
  • sloc: cpp: 32,907,025; ansic: 8,148,123; javascript: 3,679,536; python: 2,031,248; asm: 959,718; java: 804,675; xml: 617,256; sh: 111,417; objc: 100,835; perl: 88,443; cs: 53,032; makefile: 29,579; fortran: 24,137; php: 21,162; tcl: 21,147; sql: 20,809; ruby: 17,735; pascal: 12,864; yacc: 8,045; lisp: 3,388; lex: 1,323; ada: 727; awk: 329; jsp: 267; csh: 117; exp: 43; sed: 37
file content (790 lines) | stat: -rw-r--r-- 36,203 bytes parent folder | download
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
// 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 <algorithm>
#include <memory>
#include <string>
#include <vector>

#include "base/functional/bind.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/saved_tab_groups/saved_tab_group.h"
#include "components/saved_tab_groups/saved_tab_group_model.h"
#include "components/saved_tab_groups/saved_tab_group_model_observer.h"
#include "components/saved_tab_groups/saved_tab_group_sync_bridge.h"
#include "components/saved_tab_groups/saved_tab_group_tab.h"
#include "components/sync/engine/commit_queue.h"
#include "components/sync/model/data_batch.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_type_store.h"
#include "components/sync/model/model_type_store_service_impl.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/mock_model_type_change_processor.h"
#include "components/sync/test/model_type_store_test_util.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 "third_party/abseil-cpp/absl/types/optional.h"
#include "url/gurl.h"

using testing::_;

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

// 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.creation_time_windows_epoch_micros() !=
      sp2.creation_time_windows_epoch_micros()) {
    return false;
  }
  return true;
}

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;
}

syncer::EntityData CreateEntityData(
    std::unique_ptr<sync_pb::SavedTabGroupSpecifics> specific) {
  syncer::EntityData entity_data;
  entity_data.name = specific->guid();
  entity_data.specifics.set_allocated_saved_tab_group(specific.release());
  return entity_data;
}

std::unique_ptr<syncer::EntityChange> CreateEntityChange(
    std::unique_ptr<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(std::move(specific)));
    case syncer::EntityChange::ACTION_UPDATE:
      return syncer::EntityChange::CreateUpdate(
          guid, CreateEntityData(std::move(specific)));
    case syncer::EntityChange::ACTION_DELETE:
      return syncer::EntityChange::CreateDelete(guid);
  }
}

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

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

  return entity_change_list;
}

}  // anonymous namespace

// // Verifies the sync bridge correctly passes/merges data in to the model.
class SavedTabGroupSyncBridgeTest : public ::testing::Test {
 public:
  SavedTabGroupSyncBridgeTest()
      : store_(syncer::ModelTypeStoreTestUtil::CreateInMemoryStoreForTest()) {
    InitializeBridge();
  }
  ~SavedTabGroupSyncBridgeTest() override = default;

  void InitializeBridge() {
    ON_CALL(processor_, IsTrackingMetadata())
        .WillByDefault(testing::Return(true));
    bridge_ = std::make_unique<SavedTabGroupSyncBridge>(
        &saved_tab_group_model_,
        syncer::ModelTypeStoreTestUtil::FactoryForForwardingStore(store_.get()),
        processor_.CreateForwardingProcessor());
    task_environment_.RunUntilIdle();
  }

  base::test::TaskEnvironment task_environment_;
  SavedTabGroupModel saved_tab_group_model_;
  testing::NiceMock<syncer::MockModelTypeChangeProcessor> processor_;
  std::unique_ptr<syncer::ModelTypeStore> store_;
  std::unique_ptr<SavedTabGroupSyncBridge> bridge_;
};

// 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=*/absl::nullopt);
  SavedTabGroupTab tab_2(GURL("https://google.com"), u"Google",
                         group.saved_guid(), /*position=*/absl::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(AreGroupSpecificsEqual(*group.ToSpecifics(),
                                     *group_from_model->ToSpecifics()));

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

// Verify merging with preexisting data in the model merges the correct
// elements.
TEST_F(SavedTabGroupSyncBridgeTest, MergeFullSyncDataWithExistingData) {
  SavedTabGroup group(u"Test Title", tab_groups::TabGroupColorId::kBlue, {},
                      /*position=*/absl::nullopt);
  SavedTabGroupTab tab_1(GURL("https://website.com"), u"Website Title",
                         group.saved_guid(), /*position=*/absl::nullopt);
  SavedTabGroupTab tab_2(GURL("https://google.com"), u"Google",
                         group.saved_guid(), /*position=*/absl::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_windows_epoch_micros();
  base::Time tab_1_creation_time = tab_1.creation_time_windows_epoch_micros();
  base::Time tab_2_creation_time = tab_2.creation_time_windows_epoch_micros();

  saved_tab_group_model_.Add(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, group_guid, absl::nullopt,
                              group_creation_time);
  SavedTabGroupTab updated_tab_1(GURL("https://support.google.com"), u"Support",
                                 group_guid, /*position=*/0, tab_1_guid,
                                 absl::nullopt, tab_1_creation_time);
  updated_group.AddTabLocally(updated_tab_1);

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

  // Ensure the updated data is eligible to be merged.
  EXPECT_TRUE(group_from_model->ShouldMergeGroup(*updated_group.ToSpecifics()));
  EXPECT_TRUE(group_from_model->GetTab(tab_1_guid)
                  ->ShouldMergeTab(*updated_tab_1.ToSpecifics()));

  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,
                                 absl::nullopt, tab_2_creation_time);
  EXPECT_TRUE(AreTabSpecificsEqual(
      *tab_2_replica.ToSpecifics(),
      *group_from_model->GetTab(tab_2_guid)->ToSpecifics()));

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

// 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(orphaned_tab.ToSpecifics(),
                         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(missing_group.ToSpecifics(),
                         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(AreGroupSpecificsEqual(
      *missing_group.ToSpecifics(), *orphaned_group_from_model->ToSpecifics()));
  EXPECT_TRUE(AreTabSpecificsEqual(
      *orphaned_tab.ToSpecifics(),
      *orphaned_group_from_model->GetTab(orphaned_tab.saved_tab_guid())
           ->ToSpecifics()));
}

// 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.SetUpdateTimeWindowsEpochMicros(base::Time::Now() -
                                               kDiscardOrphanedTabsThreshold);

  syncer::EntityChangeList orphaned_tab_change_list;
  orphaned_tab_change_list.push_back(
      CreateEntityChange(orphaned_tab.ToSpecifics(),
                         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(missing_group.ToSpecifics(),
                         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(missing_group.ToSpecifics(),
                         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.SetUpdateTimeWindowsEpochMicros(base::Time::Now() -
                                               kDiscardOrphanedTabsThreshold);
  syncer::EntityChangeList orphaned_tab_change_list;
  orphaned_tab_change_list.push_back(
      CreateEntityChange(orphaned_tab.ToSpecifics(),
                         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(AreGroupSpecificsEqual(
      *missing_group.ToSpecifics(), *orphaned_group_from_model->ToSpecifics()));
  EXPECT_TRUE(AreTabSpecificsEqual(
      *orphaned_tab.ToSpecifics(),
      *orphaned_group_from_model->GetTab(orphaned_tab.saved_tab_guid())
           ->ToSpecifics()));
}

// 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=*/absl::nullopt);
  SavedTabGroupTab tab_2(GURL("https://google.com"), u"Google",
                         group.saved_guid(), /*position=*/absl::nullopt);
  group.AddTabLocally(tab_1).AddTabLocally(tab_2);

  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(AreGroupSpecificsEqual(*group.ToSpecifics(),
                                     *group_from_model->ToSpecifics()));

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

  // 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(additional_tab.ToSpecifics(),
                         syncer::EntityChange::ChangeType::ACTION_ADD));
  entity_change_list.push_back(
      (CreateEntityChange(orphaned_tab.ToSpecifics(),
                          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(AreTabSpecificsEqual(
        *tab.ToSpecifics(),
        *group_from_model->GetTab(tab.saved_tab_guid())->ToSpecifics()));
  }
}

// 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=*/absl::nullopt);
  SavedTabGroupTab tab_1(GURL("https://website.com"), u"Title",
                         group.saved_guid(), /*position=*/absl::nullopt);
  SavedTabGroupTab tab_2(GURL("https://google.com"), u"Google",
                         group.saved_guid(), /*position=*/absl::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(AreGroupSpecificsEqual(*group.ToSpecifics(),
                                     *group_from_model->ToSpecifics()));

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

// 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=*/absl::nullopt);
  SavedTabGroupTab tab_2(GURL("https://google.com"), u"Google",
                         group.saved_guid(), /*position=*/absl::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(group.saved_tabs()[0].ToSpecifics(),
                         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(
      AreTabSpecificsEqual(*group.saved_tabs()[1].ToSpecifics(),
                           *group_from_model->saved_tabs()[0].ToSpecifics()));
  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(
      group.ToSpecifics(), 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 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=*/absl::nullopt);
  SavedTabGroupTab tab_1(GURL("https://website.com"), u"Website Title",
                         group.saved_guid(), /*position=*/absl::nullopt);
  SavedTabGroupTab tab_2(GURL("https://google.com"), u"Google",
                         group.saved_guid(), /*position=*/absl::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_.Add(std::move(group));
}

// 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=*/absl::nullopt);
  SavedTabGroupTab tab_1(GURL("https://website.com"), u"Website Title",
                         group.saved_guid(), /*position=*/absl::nullopt);
  SavedTabGroupTab tab_2(GURL("https://google.com"), u"Google",
                         group.saved_guid(), /*position=*/absl::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_.Add(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_.Remove(group_guid);

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

  auto it_1 = base::ranges::find_if(
      tabs_missing_groups, [&](sync_pb::SavedTabGroupSpecifics specifics) {
        return specifics.guid() == tab_1_guid.AsLowercaseString();
      });
  auto it_2 = base::ranges::find_if(
      tabs_missing_groups, [&](sync_pb::SavedTabGroupSpecifics specifics) {
        return 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=*/absl::nullopt);
  SavedTabGroupTab tab_1(GURL("https://website.com"), u"Website Title",
                         group.saved_guid(), /*position=*/absl::nullopt);
  SavedTabGroupTab tab_2(GURL("https://google.com"), u"Google",
                         group.saved_guid(), /*position=*/absl::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_.Add(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_.UpdateVisualData(group_guid, &visual_data);
}

// 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=*/absl::nullopt);
  SavedTabGroupTab tab_1(GURL("https://website.com"), u"Website Title",
                         group.saved_guid(), /*position=*/absl::nullopt);
  SavedTabGroupTab tab_2(GURL("https://google.com"), u"Google",
                         group.saved_guid(), /*position=*/absl::nullopt);
  SavedTabGroupTab tab_3(GURL("https://youtube.com"), u"Youtube",
                         group.saved_guid(), /*position=*/absl::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_.Add(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);

  // TODO(dljames): Because `tab_3` was added to the middle of the group, only
  // `tab_2` will have its position updated. Once tab ordering is implemented,
  // only the affected tabs will need to be updated. In that case, the Put()
  // call for tab_1 can be removed.
  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=*/absl::nullopt);
  SavedTabGroupTab tab_1(GURL("https://website.com"), u"Website Title",
                         group.saved_guid(), /*position=*/absl::nullopt);
  SavedTabGroupTab tab_2(GURL("https://google.com"), u"Goole",
                         group.saved_guid(), /*position=*/absl::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_.Add(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=*/absl::nullopt);
  SavedTabGroupTab tab_1(GURL("https://website.com"), u"Website Title",
                         group.saved_guid(), /*position=*/absl::nullopt);
  SavedTabGroupTab tab_2(GURL("https://google.com"), u"Google",
                         group.saved_guid(), /*position=*/absl::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_.Add(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);
}

// 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=*/absl::nullopt);
  SavedTabGroupTab tab_1(GURL("https://website.com"), u"Website Title",
                         group.saved_guid(), /*position=*/absl::nullopt);
  SavedTabGroupTab tab_2(GURL("https://google.com"), u"Google",
                         group.saved_guid(), /*position=*/absl::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_.Add(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=*/absl::nullopt);
  SavedTabGroup group_2(u"Test Title 2", tab_groups::TabGroupColorId::kRed, {},
                        /*position=*/absl::nullopt);
  SavedTabGroupTab tab_1(GURL("https://website.com"), u"Website Title",
                         group.saved_guid(), /*position=*/absl::nullopt);
  SavedTabGroupTab tab_2(GURL("https://google.com"), u"Google",
                         group.saved_guid(), /*position=*/absl::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_.Add(std::move(group));
  saved_tab_group_model_.Add(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);
}