Chromium Code Reviews| Index: ppapi/tests/test_input_event.cc |
| =================================================================== |
| --- ppapi/tests/test_input_event.cc (revision 0) |
| +++ ppapi/tests/test_input_event.cc (revision 0) |
| @@ -0,0 +1,278 @@ |
| +// Copyright (c) 2011 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 "ppapi/tests/test_input_event.h" |
| + |
| +#include "ppapi/c/dev/ppb_testing_dev.h" |
| +#include "ppapi/c/pp_errors.h" |
| +#include "ppapi/c/ppb_input_event.h" |
| +#include "ppapi/cpp/input_event.h" |
| +#include "ppapi/cpp/module.h" |
| +#include "ppapi/tests/test_utils.h" |
| +#include "ppapi/tests/testing_instance.h" |
| + |
| +REGISTER_TEST_CASE(InputEvent); |
| + |
| +namespace { |
| +pp::Point GetCenter(const pp::Rect& rect) { |
| + return pp::Point( |
| + rect.x() + rect.width() / 2, |
| + rect.y() + rect.height() / 2); |
| +} |
| +} |
| + |
| +void TestInputEvent::RunTests(const std::string& filter) { |
| + RUN_TEST(Events, filter); |
| +} |
| + |
| +TestInputEvent::TestInputEvent(TestingInstance* instance) |
| + : TestCase(instance), |
| + input_event_interface_(NULL), |
| + mouse_input_event_interface_(NULL), |
| + wheel_input_event_interface_(NULL), |
| + keyboard_input_event_interface_(NULL), |
| + testing_interface_(NULL), |
| + view_rect_(), |
| + expected_input_event_(0), |
| + unexpected_input_event_(0), |
| + received_expected_event_(false), |
| + received_unexpected_event_(false), |
| + running_message_loop_(false) { |
| +} |
| + |
| +bool TestInputEvent::Init() { |
| + input_event_interface_ = static_cast<PPB_InputEvent const*>( |
| + pp::Module::Get()->GetBrowserInterface(PPB_INPUT_EVENT_INTERFACE)); |
| + mouse_input_event_interface_ = static_cast<PPB_MouseInputEvent const*>( |
| + pp::Module::Get()->GetBrowserInterface( |
| + PPB_MOUSE_INPUT_EVENT_INTERFACE)); |
| + wheel_input_event_interface_ = static_cast<PPB_WheelInputEvent const*>( |
| + pp::Module::Get()->GetBrowserInterface( |
| + PPB_WHEEL_INPUT_EVENT_INTERFACE)); |
| + keyboard_input_event_interface_ = static_cast<PPB_KeyboardInputEvent const*>( |
| + pp::Module::Get()->GetBrowserInterface( |
| + PPB_KEYBOARD_INPUT_EVENT_INTERFACE)); |
| + |
| + testing_interface_ = GetTestingInterface(); |
| + if (!testing_interface_) { |
| + instance_->AppendError("Testing interface not available"); |
| + } |
| + |
| + return |
| + !!input_event_interface_ && |
|
piman
2011/11/19 22:31:00
nit: no need for !!, pointers implicitly cast to b
bbudge
2011/11/20 02:53:57
Done.
|
| + !!mouse_input_event_interface_ && |
| + !!wheel_input_event_interface_ && |
| + !!keyboard_input_event_interface_ && |
| + !!testing_interface_; |
| +} |
| + |
| +pp::InputEvent TestInputEvent::CreateMouseEvent() { |
| + return CreateMouseEvent(PP_INPUTEVENT_TYPE_MOUSEDOWN, |
| + PP_INPUTEVENT_MOUSEBUTTON_LEFT); |
| +} |
| + |
| +pp::InputEvent TestInputEvent::CreateMouseEvent( |
| + PP_InputEvent_Type type, |
| + PP_InputEvent_MouseButton buttons) { |
| + return pp::MouseInputEvent( |
| + instance_, |
| + type, |
| + 100, // time_stamp |
| + 0, // modifiers |
| + buttons, |
| + GetCenter(view_rect_), |
| + 1, // click count |
| + pp::Point()); // movement |
| +} |
| + |
| +pp::InputEvent TestInputEvent::CreateWheelEvent() { |
| + return pp::WheelInputEvent( |
| + instance_, |
| + 100, // time_stamp |
| + 0, // modifiers |
| + pp::FloatPoint(1, 2), |
| + pp::FloatPoint(3, 4), |
| + PP_TRUE); // scroll_by_page |
| +} |
| + |
| +pp::InputEvent TestInputEvent::CreateKeyEvent() { |
| + return CreateKeyEvent(PP_INPUTEVENT_TYPE_KEYDOWN, |
| + 0x20); // space |
| +} |
| + |
| +pp::InputEvent TestInputEvent::CreateKeyEvent(PP_InputEvent_Type type, |
| + uint32_t key_code) { |
| + return pp::KeyboardInputEvent( |
| + instance_, |
| + type, |
| + 100, // time_stamp |
| + 0, // modifiers |
| + key_code, |
| + pp::Var()); |
| +} |
| + |
| +pp::InputEvent TestInputEvent::CreateCharEvent() { |
| + return CreateCharEvent(" "); // space |
| +} |
| + |
| +pp::InputEvent TestInputEvent::CreateCharEvent(const std::string& text) { |
| + return pp::KeyboardInputEvent( |
| + instance_, |
| + PP_INPUTEVENT_TYPE_CHAR, |
| + 100, // time_stamp |
| + 0, // modifiers |
| + 0, // keycode |
| + pp::Var(text)); |
| +} |
| + |
| +// Simulates the input event and make sure it is received by HandleInputEvent. |
| +bool TestInputEvent::TestRequestedInputEvent( |
| + const pp::InputEvent& input_event) { |
| + expected_input_event_ = pp::InputEvent(input_event.pp_resource()); |
| + received_expected_event_ = false; |
| + testing_interface_->SimulateInputEvent(instance_->pp_instance(), |
| + input_event.pp_resource()); |
| + while (!received_expected_event_) { |
|
piman
2011/11/19 22:31:00
So, the problem with this, is that you only have 2
bbudge
2011/11/20 02:53:57
Redid this using PostMessage. An excellent suggest
|
| + running_message_loop_ = true; |
| + testing_interface_->RunMessageLoop(instance_->pp_instance()); |
| + } |
| + return received_expected_event_; |
| +} |
| + |
| +// Simulates the unrequested and requested input events and makes sure only |
| +// the requested one is received by HandleInputEvent. We can't (reliably test |
|
piman
2011/11/19 22:31:00
typo: remove (.
bbudge
2011/11/20 02:53:57
Done.
|
| +// a case where no event is received in out-of-process plugins. |
| +bool TestInputEvent::TestUnrequestedInputEvent( |
| + const pp::InputEvent& unrequested_input_event, |
| + const pp::InputEvent& requested_input_event) { |
| + unexpected_input_event_ = pp::InputEvent( |
| + unrequested_input_event.pp_resource()); |
| + received_unexpected_event_ = false; |
| + testing_interface_->SimulateInputEvent( |
| + instance_->pp_instance(), unrequested_input_event.pp_resource()); |
| + return |
| + TestRequestedInputEvent(requested_input_event) && |
| + !received_unexpected_event_; |
| +} |
| + |
| +bool TestInputEvent::AreEquivalentEvents(PP_Resource received, |
| + PP_Resource expected) { |
| + if (!input_event_interface_->IsInputEvent(received) || |
| + !input_event_interface_->IsInputEvent(expected)) { |
| + return false; |
| + } |
| + |
| + // Test common fields, except modifiers and time stamp, which may be changed |
| + // by the renderer. |
| + int32_t received_type = input_event_interface_->GetType(received); |
| + int32_t expected_type = input_event_interface_->GetType(expected); |
| + if (received_type != expected_type) { |
| + // Allow key down events to match "raw" key down events. |
| + if (expected_type != PP_INPUTEVENT_TYPE_KEYDOWN && |
| + received_type != PP_INPUTEVENT_TYPE_RAWKEYDOWN) { |
| + return false; |
| + } |
| + } |
| + |
| + // Test event type-specific fields. |
| + switch (input_event_interface_->GetType(received)) { |
| + case PP_INPUTEVENT_TYPE_MOUSEDOWN: |
| + case PP_INPUTEVENT_TYPE_MOUSEUP: |
| + case PP_INPUTEVENT_TYPE_MOUSEMOVE: |
| + case PP_INPUTEVENT_TYPE_MOUSEENTER: |
| + case PP_INPUTEVENT_TYPE_MOUSELEAVE: |
| + // Check mouse fields, except position and movement, which may be |
| + // modified by the renderer. |
| + return |
| + mouse_input_event_interface_->GetButton(received) == |
| + mouse_input_event_interface_->GetButton(expected) && |
| + mouse_input_event_interface_->GetClickCount(received) == |
| + mouse_input_event_interface_->GetClickCount(expected); |
| + |
| + case PP_INPUTEVENT_TYPE_WHEEL: |
| + return |
| + pp::FloatPoint(wheel_input_event_interface_->GetDelta(received)) == |
| + pp::FloatPoint(wheel_input_event_interface_->GetDelta(expected)) && |
| + pp::FloatPoint(wheel_input_event_interface_->GetTicks(received)) == |
| + pp::FloatPoint(wheel_input_event_interface_->GetTicks(expected)) && |
| + wheel_input_event_interface_->GetScrollByPage(received) == |
| + wheel_input_event_interface_->GetScrollByPage(expected); |
| + |
| + case PP_INPUTEVENT_TYPE_RAWKEYDOWN: |
| + case PP_INPUTEVENT_TYPE_KEYDOWN: |
| + case PP_INPUTEVENT_TYPE_KEYUP: |
| + return |
| + keyboard_input_event_interface_->GetKeyCode(received) == |
| + keyboard_input_event_interface_->GetKeyCode(expected); |
| + |
| + case PP_INPUTEVENT_TYPE_CHAR: |
| + return |
| + keyboard_input_event_interface_->GetKeyCode(received) == |
| + keyboard_input_event_interface_->GetKeyCode(expected) && |
| + pp::Var(pp::Var::PassRef(), |
| + keyboard_input_event_interface_->GetCharacterText(received)) == |
| + pp::Var(pp::Var::PassRef(), |
| + keyboard_input_event_interface_->GetCharacterText(expected)); |
| + |
| + default: |
| + break; |
| + } |
| + |
| + return false; |
| +} |
| + |
| +bool TestInputEvent::HandleInputEvent(const pp::InputEvent& input_event) { |
| + // Some events may cause extra events to be generated, so record the first |
| + // event that matches. |
| + if (!received_expected_event_) { |
| + received_expected_event_ = AreEquivalentEvents( |
| + input_event.pp_resource(), |
| + expected_input_event_.pp_resource()); |
| + if (received_expected_event_ && running_message_loop_) { |
| + testing_interface_->QuitMessageLoop(instance_->pp_instance()); |
| + running_message_loop_ = false; |
| + } |
| + } |
| + if (!received_unexpected_event_) { |
| + received_unexpected_event_ = AreEquivalentEvents( |
| + input_event.pp_resource(), |
| + unexpected_input_event_.pp_resource()); |
| + } |
| + // Handle all input events. |
| + return true; |
| +} |
| + |
| +void TestInputEvent::DidChangeView(const pp::Rect& position, |
| + const pp::Rect& clip) { |
| + view_rect_ = position; |
| +} |
| + |
| +std::string TestInputEvent::TestEvents() { |
| + // Request all input event classes. |
| + input_event_interface_->RequestInputEvents(instance_->pp_instance(), |
| + PP_INPUTEVENT_CLASS_MOUSE | |
| + PP_INPUTEVENT_CLASS_WHEEL | |
| + PP_INPUTEVENT_CLASS_KEYBOARD); |
| + // Send the events and check that we received them. |
| + ASSERT_TRUE(TestRequestedInputEvent(CreateMouseEvent())); |
| + ASSERT_TRUE(TestRequestedInputEvent(CreateWheelEvent())); |
| + ASSERT_TRUE(TestRequestedInputEvent(CreateKeyEvent())); |
| + ASSERT_TRUE(TestRequestedInputEvent(CreateCharEvent())); |
| + |
| + // Request only mouse events. |
| + input_event_interface_->ClearInputEventRequest(instance_->pp_instance(), |
| + PP_INPUTEVENT_CLASS_WHEEL | |
| + PP_INPUTEVENT_CLASS_KEYBOARD); |
| + // Check that we only receive mouse events. |
| + ASSERT_TRUE(TestRequestedInputEvent(CreateMouseEvent())); |
| + ASSERT_TRUE(TestUnrequestedInputEvent(CreateWheelEvent(), |
| + CreateMouseEvent())); |
| + ASSERT_TRUE(TestUnrequestedInputEvent(CreateKeyEvent(), |
| + CreateMouseEvent())); |
| + ASSERT_TRUE(TestUnrequestedInputEvent(CreateCharEvent(), |
| + CreateMouseEvent())); |
| + |
| + PASS(); |
| +} |
| + |