43887ed432aa800659356558522685d3eb5fc4f4
[WebKit-https.git] / WebDriverTests / imported / w3c / webdriver / tests / actions / key.py
1 import pytest
2
3 from tests.actions.support.keys import Keys
4 from tests.actions.support.refine import filter_dict, get_keys, get_events
5
6
7 def test_lone_keyup_sends_no_events(session, key_reporter, key_chain):
8     key_chain.key_up("a").perform()
9     assert len(get_keys(key_reporter)) == 0
10     assert len(get_events(session)) == 0
11     session.actions.release()
12     assert len(get_keys(key_reporter)) == 0
13     assert len(get_events(session)) == 0
14
15
16 @pytest.mark.parametrize("value,code", [
17     (u"a", "KeyA",),
18     ("a", "KeyA",),
19     (u"\"", "Quote"),
20     (u",", "Comma"),
21     (u"\u00E0", ""),
22     (u"\u0416", ""),
23     (u"@", "Digit2"),
24     (u"\u2603", ""),
25     (u"\uF6C2", ""),  # PUA
26 ])
27 def test_single_printable_key_sends_correct_events(session,
28                                                    key_reporter,
29                                                    key_chain,
30                                                    value,
31                                                    code):
32     key_chain \
33         .key_down(value) \
34         .key_up(value) \
35         .perform()
36     expected = [
37         {"code": code, "key": value, "type": "keydown"},
38         {"code": code, "key": value, "type": "keypress"},
39         {"code": code, "key": value, "type": "keyup"},
40     ]
41     events = [filter_dict(e, expected[0]) for e in get_events(session)]
42     assert events == expected
43     assert get_keys(key_reporter) == value
44
45
46 @pytest.mark.parametrize("value", [
47     (u"\U0001F604"),
48     (u"\U0001F60D"),
49 ])
50 def test_single_emoji_records_correct_key(session, key_reporter, key_chain, value):
51     # Not using key_chain.send_keys() because we always want to treat value as
52     # one character here. `len(value)` varies by platform for non-BMP characters,
53     # so we don't want to iterate over value.
54     key_chain \
55         .key_down(value) \
56         .key_up(value) \
57         .perform()
58     # events sent by major browsers are inconsistent so only check key value
59     assert get_keys(key_reporter) == value
60
61
62 @pytest.mark.parametrize("value,code,key", [
63     (u"\uE050", "ShiftRight", "Shift"),
64     (u"\uE053", "OSRight", "Meta"),
65     (Keys.CONTROL, "ControlLeft", "Control"),
66 ])
67 def test_single_modifier_key_sends_correct_events(session,
68                                                   key_reporter,
69                                                   key_chain,
70                                                   value,
71                                                   code,
72                                                   key):
73     key_chain \
74         .key_down(value) \
75         .key_up(value) \
76         .perform()
77     all_events = get_events(session)
78     expected = [
79         {"code": code, "key": key, "type": "keydown"},
80         {"code": code, "key": key, "type": "keyup"},
81     ]
82     events = [filter_dict(e, expected[0]) for e in all_events]
83     assert events == expected
84     assert len(get_keys(key_reporter)) == 0
85
86
87 @pytest.mark.parametrize("value,code,key", [
88     (Keys.ESCAPE, "Escape", "Escape"),
89     (Keys.RIGHT, "ArrowRight", "ArrowRight"),
90 ])
91 def test_single_nonprintable_key_sends_events(session,
92                                               key_reporter,
93                                               key_chain,
94                                               value,
95                                               code,
96                                               key):
97     key_chain \
98         .key_down(value) \
99         .key_up(value) \
100         .perform()
101     expected = [
102         {"code": code, "key": key, "type": "keydown"},
103         {"code": code, "key": key, "type": "keypress"},
104         {"code": code, "key": key, "type": "keyup"},
105     ]
106     events = [filter_dict(e, expected[0]) for e in get_events(session)]
107     if len(events) == 2:
108         # most browsers don't send a keypress for non-printable keys
109         assert events == [expected[0], expected[2]]
110     else:
111         assert events == expected
112     assert len(get_keys(key_reporter)) == 0
113
114
115 def test_sequence_of_keydown_printable_keys_sends_events(session,
116                                                          key_reporter,
117                                                          key_chain):
118     key_chain \
119         .key_down("a") \
120         .key_down("b") \
121         .perform()
122     expected = [
123         {"code": "KeyA", "key": "a", "type": "keydown"},
124         {"code": "KeyA", "key": "a", "type": "keypress"},
125         {"code": "KeyB", "key": "b", "type": "keydown"},
126         {"code": "KeyB", "key": "b", "type": "keypress"},
127     ]
128     events = [filter_dict(e, expected[0]) for e in get_events(session)]
129     assert events == expected
130     assert get_keys(key_reporter) == "ab"
131
132
133 def test_sequence_of_keydown_character_keys(session, key_reporter, key_chain):
134     key_chain.send_keys("ef").perform()
135     expected = [
136         {"code": "KeyE", "key": "e", "type": "keydown"},
137         {"code": "KeyE", "key": "e", "type": "keypress"},
138         {"code": "KeyE", "key": "e", "type": "keyup"},
139         {"code": "KeyF", "key": "f", "type": "keydown"},
140         {"code": "KeyF", "key": "f", "type": "keypress"},
141         {"code": "KeyF", "key": "f", "type": "keyup"},
142     ]
143     events = [filter_dict(e, expected[0]) for e in get_events(session)]
144     assert events == expected
145     assert get_keys(key_reporter) == "ef"
146
147
148 def test_backspace_erases_keys(session, key_reporter, key_chain):
149     key_chain \
150         .send_keys("efcd") \
151         .send_keys([Keys.BACKSPACE, Keys.BACKSPACE]) \
152         .perform()
153     assert get_keys(key_reporter) == "ef"
154