File: pinned_toolbar_actions_container_unittest.cc

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (715 lines) | stat: -rw-r--r-- 31,162 bytes parent folder | download | duplicates (4)
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
// Copyright 2023 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/toolbar/pinned_toolbar_actions_container.h"

#include <vector>

#include "base/functional/bind.h"
#include "base/test/metrics/action_suffix_reader.h"
#include "chrome/browser/history/history_service_factory.h"
#include "chrome/browser/ui/actions/chrome_action_id.h"
#include "chrome/browser/ui/actions/chrome_actions.h"
#include "chrome/browser/ui/browser_actions.h"
#include "chrome/browser/ui/browser_element_identifiers.h"
#include "chrome/browser/ui/toolbar/pinned_toolbar/pinned_toolbar_actions_model.h"
#include "chrome/browser/ui/toolbar/pinned_toolbar/pinned_toolbar_actions_model_factory.h"
#include "chrome/browser/ui/toolbar/toolbar_pref_names.h"
#include "chrome/browser/ui/ui_features.h"
#include "chrome/browser/ui/views/frame/browser_view.h"
#include "chrome/browser/ui/views/frame/test_with_browser_view.h"
#include "chrome/browser/ui/views/toolbar/pinned_action_toolbar_button.h"
#include "chrome/browser/ui/views/toolbar/pinned_toolbar_actions_container_layout.h"
#include "chrome/browser/ui/views/toolbar/pinned_toolbar_button_status_indicator.h"
#include "chrome/browser/ui/views/toolbar/toolbar_button.h"
#include "chrome/browser/ui/views/toolbar/toolbar_view.h"
#include "chrome/grit/generated_resources.h"
#include "chrome/test/base/browser_with_test_window_test.h"
#include "components/keyed_service/core/keyed_service.h"
#include "components/vector_icons/vector_icons.h"
#include "content/public/browser/browser_context.h"
#include "ui/actions/action_id.h"
#include "ui/actions/actions.h"
#include "ui/base/dragdrop/drag_drop_types.h"
#include "ui/base/dragdrop/mojom/drag_drop_types.mojom-shared.h"
#include "ui/base/dragdrop/mojom/drag_drop_types.mojom.h"
#include "ui/compositor/layer_tree_owner.h"
#include "ui/events/base_event_utils.h"
#include "ui/views/accessibility/view_accessibility.h"
#include "ui/views/layout/animating_layout_manager_test_util.h"

class PinnedToolbarActionsContainerTest : public TestWithBrowserView {
 public:
  void SetUp() override {
    InitializeActionIdStringMapping();
    TestWithBrowserView::SetUp();
    AddTab(browser_view()->browser(), GURL("http://foo1.com"));
    browser_view()->browser()->tab_strip_model()->ActivateTabAt(0);

    model_ = PinnedToolbarActionsModel::Get(profile());
    ASSERT_TRUE(model_);

    model_->UpdatePinnedState(kActionShowChromeLabs, false);
    if (features::HasTabSearchToolbarButton()) {
      model_->UpdatePinnedState(kActionTabSearch, false);
    }
    WaitForAnimations();
  }

  void TearDown() override {
    model_ = nullptr;
    actions::ActionIdMap::ResetMapsForTesting();
    TestWithBrowserView::TearDown();
  }

  TestingProfile::TestingFactories GetTestingFactories() override {
    TestingProfile::TestingFactories factories =
        TestWithBrowserView::GetTestingFactories();
    factories.emplace_back(
        PinnedToolbarActionsModelFactory::GetInstance(),
        base::BindRepeating(&PinnedToolbarActionsContainerTest::
                                BuildPinnedToolbarActionsModel));
    factories.emplace_back(HistoryServiceFactory::GetInstance(),
                           HistoryServiceFactory::GetDefaultFactory());
    return factories;
  }

  static std::unique_ptr<KeyedService> BuildPinnedToolbarActionsModel(
      content::BrowserContext* context) {
    return std::make_unique<PinnedToolbarActionsModel>(
        Profile::FromBrowserContext(context));
  }

  std::vector<PinnedActionToolbarButton*> GetChildToolbarButtons() {
    std::vector<PinnedActionToolbarButton*> result;
    for (views::View* child : container()->children()) {
      if (views::Button::AsButton(child)) {
        PinnedActionToolbarButton* button =
            static_cast<PinnedActionToolbarButton*>(child);
#if BUILDFLAG(IS_MAC)
        // TODO(crbug.com/40670141): Query the model on whether this button
        // should be visible since buttons in the toolbar are only removed after
        // animations finish running, which is not reliable in unit tests on
        // Mac.
        if (!container()->IsActionPinnedOrPoppedOut(button->GetActionId())) {
          continue;
        }
#endif
        result.push_back(button);
      }
    }
    return result;
  }

