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
3 // found in the LICENSE file.
5 #include "base/strings/utf_string_conversions.h"
6 #include "ui/accessibility/ax_view_state.h"
7 #include "ui/views/accessibility/native_view_accessibility.h"
8 #include "ui/views/controls/button/button.h"
9 #include "ui/views/controls/label.h"
10 #include "ui/views/test/views_test_base.h"
15 class NativeViewAccessibilityTest
;
19 class TestButton
: public Button
{
21 TestButton() : Button(NULL
) {}
26 class NativeViewAccessibilityTest
: public ViewsTestBase
{
28 NativeViewAccessibilityTest() {}
29 ~NativeViewAccessibilityTest() override
{}
31 void SetUp() override
{
32 ViewsTestBase::SetUp();
33 button_
.reset(new TestButton());
34 button_
->SetSize(gfx::Size(20, 20));
35 button_accessibility_
= NativeViewAccessibility::Create(button_
.get());
37 label_
.reset(new Label
);
38 button_
->AddChildView(label_
.get());
39 label_accessibility_
= NativeViewAccessibility::Create(label_
.get());
42 void TearDown() override
{
43 button_accessibility_
->Destroy();
44 button_accessibility_
= NULL
;
45 label_accessibility_
->Destroy();
46 label_accessibility_
= NULL
;
47 ViewsTestBase::TearDown();
51 scoped_ptr
<TestButton
> button_
;
52 NativeViewAccessibility
* button_accessibility_
;
53 scoped_ptr
<Label
> label_
;
54 NativeViewAccessibility
* label_accessibility_
;
57 TEST_F(NativeViewAccessibilityTest
, RoleShouldMatch
) {
58 EXPECT_EQ(ui::AX_ROLE_BUTTON
, button_accessibility_
->GetData().role
);
59 EXPECT_EQ(ui::AX_ROLE_STATIC_TEXT
, label_accessibility_
->GetData().role
);
62 TEST_F(NativeViewAccessibilityTest
, BoundsShouldMatch
) {
63 gfx::Rect bounds
= button_accessibility_
->GetData().location
;
64 bounds
.Offset(button_accessibility_
->GetGlobalCoordinateOffset());
65 EXPECT_EQ(button_
->GetBoundsInScreen(), bounds
);
68 TEST_F(NativeViewAccessibilityTest
, LabelIsChildOfButton
) {
69 EXPECT_EQ(1, button_accessibility_
->GetChildCount());
70 EXPECT_EQ(label_
->GetNativeViewAccessible(),
71 button_accessibility_
->ChildAtIndex(0));
72 EXPECT_EQ(button_
->GetNativeViewAccessible(),
73 label_accessibility_
->GetParent());
76 // Subclass of NativeViewAccessibility that destroys itself when its
77 // parent widget is destroyed, for the purposes of making sure this
78 // doesn't lead to a crash.
79 class TestNativeViewAccessibility
: public NativeViewAccessibility
{
81 explicit TestNativeViewAccessibility(View
* view
)
82 : NativeViewAccessibility(view
) {}
84 void OnWidgetDestroying(Widget
* widget
) override
{
85 bool is_destroying_parent_widget
= (parent_widget_
== widget
);
86 NativeViewAccessibility::OnWidgetDestroying(widget
);
87 if (is_destroying_parent_widget
)
92 TEST_F(NativeViewAccessibilityTest
, CrashOnWidgetDestroyed
) {
93 scoped_ptr
<Widget
> parent_widget(new Widget
);
94 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
95 params
.ownership
= views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
96 params
.bounds
= gfx::Rect(50, 50, 650, 650);
97 parent_widget
->Init(params
);
99 scoped_ptr
<Widget
> child_widget(new Widget
);
100 child_widget
->Init(params
);
102 // Make sure that deleting the parent widget can't cause a crash
103 // due to NativeViewAccessibility not unregistering itself as a
104 // WidgetObserver. Note that TestNativeViewAccessibility is a subclass
105 // defined above that destroys itself when its parent widget is destroyed.
106 TestNativeViewAccessibility
* child_accessible
=
107 new TestNativeViewAccessibility(child_widget
->GetRootView());
108 child_accessible
->SetParentWidget(parent_widget
.get());
109 parent_widget
.reset();