File: compound_tab_container_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 (756 lines) | stat: -rw-r--r-- 31,149 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
// 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 "chrome/browser/ui/views/tabs/compound_tab_container.h"

#include <memory>

#include "base/memory/raw_ref.h"
#include "chrome/browser/ui/layout_constants.h"
#include "chrome/browser/ui/tabs/tab_renderer_data.h"
#include "chrome/browser/ui/views/frame/browser_root_view.h"
#include "chrome/browser/ui/views/tabs/dragging/tab_drag_context.h"
#include "chrome/browser/ui/views/tabs/fake_base_tab_strip_controller.h"
#include "chrome/browser/ui/views/tabs/fake_tab_slot_controller.h"
#include "chrome/browser/ui/views/tabs/tab_group_header.h"
#include "chrome/test/views/chrome_views_test_base.h"
#include "tab_style_views.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "ui/base/dragdrop/drag_drop_types.h"
#include "ui/base/dragdrop/drop_target_event.h"
#include "ui/base/metadata/metadata_header_macros.h"
#include "ui/base/metadata/metadata_impl_macros.h"
#include "ui/gfx/animation/animation_test_api.h"
#include "ui/views/view_utils.h"
#include "ui/views/widget/widget.h"

namespace {

class FakeTabDragContext : public TabDragContextBase {
  METADATA_HEADER(FakeTabDragContext, TabDragContextBase)
 public:
  FakeTabDragContext() = default;
  ~FakeTabDragContext() override = default;

  void UpdateAnimationTarget(TabSlotView* tab_slot_view,
                             const gfx::Rect& target_bounds) override {}
  bool IsDragSessionActive() const override { return false; }
  bool IsAnimatingDragEnd() const override { return false; }
  void CompleteEndDragAnimations() override {}
  int GetTabDragAreaWidth() const override { return width(); }
};

BEGIN_METADATA(FakeTabDragContext)
END_METADATA

class FakeTabContainerController final : public TabContainerController {
 public:
  explicit FakeTabContainerController(TabStripController& tab_strip_controller)
      : tab_strip_controller_(tab_strip_controller) {}
  ~FakeTabContainerController() override = default;

  bool IsValidModelIndex(int index) const override {
    return tab_strip_controller_->IsValidIndex(index);
  }

  std::optional<int> GetActiveIndex() const override {
    return tab_strip_controller_->GetActiveIndex();
  }

  int NumPinnedTabsInModel() const override {
    for (size_t i = 0;
         i < static_cast<size_t>(tab_strip_controller_->GetCount()); ++i) {
      if (!tab_strip_controller_->IsTabPinned(static_cast<int>(i))) {
        return static_cast<int>(i);
      }
    }

    // All tabs are pinned.
    return tab_strip_controller_->GetCount();
  }

  void OnDropIndexUpdate(std::optional<int> index, bool drop_before) override {
    tab_strip_controller_->OnDropIndexUpdate(index, drop_before);
  }

  bool IsGroupCollapsed(const tab_groups::TabGroupId& group) const override {
    return tab_strip_controller_->IsGroupCollapsed(group);
  }

  std::optional<int> GetFirstTabInGroup(
      const tab_groups::TabGroupId& group) const override {
    return tab_strip_controller_->GetFirstTabInGroup(group);
  }

  gfx::Range ListTabsInGroup(
      const tab_groups::TabGroupId& group) const override {
    return tab_strip_controller_->ListTabsInGroup(group);
  }

  bool CanExtendDragHandle() const override {
    return !tab_strip_controller_->IsFrameCondensed() &&
           !tab_strip_controller_->EverHasVisibleBackgroundTabShapes();
  }

  const views::View* GetTabClosingModeMouseWatcherHostView() const override {
    return nullptr;
  }

  bool IsAnimatingInTabStrip() const override { return false; }

  MOCK_METHOD(void,
              UpdateAnimationTarget,
              (TabSlotView*, gfx::Rect),
              (override));

 private:
  const raw_ref<TabStripController> tab_strip_controller_;
};

void SetTabDataPinned(Tab* tab, TabPinned pinned) {
  TabRendererData tab_data = tab->data();
  tab_data.pinned = pinned == TabPinned::kPinned;
  tab->SetData(tab_data);
}
}  // namespace

