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 virtual ~TestBarView();
26 virtual void ButtonPressed(Button
* sender
,
27 const ui::Event
& event
) OVERRIDE
;
28 LabelButton
* child_button() const { return child_button_
.get(); }
29 LabelButton
* second_child_button() const {
30 return second_child_button_
.get();
32 LabelButton
* third_child_button() const { return third_child_button_
.get(); }
33 LabelButton
* not_child_button() const { return not_child_button_
.get(); }
35 virtual View
* GetDefaultFocusableChild() OVERRIDE
;
40 scoped_ptr
<LabelButton
> child_button_
;
41 scoped_ptr
<LabelButton
> second_child_button_
;
42 scoped_ptr
<LabelButton
> third_child_button_
;
43 scoped_ptr
<LabelButton
> not_child_button_
;
45 DISALLOW_COPY_AND_ASSIGN(TestBarView
);
48 TestBarView::TestBarView() {
50 set_allow_deactivate_on_esc(true);
53 TestBarView::~TestBarView() {}
55 void TestBarView::ButtonPressed(Button
* sender
, const ui::Event
& event
) {
58 void TestBarView::Init() {
59 SetLayoutManager(new FillLayout());
61 child_button_
.reset(new LabelButton(this, label
));
62 AddChildView(child_button_
.get());
63 second_child_button_
.reset(new LabelButton(this, label
));
64 AddChildView(second_child_button_
.get());
65 third_child_button_
.reset(new LabelButton(this, label
));
66 AddChildView(third_child_button_
.get());
67 not_child_button_
.reset(new LabelButton(this, label
));
70 View
* TestBarView::GetDefaultFocusableChild() {
71 return child_button_
.get();
74 TEST_F(AccessiblePaneViewTest
, SimpleSetPaneFocus
) {
75 TestBarView
* test_view
= new TestBarView();
76 scoped_ptr
<Widget
> widget(new Widget());
77 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
78 params
.ownership
= Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
79 params
.bounds
= gfx::Rect(50, 50, 650, 650);
81 View
* root
= widget
->GetRootView();
82 root
->AddChildView(test_view
);
86 // Set pane focus succeeds, focus on child.
87 EXPECT_TRUE(test_view
->SetPaneFocusAndFocusDefault());
88 EXPECT_EQ(test_view
, test_view
->GetPaneFocusTraversable());
89 EXPECT_EQ(test_view
->child_button(),
90 test_view
->GetWidget()->GetFocusManager()->GetFocusedView());
92 // Set focus on non child view, focus failed, stays on pane.
93 EXPECT_TRUE(test_view
->SetPaneFocus(test_view
->not_child_button()));
94 EXPECT_FALSE(test_view
->not_child_button() ==
95 test_view
->GetWidget()->GetFocusManager()->GetFocusedView());
96 EXPECT_EQ(test_view
->child_button(),
97 test_view
->GetWidget()->GetFocusManager()->GetFocusedView());
102 // This test will not work properly in Windows because it uses ::GetNextWindow
103 // on deactivate which is rather unpredictable where the focus will land.
104 #if !defined(OS_WIN)||defined(USE_AURA)
105 TEST_F(AccessiblePaneViewTest
, SetPaneFocusAndRestore
) {
106 View
* test_view_main
= new View();
107 scoped_ptr
<Widget
> widget_main(new Widget());
108 Widget::InitParams params_main
= CreateParams(Widget::InitParams::TYPE_POPUP
);
109 params_main
.ownership
= Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
110 params_main
.bounds
= gfx::Rect(0, 0, 20, 20);
111 widget_main
->Init(params_main
);
112 View
* root_main
= widget_main
->GetRootView();
113 root_main
->AddChildView(test_view_main
);
114 widget_main
->Activate();
115 test_view_main
->GetFocusManager()->SetFocusedView(test_view_main
);
116 EXPECT_TRUE(widget_main
->IsActive());
117 EXPECT_TRUE(test_view_main
->HasFocus());
119 TestBarView
* test_view_bar
= new TestBarView();
120 scoped_ptr
<Widget
> widget_bar(new Widget());
121 Widget::InitParams params_bar
= CreateParams(Widget::InitParams::TYPE_POPUP
);
122 params_bar
.ownership
= Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
123 params_bar
.bounds
= gfx::Rect(50, 50, 650, 650);
124 widget_bar
->Init(params_bar
);
125 View
* root_bar
= widget_bar
->GetRootView();
126 root_bar
->AddChildView(test_view_bar
);
128 widget_bar
->Activate();
130 // Set pane focus succeeds, focus on child.
131 EXPECT_TRUE(test_view_bar
->SetPaneFocusAndFocusDefault());
132 EXPECT_FALSE(test_view_main
->HasFocus());
133 EXPECT_FALSE(widget_main
->IsActive());
134 EXPECT_EQ(test_view_bar
, test_view_bar
->GetPaneFocusTraversable());
135 EXPECT_EQ(test_view_bar
->child_button(),
136 test_view_bar
->GetWidget()->GetFocusManager()->GetFocusedView());
138 test_view_bar
->AcceleratorPressed(test_view_bar
->escape_key());
139 EXPECT_TRUE(widget_main
->IsActive());
140 EXPECT_FALSE(widget_bar
->IsActive());
142 widget_bar
->CloseNow();
145 widget_main
->CloseNow();
148 #endif // !defined(OS_WIN)||defined(USE_AURA)
150 TEST_F(AccessiblePaneViewTest
, TwoSetPaneFocus
) {
151 TestBarView
* test_view
= new TestBarView();
152 TestBarView
* test_view_2
= new TestBarView();
153 scoped_ptr
<Widget
> widget(new Widget());
154 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
155 params
.ownership
= Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
156 params
.bounds
= gfx::Rect(50, 50, 650, 650);
157 widget
->Init(params
);
158 View
* root
= widget
->GetRootView();
159 root
->AddChildView(test_view
);
160 root
->AddChildView(test_view_2
);
164 // Set pane focus succeeds, focus on child.
165 EXPECT_TRUE(test_view
->SetPaneFocusAndFocusDefault());
166 EXPECT_EQ(test_view
, test_view
->GetPaneFocusTraversable());
167 EXPECT_EQ(test_view
->child_button(),
168 test_view
->GetWidget()->GetFocusManager()->GetFocusedView());
170 // Set focus on another test_view, focus move to that pane.
171 EXPECT_TRUE(test_view_2
->SetPaneFocus(test_view_2
->second_child_button()));
172 EXPECT_FALSE(test_view
->child_button() ==
173 test_view
->GetWidget()->GetFocusManager()->GetFocusedView());
174 EXPECT_EQ(test_view_2
->second_child_button(),
175 test_view
->GetWidget()->GetFocusManager()->GetFocusedView());
180 TEST_F(AccessiblePaneViewTest
, PaneFocusTraversal
) {
181 TestBarView
* test_view
= new TestBarView();
182 TestBarView
* original_test_view
= new TestBarView();
183 scoped_ptr
<Widget
> widget(new Widget());
184 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
185 params
.ownership
= Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
186 params
.bounds
= gfx::Rect(50, 50, 650, 650);
187 widget
->Init(params
);
188 View
* root
= widget
->GetRootView();
189 root
->AddChildView(original_test_view
);
190 root
->AddChildView(test_view
);
194 // Set pane focus on first view.
195 EXPECT_TRUE(original_test_view
->SetPaneFocus(
196 original_test_view
->third_child_button()));
198 // Test travesal in second view.
199 // Set pane focus on second child.
200 EXPECT_TRUE(test_view
->SetPaneFocus(test_view
->second_child_button()));
202 test_view
->AcceleratorPressed(test_view
->home_key());
203 EXPECT_EQ(test_view
->child_button(),
204 test_view
->GetWidget()->GetFocusManager()->GetFocusedView());
206 test_view
->AcceleratorPressed(test_view
->end_key());
207 EXPECT_EQ(test_view
->third_child_button(),
208 test_view
->GetWidget()->GetFocusManager()->GetFocusedView());
210 test_view
->AcceleratorPressed(test_view
->left_key());
211 EXPECT_EQ(test_view
->second_child_button(),
212 test_view
->GetWidget()->GetFocusManager()->GetFocusedView());
214 test_view
->AcceleratorPressed(test_view
->right_key());
215 test_view
->AcceleratorPressed(test_view
->right_key());
216 EXPECT_EQ(test_view
->child_button(),
217 test_view
->GetWidget()->GetFocusManager()->GetFocusedView());
220 test_view
->AcceleratorPressed(test_view
->escape_key());
221 EXPECT_EQ(original_test_view
->third_child_button(),
222 test_view
->GetWidget()->GetFocusManager()->GetFocusedView());