| OLD | NEW |
| (Empty) |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/ui/views/toolbar/toolbar_view.h" | |
| 6 | |
| 7 #include <stddef.h> | |
| 8 | |
| 9 #include "base/macros.h" | |
| 10 #include "base/strings/utf_string_conversions.h" | |
| 11 #include "build/build_config.h" | |
| 12 #include "chrome/app/chrome_command_ids.h" | |
| 13 #include "chrome/browser/bookmarks/bookmark_model_factory.h" | |
| 14 #include "chrome/browser/ui/browser.h" | |
| 15 #include "chrome/browser/ui/browser_command_controller.h" | |
| 16 #include "chrome/browser/ui/browser_window.h" | |
| 17 #include "chrome/browser/ui/view_ids.h" | |
| 18 #include "chrome/browser/ui/views/frame/browser_view.h" | |
| 19 #include "chrome/test/base/in_process_browser_test.h" | |
| 20 #include "components/bookmarks/browser/bookmark_model.h" | |
| 21 #include "components/bookmarks/browser/bookmark_utils.h" | |
| 22 #include "ui/views/focus/focus_manager.h" | |
| 23 #include "ui/views/view.h" | |
| 24 #include "ui/views/widget/widget.h" | |
| 25 | |
| 26 using bookmarks::BookmarkModel; | |
| 27 | |
| 28 namespace { | |
| 29 | |
| 30 class ToolbarViewTest : public InProcessBrowserTest { | |
| 31 public: | |
| 32 ToolbarViewTest() {} | |
| 33 | |
| 34 void RunToolbarCycleFocusTest(Browser* browser); | |
| 35 | |
| 36 private: | |
| 37 DISALLOW_COPY_AND_ASSIGN(ToolbarViewTest); | |
| 38 }; | |
| 39 | |
| 40 void ToolbarViewTest::RunToolbarCycleFocusTest(Browser* browser) { | |
| 41 gfx::NativeWindow window = browser->window()->GetNativeWindow(); | |
| 42 views::Widget* widget = views::Widget::GetWidgetForNativeWindow(window); | |
| 43 views::FocusManager* focus_manager = widget->GetFocusManager(); | |
| 44 CommandUpdater* updater = browser->command_controller()->command_updater(); | |
| 45 | |
| 46 // Send focus to the toolbar as if the user pressed Alt+Shift+T. | |
| 47 updater->ExecuteCommand(IDC_FOCUS_TOOLBAR); | |
| 48 | |
| 49 views::View* first_view = focus_manager->GetFocusedView(); | |
| 50 std::vector<int> ids; | |
| 51 | |
| 52 // Press Tab to cycle through all of the controls in the toolbar until | |
| 53 // we end up back where we started. | |
| 54 bool found_reload = false; | |
| 55 bool found_location_bar = false; | |
| 56 bool found_app_menu = false; | |
| 57 const views::View* view = NULL; | |
| 58 while (view != first_view) { | |
| 59 focus_manager->AdvanceFocus(false); | |
| 60 view = focus_manager->GetFocusedView(); | |
| 61 ids.push_back(view->id()); | |
| 62 if (view->id() == VIEW_ID_RELOAD_BUTTON) | |
| 63 found_reload = true; | |
| 64 if (view->id() == VIEW_ID_APP_MENU) | |
| 65 found_app_menu = true; | |
| 66 if (view->id() == VIEW_ID_OMNIBOX) | |
| 67 found_location_bar = true; | |
| 68 if (ids.size() > 100) | |
| 69 GTEST_FAIL() << "Tabbed 100 times, still haven't cycled back!"; | |
| 70 } | |
| 71 | |
| 72 // Make sure we found a few key items. | |
| 73 ASSERT_TRUE(found_reload); | |
| 74 ASSERT_TRUE(found_app_menu); | |
| 75 ASSERT_TRUE(found_location_bar); | |
| 76 | |
| 77 // Now press Shift-Tab to cycle backwards. | |
| 78 std::vector<int> reverse_ids; | |
| 79 view = NULL; | |
| 80 while (view != first_view) { | |
| 81 focus_manager->AdvanceFocus(true); | |
| 82 view = focus_manager->GetFocusedView(); | |
| 83 reverse_ids.push_back(view->id()); | |
| 84 if (reverse_ids.size() > 100) | |
| 85 GTEST_FAIL() << "Tabbed 100 times, still haven't cycled back!"; | |
| 86 } | |
| 87 | |
| 88 // Assert that the views were focused in exactly the reverse order. | |
| 89 // The sequences should be the same length, and the last element will | |
| 90 // be the same, and the others are reverse. | |
| 91 ASSERT_EQ(ids.size(), reverse_ids.size()); | |
| 92 size_t count = ids.size(); | |
| 93 for (size_t i = 0; i < count - 1; i++) | |
| 94 EXPECT_EQ(ids[i], reverse_ids[count - 2 - i]); | |
| 95 } | |
| 96 | |
| 97 // The test is flaky on Win (http://crbug.com/152938) and crashes on CrOS under | |
| 98 // AddressSanitizer (http://crbug.com/154657). | |
| 99 IN_PROC_BROWSER_TEST_F(ToolbarViewTest, DISABLED_ToolbarCycleFocus) { | |
| 100 RunToolbarCycleFocusTest(browser()); | |
| 101 } | |
| 102 | |
| 103 #if defined(OS_WIN) | |
| 104 // http://crbug.com/152938 Flaky on win. | |
| 105 #define MAYBE_ToolbarCycleFocusWithBookmarkBar \ | |
| 106 DISABLED_ToolbarCycleFocusWithBookmarkBar | |
| 107 #else | |
| 108 #define MAYBE_ToolbarCycleFocusWithBookmarkBar ToolbarCycleFocusWithBookmarkBar | |
| 109 #endif | |
| 110 IN_PROC_BROWSER_TEST_F(ToolbarViewTest, | |
| 111 MAYBE_ToolbarCycleFocusWithBookmarkBar) { | |
| 112 CommandUpdater* updater = browser()->command_controller()->command_updater(); | |
| 113 updater->ExecuteCommand(IDC_SHOW_BOOKMARK_BAR); | |
| 114 | |
| 115 BookmarkModel* model = | |
| 116 BookmarkModelFactory::GetForBrowserContext(browser()->profile()); | |
| 117 bookmarks::AddIfNotBookmarked( | |
| 118 model, GURL("http://foo.com"), base::ASCIIToUTF16("Foo")); | |
| 119 | |
| 120 // We want to specifically test the case where the bookmark bar is | |
| 121 // already showing when a window opens, so create a second browser | |
| 122 // window with the same profile. | |
| 123 Browser* second_browser = CreateBrowser(browser()->profile()); | |
| 124 RunToolbarCycleFocusTest(second_browser); | |
| 125 } | |
| 126 | |
| 127 } // namespace | |
| OLD | NEW |