class CompoundTabContainerTest : public ChromeViewsTestBase {
 public:
  CompoundTabContainerTest()
      : animation_mode_reset_(gfx::AnimationTestApi::SetRichAnimationRenderMode(
            gfx::Animation::RichAnimationRenderMode::FORCE_ENABLED)) {}
  CompoundTabContainerTest(const CompoundTabContainerTest&) = delete;
  CompoundTabContainerTest& operator=(const CompoundTabContainerTest&) = delete;
  ~CompoundTabContainerTest() override = default;

  void SetUp() override {
    ChromeViewsTestBase::SetUp();

    tab_strip_controller_ = std::make_unique<FakeBaseTabStripController>();
    tab_container_controller_ = std::make_unique<FakeTabContainerController>(
        *(tab_strip_controller_.get()));
    ON_CALL(*tab_container_controller_, UpdateAnimationTarget)
        .WillByDefault(testing::Return());
    tab_slot_controller_ =
        std::make_unique<FakeTabSlotController>(tab_strip_controller_.get());

    std::unique_ptr<TabDragContextBase> drag_context =
        std::make_unique<FakeTabDragContext>();
    std::unique_ptr<CompoundTabContainer> tab_container =
        std::make_unique<CompoundTabContainer>(
            *tab_container_controller_.get(), nullptr /*hover_card_controller*/,
            drag_context.get(), *(tab_slot_controller_.get()),
            nullptr /*scroll_contents_view*/);
    tab_container->SetAvailableWidthCallback(base::BindRepeating(
        [](CompoundTabContainerTest* test) {
          return test->tab_container_width_;
        },
        this));

    widget_ =
        CreateTestWidget(views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET);
    tab_container_ =
        widget_->GetRootView()->AddChildView(std::move(tab_container));
    drag_context_ =
        widget_->GetRootView()->AddChildView(std::move(drag_context));
    SetTabContainerWidth(1000);

    tab_slot_controller_->set_tab_container(tab_container_);
  }

  void TearDown() override {
    drag_context_ = nullptr;
    tab_container_ = nullptr;
    widget_.reset();
    tab_slot_controller_.reset();
    tab_container_controller_.reset();
    tab_strip_controller_.reset();

    ChromeViewsTestBase::TearDown();
  }

 protected:
  Tab* AddTab(int model_index,
              TabPinned pinned,
              std::optional<tab_groups::TabGroupId> group = std::nullopt,
              TabActive active = TabActive::kInactive) {
    std::vector<TabContainer::TabInsertionParams> tabs_params;
    tabs_params.emplace_back(std::make_unique<Tab>(tab_slot_controller_.get()),
                             model_index, pinned);

    Tab* tab = tab_container_->AddTabs(std::move(tabs_params))[0];
    tab_strip_controller_->AddTab(model_index, active, pinned);

    if (active == TabActive::kActive) {
      tab_slot_controller_->set_active_tab(tab);
    }

    if (group) {
      AddTabToGroup(model_index, group.value());
    }

    SetTabDataPinned(tab, pinned);

    return tab;
  }

  // Removes the tab from the viewmodel, but leaves the Tab view itself around
  // so it can animate closed.
  void RemoveTab(int model_index) {
    bool was_active =
        tab_container_->GetTabAtModelIndex(model_index)->IsActive();
    tab_strip_controller_->RemoveTab(model_index);
    tab_container_->RemoveTab(model_index, was_active);
  }

  void AddTabToGroup(int model_index, tab_groups::TabGroupId group) {
    tab_container_->GetTabAtModelIndex(model_index)->SetGroup(group);
    tab_strip_controller_->AddTabToGroup(model_index, group);

    const auto& group_views = tab_container_->get_group_views_for_testing();
    if (group_views.find(group) == group_views.end()) {
      tab_container_->OnGroupCreated(group);
    }

    tab_container_->OnGroupMoved(group);
  }

  void SetTabContainerWidth(int width) {
    tab_container_width_ = width;
    gfx::Size size(tab_container_width_, GetLayoutConstant(TAB_STRIP_HEIGHT));
    widget_->SetSize(size);
    drag_context_->SetSize(size);
    tab_container_->SetSize(size);
  }

  int GetWidthOfActiveTab() {
    return tab_container_
        ->GetTabAtModelIndex(tab_strip_controller_->GetActiveIndex().value())
        ->width();
  }

