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
3 // found in the LICENSE file.
5 #ifndef CHROME_TEST_AUTOMATION_VALUE_CONVERSION_UTIL_H_
6 #define CHROME_TEST_AUTOMATION_VALUE_CONVERSION_UTIL_H_
10 #include "base/values.h"
11 #include "chrome/test/automation/value_conversion_traits.h"
13 // Creates a value from the given parameter. Will not return NULL.
15 base::Value
* CreateValueFrom(const T
& t
) {
16 return ValueConversionTraits
<T
>::CreateValueFrom(t
);
19 // Sets |t| from the given value. Returns true on success. |t| will not be
20 // modified unless successful.
22 bool SetFromValue(const base::Value
* value
, T
* t
) {
23 return ValueConversionTraits
<T
>::SetFromValue(value
, t
);
26 // Creates a list value containing the converted value from the the given
29 base::ListValue
* CreateListValueFrom(const T
& t
) {
30 base::ListValue
* list
= new base::ListValue();
31 list
->Append(CreateValueFrom(t
));
35 // Same as above, but with more arguments.
36 template <typename T1
, typename T2
>
37 base::ListValue
* CreateListValueFrom(const T1
& t1
, const T2
& t2
) {
38 base::ListValue
* list
= new base::ListValue();
39 list
->Append(CreateValueFrom(t1
));
40 list
->Append(CreateValueFrom(t2
));
44 // Same as above, but with more arguments.
45 template <typename T1
, typename T2
, typename T3
>
46 base::ListValue
* CreateListValueFrom(const T1
& t1
, const T2
& t2
, const T3
& t3
) {
47 base::ListValue
* list
= new base::ListValue();
48 list
->Append(CreateValueFrom(t1
));
49 list
->Append(CreateValueFrom(t2
));
50 list
->Append(CreateValueFrom(t3
));
54 // Sets |t| from the first element in the given list. Returns true on success.
55 // |t| will not be modified unless successful.
57 bool SetFromListValue(const base::ListValue
* list
, T
* t
) {
58 if (list
->GetSize() != 1)
61 if (!ValueConversionTraits
<T
>::CanConvert(*list
->begin()))
64 CHECK(SetFromValue(*list
->begin(), t
));
68 // Same as above, but with more arguments.
69 template <typename T1
, typename T2
>
70 bool SetFromListValue(const base::ListValue
* list
, T1
* t1
, T2
* t2
) {
71 if (list
->GetSize() != 2)
74 if (!ValueConversionTraits
<T1
>::CanConvert(*list
->begin()))
76 if (!ValueConversionTraits
<T2
>::CanConvert(*(list
->begin() + 1)))
79 CHECK(SetFromValue(*list
->begin(), t1
));
80 CHECK(SetFromValue(*(list
->begin() + 1), t2
));
84 // Same as above, but with more arguments.
85 template <typename T1
, typename T2
, typename T3
>
86 bool SetFromListValue(const base::ListValue
* list
, T1
* t1
, T2
* t2
, T3
* t3
) {
87 if (list
->GetSize() != 3)
90 if (!ValueConversionTraits
<T1
>::CanConvert(*list
->begin()))
92 if (!ValueConversionTraits
<T2
>::CanConvert(*(list
->begin() + 1)))
94 if (!ValueConversionTraits
<T3
>::CanConvert(*(list
->begin() + 2)))
97 CHECK(SetFromValue(*list
->begin(), t1
));
98 CHECK(SetFromValue(*(list
->begin() + 1), t2
));
99 CHECK(SetFromValue(*(list
->begin() + 2), t3
));
103 #endif // CHROME_TEST_AUTOMATION_VALUE_CONVERSION_UTIL_H_