OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 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. |
| 4 |
| 5 #include "ppapi/tests/test_input_event.h" |
| 6 |
| 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" |
| 14 |
| 15 REGISTER_TEST_CASE(InputEvent); |
| 16 |
| 17 namespace { |
| 18 |
| 19 const uint32_t kSpaceChar = 0x20; |
| 20 const char* kSpaceString = " "; |
| 21 |
| 22 #define FINISHED_WAITING_MESSAGE "TEST_INPUT_EVENT_FINISHED_WAITING" |
| 23 |
| 24 pp::Point GetCenter(const pp::Rect& rect) { |
| 25 return pp::Point( |
| 26 rect.x() + rect.width() / 2, |
| 27 rect.y() + rect.height() / 2); |
| 28 } |
| 29 } |
| 30 |
| 31 void TestInputEvent::RunTests(const std::string& filter) { |
| 32 RUN_TEST(Events, filter); |
| 33 } |
| 34 |
| 35 TestInputEvent::TestInputEvent(TestingInstance* instance) |
| 36 : TestCase(instance), |
| 37 input_event_interface_(NULL), |
| 38 mouse_input_event_interface_(NULL), |
| 39 wheel_input_event_interface_(NULL), |
| 40 keyboard_input_event_interface_(NULL), |
| 41 view_rect_(), |
| 42 expected_input_event_(0), |
| 43 received_expected_event_(false), |
| 44 received_finish_message_(false) { |
| 45 } |
| 46 |
| 47 TestInputEvent::~TestInputEvent() { |
| 48 // Remove the special listener that only responds to a |
| 49 // FINISHED_WAITING_MESSAGE string. See Init for where it gets added. |
| 50 std::string js_code; |
| 51 js_code += "var plugin = document.getElementById('plugin');" |
| 52 "plugin.removeEventListener('message'," |
| 53 " plugin.wait_for_messages_handler);" |
| 54 "delete plugin.wait_for_messages_handler;"; |
| 55 pp::Var exception; |
| 56 instance_->ExecuteScript(js_code, &exception); |
| 57 } |
| 58 |
| 59 bool TestInputEvent::Init() { |
| 60 input_event_interface_ = static_cast<PPB_InputEvent const*>( |
| 61 pp::Module::Get()->GetBrowserInterface(PPB_INPUT_EVENT_INTERFACE)); |
| 62 mouse_input_event_interface_ = static_cast<PPB_MouseInputEvent const*>( |
| 63 pp::Module::Get()->GetBrowserInterface( |
| 64 PPB_MOUSE_INPUT_EVENT_INTERFACE)); |
| 65 wheel_input_event_interface_ = static_cast<PPB_WheelInputEvent const*>( |
| 66 pp::Module::Get()->GetBrowserInterface( |
| 67 PPB_WHEEL_INPUT_EVENT_INTERFACE)); |
| 68 keyboard_input_event_interface_ = static_cast<PPB_KeyboardInputEvent const*>( |
| 69 pp::Module::Get()->GetBrowserInterface( |
| 70 PPB_KEYBOARD_INPUT_EVENT_INTERFACE)); |
| 71 |
| 72 bool success = |
| 73 input_event_interface_ && |
| 74 mouse_input_event_interface_ && |
| 75 wheel_input_event_interface_ && |
| 76 keyboard_input_event_interface_ && |
| 77 InitTestingInterface(); |
| 78 |
| 79 // Set up a listener for our message that signals that all input events have |
| 80 // been received. |
| 81 std::string js_code; |
| 82 // Note the following code is dependent on some features of test_case.html. |
| 83 // E.g., it is assumed that the DOM element where the plugin is embedded has |
| 84 // an id of 'plugin', and there is a function 'IsTestingMessage' that allows |
| 85 // us to ignore the messages that are intended for use by the testing |
| 86 // framework itself. |
| 87 js_code += "var plugin = document.getElementById('plugin');" |
| 88 "var wait_for_messages_handler = function(message_event) {" |
| 89 " if (!IsTestingMessage(message_event.data) &&" |
| 90 " message_event.data === '" FINISHED_WAITING_MESSAGE "') {" |
| 91 " plugin.postMessage('" FINISHED_WAITING_MESSAGE "');" |
| 92 " }" |
| 93 "};" |
| 94 "plugin.addEventListener('message', wait_for_messages_handler);" |
| 95 // Stash it on the plugin so we can remove it in the destructor. |
| 96 "plugin.wait_for_messages_handler = wait_for_messages_handler;"; |
| 97 pp::Var exception; |
| 98 instance_->ExecuteScript(js_code, &exception); |
| 99 success = success && exception.is_undefined(); |
| 100 |
| 101 return success; |
| 102 } |
| 103 |
| 104 pp::InputEvent TestInputEvent::CreateMouseEvent( |
| 105 PP_InputEvent_Type type, |
| 106 PP_InputEvent_MouseButton buttons) { |
| 107 return pp::MouseInputEvent( |
| 108 instance_, |
| 109 type, |
| 110 100, // time_stamp |
| 111 0, // modifiers |
| 112 buttons, |
| 113 GetCenter(view_rect_), |
| 114 1, // click count |
| 115 pp::Point()); // movement |
| 116 } |
| 117 |
| 118 pp::InputEvent TestInputEvent::CreateWheelEvent() { |
| 119 return pp::WheelInputEvent( |
| 120 instance_, |
| 121 100, // time_stamp |
| 122 0, // modifiers |
| 123 pp::FloatPoint(1, 2), |
| 124 pp::FloatPoint(3, 4), |
| 125 PP_TRUE); // scroll_by_page |
| 126 } |
| 127 |
| 128 pp::InputEvent TestInputEvent::CreateKeyEvent(PP_InputEvent_Type type, |
| 129 uint32_t key_code) { |
| 130 return pp::KeyboardInputEvent( |
| 131 instance_, |
| 132 type, |
| 133 100, // time_stamp |
| 134 0, // modifiers |
| 135 key_code, |
| 136 pp::Var()); |
| 137 } |
| 138 |
| 139 pp::InputEvent TestInputEvent::CreateCharEvent(const std::string& text) { |
| 140 return pp::KeyboardInputEvent( |
| 141 instance_, |
| 142 PP_INPUTEVENT_TYPE_CHAR, |
| 143 100, // time_stamp |
| 144 0, // modifiers |
| 145 0, // keycode |
| 146 pp::Var(text)); |
| 147 } |
| 148 |
| 149 // Simulates the input event and calls PostMessage to let us know when |
| 150 // we have received all resulting events from the browser. |
| 151 bool TestInputEvent::SimulateInputEvent( |
| 152 const pp::InputEvent& input_event) { |
| 153 expected_input_event_ = pp::InputEvent(input_event.pp_resource()); |
| 154 received_expected_event_ = false; |
| 155 received_finish_message_ = false; |
| 156 testing_interface_->SimulateInputEvent(instance_->pp_instance(), |
| 157 input_event.pp_resource()); |
| 158 instance_->PostMessage(pp::Var(FINISHED_WAITING_MESSAGE)); |
| 159 testing_interface_->RunMessageLoop(instance_->pp_instance()); |
| 160 return received_finish_message_ && received_expected_event_; |
| 161 } |
| 162 |
| 163 bool TestInputEvent::AreEquivalentEvents(PP_Resource received, |
| 164 PP_Resource expected) { |
| 165 if (!input_event_interface_->IsInputEvent(received) || |
| 166 !input_event_interface_->IsInputEvent(expected)) { |
| 167 return false; |
| 168 } |
| 169 |
| 170 // Test common fields, except modifiers and time stamp, which may be changed |
| 171 // by the browser. |
| 172 int32_t received_type = input_event_interface_->GetType(received); |
| 173 int32_t expected_type = input_event_interface_->GetType(expected); |
| 174 if (received_type != expected_type) { |
| 175 // Allow key down events to match "raw" key down events. |
| 176 if (expected_type != PP_INPUTEVENT_TYPE_KEYDOWN && |
| 177 received_type != PP_INPUTEVENT_TYPE_RAWKEYDOWN) { |
| 178 return false; |
| 179 } |
| 180 } |
| 181 |
| 182 // Test event type-specific fields. |
| 183 switch (input_event_interface_->GetType(received)) { |
| 184 case PP_INPUTEVENT_TYPE_MOUSEDOWN: |
| 185 case PP_INPUTEVENT_TYPE_MOUSEUP: |
| 186 case PP_INPUTEVENT_TYPE_MOUSEMOVE: |
| 187 case PP_INPUTEVENT_TYPE_MOUSEENTER: |
| 188 case PP_INPUTEVENT_TYPE_MOUSELEAVE: |
| 189 // Check mouse fields, except position and movement, which may be |
| 190 // modified by the renderer. |
| 191 return |
| 192 mouse_input_event_interface_->GetButton(received) == |
| 193 mouse_input_event_interface_->GetButton(expected) && |
| 194 mouse_input_event_interface_->GetClickCount(received) == |
| 195 mouse_input_event_interface_->GetClickCount(expected); |
| 196 |
| 197 case PP_INPUTEVENT_TYPE_WHEEL: |
| 198 return |
| 199 pp::FloatPoint(wheel_input_event_interface_->GetDelta(received)) == |
| 200 pp::FloatPoint(wheel_input_event_interface_->GetDelta(expected)) && |
| 201 pp::FloatPoint(wheel_input_event_interface_->GetTicks(received)) == |
| 202 pp::FloatPoint(wheel_input_event_interface_->GetTicks(expected)) && |
| 203 wheel_input_event_interface_->GetScrollByPage(received) == |
| 204 wheel_input_event_interface_->GetScrollByPage(expected); |
| 205 |
| 206 case PP_INPUTEVENT_TYPE_RAWKEYDOWN: |
| 207 case PP_INPUTEVENT_TYPE_KEYDOWN: |
| 208 case PP_INPUTEVENT_TYPE_KEYUP: |
| 209 return |
| 210 keyboard_input_event_interface_->GetKeyCode(received) == |
| 211 keyboard_input_event_interface_->GetKeyCode(expected); |
| 212 |
| 213 case PP_INPUTEVENT_TYPE_CHAR: |
| 214 return |
| 215 keyboard_input_event_interface_->GetKeyCode(received) == |
| 216 keyboard_input_event_interface_->GetKeyCode(expected) && |
| 217 pp::Var(pp::Var::PassRef(), |
| 218 keyboard_input_event_interface_->GetCharacterText(received)) == |
| 219 pp::Var(pp::Var::PassRef(), |
| 220 keyboard_input_event_interface_->GetCharacterText(expected)); |
| 221 |
| 222 default: |
| 223 break; |
| 224 } |
| 225 |
| 226 return false; |
| 227 } |
| 228 |
| 229 bool TestInputEvent::HandleInputEvent(const pp::InputEvent& input_event) { |
| 230 // Some events may cause extra events to be generated, so look for the |
| 231 // first one that matches. |
| 232 if (!received_expected_event_) { |
| 233 received_expected_event_ = AreEquivalentEvents( |
| 234 input_event.pp_resource(), |
| 235 expected_input_event_.pp_resource()); |
| 236 } |
| 237 // Handle all input events. |
| 238 return true; |
| 239 } |
| 240 |
| 241 void TestInputEvent::HandleMessage(const pp::Var& message_data) { |
| 242 if (message_data.is_string() && |
| 243 (message_data.AsString() == FINISHED_WAITING_MESSAGE)) { |
| 244 testing_interface_->QuitMessageLoop(instance_->pp_instance()); |
| 245 received_finish_message_ = true; |
| 246 } |
| 247 } |
| 248 |
| 249 void TestInputEvent::DidChangeView(const pp::Rect& position, |
| 250 const pp::Rect& clip) { |
| 251 view_rect_ = position; |
| 252 } |
| 253 |
| 254 std::string TestInputEvent::TestEvents() { |
| 255 // Request all input event classes. |
| 256 input_event_interface_->RequestInputEvents(instance_->pp_instance(), |
| 257 PP_INPUTEVENT_CLASS_MOUSE | |
| 258 PP_INPUTEVENT_CLASS_WHEEL | |
| 259 PP_INPUTEVENT_CLASS_KEYBOARD); |
| 260 // Send the events and check that we received them. |
| 261 ASSERT_TRUE( |
| 262 SimulateInputEvent(CreateMouseEvent(PP_INPUTEVENT_TYPE_MOUSEDOWN, |
| 263 PP_INPUTEVENT_MOUSEBUTTON_LEFT))); |
| 264 ASSERT_TRUE( |
| 265 SimulateInputEvent(CreateWheelEvent())); |
| 266 ASSERT_TRUE( |
| 267 SimulateInputEvent(CreateKeyEvent(PP_INPUTEVENT_TYPE_KEYDOWN, |
| 268 kSpaceChar))); |
| 269 ASSERT_TRUE( |
| 270 SimulateInputEvent(CreateCharEvent(kSpaceString))); |
| 271 |
| 272 // Request only mouse events. |
| 273 input_event_interface_->ClearInputEventRequest(instance_->pp_instance(), |
| 274 PP_INPUTEVENT_CLASS_WHEEL | |
| 275 PP_INPUTEVENT_CLASS_KEYBOARD); |
| 276 // Check that we only receive mouse events. |
| 277 ASSERT_TRUE( |
| 278 SimulateInputEvent(CreateMouseEvent(PP_INPUTEVENT_TYPE_MOUSEDOWN, |
| 279 PP_INPUTEVENT_MOUSEBUTTON_LEFT))); |
| 280 ASSERT_FALSE( |
| 281 SimulateInputEvent(CreateWheelEvent())); |
| 282 ASSERT_FALSE( |
| 283 SimulateInputEvent(CreateKeyEvent(PP_INPUTEVENT_TYPE_KEYDOWN, |
| 284 kSpaceChar))); |
| 285 ASSERT_FALSE( |
| 286 SimulateInputEvent(CreateCharEvent(kSpaceString))); |
| 287 |
| 288 PASS(); |
| 289 } |
| 290 |
OLD | NEW |