  std::unique_ptr<FakeBaseTabStripController> tab_strip_controller_;
  std::unique_ptr<FakeTabContainerController> tab_container_controller_;
  std::unique_ptr<FakeTabSlotController> tab_slot_controller_;
  raw_ptr<TabDragContextBase> drag_context_;
  raw_ptr<CompoundTabContainer> tab_container_;
  std::unique_ptr<views::Widget> widget_;

  // Used to force animation on, so that tabs aren't deleted immediately on
  // removal.
  gfx::AnimationTestApi::RenderModeResetter animation_mode_reset_;

  int tab_container_width_ = 0;
};

TEST_F(CompoundTabContainerTest, PinnedTabReparents) {
  // Start with one tab, initially pinned.
  Tab* const tab = AddTab(0, TabPinned::kPinned);
  TabContainer* const pinned_container =
      views::AsViewClass<TabContainer>(tab->parent());
  ASSERT_NE(pinned_container, nullptr);

  // Unpin the tab and it should move to the compound container for animation.
  SetTabDataPinned(tab, TabPinned::kUnpinned);
  tab_container_->SetTabPinned(0, TabPinned::kUnpinned);
  EXPECT_EQ(tab->parent(), tab_container_);

  // Complete the animation and it should move to the other TabContainer.
  tab_container_->CompleteAnimationAndLayout();
  TabContainer* const unpinned_container =
      views::AsViewClass<TabContainer>(tab->parent());
  ASSERT_NE(unpinned_container, nullptr);
  EXPECT_NE(pinned_container, unpinned_container);

  // Re-pin the tab and it should animate in the compound container again.
  SetTabDataPinned(tab, TabPinned::kPinned);
  tab_container_->SetTabPinned(0, TabPinned::kPinned);
  EXPECT_EQ(tab->parent(), tab_container_);

  // Complete animation and it should be back in the pinned container.
  tab_container_->CompleteAnimationAndLayout();
  EXPECT_EQ(tab->parent(), pinned_container);
}

TEST_F(CompoundTabContainerTest, PinDuringUnpinAnimation) {
  // Start with one tab, initially pinned.
  Tab* const tab = AddTab(0, TabPinned::kPinned);
  TabContainer* const pinned_container =
      views::AsViewClass<TabContainer>(tab->parent());
  ASSERT_NE(pinned_container, nullptr);

  // Unpin the tab and it should move to the compound container for animation.
  SetTabDataPinned(tab, TabPinned::kUnpinned);
  tab_container_->SetTabPinned(0, TabPinned::kUnpinned);
  EXPECT_EQ(tab->parent(), tab_container_);

  // Re-pin the tab and it should still be in the compound container.
  SetTabDataPinned(tab, TabPinned::kPinned);
  tab_container_->SetTabPinned(0, TabPinned::kPinned);
  EXPECT_EQ(tab->parent(), tab_container_);

  // Complete animation and it should be back in the pinned container.
  tab_container_->CompleteAnimationAndLayout();
  EXPECT_EQ(tab->parent(), pinned_container);
}

TEST_F(CompoundTabContainerTest, MoveTabsWithinContainers) {
  // Start with two tabs each pinned and unpinned.
  const Tab* const tab0 = AddTab(0, TabPinned::kPinned);
  const Tab* const tab1 = AddTab(1, TabPinned::kPinned);
  const Tab* const tab2 = AddTab(2, TabPinned::kUnpinned);
  const Tab* const tab3 = AddTab(3, TabPinned::kUnpinned);

  // Swap each pair.
  tab_container_->MoveTab(0, 1);
  EXPECT_EQ(tab_container_->GetTabAtModelIndex(0), tab1);
  EXPECT_EQ(tab_container_->GetTabAtModelIndex(1), tab0);

  tab_container_->MoveTab(2, 3);
  EXPECT_EQ(tab_container_->GetTabAtModelIndex(2), tab3);
  EXPECT_EQ(tab_container_->GetTabAtModelIndex(3), tab2);

  // And back again.
  tab_container_->MoveTab(1, 0);
  EXPECT_EQ(tab_container_->GetTabAtModelIndex(0), tab0);
  EXPECT_EQ(tab_container_->GetTabAtModelIndex(1), tab1);

  tab_container_->MoveTab(3, 2);
  EXPECT_EQ(tab_container_->GetTabAtModelIndex(2), tab2);
  EXPECT_EQ(tab_container_->GetTabAtModelIndex(3), tab3);
}

