File: page_action_interactive_uitest.cc

package info (click to toggle)
chromium 139.0.7258.138-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,120,676 kB
  • sloc: cpp: 35,100,869; 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 (954 lines) | stat: -rw-r--r-- 34,801 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
// Copyright 2025 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <string>

#include "base/test/metrics/histogram_tester.h"
#include "chrome/browser/ui/actions/chrome_action_id.h"
#include "chrome/browser/ui/browser_actions.h"
#include "chrome/browser/ui/browser_commands.h"
#include "chrome/browser/ui/browser_element_identifiers.h"
#include "chrome/browser/ui/tabs/public/tab_features.h"
#include "chrome/browser/ui/test/test_browser_ui.h"
#include "chrome/browser/ui/ui_features.h"
#include "chrome/browser/ui/views/omnibox/omnibox_view_views.h"
#include "chrome/browser/ui/views/page_action/page_action_container_view.h"
#include "chrome/browser/ui/views/page_action/page_action_controller.h"
#include "chrome/browser/ui/views/page_action/page_action_view.h"
#include "chrome/browser/ui/views/toolbar/toolbar_view.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/interactive_test_utils.h"
#include "chrome/test/base/ui_test_utils.h"
#include "chrome/test/interaction/interactive_browser_test.h"
#include "components/lens/lens_features.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/test_navigation_observer.h"
#include "content/public/test/test_utils.h"
#include "ui/views/test/ax_event_counter.h"
#include "ui/views/test/views_test_utils.h"
#include "url/gurl.h"

namespace page_actions {
namespace {

// Constants for available space adjustments.
constexpr size_t kFullSpaceTextLength = 0;
constexpr size_t kReducedSpaceTextLength = 500;

bool IsLabelVisible(PageActionView* page_action) {
  return page_action->IsChipVisible() &&
         page_action->GetLabelForTesting()->width() != 0;
}

bool IsAtMinimumSize(PageActionView* page_action) {
  return page_action->size() == page_action->GetMinimumSize();
}

bool IsIconCentered(PageActionView* page_action) {
  const auto* const image_container = page_action->GetImageContainerView();
  return image_container->x() ==
         page_action->width() - image_container->bounds().right();
}

void EnsurePageActionEnabled(actions::ActionId action_id) {
  auto* action = actions::ActionManager::Get().FindAction(action_id);
  CHECK(action);
  action->SetEnabled(true);
  action->SetVisible(true);
}

MATCHER(IsChipExpanded, "Check if the chip is expanded") {
  if (arg == nullptr) {
    *result_listener << "Page action is null";
    return false;
  }
  if (!IsLabelVisible(arg)) {
    *result_listener << "Label is not visible";
    return false;
  }
  if (IsAtMinimumSize(arg)) {
    *result_listener << "Chip is at minimum size, Size: "
                     << arg->size().ToString();
    return false;
  }
  if (arg->is_animating_label()) {
    *result_listener << "Page action is animating";
    return false;
  }
  if (IsIconCentered(arg)) {
    *result_listener << "Chip is centered, Insets: "
                     << arg->GetInsets().ToString();
    return false;
  }

  return true;
}

MATCHER(IsChipCollapsed, "Check if the chip is collapsed") {
  if (arg == nullptr) {
    *result_listener << "Page action is null";
    return false;
  }
  if (IsLabelVisible(arg)) {
    *result_listener << "Label is visible";
    return false;
  }
  if (!IsAtMinimumSize(arg)) {
    *result_listener << "Chip is not at minimum size, Size: "
                     << arg->size().ToString();
    return false;
  }
  if (arg->is_animating_label()) {
    *result_listener << "Page action is animating";
    return false;
  }
  if (!IsIconCentered(arg)) {
    *result_listener << "Chip is not centered, Insets: "
                     << arg->GetInsets().ToString();
    return false;
  }

  return true;
}

class PageActionUiTestBase {
 public:
  PageActionUiTestBase() {
    // TODO(crbug.com/424806660): These tests should not be reliant on
    // kLensOverlayOmniboxEntryPoint being enabled, but disabling it causes them
    // to fail.
    feature_list_.InitWithFeaturesAndParameters(
        /*enabled_features=*/
        {
            {
                features::kPageActionsMigration,
                {
                    {features::kPageActionsMigrationZoom.name, "true"},
                    {features::kPageActionsMigrationTranslate.name, "true"},
                    {features::kPageActionsMigrationMemorySaver.name, "true"},
                },
            },
            {lens::features::kLensOverlayOmniboxEntryPoint, {}},
        },
        /*disabled_features=*/{
            lens::features::kLensOverlay,
        });
  }

  virtual ~PageActionUiTestBase() = default;

