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/webdriver_util.h" | 5 #include "chrome/test/webdriver/webdriver_util.h" |
6 | 6 |
7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
8 #include "base/format_macros.h" | 8 #include "base/format_macros.h" |
9 #include "base/json/json_writer.h" | 9 #include "base/json/json_writer.h" |
| 10 #include "base/memory/scoped_ptr.h" |
10 #include "base/rand_util.h" | 11 #include "base/rand_util.h" |
11 #include "base/stringprintf.h" | 12 #include "base/stringprintf.h" |
12 #include "base/values.h" | 13 |
| 14 using base::DictionaryValue; |
| 15 using base::ListValue; |
| 16 using base::Value; |
13 | 17 |
14 namespace webdriver { | 18 namespace webdriver { |
15 | 19 |
16 SkipParsing* kSkipParsing = NULL; | 20 SkipParsing* kSkipParsing = NULL; |
17 | 21 |
18 std::string GenerateRandomID() { | 22 std::string GenerateRandomID() { |
19 uint64 msb = base::RandUint64(); | 23 uint64 msb = base::RandUint64(); |
20 uint64 lsb = base::RandUint64(); | 24 uint64 lsb = base::RandUint64(); |
21 return base::StringPrintf("%016" PRIx64 "%016" PRIx64, msb, lsb); | 25 return base::StringPrintf("%016" PRIx64 "%016" PRIx64, msb, lsb); |
22 } | 26 } |
23 | 27 |
24 std::string JsonStringify(const Value* value) { | 28 std::string JsonStringify(const Value* value) { |
25 std::string json; | 29 std::string json; |
26 base::JSONWriter::Write(value, false, &json); | 30 base::JSONWriter::Write(value, false, &json); |
27 return json; | 31 return json; |
28 } | 32 } |
29 | 33 |
| 34 namespace { |
| 35 |
| 36 // Truncates the given string to 40 characters, adding an ellipsis if |
| 37 // truncation was necessary. |
| 38 void TruncateString(std::string* data) { |
| 39 if (data->length() > 40) { |
| 40 data->resize(40); |
| 41 data->replace(37, 3, "..."); |
| 42 } |
| 43 } |
| 44 |
| 45 // Truncates all strings contained in the given value. |
| 46 void TruncateContainedStrings(Value* value) { |
| 47 ListValue* list; |
| 48 if (value->IsType(Value::TYPE_DICTIONARY)) { |
| 49 DictionaryValue* dict = static_cast<DictionaryValue*>(value); |
| 50 DictionaryValue::key_iterator key = dict->begin_keys(); |
| 51 for (; key != dict->end_keys(); ++key) { |
| 52 Value* child; |
| 53 if (!dict->GetWithoutPathExpansion(*key, &child)) |
| 54 continue; |
| 55 std::string data; |
| 56 if (child->GetAsString(&data)) { |
| 57 TruncateString(&data); |
| 58 dict->SetWithoutPathExpansion(*key, Value::CreateStringValue(data)); |
| 59 } else { |
| 60 TruncateContainedStrings(child); |
| 61 } |
| 62 } |
| 63 } else if (value->GetAsList(&list)) { |
| 64 for (size_t i = 0; i < list->GetSize(); ++i) { |
| 65 Value* child; |
| 66 if (!list->Get(i, &child)) |
| 67 continue; |
| 68 std::string data; |
| 69 if (child->GetAsString(&data)) { |
| 70 TruncateString(&data); |
| 71 list->Set(i, Value::CreateStringValue(data)); |
| 72 } else { |
| 73 TruncateContainedStrings(child); |
| 74 } |
| 75 } |
| 76 } |
| 77 } |
| 78 |
| 79 } // namespace |
| 80 |
| 81 std::string JsonStringifyForDisplay(const Value* value) { |
| 82 scoped_ptr<Value> copy; |
| 83 if (value->IsType(Value::TYPE_STRING)) { |
| 84 std::string data; |
| 85 value->GetAsString(&data); |
| 86 TruncateString(&data); |
| 87 copy.reset(Value::CreateStringValue(data)); |
| 88 } else { |
| 89 copy.reset(value->DeepCopy()); |
| 90 TruncateContainedStrings(copy.get()); |
| 91 } |
| 92 std::string json; |
| 93 base::JSONWriter::Write(copy.get(), true /* pretty_print */, &json); |
| 94 return json; |
| 95 } |
| 96 |
| 97 const char* GetJsonTypeName(Value::Type type) { |
| 98 switch (type) { |
| 99 case Value::TYPE_NULL: |
| 100 return "null"; |
| 101 case Value::TYPE_BOOLEAN: |
| 102 return "boolean"; |
| 103 case Value::TYPE_INTEGER: |
| 104 return "integer"; |
| 105 case Value::TYPE_DOUBLE: |
| 106 return "double"; |
| 107 case Value::TYPE_STRING: |
| 108 return "string"; |
| 109 case Value::TYPE_BINARY: |
| 110 return "binary"; |
| 111 case Value::TYPE_DICTIONARY: |
| 112 return "dictionary"; |
| 113 case Value::TYPE_LIST: |
| 114 return "list"; |
| 115 } |
| 116 return "unknown"; |
| 117 } |
| 118 |
30 ValueParser::ValueParser() { } | 119 ValueParser::ValueParser() { } |
31 | 120 |
32 ValueParser::~ValueParser() { } | 121 ValueParser::~ValueParser() { } |
33 | 122 |
34 } // namespace webdriver | 123 } // namespace webdriver |
35 | 124 |
36 bool ValueConversionTraits<webdriver::SkipParsing>::SetFromValue( | 125 bool ValueConversionTraits<webdriver::SkipParsing>::SetFromValue( |
37 const base::Value* value, const webdriver::SkipParsing* t) { | 126 const Value* value, const webdriver::SkipParsing* t) { |
38 return true; | 127 return true; |
39 } | 128 } |
40 | 129 |
41 bool ValueConversionTraits<webdriver::SkipParsing>::CanConvert( | 130 bool ValueConversionTraits<webdriver::SkipParsing>::CanConvert( |
42 const base::Value* value) { | 131 const Value* value) { |
43 return true; | 132 return true; |
44 } | 133 } |
OLD | NEW |