Unreviewed. Update W3C WebDriver imported tests.
[WebKit-https.git] / WebDriverTests / imported / w3c / webdriver / tests / actions / mouse.py
1 import pytest
2
3 from tests.support.inline import inline
4 from tests.actions.support.refine import get_events, filter_dict
5 from tests.support.wait import wait
6
7
8 def link_doc(dest):
9     content = "<a href=\"{}\" id=\"link\">destination</a>".format(dest)
10     return inline(content)
11
12
13 def get_center(rect):
14     return {
15         "x": rect["width"] / 2 + rect["x"],
16         "y": rect["height"] / 2 + rect["y"],
17     }
18
19
20 # TODO use pytest.approx once we upgrade to pytest > 3.0
21 def approx(n, m, tolerance=1):
22     return abs(n - m) <= tolerance
23
24
25 def test_click_at_coordinates(session, test_actions_page, mouse_chain):
26     div_point = {
27         "x": 82,
28         "y": 187,
29     }
30     mouse_chain \
31         .pointer_move(div_point["x"], div_point["y"], duration=1000) \
32         .click() \
33         .perform()
34     events = get_events(session)
35     assert len(events) == 4
36     for e in events:
37         if e["type"] != "mousemove":
38             assert e["pageX"] == div_point["x"]
39             assert e["pageY"] == div_point["y"]
40             assert e["target"] == "outer"
41         if e["type"] != "mousedown":
42             assert e["buttons"] == 0
43         assert e["button"] == 0
44     expected = [
45         {"type": "mousedown", "buttons": 1},
46         {"type": "mouseup",  "buttons": 0},
47         {"type": "click", "buttons": 0},
48     ]
49     filtered_events = [filter_dict(e, expected[0]) for e in events]
50     assert expected == filtered_events[1:]
51
52
53 def test_context_menu_at_coordinates(session, test_actions_page, mouse_chain):
54     div_point = {
55         "x": 82,
56         "y": 187,
57     }
58     mouse_chain \
59         .pointer_move(div_point["x"], div_point["y"]) \
60         .pointer_down(button=2) \
61         .pointer_up(button=2) \
62         .perform()
63     events = get_events(session)
64     expected = [
65         {"type": "mousedown", "button": 2},
66         {"type": "contextmenu",  "button": 2},
67     ]
68     assert len(events) == 4
69     filtered_events = [filter_dict(e, expected[0]) for e in events]
70     mousedown_contextmenu_events = [
71         x for x in filtered_events
72         if x["type"] in ["mousedown", "contextmenu"]
73     ]
74     assert expected == mousedown_contextmenu_events
75
76
77 def test_click_element_center(session, test_actions_page, mouse_chain):
78     outer = session.find.css("#outer", all=False)
79     center = get_center(outer.rect)
80     mouse_chain.click(element=outer).perform()
81     events = get_events(session)
82     assert len(events) == 4
83     event_types = [e["type"] for e in events]
84     assert ["mousemove", "mousedown", "mouseup", "click"] == event_types
85     for e in events:
86         if e["type"] != "mousemove":
87             assert approx(e["pageX"], center["x"])
88             assert approx(e["pageY"], center["y"])
89             assert e["target"] == "outer"
90
91
92 def test_click_navigation(session, url):
93     destination = url("/webdriver/tests/actions/support/test_actions_wdspec.html")
94     start = link_doc(destination)
95
96     def click(link):
97         mouse_chain = session.actions.sequence(
98             "pointer", "pointer_id", {"pointerType": "mouse"})
99         mouse_chain.click(element=link).perform()
100
101     session.url = start
102     error_message = "Did not navigate to %s" % destination
103
104     click(session.find.css("#link", all=False))
105     wait(session, lambda s: s.url == destination, error_message)
106     # repeat steps to check behaviour after document unload
107     session.url = start
108     click(session.find.css("#link", all=False))
109     wait(session, lambda s: s.url == destination, error_message)
110
111
112 @pytest.mark.parametrize("drag_duration", [0, 300, 800])
113 @pytest.mark.parametrize("dx, dy",
114     [(20, 0), (0, 15), (10, 15), (-20, 0), (10, -15), (-10, -15)])
115 def test_drag_and_drop(session, test_actions_page, mouse_chain, dx, dy, drag_duration):
116     drag_target = session.find.css("#dragTarget", all=False)
117     initial_rect = drag_target.rect
118     initial_center = get_center(initial_rect)
119     # Conclude chain with extra move to allow time for last queued
120     # coordinate-update of drag_target and to test that drag_target is "dropped".
121     mouse_chain \
122         .pointer_move(0, 0, origin=drag_target) \
123         .pointer_down() \
124         .pointer_move(dx, dy, duration=drag_duration, origin="pointer") \
125         .pointer_up() \
126         .pointer_move(80, 50, duration=100, origin="pointer") \
127         .perform()
128     # mouseup that ends the drag is at the expected destination
129     e = get_events(session)[1]
130     assert e["type"] == "mouseup"
131     assert approx(e["pageX"], initial_center["x"] + dx)
132     assert approx(e["pageY"], initial_center["y"] + dy)
133     # check resulting location of the dragged element
134     final_rect = drag_target.rect
135     assert initial_rect["x"] + dx == final_rect["x"]
136     assert initial_rect["y"] + dy == final_rect["y"]