TEST_F(CompoundTabContainerTest, MoveTabBetweenContainers) {
  // Start with one pinned tab and two unpinned tabs.
  const views::View* const pinned_container =
      AddTab(0, TabPinned::kPinned)->parent();
  const views::View* const unpinned_container =
      AddTab(1, TabPinned::kUnpinned)->parent();
  Tab* const moving_tab = AddTab(2, TabPinned::kUnpinned);

  // Pin `moving_tab` as part of a move.
  SetTabDataPinned(moving_tab, TabPinned::kPinned);
  tab_container_->MoveTab(2, 1);
  // It should be in the compound container, animating.
  EXPECT_EQ(moving_tab->parent(), tab_container_);
  EXPECT_TRUE(tab_container_->IsAnimating());

  // Finish animating and it should be pinned and at index 1.
  tab_container_->CompleteAnimationAndLayout();
  EXPECT_EQ(moving_tab->parent(), pinned_container);
  EXPECT_EQ(tab_container_->GetTabAtModelIndex(1), moving_tab);

  // Move it to index 0, then unpin it as part of another move.
  tab_container_->MoveTab(1, 0);
  SetTabDataPinned(moving_tab, TabPinned::kUnpinned);
  tab_container_->MoveTab(0, 1);
  // It should be in the compound container, animating.
  EXPECT_EQ(moving_tab->parent(), tab_container_);
  EXPECT_TRUE(tab_container_->IsAnimating());

  // It should be unpinned and at index 1.
  tab_container_->CompleteAnimationAndLayout();
  EXPECT_EQ(moving_tab->parent(), unpinned_container);
  EXPECT_EQ(tab_container_->GetTabAtModelIndex(1), moving_tab);
}

TEST_F(CompoundTabContainerTest, RemoveTab) {
  // Start with two pinned tabs and two unpinned tabs.
  AddTab(0, TabPinned::kPinned);
  AddTab(1, TabPinned::kPinned);
  AddTab(2, TabPinned::kUnpinned);
  AddTab(3, TabPinned::kUnpinned);

  // Remove the last tab.
  RemoveTab(3);
  EXPECT_EQ(tab_container_->GetTabCount(), 3);
  // Remove the middle tab.
  RemoveTab(1);
  EXPECT_EQ(tab_container_->GetTabCount(), 2);
  // Remove the first tab.
  RemoveTab(0);
  EXPECT_EQ(tab_container_->GetTabCount(), 1);
  // Remove the only remaining tab.
  RemoveTab(0);
  EXPECT_EQ(tab_container_->GetTabCount(), 0);
}

TEST_F(CompoundTabContainerTest, GetIndexOfFirstNonClosingTab) {
  // Test that CompoundTabContainer can identify the tab events should be
  // forwarded to in case one is closing.

  // Create a tabstrip with four tabs.
  Tab* first_pinned = AddTab(0, TabPinned::kPinned);
  AddTab(1, TabPinned::kPinned);
  Tab* first_unpinned = AddTab(2, TabPinned::kUnpinned);
  AddTab(3, TabPinned::kUnpinned);

  // RemoveTab below *starts* the tab removal process, but leaves the view
  // around to be animated closed.

  // Remove `first_unpinned`, so the next non-closing tab is the other unpinned
  // tab, i.e. both tabs are in `unpinned_tab_container_`.
  RemoveTab(2);
  EXPECT_EQ(tab_container_->GetModelIndexOfFirstNonClosingTab(first_unpinned),
            2);

  // Both tabs are in `pinned_tab_container_`.
  RemoveTab(0);
  EXPECT_EQ(tab_container_->GetModelIndexOfFirstNonClosingTab(first_pinned), 0);

  // One tab is in each container.
  RemoveTab(0);
  EXPECT_EQ(tab_container_->GetModelIndexOfFirstNonClosingTab(first_pinned), 0);

  // There is no next tab, and this one is unpinned.
  RemoveTab(0);
  EXPECT_EQ(tab_container_->GetModelIndexOfFirstNonClosingTab(first_unpinned),
            std::nullopt);

  // There is no next tab, and this one is pinned.
  EXPECT_EQ(tab_container_->GetModelIndexOfFirstNonClosingTab(first_pinned),
            std::nullopt);
}

