| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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/chromedriver/window_commands.h" | 5 #include "chrome/test/chromedriver/window_commands.h" |
| 6 | 6 |
| 7 #include <list> | 7 #include <list> |
| 8 | 8 |
| 9 #include "base/callback.h" | 9 #include "base/callback.h" |
| 10 #include "base/string_number_conversions.h" | 10 #include "base/string_number_conversions.h" |
| 11 #include "base/stringprintf.h" | 11 #include "base/stringprintf.h" |
| 12 #include "base/values.h" | 12 #include "base/values.h" |
| 13 #include "chrome/test/chromedriver/basic_types.h" | 13 #include "chrome/test/chromedriver/basic_types.h" |
| 14 #include "chrome/test/chromedriver/chrome.h" | 14 #include "chrome/test/chromedriver/chrome.h" |
| 15 #include "chrome/test/chromedriver/element_util.h" | 15 #include "chrome/test/chromedriver/element_util.h" |
| 16 #include "chrome/test/chromedriver/js.h" | 16 #include "chrome/test/chromedriver/js.h" |
| 17 #include "chrome/test/chromedriver/session.h" | 17 #include "chrome/test/chromedriver/session.h" |
| 18 #include "chrome/test/chromedriver/status.h" | 18 #include "chrome/test/chromedriver/status.h" |
| 19 #include "chrome/test/chromedriver/ui_events.h" | 19 #include "chrome/test/chromedriver/ui_events.h" |
| 20 #include "chrome/test/chromedriver/util.h" |
| 20 #include "chrome/test/chromedriver/web_view.h" | 21 #include "chrome/test/chromedriver/web_view.h" |
| 21 | 22 |
| 22 namespace { | 23 namespace { |
| 23 | 24 |
| 24 Status GetMouseButton(const base::DictionaryValue& params, | 25 Status GetMouseButton(const base::DictionaryValue& params, |
| 25 MouseButton* button) { | 26 MouseButton* button) { |
| 26 int button_num; | 27 int button_num; |
| 27 if (!params.GetInteger("button", &button_num)) { | 28 if (!params.GetInteger("button", &button_num)) { |
| 28 button_num = 0; // Default to left mouse button. | 29 button_num = 0; // Default to left mouse button. |
| 29 } else if (button_num < 0 || button_num > 2) { | 30 } else if (button_num < 0 || button_num > 2) { |
| (...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 137 status = web_view->ConnectIfNecessary(); | 138 status = web_view->ConnectIfNecessary(); |
| 138 if (status.IsError()) | 139 if (status.IsError()) |
| 139 return status; | 140 return status; |
| 140 | 141 |
| 141 Status nav_status = web_view->WaitForPendingNavigations(session->frame); | 142 Status nav_status = web_view->WaitForPendingNavigations(session->frame); |
| 142 if (nav_status.IsError()) | 143 if (nav_status.IsError()) |
| 143 return nav_status; | 144 return nav_status; |
| 144 status = command.Run(session, web_view, params, value); | 145 status = command.Run(session, web_view, params, value); |
| 145 // Switch to main frame and retry command if subframe no longer exists. | 146 // Switch to main frame and retry command if subframe no longer exists. |
| 146 if (status.code() == kNoSuchFrame) { | 147 if (status.code() == kNoSuchFrame) { |
| 147 session->frame = ""; | 148 session->SetToFrame("", ""); |
| 148 nav_status = web_view->WaitForPendingNavigations(session->frame); | 149 nav_status = web_view->WaitForPendingNavigations(session->frame); |
| 149 if (nav_status.IsError()) | 150 if (nav_status.IsError()) |
| 150 return nav_status; | 151 return nav_status; |
| 151 status = command.Run(session, web_view, params, value); | 152 status = command.Run(session, web_view, params, value); |
| 152 } | 153 } |
| 153 nav_status = web_view->WaitForPendingNavigations(session->frame); | 154 nav_status = web_view->WaitForPendingNavigations(session->frame); |
| 154 if (status.IsOk() && nav_status.IsError() && | 155 if (status.IsOk() && nav_status.IsError() && |
| 155 nav_status.code() != kDisconnected) | 156 nav_status.code() != kDisconnected) |
| 156 return nav_status; | 157 return nav_status; |
| 157 return status; | 158 return status; |
| (...skipping 29 matching lines...) Expand all Loading... |
| 187 Status ExecuteSwitchToFrame( | 188 Status ExecuteSwitchToFrame( |
| 188 Session* session, | 189 Session* session, |
| 189 WebView* web_view, | 190 WebView* web_view, |
| 190 const base::DictionaryValue& params, | 191 const base::DictionaryValue& params, |
| 191 scoped_ptr<base::Value>* value) { | 192 scoped_ptr<base::Value>* value) { |
| 192 const base::Value* id; | 193 const base::Value* id; |
| 193 if (!params.Get("id", &id)) | 194 if (!params.Get("id", &id)) |
| 194 return Status(kUnknownError, "missing 'id'"); | 195 return Status(kUnknownError, "missing 'id'"); |
| 195 | 196 |
| 196 if (id->IsType(base::Value::TYPE_NULL)) { | 197 if (id->IsType(base::Value::TYPE_NULL)) { |
| 197 session->frame = ""; | 198 session->SetToFrame("", ""); |
| 198 return Status(kOk); | 199 return Status(kOk); |
| 199 } | 200 } |
| 200 | 201 |
| 201 std::string script; | 202 std::string script; |
| 202 base::ListValue args; | 203 base::ListValue args; |
| 203 const base::DictionaryValue* id_dict; | 204 const base::DictionaryValue* id_dict; |
| 204 if (id->GetAsDictionary(&id_dict)) { | 205 if (id->GetAsDictionary(&id_dict)) { |
| 205 script = "function(elem) { return elem; }"; | 206 script = "function(elem) { return elem; }"; |
| 206 args.Append(id_dict->DeepCopy()); | 207 args.Append(id_dict->DeepCopy()); |
| 207 } else { | 208 } else { |
| (...skipping 13 matching lines...) Expand all Loading... |
| 221 } else { | 222 } else { |
| 222 return Status(kUnknownError, "invalid 'id'"); | 223 return Status(kUnknownError, "invalid 'id'"); |
| 223 } | 224 } |
| 224 args.Append(new base::StringValue(xpath)); | 225 args.Append(new base::StringValue(xpath)); |
| 225 } | 226 } |
| 226 std::string frame; | 227 std::string frame; |
| 227 Status status = web_view->GetFrameByFunction( | 228 Status status = web_view->GetFrameByFunction( |
| 228 session->frame, script, args, &frame); | 229 session->frame, script, args, &frame); |
| 229 if (status.IsError()) | 230 if (status.IsError()) |
| 230 return status; | 231 return status; |
| 231 session->frame = frame; | 232 |
| 233 scoped_ptr<base::Value> result; |
| 234 status = web_view->CallFunction( |
| 235 session->frame, script, args, &result); |
| 236 if (status.IsError()) |
| 237 return status; |
| 238 const base::DictionaryValue* element; |
| 239 if (!result->GetAsDictionary(&element)) |
| 240 return Status(kUnknownError, "fail to locate the sub frame element"); |
| 241 |
| 242 std::string fake_id = GenerateId(); |
| 243 const char* kSetFrameIdentifier = |
| 244 "function(frame, id) {" |
| 245 " frame.setAttribute('cd_frame_id_', id);" |
| 246 "}"; |
| 247 base::ListValue new_args; |
| 248 new_args.Append(element->DeepCopy()); |
| 249 new_args.AppendString(fake_id); |
| 250 result.reset(NULL); |
| 251 status = web_view->CallFunction( |
| 252 session->frame, kSetFrameIdentifier, new_args, &result); |
| 253 if (status.IsError()) |
| 254 return status; |
| 255 session->SetToFrame( |
| 256 frame, base::StringPrintf("//*[@cd_frame_id_ = '%s']", fake_id.c_str())); |
| 232 return Status(kOk); | 257 return Status(kOk); |
| 233 } | 258 } |
| 234 | 259 |
| 235 Status ExecuteGetTitle( | 260 Status ExecuteGetTitle( |
| 236 Session* session, | 261 Session* session, |
| 237 WebView* web_view, | 262 WebView* web_view, |
| 238 const base::DictionaryValue& params, | 263 const base::DictionaryValue& params, |
| 239 scoped_ptr<base::Value>* value) { | 264 scoped_ptr<base::Value>* value) { |
| 240 const char* kGetTitleScript = | 265 const char* kGetTitleScript = |
| 241 "function() {" | 266 "function() {" |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 335 if (has_element) { | 360 if (has_element) { |
| 336 Status status = ScrollElementIntoView( | 361 Status status = ScrollElementIntoView( |
| 337 session, web_view, element_id, &location); | 362 session, web_view, element_id, &location); |
| 338 if (status.IsError()) | 363 if (status.IsError()) |
| 339 return status; | 364 return status; |
| 340 } else { | 365 } else { |
| 341 location = session->mouse_position; | 366 location = session->mouse_position; |
| 342 } | 367 } |
| 343 | 368 |
| 344 if (has_offset) { | 369 if (has_offset) { |
| 345 location.offset(x_offset, y_offset); | 370 location.Offset(x_offset, y_offset); |
| 346 } else { | 371 } else { |
| 347 WebSize size; | 372 WebSize size; |
| 348 Status status = GetElementSize(session, web_view, element_id, &size); | 373 Status status = GetElementSize(session, web_view, element_id, &size); |
| 349 if (status.IsError()) | 374 if (status.IsError()) |
| 350 return status; | 375 return status; |
| 351 location.offset(size.width / 2, size.height / 2); | 376 location.Offset(size.width / 2, size.height / 2); |
| 352 } | 377 } |
| 353 | 378 |
| 354 std::list<MouseEvent> events; | 379 std::list<MouseEvent> events; |
| 355 events.push_back( | 380 events.push_back( |
| 356 MouseEvent(kMovedMouseEventType, kNoneMouseButton, | 381 MouseEvent(kMovedMouseEventType, kNoneMouseButton, |
| 357 location.x, location.y, 0)); | 382 location.x, location.y, 0)); |
| 358 Status status = web_view->DispatchMouseEvents(events); | 383 Status status = web_view->DispatchMouseEvents(events); |
| 359 if (status.IsOk()) | 384 if (status.IsOk()) |
| 360 session->mouse_position = location; | 385 session->mouse_position = location; |
| 361 return status; | 386 return status; |
| (...skipping 287 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 649 for (std::list<Cookie>::const_iterator it = cookies.begin(); | 674 for (std::list<Cookie>::const_iterator it = cookies.begin(); |
| 650 it != cookies.end(); ++it) { | 675 it != cookies.end(); ++it) { |
| 651 status = web_view->DeleteCookie(it->name, url); | 676 status = web_view->DeleteCookie(it->name, url); |
| 652 if (status.IsError()) | 677 if (status.IsError()) |
| 653 return status; | 678 return status; |
| 654 } | 679 } |
| 655 } | 680 } |
| 656 | 681 |
| 657 return Status(kOk); | 682 return Status(kOk); |
| 658 } | 683 } |
| OLD | NEW |