  virtual Browser* GetBrowser() const = 0;

  page_actions::PageActionController* page_action_controller() const {
    return GetBrowser()
        ->GetActiveTabInterface()
        ->GetTabFeatures()
        ->page_action_controller();
  }

  LocationBarView* location_bar() const {
    return BrowserView::GetBrowserViewForBrowser(GetBrowser())
        ->toolbar()
        ->location_bar();
  }

  OmniboxViewViews* omnibox_view() const {
    return static_cast<OmniboxViewViews*>(location_bar()->omnibox_view());
  }

  PageActionContainerView* page_action_container() const {
    return location_bar()->page_action_container();
  }

  PageActionView* GetPageActionView(actions::ActionId action_id) const {
    return page_action_container()->GetPageActionView(action_id);
  }

  PageActionView* GetTestPageActionView() const {
    return GetPageActionView(kActionShowTranslate);
  }

  void FastForwardAnimation(PageActionView* view) {
    auto animation = std::make_unique<gfx::AnimationTestApi>(
        &view->GetSlideAnimationForTesting());
    auto now = base::TimeTicks::Now();
    animation->SetStartTime(now);
    animation->Step(now + base::Minutes(1));
    EnsureLayout();
  }

  void ShowSuggestionChip(actions::ActionId action_id) const {
    EnsurePageActionEnabled(action_id);
    page_action_controller()->ShowSuggestionChip(action_id);
  }

  void HideSuggestionChip(actions::ActionId action_id) const {
    page_action_controller()->HideSuggestionChip(action_id);
  }

  PageActionView* GetTranslatePageActionView() const {
    return GetPageActionView(kActionShowTranslate);
  }

  PageActionView* GetMemorySaverPageActionView() const {
    return GetPageActionView(kActionShowMemorySaverChip);
  }

  void ShowPageAction(actions::ActionId action_id) const {
    EnsurePageActionEnabled(action_id);
    page_action_controller()->Show(action_id);
  }

  void HidePageAction(actions::ActionId action_id) const {
    EnsurePageActionEnabled(action_id);
    page_action_controller()->Hide(action_id);
  }

  void ShowTestPageActionIcon() const { ShowPageAction(kActionShowTranslate); }

  void ShowTestSuggestionChip() const {
    ShowPageAction(kActionShowTranslate);
    ShowSuggestionChip(kActionShowTranslate);
  }

  void ShowTranslatePageActionIcon() const {
    HideSuggestionChip(kActionShowTranslate);
    ShowPageAction(kActionShowTranslate);
  }

  void ShowTranslateSuggestionChip() const {
    ShowPageAction(kActionShowTranslate);
    ShowSuggestionChip(kActionShowTranslate);
  }

  void ShowMemorySaverPageActionIcon() const {
    HideSuggestionChip(kActionShowMemorySaverChip);
    ShowPageAction(kActionShowMemorySaverChip);
  }

  void ShowMemorySaverSuggestionChip() const {
    ShowPageAction(kActionShowMemorySaverChip);
    ShowSuggestionChip(kActionShowMemorySaverChip);
  }

  // Dynamically adjust the available space in the location bar by setting
  // the omnibox text length. A larger `text_length` will reduce available
  // space, while a smaller text_length (or 0) will increase available space.
  void AdjustAvailableSpace(size_t text_length) {
    omnibox_view()->SetUserText(std::u16string(text_length, 'a'));

    // Step 2: Immediately unhide the page actions.
    page_action_controller()->SetShouldHidePageActions(false);

    EnsureLayout();
  }

  void EnsureLayout() {
    views::test::RunScheduledLayout(
        BrowserView::GetBrowserViewForBrowser(GetBrowser()));
  }

 protected:
  void PerformBackNavigation(content::WebContents* web_contents) {
    content::NavigationController& controller = web_contents->GetController();
    ASSERT_TRUE(controller.CanGoBack());
    content::TestNavigationObserver back_observer(web_contents);
    controller.GoBack();
    back_observer.Wait();
  }

