Chromium Code Reviews| 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 334 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 358 | 359 |
| 359 | 360 |
| 360 scoped_refptr<BrowserProxy> PyUITestBase::GetBrowserWindow(int window_index) { | 361 scoped_refptr<BrowserProxy> PyUITestBase::GetBrowserWindow(int window_index) { |
| 361 return automation()->GetBrowserWindow(window_index); | 362 return automation()->GetBrowserWindow(window_index); |
| 362 } | 363 } |
| 363 | 364 |
| 364 std::string PyUITestBase::_SendJSONRequest(int window_index, | 365 std::string PyUITestBase::_SendJSONRequest(int window_index, |
| 365 const std::string& request, | 366 const std::string& request, |
| 366 int timeout) { | 367 int timeout) { |
| 367 std::string response; | 368 std::string response; |
| 368 base::TimeTicks time; | 369 bool success; |
| 369 if (window_index < 0) { // Do not need to target a browser window. | 370 AutomationMessageSender* automation_sender = automation(); |
| 370 time = base::TimeTicks::Now(); | 371 base::TimeTicks time = base::TimeTicks::Now(); |
| 371 if (!automation()->SendJSONRequest(request, timeout, &response)) { | 372 |
| 372 LOG(WARNING) << "SendJSONRequest returned false after " | 373 if (!automation_sender) { |
| 373 << (base::TimeTicks::Now() - time).InSeconds() | 374 ErrorResponse("The automation proxy does not exist", request, &response); |
| 374 << " seconds: " << request; | 375 } else if (!automation_sender->Send( |
| 375 } | 376 new AutomationMsg_SendJSONRequestWithBrowserIndex(window_index, request, |
| 376 } else { | 377 &response, &success), |
| 377 scoped_refptr<BrowserProxy> browser_proxy = GetBrowserWindow(window_index); | 378 timeout)) { |
| 378 if (!browser_proxy.get()) { | 379 RequestFailureResponse(request, base::TimeTicks::Now() - time, |
| 379 base::DictionaryValue error_dict; | 380 base::TimeDelta::FromMilliseconds(timeout), |
| 380 std::string error_string = StringPrintf( | 381 &response); |
| 381 "No browser at windex=%d for %s", window_index, request.c_str()); | |
| 382 LOG(WARNING) << error_string; | |
| 383 error_dict.SetString("error", error_string); | |
| 384 base::JSONWriter::Write(&error_dict, &response); | |
| 385 } else { | |
| 386 time = base::TimeTicks::Now(); | |
| 387 if (!browser_proxy->SendJSONRequest(request, timeout, &response)) { | |
| 388 LOG(WARNING) << "SendJSONRequest returned false after " | |
| 389 << (base::TimeTicks::Now() - time).InSeconds() | |
| 390 << " seconds: " << request; | |
| 391 } | |
| 392 } | |
| 393 } | 382 } |
| 394 return response; | 383 return response; |
| 395 } | 384 } |
| 396 | 385 |
| 386 void PyUITestBase::ErrorResponse( | |
| 387 const std::string& error_string, | |
| 388 const std::string& request, | |
| 389 std::string* response) { | |
| 390 base::DictionaryValue error_dict; | |
| 391 LOG(WARNING) << "Error during automation: " << response; | |
|
Nirnimesh
2012/06/15 19:52:49
LOG(ERROR)?
craigdh
2012/06/19 00:19:35
Done.
| |
| 392 error_dict.SetString("error", | |
| 393 StringPrintf("%s for %s", | |
| 394 error_string.c_str(), | |
| 395 request.c_str())); | |
| 396 base::JSONWriter::Write(&error_dict, response); | |
| 397 } | |
| 398 | |
| 399 void PyUITestBase::RequestFailureResponse( | |
| 400 const std::string& request, | |
| 401 const base::TimeDelta& duration, | |
| 402 const base::TimeDelta& timeout, | |
| 403 std::string* response) { | |
| 404 if (duration >= timeout) { | |
|
Nirnimesh
2012/06/15 19:52:49
Add a TODO to deduce timeout from the ipc Send() c
craigdh
2012/06/19 00:19:35
Done.
| |
| 405 ErrorResponse( | |
| 406 StringPrintf("Request timed out after %d seconds", | |
| 407 static_cast<int>(duration.InSeconds())), | |
| 408 request, response); | |
| 409 } else { | |
| 410 // TODO(craigdh): Determine specific cause. | |
| 411 ErrorResponse("Chrome failed to respond", request, response); | |
| 412 } | |
| 413 } | |
| 414 | |
| 397 bool PyUITestBase::ResetToDefaultTheme() { | 415 bool PyUITestBase::ResetToDefaultTheme() { |
| 398 return automation()->ResetToDefaultTheme(); | 416 return automation()->ResetToDefaultTheme(); |
| 399 } | 417 } |
| 400 | 418 |
| 401 bool PyUITestBase::SetCookie(const GURL& cookie_url, | 419 bool PyUITestBase::SetCookie(const GURL& cookie_url, |
| 402 const std::string& value, | 420 const std::string& value, |
| 403 int window_index, | 421 int window_index, |
| 404 int tab_index) { | 422 int tab_index) { |
| 405 scoped_refptr<BrowserProxy> browser_proxy = GetBrowserWindow(window_index); | 423 scoped_refptr<BrowserProxy> browser_proxy = GetBrowserWindow(window_index); |
| 406 EXPECT_TRUE(browser_proxy.get()); | 424 EXPECT_TRUE(browser_proxy.get()); |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 422 // TODO(phadjan.jr): figure out a way to unambiguously report error. | 440 // TODO(phadjan.jr): figure out a way to unambiguously report error. |
| 423 if (!browser_proxy.get()) | 441 if (!browser_proxy.get()) |
| 424 return cookie_val; | 442 return cookie_val; |
| 425 scoped_refptr<TabProxy> tab_proxy = browser_proxy->GetTab(tab_index); | 443 scoped_refptr<TabProxy> tab_proxy = browser_proxy->GetTab(tab_index); |
| 426 EXPECT_TRUE(tab_proxy.get()); | 444 EXPECT_TRUE(tab_proxy.get()); |
| 427 if (!tab_proxy.get()) | 445 if (!tab_proxy.get()) |
| 428 return cookie_val; | 446 return cookie_val; |
| 429 EXPECT_TRUE(tab_proxy->GetCookies(cookie_url, &cookie_val)); | 447 EXPECT_TRUE(tab_proxy->GetCookies(cookie_url, &cookie_val)); |
| 430 return cookie_val; | 448 return cookie_val; |
| 431 } | 449 } |
| OLD | NEW |