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/automation/automation_json_requests.h" | 5 #include "chrome/test/automation/automation_json_requests.h" |
6 | 6 |
7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
8 #include "base/file_path.h" | 8 #include "base/file_path.h" |
9 #include "base/format_macros.h" | 9 #include "base/format_macros.h" |
10 #include "base/json/json_reader.h" | 10 #include "base/json/json_reader.h" |
11 #include "base/json/json_writer.h" | 11 #include "base/json/json_writer.h" |
12 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
13 #include "base/stringprintf.h" | 13 #include "base/stringprintf.h" |
14 #include "base/test/test_timeouts.h" | 14 #include "base/test/test_timeouts.h" |
15 #include "base/time.h" | 15 #include "base/time.h" |
16 #include "base/values.h" | 16 #include "base/values.h" |
17 #include "chrome/common/automation_messages.h" | 17 #include "chrome/common/automation_messages.h" |
18 #include "chrome/test/automation/automation_proxy.h" | 18 #include "chrome/test/automation/automation_proxy.h" |
19 #include "content/common/json_value_serializer.h" | 19 #include "content/common/json_value_serializer.h" |
20 | 20 |
21 namespace { | 21 namespace { |
22 | 22 |
23 bool SendAutomationJSONRequest(AutomationMessageSender* sender, | 23 bool SendAutomationJSONRequest(AutomationMessageSender* sender, |
24 const DictionaryValue& request_dict, | 24 const DictionaryValue& request_dict, |
25 DictionaryValue* reply_dict, | 25 DictionaryValue* reply_dict, |
26 std::string* error_msg) { | 26 std::string* error_msg) { |
27 std::string request, reply; | 27 std::string request, reply; |
28 base::JSONWriter::Write(&request_dict, false, &request); | 28 base::JSONWriter::Write(&request_dict, false, &request); |
29 std::string command; | 29 bool success = false; |
30 request_dict.GetString("command", &command); | 30 int timeout_ms = TestTimeouts::action_max_timeout_ms(); |
31 LOG(INFO) << "Sending '" << command << "' command."; | |
32 | |
33 base::Time before_sending = base::Time::Now(); | 31 base::Time before_sending = base::Time::Now(); |
34 bool success = false; | 32 if (!SendAutomationJSONRequest( |
35 if (!SendAutomationJSONRequestWithDefaultTimeout( | 33 sender, request, timeout_ms, &reply, &success)) { |
36 sender, request, &reply, &success)) { | 34 int64 elapsed_ms = (base::Time::Now() - before_sending).InMilliseconds(); |
37 *error_msg = base::StringPrintf( | 35 std::string command; |
38 "Chrome did not respond to '%s'. Elapsed time was %" PRId64 " ms. " | 36 request_dict.GetString("command", &command); |
39 "Request details: (%s).", | 37 if (elapsed_ms >= timeout_ms) { |
40 command.c_str(), | 38 *error_msg = base::StringPrintf( |
41 (base::Time::Now() - before_sending).InMilliseconds(), | 39 "Chrome did not respond to '%s'. Request may have timed out. " |
42 request.c_str()); | 40 "Elapsed time was %" PRId64 " ms. Request timeout was %d ms. " |
| 41 "Request details: (%s).", |
| 42 command.c_str(), |
| 43 elapsed_ms, |
| 44 timeout_ms, |
| 45 request.c_str()); |
| 46 } else { |
| 47 *error_msg = base::StringPrintf( |
| 48 "Chrome did not respond to '%s'. Elapsed time was %" PRId64 " ms. " |
| 49 "Request details: (%s).", |
| 50 command.c_str(), |
| 51 elapsed_ms, |
| 52 request.c_str()); |
| 53 } |
43 return false; | 54 return false; |
44 } | 55 } |
45 scoped_ptr<Value> value(base::JSONReader::Read(reply, true)); | 56 scoped_ptr<Value> value(base::JSONReader::Read(reply, true)); |
46 if (!value.get() || !value->IsType(Value::TYPE_DICTIONARY)) { | 57 if (!value.get() || !value->IsType(Value::TYPE_DICTIONARY)) { |
| 58 std::string command; |
| 59 request_dict.GetString("command", &command); |
47 LOG(ERROR) << "JSON request did not return dict: " << command << "\n"; | 60 LOG(ERROR) << "JSON request did not return dict: " << command << "\n"; |
48 return false; | 61 return false; |
49 } | 62 } |
50 DictionaryValue* dict = static_cast<DictionaryValue*>(value.get()); | 63 DictionaryValue* dict = static_cast<DictionaryValue*>(value.get()); |
51 if (!success) { | 64 if (!success) { |
52 std::string error; | 65 std::string command, error; |
| 66 request_dict.GetString("command", &command); |
53 dict->GetString("error", &error); | 67 dict->GetString("error", &error); |
54 *error_msg = base::StringPrintf( | 68 *error_msg = base::StringPrintf( |
55 "Internal Chrome error during '%s': (%s). Request details: (%s).", | 69 "Internal Chrome error during '%s': (%s). Request details: (%s).", |
56 command.c_str(), | 70 command.c_str(), |
57 error.c_str(), | 71 error.c_str(), |
58 request.c_str()); | 72 request.c_str()); |
59 LOG(ERROR) << "JSON request failed: " << command << "\n" | 73 LOG(ERROR) << "JSON request failed: " << command << "\n" |
60 << " with error: " << error; | 74 << " with error: " << error; |
61 return false; | 75 return false; |
62 } | 76 } |
(...skipping 16 matching lines...) Expand all Loading... |
79 | 93 |
80 bool SendAutomationJSONRequest(AutomationMessageSender* sender, | 94 bool SendAutomationJSONRequest(AutomationMessageSender* sender, |
81 const std::string& request, | 95 const std::string& request, |
82 int timeout_ms, | 96 int timeout_ms, |
83 std::string* reply, | 97 std::string* reply, |
84 bool* success) { | 98 bool* success) { |
85 return sender->Send(new AutomationMsg_SendJSONRequest( | 99 return sender->Send(new AutomationMsg_SendJSONRequest( |
86 -1, request, reply, success), timeout_ms); | 100 -1, request, reply, success), timeout_ms); |
87 } | 101 } |
88 | 102 |
89 bool SendAutomationJSONRequestWithDefaultTimeout( | |
90 AutomationMessageSender* sender, | |
91 const std::string& request, | |
92 std::string* reply, | |
93 bool* success) { | |
94 return sender->Send(new AutomationMsg_SendJSONRequest( | |
95 -1, request, reply, success)); | |
96 } | |
97 | |
98 bool SendGetIndicesFromTabIdJSONRequest( | 103 bool SendGetIndicesFromTabIdJSONRequest( |
99 AutomationMessageSender* sender, | 104 AutomationMessageSender* sender, |
100 int tab_id, | 105 int tab_id, |
101 int* browser_index, | 106 int* browser_index, |
102 int* tab_index, | 107 int* tab_index, |
103 std::string* error_msg) { | 108 std::string* error_msg) { |
104 DictionaryValue request_dict; | 109 DictionaryValue request_dict; |
105 request_dict.SetString("command", "GetIndicesFromTab"); | 110 request_dict.SetString("command", "GetIndicesFromTab"); |
106 request_dict.SetInteger("tab_id", tab_id); | 111 request_dict.SetInteger("tab_id", tab_id); |
107 DictionaryValue reply_dict; | 112 DictionaryValue reply_dict; |
(...skipping 515 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
623 AutomationMessageSender* sender, | 628 AutomationMessageSender* sender, |
624 int* version, | 629 int* version, |
625 std::string* error_msg) { | 630 std::string* error_msg) { |
626 DictionaryValue dict; | 631 DictionaryValue dict; |
627 dict.SetString("command", "GetChromeDriverAutomationVersion"); | 632 dict.SetString("command", "GetChromeDriverAutomationVersion"); |
628 DictionaryValue reply_dict; | 633 DictionaryValue reply_dict; |
629 if (!SendAutomationJSONRequest(sender, dict, &reply_dict, error_msg)) | 634 if (!SendAutomationJSONRequest(sender, dict, &reply_dict, error_msg)) |
630 return false; | 635 return false; |
631 return reply_dict.GetInteger("version", version); | 636 return reply_dict.GetInteger("version", version); |
632 } | 637 } |
OLD | NEW |