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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
|
// Copyright (c) 2012 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 "content/common/resize_params.h"
#include "content/public/test/render_view_test.h"
#include "content/renderer/render_view_impl.h"
#include "content/renderer/render_widget.h"
#include "third_party/WebKit/public/web/WebFrameWidget.h"
#include "third_party/WebKit/public/web/WebInputMethodController.h"
#include "third_party/WebKit/public/web/WebRange.h"
#include "ui/base/ime/text_input_type.h"
namespace content {
class RenderWidgetTest : public RenderViewTest {
protected:
RenderWidget* widget() {
return static_cast<RenderViewImpl*>(view_)->GetWidget();
}
void OnResize(const ResizeParams& params) {
widget()->OnResize(params);
}
void GetCompositionRange(gfx::Range* range) {
widget()->GetCompositionRange(range);
}
bool next_paint_is_resize_ack() {
return widget()->next_paint_is_resize_ack();
}
blink::WebInputMethodController* GetInputMethodController() {
return widget()->GetInputMethodController();
}
void CommitText(std::string text) {
widget()->OnImeCommitText(
base::UTF8ToUTF16(text),
std::vector<blink::WebCompositionUnderline>(),
gfx::Range::InvalidRange(), 0);
}
ui::TextInputType GetTextInputType() { return widget()->GetTextInputType(); }
void SetFocus(bool focused) { widget()->OnSetFocus(focused); }
};
TEST_F(RenderWidgetTest, OnResize) {
// The initial bounds is empty, so setting it to the same thing should do
// nothing.
ResizeParams resize_params;
resize_params.screen_info = ScreenInfo();
resize_params.new_size = gfx::Size();
resize_params.physical_backing_size = gfx::Size();
resize_params.top_controls_height = 0.f;
resize_params.browser_controls_shrink_blink_size = false;
resize_params.is_fullscreen_granted = false;
resize_params.needs_resize_ack = false;
OnResize(resize_params);
EXPECT_EQ(resize_params.needs_resize_ack, next_paint_is_resize_ack());
// Setting empty physical backing size should not send the ack.
resize_params.new_size = gfx::Size(10, 10);
OnResize(resize_params);
EXPECT_EQ(resize_params.needs_resize_ack, next_paint_is_resize_ack());
// Setting the bounds to a "real" rect should send the ack.
render_thread_->sink().ClearMessages();
gfx::Size size(100, 100);
resize_params.new_size = size;
resize_params.physical_backing_size = size;
resize_params.needs_resize_ack = true;
OnResize(resize_params);
EXPECT_EQ(resize_params.needs_resize_ack, next_paint_is_resize_ack());
// Clear the flag.
widget()->DidReceiveCompositorFrameAck();
// Setting the same size again should not send the ack.
resize_params.needs_resize_ack = false;
OnResize(resize_params);
EXPECT_EQ(resize_params.needs_resize_ack, next_paint_is_resize_ack());
// Resetting the rect to empty should not send the ack.
resize_params.new_size = gfx::Size();
resize_params.physical_backing_size = gfx::Size();
OnResize(resize_params);
EXPECT_EQ(resize_params.needs_resize_ack, next_paint_is_resize_ack());
// Changing the screen info should not send the ack.
resize_params.screen_info.orientation_angle = 90;
OnResize(resize_params);
EXPECT_EQ(resize_params.needs_resize_ack, next_paint_is_resize_ack());
resize_params.screen_info.orientation_type =
SCREEN_ORIENTATION_VALUES_PORTRAIT_PRIMARY;
OnResize(resize_params);
EXPECT_EQ(resize_params.needs_resize_ack, next_paint_is_resize_ack());
}
class RenderWidgetInitialSizeTest : public RenderWidgetTest {
public:
RenderWidgetInitialSizeTest()
: RenderWidgetTest(), initial_size_(200, 100) {}
protected:
std::unique_ptr<ResizeParams> InitialSizeParams() override {
std::unique_ptr<ResizeParams> initial_size_params(new ResizeParams());
initial_size_params->new_size = initial_size_;
initial_size_params->physical_backing_size = initial_size_;
initial_size_params->needs_resize_ack = true;
return initial_size_params;
}
gfx::Size initial_size_;
};
TEST_F(RenderWidgetInitialSizeTest, InitialSize) {
EXPECT_EQ(initial_size_, widget()->size());
EXPECT_EQ(initial_size_, gfx::Size(widget()->GetWebWidget()->Size()));
EXPECT_TRUE(next_paint_is_resize_ack());
}
TEST_F(RenderWidgetTest, GetCompositionRangeValidComposition) {
LoadHTML(
"<div contenteditable>EDITABLE</div>"
"<script> document.querySelector('div').focus(); </script>");
blink::WebVector<blink::WebCompositionUnderline> emptyUnderlines;
DCHECK(widget()->GetInputMethodController());
widget()->GetInputMethodController()->SetComposition("hello", emptyUnderlines,
blink::WebRange(), 3, 3);
gfx::Range range;
GetCompositionRange(&range);
EXPECT_TRUE(range.IsValid());
EXPECT_EQ(0U, range.start());
EXPECT_EQ(5U, range.end());
}
TEST_F(RenderWidgetTest, GetCompositionRangeForSelection) {
LoadHTML(
"<div>NOT EDITABLE</div>"
"<script> document.execCommand('selectAll'); </script>");
gfx::Range range;
GetCompositionRange(&range);
// Selection range should not be treated as composition range.
EXPECT_FALSE(range.IsValid());
}
TEST_F(RenderWidgetTest, GetCompositionRangeInvalid) {
LoadHTML("<div>NOT EDITABLE</div>");
gfx::Range range;
GetCompositionRange(&range);
// If this test ever starts failing, one likely outcome is that WebRange
// and gfx::Range::InvalidRange are no longer expressed in the same
// values of start/end.
EXPECT_FALSE(range.IsValid());
}
// This test verifies that WebInputMethodController always exists as long as
// there is a focused frame inside the page, but, IME events are only executed
// if there is also page focus.
TEST_F(RenderWidgetTest, PageFocusIme) {
LoadHTML(
"<input/>"
" <script> document.querySelector('input').focus(); </script>");
// Give initial focus to the widget.
SetFocus(true);
// We must have an active WebInputMethodController.
EXPECT_TRUE(GetInputMethodController());
// Verify the text input type.
EXPECT_EQ(ui::TEXT_INPUT_TYPE_TEXT, GetTextInputType());
// Commit some text.
std::string text = "hello";
CommitText(text);
// The text should be committed since there is page focus in the beginning.
EXPECT_EQ(text, GetInputMethodController()->TextInputInfo().value.Utf8());
// Drop focus.
SetFocus(false);
// We must still have an active WebInputMethodController.
EXPECT_TRUE(GetInputMethodController());
// The text input type should not change.
EXPECT_EQ(ui::TEXT_INPUT_TYPE_TEXT, GetTextInputType());
// Commit the text again.
text = " world";
CommitText(text);
// This time is should not work since |m_imeAcceptEvents| is not set.
EXPECT_EQ("hello", GetInputMethodController()->TextInputInfo().value.Utf8());
// Now give focus back again and commit text.
SetFocus(true);
CommitText(text);
EXPECT_EQ("hello world",
GetInputMethodController()->TextInputInfo().value.Utf8());
}
} // namespace content
|