|
OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2010 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 namespace { | |
16 pp::Point GetCenter(const pp::Rect& rect) { | |
17 return pp::Point( | |
18 rect.x() + rect.width() / 2, | |
19 rect.y() + rect.height() / 2); | |
20 } | |
21 } | |
22 | |
23 REGISTER_TEST_CASE(InputEvent); | |
24 | |
25 TestInputEvent::TestInputEvent(TestingInstance* instance) | |
26 : TestCase(instance), | |
27 input_event_interface_(NULL), | |
28 mouse_input_event_interface_(NULL), | |
29 wheel_input_event_interface_(NULL), | |
30 keyboard_input_event_interface_(NULL), | |
31 testing_interface_(NULL), | |
32 expected_input_event_(0), | |
33 received_expected_event_(false), | |
34 view_rect_() { | |
35 } | |
36 | |
37 bool TestInputEvent::Init() { | |
38 input_event_interface_ = static_cast<PPB_InputEvent const*>( | |
39 pp::Module::Get()->GetBrowserInterface(PPB_INPUT_EVENT_INTERFACE)); | |
40 mouse_input_event_interface_ = static_cast<PPB_MouseInputEvent const*>( | |
41 pp::Module::Get()->GetBrowserInterface( | |
42 PPB_MOUSE_INPUT_EVENT_INTERFACE)); | |
43 wheel_input_event_interface_ = static_cast<PPB_WheelInputEvent const*>( | |
44 pp::Module::Get()->GetBrowserInterface( | |
45 PPB_WHEEL_INPUT_EVENT_INTERFACE)); | |
46 keyboard_input_event_interface_ = static_cast<PPB_KeyboardInputEvent const*>( | |
47 pp::Module::Get()->GetBrowserInterface( | |
48 PPB_KEYBOARD_INPUT_EVENT_INTERFACE)); | |
49 | |
50 testing_interface_ = GetTestingInterface(); | |
51 | |
52 return | |
53 !!input_event_interface_ && | |
54 !!mouse_input_event_interface_ && | |
55 !!wheel_input_event_interface_ && | |
56 !!keyboard_input_event_interface_ && | |
57 !!testing_interface_; | |
58 } | |
59 | |
60 pp::InputEvent TestInputEvent::CreateMouseEvent() { | |
61 return pp::MouseInputEvent( | |
62 instance_, | |
63 PP_INPUTEVENT_TYPE_MOUSEDOWN, | |
64 100, // time_stamp | |
65 PP_INPUTEVENT_MODIFIER_SHIFTKEY, | |
66 PP_INPUTEVENT_MOUSEBUTTON_RIGHT, | |
67 GetCenter(view_rect_), | |
68 1, // click count | |
69 pp::Point()); // movement | |
70 } | |
71 | |
72 pp::InputEvent TestInputEvent::CreateWheelEvent() { | |
73 return pp::WheelInputEvent( | |
74 instance_, | |
75 100, // time_stamp | |
76 PP_INPUTEVENT_MODIFIER_SHIFTKEY, | |
77 pp::FloatPoint(1, 2), | |
78 pp::FloatPoint(3, 4), | |
79 PP_TRUE); // scroll_by_page | |
80 } | |
81 | |
82 pp::InputEvent TestInputEvent::CreateKeyEvent() { | |
83 return pp::KeyboardInputEvent( | |
84 instance_, | |
85 PP_INPUTEVENT_TYPE_KEYDOWN, | |
86 100, // time_stamp | |
87 0, // modifiers | |
88 0x20, // Windows virtual keycode for a space | |
89 pp::Var("")); | |
90 } | |
91 | |
92 pp::InputEvent TestInputEvent::CreateCharEvent() { | |
93 return pp::KeyboardInputEvent( | |
94 instance_, | |
95 PP_INPUTEVENT_TYPE_CHAR, | |
96 100, // time_stamp | |
97 0, // modifiers | |
98 0, // keycode, undefined in this case | |
99 pp::Var(" ")); | |
100 } | |
101 | |
102 bool TestInputEvent::SimulateInputEvent(const pp::InputEvent& input_event) { | |
103 expected_input_event_ = pp::InputEvent(input_event.pp_resource()); | |
104 received_expected_event_ = false; | |
105 testing_interface_->SimulateInputEvent(instance_->pp_instance(), | |
106 input_event.pp_resource()); | |
107 // HandleInputEvent() should have been called. | |
dmichael (off chromium)
2011/11/07 17:12:58
This only works if it's dispatched synchronously,
| |
108 return received_expected_event_; | |
109 } | |
110 | |
111 bool TestInputEvent::AreEquivalentEvents(PP_Resource first, | |
112 PP_Resource second) { | |
113 if (!input_event_interface_->IsInputEvent(first) || | |
114 !input_event_interface_->IsInputEvent(second)) { | |
115 return false; | |
116 } | |
117 | |
118 // Test fields common to all events. | |
119 if (input_event_interface_->GetType(first) != | |
120 input_event_interface_->GetType(second) && | |
121 input_event_interface_->GetTimeStamp(first) != | |
122 input_event_interface_->GetTimeStamp(second) && | |
123 input_event_interface_->GetModifiers(first) != | |
124 input_event_interface_->GetModifiers(second)) { | |
125 return false; | |
126 } | |
127 | |
128 // Test event type-specific fields. | |
129 switch (input_event_interface_->GetType(first)) { | |
130 case PP_INPUTEVENT_TYPE_MOUSEDOWN: | |
131 case PP_INPUTEVENT_TYPE_MOUSEUP: | |
132 case PP_INPUTEVENT_TYPE_MOUSEMOVE: | |
133 case PP_INPUTEVENT_TYPE_MOUSEENTER: | |
134 case PP_INPUTEVENT_TYPE_MOUSELEAVE: | |
135 return | |
136 mouse_input_event_interface_->GetButton(first) == | |
137 mouse_input_event_interface_->GetButton(second) && | |
138 pp::Point(mouse_input_event_interface_->GetPosition(first)) == | |
139 pp::Point(mouse_input_event_interface_->GetPosition(second)) && | |
140 mouse_input_event_interface_->GetClickCount(first) == | |
141 mouse_input_event_interface_->GetClickCount(second) && | |
142 pp::Point(mouse_input_event_interface_->GetMovement(first)) == | |
143 pp::Point(mouse_input_event_interface_->GetMovement(second)); | |
144 | |
145 case PP_INPUTEVENT_TYPE_WHEEL: | |
146 return | |
147 pp::FloatPoint(wheel_input_event_interface_->GetDelta(first)) == | |
148 pp::FloatPoint(wheel_input_event_interface_->GetDelta(second)) && | |
149 pp::FloatPoint(wheel_input_event_interface_->GetTicks(first)) == | |
150 pp::FloatPoint(wheel_input_event_interface_->GetTicks(second)) && | |
151 wheel_input_event_interface_->GetScrollByPage(first) == | |
152 wheel_input_event_interface_->GetScrollByPage(second); | |
153 | |
154 case PP_INPUTEVENT_TYPE_RAWKEYDOWN: | |
155 case PP_INPUTEVENT_TYPE_KEYDOWN: | |
156 case PP_INPUTEVENT_TYPE_KEYUP: | |
157 return | |
158 keyboard_input_event_interface_->GetKeyCode(first) == | |
159 keyboard_input_event_interface_->GetKeyCode(second); | |
160 | |
161 case PP_INPUTEVENT_TYPE_CHAR: | |
162 return | |
163 keyboard_input_event_interface_->GetKeyCode(first) == | |
164 keyboard_input_event_interface_->GetKeyCode(second) && | |
165 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.
| |
166 keyboard_input_event_interface_->GetCharacterText(first)) == | |
167 pp::Var(pp::Var::DontManage(), | |
168 keyboard_input_event_interface_->GetCharacterText(second)); | |
169 | |
170 default: | |
171 break; | |
172 } | |
173 | |
174 return false; | |
175 } | |
176 | |
177 bool TestInputEvent::HandleInputEvent(const pp::InputEvent& input_event) { | |
178 // Some events may cause extra events to be generated, so record the first | |
179 // event that matches. | |
180 if (!received_expected_event_) { | |
181 received_expected_event_ = AreEquivalentEvents( | |
182 input_event.pp_resource(), | |
183 expected_input_event_.pp_resource()); | |
184 } | |
185 // Handle all input events. | |
186 return true; | |
187 } | |
188 | |
189 void TestInputEvent::DidChangeView(const pp::Rect& position, | |
190 const pp::Rect& clip) { | |
191 view_rect_ = position; | |
192 } | |
193 | |
194 void TestInputEvent::RunTest() { | |
195 RUN_TEST(Events); | |
196 } | |
197 | |
198 std::string TestInputEvent::TestEvents() { | |
199 // Create some events. | |
200 pp::InputEvent mouse_input_event = CreateMouseEvent(); | |
201 pp::InputEvent wheel_input_event = CreateWheelEvent(); | |
202 pp::InputEvent key_input_event = CreateKeyEvent(); | |
203 pp::InputEvent char_input_event = CreateCharEvent(); | |
204 | |
205 // Check that they were created. | |
206 ASSERT_FALSE(mouse_input_event.is_null()); | |
207 ASSERT_FALSE(wheel_input_event.is_null()); | |
208 ASSERT_FALSE(key_input_event.is_null()); | |
209 ASSERT_FALSE(char_input_event.is_null()); | |
210 | |
211 // Request all input events. | |
212 input_event_interface_->RequestInputEvents(instance_->pp_instance(), | |
213 PP_INPUTEVENT_CLASS_MOUSE | | |
214 PP_INPUTEVENT_CLASS_WHEEL | | |
215 PP_INPUTEVENT_CLASS_KEYBOARD); | |
216 // Send the events and check that we received them. | |
217 ASSERT_TRUE(SimulateInputEvent(mouse_input_event)); | |
218 ASSERT_TRUE(SimulateInputEvent(wheel_input_event)); | |
219 ASSERT_TRUE(SimulateInputEvent(key_input_event)); | |
220 ASSERT_TRUE(SimulateInputEvent(char_input_event)); | |
221 | |
222 // Request only mouse events. | |
223 input_event_interface_->ClearInputEventRequest(instance_->pp_instance(), | |
224 PP_INPUTEVENT_CLASS_WHEEL | | |
225 PP_INPUTEVENT_CLASS_KEYBOARD); | |
226 // Check that we only receive mouse events. | |
227 ASSERT_TRUE(SimulateInputEvent(mouse_input_event)); | |
228 ASSERT_FALSE(SimulateInputEvent(wheel_input_event)); | |
229 ASSERT_FALSE(SimulateInputEvent(key_input_event)); | |
230 ASSERT_FALSE(SimulateInputEvent(char_input_event)); | |
231 | |
232 PASS(); | |
233 } | |
234 | |
OLD | NEW |