  void CheckIsPoppedOut(actions::ActionId id, bool should_be_popped_out) {
    auto* container =
        browser_view()->toolbar()->pinned_toolbar_actions_container();
    if (should_be_popped_out) {
      ASSERT_NE(std::ranges::find(container->popped_out_buttons_, id,
                                  [](PinnedActionToolbarButton* button) {
                                    return button->GetActionId();
                                  }),
                container->popped_out_buttons_.end());
    } else {
      ASSERT_EQ(std::ranges::find(container->popped_out_buttons_, id,
                                  [](PinnedActionToolbarButton* button) {
                                    return button->GetActionId();
                                  }),
                container->popped_out_buttons_.end());
    }
  }

  void CheckIsPinned(actions::ActionId id, bool should_be_pinned) {
    auto* container =
        browser_view()->toolbar()->pinned_toolbar_actions_container();
    if (should_be_pinned) {
      ASSERT_NE(std::ranges::find(container->pinned_buttons_, id,
                                  [](PinnedActionToolbarButton* button) {
                                    return button->GetActionId();
                                  }),
                container->pinned_buttons_.end());
    } else {
      ASSERT_EQ(std::ranges::find(container->pinned_buttons_, id,
                                  [](PinnedActionToolbarButton* button) {
                                    return button->GetActionId();
                                  }),
                container->pinned_buttons_.end());
    }
  }

  void UpdateActionItem(const actions::ActionId& id) {
    auto* action = actions::ActionManager::Get().FindAction(
        id, browser_view()->browser()->browser_actions()->root_action_item());
    action->SetText(u"Test Action");
    action->SetTooltipText(u"Test Action");
    action->SetImage(
        ui::ImageModel::FromVectorIcon(vector_icons::kDogfoodIcon));
    action->SetVisible(true);
    action->SetEnabled(true);
    action->SetProperty(actions::kActionItemPinnableKey,
                        std::underlying_type_t<actions::ActionPinnableState>(
                            actions::ActionPinnableState::kPinnable));
    action->SetInvokeActionCallback(base::DoNothing());
  }

  void UpdatePref(const std::vector<actions::ActionId>& updated_list) {
    ScopedListPrefUpdate update(profile()->GetPrefs(), prefs::kPinnedActions);
    base::Value::List& list_of_values = update.Get();
    list_of_values.clear();
    for (auto id : updated_list) {
      const std::optional<std::string>& id_string =
          actions::ActionIdMap::ActionIdToString(id);
      // The ActionId should have a string equivalent.
      CHECK(id_string.has_value());
      list_of_values.Append(id_string.value());
    }
  }

  void WaitForAnimations() {
#if BUILDFLAG(IS_MAC)
    // TODO(crbug.com/40670141): we avoid using animations on Mac due to the
    // lack of support in unit tests. Therefore this is a no-op.
#else
    views::test::WaitForAnimatingLayoutManager(container());
#endif
  }

  PinnedToolbarActionsContainer* container() {
    return browser_view()->toolbar()->pinned_toolbar_actions_container();
  }

  PinnedToolbarActionsModel* model() { return model_.get(); }

  void SendKeyPress(views::View* view,
                    ui::KeyboardCode code,
                    int flags = ui::EF_NONE) {
    view->OnKeyPressed(ui::KeyEvent(ui::EventType::kKeyPressed, code, flags,
                                    ui::EventTimeForNow()));
    view->OnKeyReleased(ui::KeyEvent(ui::EventType::kKeyPressed, code, flags,
                                     ui::EventTimeForNow()));
  }

 private:
  raw_ptr<PinnedToolbarActionsModel> model_;
};

TEST_F(PinnedToolbarActionsContainerTest, ContainerMargins) {
  // Verify margins are correctly set.
  ASSERT_EQ(
      static_cast<PinnedToolbarActionsContainerLayout*>(
          container()->GetAnimatingLayoutManager()->target_layout_manager())
          ->interior_margin()
          .left(),
      0);
  ASSERT_EQ(
      static_cast<PinnedToolbarActionsContainerLayout*>(
          container()->GetAnimatingLayoutManager()->target_layout_manager())
          ->interior_margin()
          .right(),
      -GetLayoutConstant(TOOLBAR_ICON_DEFAULT_MARGIN));
}

