summaryrefslogtreecommitdiffstats
path: root/ui/views/accessibility/native_view_accessibility_unittest.cc
blob: 705f3efc77f963f796c9e9b103ba1b3a864e6ee2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/strings/utf_string_conversions.h"
#include "ui/accessibility/ax_view_state.h"
#include "ui/views/accessibility/native_view_accessibility.h"
#include "ui/views/controls/button/button.h"
#include "ui/views/controls/label.h"
#include "ui/views/test/views_test_base.h"

namespace views {
namespace test {

class NativeViewAccessibilityTest;

namespace {

class TestButton : public Button {
 public:
  TestButton() : Button(NULL) {}
};

}  // namespace

class NativeViewAccessibilityTest : public ViewsTestBase {
 public:
  NativeViewAccessibilityTest() {}
  ~NativeViewAccessibilityTest() override {}

  void SetUp() override {
    ViewsTestBase::SetUp();
    button_.reset(new TestButton());
    button_->SetSize(gfx::Size(20, 20));
    button_accessibility_ = NativeViewAccessibility::Create(button_.get());

    label_.reset(new Label);
    button_->AddChildView(label_.get());
    label_accessibility_ = NativeViewAccessibility::Create(label_.get());
  }

  void TearDown() override {
    button_accessibility_->Destroy();
    button_accessibility_ = NULL;
    label_accessibility_->Destroy();
    label_accessibility_ = NULL;
    ViewsTestBase::TearDown();
  }

 protected:
  scoped_ptr<TestButton> button_;
  NativeViewAccessibility* button_accessibility_;
  scoped_ptr<Label> label_;
  NativeViewAccessibility* label_accessibility_;
};

TEST_F(NativeViewAccessibilityTest, RoleShouldMatch) {
  EXPECT_EQ(ui::AX_ROLE_BUTTON, button_accessibility_->GetData().role);
  EXPECT_EQ(ui::AX_ROLE_STATIC_TEXT, label_accessibility_->GetData().role);
}

TEST_F(NativeViewAccessibilityTest, BoundsShouldMatch) {
  gfx::Rect bounds = button_accessibility_->GetData().location;
  bounds.Offset(button_accessibility_->GetGlobalCoordinateOffset());
  EXPECT_EQ(button_->GetBoundsInScreen(), bounds);
}

TEST_F(NativeViewAccessibilityTest, LabelIsChildOfButton) {
  EXPECT_EQ(1, button_accessibility_->GetChildCount());
  EXPECT_EQ(label_->GetNativeViewAccessible(),
            button_accessibility_->ChildAtIndex(0));
  EXPECT_EQ(button_->GetNativeViewAccessible(),
            label_accessibility_->GetParent());
}

// Subclass of NativeViewAccessibility that destroys itself when its
// parent widget is destroyed, for the purposes of making sure this
// doesn't lead to a crash.
class TestNativeViewAccessibility : public NativeViewAccessibility {
 public:
  explicit TestNativeViewAccessibility(View* view)
      : NativeViewAccessibility(view) {}

  void OnWidgetDestroying(Widget* widget) override {
    bool is_destroying_parent_widget = (parent_widget_ == widget);
    NativeViewAccessibility::OnWidgetDestroying(widget);
    if (is_destroying_parent_widget)
      delete this;
  }
};

TEST_F(NativeViewAccessibilityTest, CrashOnWidgetDestroyed) {
  scoped_ptr<Widget> parent_widget(new Widget);
  Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
  params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
  params.bounds = gfx::Rect(50, 50, 650, 650);
  parent_widget->Init(params);

  scoped_ptr<Widget> child_widget(new Widget);
  child_widget->Init(params);

  // Make sure that deleting the parent widget can't cause a crash
  // due to NativeViewAccessibility not unregistering itself as a
  // WidgetObserver. Note that TestNativeViewAccessibility is a subclass
  // defined above that destroys itself when its parent widget is destroyed.
  TestNativeViewAccessibility* child_accessible =
      new TestNativeViewAccessibility(child_widget->GetRootView());
  child_accessible->SetParentWidget(parent_widget.get());
  parent_widget.reset();
}

}  // namespace test
}  // namespace views