 private:
  base::test::ScopedFeatureList feature_list_;
};

class PageActionInteractiveUiTest : public InteractiveBrowserTest,
                                    public PageActionUiTestBase {
 public:
  PageActionInteractiveUiTest() = default;
  PageActionInteractiveUiTest(const PageActionInteractiveUiTest&) = delete;
  PageActionInteractiveUiTest& operator=(const PageActionInteractiveUiTest&) =
      delete;
  ~PageActionInteractiveUiTest() override = default;

  // PageActionUiTestBase:
  Browser* GetBrowser() const override { return browser(); }
};

// Tests that switching from a full available space to a reduced available space
// collapses the suggestion chip from label mode to icon-only mode.
IN_PROC_BROWSER_TEST_F(PageActionInteractiveUiTest,
                       SuggestionChipCollapsesToIconWhenSpaceIsReduced) {
  PageActionView* view = GetTestPageActionView();

  AdjustAvailableSpace(kFullSpaceTextLength);

  ShowTestSuggestionChip();
  FastForwardAnimation(view);

  EXPECT_THAT(view, IsChipExpanded());

  AdjustAvailableSpace(kReducedSpaceTextLength);

  ShowTestSuggestionChip();
  FastForwardAnimation(view);

  EXPECT_THAT(view, IsChipCollapsed());
}

// Tests that increasing available space from reduced to full restores the
// suggestion chip label (expanding from icon-only to label mode).
IN_PROC_BROWSER_TEST_F(PageActionInteractiveUiTest,
                       SuggestionChipRestoresLabelWhenSpaceIsRestored) {
  AdjustAvailableSpace(kReducedSpaceTextLength);

  PageActionView* view = GetTestPageActionView();

  ShowTestSuggestionChip();
  FastForwardAnimation(view);

  EXPECT_THAT(view, IsChipCollapsed());

  AdjustAvailableSpace(kFullSpaceTextLength);

  ShowTestSuggestionChip();
  FastForwardAnimation(view);

  EXPECT_THAT(view, IsChipExpanded());
}

// Tests that transitioning from full available space to reduced and then back
// to full toggles the suggestion chip between label and icon modes.
IN_PROC_BROWSER_TEST_F(
    PageActionInteractiveUiTest,
    SuggestionChipTransitionsBetweenLabelAndIconWhenSpaceChanges) {
  PageActionView* view = GetTestPageActionView();

  AdjustAvailableSpace(kFullSpaceTextLength);
  ShowTestSuggestionChip();
  FastForwardAnimation(view);

  EXPECT_THAT(view, IsChipExpanded());

  AdjustAvailableSpace(kReducedSpaceTextLength);

  ShowTestSuggestionChip();
  FastForwardAnimation(view);

  EXPECT_THAT(view, IsChipCollapsed());

  AdjustAvailableSpace(kFullSpaceTextLength);

  ShowTestSuggestionChip();
  FastForwardAnimation(view);

  EXPECT_THAT(view, IsChipExpanded());
}

// Tests that starting with reduced space, moving to full space, and then
// reverting to reduced space toggles the suggestion chip between icon-only and
// label modes repeatedly.
IN_PROC_BROWSER_TEST_F(PageActionInteractiveUiTest,
                       SuggestionChipSwitchesModesOnMultipleSpaceAdjustments) {
  PageActionView* view = GetTestPageActionView();
  AdjustAvailableSpace(kReducedSpaceTextLength);

  ShowTestSuggestionChip();
  FastForwardAnimation(view);

  EXPECT_FALSE(IsLabelVisible(view));
  EXPECT_TRUE(IsAtMinimumSize(view));

  AdjustAvailableSpace(kFullSpaceTextLength);

  ShowTestSuggestionChip();
  FastForwardAnimation(view);

  EXPECT_THAT(view, IsChipExpanded());

  AdjustAvailableSpace(kReducedSpaceTextLength);

  ShowTestSuggestionChip();
  FastForwardAnimation(view);

  EXPECT_THAT(view, IsChipCollapsed());
}

// Tests that calling ShowPageAction on a page action results in an icon-only
// view, ignoring any extra available space.
IN_PROC_BROWSER_TEST_F(PageActionInteractiveUiTest,
                       PageActionDisplaysIconOnlyRegardlessOfAvailableSpace) {
  ShowTestPageActionIcon();
  AdjustAvailableSpace(kFullSpaceTextLength);

  PageActionView* view = GetTestPageActionView();

  EXPECT_THAT(view, IsChipCollapsed());
}

// Tests that once a page action is shown as an icon-only view, it remains
// icon-only through available space adjustments (both increased and reduced).
IN_PROC_BROWSER_TEST_F(PageActionInteractiveUiTest,
                       PageActionIconRemainsUnchangedThroughSpaceAdjustments) {
  ShowTestPageActionIcon();
  AdjustAvailableSpace(kFullSpaceTextLength);

  PageActionView* view = GetTestPageActionView();

  EXPECT_THAT(view, IsChipCollapsed());

  AdjustAvailableSpace(kReducedSpaceTextLength);

  EXPECT_THAT(view, IsChipCollapsed());

  AdjustAvailableSpace(kFullSpaceTextLength);

  EXPECT_FALSE(IsLabelVisible(view));
  EXPECT_TRUE(IsAtMinimumSize(view));
}

// Tests that toggling the suggestion chip state for two actions reorders their
// views appropriately.
IN_PROC_BROWSER_TEST_F(PageActionInteractiveUiTest,
                       SuggestionChipReordersMultipleActions) {
  PageActionContainerView* container = page_action_container();
  ASSERT_TRUE(container);

  PageActionView* memory_saver_view = GetMemorySaverPageActionView();
  ASSERT_TRUE(memory_saver_view);
  PageActionView* translate_view = GetTranslatePageActionView();
  ASSERT_TRUE(translate_view);

  auto initial_memory_saver_index = container->GetIndexOf(memory_saver_view);
  ASSERT_TRUE(initial_memory_saver_index.has_value());
  auto initial_translate_index = container->GetIndexOf(translate_view);
  ASSERT_TRUE(initial_translate_index.has_value());

  // For this test, we assume that the translate page action appears before the
  // memory saver page action.
  EXPECT_LT(initial_translate_index.value(),
            initial_memory_saver_index.value());

  // Step 1: Activate suggestion chip for the translate action only.
  ShowTranslateSuggestionChip();

  // Expect translate view to move to the front (index 0).
  {
    auto new_translate_index = container->GetIndexOf(translate_view);
    ASSERT_TRUE(new_translate_index.has_value());
    EXPECT_EQ(new_translate_index.value(), 0u);
  }
  // The memory saver should at the initial index.
  {
    auto new_memory_saver_index = container->GetIndexOf(memory_saver_view);
    ASSERT_TRUE(new_memory_saver_index.has_value());
    EXPECT_EQ(new_memory_saver_index.value(),
              initial_memory_saver_index.value());
  }

  // Step 2: Activate suggestion chip for the memory saver page action as well.
  ShowMemorySaverSuggestionChip();

  // Now the memory saver view should move to the front.
  {
    auto new_memory_saver_index = container->GetIndexOf(memory_saver_view);
    ASSERT_TRUE(new_memory_saver_index.has_value());
    EXPECT_EQ(new_memory_saver_index.value(), 0u);
  }
  // And the translate view should now be at index 1.
  {
    auto new_translate_index = container->GetIndexOf(translate_view);
    ASSERT_TRUE(new_translate_index.has_value());
    EXPECT_EQ(new_translate_index.value(), 1u);
  }
}

IN_PROC_BROWSER_TEST_F(PageActionInteractiveUiTest,
                       EphemeralPageActionUmaNotLoggedOnBackNavigation) {
  // This test verifies that when we navigate back to a previously visited URL
  // in the same tab, ephemeral actions are *not* re-logged to
  // "PageActionController.ActionTypeShown2". The ephemeral action has already
  // been logged for that page context, so it shouldn't increment again.

  base::HistogramTester histogram_tester;

  // Step 1: Show ephemeral Translate action in our initial context (tab[0]).
  //         This should increment the histogram by 1.
  ShowPageAction(kActionShowTranslate);
  histogram_tester.ExpectTotalCount("PageActionController.ActionTypeShown2", 1);
  histogram_tester.ExpectUniqueSample("PageActionController.ActionTypeShown2",
                                      PageActionIconType::kTranslate, 1);

  // Step 2: Navigate forward to a new URL. This new navigation is a different
  //         page context, so showing the ephemeral action again logs a second
  //         time.
  GURL next_url("chrome://version");
  ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), next_url));
  ShowPageAction(kActionShowTranslate);
  histogram_tester.ExpectTotalCount("PageActionController.ActionTypeShown2", 2);
  histogram_tester.ExpectBucketCount("PageActionController.ActionTypeShown2",
                                     PageActionIconType::kTranslate, 2);

  // Step 3: Go back to the previous URL in the same tab. This *reverts* to the
  //         old page context that already had ephemeral actions shown/logged.
  //         Therefore, re-showing the ephemeral action now should NOT increment
  //         the histogram again.
  PerformBackNavigation(browser()->tab_strip_model()->GetActiveWebContents());

  ShowPageAction(kActionShowTranslate);

  // Histogram should increase at 3 total samples; since the url have changed in
  // the same page.
  histogram_tester.ExpectTotalCount("PageActionController.ActionTypeShown2", 3);
  histogram_tester.ExpectBucketCount("PageActionController.ActionTypeShown2",
                                     PageActionIconType::kTranslate, 3);
}