TEST_F(CompoundTabContainerTest, ExitsClosingModeAtStandardWidth) {
  AddTab(0, TabPinned::kUnpinned, std::nullopt, TabActive::kActive);

  // Create just enough tabs so tabs are not full size.
  const int standard_width =
      TabStyle::Get()->GetStandardWidth(/*is_split*/ false);
  while (tab_container_->GetTabAtModelIndex(0)->width() == standard_width) {
    AddTab(0, TabPinned::kUnpinned);
    tab_container_->CompleteAnimationAndLayout();
  }

  // The test closes two tabs, we need at least one left over after that.
  ASSERT_GE(tab_container_->GetTabCount(), 3);

  // Enter tab closing mode manually; this would normally happen as the result
  // of a mouse/touch-based tab closure action.
  tab_container_->EnterTabClosingMode(std::nullopt, CloseTabSource::kFromMouse);

  // Close the second-to-last tab; tab closing mode should remain active,
  // constraining tab widths to below full size.
  RemoveTab(tab_container_->GetTabCount() - 2);
  tab_container_->CompleteAnimationAndLayout();
  ASSERT_LT(GetWidthOfActiveTab(), standard_width);

  // Close the last tab; tab closing mode should allow tabs to resize to full
  // size.
  RemoveTab(tab_container_->GetTabCount() - 1);
  tab_container_->CompleteAnimationAndLayout();
  EXPECT_EQ(GetWidthOfActiveTab(), standard_width);
}

TEST_F(CompoundTabContainerTest, ClosingPinnedTabsEngagesClosingMode) {
  // This test verifies that tab closing mode engages correctly when closing a
  // pinned tab.

  // Add two unpinned tabs to be governed by closing mode.
  AddTab(0, TabPinned::kUnpinned, std::nullopt, TabActive::kActive);
  AddTab(1, TabPinned::kUnpinned, std::nullopt, TabActive::kInactive);

  // Create just enough (pinned) tabs so the active tab is not full size.
  const int standard_width =
      TabStyle::Get()->GetStandardWidth(/*is_split*/ false);
  while (tab_container_->GetTabAtModelIndex(tab_container_->GetTabCount() - 1)
             ->width() == standard_width) {
    AddTab(0, TabPinned::kPinned, std::nullopt, TabActive::kInactive);
    tab_container_->CompleteAnimationAndLayout();
  }

  // The test closes two tabs, we need at least one left over after that.
  ASSERT_GE(tab_container_->GetTabCount(), 3);

  // Enter tab closing mode manually; this would normally happen as the result
  // of a mouse/touch-based tab closure action.
  tab_container_->EnterTabClosingMode(std::nullopt, CloseTabSource::kFromMouse);

  // Close the third-to-last tab, which is the last pinned tab; tab closing mode
  // should constrain tab widths to below full size.
  RemoveTab(tab_container_->GetTabCount() - 3);
  tab_container_->CompleteAnimationAndLayout();
  ASSERT_LT(GetWidthOfActiveTab(), standard_width);

  // Close the last tab, which is the inactive unpinned tab; tab closing mode
  // should allow tabs to resize to full size.
  RemoveTab(tab_container_->GetTabCount() - 1);
  tab_container_->CompleteAnimationAndLayout();
  EXPECT_EQ(GetWidthOfActiveTab(), standard_width);
}

TEST_F(CompoundTabContainerTest, ExitsClosingModeWhenClosingLastUnpinnedTab) {
  // Add two unpinned tabs to be governed by closing mode.
  AddTab(0, TabPinned::kUnpinned, std::nullopt, TabActive::kInactive);
  AddTab(1, TabPinned::kUnpinned, std::nullopt, TabActive::kActive);

  // Create just enough (pinned) tabs so the active tab is not full size.
  const int standard_width =
      TabStyle::Get()->GetStandardWidth(/*is_split*/ false);
  while (tab_container_->GetTabAtModelIndex(tab_container_->GetTabCount() - 1)
             ->width() == standard_width) {
    AddTab(0, TabPinned::kPinned);
    tab_container_->CompleteAnimationAndLayout();
  }

  // The test closes two tabs, we need at least one left over after that.
  ASSERT_GE(tab_container_->GetTabCount(), 3);

  // Enter tab closing mode manually; this would normally happen as the result
  // of a mouse/touch-based tab closure action.
  tab_container_->EnterTabClosingMode(std::nullopt, CloseTabSource::kFromMouse);

  // Close the second-to-last tab, which is the inactive unpinned tab; tab
  // closing mode should remain active, constraining tab widths to below full
  // size.
  RemoveTab(tab_container_->GetTabCount() - 2);
  tab_container_->CompleteAnimationAndLayout();
  ASSERT_LT(GetWidthOfActiveTab(), standard_width);

  // Close the last tab, which is the active unpinned tab; tab closing mode
  // should exit.
  RemoveTab(tab_container_->GetTabCount() - 1);
  tab_container_->CompleteAnimationAndLayout();
  EXPECT_FALSE(tab_container_->InTabClose());
}