TEST_F(PinnedToolbarActionsContainerTest, PinningAndUnpinning) {
  // clang-format on
  UpdateActionItem(actions::kActionCut);

  // Verify there are no pinned buttons.
  auto pinned_buttons = GetChildToolbarButtons();
  ASSERT_EQ(pinned_buttons.size(), 0u);
  // Verify pinning an action adds a button.
  model()->UpdatePinnedState(actions::kActionCut, true);
  pinned_buttons = GetChildToolbarButtons();
  ASSERT_EQ(pinned_buttons.size(), 1u);
  // Check the context menu
  EXPECT_FALSE(pinned_buttons[0]->menu_model()->IsVisibleAt(0));
  EXPECT_TRUE(pinned_buttons[0]->menu_model()->IsVisibleAt(1));
  EXPECT_EQ(
      pinned_buttons[0]->menu_model()->GetLabelAt(1),
      l10n_util::GetStringUTF16(IDS_SIDE_PANEL_TOOLBAR_BUTTON_CXMENU_UNPIN));
  // Verify pressing the toolbar button invokes the action.
  ASSERT_EQ(actions::ActionManager::Get()
                .FindAction(actions::kActionCut)
                ->GetInvokeCount(),
            0);
  pinned_buttons[0]->button_controller()->NotifyClick();
  ASSERT_EQ(actions::ActionManager::Get()
                .FindAction(actions::kActionCut)
                ->GetInvokeCount(),
            1);
  // Verify unpinning an action removes a button.
  model()->UpdatePinnedState(actions::kActionCut, false);
  WaitForAnimations();
  pinned_buttons = GetChildToolbarButtons();
  ASSERT_EQ(pinned_buttons.size(), 0u);
}

TEST_F(PinnedToolbarActionsContainerTest,
       UnpinnedToolbarButtonsPoppedOutWhileActive) {
  UpdateActionItem(actions::kActionCut);

  // Verify there are no pinned buttons.
  auto toolbar_buttons = GetChildToolbarButtons();
  ASSERT_EQ(toolbar_buttons.size(), 0u);
  // Verify activating a button does not pin and adds to popped out buttons.
  container()->UpdateActionState(actions::kActionCut, true);
  CheckIsPoppedOut(actions::kActionCut, true);
  CheckIsPinned(actions::kActionCut, false);
  toolbar_buttons = GetChildToolbarButtons();
  // Check the context menu
  EXPECT_TRUE(toolbar_buttons[0]->menu_model()->IsVisibleAt(0));
  EXPECT_FALSE(toolbar_buttons[0]->menu_model()->IsVisibleAt(1));
  EXPECT_EQ(
      toolbar_buttons[0]->menu_model()->GetLabelAt(0),
      l10n_util::GetStringUTF16(IDS_SIDE_PANEL_TOOLBAR_BUTTON_CXMENU_PIN));
  ASSERT_EQ(toolbar_buttons.size(), 1u);
  // Verify deactivating a button removes it from popped out buttons.
  container()->UpdateActionState(actions::kActionCut, false);
  WaitForAnimations();
  CheckIsPoppedOut(actions::kActionCut, false);
  CheckIsPinned(actions::kActionCut, false);
  toolbar_buttons = GetChildToolbarButtons();
  ASSERT_EQ(toolbar_buttons.size(), 0u);
}

TEST_F(PinnedToolbarActionsContainerTest,
       StateChangesBetweenPinnedandUnpinnedWhileActive) {
  UpdateActionItem(actions::kActionCut);

  // Verify there are no pinned buttons.
  auto toolbar_buttons = GetChildToolbarButtons();
  ASSERT_EQ(toolbar_buttons.size(), 0u);
  // Verify activating a button does not pin and adds to popped out buttons.
  container()->UpdateActionState(actions::kActionCut, true);
  CheckIsPoppedOut(actions::kActionCut, true);
  CheckIsPinned(actions::kActionCut, false);
  toolbar_buttons = GetChildToolbarButtons();
  ASSERT_EQ(toolbar_buttons.size(), 1u);
  // Pin active button and verify state.
  model()->UpdatePinnedState(actions::kActionCut, true);
  CheckIsPoppedOut(actions::kActionCut, false);
  CheckIsPinned(actions::kActionCut, true);
  toolbar_buttons = GetChildToolbarButtons();
  ASSERT_EQ(toolbar_buttons.size(), 1u);
  // Unpin active button and verify state.
  model()->UpdatePinnedState(actions::kActionCut, false);
  WaitForAnimations();
  CheckIsPoppedOut(actions::kActionCut, true);
  CheckIsPinned(actions::kActionCut, false);
  toolbar_buttons = GetChildToolbarButtons();
  ASSERT_EQ(toolbar_buttons.size(), 1u);
}