IN_PROC_BROWSER_TEST_F(PageActionInteractiveUiTest,
                       EphemeralPageActionUmaLoggedOncePerContext) {
  // This test verifies that ephemeral page actions (like a Translate icon or
  // Memory Saver chip) only log to "PageActionController.ActionTypeShown2" the
  // first time they appear in a given page context. A "page context" is
  // determined by the combination of (tab, navigation). Re-showing the same
  // ephemeral action in the same context should NOT increment the histogram,
  // whereas switching tabs or navigating creates a new context that does log
  // again.

  base::HistogramTester histogram_tester;

  // 1) Show the ephemeral Translate action in the initial tab (tab[0]) for the
  //    very first time. This should increment the histogram by 1.
  ShowPageAction(kActionShowTranslate);
  histogram_tester.ExpectTotalCount("PageActionController.ActionTypeShown2", 1);
  histogram_tester.ExpectUniqueSample("PageActionController.ActionTypeShown2",
                                      PageActionIconType::kTranslate, 1);

  // 2) Hide and re-show the same Translate icon within the same page context
  //    (same tab, same navigation). Because it's ephemeral and already shown,
  //    the histogram should not increment again.
  HidePageAction(kActionShowTranslate);
  ShowPageAction(kActionShowTranslate);
  histogram_tester.ExpectTotalCount("PageActionController.ActionTypeShown2", 1);
  histogram_tester.ExpectUniqueSample("PageActionController.ActionTypeShown2",
                                      PageActionIconType::kTranslate, 1);

  // 3) Navigate to a new URL in the same tab (tab[0]). This is now a new page
  //    context. Showing the ephemeral Translate action again in this context
  //    should increment the histogram by 1.
  ASSERT_TRUE(
      ui_test_utils::NavigateToURL(browser(), GURL("chrome://settings")));
  ShowPageAction(kActionShowTranslate);
  histogram_tester.ExpectTotalCount("PageActionController.ActionTypeShown2", 2);
  histogram_tester.ExpectUniqueSample("PageActionController.ActionTypeShown2",
                                      PageActionIconType::kTranslate, 2);

  // 4) Open a brand new tab (tab[1]) and activate it. Because each tab
  // maintains its own context, showing ephemeral actions for the first time in
  // tab[1] should log again. Then, show both the Translate icon and the Memory
  // Saver chip here, which should each increment the histogram for their
  // respective actions.
  ASSERT_TRUE(
      AddTabAtIndex(1, GURL("chrome://version"), ui::PAGE_TRANSITION_LINK));
  browser()->tab_strip_model()->ActivateTabAt(1);

  // Show ephemeral Translate action in tab[1].
  ShowPageAction(kActionShowTranslate);
  histogram_tester.ExpectTotalCount("PageActionController.ActionTypeShown2", 3);
  histogram_tester.ExpectUniqueSample("PageActionController.ActionTypeShown2",
                                      PageActionIconType::kTranslate, 3);

  // Show ephemeral Memory Saver chip in tab[1].
  ShowPageAction(kActionShowMemorySaverChip);
  histogram_tester.ExpectTotalCount("PageActionController.ActionTypeShown2", 4);
  histogram_tester.ExpectBucketCount("PageActionController.ActionTypeShown2",
                                     PageActionIconType::kTranslate, 3);
  histogram_tester.ExpectBucketCount("PageActionController.ActionTypeShown2",
                                     PageActionIconType::kMemorySaver, 1);

  // 5) Switch back to tab[0] (where the Translate action was already shown
  // after navigation). Re-showing the ephemeral icon should NOT increment the
  // metric, since it's the same context in tab[0].
  browser()->tab_strip_model()->ActivateTabAt(0);
  ShowPageAction(kActionShowTranslate);
  histogram_tester.ExpectTotalCount("PageActionController.ActionTypeShown2", 4);
}