TEST_F(CompoundTabContainerTest, UpdateAnimationTarget) {
  using testing::Return;

  gfx::Rect animation_target(10, 10);

  // Start with one unpinned tab.
  Tab* tab = AddTab(0, TabPinned::kUnpinned);
  // Verify that animation target updates for unpinned container are unchanged
  // when there are no pinned tabs.
  EXPECT_CALL(*tab_container_controller_,
              UpdateAnimationTarget(testing::_, animation_target))
      .WillOnce(Return());
  tab_container_->UpdateAnimationTarget(tab, animation_target,
                                        TabPinned::kUnpinned);

  // Add a pinned tab.
  AddTab(0, TabPinned::kPinned);
  // Verify that animation target updates for pinned container are unchanged.
  EXPECT_CALL(*tab_container_controller_,
              UpdateAnimationTarget(testing::_, animation_target))
      .WillOnce(Return());
  tab_container_->UpdateAnimationTarget(tab, animation_target,
                                        TabPinned::kPinned);

  // Verify that animation target updates for unpinned container are adjusted
  // when there are pinned tabs.
  EXPECT_CALL(*tab_container_controller_,
              UpdateAnimationTarget(testing::_, testing::Ne(animation_target)))
      .WillOnce(Return());
  tab_container_->UpdateAnimationTarget(tab, animation_target,
                                        TabPinned::kUnpinned);
}

TEST_F(CompoundTabContainerTest, SubContainersOverlap) {
  // With only pinned tabs, the compound container should match the pinned
  // container's width.
  views::View* const pinned_container = AddTab(0, TabPinned::kPinned)->parent();
  tab_container_->CompleteAnimationAndLayout();
  EXPECT_EQ(tab_container_->GetPreferredSize().width(),
            pinned_container->GetPreferredSize().width());
  EXPECT_EQ(tab_container_->GetMinimumSize().width(),
            pinned_container->GetMinimumSize().width());
  EXPECT_EQ(pinned_container->bounds().width(),
            pinned_container->GetPreferredSize().width());

  // With both subcontainers nonempty, the compound container's width should be
  // less than the sum of its parts.
  views::View* const unpinned_container =
      AddTab(1, TabPinned::kUnpinned)->parent();
  tab_container_->CompleteAnimationAndLayout();
  EXPECT_LT(tab_container_->GetPreferredSize().width(),
            pinned_container->GetPreferredSize().width() +
                unpinned_container->GetPreferredSize().width());
  EXPECT_LT(tab_container_->GetMinimumSize().width(),
            pinned_container->GetMinimumSize().width() +
                unpinned_container->GetMinimumSize().width());
  // And the two containers should overlap.
  EXPECT_LT(unpinned_container->bounds().x(),
            pinned_container->bounds().right());

  // Same as case 1, but reversed.
  RemoveTab(0);
  tab_container_->CompleteAnimationAndLayout();
  EXPECT_EQ(tab_container_->GetPreferredSize().width(),
            unpinned_container->GetPreferredSize().width());
  EXPECT_EQ(tab_container_->GetMinimumSize().width(),
            unpinned_container->GetMinimumSize().width());
  EXPECT_EQ(unpinned_container->bounds().width(),
            unpinned_container->GetPreferredSize().width());
}

