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 #ifndef CHROME_TEST_AUTOMATION_JAVASCRIPT_MESSAGE_UTILS_H_ | 5 #ifndef CHROME_TEST_AUTOMATION_JAVASCRIPT_MESSAGE_UTILS_H_ |
6 #define CHROME_TEST_AUTOMATION_JAVASCRIPT_MESSAGE_UTILS_H_ | 6 #define CHROME_TEST_AUTOMATION_JAVASCRIPT_MESSAGE_UTILS_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include <string> | 9 #include <string> |
10 #include <vector> | 10 #include <vector> |
11 | 11 |
12 #include "base/json/json_writer.h" | 12 #include "base/json/json_writer.h" |
13 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
14 #include "base/stringprintf.h" | 14 #include "base/stringprintf.h" |
15 #include "base/values.h" | 15 #include "base/values.h" |
16 #include "chrome/test/automation/dom_element_proxy.h" | 16 #include "chrome/test/automation/dom_element_proxy.h" |
17 | 17 #include "chrome/test/automation/value_conversion_traits.h" |
18 // ValueConversionTraits contains functions for creating a value from a | |
19 // type, and setting a type from a value. This is general-purpose and can | |
20 // be moved to a common location if needed. | |
21 template <class T> | |
22 struct ValueConversionTraits { | |
23 }; | |
24 | |
25 template <> | |
26 struct ValueConversionTraits<int> { | |
27 static Value* CreateValue(int t) { | |
28 return Value::CreateIntegerValue(t); | |
29 } | |
30 static bool SetFromValue(Value* value, int* t) { | |
31 return value->GetAsInteger(t); | |
32 } | |
33 }; | |
34 | |
35 template <> | |
36 struct ValueConversionTraits<bool> { | |
37 static Value* CreateValue(bool t) { | |
38 return Value::CreateBooleanValue(t); | |
39 } | |
40 static bool SetFromValue(Value* value, bool* t) { | |
41 return value->GetAsBoolean(t); | |
42 } | |
43 }; | |
44 | |
45 template <> | |
46 struct ValueConversionTraits<std::string> { | |
47 static Value* CreateValue(const std::string& t) { | |
48 return Value::CreateStringValue(t); | |
49 } | |
50 static bool SetFromValue(Value* value, std::string* t) { | |
51 return value->GetAsString(t); | |
52 } | |
53 }; | |
54 | 18 |
55 template <> | 19 template <> |
56 struct ValueConversionTraits<DOMElementProxy::By> { | 20 struct ValueConversionTraits<DOMElementProxy::By> { |
57 typedef DOMElementProxy::By type; | 21 typedef DOMElementProxy::By type; |
58 static Value* CreateValue(const type& t) { | 22 static Value* CreateValueFrom(const type& t) { |
59 DictionaryValue* value = new DictionaryValue(); | 23 DictionaryValue* value = new DictionaryValue(); |
60 std::string by_type; | 24 std::string by_type; |
61 switch (t.type()) { | 25 switch (t.type()) { |
62 case type::TYPE_XPATH: | 26 case type::TYPE_XPATH: |
63 by_type = "xpath"; | 27 by_type = "xpath"; |
64 break; | 28 break; |
65 case type::TYPE_SELECTORS: | 29 case type::TYPE_SELECTORS: |
66 by_type = "selectors"; | 30 by_type = "selectors"; |
67 break; | 31 break; |
68 case type::TYPE_TEXT: | 32 case type::TYPE_TEXT: |
69 by_type = "text"; | 33 by_type = "text"; |
70 break; | 34 break; |
71 default: | 35 default: |
72 NOTREACHED(); | 36 NOTREACHED(); |
73 break; | 37 break; |
74 } | 38 } |
75 value->SetString("type", by_type); | 39 value->SetString("type", by_type); |
76 value->SetString("queryString", t.query()); | 40 value->SetString("queryString", t.query()); |
77 return value; | 41 return value; |
78 } | 42 } |
79 }; | 43 }; |
80 | 44 |
81 template <typename T> | 45 template <typename T> |
82 struct ValueConversionTraits<std::vector<T> > { | 46 struct ValueConversionTraits<std::vector<T> > { |
83 static Value* CreateValue(const std::vector<T>& t) { | 47 static Value* CreateValueFrom(const std::vector<T>& t) { |
84 ListValue* value = new ListValue(); | 48 ListValue* value = new ListValue(); |
85 for (size_t i = 0; i < t.size(); i++) { | 49 for (size_t i = 0; i < t.size(); i++) { |
86 value->Append(ValueConversionTraits<T>::CreateValue(t[i])); | 50 value->Append(ValueConversionTraits<T>::CreateValueFrom(t[i])); |
87 } | 51 } |
88 return value; | 52 return value; |
89 } | 53 } |
90 static bool SetFromValue(Value* value, std::vector<T>* t) { | 54 static bool SetFromValue(const Value* value, std::vector<T>* t) { |
91 if (!value->IsType(Value::TYPE_LIST)) | 55 if (!value->IsType(Value::TYPE_LIST)) |
92 return false; | 56 return false; |
93 | 57 |
94 ListValue* list_value = static_cast<ListValue*>(value); | 58 const ListValue* list_value = static_cast<const ListValue*>(value); |
95 ListValue::const_iterator iter; | 59 ListValue::const_iterator iter; |
96 for (iter = list_value->begin(); iter != list_value->end(); ++iter) { | 60 for (iter = list_value->begin(); iter != list_value->end(); ++iter) { |
| 61 if (!ValueConversionTraits<T>::CanConvert(*iter)) |
| 62 return false; |
| 63 } |
| 64 for (iter = list_value->begin(); iter != list_value->end(); ++iter) { |
97 T inner_value; | 65 T inner_value; |
98 ValueConversionTraits<T>::SetFromValue(*iter, &inner_value); | 66 ValueConversionTraits<T>::SetFromValue(*iter, &inner_value); |
99 t->push_back(inner_value); | 67 t->push_back(inner_value); |
100 } | 68 } |
101 return true; | 69 return true; |
102 } | 70 } |
103 }; | 71 }; |
104 | 72 |
105 namespace javascript_utils { | 73 namespace javascript_utils { |
106 | 74 |
107 // Converts |arg| to a JSON string. | 75 // Converts |arg| to a JSON string. |
108 template <typename T> | 76 template <typename T> |
109 std::string JSONStringify(const T& arg) { | 77 std::string JSONStringify(const T& arg) { |
110 std::string javascript; | 78 std::string javascript; |
111 scoped_ptr<Value> value(ValueConversionTraits<T>::CreateValue(arg)); | 79 scoped_ptr<Value> value(ValueConversionTraits<T>::CreateValueFrom(arg)); |
112 base::JSONWriter::Write(value.get(), false, &javascript); | 80 base::JSONWriter::Write(value.get(), false, &javascript); |
113 return javascript; | 81 return javascript; |
114 } | 82 } |
115 | 83 |
116 // Converts |arg| to a JSON string and returns a string formatted as | 84 // Converts |arg| to a JSON string and returns a string formatted as |
117 // |format| specifies. |format| should only expect string arguments. | 85 // |format| specifies. |format| should only expect string arguments. |
118 template <typename T> | 86 template <typename T> |
119 std::string JavaScriptPrintf(const std::string& format, const T& arg) { | 87 std::string JavaScriptPrintf(const std::string& format, const T& arg) { |
120 return base::StringPrintf(format.c_str(), JSONStringify(arg).c_str()); | 88 return base::StringPrintf(format.c_str(), JSONStringify(arg).c_str()); |
121 } | 89 } |
(...skipping 13 matching lines...) Expand all Loading... |
135 const T2& arg2, const T3& arg3) { | 103 const T2& arg2, const T3& arg3) { |
136 return base::StringPrintf(format.c_str(), | 104 return base::StringPrintf(format.c_str(), |
137 JSONStringify(arg1).c_str(), | 105 JSONStringify(arg1).c_str(), |
138 JSONStringify(arg2).c_str(), | 106 JSONStringify(arg2).c_str(), |
139 JSONStringify(arg3).c_str()); | 107 JSONStringify(arg3).c_str()); |
140 } | 108 } |
141 | 109 |
142 } // namespace javascript_utils | 110 } // namespace javascript_utils |
143 | 111 |
144 #endif // CHROME_TEST_AUTOMATION_JAVASCRIPT_MESSAGE_UTILS_H_ | 112 #endif // CHROME_TEST_AUTOMATION_JAVASCRIPT_MESSAGE_UTILS_H_ |
OLD | NEW |