// Verifies that "…Icon.CTR2" histograms emit kShown once-per-context.
// The test mirrors EphemeralPageActionUmaLoggedOncePerContext.
IN_PROC_BROWSER_TEST_F(PageActionInteractiveUiTest,
                       CTR2HistogramsLoggedOncePerContext) {
  base::HistogramTester histogram_tester;

  constexpr char kGeneralHistogram[] = "PageActionController.Icon.CTR2";
  constexpr char kTranslateHistogram[] =
      "PageActionController.Translate.Icon.CTR2";

  // 1. Initial page-context (tab[0], first navigation).
  ShowPageAction(kActionShowTranslate);
  histogram_tester.ExpectUniqueSample(kGeneralHistogram,
                                      PageActionCTREvent::kShown, 1);
  histogram_tester.ExpectUniqueSample(kTranslateHistogram,
                                      PageActionCTREvent::kShown, 1);

  // 2. Hide + re-show in the SAME context → no additional logging.
  HidePageAction(kActionShowTranslate);
  ShowPageAction(kActionShowTranslate);
  histogram_tester.ExpectTotalCount(kGeneralHistogram, 1);
  histogram_tester.ExpectTotalCount(kTranslateHistogram, 1);

  // 3. New navigation in the SAME tab → new context, logs again.
  ASSERT_TRUE(
      ui_test_utils::NavigateToURL(browser(), GURL("chrome://settings")));
  ShowPageAction(kActionShowTranslate);
  histogram_tester.ExpectTotalCount(kGeneralHistogram, 2);
  histogram_tester.ExpectBucketCount(kTranslateHistogram,
                                     PageActionCTREvent::kShown, 2);

  // 4. Open a new tab → brand-new context.
  ASSERT_TRUE(
      AddTabAtIndex(1, GURL("chrome://version"), ui::PAGE_TRANSITION_LINK));
  browser()->tab_strip_model()->ActivateTabAt(1);

  // 4-a) First show of Translate in tab[1] logs again.
  ShowPageAction(kActionShowTranslate);
  histogram_tester.ExpectTotalCount(kGeneralHistogram, 3);
  histogram_tester.ExpectBucketCount(kTranslateHistogram,
                                     PageActionCTREvent::kShown, 3);

  browser()->tab_strip_model()->ActivateTabAt(0);
  ShowPageAction(kActionShowTranslate);
  histogram_tester.ExpectTotalCount(kGeneralHistogram, 3);
  histogram_tester.ExpectBucketCount(kTranslateHistogram,
                                     PageActionCTREvent::kShown, 3);
}