TEST_F(PinnedToolbarActionsContainerTest, PoppedOutButtonsAreAfterPinned) {
  UpdateActionItem(actions::kActionCut);
  UpdateActionItem(actions::kActionCopy);

  // Verify there are no pinned buttons.
  auto toolbar_buttons = GetChildToolbarButtons();
  ASSERT_EQ(toolbar_buttons.size(), 0u);
  // Pin both and verify order matches the order they were added.
  model()->UpdatePinnedState(actions::kActionCut, true);
  model()->UpdatePinnedState(actions::kActionCopy, true);
  toolbar_buttons = GetChildToolbarButtons();
  ASSERT_EQ(toolbar_buttons.size(), 2u);
  ASSERT_EQ(toolbar_buttons[0]->GetActionId(), actions::kActionCut);
  ASSERT_EQ(toolbar_buttons[1]->GetActionId(), actions::kActionCopy);
  // Make kActionCut popped out instead of pinned and verify order.
  container()->UpdateActionState(actions::kActionCut, true);
  model()->UpdatePinnedState(actions::kActionCut, false);
  WaitForAnimations();
  CheckIsPoppedOut(actions::kActionCut, true);
  CheckIsPinned(actions::kActionCut, false);
  toolbar_buttons = GetChildToolbarButtons();
  ASSERT_EQ(toolbar_buttons.size(), 2u);
  ASSERT_EQ(toolbar_buttons[0]->GetActionId(), actions::kActionCopy);
  ASSERT_EQ(toolbar_buttons[1]->GetActionId(), actions::kActionCut);
}

// TODO(b/40670141): Crashing on Mac due to the default pinned state of Chrome
// Labs button and animations not working properly on mac unittests
#if BUILDFLAG(IS_MAC)
#define MAYBE_DividerNotVisibleWhileButtonPoppedOut \
  DISABLED_DividerNotVisibleWhileButtonPoppedOut
#else
#define MAYBE_DividerNotVisibleWhileButtonPoppedOut \
  DividerNotVisibleWhileButtonPoppedOut
#endif
TEST_F(PinnedToolbarActionsContainerTest,
       MAYBE_DividerNotVisibleWhileButtonPoppedOut) {
  UpdateActionItem(actions::kActionCut);

  // Verify there are no child views visible. Note the divider still exists but
  // should not be visible.
  auto child_views = container()->children();
  ASSERT_EQ(child_views.size(), 1u);
  ASSERT_FALSE(child_views[0]->GetVisible());
  // Make kActionCut popped out and verify order and visibility of the divider.
  container()->UpdateActionState(actions::kActionCut, true);
  CheckIsPoppedOut(actions::kActionCut, true);
  CheckIsPinned(actions::kActionCut, false);
  child_views = container()->children();
  ASSERT_EQ(child_views.size(), 2u);
  ASSERT_EQ(
      static_cast<PinnedActionToolbarButton*>(child_views[1])->GetActionId(),
      actions::kActionCut);
  ASSERT_EQ(child_views[0]->GetProperty(views::kElementIdentifierKey),
            kPinnedToolbarActionsContainerDividerElementId);
  ASSERT_FALSE(child_views[0]->GetVisible());
}

TEST_F(PinnedToolbarActionsContainerTest, AccessibleCheckedState) {
  UpdateActionItem(actions::kActionCut);
  model()->UpdatePinnedState(actions::kActionCut, true);
  auto pinned_action_buttons = GetChildToolbarButtons();

  ui::AXNodeData data;
  pinned_action_buttons[0]->GetViewAccessibility().GetAccessibleNodeData(&data);
  EXPECT_EQ(data.GetCheckedState(), ax::mojom::CheckedState::kFalse);

  data = ui::AXNodeData();
  pinned_action_buttons[0]->AddHighlight();
  pinned_action_buttons[0]->GetViewAccessibility().GetAccessibleNodeData(&data);
  EXPECT_EQ(data.GetCheckedState(), ax::mojom::CheckedState::kTrue);

  data = ui::AXNodeData();
  pinned_action_buttons[0]->ResetHighlight();
  pinned_action_buttons[0]->GetViewAccessibility().GetAccessibleNodeData(&data);
  EXPECT_EQ(data.GetCheckedState(), ax::mojom::CheckedState::kFalse);
}

