| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "base/base_paths.h" | 5 #include "base/base_paths.h" |
| 6 #include "base/logging.h" | 6 #include "base/logging.h" |
| 7 #include "base/memory/scoped_ptr.h" | 7 #include "base/memory/scoped_ptr.h" |
| 8 #include "base/path_service.h" | 8 #include "base/path_service.h" |
| 9 #include "base/string_number_conversions.h" | 9 #include "base/string_number_conversions.h" |
| 10 #include "base/string_util.h" | 10 #include "base/string_util.h" |
| 11 #include "base/time.h" | 11 #include "base/time.h" |
| 12 #include "base/utf_string_conversions.h" | 12 #include "base/utf_string_conversions.h" |
| 13 #include "base/values.h" | 13 #include "base/values.h" |
| 14 #include "chrome/common/automation_messages.h" |
| 14 #include "chrome/common/chrome_switches.h" | 15 #include "chrome/common/chrome_switches.h" |
| 15 #include "chrome/test/automation/automation_proxy.h" | 16 #include "chrome/test/automation/automation_proxy.h" |
| 16 #include "chrome/test/automation/tab_proxy.h" | 17 #include "chrome/test/automation/tab_proxy.h" |
| 17 #include "chrome/test/pyautolib/pyautolib.h" | 18 #include "chrome/test/pyautolib/pyautolib.h" |
| 18 #include "googleurl/src/gurl.h" | 19 #include "googleurl/src/gurl.h" |
| 19 | 20 |
| 20 static int64 StringToId(const std::wstring& str) { | 21 static int64 StringToId(const std::wstring& str) { |
| 21 int64 id; | 22 int64 id; |
| 22 base::StringToInt64(WideToUTF8(str), &id); | 23 base::StringToInt64(WideToUTF8(str), &id); |
| 23 return id; | 24 return id; |
| (...skipping 339 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 363 | 364 |
| 364 | 365 |
| 365 scoped_refptr<BrowserProxy> PyUITestBase::GetBrowserWindow(int window_index) { | 366 scoped_refptr<BrowserProxy> PyUITestBase::GetBrowserWindow(int window_index) { |
| 366 return automation()->GetBrowserWindow(window_index); | 367 return automation()->GetBrowserWindow(window_index); |
| 367 } | 368 } |
| 368 | 369 |
| 369 std::string PyUITestBase::_SendJSONRequest(int window_index, | 370 std::string PyUITestBase::_SendJSONRequest(int window_index, |
| 370 const std::string& request, | 371 const std::string& request, |
| 371 int timeout) { | 372 int timeout) { |
| 372 std::string response; | 373 std::string response; |
| 373 base::TimeTicks time; | 374 bool success; |
| 374 if (window_index < 0) { // Do not need to target a browser window. | 375 AutomationMessageSender* automation_sender = automation(); |
| 375 time = base::TimeTicks::Now(); | 376 base::TimeTicks time = base::TimeTicks::Now(); |
| 376 if (!automation()->SendJSONRequest(request, timeout, &response)) { | 377 |
| 377 LOG(WARNING) << "SendJSONRequest returned false after " | 378 if (!automation_sender) { |
| 378 << (base::TimeTicks::Now() - time).InSeconds() | 379 ErrorResponse("The automation proxy does not exist", request, &response); |
| 379 << " seconds: " << request; | 380 } else if (!automation_sender->Send( |
| 380 } | 381 new AutomationMsg_SendJSONRequest(window_index, request, &response, |
| 381 } else { | 382 &success), |
| 382 scoped_refptr<BrowserProxy> browser_proxy = GetBrowserWindow(window_index); | 383 timeout)) { |
| 383 if (!browser_proxy.get()) { | 384 RequestFailureResponse(request, base::TimeTicks::Now() - time, |
| 384 base::DictionaryValue error_dict; | 385 base::TimeDelta::FromMilliseconds(timeout), |
| 385 std::string error_string = StringPrintf( | 386 &response); |
| 386 "No browser at windex=%d for %s", window_index, request.c_str()); | |
| 387 LOG(WARNING) << error_string; | |
| 388 error_dict.SetString("error", error_string); | |
| 389 base::JSONWriter::Write(&error_dict, &response); | |
| 390 } else { | |
| 391 time = base::TimeTicks::Now(); | |
| 392 if (!browser_proxy->SendJSONRequest(request, timeout, &response)) { | |
| 393 LOG(WARNING) << "SendJSONRequest returned false after " | |
| 394 << (base::TimeTicks::Now() - time).InSeconds() | |
| 395 << " seconds: " << request; | |
| 396 } | |
| 397 } | |
| 398 } | 387 } |
| 399 return response; | 388 return response; |
| 400 } | 389 } |
| 401 | 390 |
| 391 void PyUITestBase::ErrorResponse( |
| 392 const std::string& error_string, |
| 393 const std::string& request, |
| 394 std::string* response) { |
| 395 base::DictionaryValue error_dict; |
| 396 LOG(ERROR) << "Error during automation: " << response; |
| 397 error_dict.SetString("error", |
| 398 StringPrintf("%s for %s", |
| 399 error_string.c_str(), |
| 400 request.c_str())); |
| 401 base::JSONWriter::Write(&error_dict, response); |
| 402 } |
| 403 |
| 404 void PyUITestBase::RequestFailureResponse( |
| 405 const std::string& request, |
| 406 const base::TimeDelta& duration, |
| 407 const base::TimeDelta& timeout, |
| 408 std::string* response) { |
| 409 // TODO(craigdh): Determine timeout directly from IPC's Send(). |
| 410 if (duration >= timeout) { |
| 411 ErrorResponse( |
| 412 StringPrintf("Request timed out after %d seconds", |
| 413 static_cast<int>(duration.InSeconds())), |
| 414 request, response); |
| 415 } else { |
| 416 // TODO(craigdh): Determine specific cause. |
| 417 ErrorResponse("Chrome failed to respond", request, response); |
| 418 } |
| 419 } |
| 420 |
| 402 bool PyUITestBase::ResetToDefaultTheme() { | 421 bool PyUITestBase::ResetToDefaultTheme() { |
| 403 return automation()->ResetToDefaultTheme(); | 422 return automation()->ResetToDefaultTheme(); |
| 404 } | 423 } |
| 405 | 424 |
| 406 bool PyUITestBase::SetCookie(const GURL& cookie_url, | 425 bool PyUITestBase::SetCookie(const GURL& cookie_url, |
| 407 const std::string& value, | 426 const std::string& value, |
| 408 int window_index, | 427 int window_index, |
| 409 int tab_index) { | 428 int tab_index) { |
| 410 scoped_refptr<BrowserProxy> browser_proxy = GetBrowserWindow(window_index); | 429 scoped_refptr<BrowserProxy> browser_proxy = GetBrowserWindow(window_index); |
| 411 EXPECT_TRUE(browser_proxy.get()); | 430 EXPECT_TRUE(browser_proxy.get()); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 427 // TODO(phadjan.jr): figure out a way to unambiguously report error. | 446 // TODO(phadjan.jr): figure out a way to unambiguously report error. |
| 428 if (!browser_proxy.get()) | 447 if (!browser_proxy.get()) |
| 429 return cookie_val; | 448 return cookie_val; |
| 430 scoped_refptr<TabProxy> tab_proxy = browser_proxy->GetTab(tab_index); | 449 scoped_refptr<TabProxy> tab_proxy = browser_proxy->GetTab(tab_index); |
| 431 EXPECT_TRUE(tab_proxy.get()); | 450 EXPECT_TRUE(tab_proxy.get()); |
| 432 if (!tab_proxy.get()) | 451 if (!tab_proxy.get()) |
| 433 return cookie_val; | 452 return cookie_val; |
| 434 EXPECT_TRUE(tab_proxy->GetCookies(cookie_url, &cookie_val)); | 453 EXPECT_TRUE(tab_proxy->GetCookies(cookie_url, &cookie_val)); |
| 435 return cookie_val; | 454 return cookie_val; |
| 436 } | 455 } |
| OLD | NEW |