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