TEST_F(PinnedToolbarActionsContainerTest, MovingActionsUpdateOrderUsingDrag) {
  UpdateActionItem(actions::kActionCut);
  UpdateActionItem(actions::kActionCopy);

  // Verify there are no pinned buttons.
  auto toolbar_buttons = GetChildToolbarButtons();
  ASSERT_EQ(toolbar_buttons.size(), 0u);
  // Pin both and verify order matches the order they were added.
  model()->UpdatePinnedState(actions::kActionCut, true);
  model()->UpdatePinnedState(actions::kActionCopy, true);
  toolbar_buttons = GetChildToolbarButtons();
  ASSERT_EQ(toolbar_buttons.size(), 2u);
  ASSERT_EQ(toolbar_buttons[0]->GetActionId(), actions::kActionCut);
  ASSERT_EQ(toolbar_buttons[1]->GetActionId(), actions::kActionCopy);
  WaitForAnimations();

  // Drag to reorder the two actions.
  auto* drag_view = toolbar_buttons[1];
  EXPECT_TRUE(
      container()->CanStartDragForView(drag_view, gfx::Point(), gfx::Point()));
  ui::OSExchangeData drag_data;
  container()->WriteDragDataForView(drag_view, gfx::Point(), &drag_data);
  gfx::Point drag_location = toolbar_buttons[0]->bounds().CenterPoint();
  ui::DropTargetEvent drop_event(drag_data, gfx::PointF(drag_location),
                                 gfx::PointF(drag_location),
                                 ui::DragDropTypes::DRAG_MOVE);
  container()->OnDragUpdated(drop_event);
  auto drop_cb = container()->GetDropCallback(drop_event);
  ui::mojom::DragOperation output_drag_op = ui::mojom::DragOperation::kNone;
  std::move(drop_cb).Run(drop_event, output_drag_op,
                         /*drag_image_layer_owner=*/nullptr);
  WaitForAnimations();

  // Verify the order gets updated in the ui.
  toolbar_buttons = GetChildToolbarButtons();
  ASSERT_EQ(toolbar_buttons.size(), 2u);
  ASSERT_EQ(toolbar_buttons[0]->GetActionId(), actions::kActionCopy);
  ASSERT_EQ(toolbar_buttons[1]->GetActionId(), actions::kActionCut);
}

// TODO(crbug.com/40670141): Test currently disabled on MacOS since waiting
// for animations is disabled on there and hence we do not wait for the bounds
// to be updated for the toolbar buttons.
#if !BUILDFLAG(IS_MAC)
TEST_F(PinnedToolbarActionsContainerTest,
       MovingWithExtraActionsInModelUsingDrag) {
  UpdateActionItem(actions::kActionCut);
  UpdateActionItem(actions::kActionCopy);

  // Set pinned state for an action item that isn't registered
  model()->UpdatePinnedState(kActionExit, true);
  model()->UpdatePinnedState(actions::kActionCut, true);
  model()->UpdatePinnedState(actions::kActionCopy, true);

  auto toolbar_buttons = GetChildToolbarButtons();
  ASSERT_EQ(toolbar_buttons.size(), 2u);
  ASSERT_EQ(toolbar_buttons[0]->GetActionId(), actions::kActionCut);
  ASSERT_EQ(toolbar_buttons[1]->GetActionId(), actions::kActionCopy);
  WaitForAnimations();

  // Drag to reorder the two actions.
  auto* drag_view = toolbar_buttons[0];
  EXPECT_TRUE(
      container()->CanStartDragForView(drag_view, gfx::Point(), gfx::Point()));
  ui::OSExchangeData drag_data;
  container()->WriteDragDataForView(drag_view, gfx::Point(), &drag_data);
  gfx::Point drag_location = toolbar_buttons[1]->bounds().CenterPoint();
  ui::DropTargetEvent drop_event(drag_data, gfx::PointF(drag_location),
                                 gfx::PointF(drag_location),
                                 ui::DragDropTypes::DRAG_MOVE);
  container()->OnDragUpdated(drop_event);
  auto drop_cb = container()->GetDropCallback(drop_event);
  ui::mojom::DragOperation output_drag_op = ui::mojom::DragOperation::kNone;
  std::move(drop_cb).Run(drop_event, output_drag_op,
                         /*drag_image_layer_owner=*/nullptr);
  WaitForAnimations();

  // Verify the order gets updated in the ui.
  toolbar_buttons = GetChildToolbarButtons();
  ASSERT_EQ(toolbar_buttons.size(), 2u);
  ASSERT_EQ(toolbar_buttons[0]->GetActionId(), actions::kActionCopy);
  ASSERT_EQ(toolbar_buttons[1]->GetActionId(), actions::kActionCut);
}
#endif

