OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2014 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "base/json/json_reader.h" | |
6 #include "base/strings/string_number_conversions.h" | |
7 #include "base/strings/string_split.h" | |
8 #include "base/values.h" | |
9 #include "chrome/test/chromedriver/chrome/mobile_devices.h" | |
10 #include "chrome/test/chromedriver/chrome/status.h" | |
11 #include "chrome/test/chromedriver/mobile_device.h" | |
12 | |
13 MobileDevice::MobileDevice() {} | |
14 MobileDevice::MobileDevice(DeviceMetrics device_metrics, std::string user_agent) | |
15 : device_metrics(device_metrics), user_agent(user_agent) {} | |
16 MobileDevice::~MobileDevice() {} | |
17 | |
18 Status FindMobileDevice(std::string device_name, | |
19 scoped_ptr<MobileDevice>* mobile_device) { | |
20 base::JSONReader json_reader(base::JSON_ALLOW_TRAILING_COMMAS); | |
21 scoped_ptr<base::Value> devices_value; | |
22 devices_value.reset(json_reader.ReadToValue(kMobileDevices)); | |
23 if (!devices_value.get()) | |
24 return Status(kUnknownError, | |
25 "Could not parse mobile device list because " + | |
stgao
2014/05/14 05:10:14
lowercase for error message. "Could" -> "could".
sam.rawlins
2014/05/16 21:09:38
Done.
| |
26 json_reader.GetErrorMessage()); | |
27 | |
28 base::ListValue* mobile_devices; | |
29 if (!devices_value->GetAsList(&mobile_devices)) | |
30 return Status(kUnknownError, "malformed device metrics list"); | |
31 | |
32 for (base::ListValue::iterator it = mobile_devices->begin(); | |
33 it != mobile_devices->end(); | |
34 ++it) { | |
35 base::ListValue* device = NULL; | |
36 (*it)->GetAsList(&device); | |
samuong
2014/05/09 17:58:54
GetAsList, as well as GetString, StringToInt and S
sam.rawlins
2014/05/16 21:09:38
Done.
| |
37 if (device != NULL) { | |
38 std::string name; | |
39 device->GetString(0, &name); | |
40 if (name != device_name) | |
41 continue; | |
42 | |
43 scoped_ptr<MobileDevice> tmp_mobile_device(new MobileDevice()); | |
44 std::string device_metrics_string; | |
45 device->GetString(1, &tmp_mobile_device->user_agent); | |
46 device->GetString(2, &device_metrics_string); | |
47 std::vector<std::string> metrics_vector; | |
48 base::SplitString(device_metrics_string, 'x', &metrics_vector); | |
49 if (metrics_vector.size() < 3) | |
50 return Status(kUnknownError, "malformed device metrics string"); | |
51 | |
52 int width = 0; | |
53 int height = 0; | |
54 double device_scale_factor = 0.0; | |
55 base::StringToInt(metrics_vector[0], &width); | |
56 base::StringToInt(metrics_vector[1], &height); | |
57 base::StringToDouble(metrics_vector[2], &device_scale_factor); | |
58 tmp_mobile_device->device_metrics = | |
59 DeviceMetrics(width, height, device_scale_factor); | |
60 | |
61 *mobile_device = tmp_mobile_device.Pass(); | |
62 return Status(kOk); | |
63 } | |
64 } | |
65 | |
66 return Status(kUnknownError, "must be a valid device"); | |
67 } | |
OLD | NEW |