Skip to content

Commit 93180d5

Browse files
committed
Add TouchScroll and TouchFlick command emulation
1 parent b46ac91 commit 93180d5

File tree

6 files changed

+289
-25
lines changed

6 files changed

+289
-25
lines changed

src/chrome/test/webdriver/commands/touch_commands.cc

Lines changed: 99 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,42 @@ TouchScrollCommand::~TouchScrollCommand() {}
208208
void TouchScrollCommand::ExecutePost(Response *const response)
209209
{
210210
Error* error;
211-
printf("Execute Touch Scroll Comman post\n");
211+
printf("Execute Scroll Comman post\n");
212+
213+
int x, y;
214+
if (!GetIntegerParameter("xoffset", &x) ||
215+
!GetIntegerParameter("yoffset", &y)) {
216+
response->SetError(new Error(
217+
kBadRequest,
218+
"Missing or invalid 'xoffset' or 'yoffset' parameters"));
219+
return;
220+
}
221+
222+
Point offset(x, y);
223+
224+
std::string element_name;
225+
bool has_element_ = GetStringParameter("element", &element_name);
226+
227+
if (has_element_) {
228+
ElementId element = ElementId(element_name);
229+
Point location;
230+
error = session_->GetElementLocationInView(element, &location);
231+
if (error) {
232+
response->SetError(error);
233+
return;
234+
}
235+
236+
error = session_->TouchScroll(element, offset);
237+
}
238+
else
239+
{
240+
error = session_->TouchScroll(offset);
241+
}
242+
243+
if (error) {
244+
response->SetError(error);
245+
return;
246+
}
212247

213248
}
214249