TEST_F(PinnedToolbarActionsContainerTest, ContextMenuPinTest) {
  // clang-format on
  UpdateActionItem(actions::kActionCut);

  // Verify there are no pinned buttons.
  auto pinned_buttons = GetChildToolbarButtons();
  ASSERT_EQ(pinned_buttons.size(), 0u);
  // Verify pinning an action adds a button.
  model()->UpdatePinnedState(actions::kActionCut, true);
  pinned_buttons = GetChildToolbarButtons();
  ASSERT_EQ(pinned_buttons.size(), 1u);
  // Check the context menu. Callback should unpin the button.
  EXPECT_EQ(pinned_buttons[0]->menu_model()->GetItemCount(), 3u);
  EXPECT_FALSE(pinned_buttons[0]->menu_model()->IsVisibleAt(0));
  EXPECT_TRUE(pinned_buttons[0]->menu_model()->IsVisibleAt(1));
  EXPECT_EQ(
      pinned_buttons[0]->menu_model()->GetLabelAt(1),
      l10n_util::GetStringUTF16(IDS_SIDE_PANEL_TOOLBAR_BUTTON_CXMENU_UNPIN));
  EXPECT_EQ(pinned_buttons[0]->menu_model()->GetLabelAt(2),
            l10n_util::GetStringUTF16(IDS_SHOW_CUSTOMIZE_CHROME_TOOLBAR));
  pinned_buttons[0]->menu_model()->ActivatedAt(1);
  WaitForAnimations();
  pinned_buttons = GetChildToolbarButtons();
  ASSERT_EQ(pinned_buttons.size(), 0u);
  // Callback for pop out button should pin the action.
  container()->UpdateActionState(actions::kActionCut, true);
  auto child_views = container()->children();
  auto* pop_out_button =
      static_cast<PinnedActionToolbarButton*>(child_views[1]);
  EXPECT_TRUE(pop_out_button->menu_model()->IsVisibleAt(0));
  EXPECT_FALSE(pop_out_button->menu_model()->IsVisibleAt(1));
  EXPECT_EQ(
      pop_out_button->menu_model()->GetLabelAt(0),
      l10n_util::GetStringUTF16(IDS_SIDE_PANEL_TOOLBAR_BUTTON_CXMENU_PIN));
  pop_out_button->menu_model()->ActivatedAt(0);
  CheckIsPinned(actions::kActionCut, true);
}

TEST_F(PinnedToolbarActionsContainerTest, StatusIndicatorTest) {
  // clang-format on
  UpdateActionItem(actions::kActionCut);

  // Verify there are no pinned buttons.
  auto pinned_buttons = GetChildToolbarButtons();
  ASSERT_EQ(pinned_buttons.size(), 0u);
  // Verify pinning an action adds a button.
  model()->UpdatePinnedState(actions::kActionCut, true);
  pinned_buttons = GetChildToolbarButtons();
  ASSERT_EQ(pinned_buttons.size(), 1u);
  // Check the status indicator. It should not be visible.
  PinnedToolbarButtonStatusIndicator* status_indicator =
      pinned_buttons[0]->GetStatusIndicatorForTesting();
  EXPECT_EQ(status_indicator->GetVisible(), false);
  // Make indicator visible.
  pinned_buttons[0]->SetActionEngaged(true);
  pinned_buttons[0]->UpdateStatusIndicator();
  EXPECT_EQ(status_indicator->GetVisible(), true);
  // Hide indicator.
  pinned_buttons[0]->HideStatusIndicator();
  EXPECT_EQ(status_indicator->GetVisible(), false);
}

TEST_F(PinnedToolbarActionsContainerTest, UpdatesFromSyncUpdateContainer) {
  UpdateActionItem(actions::kActionCut);
  UpdateActionItem(actions::kActionCopy);
  UpdateActionItem(actions::kActionPaste);

  // Verify there are no pinned buttons.
  auto toolbar_buttons = GetChildToolbarButtons();
  ASSERT_EQ(toolbar_buttons.size(), 0u);

  // Simulate an update where 2 actions are added to the prefs object.
  UpdatePref({actions::kActionCut, actions::kActionCopy});
  toolbar_buttons = GetChildToolbarButtons();
  ASSERT_EQ(toolbar_buttons.size(), 2u);
  ASSERT_EQ(toolbar_buttons[0]->GetActionId(), actions::kActionCut);
  ASSERT_EQ(toolbar_buttons[1]->GetActionId(), actions::kActionCopy);

  // Simulate an update where an action is added in between pinned actions.
  UpdatePref(
      {actions::kActionCut, actions::kActionPaste, actions::kActionCopy});
  toolbar_buttons = GetChildToolbarButtons();
  ASSERT_EQ(toolbar_buttons.size(), 3u);
  ASSERT_EQ(toolbar_buttons[0]->GetActionId(), actions::kActionCut);
  ASSERT_EQ(toolbar_buttons[1]->GetActionId(), actions::kActionPaste);
  ASSERT_EQ(toolbar_buttons[2]->GetActionId(), actions::kActionCopy);

  // Simulate an update where an action is removed from the pinned actions list.
  UpdatePref({actions::kActionPaste, actions::kActionCopy});
  WaitForAnimations();
  toolbar_buttons = GetChildToolbarButtons();
  ASSERT_EQ(toolbar_buttons.size(), 2u);
  ASSERT_EQ(toolbar_buttons[0]->GetActionId(), actions::kActionPaste);
  ASSERT_EQ(toolbar_buttons[1]->GetActionId(), actions::kActionCopy);

  // Simulate an update where an action is moved in the pinned actions list.
  UpdatePref({actions::kActionCopy, actions::kActionPaste});
  WaitForAnimations();
  toolbar_buttons = GetChildToolbarButtons();
  ASSERT_EQ(toolbar_buttons.size(), 2u);
  ASSERT_EQ(toolbar_buttons[0]->GetActionId(), actions::kActionCopy);
  ASSERT_EQ(toolbar_buttons[1]->GetActionId(), actions::kActionPaste);
}

