Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ppapi/tests/test_input_event.h" | 5 #include "ppapi/tests/test_input_event.h" |
| 6 | 6 |
| 7 #include "ppapi/c/dev/ppb_testing_dev.h" | 7 #include "ppapi/c/dev/ppb_testing_dev.h" |
| 8 #include "ppapi/c/pp_errors.h" | 8 #include "ppapi/c/pp_errors.h" |
| 9 #include "ppapi/c/ppb_input_event.h" | 9 #include "ppapi/c/ppb_input_event.h" |
| 10 #include "ppapi/cpp/input_event.h" | 10 #include "ppapi/cpp/input_event.h" |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 32 RUN_TEST(Events, filter); | 32 RUN_TEST(Events, filter); |
| 33 } | 33 } |
| 34 | 34 |
| 35 TestInputEvent::TestInputEvent(TestingInstance* instance) | 35 TestInputEvent::TestInputEvent(TestingInstance* instance) |
| 36 : TestCase(instance), | 36 : TestCase(instance), |
| 37 input_event_interface_(NULL), | 37 input_event_interface_(NULL), |
| 38 mouse_input_event_interface_(NULL), | 38 mouse_input_event_interface_(NULL), |
| 39 wheel_input_event_interface_(NULL), | 39 wheel_input_event_interface_(NULL), |
| 40 keyboard_input_event_interface_(NULL), | 40 keyboard_input_event_interface_(NULL), |
| 41 view_rect_(), | 41 view_rect_(), |
| 42 expected_input_event_(0), | 42 expected_event_type_(PP_INPUTEVENT_TYPE_UNDEFINED), |
| 43 expected_mouse_buttons_(PP_INPUTEVENT_MOUSEBUTTON_LEFT), | |
| 44 expected_mouse_click_count_(0), | |
| 45 expected_wheel_delta_(0, 0), | |
| 46 expected_wheel_ticks_(0, 0), | |
| 47 expected_wheel_scroll_by_page_(PP_FALSE), | |
| 48 expected_key_code_(0), | |
| 49 expected_char_text_(), | |
|
dmichael (off chromium)
2011/12/13 23:40:55
As we discussed on IM, I think you've uncovered a
bbudge
2011/12/14 03:00:05
Done.
| |
| 43 received_expected_event_(false), | 50 received_expected_event_(false), |
| 44 received_finish_message_(false) { | 51 received_finish_message_(false) { |
| 45 } | 52 } |
| 46 | 53 |
| 47 TestInputEvent::~TestInputEvent() { | 54 TestInputEvent::~TestInputEvent() { |
| 48 // Remove the special listener that only responds to a | 55 // Remove the special listener that only responds to a |
| 49 // FINISHED_WAITING_MESSAGE string. See Init for where it gets added. | 56 // FINISHED_WAITING_MESSAGE string. See Init for where it gets added. |
| 50 std::string js_code; | 57 std::string js_code; |
| 51 js_code += "var plugin = document.getElementById('plugin');" | 58 js_code += "var plugin = document.getElementById('plugin');" |
| 52 "plugin.removeEventListener('message'," | 59 "plugin.removeEventListener('message'," |
| 53 " plugin.wait_for_messages_handler);" | 60 " plugin.wait_for_messages_handler);" |
| 54 "delete plugin.wait_for_messages_handler;"; | 61 "delete plugin.wait_for_messages_handler;"; |
| 55 pp::Var exception; | 62 pp::Var script(js_code); |
| 56 instance_->ExecuteScript(js_code, &exception); | 63 PP_Var exception = PP_MakeUndefined(); |
| 64 testing_interface_->ExecuteScript( | |
| 65 instance_->pp_instance(), script.pp_var(), &exception); | |
| 57 } | 66 } |
| 58 | 67 |
| 59 bool TestInputEvent::Init() { | 68 bool TestInputEvent::Init() { |
| 60 input_event_interface_ = static_cast<const PPB_InputEvent*>( | 69 input_event_interface_ = static_cast<const PPB_InputEvent*>( |
| 61 pp::Module::Get()->GetBrowserInterface(PPB_INPUT_EVENT_INTERFACE)); | 70 pp::Module::Get()->GetBrowserInterface(PPB_INPUT_EVENT_INTERFACE)); |
| 62 mouse_input_event_interface_ = static_cast<const PPB_MouseInputEvent*>( | 71 mouse_input_event_interface_ = static_cast<const PPB_MouseInputEvent*>( |
| 63 pp::Module::Get()->GetBrowserInterface( | 72 pp::Module::Get()->GetBrowserInterface( |
| 64 PPB_MOUSE_INPUT_EVENT_INTERFACE)); | 73 PPB_MOUSE_INPUT_EVENT_INTERFACE)); |
| 65 wheel_input_event_interface_ = static_cast<const PPB_WheelInputEvent*>( | 74 wheel_input_event_interface_ = static_cast<const PPB_WheelInputEvent*>( |
| 66 pp::Module::Get()->GetBrowserInterface( | 75 pp::Module::Get()->GetBrowserInterface( |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 87 js_code += "var plugin = document.getElementById('plugin');" | 96 js_code += "var plugin = document.getElementById('plugin');" |
| 88 "var wait_for_messages_handler = function(message_event) {" | 97 "var wait_for_messages_handler = function(message_event) {" |
| 89 " if (!IsTestingMessage(message_event.data) &&" | 98 " if (!IsTestingMessage(message_event.data) &&" |
| 90 " message_event.data === '" FINISHED_WAITING_MESSAGE "') {" | 99 " message_event.data === '" FINISHED_WAITING_MESSAGE "') {" |
| 91 " plugin.postMessage('" FINISHED_WAITING_MESSAGE "');" | 100 " plugin.postMessage('" FINISHED_WAITING_MESSAGE "');" |
| 92 " }" | 101 " }" |
| 93 "};" | 102 "};" |
| 94 "plugin.addEventListener('message', wait_for_messages_handler);" | 103 "plugin.addEventListener('message', wait_for_messages_handler);" |
| 95 // Stash it on the plugin so we can remove it in the destructor. | 104 // Stash it on the plugin so we can remove it in the destructor. |
| 96 "plugin.wait_for_messages_handler = wait_for_messages_handler;"; | 105 "plugin.wait_for_messages_handler = wait_for_messages_handler;"; |
| 97 pp::Var exception; | 106 pp::Var script(js_code); |
| 98 instance_->ExecuteScript(js_code, &exception); | 107 PP_Var exception = PP_MakeUndefined(); |
| 99 success = success && exception.is_undefined(); | 108 testing_interface_->ExecuteScript( |
| 109 instance_->pp_instance(), script.pp_var(), &exception); | |
| 110 success = success && exception.type == PP_VARTYPE_UNDEFINED; | |
| 100 | 111 |
| 101 return success; | 112 return success; |
| 102 } | 113 } |
| 103 | 114 |
| 104 pp::InputEvent TestInputEvent::CreateMouseEvent( | 115 pp::InputEvent TestInputEvent::CreateMouseEvent( |
| 105 PP_InputEvent_Type type, | 116 PP_InputEvent_Type type, |
| 106 PP_InputEvent_MouseButton buttons) { | 117 PP_InputEvent_MouseButton buttons) { |
| 118 expected_event_type_ = type; | |
| 119 expected_mouse_buttons_ = buttons; | |
| 120 expected_mouse_click_count_ = 1; | |
| 107 return pp::MouseInputEvent( | 121 return pp::MouseInputEvent( |
| 108 instance_, | 122 instance_, |
| 109 type, | 123 type, |
| 110 100, // time_stamp | 124 100, // time_stamp |
| 111 0, // modifiers | 125 0, // modifiers |
| 112 buttons, | 126 buttons, |
| 113 GetCenter(view_rect_), | 127 GetCenter(view_rect_), |
| 114 1, // click count | 128 1, // click count |
| 115 pp::Point()); // movement | 129 pp::Point()); // movement |
| 116 } | 130 } |
| 117 | 131 |
| 118 pp::InputEvent TestInputEvent::CreateWheelEvent() { | 132 pp::InputEvent TestInputEvent::CreateWheelEvent() { |
| 133 expected_event_type_ = PP_INPUTEVENT_TYPE_WHEEL; | |
| 134 expected_wheel_delta_ = pp::FloatPoint(1, 2); | |
| 135 expected_wheel_ticks_ = pp::FloatPoint(3, 4); | |
| 136 expected_wheel_scroll_by_page_ = PP_TRUE; | |
| 119 return pp::WheelInputEvent( | 137 return pp::WheelInputEvent( |
| 120 instance_, | 138 instance_, |
| 121 100, // time_stamp | 139 100, // time_stamp |
| 122 0, // modifiers | 140 0, // modifiers |
| 123 pp::FloatPoint(1, 2), | 141 expected_wheel_delta_, |
| 124 pp::FloatPoint(3, 4), | 142 expected_wheel_ticks_, |
| 125 PP_TRUE); // scroll_by_page | 143 true); |
| 126 } | 144 } |
| 127 | 145 |
| 128 pp::InputEvent TestInputEvent::CreateKeyEvent(PP_InputEvent_Type type, | 146 pp::InputEvent TestInputEvent::CreateKeyEvent(PP_InputEvent_Type type, |
| 129 uint32_t key_code) { | 147 uint32_t key_code) { |
| 148 expected_event_type_ = type; | |
| 149 expected_key_code_ = key_code; | |
| 130 return pp::KeyboardInputEvent( | 150 return pp::KeyboardInputEvent( |
| 131 instance_, | 151 instance_, |
| 132 type, | 152 type, |
| 133 100, // time_stamp | 153 100, // time_stamp |
| 134 0, // modifiers | 154 0, // modifiers |
| 135 key_code, | 155 key_code, |
| 136 pp::Var()); | 156 pp::Var()); |
| 137 } | 157 } |
| 138 | 158 |
| 139 pp::InputEvent TestInputEvent::CreateCharEvent(const std::string& text) { | 159 pp::InputEvent TestInputEvent::CreateCharEvent(const std::string& text) { |
| 160 expected_event_type_ = PP_INPUTEVENT_TYPE_CHAR; | |
| 161 expected_char_text_ = text; | |
| 140 return pp::KeyboardInputEvent( | 162 return pp::KeyboardInputEvent( |
| 141 instance_, | 163 instance_, |
| 142 PP_INPUTEVENT_TYPE_CHAR, | 164 expected_event_type_, |
| 143 100, // time_stamp | 165 100, // time_stamp |
| 144 0, // modifiers | 166 0, // modifiers |
| 145 0, // keycode | 167 0, // keycode |
| 146 pp::Var(text)); | 168 pp::Var(text)); |
| 147 } | 169 } |
| 148 | 170 |
| 149 // Simulates the input event and calls PostMessage to let us know when | 171 // Simulates the input event and calls PostMessage to let us know when |
| 150 // we have received all resulting events from the browser. | 172 // we have received all resulting events from the browser. |
| 151 bool TestInputEvent::SimulateInputEvent( | 173 bool TestInputEvent::SimulateInputEvent( |
| 152 const pp::InputEvent& input_event) { | 174 const pp::InputEvent& input_event) { |
| 153 expected_input_event_ = pp::InputEvent(input_event.pp_resource()); | |
| 154 received_expected_event_ = false; | 175 received_expected_event_ = false; |
| 155 received_finish_message_ = false; | 176 received_finish_message_ = false; |
| 156 testing_interface_->SimulateInputEvent(instance_->pp_instance(), | 177 testing_interface_->SimulateInputEvent(instance_->pp_instance(), |
| 157 input_event.pp_resource()); | 178 input_event.pp_resource()); |
| 158 instance_->PostMessage(pp::Var(FINISHED_WAITING_MESSAGE)); | 179 instance_->PostMessage(pp::Var(FINISHED_WAITING_MESSAGE)); |
| 159 testing_interface_->RunMessageLoop(instance_->pp_instance()); | 180 testing_interface_->RunMessageLoop(instance_->pp_instance()); |
| 160 return received_finish_message_ && received_expected_event_; | 181 return received_finish_message_ && received_expected_event_; |
| 161 } | 182 } |
| 162 | 183 |
| 163 bool TestInputEvent::AreEquivalentEvents(PP_Resource received, | 184 bool TestInputEvent::IsExpectedEvent(PP_Resource input_event) { |
| 164 PP_Resource expected) { | 185 if (!input_event_interface_->IsInputEvent(input_event)) { |
| 165 if (!input_event_interface_->IsInputEvent(received) || | |
| 166 !input_event_interface_->IsInputEvent(expected)) { | |
| 167 return false; | 186 return false; |
| 168 } | 187 } |
| 169 | |
| 170 // Test common fields, except modifiers and time stamp, which may be changed | 188 // Test common fields, except modifiers and time stamp, which may be changed |
| 171 // by the browser. | 189 // by the browser. |
| 172 int32_t received_type = input_event_interface_->GetType(received); | 190 int32_t event_type = input_event_interface_->GetType(input_event); |
| 173 int32_t expected_type = input_event_interface_->GetType(expected); | 191 if (event_type != expected_event_type_) { |
| 174 if (received_type != expected_type) { | |
| 175 // Allow key down events to match "raw" key down events. | 192 // Allow key down events to match "raw" key down events. |
| 176 if (expected_type != PP_INPUTEVENT_TYPE_KEYDOWN && | 193 if (expected_event_type_ != PP_INPUTEVENT_TYPE_KEYDOWN && |
| 177 received_type != PP_INPUTEVENT_TYPE_RAWKEYDOWN) { | 194 event_type != PP_INPUTEVENT_TYPE_RAWKEYDOWN) { |
| 178 return false; | 195 return false; |
| 179 } | 196 } |
| 180 } | 197 } |
| 181 | 198 |
| 182 // Test event type-specific fields. | 199 // Test event type-specific fields. |
| 183 switch (input_event_interface_->GetType(received)) { | 200 switch (event_type) { |
| 184 case PP_INPUTEVENT_TYPE_MOUSEDOWN: | 201 case PP_INPUTEVENT_TYPE_MOUSEDOWN: |
| 185 case PP_INPUTEVENT_TYPE_MOUSEUP: | 202 case PP_INPUTEVENT_TYPE_MOUSEUP: |
| 186 case PP_INPUTEVENT_TYPE_MOUSEMOVE: | 203 case PP_INPUTEVENT_TYPE_MOUSEMOVE: |
| 187 case PP_INPUTEVENT_TYPE_MOUSEENTER: | 204 case PP_INPUTEVENT_TYPE_MOUSEENTER: |
| 188 case PP_INPUTEVENT_TYPE_MOUSELEAVE: | 205 case PP_INPUTEVENT_TYPE_MOUSELEAVE: |
| 189 // Check mouse fields, except position and movement, which may be | 206 // Check mouse fields, except position and movement, which may be |
| 190 // modified by the renderer. | 207 // modified by the renderer. |
| 191 return | 208 return |
| 192 mouse_input_event_interface_->GetButton(received) == | 209 mouse_input_event_interface_->GetButton(input_event) == |
| 193 mouse_input_event_interface_->GetButton(expected) && | 210 expected_mouse_buttons_ && |
| 194 mouse_input_event_interface_->GetClickCount(received) == | 211 mouse_input_event_interface_->GetClickCount(input_event) == |
| 195 mouse_input_event_interface_->GetClickCount(expected); | 212 expected_mouse_click_count_; |
| 196 | 213 |
| 197 case PP_INPUTEVENT_TYPE_WHEEL: | 214 case PP_INPUTEVENT_TYPE_WHEEL: |
| 198 return | 215 return |
| 199 pp::FloatPoint(wheel_input_event_interface_->GetDelta(received)) == | 216 pp::FloatPoint(wheel_input_event_interface_->GetDelta(input_event)) == |
| 200 pp::FloatPoint(wheel_input_event_interface_->GetDelta(expected)) && | 217 expected_wheel_delta_ && |
| 201 pp::FloatPoint(wheel_input_event_interface_->GetTicks(received)) == | 218 pp::FloatPoint(wheel_input_event_interface_->GetTicks(input_event)) == |
| 202 pp::FloatPoint(wheel_input_event_interface_->GetTicks(expected)) && | 219 expected_wheel_ticks_ && |
| 203 wheel_input_event_interface_->GetScrollByPage(received) == | 220 wheel_input_event_interface_->GetScrollByPage(input_event) == |
| 204 wheel_input_event_interface_->GetScrollByPage(expected); | 221 expected_wheel_scroll_by_page_; |
| 205 | 222 |
| 206 case PP_INPUTEVENT_TYPE_RAWKEYDOWN: | 223 case PP_INPUTEVENT_TYPE_RAWKEYDOWN: |
| 207 case PP_INPUTEVENT_TYPE_KEYDOWN: | 224 case PP_INPUTEVENT_TYPE_KEYDOWN: |
| 208 case PP_INPUTEVENT_TYPE_KEYUP: | 225 case PP_INPUTEVENT_TYPE_KEYUP: |
| 209 return | 226 return |
| 210 keyboard_input_event_interface_->GetKeyCode(received) == | 227 keyboard_input_event_interface_->GetKeyCode(input_event) == |
| 211 keyboard_input_event_interface_->GetKeyCode(expected); | 228 expected_key_code_; |
| 212 | 229 |
| 213 case PP_INPUTEVENT_TYPE_CHAR: | 230 case PP_INPUTEVENT_TYPE_CHAR: |
| 214 return | 231 return |
| 215 keyboard_input_event_interface_->GetKeyCode(received) == | |
| 216 keyboard_input_event_interface_->GetKeyCode(expected) && | |
| 217 pp::Var(pp::Var::PassRef(), | 232 pp::Var(pp::Var::PassRef(), |
| 218 keyboard_input_event_interface_->GetCharacterText(received)) == | 233 keyboard_input_event_interface_->GetCharacterText(input_event)) == |
| 219 pp::Var(pp::Var::PassRef(), | 234 pp::Var(expected_char_text_); |
| 220 keyboard_input_event_interface_->GetCharacterText(expected)); | |
| 221 | 235 |
| 222 default: | 236 default: |
| 223 break; | 237 break; |
| 224 } | 238 } |
| 225 | 239 |
| 226 return false; | 240 return false; |
| 227 } | 241 } |
| 228 | 242 |
| 229 bool TestInputEvent::HandleInputEvent(const pp::InputEvent& input_event) { | 243 bool TestInputEvent::HandleInputEvent(const pp::InputEvent& input_event) { |
| 230 // Some events may cause extra events to be generated, so look for the | 244 // Some events may cause extra events to be generated, so look for the |
| 231 // first one that matches. | 245 // first one that matches. |
| 232 if (!received_expected_event_) { | 246 if (!received_expected_event_) { |
| 233 received_expected_event_ = AreEquivalentEvents( | 247 received_expected_event_ = IsExpectedEvent(input_event.pp_resource()); |
| 234 input_event.pp_resource(), | |
| 235 expected_input_event_.pp_resource()); | |
| 236 } | 248 } |
| 237 // Handle all input events. | 249 // Handle all input events. |
| 238 return true; | 250 return true; |
| 239 } | 251 } |
| 240 | 252 |
| 241 void TestInputEvent::HandleMessage(const pp::Var& message_data) { | 253 void TestInputEvent::HandleMessage(const pp::Var& message_data) { |
| 242 if (message_data.is_string() && | 254 if (message_data.is_string() && |
| 243 (message_data.AsString() == FINISHED_WAITING_MESSAGE)) { | 255 (message_data.AsString() == FINISHED_WAITING_MESSAGE)) { |
| 244 testing_interface_->QuitMessageLoop(instance_->pp_instance()); | 256 testing_interface_->QuitMessageLoop(instance_->pp_instance()); |
| 245 received_finish_message_ = true; | 257 received_finish_message_ = true; |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 281 SimulateInputEvent(CreateWheelEvent())); | 293 SimulateInputEvent(CreateWheelEvent())); |
| 282 ASSERT_FALSE( | 294 ASSERT_FALSE( |
| 283 SimulateInputEvent(CreateKeyEvent(PP_INPUTEVENT_TYPE_KEYDOWN, | 295 SimulateInputEvent(CreateKeyEvent(PP_INPUTEVENT_TYPE_KEYDOWN, |
| 284 kSpaceChar))); | 296 kSpaceChar))); |
| 285 ASSERT_FALSE( | 297 ASSERT_FALSE( |
| 286 SimulateInputEvent(CreateCharEvent(kSpaceString))); | 298 SimulateInputEvent(CreateCharEvent(kSpaceString))); |
| 287 | 299 |
| 288 PASS(); | 300 PASS(); |
| 289 } | 301 } |
| 290 | 302 |
| OLD | NEW |