TEST_F(CompoundTabContainerTest, AvailableWidth) {
  views::View* const pinned_container = AddTab(0, TabPinned::kPinned)->parent();
  views::View* const unpinned_container =
      AddTab(1, TabPinned::kUnpinned)->parent();

  // `pinned_container` gets as much space as we can give it - in this test
  // harness, that's `tab_container_`'s width.
  EXPECT_EQ(tab_container_->GetAvailableSize(pinned_container).width().value(),
            tab_container_->width());

  // `unpinned_container` doesn't, because `pinned_container` has some reserved.
  EXPECT_LT(
      tab_container_->GetAvailableSize(unpinned_container).width().value(),
      tab_container_->width());

  // Because of the overlap, `unpinned_container` should have slightly more
  // available width than `(total available - pinned_container reserved width)`.
  EXPECT_GT(
      tab_container_->GetAvailableSize(unpinned_container).width().value(),
      tab_container_->width() - pinned_container->GetPreferredSize().width());
}

TEST_F(CompoundTabContainerTest, GetEventAndTooltipHandlerForOverlappingArea) {
  Tab* const pinned_tab = AddTab(0, TabPinned::kPinned);
  views::View* const pinned_container = pinned_tab->parent();
  Tab* const unpinned_tab = AddTab(1, TabPinned::kUnpinned);
  views::View* const unpinned_container = unpinned_tab->parent();
  tab_container_->CompleteAnimationAndLayout();

  // Points squarely in each tab should be handled by the tab.
  EXPECT_EQ(pinned_tab, tab_container_->GetEventHandlerForPoint(
                            pinned_container->bounds().CenterPoint()));
  LOG(ERROR) << tab_container_
                    ->GetEventHandlerForPoint(
                        pinned_container->bounds().CenterPoint())
                    ->GetClassName();
  EXPECT_EQ(pinned_tab, tab_container_->GetTooltipHandlerForPoint(
                            pinned_container->bounds().CenterPoint()));
  EXPECT_EQ(unpinned_tab, tab_container_->GetEventHandlerForPoint(
                              unpinned_container->bounds().CenterPoint()));
  EXPECT_EQ(unpinned_tab, tab_container_->GetTooltipHandlerForPoint(
                              unpinned_container->bounds().CenterPoint()));

  auto averagePoint = [](gfx::Point point, gfx::Point other) {
    return gfx::Point((point.x() + other.x()) / 2, (point.y() + other.y()) / 2);
  };

  const gfx::Point pinned_container_right =
      pinned_container->bounds().right_center();
  const gfx::Point unpinned_container_left =
      unpinned_container->bounds().left_center();
  const gfx::Point center =
      averagePoint(pinned_container_right, unpinned_container_left);

  // A point in the overlap area, but left of the tab divider between the two
  // containers, should go to the pinned container.
  const gfx::Point pinned_overlap_test_point =
      averagePoint(center, unpinned_container_left);
  EXPECT_EQ(pinned_tab,
            tab_container_->GetEventHandlerForPoint(pinned_overlap_test_point));
  EXPECT_EQ(pinned_tab, tab_container_->GetTooltipHandlerForPoint(
                            pinned_overlap_test_point));

  // A point in the overlap area, but right of the tab divider between the two
  // containers, should go to the unpinned container.
  const gfx::Point unpinned_overlap_test_point =
      averagePoint(center, pinned_container_right);
  EXPECT_EQ(unpinned_tab, tab_container_->GetEventHandlerForPoint(
                              unpinned_overlap_test_point));
  EXPECT_EQ(unpinned_tab, tab_container_->GetTooltipHandlerForPoint(
                              unpinned_overlap_test_point));
}

namespace {
ui::DropTargetEvent MakeEventForDragLocation(const gfx::Point& p) {
  return ui::DropTargetEvent({}, gfx::PointF(p), {},
                             ui::DragDropTypes::DRAG_LINK);
}
}  // namespace