@@ -219,11 +254,44 @@ TouchLongClickCommand::TouchLongClickCommand(const std::vector<std::string>& pat
219254

220255
TouchLongClickCommand::~TouchLongClickCommand() {}
221256

257+
bool TouchLongClickCommand::Init(Response* const response) {
258+
if (!TouchCommand::Init(response))
259+
return false;
260+
261+
std::string element_name;
262+
has_element_ = GetStringParameter("element", &element_name);
263+
264+
if (has_element_) {
265+
element_ = ElementId(element_name);
266+
}
267+
268+
if (!has_element_ ) {
269+
response->SetError(new Error(
270+
kBadRequest, "Invalid command arguments"));
271+
}
272+
273+
return true;
274+
}
275+
222276
void TouchLongClickCommand::ExecutePost(Response *const response)
223277
{
224278
Error* error;
225279
printf("Execute Touch LongClick post\n");
226280

281+
Point location;
282+
283+
error = session_->GetElementLocationInView(element_, &location);
284+
if (error) {
285+
response->SetError(error);
286+
return;
287+
}
288+
289+
error = session_->TouchLongClick(location);
290+
if (error) {
291+
response->SetError(error);
292+
return;
293+
}
294+
227295
}
228296

229297
TouchFlickCommand::TouchFlickCommand(const std::vector<std::string>& path_segments,
@@ -238,6 +306,36 @@ void TouchFlickCommand::ExecutePost(Response *const response)
238306
Error* error;
239307
printf("Execute Touch Flick post\n");
240308

309+
int xSpeed, ySpeed;
310+
int xoffset, yoffset, speed;
311+
std::string element_name;
312+
if (GetIntegerParameter("xspeed", &xSpeed) &&
313+
GetIntegerParameter("yspeed", &ySpeed))
314+
{
315+
error = session_->TouchFlick(xSpeed, ySpeed);
316+
}
317+
else if (GetStringParameter("element", &element_name) && GetIntegerParameter("xoffset", &xoffset) &&
318+
GetIntegerParameter("yoffset", &yoffset) && GetIntegerParameter("speed", &speed))
319+
{
320+
Point location(xoffset, yoffset);
321+
ElementId element = ElementId(element_name);
322+
error = session_->TouchFlick(element, location, speed);
323+
}
324+
else
325+
{
326+
if (GetStringParameter("element", &element_name))
327+
printf("ElementName\n");
328+
response->SetError(new Error(
329+
kBadRequest,
330+
"Missing or invalid parameters"));
331+
return;
332+
}
333+
334+
if (error) {
335+
response->SetError(error);
336+
return;
337+
}
338+
241339
}
242340

243341

src/chrome/test/webdriver/commands/touch_commands.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,10 +112,13 @@ class TouchLongClickCommand : public TouchCommand {
112112
TouchLongClickCommand(const std::vector<std::string>& path_segments,
113113
const base::DictionaryValue* const parameters);
114114
virtual ~TouchLongClickCommand();
115-
115+
virtual bool Init(Response* const response) OVERRIDE;
116116
virtual void ExecutePost(Response* const response) OVERRIDE;
117117

118-
private:
118+
private:
119+
bool has_element_;
120+
ElementId element_;
121+
119122
DISALLOW_COPY_AND_ASSIGN(TouchLongClickCommand);
120123
};
121124

src/chrome/test/webdriver/webdriver_automation.cc

Lines changed: 83 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2130,16 +2130,8 @@ void Automation::TouchDown(const WebViewId &view_id, const Point &p, Error **err
21302130
points.append(touchPoint);
21312131

21322132
QTouchEvent *touchBeginEvent = new QTouchEvent(QEvent::TouchBegin, QTouchEvent::TouchScreen, Qt::NoModifier, Qt::TouchPointPressed, points);
2133-
QTouchEvent *touchPressEvent = new QTouchEvent(QEvent::TouchUpdate, QTouchEvent::TouchScreen, Qt::NoModifier, Qt::TouchPointPressed, points);
2134-
QTouchEvent *touchEndEvent = new QTouchEvent(QEvent::TouchEnd, QTouchEvent::TouchScreen, Qt::NoModifier, Qt::TouchPointPressed, points);
21352133

21362134
QApplication::postEvent(view, touchBeginEvent);
2137-
QApplication::postEvent(view, touchPressEvent);
2138-
QApplication::postEvent(view, touchEndEvent);
2139-
2140-
//additional we send mouse event for QWebView
2141-
QMouseEvent *pressEvent = new QMouseEvent(QEvent::MouseButtonPress, point, Qt::LeftButton, Qt::NoButton, Qt::NoModifier);
2142-
QApplication::postEvent(view, pressEvent);
21432135

21442136
}
21452137

@@ -2162,17 +2154,34 @@ void Automation::TouchUp(const WebViewId &view_id, const Point &p, Error **error
21622154
touchPoint.setPressure(1);
21632155
points.append(touchPoint);
21642156

2165-
QTouchEvent *touchBeginEvent = new QTouchEvent(QEvent::TouchBegin, QTouchEvent::TouchScreen, Qt::NoModifier, Qt::TouchPointReleased, points);
2166-
QTouchEvent *touchReleaseEvent = new QTouchEvent(QEvent::TouchUpdate, QTouchEvent::TouchScreen, Qt::NoModifier, Qt::TouchPointReleased, points);
21672157
QTouchEvent *touchEndEvent = new QTouchEvent(QEvent::TouchEnd, QTouchEvent::TouchScreen, Qt::NoModifier, Qt::TouchPointReleased, points);
21682158

2169-
QApplication::postEvent(view, touchBeginEvent);
2170-
QApplication::postEvent(view, touchReleaseEvent);
21712159
QApplication::postEvent(view, touchEndEvent);
21722160

2173-
//additional we send mouse event for QWebView
2174-
QMouseEvent *pressEvent = new QMouseEvent(QEvent::MouseButtonPress, point, Qt::LeftButton, Qt::NoButton, Qt::NoModifier);
2175-
QApplication::postEvent(view, pressEvent);
2161+
}
2162+
2163+
void Automation::TouchMove(const WebViewId &view_id, const Point &p, Error **error)
2164+
{
2165+
if(!checkView(view_id))
2166+
{
2167+
*error = new Error(kNoSuchWindow);
2168+
return;
2169+
}
2170+
2171+
QWidget *view = view_id.GetView();
2172+
2173+
QPoint point = ConvertPointToQPoint(p);
2174+
2175+
QList<QTouchEvent::TouchPoint> points;
2176+
QTouchEvent::TouchPoint touchPoint(1);
2177+
touchPoint.setPos(point);
2178+
touchPoint.setState(Qt::TouchPointMoved);
2179+
touchPoint.setPressure(1);
2180+
points.append(touchPoint);
2181+
2182+
QTouchEvent *touchMoveEvent = new QTouchEvent(QEvent::TouchUpdate, QTouchEvent::TouchScreen, Qt::NoModifier, Qt::TouchPointMoved, points);
2183+
2184+
QApplication::postEvent(view, touchMoveEvent);
21762185

21772186
}
21782187

@@ -2217,6 +2226,65 @@ void Automation::TouchLongClick(const WebViewId &view_id, const Point &p, Error
22172226

22182227
}
22192228

2229+
void Automation::TouchScroll(const WebViewId &view_id, const Point &offset, Error **error)
2230+
{
2231+
if(!checkView(view_id))
2232+
{
2233+
*error = new Error(kNoSuchWindow);
2234+
return;
2235+
}
2236+
2237+
QWidget *view = view_id.GetView();
2238+
2239+
QWebView *webView = qobject_cast<QWebView*>(view);
2240+
webView->page()->mainFrame()->scroll(offset.x(), offset.y());
2241+
}
2242+
2243+
void Automation::TouchScrollElement(const WebViewId &view_id, const ElementId &element, const Point &offset, Error **error)
2244+
{
2245+
element;
2246+
if(!checkView(view_id))
2247+
{
2248+
*error = new Error(kNoSuchWindow);
2249+
return;
2250+
}
2251+
2252+
QWidget *view = view_id.GetView();
2253+
2254+
QWebView *webView = qobject_cast<QWebView*>(view);
2255+
webView->page()->mainFrame()->scroll(-offset.x(), -offset.y());
2256+
}
2257+
2258+
void Automation::TouchFlick(const WebViewId &view_id, const int &xSpeed, const int &ySpeed, Error **error)
2259+
{
2260+
if(!checkView(view_id))
2261+
{
2262+
*error = new Error(kNoSuchWindow);
2263+
return;
2264+
}
2265+
2266+
QWidget *view = view_id.GetView();
2267+
2268+
QWebView *webView = qobject_cast<QWebView*>(view);
2269+
std::cout<<"Speed x= "<<xSpeed*3<<" y="<<ySpeed*3<<std::endl;
2270+
webView->page()->mainFrame()->scroll(xSpeed*3, ySpeed*3);
2271+
}
2272+
2273+
void Automation::TouchFlickElement(const WebViewId &view_id, const ElementId &element, const Point &offset, const int &speed, Error **error)
2274+
{
2275+
element;
2276+
if(!checkView(view_id))
2277+
{
2278+
*error = new Error(kNoSuchWindow);
2279+
return;
2280+
}
2281+
2282+
QWidget *view = view_id.GetView();
2283+
std::cout<<"Offset x= "<<-offset.x()*(speed+1)<<" y="<<-offset.y()*(speed+1)<<std::endl;
2284+
QWebView *webView = qobject_cast<QWebView*>(view);
2285+
webView->page()->mainFrame()->scroll(-offset.x()*(speed+1), -offset.y()*(speed+1));
2286+
}
2287+
22202288

22212289
void Automation::OverrideGeolocation(const DictionaryValue* geolocation,
22222290
Error** error)

src/chrome/test/webdriver/webdriver_automation.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,7 @@ class Automation : public QObject {
329329
void GetGeolocation(scoped_ptr<base::DictionaryValue>* geolocation,
330330
Error** error);
331331

332+
//touch commands emulation
332333
void TouchClick(const WebViewId &view_id, const Point &p, Error **error);
333334

334335
void TouchDoubleClick(const WebViewId &view_id, const Point &p, Error **error);
@@ -337,8 +338,18 @@ class Automation : public QObject {
337338

338339
void TouchUp(const WebViewId &view_id, const Point &p, Error **error);
339340

341+
void TouchMove(const WebViewId &view_id, const Point &p, Error **error);
342+
340343
void TouchLongClick(const WebViewId &view_id, const Point &p, Error **error);
341344

345+
void TouchScroll(const WebViewId &view_id, const Point &offset, Error **error);
346+
347+
void TouchScrollElement(const WebViewId &view_id, const ElementId &element, const Point &offset, Error **error);
348+
349+
void TouchFlick(const WebViewId &view_id, const int &xSpeed, const int &ySpeed, Error **error);
350+
351+
void TouchFlickElement(const WebViewId &view_id, const ElementId &element, const Point &offset, const int &speed, Error **error);
352+
342353
// Overrides the current geolocation.
343354
void OverrideGeolocation(const base::DictionaryValue* geolocation,
344355
Error** error);

0 commit comments

Comments
 (0)