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/webdriver/utility_functions.h" | 5 #include "chrome/test/webdriver/utility_functions.h" |
6 | 6 |
7 #include <string.h> | |
8 #include <wchar.h> | |
9 #include <algorithm> | |
10 #include <sstream> | |
11 | |
12 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
13 #include "base/format_macros.h" | 8 #include "base/format_macros.h" |
14 #include "base/json/json_reader.h" | |
15 #include "base/logging.h" | |
16 #include "base/memory/scoped_ptr.h" | |
17 #include "base/rand_util.h" | 9 #include "base/rand_util.h" |
18 #include "base/stringprintf.h" | 10 #include "base/stringprintf.h" |
19 | 11 |
20 namespace webdriver { | 12 namespace webdriver { |
21 | 13 |
22 std::string print_valuetype(Value::ValueType e) { | |
23 switch (e) { | |
24 case Value::TYPE_NULL: | |
25 return "NULL "; | |
26 case Value::TYPE_BOOLEAN: | |
27 return "BOOL"; | |
28 case Value::TYPE_INTEGER: | |
29 return "INT"; | |
30 case Value::TYPE_DOUBLE: | |
31 return "DOUBLE"; | |
32 case Value::TYPE_STRING: | |
33 return "STRING"; | |
34 case Value::TYPE_BINARY: | |
35 return "BIN"; | |
36 case Value::TYPE_DICTIONARY: | |
37 return "DICT"; | |
38 case Value::TYPE_LIST: | |
39 return "LIST"; | |
40 default: | |
41 return "ERROR"; | |
42 } | |
43 } | |
44 | |
45 void CheckValueType(const Value::ValueType expected, | |
46 const Value* const actual) { | |
47 DCHECK(actual != NULL) << "Expected value to be non-NULL"; | |
48 DCHECK(expected == actual->GetType()) | |
49 << "Expected " << print_valuetype(expected) | |
50 << ", but was " << print_valuetype(actual->GetType()); | |
51 } | |
52 | |
53 bool ParseJSONDictionary(const std::string& json, DictionaryValue** dict, | |
54 std::string* error) { | |
55 int error_code = 0; | |
56 Value* params = | |
57 base::JSONReader::ReadAndReturnError(json, true, &error_code, error); | |
58 if (error_code != 0) { | |
59 VLOG(1) << "Could not parse JSON object, " << *error; | |
60 if (params) | |
61 delete params; | |
62 return false; | |
63 } | |
64 | |
65 if (!params || params->GetType() != Value::TYPE_DICTIONARY) { | |
66 *error = "Data passed in URL must be of type dictionary."; | |
67 VLOG(1) << "Invalid type to parse"; | |
68 if (params) | |
69 delete params; | |
70 return false; | |
71 } | |
72 | |
73 *dict = static_cast<DictionaryValue*>(params); | |
74 return true; | |
75 } | |
76 | |
77 std::string GenerateRandomID() { | 14 std::string GenerateRandomID() { |
78 uint64 msb = base::RandUint64(); | 15 uint64 msb = base::RandUint64(); |
79 uint64 lsb = base::RandUint64(); | 16 uint64 lsb = base::RandUint64(); |
80 return base::StringPrintf("%016" PRIx64 "%016" PRIx64, msb, lsb); | 17 return base::StringPrintf("%016" PRIx64 "%016" PRIx64, msb, lsb); |
81 } | 18 } |
82 | 19 |
83 } // namespace webdriver | 20 } // namespace webdriver |
OLD | NEW |