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,234 @@ |
+// Copyright (c) 2010 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" |
+ |
+namespace { |
+pp::Point GetCenter(const pp::Rect& rect) { |
+ return pp::Point( |
+ rect.x() + rect.width() / 2, |
+ rect.y() + rect.height() / 2); |
+} |
+} |
+ |
+REGISTER_TEST_CASE(InputEvent); |
+ |
+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), |
+ expected_input_event_(0), |
+ received_expected_event_(false), |
+ view_rect_() { |
+} |
+ |
+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(); |
+ |
+ return |
+ !!input_event_interface_ && |
+ !!mouse_input_event_interface_ && |
+ !!wheel_input_event_interface_ && |
+ !!keyboard_input_event_interface_ && |
+ !!testing_interface_; |
+} |
+ |
+pp::InputEvent TestInputEvent::CreateMouseEvent() { |
+ return pp::MouseInputEvent( |
+ instance_, |
+ PP_INPUTEVENT_TYPE_MOUSEDOWN, |
+ 100, // time_stamp |
+ PP_INPUTEVENT_MODIFIER_SHIFTKEY, |
+ PP_INPUTEVENT_MOUSEBUTTON_RIGHT, |
+ GetCenter(view_rect_), |
+ 1, // click count |
+ pp::Point()); // movement |
+} |
+ |
+pp::InputEvent TestInputEvent::CreateWheelEvent() { |
+ return pp::WheelInputEvent( |
+ instance_, |
+ 100, // time_stamp |
+ PP_INPUTEVENT_MODIFIER_SHIFTKEY, |
+ pp::FloatPoint(1, 2), |
+ pp::FloatPoint(3, 4), |
+ PP_TRUE); // scroll_by_page |
+} |
+ |
+pp::InputEvent TestInputEvent::CreateKeyEvent() { |
+ return pp::KeyboardInputEvent( |
+ instance_, |
+ PP_INPUTEVENT_TYPE_KEYDOWN, |
+ 100, // time_stamp |
+ 0, // modifiers |
+ 0x20, // Windows virtual keycode for a space |
+ pp::Var("")); |
+} |
+ |
+pp::InputEvent TestInputEvent::CreateCharEvent() { |
+ return pp::KeyboardInputEvent( |
+ instance_, |
+ PP_INPUTEVENT_TYPE_CHAR, |
+ 100, // time_stamp |
+ 0, // modifiers |
+ 0, // keycode, undefined in this case |
+ pp::Var(" ")); |
+} |
+ |
+bool TestInputEvent::SimulateInputEvent(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()); |
+ // HandleInputEvent() should have been called. |
dmichael (off chromium)
2011/11/07 17:12:58
This only works if it's dispatched synchronously,
|
+ return received_expected_event_; |
+} |
+ |
+bool TestInputEvent::AreEquivalentEvents(PP_Resource first, |
+ PP_Resource second) { |
+ if (!input_event_interface_->IsInputEvent(first) || |
+ !input_event_interface_->IsInputEvent(second)) { |
+ return false; |
+ } |
+ |
+ // Test fields common to all events. |
+ if (input_event_interface_->GetType(first) != |
+ input_event_interface_->GetType(second) && |
+ input_event_interface_->GetTimeStamp(first) != |
+ input_event_interface_->GetTimeStamp(second) && |
+ input_event_interface_->GetModifiers(first) != |
+ input_event_interface_->GetModifiers(second)) { |
+ return false; |
+ } |
+ |
+ // Test event type-specific fields. |
+ switch (input_event_interface_->GetType(first)) { |
+ 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: |
+ return |
+ mouse_input_event_interface_->GetButton(first) == |
+ mouse_input_event_interface_->GetButton(second) && |
+ pp::Point(mouse_input_event_interface_->GetPosition(first)) == |
+ pp::Point(mouse_input_event_interface_->GetPosition(second)) && |
+ mouse_input_event_interface_->GetClickCount(first) == |
+ mouse_input_event_interface_->GetClickCount(second) && |
+ pp::Point(mouse_input_event_interface_->GetMovement(first)) == |
+ pp::Point(mouse_input_event_interface_->GetMovement(second)); |
+ |
+ case PP_INPUTEVENT_TYPE_WHEEL: |
+ return |
+ pp::FloatPoint(wheel_input_event_interface_->GetDelta(first)) == |
+ pp::FloatPoint(wheel_input_event_interface_->GetDelta(second)) && |
+ pp::FloatPoint(wheel_input_event_interface_->GetTicks(first)) == |
+ pp::FloatPoint(wheel_input_event_interface_->GetTicks(second)) && |
+ wheel_input_event_interface_->GetScrollByPage(first) == |
+ wheel_input_event_interface_->GetScrollByPage(second); |
+ |
+ case PP_INPUTEVENT_TYPE_RAWKEYDOWN: |
+ case PP_INPUTEVENT_TYPE_KEYDOWN: |
+ case PP_INPUTEVENT_TYPE_KEYUP: |
+ return |
+ keyboard_input_event_interface_->GetKeyCode(first) == |
+ keyboard_input_event_interface_->GetKeyCode(second); |
+ |
+ case PP_INPUTEVENT_TYPE_CHAR: |
+ return |
+ keyboard_input_event_interface_->GetKeyCode(first) == |
+ keyboard_input_event_interface_->GetKeyCode(second) && |
+ pp::Var(pp::Var::DontManage(), |
dmichael (off chromium)
2011/11/07 17:12:58
Please don't use DontManage. It's a dangerous opti
bbudge
2011/11/08 00:05:05
Done. I admit, I find this confusing.
|
+ keyboard_input_event_interface_->GetCharacterText(first)) == |
+ pp::Var(pp::Var::DontManage(), |
+ keyboard_input_event_interface_->GetCharacterText(second)); |
+ |
+ 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()); |
+ } |
+ // Handle all input events. |
+ return true; |
+} |
+ |
+void TestInputEvent::DidChangeView(const pp::Rect& position, |
+ const pp::Rect& clip) { |
+ view_rect_ = position; |
+} |
+ |
+void TestInputEvent::RunTest() { |
+ RUN_TEST(Events); |
+} |
+ |
+std::string TestInputEvent::TestEvents() { |
+ // Create some events. |
+ pp::InputEvent mouse_input_event = CreateMouseEvent(); |
+ pp::InputEvent wheel_input_event = CreateWheelEvent(); |
+ pp::InputEvent key_input_event = CreateKeyEvent(); |
+ pp::InputEvent char_input_event = CreateCharEvent(); |
+ |
+ // Check that they were created. |
+ ASSERT_FALSE(mouse_input_event.is_null()); |
+ ASSERT_FALSE(wheel_input_event.is_null()); |
+ ASSERT_FALSE(key_input_event.is_null()); |
+ ASSERT_FALSE(char_input_event.is_null()); |
+ |
+ // Request all input events. |
+ 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(SimulateInputEvent(mouse_input_event)); |
+ ASSERT_TRUE(SimulateInputEvent(wheel_input_event)); |
+ ASSERT_TRUE(SimulateInputEvent(key_input_event)); |
+ ASSERT_TRUE(SimulateInputEvent(char_input_event)); |
+ |
+ // 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(SimulateInputEvent(mouse_input_event)); |
+ ASSERT_FALSE(SimulateInputEvent(wheel_input_event)); |
+ ASSERT_FALSE(SimulateInputEvent(key_input_event)); |
+ ASSERT_FALSE(SimulateInputEvent(char_input_event)); |
+ |
+ PASS(); |
+} |
+ |
Property changes on: ppapi\tests\test_input_event.cc |
___________________________________________________________________ |
Added: svn:eol-style |
+ LF |