| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "base/memory/ptr_util.h" |
| 5 #include "base/strings/utf_string_conversions.h" | 6 #include "base/strings/utf_string_conversions.h" |
| 6 #include "ui/accessibility/ax_node_data.h" | 7 #include "ui/accessibility/ax_node_data.h" |
| 7 #include "ui/gfx/geometry/rect_conversions.h" | 8 #include "ui/gfx/geometry/rect_conversions.h" |
| 8 #include "ui/views/accessibility/native_view_accessibility.h" | 9 #include "ui/views/accessibility/native_view_accessibility.h" |
| 9 #include "ui/views/controls/button/button.h" | 10 #include "ui/views/controls/button/button.h" |
| 10 #include "ui/views/controls/label.h" | 11 #include "ui/views/controls/label.h" |
| 11 #include "ui/views/test/views_test_base.h" | 12 #include "ui/views/test/views_test_base.h" |
| 12 | 13 |
| 13 namespace views { | 14 namespace views { |
| 14 namespace test { | 15 namespace test { |
| (...skipping 12 matching lines...) Expand all Loading... |
| 27 class NativeViewAccessibilityTest : public ViewsTestBase { | 28 class NativeViewAccessibilityTest : public ViewsTestBase { |
| 28 public: | 29 public: |
| 29 NativeViewAccessibilityTest() {} | 30 NativeViewAccessibilityTest() {} |
| 30 ~NativeViewAccessibilityTest() override {} | 31 ~NativeViewAccessibilityTest() override {} |
| 31 | 32 |
| 32 void SetUp() override { | 33 void SetUp() override { |
| 33 ViewsTestBase::SetUp(); | 34 ViewsTestBase::SetUp(); |
| 34 | 35 |
| 35 widget_ = new views::Widget; | 36 widget_ = new views::Widget; |
| 36 views::Widget::InitParams params = | 37 views::Widget::InitParams params = |
| 37 CreateParams(views::Widget::InitParams::TYPE_WINDOW_FRAMELESS); | 38 CreateParams(views::Widget::InitParams::TYPE_WINDOW); |
| 38 params.bounds = gfx::Rect(0, 0, 200, 200); | 39 params.bounds = gfx::Rect(0, 0, 200, 200); |
| 39 widget_->Init(params); | 40 widget_->Init(params); |
| 40 | 41 |
| 41 button_ = new TestButton(); | 42 button_ = new TestButton(); |
| 42 button_->SetSize(gfx::Size(20, 20)); | 43 button_->SetSize(gfx::Size(20, 20)); |
| 43 button_accessibility_ = NativeViewAccessibility::Create(button_); | 44 button_accessibility_ = NativeViewAccessibility::Create(button_); |
| 44 | 45 |
| 45 label_ = new Label(); | 46 label_ = new Label(); |
| 46 button_->AddChildView(label_); | 47 button_->AddChildView(label_); |
| 47 label_accessibility_ = NativeViewAccessibility::Create(label_); | 48 label_accessibility_ = NativeViewAccessibility::Create(label_); |
| 48 | 49 |
| 49 widget_->SetContentsView(button_); | 50 widget_->GetContentsView()->AddChildView(button_); |
| 50 } | 51 } |
| 51 | 52 |
| 52 void TearDown() override { | 53 void TearDown() override { |
| 53 if (!widget_->IsClosed()) | 54 if (!widget_->IsClosed()) |
| 54 widget_->Close(); | 55 widget_->Close(); |
| 55 button_accessibility_->Destroy(); | 56 button_accessibility_->Destroy(); |
| 56 button_accessibility_ = NULL; | 57 button_accessibility_ = NULL; |
| 57 label_accessibility_->Destroy(); | 58 label_accessibility_->Destroy(); |
| 58 label_accessibility_ = NULL; | 59 label_accessibility_ = NULL; |
| 59 ViewsTestBase::TearDown(); | 60 ViewsTestBase::TearDown(); |
| (...skipping 20 matching lines...) Expand all Loading... |
| 80 } | 81 } |
| 81 | 82 |
| 82 TEST_F(NativeViewAccessibilityTest, LabelIsChildOfButton) { | 83 TEST_F(NativeViewAccessibilityTest, LabelIsChildOfButton) { |
| 83 EXPECT_EQ(1, button_accessibility_->GetChildCount()); | 84 EXPECT_EQ(1, button_accessibility_->GetChildCount()); |
| 84 EXPECT_EQ(label_->GetNativeViewAccessible(), | 85 EXPECT_EQ(label_->GetNativeViewAccessible(), |
| 85 button_accessibility_->ChildAtIndex(0)); | 86 button_accessibility_->ChildAtIndex(0)); |
| 86 EXPECT_EQ(button_->GetNativeViewAccessible(), | 87 EXPECT_EQ(button_->GetNativeViewAccessible(), |
| 87 label_accessibility_->GetParent()); | 88 label_accessibility_->GetParent()); |
| 88 } | 89 } |
| 89 | 90 |
| 91 TEST_F(NativeViewAccessibilityTest, WritableFocus) { |
| 92 widget_->Show(); |
| 93 // Make |button_| focusable, and focus/unfocus it via NativeViewAccessibility. |
| 94 button_->SetFocusBehavior(View::FocusBehavior::ALWAYS); |
| 95 EXPECT_EQ(nullptr, button_->GetFocusManager()->GetFocusedView()); |
| 96 EXPECT_EQ(nullptr, button_accessibility_->GetFocus()); |
| 97 EXPECT_TRUE(button_accessibility_->SetFocused(true)); |
| 98 EXPECT_EQ(button_, button_->GetFocusManager()->GetFocusedView()); |
| 99 EXPECT_EQ(button_->GetNativeViewAccessible(), |
| 100 button_accessibility_->GetFocus()); |
| 101 EXPECT_TRUE(button_accessibility_->SetFocused(false)); |
| 102 EXPECT_EQ(nullptr, button_->GetFocusManager()->GetFocusedView()); |
| 103 EXPECT_EQ(nullptr, button_accessibility_->GetFocus()); |
| 104 } |
| 105 |
| 90 // Subclass of NativeViewAccessibility that destroys itself when its | 106 // Subclass of NativeViewAccessibility that destroys itself when its |
| 91 // parent widget is destroyed, for the purposes of making sure this | 107 // parent widget is destroyed, for the purposes of making sure this |
| 92 // doesn't lead to a crash. | 108 // doesn't lead to a crash. |
| 93 class TestNativeViewAccessibility : public NativeViewAccessibility { | 109 class TestNativeViewAccessibility : public NativeViewAccessibility { |
| 94 public: | 110 public: |
| 95 explicit TestNativeViewAccessibility(View* view) | 111 explicit TestNativeViewAccessibility(View* view) |
| 96 : NativeViewAccessibility(view) {} | 112 : NativeViewAccessibility(view) {} |
| 97 | 113 |
| 98 void OnWidgetDestroying(Widget* widget) override { | 114 void OnWidgetDestroying(Widget* widget) override { |
| 99 bool is_destroying_parent_widget = (parent_widget_ == widget); | 115 bool is_destroying_parent_widget = (parent_widget_ == widget); |
| (...skipping 18 matching lines...) Expand all Loading... |
| 118 // WidgetObserver. Note that TestNativeViewAccessibility is a subclass | 134 // WidgetObserver. Note that TestNativeViewAccessibility is a subclass |
| 119 // defined above that destroys itself when its parent widget is destroyed. | 135 // defined above that destroys itself when its parent widget is destroyed. |
| 120 TestNativeViewAccessibility* child_accessible = | 136 TestNativeViewAccessibility* child_accessible = |
| 121 new TestNativeViewAccessibility(child_widget->GetRootView()); | 137 new TestNativeViewAccessibility(child_widget->GetRootView()); |
| 122 child_accessible->SetParentWidget(parent_widget.get()); | 138 child_accessible->SetParentWidget(parent_widget.get()); |
| 123 parent_widget.reset(); | 139 parent_widget.reset(); |
| 124 } | 140 } |
| 125 | 141 |
| 126 } // namespace test | 142 } // namespace test |
| 127 } // namespace views | 143 } // namespace views |
| OLD | NEW |