TEST_F(PinnedToolbarActionsContainerTest,
       MovingActionsUpdateOrderUsingKeyboard) {
  UpdateActionItem(actions::kActionCut);
  UpdateActionItem(actions::kActionCopy);
  UpdateActionItem(actions::kActionPaste);

  auto* model = PinnedToolbarActionsModel::Get(profile());
  ASSERT_TRUE(model);
  // Verify there are no pinned buttons.
  auto toolbar_buttons = GetChildToolbarButtons();
  ASSERT_EQ(toolbar_buttons.size(), 0u);
  // Pin both and verify order matches the order they were added.
  model->UpdatePinnedState(actions::kActionCut, true);
  model->UpdatePinnedState(actions::kActionCopy, true);
  model->UpdatePinnedState(actions::kActionPaste, true);
  toolbar_buttons = GetChildToolbarButtons();
  ASSERT_EQ(toolbar_buttons.size(), 3u);
  ASSERT_EQ(toolbar_buttons[0]->GetActionId(), actions::kActionCut);
  ASSERT_EQ(toolbar_buttons[1]->GetActionId(), actions::kActionCopy);
  ASSERT_EQ(toolbar_buttons[2]->GetActionId(), actions::kActionPaste);

  constexpr int kModifiedFlag =
#if BUILDFLAG(IS_MAC)
      ui::EF_COMMAND_DOWN;
#else
      ui::EF_CONTROL_DOWN;
#endif

  // Reorder the first actions to the right using keyboard.
  SendKeyPress(toolbar_buttons[0], ui::VKEY_RIGHT, kModifiedFlag);
  toolbar_buttons = GetChildToolbarButtons();
  ASSERT_EQ(toolbar_buttons.size(), 3u);
  ASSERT_EQ(toolbar_buttons[0]->GetActionId(), actions::kActionCopy);
  ASSERT_EQ(toolbar_buttons[1]->GetActionId(), actions::kActionCut);
  ASSERT_EQ(toolbar_buttons[2]->GetActionId(), actions::kActionPaste);

  // Reorder the second actions to the right using keyboard.
  SendKeyPress(toolbar_buttons[1], ui::VKEY_RIGHT, kModifiedFlag);
  toolbar_buttons = GetChildToolbarButtons();
  ASSERT_EQ(toolbar_buttons.size(), 3u);
  ASSERT_EQ(toolbar_buttons[0]->GetActionId(), actions::kActionCopy);
  ASSERT_EQ(toolbar_buttons[1]->GetActionId(), actions::kActionPaste);
  ASSERT_EQ(toolbar_buttons[2]->GetActionId(), actions::kActionCut);

  // Reorder the last actions to the right using keyboard.
  SendKeyPress(toolbar_buttons[2], ui::VKEY_RIGHT, kModifiedFlag);
  toolbar_buttons = GetChildToolbarButtons();
  ASSERT_EQ(toolbar_buttons.size(), 3u);
  ASSERT_EQ(toolbar_buttons[0]->GetActionId(), actions::kActionCopy);
  ASSERT_EQ(toolbar_buttons[1]->GetActionId(), actions::kActionPaste);
  ASSERT_EQ(toolbar_buttons[2]->GetActionId(), actions::kActionCut);

  // Reorder the last actions to the left using keyboard.
  SendKeyPress(toolbar_buttons[2], ui::VKEY_LEFT, kModifiedFlag);
  toolbar_buttons = GetChildToolbarButtons();
  ASSERT_EQ(toolbar_buttons.size(), 3u);
  ASSERT_EQ(toolbar_buttons[0]->GetActionId(), actions::kActionCopy);
  ASSERT_EQ(toolbar_buttons[1]->GetActionId(), actions::kActionCut);
  ASSERT_EQ(toolbar_buttons[2]->GetActionId(), actions::kActionPaste);
}

