Chromium Code Reviews| 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 pp::Point GetCenter(const pp::Rect& rect) { | |
| 19 return pp::Point( | |
| 20 rect.x() + rect.width() / 2, | |
| 21 rect.y() + rect.height() / 2); | |
| 22 } | |
| 23 } | |
| 24 | |
| 25 void TestInputEvent::RunTests(const std::string& filter) { | |
| 26 RUN_TEST(Events, filter); | |
| 27 } | |
| 28 | |
| 29 TestInputEvent::TestInputEvent(TestingInstance* instance) | |
| 30 : TestCase(instance), | |
| 31 input_event_interface_(NULL), | |
| 32 mouse_input_event_interface_(NULL), | |
| 33 wheel_input_event_interface_(NULL), | |
| 34 keyboard_input_event_interface_(NULL), | |
| 35 testing_interface_(NULL), | |
| 36 view_rect_(), | |
| 37 expected_input_event_(0), | |
| 38 unexpected_input_event_(0), | |
| 39 received_expected_event_(false), | |
| 40 received_unexpected_event_(false), | |
| 41 running_message_loop_(false) { | |
| 42 } | |
| 43 | |
| 44 bool TestInputEvent::Init() { | |
| 45 input_event_interface_ = static_cast<PPB_InputEvent const*>( | |
| 46 pp::Module::Get()->GetBrowserInterface(PPB_INPUT_EVENT_INTERFACE)); | |
| 47 mouse_input_event_interface_ = static_cast<PPB_MouseInputEvent const*>( | |
| 48 pp::Module::Get()->GetBrowserInterface( | |
| 49 PPB_MOUSE_INPUT_EVENT_INTERFACE)); | |
| 50 wheel_input_event_interface_ = static_cast<PPB_WheelInputEvent const*>( | |
| 51 pp::Module::Get()->GetBrowserInterface( | |
| 52 PPB_WHEEL_INPUT_EVENT_INTERFACE)); | |
| 53 keyboard_input_event_interface_ = static_cast<PPB_KeyboardInputEvent const*>( | |
| 54 pp::Module::Get()->GetBrowserInterface( | |
| 55 PPB_KEYBOARD_INPUT_EVENT_INTERFACE)); | |
| 56 | |
| 57 testing_interface_ = GetTestingInterface(); | |
| 58 if (!testing_interface_) { | |
| 59 instance_->AppendError("Testing interface not available"); | |
| 60 } | |
| 61 | |
| 62 return | |
| 63 !!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.
| |
| 64 !!mouse_input_event_interface_ && | |
| 65 !!wheel_input_event_interface_ && | |
| 66 !!keyboard_input_event_interface_ && | |
| 67 !!testing_interface_; | |
| 68 } | |
| 69 | |
| 70 pp::InputEvent TestInputEvent::CreateMouseEvent() { | |
| 71 return CreateMouseEvent(PP_INPUTEVENT_TYPE_MOUSEDOWN, | |
| 72 PP_INPUTEVENT_MOUSEBUTTON_LEFT); | |
| 73 } | |
| 74 | |
| 75 pp::InputEvent TestInputEvent::CreateMouseEvent( | |
| 76 PP_InputEvent_Type type, | |
| 77 PP_InputEvent_MouseButton buttons) { | |
| 78 return pp::MouseInputEvent( | |
| 79 instance_, | |
| 80 type, | |
| 81 100, // time_stamp | |
| 82 0, // modifiers | |
| 83 buttons, | |
| 84 GetCenter(view_rect_), | |
| 85 1, // click count | |
| 86 pp::Point()); // movement | |
| 87 } | |
| 88 | |
| 89 pp::InputEvent TestInputEvent::CreateWheelEvent() { | |
| 90 return pp::WheelInputEvent( | |
| 91 instance_, | |
| 92 100, // time_stamp | |
| 93 0, // modifiers | |
| 94 pp::FloatPoint(1, 2), | |
| 95 pp::FloatPoint(3, 4), | |
| 96 PP_TRUE); // scroll_by_page | |
| 97 } | |
| 98 | |
| 99 pp::InputEvent TestInputEvent::CreateKeyEvent() { | |
| 100 return CreateKeyEvent(PP_INPUTEVENT_TYPE_KEYDOWN, | |
| 101 0x20); // space | |
| 102 } | |
| 103 | |
| 104 pp::InputEvent TestInputEvent::CreateKeyEvent(PP_InputEvent_Type type, | |
| 105 uint32_t key_code) { | |
| 106 return pp::KeyboardInputEvent( | |
| 107 instance_, | |
| 108 type, | |
| 109 100, // time_stamp | |
| 110 0, // modifiers | |
| 111 key_code, | |
| 112 pp::Var()); | |
| 113 } | |
| 114 | |
| 115 pp::InputEvent TestInputEvent::CreateCharEvent() { | |
| 116 return CreateCharEvent(" "); // space | |
| 117 } | |
| 118 | |
| 119 pp::InputEvent TestInputEvent::CreateCharEvent(const std::string& text) { | |
| 120 return pp::KeyboardInputEvent( | |
| 121 instance_, | |
| 122 PP_INPUTEVENT_TYPE_CHAR, | |
| 123 100, // time_stamp | |
| 124 0, // modifiers | |
| 125 0, // keycode | |
| 126 pp::Var(text)); | |
| 127 } | |
| 128 | |
| 129 // Simulates the input event and make sure it is received by HandleInputEvent. | |
| 130 bool TestInputEvent::TestRequestedInputEvent( | |
| 131 const pp::InputEvent& input_event) { | |
| 132 expected_input_event_ = pp::InputEvent(input_event.pp_resource()); | |
| 133 received_expected_event_ = false; | |
| 134 testing_interface_->SimulateInputEvent(instance_->pp_instance(), | |
| 135 input_event.pp_resource()); | |
| 136 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
| |
| 137 running_message_loop_ = true; | |
| 138 testing_interface_->RunMessageLoop(instance_->pp_instance()); | |
| 139 } | |
| 140 return received_expected_event_; | |
| 141 } | |
| 142 | |
| 143 // Simulates the unrequested and requested input events and makes sure only | |
| 144 // 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.
| |
| 145 // a case where no event is received in out-of-process plugins. | |
| 146 bool TestInputEvent::TestUnrequestedInputEvent( | |
| 147 const pp::InputEvent& unrequested_input_event, | |
| 148 const pp::InputEvent& requested_input_event) { | |
| 149 unexpected_input_event_ = pp::InputEvent( | |
| 150 unrequested_input_event.pp_resource()); | |
| 151 received_unexpected_event_ = false; | |
| 152 testing_interface_->SimulateInputEvent( | |
| 153 instance_->pp_instance(), unrequested_input_event.pp_resource()); | |
| 154 return | |
| 155 TestRequestedInputEvent(requested_input_event) && | |
| 156 !received_unexpected_event_; | |
| 157 } | |
| 158 | |
| 159 bool TestInputEvent::AreEquivalentEvents(PP_Resource received, | |
| 160 PP_Resource expected) { | |
| 161 if (!input_event_interface_->IsInputEvent(received) || | |
| 162 !input_event_interface_->IsInputEvent(expected)) { | |
| 163 return false; | |
| 164 } | |
| 165 | |
| 166 // Test common fields, except modifiers and time stamp, which may be changed | |
| 167 // by the renderer. | |
| 168 int32_t received_type = input_event_interface_->GetType(received); | |
| 169 int32_t expected_type = input_event_interface_->GetType(expected); | |
| 170 if (received_type != expected_type) { | |
| 171 // Allow key down events to match "raw" key down events. | |
| 172 if (expected_type != PP_INPUTEVENT_TYPE_KEYDOWN && | |
| 173 received_type != PP_INPUTEVENT_TYPE_RAWKEYDOWN) { | |
| 174 return false; | |
| 175 } | |
| 176 } | |
| 177 | |
| 178 // Test event type-specific fields. | |
| 179 switch (input_event_interface_->GetType(received)) { | |
| 180 case PP_INPUTEVENT_TYPE_MOUSEDOWN: | |
| 181 case PP_INPUTEVENT_TYPE_MOUSEUP: | |
| 182 case PP_INPUTEVENT_TYPE_MOUSEMOVE: | |
| 183 case PP_INPUTEVENT_TYPE_MOUSEENTER: | |
| 184 case PP_INPUTEVENT_TYPE_MOUSELEAVE: | |
| 185 // Check mouse fields, except position and movement, which may be | |
| 186 // modified by the renderer. | |
| 187 return | |
| 188 mouse_input_event_interface_->GetButton(received) == | |
| 189 mouse_input_event_interface_->GetButton(expected) && | |
| 190 mouse_input_event_interface_->GetClickCount(received) == | |
| 191 mouse_input_event_interface_->GetClickCount(expected); | |
| 192 | |
| 193 case PP_INPUTEVENT_TYPE_WHEEL: | |
| 194 return | |
| 195 pp::FloatPoint(wheel_input_event_interface_->GetDelta(received)) == | |
| 196 pp::FloatPoint(wheel_input_event_interface_->GetDelta(expected)) && | |
| 197 pp::FloatPoint(wheel_input_event_interface_->GetTicks(received)) == | |
| 198 pp::FloatPoint(wheel_input_event_interface_->GetTicks(expected)) && | |
| 199 wheel_input_event_interface_->GetScrollByPage(received) == | |
| 200 wheel_input_event_interface_->GetScrollByPage(expected); | |
| 201 | |
| 202 case PP_INPUTEVENT_TYPE_RAWKEYDOWN: | |
| 203 case PP_INPUTEVENT_TYPE_KEYDOWN: | |
| 204 case PP_INPUTEVENT_TYPE_KEYUP: | |
| 205 return | |
| 206 keyboard_input_event_interface_->GetKeyCode(received) == | |
| 207 keyboard_input_event_interface_->GetKeyCode(expected); | |
| 208 | |
| 209 case PP_INPUTEVENT_TYPE_CHAR: | |
| 210 return | |
| 211 keyboard_input_event_interface_->GetKeyCode(received) == | |
| 212 keyboard_input_event_interface_->GetKeyCode(expected) && | |
| 213 pp::Var(pp::Var::PassRef(), | |
| 214 keyboard_input_event_interface_->GetCharacterText(received)) == | |
| 215 pp::Var(pp::Var::PassRef(), | |
| 216 keyboard_input_event_interface_->GetCharacterText(expected)); | |
| 217 | |
| 218 default: | |
| 219 break; | |
| 220 } | |
| 221 | |
| 222 return false; | |
| 223 } | |
| 224 | |
| 225 bool TestInputEvent::HandleInputEvent(const pp::InputEvent& input_event) { | |
| 226 // Some events may cause extra events to be generated, so record the first | |
| 227 // event that matches. | |
| 228 if (!received_expected_event_) { | |
| 229 received_expected_event_ = AreEquivalentEvents( | |
| 230 input_event.pp_resource(), | |
| 231 expected_input_event_.pp_resource()); | |
| 232 if (received_expected_event_ && running_message_loop_) { | |
| 233 testing_interface_->QuitMessageLoop(instance_->pp_instance()); | |
| 234 running_message_loop_ = false; | |
| 235 } | |
| 236 } | |
| 237 if (!received_unexpected_event_) { | |
| 238 received_unexpected_event_ = AreEquivalentEvents( | |
| 239 input_event.pp_resource(), | |
| 240 unexpected_input_event_.pp_resource()); | |
| 241 } | |
| 242 // Handle all input events. | |
| 243 return true; | |
| 244 } | |
| 245 | |
| 246 void TestInputEvent::DidChangeView(const pp::Rect& position, | |
| 247 const pp::Rect& clip) { | |
| 248 view_rect_ = position; | |
| 249 } | |
| 250 | |
| 251 std::string TestInputEvent::TestEvents() { | |
| 252 // Request all input event classes. | |
| 253 input_event_interface_->RequestInputEvents(instance_->pp_instance(), | |
| 254 PP_INPUTEVENT_CLASS_MOUSE | | |
| 255 PP_INPUTEVENT_CLASS_WHEEL | | |
| 256 PP_INPUTEVENT_CLASS_KEYBOARD); | |
| 257 // Send the events and check that we received them. | |
| 258 ASSERT_TRUE(TestRequestedInputEvent(CreateMouseEvent())); | |
| 259 ASSERT_TRUE(TestRequestedInputEvent(CreateWheelEvent())); | |
| 260 ASSERT_TRUE(TestRequestedInputEvent(CreateKeyEvent())); | |
| 261 ASSERT_TRUE(TestRequestedInputEvent(CreateCharEvent())); | |
| 262 | |
| 263 // Request only mouse events. | |
| 264 input_event_interface_->ClearInputEventRequest(instance_->pp_instance(), | |
| 265 PP_INPUTEVENT_CLASS_WHEEL | | |
| 266 PP_INPUTEVENT_CLASS_KEYBOARD); | |
| 267 // Check that we only receive mouse events. | |
| 268 ASSERT_TRUE(TestRequestedInputEvent(CreateMouseEvent())); | |
| 269 ASSERT_TRUE(TestUnrequestedInputEvent(CreateWheelEvent(), | |
| 270 CreateMouseEvent())); | |
| 271 ASSERT_TRUE(TestUnrequestedInputEvent(CreateKeyEvent(), | |
| 272 CreateMouseEvent())); | |
| 273 ASSERT_TRUE(TestUnrequestedInputEvent(CreateCharEvent(), | |
| 274 CreateMouseEvent())); | |
| 275 | |
| 276 PASS(); | |
| 277 } | |
| 278 | |
| OLD | NEW |