| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/test/webdriver/commands/mouse_commands.h" | 5 #include "chrome/test/webdriver/commands/mouse_commands.h" |
| 6 | 6 |
| 7 #include "base/values.h" | 7 #include "base/values.h" |
| 8 #include "chrome/common/automation_constants.h" | 8 #include "chrome/common/automation_constants.h" |
| 9 #include "chrome/test/automation/value_conversion_util.h" |
| 9 #include "chrome/test/webdriver/commands/response.h" | 10 #include "chrome/test/webdriver/commands/response.h" |
| 10 #include "chrome/test/webdriver/session.h" | 11 #include "chrome/test/webdriver/session.h" |
| 11 #include "chrome/test/webdriver/utility_functions.h" | |
| 12 #include "chrome/test/webdriver/web_element_id.h" | 12 #include "chrome/test/webdriver/web_element_id.h" |
| 13 #include "chrome/test/webdriver/webdriver_basic_types.h" |
| 13 #include "chrome/test/webdriver/webdriver_error.h" | 14 #include "chrome/test/webdriver/webdriver_error.h" |
| 14 #include "ui/gfx/point.h" | 15 #include "chrome/test/webdriver/webdriver_util.h" |
| 15 #include "ui/gfx/size.h" | |
| 16 | 16 |
| 17 namespace { | 17 namespace { |
| 18 | 18 |
| 19 const int kLeftButton = 0; | 19 const int kLeftButton = 0; |
| 20 const int kMiddleButton = 1; | 20 const int kMiddleButton = 1; |
| 21 const int kRightButton = 2; | 21 const int kRightButton = 2; |
| 22 | 22 |
| 23 } // namespace | 23 } // namespace |
| 24 | 24 |
| 25 namespace webdriver { | 25 namespace webdriver { |
| (...skipping 13 matching lines...) Expand all Loading... |
| 39 std::string tag_name; | 39 std::string tag_name; |
| 40 Error* error = session_->GetElementTagName( | 40 Error* error = session_->GetElementTagName( |
| 41 session_->current_target(), element, &tag_name); | 41 session_->current_target(), element, &tag_name); |
| 42 if (error) { | 42 if (error) { |
| 43 response->SetError(error); | 43 response->SetError(error); |
| 44 return; | 44 return; |
| 45 } | 45 } |
| 46 | 46 |
| 47 if (tag_name == "option") { | 47 if (tag_name == "option") { |
| 48 const char* kCanOptionBeToggledScript = | 48 const char* kCanOptionBeToggledScript = |
| 49 "return (function(option) {" | 49 "function(option) {" |
| 50 " var select = option.parentElement;" | 50 " var select = option.parentElement;" |
| 51 " if (!select || select.tagName.toLowerCase() != 'select')" | 51 " if (!select || select.tagName.toLowerCase() != 'select')" |
| 52 " throw new Error('Option element is not in a select');" | 52 " throw new Error('Option element is not in a select');" |
| 53 " return select.multiple;" | 53 " return select.multiple;" |
| 54 "}).apply(null, arguments);"; | 54 "}"; |
| 55 ListValue args; | 55 bool can_be_toggled; |
| 56 args.Append(element.ToValue()); | 56 error = session_->ExecuteScriptAndParse( |
| 57 Value* value = NULL; | 57 session_->current_target(), |
| 58 error = session_->ExecuteScript( | 58 kCanOptionBeToggledScript, |
| 59 session_->current_target(), kCanOptionBeToggledScript, &args, &value); | 59 "canOptionBeToggled", |
| 60 CreateListValueFrom(element), |
| 61 CreateDirectValueParser(&can_be_toggled)); |
| 60 if (error) { | 62 if (error) { |
| 61 response->SetError(error); | 63 response->SetError(error); |
| 62 return; | 64 return; |
| 63 } | 65 } |
| 64 scoped_ptr<Value> scoped_value(value); | |
| 65 bool can_be_toggled; | |
| 66 if (!value->GetAsBoolean(&can_be_toggled)) { | |
| 67 response->SetError( | |
| 68 new Error(kUnknownError, "canOptionBeToggled returned non-boolean: " + | |
| 69 JsonStringify(value))); | |
| 70 return; | |
| 71 } | |
| 72 | 66 |
| 73 if (can_be_toggled) { | 67 if (can_be_toggled) { |
| 74 error = session_->ToggleOptionElement( | 68 error = session_->ToggleOptionElement( |
| 75 session_->current_target(), element); | 69 session_->current_target(), element); |
| 76 } else { | 70 } else { |
| 77 error = session_->SetOptionElementSelected( | 71 error = session_->SetOptionElementSelected( |
| 78 session_->current_target(), element, true); | 72 session_->current_target(), element, true); |
| 79 } | 73 } |
| 80 } else { | 74 } else { |
| 81 gfx::Point location; | 75 Point location; |
| 82 error = session_->GetClickableLocation(element, &location); | 76 error = session_->GetClickableLocation(element, &location); |
| 83 if (!error) | 77 if (!error) |
| 84 error = session_->MouseMoveAndClick(location, automation::kLeftButton); | 78 error = session_->MouseMoveAndClick(location, automation::kLeftButton); |
| 85 } | 79 } |
| 86 if (error) { | 80 if (error) { |
| 87 response->SetError(error); | 81 response->SetError(error); |
| 88 return; | 82 return; |
| 89 } | 83 } |
| 90 } | 84 } |
| 91 | 85 |
| 92 HoverCommand::HoverCommand(const std::vector<std::string>& path_segments, | 86 HoverCommand::HoverCommand(const std::vector<std::string>& path_segments, |
| 93 const DictionaryValue* const parameters) | 87 const DictionaryValue* const parameters) |
| 94 : WebElementCommand(path_segments, parameters) {} | 88 : WebElementCommand(path_segments, parameters) {} |
| 95 | 89 |
| 96 HoverCommand::~HoverCommand() {} | 90 HoverCommand::~HoverCommand() {} |
| 97 | 91 |
| 98 bool HoverCommand::DoesPost() { | 92 bool HoverCommand::DoesPost() { |
| 99 return true; | 93 return true; |
| 100 } | 94 } |
| 101 | 95 |
| 102 void HoverCommand::ExecutePost(Response* response) { | 96 void HoverCommand::ExecutePost(Response* response) { |
| 103 Error* error = NULL; | 97 Error* error = NULL; |
| 104 gfx::Point location; | 98 Point location; |
| 105 error = session_->GetClickableLocation(element, &location); | 99 error = session_->GetClickableLocation(element, &location); |
| 106 if (!error) | 100 if (!error) |
| 107 error = session_->MouseMove(location); | 101 error = session_->MouseMove(location); |
| 108 if (error) { | 102 if (error) { |
| 109 response->SetError(error); | 103 response->SetError(error); |
| 110 return; | 104 return; |
| 111 } | 105 } |
| 112 } | 106 } |
| 113 | 107 |
| 114 DragCommand::DragCommand(const std::vector<std::string>& path_segments, | 108 DragCommand::DragCommand(const std::vector<std::string>& path_segments, |
| (...skipping 14 matching lines...) Expand all Loading... |
| 129 | 123 |
| 130 return true; | 124 return true; |
| 131 } | 125 } |
| 132 | 126 |
| 133 bool DragCommand::DoesPost() { | 127 bool DragCommand::DoesPost() { |
| 134 return true; | 128 return true; |
| 135 } | 129 } |
| 136 | 130 |
| 137 void DragCommand::ExecutePost(Response* response) { | 131 void DragCommand::ExecutePost(Response* response) { |
| 138 Error* error = NULL; | 132 Error* error = NULL; |
| 139 gfx::Point drag_from; | 133 Point drag_from; |
| 140 error = session_->GetClickableLocation(element, &drag_from); | 134 error = session_->GetClickableLocation(element, &drag_from); |
| 141 if (error) { | 135 if (error) { |
| 142 response->SetError(error); | 136 response->SetError(error); |
| 143 return; | 137 return; |
| 144 } | 138 } |
| 145 | 139 |
| 146 gfx::Point drag_to(drag_from); | 140 Point drag_to(drag_from); |
| 147 drag_to.Offset(drag_x_, drag_y_); | 141 drag_to.Offset(drag_x_, drag_y_); |
| 148 if (drag_to.x() < 0 || drag_to.y() < 0) | 142 if (drag_to.x() < 0 || drag_to.y() < 0) |
| 149 error = new Error(kBadRequest, "Invalid (x,y) coordinates"); | 143 error = new Error(kBadRequest, "Invalid (x,y) coordinates"); |
| 150 if (!error) | 144 if (!error) |
| 151 error = session_->MouseDrag(drag_from, drag_to); | 145 error = session_->MouseDrag(drag_from, drag_to); |
| 152 | 146 |
| 153 if (error) { | 147 if (error) { |
| 154 response->SetError(error); | 148 response->SetError(error); |
| 155 return; | 149 return; |
| 156 } | 150 } |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 192 if (!has_element_ && !has_offset_) { | 186 if (!has_element_ && !has_offset_) { |
| 193 response->SetError(new Error( | 187 response->SetError(new Error( |
| 194 kBadRequest, "Invalid command arguments")); | 188 kBadRequest, "Invalid command arguments")); |
| 195 return false; | 189 return false; |
| 196 } | 190 } |
| 197 | 191 |
| 198 return true; | 192 return true; |
| 199 } | 193 } |
| 200 | 194 |
| 201 void MoveToCommand::ExecutePost(Response* const response) { | 195 void MoveToCommand::ExecutePost(Response* const response) { |
| 202 gfx::Point location; | 196 Point location; |
| 203 Error* error; | 197 Error* error; |
| 204 | 198 |
| 205 if (has_element_) { | 199 if (has_element_) { |
| 206 // If an element is specified, calculate the coordinate. | 200 // If an element is specified, calculate the coordinate. |
| 207 error = session_->GetElementLocationInView(element_, &location); | 201 error = session_->GetElementLocationInView(element_, &location); |
| 208 if (error) { | 202 if (error) { |
| 209 response->SetError(error); | 203 response->SetError(error); |
| 210 return; | 204 return; |
| 211 } | 205 } |
| 212 } else { | 206 } else { |
| 213 // If not, use the current mouse position. | 207 // If not, use the current mouse position. |
| 214 location = session_->get_mouse_position(); | 208 location = session_->get_mouse_position(); |
| 215 } | 209 } |
| 216 | 210 |
| 217 if (has_offset_) { | 211 if (has_offset_) { |
| 218 // If an offset is specified, translate by the offset. | 212 // If an offset is specified, translate by the offset. |
| 219 location.Offset(x_offset_, y_offset_); | 213 location.Offset(x_offset_, y_offset_); |
| 220 } else { | 214 } else { |
| 221 DCHECK(has_element_); | 215 DCHECK(has_element_); |
| 222 | 216 |
| 223 // If not, calculate the half of the element size and translate by it. | 217 // If not, calculate the half of the element size and translate by it. |
| 224 gfx::Size size; | 218 Size size; |
| 225 error = session_->GetElementSize(session_->current_target(), | 219 error = session_->GetElementSize(session_->current_target(), |
| 226 element_, &size); | 220 element_, &size); |
| 227 if (error) { | 221 if (error) { |
| 228 response->SetError(error); | 222 response->SetError(error); |
| 229 return; | 223 return; |
| 230 } | 224 } |
| 231 | 225 |
| 232 location.Offset(size.width() / 2, size.height() / 2); | 226 location.Offset(size.width() / 2, size.height() / 2); |
| 233 } | 227 } |
| 234 | 228 |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 308 | 302 |
| 309 void DoubleClickCommand::ExecutePost(Response* const response) { | 303 void DoubleClickCommand::ExecutePost(Response* const response) { |
| 310 Error* error = session_->MouseDoubleClick(); | 304 Error* error = session_->MouseDoubleClick(); |
| 311 if (error) { | 305 if (error) { |
| 312 response->SetError(error); | 306 response->SetError(error); |
| 313 return; | 307 return; |
| 314 } | 308 } |
| 315 } | 309 } |
| 316 | 310 |
| 317 } // namespace webdriver | 311 } // namespace webdriver |
| OLD | NEW |