TEST_F(PinnedToolbarActionsContainerTest,
       ActionRemainsInToolbarWhenSetToBeEphemerallyVisible) {
  UpdateActionItem(actions::kActionCut);

  // Verify there are no buttons.
  auto toolbar_buttons = GetChildToolbarButtons();
  ASSERT_EQ(toolbar_buttons.size(), 0u);
  // Verify setting as ephemerally visible pops out the button.
  container()->ShowActionEphemerallyInToolbar(actions::kActionCut, true);
  CheckIsPoppedOut(actions::kActionCut, true);
  CheckIsPinned(actions::kActionCut, false);
  // Verify pinning the button switches it to pinned.
  model()->UpdatePinnedState(actions::kActionCut, true);
  CheckIsPoppedOut(actions::kActionCut, false);
  CheckIsPinned(actions::kActionCut, true);
  // Verify it is still pinned when it does not need to be ephemerally shown.
  container()->ShowActionEphemerallyInToolbar(actions::kActionCut, false);
  CheckIsPoppedOut(actions::kActionCut, false);
  CheckIsPinned(actions::kActionCut, true);
  // Set as ephemerally visible again and verify it is still popped out when
  // unpinned.
  container()->ShowActionEphemerallyInToolbar(actions::kActionCut, true);
  model()->UpdatePinnedState(actions::kActionCut, false);
  CheckIsPoppedOut(actions::kActionCut, true);
  CheckIsPinned(actions::kActionCut, false);
  // Verify setting as not ephemerally visible remove the popped out button.
  container()->ShowActionEphemerallyInToolbar(actions::kActionCut, false);
  CheckIsPoppedOut(actions::kActionCut, false);
  CheckIsPinned(actions::kActionCut, false);
}

TEST_F(PinnedToolbarActionsContainerTest, ActiveActionSkipsExecution) {
  UpdateActionItem(actions::kActionCut);
  container()->UpdateActionState(actions::kActionCut, true);
  auto toolbar_buttons = GetChildToolbarButtons();
  ASSERT_EQ(toolbar_buttons.size(), 1u);

  auto* pinned_button = toolbar_buttons[0];

  EXPECT_FALSE(pinned_button->ShouldSkipExecutionForTesting());

  pinned_button->SetIsActionShowingBubble(true);
  ui::MouseEvent press_event(ui::EventType::kMousePressed, gfx::Point(),
                             gfx::Point(), ui::EventTimeForNow(),
                             ui::EF_LEFT_MOUSE_BUTTON, 0);
  ui::MouseEvent release_event(ui::EventType::kMouseReleased, gfx::Point(),
                               gfx::Point(), ui::EventTimeForNow(),
                               ui::EF_LEFT_MOUSE_BUTTON, 0);
  pinned_button->OnMousePressed(press_event);

  EXPECT_TRUE(pinned_button->ShouldSkipExecutionForTesting());

  pinned_button->OnMouseReleased(release_event);

  EXPECT_FALSE(pinned_button->ShouldSkipExecutionForTesting());
}

TEST_F(PinnedToolbarActionsContainerTest, MetricsRecordedForPinnableActions) {
  // Verify all pinnable buttons have a suffix listed in actions.xml.
  actions::ActionItemVector action_items;
  actions::ActionManager::Get().GetActions(
      action_items,
      browser_view()->browser()->GetActions()->root_action_item());
  size_t pinnable_count =
      std::ranges::count_if(action_items, [](actions::ActionItem* action) {
        return action->GetProperty(actions::kActionItemPinnableKey) ==
               std::underlying_type_t<actions::ActionPinnableState>(
                   actions::ActionPinnableState::kPinnable);
      });
  const auto pinnable_action_suffixes = base::ReadActionSuffixesForAction(
      "Actions.PinnedToolbarButtonActivation");
  EXPECT_EQ(1U, pinnable_action_suffixes.size());
  // Only one of history or history clusters should be pinnable.
  size_t expected_pinnable_count = pinnable_action_suffixes[0].size() - 1;
  if (!features::HasTabSearchToolbarButton()) {
    // Tab search is not pinnable if the feature is disabled.
    expected_pinnable_count -= 1;
  }
#if BUILDFLAG(IS_CHROMEOS)
  // Downloads action item does not exist for ChromeOS.
  EXPECT_EQ(pinnable_count, expected_pinnable_count - 1);
#else
  EXPECT_EQ(pinnable_count, expected_pinnable_count);
#endif  // BUILDFLAG(IS_CHROMEOS)
}