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