Allow overlapping sync and async startup requests
[chromium-blink-merge.git] / chrome / test / automation / value_conversion_util.h
blob8e282d1245b20aaa049023f06fa2e9a5ea7b2948
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_
8 #include <string>
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.
14 template <typename T>
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.
21 template <typename T>
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
27 // parameter.
28 template <typename T>
29 base::ListValue* CreateListValueFrom(const T& t) {
30 base::ListValue* list = new base::ListValue();
31 list->Append(CreateValueFrom(t));
32 return list;
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));
41 return list;
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));
51 return list;
54 // Sets |t| from the first element in the given list. Returns true on success.
55 // |t| will not be modified unless successful.
56 template <typename T>
57 bool SetFromListValue(const base::ListValue* list, T* t) {
58 if (list->GetSize() != 1)
59 return false;
61 if (!ValueConversionTraits<T>::CanConvert(*list->begin()))
62 return false;
64 CHECK(SetFromValue(*list->begin(), t));
65 return true;
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)
72 return false;
74 if (!ValueConversionTraits<T1>::CanConvert(*list->begin()))
75 return false;
76 if (!ValueConversionTraits<T2>::CanConvert(*(list->begin() + 1)))
77 return false;
79 CHECK(SetFromValue(*list->begin(), t1));
80 CHECK(SetFromValue(*(list->begin() + 1), t2));
81 return true;
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)
88 return false;
90 if (!ValueConversionTraits<T1>::CanConvert(*list->begin()))
91 return false;
92 if (!ValueConversionTraits<T2>::CanConvert(*(list->begin() + 1)))
93 return false;
94 if (!ValueConversionTraits<T3>::CanConvert(*(list->begin() + 2)))
95 return false;
97 CHECK(SetFromValue(*list->begin(), t1));
98 CHECK(SetFromValue(*(list->begin() + 1), t2));
99 CHECK(SetFromValue(*(list->begin() + 2), t3));
100 return true;
103 #endif // CHROME_TEST_AUTOMATION_VALUE_CONVERSION_UTIL_H_