class PageActionMetricsInteractiveUiTest : public InteractiveBrowserTest,
                                           public PageActionUiTestBase {
 public:
  PageActionMetricsInteractiveUiTest() = default;

  PageActionMetricsInteractiveUiTest(
      const PageActionMetricsInteractiveUiTest&) = delete;
  PageActionMetricsInteractiveUiTest& operator=(
      const PageActionInteractiveUiTest&) = delete;
  ~PageActionMetricsInteractiveUiTest() override = default;

  // PageActionUiTestBase:
  Browser* GetBrowser() const override { return browser(); }

 protected:
  void SetZoomLevel(content::PageZoom zoom_level) {
    chrome::Zoom(GetBrowser(), zoom_level);
  }

  auto DoZoomIn() {
    return Do([&]() { SetZoomLevel(content::PAGE_ZOOM_IN); });
  }

  auto DoZoomOut() {
    return Do([&]() { SetZoomLevel(content::PAGE_ZOOM_OUT); });
  }
};

IN_PROC_BROWSER_TEST_F(PageActionMetricsInteractiveUiTest, ClickHistogramLogs) {
  base::HistogramTester histogram_tester;
  const char* general_histogram = "PageActionController.Icon.CTR2";
  const std::string specific_histogram = "PageActionController.Zoom.Icon.CTR2";

  RunTestSequence(
      DoZoomIn(), WaitForShow(kActionItemZoomElementId),

      CheckResult(
          [&]() { return histogram_tester.GetTotalSum(general_histogram); },
          testing::Eq(0)),
      CheckResult(
          [&]() { return histogram_tester.GetTotalSum(specific_histogram); },
          testing::Eq(0)),

      PressButton(kActionItemZoomElementId),

      CheckResult(
          [&]() {
            return histogram_tester.GetBucketCount(
                general_histogram, PageActionCTREvent::kClicked);
          },
          testing::Eq(1)),
      CheckResult(
          [&]() {
            return histogram_tester.GetBucketCount(
                specific_histogram, PageActionCTREvent::kClicked);
          },
          testing::Eq(1)),

      PressButton(kActionItemZoomElementId),

      CheckResult(
          [&]() {
            return histogram_tester.GetBucketCount(
                general_histogram, PageActionCTREvent::kClicked);
          },
          testing::Eq(2)),
      CheckResult(
          [&]() {
            return histogram_tester.GetBucketCount(
                specific_histogram, PageActionCTREvent::kClicked);
          },
          testing::Eq(2)));
}

// Verifies that the "NumberActionsShown3" exact-linear histogram records
// the correct bucket for one vs. two simultaneously visible ephemeral actions.
IN_PROC_BROWSER_TEST_F(PageActionMetricsInteractiveUiTest,
                       NumberActionsShown3HistogramLogged) {
  base::HistogramTester histogram_tester;

  // 1) Show the Translate suggestion chip (1 visible ephemeral action).
  ShowPageAction(kActionShowTranslate);

  // 2) Show the Memory Saver suggestion chip (now 2 visible ephemeral actions).
  ShowPageAction(kActionShowMemorySaverChip);

  // Expect exactly one sample in bucket “1” and one in bucket “2”.
  histogram_tester.ExpectBucketCount("PageActionController.NumberActionsShown3",
                                     1, 1);
  histogram_tester.ExpectBucketCount("PageActionController.NumberActionsShown3",
                                     2, 1);
}

