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
|
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <stddef.h>
#include "base/functional/bind.h"
#include "base/location.h"
#include "base/run_loop.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/scoped_feature_list.h"
#include "build/build_config.h"
#include "chrome/app/chrome_command_ids.h"
#include "chrome/browser/bookmarks/bookmark_model_factory.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/browser/profiles/profile_window.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_command_controller.h"
#include "chrome/browser/ui/browser_element_identifiers.h"
#include "chrome/browser/ui/browser_finder.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/browser/ui/tabs/tab_menu_model.h"
#include "chrome/browser/ui/ui_features.h"
#include "chrome/browser/ui/view_ids.h"
#include "chrome/browser/ui/views/tabs/tab_strip.h"
#include "chrome/browser/ui/views/toolbar/reload_button.h"
#include "chrome/browser/ui/views/toolbar/toolbar_button.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/bookmarks/browser/bookmark_model.h"
#include "components/bookmarks/browser/bookmark_utils.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/test_navigation_observer.h"
#include "net/dns/mock_host_resolver.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
#include "ui/base/interaction/element_identifier.h"
#include "ui/base/mojom/menu_source_type.mojom.h"
#include "ui/base/test/ui_controls.h"
#include "ui/base/ui_base_types.h"
#include "ui/views/focus/focus_manager.h"
#include "ui/views/test/widget_activation_waiter.h"
#include "ui/views/test/widget_test.h"
#include "ui/views/view.h"
#include "ui/views/widget/widget.h"
using bookmarks::BookmarkModel;
class ToolbarViewTest : public InteractiveBrowserTest {
public:
ToolbarViewTest() {
scoped_feature_list_.InitWithFeatures(
/* enabled_features =*/{features::kSideBySide},
/* disabled_features =*/
{});
}
ToolbarViewTest(const ToolbarViewTest&) = delete;
ToolbarViewTest& operator=(const ToolbarViewTest&) = delete;
void SetUpOnMainThread() override {
InteractiveBrowserTest::SetUpOnMainThread();
host_resolver()->AddRule("*", "127.0.0.1");
}
void RunToolbarCycleFocusTest(Browser* browser);
void SetLocationBarSecurityLevelForTesting(
security_state::SecurityLevel security_level) {
BrowserView* browser_view =
BrowserView::GetBrowserViewForBrowser(browser());
LocationBarView* location_bar_view = browser_view->GetLocationBarView();
LocationIconView* location_icon_view =
location_bar_view->location_icon_view();
location_icon_view->SetSecurityLevelForTesting(security_level);
}
auto OpenSideBySideTab(int tab_index) {
const char kTabToHover[] = "Tab to hover";
return Steps(NameDescendantViewByType<Tab>(kTabStripElementId, kTabToHover,
tab_index),
MoveMouseTo(kTabToHover),
MayInvolveNativeContextMenu(
ClickMouse(ui_controls::RIGHT),
SelectMenuItem(TabMenuModel::kSplitTabsMenuItem)));
}
private:
base::test::ScopedFeatureList scoped_feature_list_;
};
void ToolbarViewTest::RunToolbarCycleFocusTest(Browser* browser) {
gfx::NativeWindow window = browser->window()->GetNativeWindow();
views::Widget* widget = views::Widget::GetWidgetForNativeWindow(window);
// Test relies on browser window activation, while platform such as Linux's
// window activation is asynchronous.
views::test::WaitForWidgetActive(widget, true);
// Send focus to the toolbar as if the user pressed Alt+Shift+T. This should
// happen after the browser window activation.
CommandUpdater* updater = browser->command_controller();
updater->ExecuteCommand(IDC_FOCUS_TOOLBAR);
views::FocusManager* focus_manager = widget->GetFocusManager();
views::View* first_view = focus_manager->GetFocusedView();
std::vector<int> ids;
// Press Tab to cycle through all of the controls in the toolbar until
// we end up back where we started.
bool found_reload = false;
bool found_location_bar = false;
bool found_app_menu = false;
const views::View* view = nullptr;
while (view != first_view) {
focus_manager->AdvanceFocus(false);
view = focus_manager->GetFocusedView();
ids.push_back(view->GetID());
if (view->GetID() == VIEW_ID_RELOAD_BUTTON) {
found_reload = true;
}
if (view->GetID() == VIEW_ID_APP_MENU) {
found_app_menu = true;
}
if (view->GetID() == VIEW_ID_OMNIBOX) {
found_location_bar = true;
}
if (ids.size() > 100) {
GTEST_FAIL() << "Tabbed 100 times, still haven't cycled back!";
}
}
// Make sure we found a few key items.
ASSERT_TRUE(found_reload);
ASSERT_TRUE(found_app_menu);
ASSERT_TRUE(found_location_bar);
// Now press Shift-Tab to cycle backwards.
std::vector<int> reverse_ids;
view = nullptr;
while (view != first_view) {
focus_manager->AdvanceFocus(true);
view = focus_manager->GetFocusedView();
reverse_ids.push_back(view->GetID());
if (reverse_ids.size() > 100) {
GTEST_FAIL() << "Tabbed 100 times, still haven't cycled back!";
}
}
// Assert that the views were focused in exactly the reverse order.
// The sequences should be the same length, and the last element will
// be the same, and the others are reverse.
ASSERT_EQ(ids.size(), reverse_ids.size());
size_t count = ids.size();
for (size_t i = 0; i < count - 1; i++) {
EXPECT_EQ(ids[i], reverse_ids[count - 2 - i]);
}
EXPECT_EQ(ids[count - 1], reverse_ids[count - 1]);
}
IN_PROC_BROWSER_TEST_F(ToolbarViewTest, ToolbarCycleFocus) {
RunToolbarCycleFocusTest(browser());
}
IN_PROC_BROWSER_TEST_F(ToolbarViewTest, ToolbarCycleFocusWithBookmarkBar) {
CommandUpdater* updater = browser()->command_controller();
updater->ExecuteCommand(IDC_SHOW_BOOKMARK_BAR);
BookmarkModel* model =
BookmarkModelFactory::GetForBrowserContext(browser()->profile());
bookmarks::AddIfNotBookmarked(model, GURL("http://foo.com"), u"Foo");
// We want to specifically test the case where the bookmark bar is
// already showing when a window opens, so create a second browser
// window with the same profile.
Browser* second_browser = CreateBrowser(browser()->profile());
RunToolbarCycleFocusTest(second_browser);
}
IN_PROC_BROWSER_TEST_F(ToolbarViewTest, BackButtonUpdate) {
ToolbarButtonProvider* toolbar_button_provider =
BrowserView::GetBrowserViewForBrowser(browser())->toolbar();
ToolbarButton* back_button = toolbar_button_provider->GetBackButton();
EXPECT_FALSE(back_button->GetEnabled());
// Navigate to title1.html. Back button should be enabled.
GURL url = ui_test_utils::GetTestUrl(
base::FilePath(), base::FilePath(FILE_PATH_LITERAL("title1.html")));
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), url));
EXPECT_TRUE(back_button->GetEnabled());
// Delete old navigations. Back button will be disabled.
auto& controller =
browser()->tab_strip_model()->GetActiveWebContents()->GetController();
controller.DeleteNavigationEntries(base::BindRepeating(
[&](content::NavigationEntry* entry) { return true; }));
EXPECT_FALSE(back_button->GetEnabled());
}
IN_PROC_BROWSER_TEST_F(ToolbarViewTest, BackButtonHoverThenClick) {
ToolbarButtonProvider* toolbar_button_provider =
BrowserView::GetBrowserViewForBrowser(browser())->toolbar();
ToolbarButton* back_button = toolbar_button_provider->GetBackButton();
EXPECT_FALSE(back_button->GetEnabled());
// Navigate to title1.html. Back button should be enabled.
GURL url = ui_test_utils::GetTestUrl(
base::FilePath(), base::FilePath(FILE_PATH_LITERAL("title1.html")));
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), url));
EXPECT_TRUE(back_button->GetEnabled());
// Mouse over and click on the back button. This should navigate back in
// session history.
content::TestNavigationObserver back_nav_observer(
browser()->tab_strip_model()->GetActiveWebContents());
ui_test_utils::ClickOnView(back_button);
back_nav_observer.Wait();
EXPECT_FALSE(back_button->GetEnabled());
}
// TODO(crbug.com/40252318): The ui test utils do not seem to adequately
// simulate mouse hovering on Mac.
#if BUILDFLAG(IS_MAC)
#define MAYBE_BackButtonHoverMetricsLogged DISABLED_BackButtonHoverMetricsLogged
#else
#define MAYBE_BackButtonHoverMetricsLogged BackButtonHoverMetricsLogged
#endif
IN_PROC_BROWSER_TEST_F(ToolbarViewTest, MAYBE_BackButtonHoverMetricsLogged) {
ASSERT_TRUE(embedded_test_server()->Start());
ToolbarButtonProvider* toolbar_button_provider =
BrowserView::GetBrowserViewForBrowser(browser())->toolbar();
// Set the initial mouse position to a known state. If the mouse happens to
// be over the back button at the start of the test, then the mouse movement
// done by the test wouldn't be seen as a mouse enter.
// The choice of using the reload button as the starting position is
// arbitrary.
const gfx::Point start_position = ui_test_utils::GetCenterInScreenCoordinates(
toolbar_button_provider->GetReloadButton());
ui_controls::SendMouseMove(start_position.x(), start_position.y());
const GURL first_url =
embedded_test_server()->GetURL("a.test", "/title1.html");
const GURL cross_site_url =
embedded_test_server()->GetURL("b.test", "/title2.html");
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), first_url));
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), cross_site_url));
ToolbarButton* back_button = toolbar_button_provider->GetBackButton();
EXPECT_TRUE(back_button->GetEnabled());
base::HistogramTester histogram_tester;
content::TestNavigationObserver back_nav_observer(
browser()->tab_strip_model()->GetActiveWebContents());
ui_test_utils::ClickOnView(back_button);
back_nav_observer.Wait();
// content/ internal tests cover the details of various navigation scenarios
// in relation to this histogram. It's enough for this test confirm that a
// sample was added, rather than its specific value.
histogram_tester.ExpectTotalCount(
"Preloading.PrerenderBackNavigationEligibility.BackButtonHover", 1);
}
IN_PROC_BROWSER_TEST_F(ToolbarViewTest,
ToolbarForRegularProfileHasExtensionsToolbarContainer) {
// Verify the normal browser has an extensions toolbar container.
ExtensionsToolbarContainer* extensions_container =
BrowserView::GetBrowserViewForBrowser(browser())
->toolbar()
->extensions_container();
EXPECT_NE(nullptr, extensions_container);
}
// TODO(crbug.com/41474891): Setup test profiles properly for CrOS.
#if BUILDFLAG(IS_CHROMEOS)
#define MAYBE_ExtensionsToolbarContainerForGuest \
DISABLED_ExtensionsToolbarContainerForGuest
#else
#define MAYBE_ExtensionsToolbarContainerForGuest \
ExtensionsToolbarContainerForGuest
#endif
IN_PROC_BROWSER_TEST_F(ToolbarViewTest,
MAYBE_ExtensionsToolbarContainerForGuest) {
// Verify guest browser does not have an extensions toolbar container.
profiles::SwitchToGuestProfile();
ui_test_utils::WaitForBrowserToOpen();
Profile* guest = g_browser_process->profile_manager()->GetProfileByPath(
ProfileManager::GetGuestProfilePath());
ASSERT_TRUE(guest);
Browser* target_browser = chrome::FindAnyBrowser(guest, true);
ASSERT_TRUE(target_browser);
ExtensionsToolbarContainer* extensions_container =
BrowserView::GetBrowserViewForBrowser(target_browser)
->toolbar()
->extensions_container();
EXPECT_EQ(nullptr, extensions_container);
}
// Verifies that the identifiers for the pop-up menus are properly assigned so
// that the menu can be located by tests when it is shown.
//
// The back button is just one example for which the menu identifier is defined.
IN_PROC_BROWSER_TEST_F(ToolbarViewTest, BackButtonMenu) {
DEFINE_LOCAL_ELEMENT_IDENTIFIER_VALUE(kWebContentsId);
ASSERT_TRUE(embedded_test_server()->Start());
const GURL url1 = embedded_test_server()->GetURL("/title1.html");
const GURL url2 = embedded_test_server()->GetURL("/title2.html");
const GURL url3 = embedded_test_server()->GetURL("/title3.html");
RunTestSequence(
InstrumentTab(kWebContentsId), NavigateWebContents(kWebContentsId, url1),
NavigateWebContents(kWebContentsId, url2),
NavigateWebContents(kWebContentsId, url3),
// Show the context menu.
MoveMouseTo(kToolbarBackButtonElementId), ClickMouse(ui_controls::RIGHT),
Log("Logging to probe crbug.com/1489499. Waiting for back button menu."),
// Dismiss the context menu by clicking on it.
Log("Moving mouse to menu."),
MoveMouseTo(kToolbarBackButtonMenuElementId),
Log("Clicking mouse to dismiss."), ClickMouse(),
Log("Waiting for menu to dismiss."),
WaitForHide(kToolbarBackButtonMenuElementId), Log("Menu dismissed."));
}
// TODO(crbug.com/402492418): Find workaround for Mac and ChromeOS.
#if BUILDFLAG(IS_CHROMEOS)
#define MAYBE_SplitTabsToolbarButton DISABLED_SplitTabsToolbarButton
#else
#define MAYBE_SplitTabsToolbarButton SplitTabsToolbarButton
#endif
IN_PROC_BROWSER_TEST_F(ToolbarViewTest, MAYBE_SplitTabsToolbarButton) {
DEFINE_LOCAL_ELEMENT_IDENTIFIER_VALUE(kWebContents1Id);
DEFINE_LOCAL_ELEMENT_IDENTIFIER_VALUE(kWebContents2Id);
DEFINE_LOCAL_ELEMENT_IDENTIFIER_VALUE(kWebContents3Id);
ASSERT_TRUE(embedded_test_server()->Start());
const GURL url1 = embedded_test_server()->GetURL("/title1.html");
RunTestSequence(InstrumentTab(kWebContents1Id),
NavigateWebContents(kWebContents1Id, url1),
AddInstrumentedTab(kWebContents2Id, url1),
AddInstrumentedTab(kWebContents3Id, url1),
SelectTab(kTabStripElementId, 0), OpenSideBySideTab(1),
WaitForShow(kToolbarSplitTabsToolbarButtonElementId),
SelectTab(kTabStripElementId, 2),
WaitForHide(kToolbarSplitTabsToolbarButtonElementId));
}
// Tests that the browser updates the toolbar's visible security state only
// when the state changes, not every time it's asked to update.
IN_PROC_BROWSER_TEST_F(ToolbarViewTest, SecurityStateChanged) {
BrowserView* browser_view = BrowserView::GetBrowserViewForBrowser(browser());
// Set the location bar's initial security level and check that the browser
// updates to it.
SetLocationBarSecurityLevelForTesting(security_state::SecurityLevel::SECURE);
EXPECT_TRUE(browser_view->UpdateToolbarSecurityState());
// The security level has not changed, so asking the browser again to update
// should fail.
EXPECT_FALSE(browser_view->UpdateToolbarSecurityState());
// Change the security level and check that the browser updates its toolbar.
SetLocationBarSecurityLevelForTesting(
security_state::SecurityLevel::DANGEROUS);
EXPECT_TRUE(browser_view->UpdateToolbarSecurityState());
}
|