1 // Copyright (c) 2012 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 "ui/views/accessible_pane_view.h"
7 #include "ui/base/accelerators/accelerator.h"
8 #include "ui/views/controls/button/label_button.h"
9 #include "ui/views/layout/fill_layout.h"
10 #include "ui/views/test/views_test_base.h"
11 #include "ui/views/widget/widget.h"
15 // TODO(alicet): bring pane rotation into views and add tests.
16 // See browser_view.cc for details.
18 typedef ViewsTestBase AccessiblePaneViewTest
;
20 class TestBarView
: public AccessiblePaneView
,
21 public ButtonListener
{
24 ~TestBarView() override
;
26 void ButtonPressed(Button
* sender
, const ui::Event
& event
) override
;
27 LabelButton
* child_button() const { return child_button_
.get(); }
28 LabelButton
* second_child_button() const {
29 return second_child_button_
.get();
31 LabelButton
* third_child_button() const { return third_child_button_
.get(); }
32 LabelButton
* not_child_button() const { return not_child_button_
.get(); }
34 View
* GetDefaultFocusableChild() override
;
39 scoped_ptr
<LabelButton
> child_button_
;
40 scoped_ptr
<LabelButton
> second_child_button_
;
41 scoped_ptr
<LabelButton
> third_child_button_
;
42 scoped_ptr
<LabelButton
> not_child_button_
;
44 DISALLOW_COPY_AND_ASSIGN(TestBarView
);
47 TestBarView::TestBarView() {
49 set_allow_deactivate_on_esc(true);
52 TestBarView::~TestBarView() {}
54 void TestBarView::ButtonPressed(Button
* sender
, const ui::Event
& event
) {
57 void TestBarView::Init() {
58 SetLayoutManager(new FillLayout());
60 child_button_
.reset(new LabelButton(this, label
));
61 AddChildView(child_button_
.get());
62 second_child_button_
.reset(new LabelButton(this, label
));
63 AddChildView(second_child_button_
.get());
64 third_child_button_
.reset(new LabelButton(this, label
));
65 AddChildView(third_child_button_
.get());
66 not_child_button_
.reset(new LabelButton(this, label
));
69 View
* TestBarView::GetDefaultFocusableChild() {
70 return child_button_
.get();
73 TEST_F(AccessiblePaneViewTest
, SimpleSetPaneFocus
) {
74 TestBarView
* test_view
= new TestBarView();
75 scoped_ptr
<Widget
> widget(new Widget());
76 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
77 params
.ownership
= Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
78 params
.bounds
= gfx::Rect(50, 50, 650, 650);
80 View
* root
= widget
->GetRootView();
81 root
->AddChildView(test_view
);
85 // Set pane focus succeeds, focus on child.
86 EXPECT_TRUE(test_view
->SetPaneFocusAndFocusDefault());
87 EXPECT_EQ(test_view
, test_view
->GetPaneFocusTraversable());
88 EXPECT_EQ(test_view
->child_button(),
89 test_view
->GetWidget()->GetFocusManager()->GetFocusedView());
91 // Set focus on non child view, focus failed, stays on pane.
92 EXPECT_TRUE(test_view
->SetPaneFocus(test_view
->not_child_button()));
93 EXPECT_FALSE(test_view
->not_child_button() ==
94 test_view
->GetWidget()->GetFocusManager()->GetFocusedView());
95 EXPECT_EQ(test_view
->child_button(),
96 test_view
->GetWidget()->GetFocusManager()->GetFocusedView());
101 // This test will not work properly in Windows because it uses ::GetNextWindow
102 // on deactivate which is rather unpredictable where the focus will land.
103 TEST_F(AccessiblePaneViewTest
, SetPaneFocusAndRestore
) {
104 View
* test_view_main
= new View();
105 scoped_ptr
<Widget
> widget_main(new Widget());
106 Widget::InitParams params_main
= CreateParams(Widget::InitParams::TYPE_POPUP
);
107 params_main
.ownership
= Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
108 params_main
.bounds
= gfx::Rect(0, 0, 20, 20);
109 widget_main
->Init(params_main
);
110 View
* root_main
= widget_main
->GetRootView();
111 root_main
->AddChildView(test_view_main
);
112 widget_main
->Activate();
113 test_view_main
->GetFocusManager()->SetFocusedView(test_view_main
);
114 EXPECT_TRUE(widget_main
->IsActive());
115 EXPECT_TRUE(test_view_main
->HasFocus());
117 TestBarView
* test_view_bar
= new TestBarView();
118 scoped_ptr
<Widget
> widget_bar(new Widget());
119 Widget::InitParams params_bar
= CreateParams(Widget::InitParams::TYPE_POPUP
);
120 params_bar
.ownership
= Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
121 params_bar
.bounds
= gfx::Rect(50, 50, 650, 650);
122 widget_bar
->Init(params_bar
);
123 View
* root_bar
= widget_bar
->GetRootView();
124 root_bar
->AddChildView(test_view_bar
);
126 widget_bar
->Activate();
128 // Set pane focus succeeds, focus on child.
129 EXPECT_TRUE(test_view_bar
->SetPaneFocusAndFocusDefault());
130 EXPECT_FALSE(test_view_main
->HasFocus());
131 EXPECT_FALSE(widget_main
->IsActive());
132 EXPECT_EQ(test_view_bar
, test_view_bar
->GetPaneFocusTraversable());
133 EXPECT_EQ(test_view_bar
->child_button(),
134 test_view_bar
->GetWidget()->GetFocusManager()->GetFocusedView());
136 test_view_bar
->AcceleratorPressed(test_view_bar
->escape_key());
137 EXPECT_TRUE(widget_main
->IsActive());
138 EXPECT_FALSE(widget_bar
->IsActive());
140 widget_bar
->CloseNow();
143 widget_main
->CloseNow();
147 TEST_F(AccessiblePaneViewTest
, TwoSetPaneFocus
) {
148 TestBarView
* test_view
= new TestBarView();
149 TestBarView
* test_view_2
= new TestBarView();
150 scoped_ptr
<Widget
> widget(new Widget());
151 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
152 params
.ownership
= Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
153 params
.bounds
= gfx::Rect(50, 50, 650, 650);
154 widget
->Init(params
);
155 View
* root
= widget
->GetRootView();
156 root
->AddChildView(test_view
);
157 root
->AddChildView(test_view_2
);
161 // Set pane focus succeeds, focus on child.
162 EXPECT_TRUE(test_view
->SetPaneFocusAndFocusDefault());
163 EXPECT_EQ(test_view
, test_view
->GetPaneFocusTraversable());
164 EXPECT_EQ(test_view
->child_button(),
165 test_view
->GetWidget()->GetFocusManager()->GetFocusedView());
167 // Set focus on another test_view, focus move to that pane.
168 EXPECT_TRUE(test_view_2
->SetPaneFocus(test_view_2
->second_child_button()));
169 EXPECT_FALSE(test_view
->child_button() ==
170 test_view
->GetWidget()->GetFocusManager()->GetFocusedView());
171 EXPECT_EQ(test_view_2
->second_child_button(),
172 test_view
->GetWidget()->GetFocusManager()->GetFocusedView());
177 TEST_F(AccessiblePaneViewTest
, PaneFocusTraversal
) {
178 TestBarView
* test_view
= new TestBarView();
179 TestBarView
* original_test_view
= new TestBarView();
180 scoped_ptr
<Widget
> widget(new Widget());
181 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
182 params
.ownership
= Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
183 params
.bounds
= gfx::Rect(50, 50, 650, 650);
184 widget
->Init(params
);
185 View
* root
= widget
->GetRootView();
186 root
->AddChildView(original_test_view
);
187 root
->AddChildView(test_view
);
191 // Set pane focus on first view.
192 EXPECT_TRUE(original_test_view
->SetPaneFocus(
193 original_test_view
->third_child_button()));
195 // Test travesal in second view.
196 // Set pane focus on second child.
197 EXPECT_TRUE(test_view
->SetPaneFocus(test_view
->second_child_button()));
199 test_view
->AcceleratorPressed(test_view
->home_key());
200 EXPECT_EQ(test_view
->child_button(),
201 test_view
->GetWidget()->GetFocusManager()->GetFocusedView());
203 test_view
->AcceleratorPressed(test_view
->end_key());
204 EXPECT_EQ(test_view
->third_child_button(),
205 test_view
->GetWidget()->GetFocusManager()->GetFocusedView());
207 test_view
->AcceleratorPressed(test_view
->left_key());
208 EXPECT_EQ(test_view
->second_child_button(),
209 test_view
->GetWidget()->GetFocusManager()->GetFocusedView());
211 test_view
->AcceleratorPressed(test_view
->right_key());
212 test_view
->AcceleratorPressed(test_view
->right_key());
213 EXPECT_EQ(test_view
->child_button(),
214 test_view
->GetWidget()->GetFocusManager()->GetFocusedView());
217 test_view
->AcceleratorPressed(test_view
->escape_key());
218 EXPECT_EQ(original_test_view
->third_child_button(),
219 test_view
->GetWidget()->GetFocusManager()->GetFocusedView());