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/text_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 TextButton
* child_button() const { return child_button_
.get(); }
29 TextButton
* second_child_button() const { return second_child_button_
.get(); }
30 TextButton
* third_child_button() const { return third_child_button_
.get(); }
31 TextButton
* not_child_button() const { return not_child_button_
.get(); }
33 virtual View
* GetDefaultFocusableChild() OVERRIDE
;
38 scoped_ptr
<TextButton
> child_button_
;
39 scoped_ptr
<TextButton
> second_child_button_
;
40 scoped_ptr
<TextButton
> third_child_button_
;
41 scoped_ptr
<TextButton
> not_child_button_
;
43 DISALLOW_COPY_AND_ASSIGN(TestBarView
);
46 TestBarView::TestBarView() {
50 TestBarView::~TestBarView() {}
52 void TestBarView::ButtonPressed(views::Button
* sender
, const ui::Event
& event
) {
55 void TestBarView::Init() {
56 SetLayoutManager(new views::FillLayout());
58 child_button_
.reset(new TextButton(this, label
));
59 AddChildView(child_button_
.get());
60 second_child_button_
.reset(new TextButton(this, label
));
61 AddChildView(second_child_button_
.get());
62 third_child_button_
.reset(new TextButton(this, label
));
63 AddChildView(third_child_button_
.get());
64 not_child_button_
.reset(new TextButton(this, label
));
67 View
* TestBarView::GetDefaultFocusableChild() {
68 return child_button_
.get();
71 TEST_F(AccessiblePaneViewTest
, SimpleSetPaneFocus
) {
72 TestBarView
* test_view
= new TestBarView();
73 scoped_ptr
<Widget
> widget(new Widget());
74 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
75 params
.ownership
= views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
76 params
.bounds
= gfx::Rect(50, 50, 650, 650);
78 View
* root
= widget
->GetRootView();
79 root
->AddChildView(test_view
);
83 // Set pane focus succeeds, focus on child.
84 EXPECT_TRUE(test_view
->SetPaneFocusAndFocusDefault());
85 EXPECT_EQ(test_view
, test_view
->GetPaneFocusTraversable());
86 EXPECT_EQ(test_view
->child_button(),
87 test_view
->GetWidget()->GetFocusManager()->GetFocusedView());
89 // Set focus on non child view, focus failed, stays on pane.
90 EXPECT_TRUE(test_view
->SetPaneFocus(test_view
->not_child_button()));
91 EXPECT_FALSE(test_view
->not_child_button() ==
92 test_view
->GetWidget()->GetFocusManager()->GetFocusedView());
93 EXPECT_EQ(test_view
->child_button(),
94 test_view
->GetWidget()->GetFocusManager()->GetFocusedView());
99 TEST_F(AccessiblePaneViewTest
, TwoSetPaneFocus
) {
100 TestBarView
* test_view
= new TestBarView();
101 TestBarView
* test_view_2
= new TestBarView();
102 scoped_ptr
<Widget
> widget(new Widget());
103 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
104 params
.ownership
= views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
105 params
.bounds
= gfx::Rect(50, 50, 650, 650);
106 widget
->Init(params
);
107 View
* root
= widget
->GetRootView();
108 root
->AddChildView(test_view
);
109 root
->AddChildView(test_view_2
);
113 // Set pane focus succeeds, focus on child.
114 EXPECT_TRUE(test_view
->SetPaneFocusAndFocusDefault());
115 EXPECT_EQ(test_view
, test_view
->GetPaneFocusTraversable());
116 EXPECT_EQ(test_view
->child_button(),
117 test_view
->GetWidget()->GetFocusManager()->GetFocusedView());
119 // Set focus on another test_view, focus move to that pane.
120 EXPECT_TRUE(test_view_2
->SetPaneFocus(test_view_2
->second_child_button()));
121 EXPECT_FALSE(test_view
->child_button() ==
122 test_view
->GetWidget()->GetFocusManager()->GetFocusedView());
123 EXPECT_EQ(test_view_2
->second_child_button(),
124 test_view
->GetWidget()->GetFocusManager()->GetFocusedView());
129 TEST_F(AccessiblePaneViewTest
, PaneFocusTraversal
) {
130 TestBarView
* test_view
= new TestBarView();
131 TestBarView
* original_test_view
= new TestBarView();
132 scoped_ptr
<Widget
> widget(new Widget());
133 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
134 params
.ownership
= views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
135 params
.bounds
= gfx::Rect(50, 50, 650, 650);
136 widget
->Init(params
);
137 View
* root
= widget
->GetRootView();
138 root
->AddChildView(original_test_view
);
139 root
->AddChildView(test_view
);
143 // Set pane focus on first view.
144 EXPECT_TRUE(original_test_view
->SetPaneFocus(
145 original_test_view
->third_child_button()));
147 // Test travesal in second view.
148 // Set pane focus on second child.
149 EXPECT_TRUE(test_view
->SetPaneFocus(test_view
->second_child_button()));
151 test_view
->AcceleratorPressed(test_view
->home_key());
152 EXPECT_EQ(test_view
->child_button(),
153 test_view
->GetWidget()->GetFocusManager()->GetFocusedView());
155 test_view
->AcceleratorPressed(test_view
->end_key());
156 EXPECT_EQ(test_view
->third_child_button(),
157 test_view
->GetWidget()->GetFocusManager()->GetFocusedView());
159 test_view
->AcceleratorPressed(test_view
->left_key());
160 EXPECT_EQ(test_view
->second_child_button(),
161 test_view
->GetWidget()->GetFocusManager()->GetFocusedView());
163 test_view
->AcceleratorPressed(test_view
->right_key());
164 test_view
->AcceleratorPressed(test_view
->right_key());
165 EXPECT_EQ(test_view
->child_button(),
166 test_view
->GetWidget()->GetFocusManager()->GetFocusedView());
169 test_view
->AcceleratorPressed(test_view
->escape_key());
170 EXPECT_EQ(original_test_view
->third_child_button(),
171 test_view
->GetWidget()->GetFocusManager()->GetFocusedView());