Commit 2d9d777a authored by Lan Wei's avatar Lan Wei Committed by Commit Bot

[ChromeDriver] More refactor of ExecutePerformActions function

In ExecutePerformActions function, we dispatch key events first, mouse
events then touch events, some code are redundant with different pointer
types. I cleaned some code shared with different pointer type.

Bug: 606367
Change-Id: I27e7e538db8026b87938abd19ae71f59e1c6844a
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1762769Reviewed-by: default avatarJohn Chen <johnchen@chromium.org>
Commit-Queue: Lan Wei <lanwei@chromium.org>
Cr-Commit-Position: refs/heads/master@{#689160}
parent d6b13668
...@@ -1288,25 +1288,42 @@ Status ExecutePerformActions(Session* session, ...@@ -1288,25 +1288,42 @@ Status ExecutePerformActions(Session* session,
if (!session->input_state_table.GetDictionary(id, &input_state)) if (!session->input_state_table.GetDictionary(id, &input_state))
return Status(kUnknownError, "missing input state"); return Status(kUnknownError, "missing input state");
action->GetString("type", &type);
if (i == 0) { if (i == 0) {
if (pointer_id_set.find(id) != pointer_id_set.end()) if (pointer_id_set.find(id) != pointer_id_set.end())
return Status(kInvalidArgument, "'id' already exists"); return Status(kInvalidArgument, "'id' already exists");
pointer_id_set.insert(id); pointer_id_set.insert(id);
action_input_states.push_back(input_state); action_input_states.push_back(input_state);
if (type == "pointer") {
Status status = WindowViewportSize(
session, web_view, &viewport_width, &viewport_height);
if (status.IsError())
return status;
input_state->GetInteger("x", &init_x);
input_state->GetInteger("y", &init_y);
action_locations.insert(
std::make_pair(id, gfx::Point(init_x, init_y)));
std::string pointer_type;
action->GetString("pointerType", &pointer_type);
if (pointer_type == "mouse" || pointer_type == "pen")
buttons[id] = input_state->FindKey("pressed")->GetInt();
else if (pointer_type == "touch")
has_touch_start[id] = false;
}
} }
action->GetString("type", &type);
action->GetString("subtype", &action_type); action->GetString("subtype", &action_type);
int duration = 0; int duration = 0;
if (action_type == "pause") { if (action_type == "pause") {
GetOptionalInt(action, "duration", &duration); GetOptionalInt(action, "duration", &duration);
tick_duration = std::max(tick_duration, duration); tick_duration = std::max(tick_duration, duration);
} } else {
if (type == "key") { if (type == "key") {
std::list<KeyEvent> dispatch_key_events; std::list<KeyEvent> dispatch_key_events;
KeyEventBuilder builder; KeyEventBuilder builder;
if (action_type != "pause") {
Status status = ConvertKeyActionToKeyEvent(action, input_state, Status status = ConvertKeyActionToKeyEvent(action, input_state,
action_type == "keyDown", action_type == "keyDown",
&dispatch_key_events); &dispatch_key_events);
...@@ -1327,33 +1344,10 @@ Status ExecutePerformActions(Session* session, ...@@ -1327,33 +1344,10 @@ Status ExecutePerformActions(Session* session,
if (status.IsError()) if (status.IsError())
return status; return status;
} }
}
} else if (type == "pointer") { } else if (type == "pointer") {
std::string pointer_type; std::string pointer_type;
action->GetString("pointerType", &pointer_type); action->GetString("pointerType", &pointer_type);
if (i == 0) { double x = 0, y = 0;
Status status = WindowViewportSize(
session, web_view, &viewport_width, &viewport_height);
if (status.IsError())
return status;
input_state->GetInteger("x", &init_x);
input_state->GetInteger("y", &init_y);
action_locations.insert(
std::make_pair(id, gfx::Point(init_x, init_y)));
if (pointer_type == "mouse" || pointer_type == "pen")
buttons[id] = input_state->FindKey("pressed")->GetInt();
else if (pointer_type == "touch")
has_touch_start[id] = false;
}
if (action_type != "pause") {
std::list<MouseEvent> dispatch_mouse_events;
std::list<TouchEvent> dispatch_touch_events;
double x = 0;
double y = 0;
OriginType origin = kViewPort; OriginType origin = kViewPort;
std::string element_id = ""; std::string element_id = "";
if (action_type == "pointerMove") { if (action_type == "pointerMove") {
...@@ -1364,13 +1358,29 @@ Status ExecutePerformActions(Session* session, ...@@ -1364,13 +1358,29 @@ Status ExecutePerformActions(Session* session,
if (action->GetDictionary("origin", &origin_dict)) { if (action->GetDictionary("origin", &origin_dict)) {
origin = kElement; origin = kElement;
origin_dict->GetString(GetElementKey(), &element_id); origin_dict->GetString(GetElementKey(), &element_id);
if (!element_id.empty()) {
int center_x = 0, center_y = 0;
Status status = ElementInViewCenter(
session, web_view, element_id, &center_x, &center_y);
if (status.IsError())
return status;
x += center_x;
y += center_y;
}
} else { } else {
std::string origin_str; std::string origin_str;
action->GetString("origin", &origin_str); action->GetString("origin", &origin_str);
if (origin_str == "pointer") if (origin_str == "pointer") {
origin = kPointer; origin = kPointer;
x += action_locations[id].x();
y += action_locations[id].y();
}
} }
} }
if (x < 0 || x > viewport_width || y < 0 || y > viewport_height)
return Status(kMoveTargetOutOfBounds);
action_locations[id] = gfx::Point(x, y);
duration = 0; duration = 0;
GetOptionalInt(action, "duration", &duration); GetOptionalInt(action, "duration", &duration);
...@@ -1378,6 +1388,7 @@ Status ExecutePerformActions(Session* session, ...@@ -1378,6 +1388,7 @@ Status ExecutePerformActions(Session* session,
} }
if (pointer_type == "mouse" || pointer_type == "pen") { if (pointer_type == "mouse" || pointer_type == "pen") {
std::list<MouseEvent> dispatch_mouse_events;
int click_count = 0; int click_count = 0;
if (action_type == "pointerDown" || action_type == "pointerUp") { if (action_type == "pointerDown" || action_type == "pointerUp") {
std::string button; std::string button;
...@@ -1388,36 +1399,11 @@ Status ExecutePerformActions(Session* session, ...@@ -1388,36 +1399,11 @@ Status ExecutePerformActions(Session* session,
button_type[id].clear(); button_type[id].clear();
} }
MouseEvent event(StringToMouseEventType(action_type), MouseEvent event(StringToMouseEventType(action_type),
StringToMouseButton(button_type[id]), x, y, 0, StringToMouseButton(button_type[id]),
buttons[id], click_count); action_locations[id].x(),
action_locations[id].y(), 0, buttons[id],
click_count);
event.pointer_type = StringToPointerType(pointer_type); event.pointer_type = StringToPointerType(pointer_type);
if (action_type == "pointerDown")
buttons[id] |= StringToModifierMouseButton(button_type[id]);
else if (action_type == "pointerUp")
buttons[id] &= ~StringToModifierMouseButton(button_type[id]);
if (event.type == kMovedMouseEventType) {
if (origin == kPointer) {
event.x += action_locations[id].x();
event.y += action_locations[id].y();
} else if (!element_id.empty()) {
int center_x = 0, center_y = 0;
Status status = ElementInViewCenter(
session, web_view, element_id, &center_x, &center_y);
if (status.IsError())
return status;
event.x += center_x;
event.y += center_y;
}
if (event.x < 0 || event.x > viewport_width || event.y < 0 ||
event.y > viewport_height) {
return Status(kMoveTargetOutOfBounds);
}
action_locations[id] = gfx::Point(event.x, event.y);
} else {
event.x = action_locations[id].x();
event.y = action_locations[id].y();
}
event.modifiers = session->sticky_modifiers; event.modifiers = session->sticky_modifiers;
if (event.type == kPressedMouseEventType) { if (event.type == kPressedMouseEventType) {
base::TimeTicks timestamp = base::TimeTicks::Now(); base::TimeTicks timestamp = base::TimeTicks::Now();
...@@ -1426,14 +1412,10 @@ Status ExecutePerformActions(Session* session, ...@@ -1426,14 +1412,10 @@ Status ExecutePerformActions(Session* session,
session->mouse_position.y, session->click_count, timestamp, session->mouse_position.y, session->click_count, timestamp,
session->mouse_click_timestamp); session->mouse_click_timestamp);
event.click_count = is_repeated_click ? 2 : 1; event.click_count = is_repeated_click ? 2 : 1;
buttons[id] |= StringToModifierMouseButton(button_type[id]);
session->mouse_position = WebPoint(event.x, event.y); session->mouse_position = WebPoint(event.x, event.y);
session->click_count = event.click_count; session->click_count = event.click_count;
session->mouse_click_timestamp = timestamp; session->mouse_click_timestamp = timestamp;
} else if (event.type == kReleasedMouseEventType) {
event.click_count = session->click_count;
}
dispatch_mouse_events.push_back(event);
if (event.type == kPressedMouseEventType) {
session->input_cancel_list.emplace_back( session->input_cancel_list.emplace_back(
action_input_states[j], &event, nullptr, nullptr); action_input_states[j], &event, nullptr, nullptr);
action_input_states[j]->SetInteger( action_input_states[j]->SetInteger(
...@@ -1441,12 +1423,14 @@ Status ExecutePerformActions(Session* session, ...@@ -1441,12 +1423,14 @@ Status ExecutePerformActions(Session* session,
action_input_states[j]->FindKey("pressed")->GetInt() | action_input_states[j]->FindKey("pressed")->GetInt() |
(1 << event.button)); (1 << event.button));
} else if (event.type == kReleasedMouseEventType) { } else if (event.type == kReleasedMouseEventType) {
event.click_count = session->click_count;
buttons[id] &= ~StringToModifierMouseButton(button_type[id]);
action_input_states[j]->SetInteger( action_input_states[j]->SetInteger(
"pressed", "pressed",
action_input_states[j]->FindKey("pressed")->GetInt() & action_input_states[j]->FindKey("pressed")->GetInt() &
~(1 << event.button)); ~(1 << event.button));
} }
dispatch_mouse_events.push_back(event);
Status status = web_view->DispatchMouseEvents( Status status = web_view->DispatchMouseEvents(
dispatch_mouse_events, session->GetCurrentFrameId()); dispatch_mouse_events, session->GetCurrentFrameId());
if (status.IsError()) if (status.IsError())
...@@ -1454,29 +1438,9 @@ Status ExecutePerformActions(Session* session, ...@@ -1454,29 +1438,9 @@ Status ExecutePerformActions(Session* session,
} else if (pointer_type == "touch") { } else if (pointer_type == "touch") {
if (action_type == "pointerDown") if (action_type == "pointerDown")
has_touch_start[id] = true; has_touch_start[id] = true;
TouchEvent event(StringToTouchEventType(action_type),
TouchEvent event(StringToTouchEventType(action_type), x, y); action_locations[id].x(),
if (event.type == kTouchMove) { action_locations[id].y());
if (origin == kPointer) {
event.x += action_locations[id].x();
event.y += action_locations[id].y();
} else if (!element_id.empty()) {
int center_x = 0, center_y = 0;
Status status = ElementInViewCenter(
session, web_view, element_id, &center_x, &center_y);
if (status.IsError())
return status;
event.x += center_x;
event.y += center_y;
}
if (event.x < 0 || event.x > viewport_width || event.y < 0 ||
event.y > viewport_height)
return Status(kMoveTargetOutOfBounds);
action_locations[id] = gfx::Point(event.x, event.y);
} else {
event.x = action_locations[id].x();
event.y = action_locations[id].y();
}
if (event.type == kTouchStart) { if (event.type == kTouchStart) {
session->input_cancel_list.emplace_back( session->input_cancel_list.emplace_back(
action_input_states[j], nullptr, &event, nullptr); action_input_states[j], nullptr, &event, nullptr);
...@@ -1485,19 +1449,17 @@ Status ExecutePerformActions(Session* session, ...@@ -1485,19 +1449,17 @@ Status ExecutePerformActions(Session* session,
action_input_states[j]->SetInteger("pressed", 0); action_input_states[j]->SetInteger("pressed", 0);
} }
if (has_touch_start[id]) { if (has_touch_start[id]) {
dispatch_touch_events.push_back(event); Status status = web_view->DispatchTouchEvent(event);
Status status =
web_view->DispatchTouchEvents(dispatch_touch_events);
if (status.IsError()) if (status.IsError())
return status; return status;
} }
if (action_type == "pointerUp") if (action_type == "pointerUp")
has_touch_start[id] = false; has_touch_start[id] = false;
} }
}
action_input_states[j]->SetInteger("x", action_locations[id].x()); action_input_states[j]->SetInteger("x", action_locations[id].x());
action_input_states[j]->SetInteger("y", action_locations[id].y()); action_input_states[j]->SetInteger("y", action_locations[id].y());
} }
}
if (tick_duration > 0) { if (tick_duration > 0) {
base::PlatformThread::Sleep( base::PlatformThread::Sleep(
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment