| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ui/views/accessible_pane_view.h" | 5 #include "ui/views/accessible_pane_view.h" |
| 6 | 6 |
| 7 #include "ui/base/accelerators/accelerator.h" | 7 #include "ui/base/accelerators/accelerator.h" |
| 8 #include "ui/views/controls/button/text_button.h" | 8 #include "ui/views/controls/button/label_button.h" |
| 9 #include "ui/views/layout/fill_layout.h" | 9 #include "ui/views/layout/fill_layout.h" |
| 10 #include "ui/views/test/views_test_base.h" | 10 #include "ui/views/test/views_test_base.h" |
| 11 #include "ui/views/widget/widget.h" | 11 #include "ui/views/widget/widget.h" |
| 12 | 12 |
| 13 namespace views { | 13 namespace views { |
| 14 | 14 |
| 15 // TODO(alicet): bring pane rotation into views and add tests. | 15 // TODO(alicet): bring pane rotation into views and add tests. |
| 16 // See browser_view.cc for details. | 16 // See browser_view.cc for details. |
| 17 | 17 |
| 18 typedef ViewsTestBase AccessiblePaneViewTest; | 18 typedef ViewsTestBase AccessiblePaneViewTest; |
| 19 | 19 |
| 20 class TestBarView : public AccessiblePaneView, | 20 class TestBarView : public AccessiblePaneView, |
| 21 public ButtonListener { | 21 public ButtonListener { |
| 22 public: | 22 public: |
| 23 TestBarView(); | 23 TestBarView(); |
| 24 virtual ~TestBarView(); | 24 virtual ~TestBarView(); |
| 25 | 25 |
| 26 virtual void ButtonPressed(Button* sender, | 26 virtual void ButtonPressed(Button* sender, |
| 27 const ui::Event& event) OVERRIDE; | 27 const ui::Event& event) OVERRIDE; |
| 28 TextButton* child_button() const { return child_button_.get(); } | 28 LabelButton* child_button() const { return child_button_.get(); } |
| 29 TextButton* second_child_button() const { return second_child_button_.get(); } | 29 LabelButton* second_child_button() const { |
| 30 TextButton* third_child_button() const { return third_child_button_.get(); } | 30 return second_child_button_.get(); |
| 31 TextButton* not_child_button() const { return not_child_button_.get(); } | 31 } |
| 32 LabelButton* third_child_button() const { return third_child_button_.get(); } |
| 33 LabelButton* not_child_button() const { return not_child_button_.get(); } |
| 32 | 34 |
| 33 virtual View* GetDefaultFocusableChild() OVERRIDE; | 35 virtual View* GetDefaultFocusableChild() OVERRIDE; |
| 34 | 36 |
| 35 private: | 37 private: |
| 36 void Init(); | 38 void Init(); |
| 37 | 39 |
| 38 scoped_ptr<TextButton> child_button_; | 40 scoped_ptr<LabelButton> child_button_; |
| 39 scoped_ptr<TextButton> second_child_button_; | 41 scoped_ptr<LabelButton> second_child_button_; |
| 40 scoped_ptr<TextButton> third_child_button_; | 42 scoped_ptr<LabelButton> third_child_button_; |
| 41 scoped_ptr<TextButton> not_child_button_; | 43 scoped_ptr<LabelButton> not_child_button_; |
| 42 | 44 |
| 43 DISALLOW_COPY_AND_ASSIGN(TestBarView); | 45 DISALLOW_COPY_AND_ASSIGN(TestBarView); |
| 44 }; | 46 }; |
| 45 | 47 |
| 46 TestBarView::TestBarView() { | 48 TestBarView::TestBarView() { |
| 47 Init(); | 49 Init(); |
| 48 set_allow_deactivate_on_esc(true); | 50 set_allow_deactivate_on_esc(true); |
| 49 } | 51 } |
| 50 | 52 |
| 51 TestBarView::~TestBarView() {} | 53 TestBarView::~TestBarView() {} |
| 52 | 54 |
| 53 void TestBarView::ButtonPressed(views::Button* sender, const ui::Event& event) { | 55 void TestBarView::ButtonPressed(Button* sender, const ui::Event& event) { |
| 54 } | 56 } |
| 55 | 57 |
| 56 void TestBarView::Init() { | 58 void TestBarView::Init() { |
| 57 SetLayoutManager(new views::FillLayout()); | 59 SetLayoutManager(new FillLayout()); |
| 58 string16 label; | 60 string16 label; |
| 59 child_button_.reset(new TextButton(this, label)); | 61 child_button_.reset(new LabelButton(this, label)); |
| 60 AddChildView(child_button_.get()); | 62 AddChildView(child_button_.get()); |
| 61 second_child_button_.reset(new TextButton(this, label)); | 63 second_child_button_.reset(new LabelButton(this, label)); |
| 62 AddChildView(second_child_button_.get()); | 64 AddChildView(second_child_button_.get()); |
| 63 third_child_button_.reset(new TextButton(this, label)); | 65 third_child_button_.reset(new LabelButton(this, label)); |
| 64 AddChildView(third_child_button_.get()); | 66 AddChildView(third_child_button_.get()); |
| 65 not_child_button_.reset(new TextButton(this, label)); | 67 not_child_button_.reset(new LabelButton(this, label)); |
| 66 } | 68 } |
| 67 | 69 |
| 68 View* TestBarView::GetDefaultFocusableChild() { | 70 View* TestBarView::GetDefaultFocusableChild() { |
| 69 return child_button_.get(); | 71 return child_button_.get(); |
| 70 } | 72 } |
| 71 | 73 |
| 72 TEST_F(AccessiblePaneViewTest, SimpleSetPaneFocus) { | 74 TEST_F(AccessiblePaneViewTest, SimpleSetPaneFocus) { |
| 73 TestBarView* test_view = new TestBarView(); | 75 TestBarView* test_view = new TestBarView(); |
| 74 scoped_ptr<Widget> widget(new Widget()); | 76 scoped_ptr<Widget> widget(new Widget()); |
| 75 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP); | 77 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP); |
| 76 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; | 78 params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; |
| 77 params.bounds = gfx::Rect(50, 50, 650, 650); | 79 params.bounds = gfx::Rect(50, 50, 650, 650); |
| 78 widget->Init(params); | 80 widget->Init(params); |
| 79 View* root = widget->GetRootView(); | 81 View* root = widget->GetRootView(); |
| 80 root->AddChildView(test_view); | 82 root->AddChildView(test_view); |
| 81 widget->Show(); | 83 widget->Show(); |
| 82 widget->Activate(); | 84 widget->Activate(); |
| 83 | 85 |
| 84 // Set pane focus succeeds, focus on child. | 86 // Set pane focus succeeds, focus on child. |
| 85 EXPECT_TRUE(test_view->SetPaneFocusAndFocusDefault()); | 87 EXPECT_TRUE(test_view->SetPaneFocusAndFocusDefault()); |
| 86 EXPECT_EQ(test_view, test_view->GetPaneFocusTraversable()); | 88 EXPECT_EQ(test_view, test_view->GetPaneFocusTraversable()); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 97 widget.reset(); | 99 widget.reset(); |
| 98 } | 100 } |
| 99 | 101 |
| 100 // This test will not work properly in Windows because it uses ::GetNextWindow | 102 // This test will not work properly in Windows because it uses ::GetNextWindow |
| 101 // on deactivate which is rather unpredictable where the focus will land. | 103 // on deactivate which is rather unpredictable where the focus will land. |
| 102 #if !defined(OS_WIN)||defined(USE_AURA) | 104 #if !defined(OS_WIN)||defined(USE_AURA) |
| 103 TEST_F(AccessiblePaneViewTest, SetPaneFocusAndRestore) { | 105 TEST_F(AccessiblePaneViewTest, SetPaneFocusAndRestore) { |
| 104 View* test_view_main = new View(); | 106 View* test_view_main = new View(); |
| 105 scoped_ptr<Widget> widget_main(new Widget()); | 107 scoped_ptr<Widget> widget_main(new Widget()); |
| 106 Widget::InitParams params_main = CreateParams(Widget::InitParams::TYPE_POPUP); | 108 Widget::InitParams params_main = CreateParams(Widget::InitParams::TYPE_POPUP); |
| 107 params_main.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; | 109 params_main.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; |
| 108 params_main.bounds = gfx::Rect(0, 0, 20, 20); | 110 params_main.bounds = gfx::Rect(0, 0, 20, 20); |
| 109 widget_main->Init(params_main); | 111 widget_main->Init(params_main); |
| 110 View* root_main = widget_main->GetRootView(); | 112 View* root_main = widget_main->GetRootView(); |
| 111 root_main->AddChildView(test_view_main); | 113 root_main->AddChildView(test_view_main); |
| 112 widget_main->Activate(); | 114 widget_main->Activate(); |
| 113 test_view_main->GetFocusManager()->SetFocusedView(test_view_main); | 115 test_view_main->GetFocusManager()->SetFocusedView(test_view_main); |
| 114 EXPECT_TRUE(widget_main->IsActive()); | 116 EXPECT_TRUE(widget_main->IsActive()); |
| 115 EXPECT_TRUE(test_view_main->HasFocus()); | 117 EXPECT_TRUE(test_view_main->HasFocus()); |
| 116 | 118 |
| 117 TestBarView* test_view_bar = new TestBarView(); | 119 TestBarView* test_view_bar = new TestBarView(); |
| 118 scoped_ptr<Widget> widget_bar(new Widget()); | 120 scoped_ptr<Widget> widget_bar(new Widget()); |
| 119 Widget::InitParams params_bar = CreateParams(Widget::InitParams::TYPE_POPUP); | 121 Widget::InitParams params_bar = CreateParams(Widget::InitParams::TYPE_POPUP); |
| 120 params_bar.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; | 122 params_bar.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; |
| 121 params_bar.bounds = gfx::Rect(50, 50, 650, 650); | 123 params_bar.bounds = gfx::Rect(50, 50, 650, 650); |
| 122 widget_bar->Init(params_bar); | 124 widget_bar->Init(params_bar); |
| 123 View* root_bar = widget_bar->GetRootView(); | 125 View* root_bar = widget_bar->GetRootView(); |
| 124 root_bar->AddChildView(test_view_bar); | 126 root_bar->AddChildView(test_view_bar); |
| 125 widget_bar->Show(); | 127 widget_bar->Show(); |
| 126 widget_bar->Activate(); | 128 widget_bar->Activate(); |
| 127 | 129 |
| 128 // Set pane focus succeeds, focus on child. | 130 // Set pane focus succeeds, focus on child. |
| 129 EXPECT_TRUE(test_view_bar->SetPaneFocusAndFocusDefault()); | 131 EXPECT_TRUE(test_view_bar->SetPaneFocusAndFocusDefault()); |
| 130 EXPECT_FALSE(test_view_main->HasFocus()); | 132 EXPECT_FALSE(test_view_main->HasFocus()); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 143 widget_main->CloseNow(); | 145 widget_main->CloseNow(); |
| 144 widget_main.reset(); | 146 widget_main.reset(); |
| 145 } | 147 } |
| 146 #endif // !defined(OS_WIN)||defined(USE_AURA) | 148 #endif // !defined(OS_WIN)||defined(USE_AURA) |
| 147 | 149 |
| 148 TEST_F(AccessiblePaneViewTest, TwoSetPaneFocus) { | 150 TEST_F(AccessiblePaneViewTest, TwoSetPaneFocus) { |
| 149 TestBarView* test_view = new TestBarView(); | 151 TestBarView* test_view = new TestBarView(); |
| 150 TestBarView* test_view_2 = new TestBarView(); | 152 TestBarView* test_view_2 = new TestBarView(); |
| 151 scoped_ptr<Widget> widget(new Widget()); | 153 scoped_ptr<Widget> widget(new Widget()); |
| 152 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP); | 154 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP); |
| 153 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; | 155 params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; |
| 154 params.bounds = gfx::Rect(50, 50, 650, 650); | 156 params.bounds = gfx::Rect(50, 50, 650, 650); |
| 155 widget->Init(params); | 157 widget->Init(params); |
| 156 View* root = widget->GetRootView(); | 158 View* root = widget->GetRootView(); |
| 157 root->AddChildView(test_view); | 159 root->AddChildView(test_view); |
| 158 root->AddChildView(test_view_2); | 160 root->AddChildView(test_view_2); |
| 159 widget->Show(); | 161 widget->Show(); |
| 160 widget->Activate(); | 162 widget->Activate(); |
| 161 | 163 |
| 162 // Set pane focus succeeds, focus on child. | 164 // Set pane focus succeeds, focus on child. |
| 163 EXPECT_TRUE(test_view->SetPaneFocusAndFocusDefault()); | 165 EXPECT_TRUE(test_view->SetPaneFocusAndFocusDefault()); |
| 164 EXPECT_EQ(test_view, test_view->GetPaneFocusTraversable()); | 166 EXPECT_EQ(test_view, test_view->GetPaneFocusTraversable()); |
| 165 EXPECT_EQ(test_view->child_button(), | 167 EXPECT_EQ(test_view->child_button(), |
| 166 test_view->GetWidget()->GetFocusManager()->GetFocusedView()); | 168 test_view->GetWidget()->GetFocusManager()->GetFocusedView()); |
| 167 | 169 |
| 168 // Set focus on another test_view, focus move to that pane. | 170 // Set focus on another test_view, focus move to that pane. |
| 169 EXPECT_TRUE(test_view_2->SetPaneFocus(test_view_2->second_child_button())); | 171 EXPECT_TRUE(test_view_2->SetPaneFocus(test_view_2->second_child_button())); |
| 170 EXPECT_FALSE(test_view->child_button() == | 172 EXPECT_FALSE(test_view->child_button() == |
| 171 test_view->GetWidget()->GetFocusManager()->GetFocusedView()); | 173 test_view->GetWidget()->GetFocusManager()->GetFocusedView()); |
| 172 EXPECT_EQ(test_view_2->second_child_button(), | 174 EXPECT_EQ(test_view_2->second_child_button(), |
| 173 test_view->GetWidget()->GetFocusManager()->GetFocusedView()); | 175 test_view->GetWidget()->GetFocusManager()->GetFocusedView()); |
| 174 widget->CloseNow(); | 176 widget->CloseNow(); |
| 175 widget.reset(); | 177 widget.reset(); |
| 176 } | 178 } |
| 177 | 179 |
| 178 TEST_F(AccessiblePaneViewTest, PaneFocusTraversal) { | 180 TEST_F(AccessiblePaneViewTest, PaneFocusTraversal) { |
| 179 TestBarView* test_view = new TestBarView(); | 181 TestBarView* test_view = new TestBarView(); |
| 180 TestBarView* original_test_view = new TestBarView(); | 182 TestBarView* original_test_view = new TestBarView(); |
| 181 scoped_ptr<Widget> widget(new Widget()); | 183 scoped_ptr<Widget> widget(new Widget()); |
| 182 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP); | 184 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP); |
| 183 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; | 185 params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; |
| 184 params.bounds = gfx::Rect(50, 50, 650, 650); | 186 params.bounds = gfx::Rect(50, 50, 650, 650); |
| 185 widget->Init(params); | 187 widget->Init(params); |
| 186 View* root = widget->GetRootView(); | 188 View* root = widget->GetRootView(); |
| 187 root->AddChildView(original_test_view); | 189 root->AddChildView(original_test_view); |
| 188 root->AddChildView(test_view); | 190 root->AddChildView(test_view); |
| 189 widget->Show(); | 191 widget->Show(); |
| 190 widget->Activate(); | 192 widget->Activate(); |
| 191 | 193 |
| 192 // Set pane focus on first view. | 194 // Set pane focus on first view. |
| 193 EXPECT_TRUE(original_test_view->SetPaneFocus( | 195 EXPECT_TRUE(original_test_view->SetPaneFocus( |
| (...skipping 21 matching lines...) Expand all Loading... |
| 215 test_view->GetWidget()->GetFocusManager()->GetFocusedView()); | 217 test_view->GetWidget()->GetFocusManager()->GetFocusedView()); |
| 216 | 218 |
| 217 // ESC | 219 // ESC |
| 218 test_view->AcceleratorPressed(test_view->escape_key()); | 220 test_view->AcceleratorPressed(test_view->escape_key()); |
| 219 EXPECT_EQ(original_test_view->third_child_button(), | 221 EXPECT_EQ(original_test_view->third_child_button(), |
| 220 test_view->GetWidget()->GetFocusManager()->GetFocusedView()); | 222 test_view->GetWidget()->GetFocusManager()->GetFocusedView()); |
| 221 widget->CloseNow(); | 223 widget->CloseNow(); |
| 222 widget.reset(); | 224 widget.reset(); |
| 223 } | 225 } |
| 224 } // namespace views | 226 } // namespace views |
| OLD | NEW |