cc: Added inline to Tile::IsReadyToDraw
[chromium-blink-merge.git] / ppapi / tests / test_ime_input_event.cc
blobc28a0446831e79d1864435c5e0c15d60072da419
1 // Copyright (c) 2012 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 "ppapi/tests/test_ime_input_event.h"
7 #include "ppapi/c/dev/ppb_testing_dev.h"
8 #include "ppapi/c/pp_errors.h"
9 #include "ppapi/c/ppb_input_event.h"
10 #include "ppapi/cpp/input_event.h"
11 #include "ppapi/cpp/module.h"
12 #include "ppapi/tests/test_utils.h"
13 #include "ppapi/tests/testing_instance.h"
15 REGISTER_TEST_CASE(ImeInputEvent);
17 namespace {
19 // Japanese Kanji letters
20 const char* kCompositionChar[] = {
21 "\xE6\x96\x87", // An example character of normal unicode.
22 "\xF0\xA0\xAE\x9F", // An example character of surrogate pair.
23 "\xF0\x9F\x98\x81" // An example character of surrogate pair(emoji).
26 const char kCompositionText[] = "\xE6\x96\x87\xF0\xA0\xAE\x9F\xF0\x9F\x98\x81";
28 #define FINISHED_WAITING_MESSAGE "TEST_IME_INPUT_EVENT_FINISHED_WAITING"
30 } // namespace
32 TestImeInputEvent::TestImeInputEvent(TestingInstance* instance)
33 : TestCase(instance),
34 input_event_interface_(NULL),
35 keyboard_input_event_interface_(NULL),
36 ime_input_event_interface_(NULL),
37 received_unexpected_event_(true),
38 received_finish_message_(false) {
41 TestImeInputEvent::~TestImeInputEvent() {
42 // Remove the special listener that only responds to a
43 // FINISHED_WAITING_MESSAGE string. See Init for where it gets added.
44 std::string js_code;
45 js_code = "var plugin = document.getElementById('plugin');"
46 "plugin.removeEventListener('message',"
47 " plugin.wait_for_messages_handler);"
48 "delete plugin.wait_for_messages_handler;";
49 instance_->EvalScript(js_code);
52 void TestImeInputEvent::RunTests(const std::string& filter) {
53 RUN_TEST(ImeCommit, filter);
54 RUN_TEST(ImeCancel, filter);
55 RUN_TEST(ImeUnawareCommit, filter);
56 RUN_TEST(ImeUnawareCancel, filter);
59 bool TestImeInputEvent::Init() {
60 input_event_interface_ = static_cast<const PPB_InputEvent*>(
61 pp::Module::Get()->GetBrowserInterface(PPB_INPUT_EVENT_INTERFACE));
62 keyboard_input_event_interface_ =
63 static_cast<const PPB_KeyboardInputEvent*>(
64 pp::Module::Get()->GetBrowserInterface(
65 PPB_KEYBOARD_INPUT_EVENT_INTERFACE));
66 ime_input_event_interface_ = static_cast<const PPB_IMEInputEvent*>(
67 pp::Module::Get()->GetBrowserInterface(
68 PPB_IME_INPUT_EVENT_INTERFACE));
70 bool success =
71 input_event_interface_ &&
72 keyboard_input_event_interface_ &&
73 ime_input_event_interface_ &&
74 CheckTestingInterface();
76 // Set up a listener for our message that signals that all input events have
77 // been received.
78 // Note the following code is dependent on some features of test_case.html.
79 // E.g., it is assumed that the DOM element where the plugin is embedded has
80 // an id of 'plugin', and there is a function 'IsTestingMessage' that allows
81 // us to ignore the messages that are intended for use by the testing
82 // framework itself.
83 std::string js_code =
84 "var plugin = document.getElementById('plugin');"
85 "var wait_for_messages_handler = function(message_event) {"
86 " if (!IsTestingMessage(message_event.data) &&"
87 " message_event.data === '" FINISHED_WAITING_MESSAGE "') {"
88 " plugin.postMessage('" FINISHED_WAITING_MESSAGE "');"
89 " }"
90 "};"
91 "plugin.addEventListener('message', wait_for_messages_handler);"
92 // Stash it on the plugin so we can remove it in the destructor.
93 "plugin.wait_for_messages_handler = wait_for_messages_handler;";
94 instance_->EvalScript(js_code);
96 return success;
99 bool TestImeInputEvent::HandleInputEvent(const pp::InputEvent& input_event) {
100 // Check whether the IME related events comes in the expected order.
101 switch (input_event.GetType()) {
102 case PP_INPUTEVENT_TYPE_IME_COMPOSITION_START:
103 case PP_INPUTEVENT_TYPE_IME_COMPOSITION_UPDATE:
104 case PP_INPUTEVENT_TYPE_IME_COMPOSITION_END:
105 case PP_INPUTEVENT_TYPE_IME_TEXT:
106 case PP_INPUTEVENT_TYPE_CHAR:
107 if (expected_events_.empty()) {
108 received_unexpected_event_ = true;
109 } else {
110 received_unexpected_event_ =
111 !AreEquivalentEvents(input_event.pp_resource(),
112 expected_events_.front().pp_resource());
113 expected_events_.erase(expected_events_.begin());
115 break;
117 default:
118 // Don't care for any other input event types for this test.
119 break;
122 // Handle all input events.
123 return true;
126 void TestImeInputEvent::HandleMessage(const pp::Var& message_data) {
127 if (message_data.is_string() &&
128 (message_data.AsString() == FINISHED_WAITING_MESSAGE)) {
129 testing_interface_->QuitMessageLoop(instance_->pp_instance());
130 received_finish_message_ = true;
134 void TestImeInputEvent::DidChangeView(const pp::View& view) {
135 view_rect_ = view.GetRect();
138 pp::InputEvent TestImeInputEvent::CreateImeCompositionStartEvent() {
139 return pp::IMEInputEvent(
140 instance_,
141 PP_INPUTEVENT_TYPE_IME_COMPOSITION_START,
142 100, // time_stamp
143 pp::Var(""),
144 std::vector<uint32_t>(),
145 -1, // target_segment
146 std::make_pair(0U, 0U) // selection
150 pp::InputEvent TestImeInputEvent::CreateImeCompositionUpdateEvent(
151 const std::string& text,
152 const std::vector<uint32_t>& segments,
153 int32_t target_segment,
154 const std::pair<uint32_t, uint32_t>& selection) {
155 return pp::IMEInputEvent(
156 instance_,
157 PP_INPUTEVENT_TYPE_IME_COMPOSITION_UPDATE,
158 100, // time_stamp
159 text,
160 segments,
161 target_segment,
162 selection
166 pp::InputEvent TestImeInputEvent::CreateImeCompositionEndEvent(
167 const std::string& text) {
168 return pp::IMEInputEvent(
169 instance_,
170 PP_INPUTEVENT_TYPE_IME_COMPOSITION_END,
171 100, // time_stamp
172 pp::Var(text),
173 std::vector<uint32_t>(),
174 -1, // target_segment
175 std::make_pair(0U, 0U) // selection
179 pp::InputEvent TestImeInputEvent::CreateImeTextEvent(const std::string& text) {
180 return pp::IMEInputEvent(
181 instance_,
182 PP_INPUTEVENT_TYPE_IME_TEXT,
183 100, // time_stamp
184 pp::Var(text),
185 std::vector<uint32_t>(),
186 -1, // target_segment
187 std::make_pair(0U, 0U) // selection
191 pp::InputEvent TestImeInputEvent::CreateCharEvent(const std::string& text) {
192 return pp::KeyboardInputEvent(
193 instance_,
194 PP_INPUTEVENT_TYPE_CHAR,
195 100, // time_stamp
196 0, // modifiers
197 0, // keycode
198 pp::Var(text));
201 void TestImeInputEvent::GetFocusBySimulatingMouseClick() {
202 // For receiving IME events, the plugin DOM node needs to be focused.
203 // The following code is for achieving that by simulating a mouse click event.
204 input_event_interface_->RequestInputEvents(instance_->pp_instance(),
205 PP_INPUTEVENT_CLASS_MOUSE);
206 SimulateInputEvent(pp::MouseInputEvent(
207 instance_,
208 PP_INPUTEVENT_TYPE_MOUSEDOWN,
209 100, // time_stamp
210 0, // modifiers
211 PP_INPUTEVENT_MOUSEBUTTON_LEFT,
212 pp::Point(
213 view_rect_.x() + view_rect_.width() / 2,
214 view_rect_.y() + view_rect_.height() / 2),
215 1, // click count
216 pp::Point())); // movement
219 // Simulates the input event and calls PostMessage to let us know when
220 // we have received all resulting events from the browser.
221 bool TestImeInputEvent::SimulateInputEvent(const pp::InputEvent& input_event) {
222 received_unexpected_event_ = false;
223 received_finish_message_ = false;
224 testing_interface_->SimulateInputEvent(instance_->pp_instance(),
225 input_event.pp_resource());
226 instance_->PostMessage(pp::Var(FINISHED_WAITING_MESSAGE));
227 testing_interface_->RunMessageLoop(instance_->pp_instance());
228 return received_finish_message_ && !received_unexpected_event_;
231 bool TestImeInputEvent::AreEquivalentEvents(PP_Resource received,
232 PP_Resource expected) {
233 if (!input_event_interface_->IsInputEvent(received) ||
234 !input_event_interface_->IsInputEvent(expected)) {
235 return false;
238 // Test common fields, except modifiers and time stamp, which may be changed
239 // by the browser.
240 int32_t received_type = input_event_interface_->GetType(received);
241 int32_t expected_type = input_event_interface_->GetType(expected);
242 if (received_type != expected_type)
243 return false;
245 // Test event type-specific fields.
246 switch (received_type) {
247 case PP_INPUTEVENT_TYPE_IME_COMPOSITION_START:
248 // COMPOSITION_START does not convey further information.
249 break;
251 case PP_INPUTEVENT_TYPE_IME_COMPOSITION_END:
252 case PP_INPUTEVENT_TYPE_IME_TEXT:
253 // For COMPOSITION_END and TEXT, GetText() has meaning.
254 return pp::Var(pp::PASS_REF,
255 ime_input_event_interface_->GetText(received)) ==
256 pp::Var(pp::PASS_REF,
257 ime_input_event_interface_->GetText(expected));
259 case PP_INPUTEVENT_TYPE_IME_COMPOSITION_UPDATE:
260 // For COMPOSITION_UPDATE, all fields must be checked.
262 uint32_t received_segment_number =
263 ime_input_event_interface_->GetSegmentNumber(received);
264 uint32_t expected_segment_number =
265 ime_input_event_interface_->GetSegmentNumber(expected);
266 if (received_segment_number != expected_segment_number)
267 return false;
269 // The "<=" is not a bug. i-th segment is represented as the pair of
270 // i-th and (i+1)-th offsets in Pepper IME API.
271 for (uint32_t i = 0; i <= received_segment_number; ++i) {
272 if (ime_input_event_interface_->GetSegmentOffset(received, i) !=
273 ime_input_event_interface_->GetSegmentOffset(expected, i))
274 return false;
277 uint32_t received_selection_start = 0;
278 uint32_t received_selection_end = 0;
279 uint32_t expected_selection_start = 0;
280 uint32_t expected_selection_end = 0;
281 ime_input_event_interface_->GetSelection(
282 received, &received_selection_start, &received_selection_end);
283 ime_input_event_interface_->GetSelection(
284 expected, &expected_selection_start, &expected_selection_end);
285 if (received_selection_start != expected_selection_start ||
286 received_selection_end != expected_selection_end) {
287 return true;
290 return pp::Var(pp::PASS_REF,
291 ime_input_event_interface_->GetText(received)) ==
292 pp::Var(pp::PASS_REF,
293 ime_input_event_interface_->GetText(expected)) &&
294 ime_input_event_interface_->GetTargetSegment(received) ==
295 ime_input_event_interface_->GetTargetSegment(expected);
298 case PP_INPUTEVENT_TYPE_CHAR:
299 return
300 keyboard_input_event_interface_->GetKeyCode(received) ==
301 keyboard_input_event_interface_->GetKeyCode(expected) &&
302 pp::Var(pp::PASS_REF,
303 keyboard_input_event_interface_->GetCharacterText(received)) ==
304 pp::Var(pp::PASS_REF,
305 keyboard_input_event_interface_->GetCharacterText(expected));
307 default:
308 break;
310 return true;
313 std::string TestImeInputEvent::TestImeCommit() {
314 GetFocusBySimulatingMouseClick();
316 input_event_interface_->RequestInputEvents(instance_->pp_instance(),
317 PP_INPUTEVENT_CLASS_KEYBOARD |
318 PP_INPUTEVENT_CLASS_IME);
320 std::vector<uint32_t> segments;
321 segments.push_back(0U);
322 segments.push_back(3U);
323 segments.push_back(7U);
324 segments.push_back(11U);
325 pp::InputEvent update_event = CreateImeCompositionUpdateEvent(
326 kCompositionText, segments, 1, std::make_pair(3U, 7U));
328 expected_events_.clear();
329 expected_events_.push_back(CreateImeCompositionStartEvent());
330 expected_events_.push_back(update_event);
331 expected_events_.push_back(CreateImeCompositionEndEvent(kCompositionText));
332 expected_events_.push_back(CreateImeTextEvent(kCompositionText));
334 // Simulate the case when IME successfully committed some text.
335 ASSERT_TRUE(SimulateInputEvent(update_event));
336 ASSERT_TRUE(SimulateInputEvent(CreateImeTextEvent(kCompositionText)));
338 ASSERT_TRUE(expected_events_.empty());
339 PASS();
342 std::string TestImeInputEvent::TestImeCancel() {
343 GetFocusBySimulatingMouseClick();
345 input_event_interface_->RequestInputEvents(instance_->pp_instance(),
346 PP_INPUTEVENT_CLASS_KEYBOARD |
347 PP_INPUTEVENT_CLASS_IME);
349 std::vector<uint32_t> segments;
350 segments.push_back(0U);
351 segments.push_back(3U);
352 segments.push_back(7U);
353 segments.push_back(11U);
354 pp::InputEvent update_event = CreateImeCompositionUpdateEvent(
355 kCompositionText, segments, 1, std::make_pair(3U, 7U));
357 expected_events_.clear();
358 expected_events_.push_back(CreateImeCompositionStartEvent());
359 expected_events_.push_back(update_event);
360 expected_events_.push_back(CreateImeCompositionEndEvent(std::string()));
362 // Simulate the case when IME canceled composition.
363 ASSERT_TRUE(SimulateInputEvent(update_event));
364 ASSERT_TRUE(SimulateInputEvent(CreateImeCompositionEndEvent(std::string())));
366 ASSERT_TRUE(expected_events_.empty());
367 PASS();
370 std::string TestImeInputEvent::TestImeUnawareCommit() {
371 GetFocusBySimulatingMouseClick();
373 input_event_interface_->ClearInputEventRequest(instance_->pp_instance(),
374 PP_INPUTEVENT_CLASS_IME);
375 input_event_interface_->RequestInputEvents(instance_->pp_instance(),
376 PP_INPUTEVENT_CLASS_KEYBOARD);
378 std::vector<uint32_t> segments;
379 segments.push_back(0U);
380 segments.push_back(3U);
381 segments.push_back(7U);
382 segments.push_back(11U);
383 pp::InputEvent update_event = CreateImeCompositionUpdateEvent(
384 kCompositionText, segments, 1, std::make_pair(3U, 7U));
386 expected_events_.clear();
387 expected_events_.push_back(CreateCharEvent(kCompositionChar[0]));
388 expected_events_.push_back(CreateCharEvent(kCompositionChar[1]));
389 expected_events_.push_back(CreateCharEvent(kCompositionChar[2]));
391 // Test for IME-unaware plugins. Commit event is translated to char events.
392 ASSERT_TRUE(SimulateInputEvent(update_event));
393 ASSERT_TRUE(SimulateInputEvent(CreateImeTextEvent(kCompositionText)));
395 ASSERT_TRUE(expected_events_.empty());
396 PASS();
400 std::string TestImeInputEvent::TestImeUnawareCancel() {
401 GetFocusBySimulatingMouseClick();
403 input_event_interface_->ClearInputEventRequest(instance_->pp_instance(),
404 PP_INPUTEVENT_CLASS_IME);
405 input_event_interface_->RequestInputEvents(instance_->pp_instance(),
406 PP_INPUTEVENT_CLASS_KEYBOARD);
408 std::vector<uint32_t> segments;
409 segments.push_back(0U);
410 segments.push_back(3U);
411 segments.push_back(7U);
412 segments.push_back(11U);
413 pp::InputEvent update_event = CreateImeCompositionUpdateEvent(
414 kCompositionText, segments, 1, std::make_pair(3U, 7U));
416 expected_events_.clear();
418 // Test for IME-unaware plugins. Cancel won't issue any events.
419 ASSERT_TRUE(SimulateInputEvent(update_event));
420 ASSERT_TRUE(SimulateInputEvent(CreateImeCompositionEndEvent(std::string())));
422 ASSERT_TRUE(expected_events_.empty());
423 PASS();