1<?php
2
3namespace Facebook\WebDriver\Remote;
4
5use Facebook\WebDriver\Interactions\Touch\WebDriverTouchScreen;
6use Facebook\WebDriver\WebDriverElement;
7
8/**
9 * Execute touch commands for RemoteWebDriver.
10 */
11class RemoteTouchScreen implements WebDriverTouchScreen
12{
13    /**
14     * @var RemoteExecuteMethod
15     */
16    private $executor;
17
18    /**
19     * @param RemoteExecuteMethod $executor
20     */
21    public function __construct(RemoteExecuteMethod $executor)
22    {
23        $this->executor = $executor;
24    }
25
26    /**
27     * @param WebDriverElement $element
28     *
29     * @return RemoteTouchScreen The instance.
30     */
31    public function tap(WebDriverElement $element)
32    {
33        $this->executor->execute(
34            DriverCommand::TOUCH_SINGLE_TAP,
35            ['element' => $element->getID()]
36        );
37
38        return $this;
39    }
40
41    /**
42     * @param WebDriverElement $element
43     *
44     * @return RemoteTouchScreen The instance.
45     */
46    public function doubleTap(WebDriverElement $element)
47    {
48        $this->executor->execute(
49            DriverCommand::TOUCH_DOUBLE_TAP,
50            ['element' => $element->getID()]
51        );
52
53        return $this;
54    }
55
56    /**
57     * @param int $x
58     * @param int $y
59     *
60     * @return RemoteTouchScreen The instance.
61     */
62    public function down($x, $y)
63    {
64        $this->executor->execute(DriverCommand::TOUCH_DOWN, [
65            'x' => $x,
66            'y' => $y,
67        ]);
68
69        return $this;
70    }
71
72    /**
73     * @param int $xspeed
74     * @param int $yspeed
75     *
76     * @return RemoteTouchScreen The instance.
77     */
78    public function flick($xspeed, $yspeed)
79    {
80        $this->executor->execute(DriverCommand::TOUCH_FLICK, [
81            'xspeed' => $xspeed,
82            'yspeed' => $yspeed,
83        ]);
84
85        return $this;
86    }
87
88    /**
89     * @param WebDriverElement $element
90     * @param int $xoffset
91     * @param int $yoffset
92     * @param int $speed
93     *
94     * @return RemoteTouchScreen The instance.
95     */
96    public function flickFromElement(WebDriverElement $element, $xoffset, $yoffset, $speed)
97    {
98        $this->executor->execute(DriverCommand::TOUCH_FLICK, [
99            'xoffset' => $xoffset,
100            'yoffset' => $yoffset,
101            'element' => $element->getID(),
102            'speed' => $speed,
103        ]);
104
105        return $this;
106    }
107
108    /**
109     * @param WebDriverElement $element
110     *
111     * @return RemoteTouchScreen The instance.
112     */
113    public function longPress(WebDriverElement $element)
114    {
115        $this->executor->execute(
116            DriverCommand::TOUCH_LONG_PRESS,
117            ['element' => $element->getID()]
118        );
119
120        return $this;
121    }
122
123    /**
124     * @param int $x
125     * @param int $y
126     *
127     * @return RemoteTouchScreen The instance.
128     */
129    public function move($x, $y)
130    {
131        $this->executor->execute(DriverCommand::TOUCH_MOVE, [
132            'x' => $x,
133            'y' => $y,
134        ]);
135
136        return $this;
137    }
138
139    /**
140     * @param int $xoffset
141     * @param int $yoffset
142     *
143     * @return RemoteTouchScreen The instance.
144     */
145    public function scroll($xoffset, $yoffset)
146    {
147        $this->executor->execute(DriverCommand::TOUCH_SCROLL, [
148            'xoffset' => $xoffset,
149            'yoffset' => $yoffset,
150        ]);
151
152        return $this;
153    }
154
155    /**
156     * @param WebDriverElement $element
157     * @param int $xoffset
158     * @param int $yoffset
159     *
160     * @return RemoteTouchScreen The instance.
161     */
162    public function scrollFromElement(WebDriverElement $element, $xoffset, $yoffset)
163    {
164        $this->executor->execute(DriverCommand::TOUCH_SCROLL, [
165            'element' => $element->getID(),
166            'xoffset' => $xoffset,
167            'yoffset' => $yoffset,
168        ]);
169
170        return $this;
171    }
172
173    /**
174     * @param int $x
175     * @param int $y
176     *
177     * @return RemoteTouchScreen The instance.
178     */
179    public function up($x, $y)
180    {
181        $this->executor->execute(DriverCommand::TOUCH_UP, [
182            'x' => $x,
183            'y' => $y,
184        ]);
185
186        return $this;
187    }
188}
189