// Verifies that the "PagesWithActionsShown3" enumeration histogram records
// a kPageShown on navigation, a kActionShown on the first ephemeral action,
// and a kMultipleActionsShown once two appear.
IN_PROC_BROWSER_TEST_F(PageActionMetricsInteractiveUiTest,
                       PagesWithActionsShown3EventsLogged) {
  base::HistogramTester histogram_tester;

  // Navigate to a fresh URL to trigger a kPageShown event.
  GURL test_url("chrome://version");
  ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), test_url));

  // Show two ephemeral suggestion chips in sequence.
  ShowPageAction(kActionShowTranslate);        // logs kActionShown
  ShowPageAction(kActionShowMemorySaverChip);  // logs kMultipleActionsShown

  // Verify each enumeration event was recorded exactly once.
  histogram_tester.ExpectBucketCount(
      "PageActionController.PagesWithActionsShown3",
      PageActionPageEvent::kPageShown, 1);
  histogram_tester.ExpectBucketCount(
      "PageActionController.PagesWithActionsShown3",
      PageActionPageEvent::kActionShown, 1);
  histogram_tester.ExpectBucketCount(
      "PageActionController.PagesWithActionsShown3",
      PageActionPageEvent::kMultipleActionsShown, 1);
}

// TODO(crbug.com/411078148): Re-enable on Mac.
#if BUILDFLAG(IS_MAC)
#define MAYBE_SuggestionChipWithAnnouncement \
  DISABLED_SuggestionChipWithAnnouncement
#else
#define MAYBE_SuggestionChipWithAnnouncement SuggestionChipWithAnnouncement
#endif
// Tests that showing a suggestion chip with announcements enabled will
// announce the chip on a screen reader.
IN_PROC_BROWSER_TEST_F(PageActionInteractiveUiTest,
                       MAYBE_SuggestionChipWithAnnouncement) {
  views::test::AXEventCounter counter(views::AXUpdateNotifier::Get());
  ASSERT_EQ(0, counter.GetCount(ax::mojom::Event::kAlert));

  ShowTranslatePageActionIcon();
  page_action_controller()->ShowSuggestionChip(
      kActionShowTranslate, {
                                .should_animate = false,
                                .should_announce_chip = false,
                            });
  EXPECT_EQ(0, counter.GetCount(ax::mojom::Event::kAlert));

  // Reshow the chip with announcements enabled.
  HideSuggestionChip(kActionShowTranslate);
  page_action_controller()->ShowSuggestionChip(kActionShowTranslate,
                                               {
                                                   .should_animate = false,
                                                   .should_announce_chip = true,
                                               });
  EXPECT_EQ(1, counter.GetCount(ax::mojom::Event::kAlert));
}

class PageActionPixelTestBase : public UiBrowserTest,
                                public PageActionUiTestBase {
 public:
  PageActionPixelTestBase() = default;
  PageActionPixelTestBase(const PageActionPixelTestBase&) = delete;
  PageActionPixelTestBase& operator=(const PageActionPixelTestBase&) = delete;
  ~PageActionPixelTestBase() override = default;

  // PageActionUiTestBase:
  Browser* GetBrowser() const final { return browser(); }

  // UiBrowserTest:
  void ShowUi(const std::string& /*name*/) override {
    views::test::RunScheduledLayout(
        BrowserView::GetBrowserViewForBrowser(GetBrowser()));
  }

  void WaitForUserDismissal() final {}
};

class PageActionPixelIconsHiddenTest : public PageActionPixelTestBase {
 public:
  PageActionPixelIconsHiddenTest() = default;
  PageActionPixelIconsHiddenTest(const PageActionPixelIconsHiddenTest&) =
      delete;
  PageActionPixelIconsHiddenTest& operator=(
      const PageActionPixelIconsHiddenTest&) = delete;
  ~PageActionPixelIconsHiddenTest() override = default;

  // UiBrowserTest:
  void ShowUi(const std::string& name) override {
    // Default scenario: do nothing.
    PageActionPixelTestBase::ShowUi(name);
  }

  bool VerifyUi() override {
    PageActionView* test_view = GetTestPageActionView();
    EXPECT_FALSE(test_view->GetVisible());
    return true;
  }
};

IN_PROC_BROWSER_TEST_F(PageActionPixelIconsHiddenTest, InvokeUi_Default) {
  ShowAndVerifyUi();
}

class PageActionPixelShowIconTest : public PageActionPixelTestBase {
 public:
  PageActionPixelShowIconTest() = default;
  PageActionPixelShowIconTest(const PageActionPixelShowIconTest&) = delete;
  PageActionPixelShowIconTest& operator=(const PageActionPixelShowIconTest&) =
      delete;
  ~PageActionPixelShowIconTest() override = default;

