|
1 | | -import copy |
2 | 1 | import pytest |
3 | 2 |
|
4 | | -from collections import defaultdict |
5 | | - |
6 | 3 | from webdriver.bidi.modules.input import Actions |
7 | | -from webdriver.bidi.modules.script import ContextTarget |
8 | 4 |
|
9 | | -from tests.support.helpers import filter_dict, filter_supported_key_events |
10 | | -from tests.support.keys import ALL_EVENTS, Keys, ALTERNATIVE_KEY_NAMES |
11 | | -from .. import get_events, get_keys_value |
| 5 | +from tests.support.keys import Keys |
| 6 | +from .. import get_keys_value |
12 | 7 |
|
13 | 8 | pytestmark = pytest.mark.asyncio |
14 | 9 |
|
@@ -50,263 +45,6 @@ async def test_key_codepoint( |
50 | 45 | assert keys_value == value |
51 | 46 |
|
52 | 47 |
|
53 | | -@pytest.mark.parametrize( |
54 | | - "key,event", |
55 | | - [ |
56 | | - (Keys.ESCAPE, "ESCAPE"), |
57 | | - (Keys.RIGHT, "RIGHT"), |
58 | | - ], |
59 | | -) |
60 | | -async def test_key_non_printable_key( |
61 | | - bidi_session, top_context, setup_key_test, key, event |
62 | | -): |
63 | | - code = ALL_EVENTS[event]["code"] |
64 | | - value = ALL_EVENTS[event]["key"] |
65 | | - |
66 | | - actions = Actions() |
67 | | - (actions.add_key().key_down(key).key_up(key)) |
68 | | - await bidi_session.input.perform_actions( |
69 | | - actions=actions, context=top_context["context"] |
70 | | - ) |
71 | | - all_events = await get_events(bidi_session, top_context["context"]) |
72 | | - |
73 | | - expected = [ |
74 | | - {"code": code, "key": value, "type": "keydown"}, |
75 | | - {"code": code, "key": value, "type": "keypress"}, |
76 | | - {"code": code, "key": value, "type": "keyup"}, |
77 | | - ] |
78 | | - |
79 | | - # Make a copy for alternate key property values |
80 | | - # Note: only keydown and keyup are affected by alternate key names |
81 | | - alt_expected = copy.deepcopy(expected) |
82 | | - if event in ALTERNATIVE_KEY_NAMES: |
83 | | - alt_expected[0]["key"] = ALTERNATIVE_KEY_NAMES[event] |
84 | | - alt_expected[2]["key"] = ALTERNATIVE_KEY_NAMES[event] |
85 | | - |
86 | | - (_, expected) = filter_supported_key_events(all_events, expected) |
87 | | - (events, alt_expected) = filter_supported_key_events(all_events, alt_expected) |
88 | | - if len(events) == 2: |
89 | | - # most browsers don't send a keypress for non-printable keys |
90 | | - assert events == [expected[0], expected[2]] or events == [ |
91 | | - alt_expected[0], |
92 | | - alt_expected[2], |
93 | | - ] |
94 | | - else: |
95 | | - assert events == expected or events == alt_expected |
96 | | - |
97 | | - keys_value = await get_keys_value(bidi_session, top_context["context"]) |
98 | | - assert len(keys_value) == 0 |
99 | | - |
100 | | - |
101 | | -@pytest.mark.parametrize( |
102 | | - "key, event", |
103 | | - [ |
104 | | - (Keys.ALT, "ALT"), |
105 | | - (Keys.CONTROL, "CONTROL"), |
106 | | - (Keys.META, "META"), |
107 | | - (Keys.SHIFT, "SHIFT"), |
108 | | - (Keys.R_ALT, "R_ALT"), |
109 | | - (Keys.R_CONTROL, "R_CONTROL"), |
110 | | - (Keys.R_META, "R_META"), |
111 | | - (Keys.R_SHIFT, "R_SHIFT"), |
112 | | - ], |
113 | | -) |
114 | | -async def test_key_modifier_key( |
115 | | - bidi_session, top_context, setup_key_test, key, event |
116 | | -): |
117 | | - code = ALL_EVENTS[event]["code"] |
118 | | - value = ALL_EVENTS[event]["key"] |
119 | | - |
120 | | - actions = Actions() |
121 | | - (actions.add_key().key_down(key).key_up(key)) |
122 | | - await bidi_session.input.perform_actions( |
123 | | - actions=actions, context=top_context["context"] |
124 | | - ) |
125 | | - all_events = await get_events(bidi_session, top_context["context"]) |
126 | | - |
127 | | - expected = [ |
128 | | - {"code": code, "key": value, "type": "keydown"}, |
129 | | - {"code": code, "key": value, "type": "keyup"}, |
130 | | - ] |
131 | | - |
132 | | - (events, expected) = filter_supported_key_events(all_events, expected) |
133 | | - assert events == expected |
134 | | - |
135 | | - keys_value = await get_keys_value(bidi_session, top_context["context"]) |
136 | | - assert len(keys_value) == 0 |
137 | | - |
138 | | - |
139 | | -@pytest.mark.parametrize( |
140 | | - "value,code", |
141 | | - [ |
142 | | - ("a", "KeyA"), |
143 | | - ("a", "KeyA"), |
144 | | - ('"', "Quote"), |
145 | | - (",", "Comma"), |
146 | | - ("\u00E0", ""), |
147 | | - ("\u0416", ""), |
148 | | - ("@", "Digit2"), |
149 | | - ("\u2603", ""), |
150 | | - ("\uF6C2", ""), # PUA |
151 | | - ], |
152 | | -) |
153 | | -async def test_key_printable_key( |
154 | | - bidi_session, |
155 | | - top_context, |
156 | | - setup_key_test, |
157 | | - value, |
158 | | - code, |
159 | | -): |
160 | | - actions = Actions() |
161 | | - (actions.add_key().key_down(value).key_up(value)) |
162 | | - await bidi_session.input.perform_actions( |
163 | | - actions=actions, context=top_context["context"] |
164 | | - ) |
165 | | - |
166 | | - all_events = await get_events(bidi_session, top_context["context"]) |
167 | | - |
168 | | - expected = [ |
169 | | - {"code": code, "key": value, "type": "keydown"}, |
170 | | - {"code": code, "key": value, "type": "keypress"}, |
171 | | - {"code": code, "key": value, "type": "keyup"}, |
172 | | - ] |
173 | | - |
174 | | - (events, expected) = filter_supported_key_events(all_events, expected) |
175 | | - assert events == expected |
176 | | - |
177 | | - keys_value = await get_keys_value(bidi_session, top_context["context"]) |
178 | | - assert keys_value == value |
179 | | - |
180 | | - |
181 | | -@pytest.mark.parametrize("use_keyup", [True, False]) |
182 | | -async def test_key_printable_sequence( |
183 | | - bidi_session, top_context, setup_key_test, use_keyup |
184 | | -): |
185 | | - actions = Actions() |
186 | | - key_source = actions.add_key() |
187 | | - if use_keyup: |
188 | | - actions.add_key().send_keys("ab") |
189 | | - else: |
190 | | - actions.add_key().key_down("a").key_down("b") |
191 | | - |
192 | | - await bidi_session.input.perform_actions( |
193 | | - actions=actions, context=top_context["context"] |
194 | | - ) |
195 | | - all_events = await get_events(bidi_session, top_context["context"]) |
196 | | - |
197 | | - expected = [ |
198 | | - {"code": "KeyA", "key": "a", "type": "keydown"}, |
199 | | - {"code": "KeyA", "key": "a", "type": "keypress"}, |
200 | | - {"code": "KeyA", "key": "a", "type": "keyup"}, |
201 | | - {"code": "KeyB", "key": "b", "type": "keydown"}, |
202 | | - {"code": "KeyB", "key": "b", "type": "keypress"}, |
203 | | - {"code": "KeyB", "key": "b", "type": "keyup"}, |
204 | | - ] |
205 | | - expected = [e for e in expected if use_keyup or e["type"] is not "keyup"] |
206 | | - |
207 | | - (events, expected) = filter_supported_key_events(all_events, expected) |
208 | | - assert events == expected |
209 | | - |
210 | | - keys_value = await get_keys_value(bidi_session, top_context["context"]) |
211 | | - assert keys_value == "ab" |
212 | | - |
213 | | - |
214 | | -@pytest.mark.parametrize("name,expected", ALL_EVENTS.items()) |
215 | | -async def test_key_special_key_sends_keydown( |
216 | | - bidi_session, |
217 | | - top_context, |
218 | | - setup_key_test, |
219 | | - name, |
220 | | - expected, |
221 | | -): |
222 | | - if name.startswith("F"): |
223 | | - # Prevent default behavior for F1, etc., but only after keydown |
224 | | - # bubbles up to body. (Otherwise activated browser menus/functions |
225 | | - # may interfere with subsequent tests.) |
226 | | - await bidi_session.script.evaluate( |
227 | | - expression=""" |
228 | | - document.body.addEventListener("keydown", |
229 | | - function(e) { e.preventDefault() }); |
230 | | - """, |
231 | | - target=ContextTarget(top_context["context"]), |
232 | | - await_promise=False, |
233 | | - ) |
234 | | - |
235 | | - actions = Actions() |
236 | | - (actions.add_key().key_down(getattr(Keys, name))) |
237 | | - await bidi_session.input.perform_actions( |
238 | | - actions=actions, context=top_context["context"] |
239 | | - ) |
240 | | - |
241 | | - # only interested in keydown |
242 | | - all_events = await get_events(bidi_session, top_context["context"]) |
243 | | - first_event = all_events[0] |
244 | | - # make a copy so we can throw out irrelevant keys and compare to events |
245 | | - expected = dict(expected) |
246 | | - |
247 | | - del expected["value"] |
248 | | - |
249 | | - # make another copy for alternative key names |
250 | | - alt_expected = copy.deepcopy(expected) |
251 | | - if name in ALTERNATIVE_KEY_NAMES: |
252 | | - alt_expected["key"] = ALTERNATIVE_KEY_NAMES[name] |
253 | | - |
254 | | - # check and remove keys that aren't in expected |
255 | | - assert first_event["type"] == "keydown" |
256 | | - assert first_event["repeat"] is False |
257 | | - first_event = filter_dict(first_event, expected) |
258 | | - if first_event["code"] is None: |
259 | | - del first_event["code"] |
260 | | - del expected["code"] |
261 | | - del alt_expected["code"] |
262 | | - assert first_event == expected or first_event == alt_expected |
263 | | - # only printable characters should be recorded in input field |
264 | | - keys_value = await get_keys_value(bidi_session, top_context["context"]) |
265 | | - if len(expected["key"]) == 1: |
266 | | - assert keys_value == expected["key"] |
267 | | - else: |
268 | | - assert len(keys_value) == 0 |
269 | | - |
270 | | - |
271 | | -async def test_key_space(bidi_session, top_context, setup_key_test): |
272 | | - actions = Actions() |
273 | | - ( |
274 | | - actions.add_key() |
275 | | - .key_down(Keys.SPACE) |
276 | | - .key_up(Keys.SPACE) |
277 | | - .key_down(" ") |
278 | | - .key_up(" ") |
279 | | - ) |
280 | | - |
281 | | - await bidi_session.input.perform_actions( |
282 | | - actions=actions, context=top_context["context"] |
283 | | - ) |
284 | | - all_events = await get_events(bidi_session, top_context["context"]) |
285 | | - |
286 | | - by_type = defaultdict(list) |
287 | | - for event in all_events: |
288 | | - by_type[event["type"]].append(event) |
289 | | - |
290 | | - for event_type in by_type: |
291 | | - events = by_type[event_type] |
292 | | - assert len(events) == 2 |
293 | | - assert events[0] == events[1] |
294 | | - |
295 | | - |
296 | | -async def test_keyup_only_sends_no_events(bidi_session, top_context, setup_key_test): |
297 | | - actions = Actions() |
298 | | - actions.add_key().key_up("a") |
299 | | - await bidi_session.input.perform_actions( |
300 | | - actions=actions, context=top_context["context"] |
301 | | - ) |
302 | | - |
303 | | - events = await get_events(bidi_session, top_context["context"]) |
304 | | - assert len(events) == 0 |
305 | | - |
306 | | - keys_value = await get_keys_value(bidi_session, top_context["context"]) |
307 | | - assert len(keys_value) == 0 |
308 | | - |
309 | | - |
310 | 48 | async def test_null_response_value(bidi_session, top_context): |
311 | 49 | actions = Actions() |
312 | 50 | actions.add_key().key_down("a").key_up("a") |
|
0 commit comments