File: tab_alert_controller_interactive_uitest.cc

package info (click to toggle)
chromium 140.0.7339.185-1
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 6,193,740 kB
  • sloc: cpp: 35,093,945; ansic: 7,161,670; javascript: 4,199,694; python: 1,441,797; asm: 949,904; xml: 747,515; pascal: 187,748; perl: 88,691; sh: 88,248; objc: 79,953; sql: 52,714; cs: 44,599; fortran: 24,137; makefile: 22,114; tcl: 15,277; php: 13,980; yacc: 9,000; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (131 lines) | stat: -rw-r--r-- 5,191 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
// 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 <optional>

#include "base/callback_list.h"
#include "base/functional/bind.h"
#include "build/build_config.h"
#include "chrome/browser/glic/browser_ui/glic_tab_indicator_helper.h"
#include "chrome/browser/glic/test_support/interactive_glic_test.h"
#include "chrome/browser/ui/browser_element_identifiers.h"
#include "chrome/browser/ui/tabs/alert/tab_alert.h"
#include "chrome/browser/ui/tabs/alert/tab_alert_controller.h"
#include "chrome/browser/ui/tabs/public/tab_features.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/ui_test_utils.h"
#include "chrome/test/interaction/interactive_browser_test.h"
#include "components/tabs/public/tab_interface.h"
#include "content/public/test/browser_test.h"
#include "ui/base/interaction/element_identifier.h"
#include "ui/base/interaction/interactive_test.h"
#include "ui/base/interaction/state_observer.h"
#include "ui/views/interaction/interaction_test_util_views.h"
#include "url/gurl.h"

namespace {
class TabAlertControllerObserver
    : public ui::test::StateObserver<std::optional<tabs::TabAlert>> {
 public:
  TabAlertControllerObserver(Browser* browser, int tab_index) {
    callback_subscription_ =
        browser->tab_strip_model()
            ->GetTabAtIndex(tab_index)
            ->GetTabFeatures()
            ->tab_alert_controller()
            ->AddAlertToShowChangedCallback(base::BindRepeating(
                &TabAlertControllerObserver::OnAlertToShowChanged,
                base::Unretained(this)));
  }

  void OnAlertToShowChanged(std::optional<tabs::TabAlert> alert) {
    OnStateObserverStateChanged(alert);
  }

  ~TabAlertControllerObserver() override = default;

 private:
  base::CallbackListSubscription callback_subscription_;
};

DEFINE_LOCAL_STATE_IDENTIFIER_VALUE(TabAlertControllerObserver,
                                    kTab1AlertState);
DEFINE_LOCAL_STATE_IDENTIFIER_VALUE(TabAlertControllerObserver,
                                    kTab2AlertState);
DEFINE_LOCAL_ELEMENT_IDENTIFIER_VALUE(kFirstTabId);
DEFINE_LOCAL_ELEMENT_IDENTIFIER_VALUE(kSecondTabId);

}  // namespace

class TabAlertControllerInteractiveUiTest
    : public glic::test::InteractiveGlicTest {
 public:
  TabAlertControllerInteractiveUiTest() = default;
  ~TabAlertControllerInteractiveUiTest() override = default;

  GURL GetTestUrl() const {
    return embedded_test_server()->GetURL("/links.html");
  }

  auto LoadStartingPage(ui::ElementIdentifier id,
                        std::optional<int> tab_index,
                        BrowserSpecifier in_browser) {
    return Steps(InstrumentTab(id, tab_index, in_browser),
                 NavigateWebContents(id, GetTestUrl()));
  }

 protected:
  const InteractiveBrowserTest::DeepQuery kMockGlicContextAccessButton = {
      "#contextAccessIndicator"};
};

IN_PROC_BROWSER_TEST_F(TabAlertControllerInteractiveUiTest,
                       TabAlertControllerAccessingSwitchTabs) {
  RunTestSequence(
      LoadStartingPage(kFirstTabId, 0, browser()),
      AddInstrumentedTab(kSecondTabId, GetTestUrl()),
      ObserveState(kTab1AlertState, browser(), 0),
      ObserveState(kTab2AlertState, browser(), 1),
      OpenGlicWindow(
          glic::test::InteractiveGlicTest::GlicWindowMode::kAttached),
      SelectTab(kTabStripElementId, 0),
      ClickMockGlicElement(kMockGlicContextAccessButton),
      WaitForState(kTab1AlertState,
                   std::make_optional(tabs::TabAlert::GLIC_ACCESSING)),
      SelectTab(kTabStripElementId, 1),
      WaitForState(kTab1AlertState, std::nullopt),
      WaitForState(kTab2AlertState,
                   std::make_optional(tabs::TabAlert::GLIC_ACCESSING)));
}

IN_PROC_BROWSER_TEST_F(TabAlertControllerInteractiveUiTest,
                       AlertControllerChangesOnTabMovedBetweenBrowsers) {
#if BUILDFLAG(IS_LINUX)
  if (views::test::InteractionTestUtilSimulatorViews::IsWayland()) {
    GTEST_SKIP()
        << "Programmatic window activation is not supported in the Weston "
           "reference implementation of Wayland used by test bots.";
  }
#endif

  Browser* const browser2 = CreateBrowser(browser()->profile());
  RunTestSequence(
      LoadStartingPage(kFirstTabId, 0, browser()),
      LoadStartingPage(kSecondTabId, 0, browser2),
      OpenGlicWindow(
          glic::test::InteractiveGlicTest::GlicWindowMode::kDetached),
      ActivateSurface(kBrowserViewElementId),
      ObserveState(kTab1AlertState, browser(), 0),
      ObserveState(kTab2AlertState, browser2, 0),
      ClickMockGlicElement(kMockGlicContextAccessButton),
      WaitForState(kTab1AlertState,
                   std::make_optional(tabs::TabAlert::GLIC_ACCESSING)),
      InContext(browser2->window()->GetElementContext(),
                ActivateSurface(kBrowserViewElementId)),
      WaitForState(kTab1AlertState, std::nullopt),
      InContext(browser2->window()->GetElementContext(),
                SelectTab(kTabStripElementId, 0)),
      WaitForState(kTab2AlertState,
                   std::make_optional(tabs::TabAlert::GLIC_ACCESSING)));
}