forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmagnifier_test_utils.cc
187 lines (148 loc) · 5.99 KB
/
magnifier_test_utils.cc
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
// Copyright 2018 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 "ash/magnifier/magnifier_test_utils.h"
#include "ash/shell.h"
#include "ui/base/ime/input_method.h"
#include "ui/views/controls/button/label_button.h"
#include "ui/views/controls/textfield/textfield.h"
#include "ui/views/layout/fill_layout.h"
#include "ui/views/widget/widget.h"
#include "ui/views/widget/widget_delegate.h"
#include "ui/wm/core/coordinate_conversion.h"
namespace ash {
namespace {
aura::Window* GetViewRootWindow(views::View* view) {
DCHECK(view);
return view->GetWidget()->GetNativeWindow()->GetRootWindow();
}
gfx::Rect GetBoundsInRoot(const gfx::Rect& bounds_in_screen,
views::View* view) {
gfx::Rect bounds = bounds_in_screen;
::wm::ConvertRectFromScreen(GetViewRootWindow(view), &bounds);
return bounds;
}
} // namespace
////////////////////////////////////////////////////////////////////////////////
// TestFocusView:
// A view that contains two buttons positioned at constant bounds with ability
// to request focus on either one.
class TestFocusView : public views::WidgetDelegateView {
public:
TestFocusView()
: button_1_(AddChildView(std::make_unique<views::LabelButton>())),
button_2_(AddChildView(std::make_unique<views::LabelButton>())) {}
~TestFocusView() override = default;
gfx::Size CalculatePreferredSize() const override {
return MagnifierFocusTestHelper::kTestFocusViewSize;
}
void Layout() override {
// Layout the first button at the top of the view.
button_1_->SetBounds(0, 0,
MagnifierFocusTestHelper::kTestFocusViewSize.width(),
MagnifierFocusTestHelper::kButtonHeight);
// And the second at the other end at the bottom of the view.
button_2_->SetBounds(0,
MagnifierFocusTestHelper::kTestFocusViewSize.height() -
MagnifierFocusTestHelper::kButtonHeight,
MagnifierFocusTestHelper::kTestFocusViewSize.width(),
MagnifierFocusTestHelper::kButtonHeight);
}
views::LabelButton* button_1_;
views::LabelButton* button_2_;
private:
DISALLOW_COPY_AND_ASSIGN(TestFocusView);
};
////////////////////////////////////////////////////////////////////////////////
// TestTextInputView:
// A view that contains a single text field for testing text input events.
class TestTextInputView : public views::WidgetDelegateView {
public:
TestTextInputView() : text_field_(new views::Textfield) {
text_field_->SetTextInputType(ui::TEXT_INPUT_TYPE_TEXT);
AddChildView(text_field_);
SetLayoutManager(std::make_unique<views::FillLayout>());
}
~TestTextInputView() override = default;
gfx::Size CalculatePreferredSize() const override {
return gfx::Size(50, 50);
}
void FocusOnTextInput() { text_field_->RequestFocus(); }
private:
views::Textfield* text_field_; // owned by views hierarchy.
DISALLOW_COPY_AND_ASSIGN(TestTextInputView);
};
////////////////////////////////////////////////////////////////////////////////
// MagnifierFocusTestHelper:
// static
constexpr int MagnifierFocusTestHelper::kButtonHeight;
// static
constexpr gfx::Size MagnifierFocusTestHelper::kTestFocusViewSize;
void MagnifierFocusTestHelper::CreateAndShowFocusTestView(
const gfx::Point& location) {
focus_test_view_ = new TestFocusView;
views::Widget* widget = views::Widget::CreateWindowWithContext(
focus_test_view_, Shell::GetPrimaryRootWindow(),
gfx::Rect(location, MagnifierFocusTestHelper::kTestFocusViewSize));
widget->Show();
}
void MagnifierFocusTestHelper::FocusFirstButton() {
DCHECK(focus_test_view_);
focus_test_view_->button_1_->RequestFocus();
}
void MagnifierFocusTestHelper::FocusSecondButton() {
DCHECK(focus_test_view_);
focus_test_view_->button_2_->RequestFocus();
}
gfx::Rect MagnifierFocusTestHelper::GetFirstButtonBoundsInRoot() const {
DCHECK(focus_test_view_);
return GetBoundsInRoot(focus_test_view_->button_1_->GetBoundsInScreen(),
focus_test_view_);
}
gfx::Rect MagnifierFocusTestHelper::GetSecondButtonBoundsInRoot() const {
DCHECK(focus_test_view_);
return GetBoundsInRoot(focus_test_view_->button_2_->GetBoundsInScreen(),
focus_test_view_);
}
////////////////////////////////////////////////////////////////////////////////
// MagnifierTextInputTestHelper:
void MagnifierTextInputTestHelper::CreateAndShowTextInputView(
const gfx::Rect& bounds) {
CreateAndShowTextInputViewInRoot(bounds, Shell::GetPrimaryRootWindow());
}
void MagnifierTextInputTestHelper::CreateAndShowTextInputViewInRoot(
const gfx::Rect& bounds,
aura::Window* root) {
text_input_view_ = new TestTextInputView;
views::Widget* widget =
views::Widget::CreateWindowWithContext(text_input_view_, root, bounds);
widget->Show();
}
gfx::Rect MagnifierTextInputTestHelper::GetTextInputViewBounds() {
DCHECK(text_input_view_);
gfx::Rect bounds = text_input_view_->bounds();
gfx::Point origin = bounds.origin();
// Convert origin to screen coordinates.
views::View::ConvertPointToScreen(text_input_view_, &origin);
// Convert origin to root window coordinates.
::wm::ConvertPointFromScreen(GetViewRootWindow(text_input_view_), &origin);
return gfx::Rect(origin.x(), origin.y(), bounds.width(), bounds.height());
}
gfx::Rect MagnifierTextInputTestHelper::GetCaretBounds() {
return GetBoundsInRoot(
GetInputMethod()->GetTextInputClient()->GetCaretBounds(),
text_input_view_);
}
void MagnifierTextInputTestHelper::FocusOnTextInputView() {
DCHECK(text_input_view_);
text_input_view_->FocusOnTextInput();
}
void MagnifierTextInputTestHelper::MaximizeWidget() {
DCHECK(text_input_view_);
text_input_view_->GetWidget()->Maximize();
}
ui::InputMethod* MagnifierTextInputTestHelper::GetInputMethod() {
DCHECK(text_input_view_);
return text_input_view_->GetWidget()->GetInputMethod();
}
} // namespace ash