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/capabilities.h" | 5 #include "chrome/test/chromedriver/capabilities.h" |
6 | 6 |
7 #include <map> | 7 #include <map> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/callback.h" | 10 #include "base/callback.h" |
11 #include "base/json/string_escape.h" | 11 #include "base/json/string_escape.h" |
12 #include "base/logging.h" | 12 #include "base/logging.h" |
13 #include "base/strings/string_number_conversions.h" | 13 #include "base/strings/string_number_conversions.h" |
14 #include "base/strings/string_split.h" | 14 #include "base/strings/string_split.h" |
15 #include "base/strings/string_tokenizer.h" | 15 #include "base/strings/string_tokenizer.h" |
16 #include "base/strings/string_util.h" | 16 #include "base/strings/string_util.h" |
17 #include "base/strings/stringprintf.h" | 17 #include "base/strings/stringprintf.h" |
18 #include "base/strings/utf_string_conversions.h" | 18 #include "base/strings/utf_string_conversions.h" |
19 #include "base/values.h" | 19 #include "base/values.h" |
| 20 #include "chrome/test/chromedriver/chrome/mobile_device.h" |
20 #include "chrome/test/chromedriver/chrome/status.h" | 21 #include "chrome/test/chromedriver/chrome/status.h" |
21 #include "chrome/test/chromedriver/logging.h" | 22 #include "chrome/test/chromedriver/logging.h" |
22 #include "net/base/net_util.h" | 23 #include "net/base/net_util.h" |
23 | 24 |
24 namespace { | 25 namespace { |
25 | 26 |
26 typedef base::Callback<Status(const base::Value&, Capabilities*)> Parser; | 27 typedef base::Callback<Status(const base::Value&, Capabilities*)> Parser; |
27 | 28 |
28 Status ParseBoolean( | 29 Status ParseBoolean( |
29 bool* to_set, | 30 bool* to_set, |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
77 Status IgnoreCapability(const base::Value& option, Capabilities* capabilities) { | 78 Status IgnoreCapability(const base::Value& option, Capabilities* capabilities) { |
78 return Status(kOk); | 79 return Status(kOk); |
79 } | 80 } |
80 | 81 |
81 Status ParseLogPath(const base::Value& option, Capabilities* capabilities) { | 82 Status ParseLogPath(const base::Value& option, Capabilities* capabilities) { |
82 if (!option.GetAsString(&capabilities->log_path)) | 83 if (!option.GetAsString(&capabilities->log_path)) |
83 return Status(kUnknownError, "must be a string"); | 84 return Status(kUnknownError, "must be a string"); |
84 return Status(kOk); | 85 return Status(kOk); |
85 } | 86 } |
86 | 87 |
| 88 Status ParseDeviceName(std::string device_name, Capabilities* capabilities) { |
| 89 scoped_ptr<MobileDevice> device; |
| 90 Status status = FindMobileDevice(device_name, &device); |
| 91 |
| 92 if (status.IsError()) { |
| 93 return Status(kUnknownError, |
| 94 "'" + device_name + "' must be a valid device", |
| 95 status); |
| 96 } |
| 97 |
| 98 capabilities->device_metrics.reset(device->device_metrics.release()); |
| 99 capabilities->switches.SetSwitch("user-agent", device->user_agent); |
| 100 |
| 101 return Status(kOk); |
| 102 } |
| 103 |
| 104 Status ParseMobileEmulation(const base::Value& option, |
| 105 Capabilities* capabilities) { |
| 106 const base::DictionaryValue* mobile_emulation; |
| 107 if (!option.GetAsDictionary(&mobile_emulation)) |
| 108 return Status(kUnknownError, "'mobileEmulation' must be a dictionary"); |
| 109 |
| 110 if (mobile_emulation->HasKey("deviceName")) { |
| 111 // Cannot use any other options with deviceName. |
| 112 if (mobile_emulation->size() > 1) |
| 113 return Status(kUnknownError, "'deviceName' must be used alone"); |
| 114 |
| 115 std::string device_name; |
| 116 if (!mobile_emulation->GetString("deviceName", &device_name)) |
| 117 return Status(kUnknownError, "'deviceName' must be a string"); |
| 118 |
| 119 return ParseDeviceName(device_name, capabilities); |
| 120 } |
| 121 |
| 122 if (mobile_emulation->HasKey("deviceMetrics")) { |
| 123 const base::DictionaryValue* metrics; |
| 124 if (!mobile_emulation->GetDictionary("deviceMetrics", &metrics)) |
| 125 return Status(kUnknownError, "'deviceMetrics' must be a dictionary"); |
| 126 |
| 127 int width; |
| 128 int height; |
| 129 double device_scale_factor; |
| 130 if (!metrics->GetInteger("width", &width) || |
| 131 !metrics->GetInteger("height", &height) || |
| 132 !metrics->GetDouble("pixelRatio", &device_scale_factor)) |
| 133 return Status(kUnknownError, "invalid 'deviceMetrics'"); |
| 134 |
| 135 DeviceMetrics* device_metrics = |
| 136 new DeviceMetrics(width, height, device_scale_factor); |
| 137 capabilities->device_metrics = |
| 138 scoped_ptr<DeviceMetrics>(device_metrics); |
| 139 } |
| 140 |
| 141 if (mobile_emulation->HasKey("userAgent")) { |
| 142 std::string user_agent; |
| 143 if (!mobile_emulation->GetString("userAgent", &user_agent)) |
| 144 return Status(kUnknownError, "'userAgent' must be a string"); |
| 145 |
| 146 capabilities->switches.SetSwitch("user-agent", user_agent); |
| 147 } |
| 148 |
| 149 return Status(kOk); |
| 150 } |
| 151 |
87 Status ParseSwitches(const base::Value& option, | 152 Status ParseSwitches(const base::Value& option, |
88 Capabilities* capabilities) { | 153 Capabilities* capabilities) { |
89 const base::ListValue* switches_list = NULL; | 154 const base::ListValue* switches_list = NULL; |
90 if (!option.GetAsList(&switches_list)) | 155 if (!option.GetAsList(&switches_list)) |
91 return Status(kUnknownError, "must be a list"); | 156 return Status(kUnknownError, "must be a list"); |
92 for (size_t i = 0; i < switches_list->GetSize(); ++i) { | 157 for (size_t i = 0; i < switches_list->GetSize(); ++i) { |
93 std::string arg_string; | 158 std::string arg_string; |
94 if (!switches_list->GetString(i, &arg_string)) | 159 if (!switches_list->GetString(i, &arg_string)) |
95 return Status(kUnknownError, "each argument must be a string"); | 160 return Status(kUnknownError, "each argument must be a string"); |
96 capabilities->switches.SetUnparsedSwitch(arg_string); | 161 capabilities->switches.SetUnparsedSwitch(arg_string); |
(...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
265 parser_map["androidUseRunningApp"] = | 330 parser_map["androidUseRunningApp"] = |
266 base::Bind(&ParseBoolean, &capabilities->android_use_running_app); | 331 base::Bind(&ParseBoolean, &capabilities->android_use_running_app); |
267 parser_map["args"] = base::Bind(&ParseSwitches); | 332 parser_map["args"] = base::Bind(&ParseSwitches); |
268 parser_map["loadAsync"] = base::Bind(&IgnoreDeprecatedOption, "loadAsync"); | 333 parser_map["loadAsync"] = base::Bind(&IgnoreDeprecatedOption, "loadAsync"); |
269 } else if (is_existing) { | 334 } else if (is_existing) { |
270 parser_map["debuggerAddress"] = base::Bind(&ParseUseExistingBrowser); | 335 parser_map["debuggerAddress"] = base::Bind(&ParseUseExistingBrowser); |
271 } else { | 336 } else { |
272 parser_map["args"] = base::Bind(&ParseSwitches); | 337 parser_map["args"] = base::Bind(&ParseSwitches); |
273 parser_map["binary"] = base::Bind(&ParseFilePath, &capabilities->binary); | 338 parser_map["binary"] = base::Bind(&ParseFilePath, &capabilities->binary); |
274 parser_map["detach"] = base::Bind(&ParseBoolean, &capabilities->detach); | 339 parser_map["detach"] = base::Bind(&ParseBoolean, &capabilities->detach); |
| 340 parser_map["mobileEmulation"] = base::Bind(&ParseMobileEmulation); |
275 parser_map["excludeSwitches"] = base::Bind(&ParseExcludeSwitches); | 341 parser_map["excludeSwitches"] = base::Bind(&ParseExcludeSwitches); |
276 parser_map["extensions"] = base::Bind(&ParseExtensions); | 342 parser_map["extensions"] = base::Bind(&ParseExtensions); |
277 parser_map["forceDevToolsScreenshot"] = base::Bind( | 343 parser_map["forceDevToolsScreenshot"] = base::Bind( |
278 &ParseBoolean, &capabilities->force_devtools_screenshot); | 344 &ParseBoolean, &capabilities->force_devtools_screenshot); |
279 parser_map["loadAsync"] = base::Bind(&IgnoreDeprecatedOption, "loadAsync"); | 345 parser_map["loadAsync"] = base::Bind(&IgnoreDeprecatedOption, "loadAsync"); |
280 parser_map["localState"] = | 346 parser_map["localState"] = |
281 base::Bind(&ParseDict, &capabilities->local_state); | 347 base::Bind(&ParseDict, &capabilities->local_state); |
282 parser_map["logPath"] = base::Bind(&ParseLogPath); | 348 parser_map["logPath"] = base::Bind(&ParseLogPath); |
283 parser_map["minidumpPath"] = | 349 parser_map["minidumpPath"] = |
284 base::Bind(&ParseString, &capabilities->minidump_path); | 350 base::Bind(&ParseString, &capabilities->minidump_path); |
(...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
433 if (desired_caps.Get(it->first, &capability)) { | 499 if (desired_caps.Get(it->first, &capability)) { |
434 Status status = it->second.Run(*capability, this); | 500 Status status = it->second.Run(*capability, this); |
435 if (status.IsError()) { | 501 if (status.IsError()) { |
436 return Status( | 502 return Status( |
437 kUnknownError, "cannot parse capability: " + it->first, status); | 503 kUnknownError, "cannot parse capability: " + it->first, status); |
438 } | 504 } |
439 } | 505 } |
440 } | 506 } |
441 return Status(kOk); | 507 return Status(kOk); |
442 } | 508 } |
OLD | NEW |