TEST_F(CompoundTabContainerTest, DropIndexForDragLocationIsCorrect) {
  auto group = tab_groups::TabGroupId::GenerateNew();
  const Tab* const tab1 =
      AddTab(0, TabPinned::kPinned, std::nullopt, TabActive::kActive);
  const Tab* const tab2 = AddTab(1, TabPinned::kUnpinned, group);
  const Tab* const tab3 = AddTab(2, TabPinned::kUnpinned, group);
  tab_container_->CompleteAnimationAndLayout();

  const TabGroupHeader* const group_header =
      tab_container_->GetGroupViews(group)->header();

  using DropIndex = BrowserRootView::DropIndex;
  using BrowserRootView::DropIndex::GroupInclusion::kDontIncludeInGroup;
  using BrowserRootView::DropIndex::GroupInclusion::kIncludeInGroup;
  using BrowserRootView::DropIndex::RelativeToIndex::kInsertBeforeIndex;
  using BrowserRootView::DropIndex::RelativeToIndex::kReplaceIndex;

  const auto bounds_in_ctc = [this](const views::View* view) {
    return ToEnclosingRect(views::View::ConvertRectToTarget(
        view, tab_container_, gfx::RectF(view->GetLocalBounds())));
  };

  // Check dragging near the edge of each tab.
  EXPECT_EQ((DropIndex{.index = 0,
                       .relative_to_index = kInsertBeforeIndex,
                       .group_inclusion = kDontIncludeInGroup}),
            tab_container_->GetDropIndex(MakeEventForDragLocation(
                bounds_in_ctc(tab1).left_center() + gfx::Vector2d(1, 0))));
  EXPECT_EQ((DropIndex{.index = 1,
                       .relative_to_index = kInsertBeforeIndex,
                       .group_inclusion = kDontIncludeInGroup}),
            tab_container_->GetDropIndex(MakeEventForDragLocation(
                bounds_in_ctc(tab1).right_center() + gfx::Vector2d(-1, 0))));
  EXPECT_EQ((DropIndex{.index = 1,
                       .relative_to_index = kInsertBeforeIndex,
                       .group_inclusion = kIncludeInGroup}),
            tab_container_->GetDropIndex(MakeEventForDragLocation(
                bounds_in_ctc(tab2).left_center() + gfx::Vector2d(1, 0))));
  EXPECT_EQ((DropIndex{.index = 2,
                       .relative_to_index = kInsertBeforeIndex,
                       .group_inclusion = kDontIncludeInGroup}),
            tab_container_->GetDropIndex(MakeEventForDragLocation(
                bounds_in_ctc(tab2).right_center() + gfx::Vector2d(-1, 0))));
  EXPECT_EQ((DropIndex{.index = 2,
                       .relative_to_index = kInsertBeforeIndex,
                       .group_inclusion = kDontIncludeInGroup}),
            tab_container_->GetDropIndex(MakeEventForDragLocation(
                bounds_in_ctc(tab3).left_center() + gfx::Vector2d(1, 0))));
  EXPECT_EQ((DropIndex{.index = 3,
                       .relative_to_index = kInsertBeforeIndex,
                       .group_inclusion = kDontIncludeInGroup}),
            tab_container_->GetDropIndex(MakeEventForDragLocation(
                bounds_in_ctc(tab3).right_center() + gfx::Vector2d(-1, 0))));

  // Check dragging in the center of each tab.
  EXPECT_EQ((DropIndex{.index = 0,
                       .relative_to_index = kReplaceIndex,
                       .group_inclusion = kDontIncludeInGroup}),
            tab_container_->GetDropIndex(
                MakeEventForDragLocation(bounds_in_ctc(tab1).CenterPoint())));
  EXPECT_EQ((DropIndex{.index = 1,
                       .relative_to_index = kReplaceIndex,
                       .group_inclusion = kDontIncludeInGroup}),
            tab_container_->GetDropIndex(
                MakeEventForDragLocation(bounds_in_ctc(tab2).CenterPoint())));
  EXPECT_EQ((DropIndex{.index = 2,
                       .relative_to_index = kReplaceIndex,
                       .group_inclusion = kDontIncludeInGroup}),
            tab_container_->GetDropIndex(
                MakeEventForDragLocation(bounds_in_ctc(tab3).CenterPoint())));

  // Check dragging over group header.
  // The left half of the header should drop outside the group.
  EXPECT_EQ(
      (DropIndex{.index = 1,
                 .relative_to_index = kInsertBeforeIndex,
                 .group_inclusion = kDontIncludeInGroup}),
      tab_container_->GetDropIndex(MakeEventForDragLocation(
          bounds_in_ctc(group_header).CenterPoint() + gfx::Vector2d(-1, 0))));
  // The right half of the header should drop inside the group.
  EXPECT_EQ(
      (DropIndex{.index = 1,
                 .relative_to_index = kInsertBeforeIndex,
                 .group_inclusion = kIncludeInGroup}),
      tab_container_->GetDropIndex(MakeEventForDragLocation(
          bounds_in_ctc(group_header).CenterPoint() + gfx::Vector2d(1, 0))));
}