]>
Commit | Line | Data |
---|---|---|
747b0cb4 EH |
1 | /* |
2 | * Test code for qdev global-properties handling | |
3 | * | |
4 | * Copyright (c) 2012 Red Hat Inc. | |
5 | * | |
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
7 | * of this software and associated documentation files (the "Software"), to deal | |
8 | * in the Software without restriction, including without limitation the rights | |
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
10 | * copies of the Software, and to permit persons to whom the Software is | |
11 | * furnished to do so, subject to the following conditions: | |
12 | * | |
13 | * The above copyright notice and this permission notice shall be included in | |
14 | * all copies or substantial portions of the Software. | |
15 | * | |
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | |
19 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
22 | * THE SOFTWARE. | |
23 | */ | |
24 | ||
25 | #include <glib.h> | |
26 | #include <stdint.h> | |
27 | ||
28 | #include "hw/qdev.h" | |
99a0b036 EH |
29 | #include "qom/object.h" |
30 | #include "qapi/visitor.h" | |
747b0cb4 EH |
31 | |
32 | ||
33 | #define TYPE_STATIC_PROPS "static_prop_type" | |
34 | #define STATIC_TYPE(obj) \ | |
35 | OBJECT_CHECK(MyType, (obj), TYPE_STATIC_PROPS) | |
36 | ||
37 | #define PROP_DEFAULT 100 | |
38 | ||
39 | typedef struct MyType { | |
40 | DeviceState parent_obj; | |
41 | ||
42 | uint32_t prop1; | |
43 | uint32_t prop2; | |
44 | } MyType; | |
45 | ||
46 | static Property static_props[] = { | |
47 | DEFINE_PROP_UINT32("prop1", MyType, prop1, PROP_DEFAULT), | |
48 | DEFINE_PROP_UINT32("prop2", MyType, prop2, PROP_DEFAULT), | |
49 | DEFINE_PROP_END_OF_LIST() | |
50 | }; | |
51 | ||
52 | static void static_prop_class_init(ObjectClass *oc, void *data) | |
53 | { | |
54 | DeviceClass *dc = DEVICE_CLASS(oc); | |
55 | ||
56 | dc->realize = NULL; | |
57 | dc->props = static_props; | |
58 | } | |
59 | ||
60 | static const TypeInfo static_prop_type = { | |
61 | .name = TYPE_STATIC_PROPS, | |
62 | .parent = TYPE_DEVICE, | |
63 | .instance_size = sizeof(MyType), | |
64 | .class_init = static_prop_class_init, | |
65 | }; | |
66 | ||
67 | /* Test simple static property setting to default value */ | |
68 | static void test_static_prop(void) | |
69 | { | |
70 | MyType *mt; | |
71 | ||
72 | mt = STATIC_TYPE(object_new(TYPE_STATIC_PROPS)); | |
73 | qdev_init_nofail(DEVICE(mt)); | |
74 | ||
75 | g_assert_cmpuint(mt->prop1, ==, PROP_DEFAULT); | |
76 | } | |
77 | ||
78 | /* Test setting of static property using global properties */ | |
79 | static void test_static_globalprop(void) | |
80 | { | |
81 | MyType *mt; | |
82 | static GlobalProperty props[] = { | |
83 | { TYPE_STATIC_PROPS, "prop1", "200" }, | |
84 | {} | |
85 | }; | |
86 | ||
87 | qdev_prop_register_global_list(props); | |
88 | ||
89 | mt = STATIC_TYPE(object_new(TYPE_STATIC_PROPS)); | |
90 | qdev_init_nofail(DEVICE(mt)); | |
91 | ||
92 | g_assert_cmpuint(mt->prop1, ==, 200); | |
93 | g_assert_cmpuint(mt->prop2, ==, PROP_DEFAULT); | |
94 | } | |
95 | ||
99a0b036 EH |
96 | #define TYPE_DYNAMIC_PROPS "dynamic-prop-type" |
97 | #define DYNAMIC_TYPE(obj) \ | |
98 | OBJECT_CHECK(MyType, (obj), TYPE_DYNAMIC_PROPS) | |
99 | ||
100 | static void prop1_accessor(Object *obj, | |
101 | Visitor *v, | |
102 | void *opaque, | |
103 | const char *name, | |
104 | Error **errp) | |
105 | { | |
106 | MyType *mt = DYNAMIC_TYPE(obj); | |
107 | ||
108 | visit_type_uint32(v, &mt->prop1, name, errp); | |
109 | } | |
110 | ||
111 | static void prop2_accessor(Object *obj, | |
112 | Visitor *v, | |
113 | void *opaque, | |
114 | const char *name, | |
115 | Error **errp) | |
116 | { | |
117 | MyType *mt = DYNAMIC_TYPE(obj); | |
118 | ||
119 | visit_type_uint32(v, &mt->prop2, name, errp); | |
120 | } | |
121 | ||
122 | static void dynamic_instance_init(Object *obj) | |
123 | { | |
124 | object_property_add(obj, "prop1", "uint32", prop1_accessor, prop1_accessor, | |
125 | NULL, NULL, NULL); | |
126 | object_property_add(obj, "prop2", "uint32", prop2_accessor, prop2_accessor, | |
127 | NULL, NULL, NULL); | |
128 | } | |
129 | ||
130 | static void dynamic_class_init(ObjectClass *oc, void *data) | |
131 | { | |
132 | DeviceClass *dc = DEVICE_CLASS(oc); | |
133 | ||
134 | dc->realize = NULL; | |
135 | } | |
136 | ||
137 | ||
138 | static const TypeInfo dynamic_prop_type = { | |
139 | .name = TYPE_DYNAMIC_PROPS, | |
140 | .parent = TYPE_DEVICE, | |
141 | .instance_size = sizeof(MyType), | |
142 | .instance_init = dynamic_instance_init, | |
143 | .class_init = dynamic_class_init, | |
144 | }; | |
145 | ||
146 | /* Test setting of static property using global properties */ | |
147 | static void test_dynamic_globalprop(void) | |
148 | { | |
149 | MyType *mt; | |
150 | static GlobalProperty props[] = { | |
151 | { TYPE_DYNAMIC_PROPS, "prop1", "101" }, | |
152 | { TYPE_DYNAMIC_PROPS, "prop2", "102" }, | |
153 | {} | |
154 | }; | |
155 | ||
156 | qdev_prop_register_global_list(props); | |
157 | ||
158 | mt = DYNAMIC_TYPE(object_new(TYPE_DYNAMIC_PROPS)); | |
159 | qdev_init_nofail(DEVICE(mt)); | |
160 | ||
161 | g_assert_cmpuint(mt->prop1, ==, 101); | |
162 | g_assert_cmpuint(mt->prop2, ==, 102); | |
163 | } | |
164 | ||
747b0cb4 EH |
165 | int main(int argc, char **argv) |
166 | { | |
167 | g_test_init(&argc, &argv, NULL); | |
168 | ||
169 | module_call_init(MODULE_INIT_QOM); | |
170 | type_register_static(&static_prop_type); | |
99a0b036 | 171 | type_register_static(&dynamic_prop_type); |
747b0cb4 EH |
172 | |
173 | g_test_add_func("/qdev/properties/static/default", test_static_prop); | |
174 | g_test_add_func("/qdev/properties/static/global", test_static_globalprop); | |
99a0b036 | 175 | g_test_add_func("/qdev/properties/dynamic/global", test_dynamic_globalprop); |
747b0cb4 EH |
176 | |
177 | g_test_run(); | |
178 | ||
179 | return 0; | |
180 | } |