  // UiBrowserTest:
  void ShowUi(const std::string& name) override {
    ShowTestPageActionIcon();
    PageActionPixelTestBase::ShowUi(name);
  }

  bool VerifyUi() override {
    PageActionView* test_view = GetTestPageActionView();
    EXPECT_TRUE(test_view->GetVisible());
    EXPECT_FALSE(IsLabelVisible(test_view));
    EXPECT_TRUE(IsAtMinimumSize(test_view));
    return true;
  }
};

IN_PROC_BROWSER_TEST_F(PageActionPixelShowIconTest, InvokeUi_Default) {
  ShowAndVerifyUi();
}

class PageActionPixelShowChipTest : public PageActionPixelTestBase {
 public:
  PageActionPixelShowChipTest() = default;
  PageActionPixelShowChipTest(const PageActionPixelShowChipTest&) = delete;
  PageActionPixelShowChipTest& operator=(const PageActionPixelShowChipTest&) =
      delete;
  ~PageActionPixelShowChipTest() override = default;

  // UiBrowserTest:
  void ShowUi(const std::string& name) override {
    AdjustAvailableSpace(kFullSpaceTextLength);
    ShowTestSuggestionChip();
    FastForwardAnimation(GetTestPageActionView());
    PageActionPixelTestBase::ShowUi(name);
  }

  bool VerifyUi() override {
    PageActionView* test_view = GetTestPageActionView();
    EXPECT_TRUE(test_view->GetVisible());
    EXPECT_TRUE(IsLabelVisible(test_view));
    EXPECT_FALSE(IsAtMinimumSize(test_view));
    return true;
  }
};

IN_PROC_BROWSER_TEST_F(PageActionPixelShowChipTest, InvokeUi_Default) {
  ShowAndVerifyUi();
}

class PageActionPixelShowChipReducedTest : public PageActionPixelTestBase {
 public:
  PageActionPixelShowChipReducedTest() = default;
  PageActionPixelShowChipReducedTest(
      const PageActionPixelShowChipReducedTest&) = delete;
  PageActionPixelShowChipReducedTest& operator=(
      const PageActionPixelShowChipReducedTest&) = delete;
  ~PageActionPixelShowChipReducedTest() override = default;

  // UiBrowserTest:
  void ShowUi(const std::string& name) override {
    AdjustAvailableSpace(kReducedSpaceTextLength);
    ShowTestSuggestionChip();
    FastForwardAnimation(GetTestPageActionView());
    PageActionPixelTestBase::ShowUi(name);
  }

  bool VerifyUi() override {
    PageActionView* test_view = GetTestPageActionView();
    EXPECT_TRUE(test_view->GetVisible());
    EXPECT_FALSE(IsLabelVisible(test_view));
    EXPECT_TRUE(IsAtMinimumSize(test_view));
    return true;
  }
};

IN_PROC_BROWSER_TEST_F(PageActionPixelShowChipReducedTest, InvokeUi_Default) {
  ShowAndVerifyUi();
}

class PageActionPixelReorderTest : public PageActionPixelTestBase {
 public:
  PageActionPixelReorderTest() = default;
  PageActionPixelReorderTest(const PageActionPixelReorderTest&) = delete;
  PageActionPixelReorderTest& operator=(const PageActionPixelReorderTest&) =
      delete;
  ~PageActionPixelReorderTest() override = default;

  // UiBrowserTest:
  void ShowUi(const std::string& name) override {
    ShowMemorySaverPageActionIcon();

    // Now, activate the suggestion chip for the translate action.
    ShowTranslateSuggestionChip();

    // Run any pending layout tasks.
    PageActionPixelTestBase::ShowUi(name);
  }

  bool VerifyUi() override {
    PageActionContainerView* container = page_action_container();
    PageActionView* memory_saver_view = GetMemorySaverPageActionView();
    PageActionView* translate_view = GetTranslatePageActionView();

    // Get the current indices as optionals.
    auto memory_saver_index = container->GetIndexOf(memory_saver_view);
    auto translate_index = container->GetIndexOf(translate_view);
    if (!memory_saver_index.has_value() || !translate_index.has_value()) {
      return false;
    }

    // Expect the Translate action (suggestion chip) to be at index 0,
    // and the memory saver page action to be at index 1.
    EXPECT_EQ(translate_index.value(), 0u);
    EXPECT_EQ(memory_saver_index.value(), 1u);

    return true;
  }
};

IN_PROC_BROWSER_TEST_F(PageActionPixelReorderTest, InvokeUi_Default) {
  ShowAndVerifyUi();
}

}  // namespace
}  // namespace page_actions