3e83b15b35317f589d3aa74d7f5106da67ea17da
[WebKit-https.git] / WebDriverTests / imported / w3c / webdriver / tests / actions / sequence.py
1 # META: timeout=long
2
3 import pytest
4
5 from tests.actions.support.refine import get_keys, filter_dict, get_events
6 from tests.actions.support.keys import Keys
7
8
9 def test_no_actions_send_no_events(session, key_reporter, key_chain):
10     key_chain.perform()
11     assert len(get_keys(key_reporter)) == 0
12     assert len(get_events(session)) == 0
13
14
15 def test_release_char_sequence_sends_keyup_events_in_reverse(session,
16                                                              key_reporter,
17                                                              key_chain):
18     key_chain \
19         .key_down("a") \
20         .key_down("b") \
21         .perform()
22     # reset so we only see the release events
23     session.execute_script("resetEvents();")
24     session.actions.release()
25     expected = [
26         {"code": "KeyB", "key": "b", "type": "keyup"},
27         {"code": "KeyA", "key": "a", "type": "keyup"},
28     ]
29     events = [filter_dict(e, expected[0]) for e in get_events(session)]
30     assert events == expected
31
32
33 def test_release_no_actions_sends_no_events(session, key_reporter):
34     session.actions.release()
35     assert len(get_keys(key_reporter)) == 0
36     assert len(get_events(session)) == 0
37
38
39 @pytest.mark.parametrize("modifier, prop", [
40     (Keys.CONTROL, "ctrlKey"),
41     (Keys.ALT, "altKey"),
42     (Keys.META, "metaKey"),
43     (Keys.SHIFT, "shiftKey"),
44     (Keys.R_CONTROL, "ctrlKey"),
45     (Keys.R_ALT, "altKey"),
46     (Keys.R_META, "metaKey"),
47     (Keys.R_SHIFT, "shiftKey"),
48 ])
49 def test_control_click(session,
50                        test_actions_page,
51                        key_chain,
52                        mouse_chain,
53                        modifier,
54                        prop):
55     key_chain \
56         .pause(0) \
57         .key_down(modifier) \
58         .pause(200) \
59         .key_up(modifier)
60     outer = session.find.css("#outer", all=False)
61     mouse_chain.click(element=outer)
62     session.actions.perform([key_chain.dict, mouse_chain.dict])
63     expected = [
64         {"type": "mousemove"},
65         {"type": "mousedown"},
66         {"type": "mouseup"},
67         {"type": "click"},
68     ]
69     defaults = {
70         "altKey": False,
71         "metaKey": False,
72         "shiftKey": False,
73         "ctrlKey": False
74     }
75     for e in expected:
76         e.update(defaults)
77         if e["type"] != "mousemove":
78             e[prop] = True
79     filtered_events = [filter_dict(e, expected[0]) for e in get_events(session)]
80     assert expected == filtered_events
81
82
83 def test_release_control_click(session, key_reporter, key_chain, mouse_chain):
84     key_chain \
85         .pause(0) \
86         .key_down(Keys.CONTROL)
87     mouse_chain \
88         .pointer_move(0, 0, origin=key_reporter) \
89         .pointer_down()
90     session.actions.perform([key_chain.dict, mouse_chain.dict])
91     session.execute_script("""
92         var keyReporter = document.getElementById("keys");
93         ["mousedown", "mouseup"].forEach((e) => {
94             keyReporter.addEventListener(e, recordPointerEvent);
95           });
96         resetEvents();
97     """)
98     session.actions.release()
99     expected = [
100         {"type": "mouseup"},
101         {"type": "keyup"},
102     ]
103     events = [filter_dict(e, expected[0]) for e in get_events(session)]
104     assert events == expected
105
106
107 def test_many_modifiers_click(session, test_actions_page, key_chain, mouse_chain):
108     outer = session.find.css("#outer", all=False)
109     key_chain \
110         .pause(0) \
111         .key_down(Keys.CONTROL) \
112         .key_down(Keys.SHIFT) \
113         .pause(0) \
114         .key_up(Keys.CONTROL) \
115         .key_up(Keys.SHIFT)
116     mouse_chain \
117         .pointer_move(0, 0, origin=outer) \
118         .pause(0) \
119         .pointer_down() \
120         .pointer_up() \
121         .pause(0) \
122         .pause(0) \
123         .pointer_down()
124     session.actions.perform([key_chain.dict, mouse_chain.dict])
125     expected = [
126         {"type": "mousemove"},
127         # shift and ctrl presses
128         {"type": "mousedown"},
129         {"type": "mouseup"},
130         {"type": "click"},
131         # no modifiers pressed
132         {"type": "mousedown"},
133     ]
134     defaults = {
135         "altKey": False,
136         "metaKey": False,
137         "shiftKey": False,
138         "ctrlKey": False
139     }
140     for e in expected:
141         e.update(defaults)
142     for e in expected[1:4]:
143         e["shiftKey"] = True
144         e["ctrlKey"] = True
145     events = [filter_dict(e, expected[0]) for e in get_events(session)]
146     assert events == expected