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"
24 class ToolbarViewTest
: public InProcessBrowserTest
{
28 void RunToolbarCycleFocusTest(Browser
* browser
);
31 DISALLOW_COPY_AND_ASSIGN(ToolbarViewTest
);
34 void ToolbarViewTest::RunToolbarCycleFocusTest(Browser
* browser
) {
35 gfx::NativeWindow window
= browser
->window()->GetNativeWindow();
36 views::Widget
* widget
= views::Widget::GetWidgetForNativeWindow(window
);
37 views::FocusManager
* focus_manager
= widget
->GetFocusManager();
38 CommandUpdater
* updater
= browser
->command_controller()->command_updater();
40 // Send focus to the toolbar as if the user pressed Alt+Shift+T.
41 updater
->ExecuteCommand(IDC_FOCUS_TOOLBAR
);
43 views::View
* first_view
= focus_manager
->GetFocusedView();
46 // Press Tab to cycle through all of the controls in the toolbar until
47 // we end up back where we started.
48 bool found_reload
= false;
49 bool found_location_bar
= false;
50 bool found_app_menu
= false;
51 const views::View
* view
= NULL
;
52 while (view
!= first_view
) {
53 focus_manager
->AdvanceFocus(false);
54 view
= focus_manager
->GetFocusedView();
55 ids
.push_back(view
->id());
56 if (view
->id() == VIEW_ID_RELOAD_BUTTON
)
58 if (view
->id() == VIEW_ID_APP_MENU
)
59 found_app_menu
= true;
60 if (view
->id() == VIEW_ID_OMNIBOX
)
61 found_location_bar
= true;
63 GTEST_FAIL() << "Tabbed 100 times, still haven't cycled back!";
66 // Make sure we found a few key items.
67 ASSERT_TRUE(found_reload
);
68 ASSERT_TRUE(found_app_menu
);
69 ASSERT_TRUE(found_location_bar
);
71 // Now press Shift-Tab to cycle backwards.
72 std::vector
<int> reverse_ids
;
74 while (view
!= first_view
) {
75 focus_manager
->AdvanceFocus(true);
76 view
= focus_manager
->GetFocusedView();
77 reverse_ids
.push_back(view
->id());
78 if (reverse_ids
.size() > 100)
79 GTEST_FAIL() << "Tabbed 100 times, still haven't cycled back!";
82 // Assert that the views were focused in exactly the reverse order.
83 // The sequences should be the same length, and the last element will
84 // be the same, and the others are reverse.
85 ASSERT_EQ(ids
.size(), reverse_ids
.size());
86 size_t count
= ids
.size();
87 for (size_t i
= 0; i
< count
- 1; i
++)
88 EXPECT_EQ(ids
[i
], reverse_ids
[count
- 2 - i
]);
91 // The test is flaky on Win (http://crbug.com/152938) and crashes on CrOS under
92 // AddressSanitizer (http://crbug.com/154657).
93 IN_PROC_BROWSER_TEST_F(ToolbarViewTest
, DISABLED_ToolbarCycleFocus
) {
94 RunToolbarCycleFocusTest(browser());
98 // http://crbug.com/152938 Flaky on win.
99 #define MAYBE_ToolbarCycleFocusWithBookmarkBar \
100 DISABLED_ToolbarCycleFocusWithBookmarkBar
102 #define MAYBE_ToolbarCycleFocusWithBookmarkBar ToolbarCycleFocusWithBookmarkBar
104 IN_PROC_BROWSER_TEST_F(ToolbarViewTest
,
105 MAYBE_ToolbarCycleFocusWithBookmarkBar
) {
106 CommandUpdater
* updater
= browser()->command_controller()->command_updater();
107 updater
->ExecuteCommand(IDC_SHOW_BOOKMARK_BAR
);
109 BookmarkModel
* model
=
110 BookmarkModelFactory::GetForProfile(browser()->profile());
111 bookmark_utils::AddIfNotBookmarked(
112 model
, GURL("http://foo.com"), base::ASCIIToUTF16("Foo"));
114 // We want to specifically test the case where the bookmark bar is
115 // already showing when a window opens, so create a second browser
116 // window with the same profile.
117 Browser
* second_browser
= CreateBrowser(browser()->profile());
118 RunToolbarCycleFocusTest(second_browser
);