ui/cocoa: Constify qkeycode translation arrays
[qemu/rayw.git] / tests / unit / test-qmp-event.c
blobd58c3b78f2088c8609621ab75ee1207c91612bb9
1 /*
2 * qapi event unit-tests.
4 * Copyright (c) 2014 Wenchao Xia
6 * Authors:
7 * Wenchao Xia <wenchaoqemu@gmail.com>
9 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
10 * See the COPYING.LIB file in the top-level directory.
14 #include "qemu/osdep.h"
16 #include "qemu-common.h"
17 #include "qapi/compat-policy.h"
18 #include "qapi/error.h"
19 #include "qapi/qmp/qbool.h"
20 #include "qapi/qmp/qdict.h"
21 #include "qapi/qmp/qjson.h"
22 #include "qapi/qmp/qnum.h"
23 #include "qapi/qmp/qstring.h"
24 #include "qapi/qmp-event.h"
25 #include "test-qapi-events.h"
26 #include "test-qapi-emit-events.h"
28 typedef struct TestEventData {
29 QDict *expect;
30 bool emitted;
31 } TestEventData;
33 TestEventData *test_event_data;
34 static GMutex test_event_lock;
36 void test_qapi_event_emit(test_QAPIEvent event, QDict *d)
38 QDict *t;
39 int64_t s, ms;
41 /* Verify that we have timestamp, then remove it to compare other fields */
42 t = qdict_get_qdict(d, "timestamp");
43 g_assert(t);
44 s = qdict_get_try_int(t, "seconds", -2);
45 ms = qdict_get_try_int(t, "microseconds", -2);
46 if (s == -1) {
47 g_assert(ms == -1);
48 } else {
49 g_assert(s >= 0);
50 g_assert(ms >= 0 && ms <= 999999);
52 g_assert(qdict_size(t) == 2);
54 qdict_del(d, "timestamp");
56 g_assert(qobject_is_equal(QOBJECT(d), QOBJECT(test_event_data->expect)));
57 test_event_data->emitted = true;
60 static void event_prepare(TestEventData *data,
61 const void *unused)
63 /* Global variable test_event_data was used to pass the expectation, so
64 test cases can't be executed at same time. */
65 g_mutex_lock(&test_event_lock);
66 test_event_data = data;
69 static void event_teardown(TestEventData *data,
70 const void *unused)
72 test_event_data = NULL;
73 g_mutex_unlock(&test_event_lock);
76 static void event_test_add(const char *testpath,
77 void (*test_func)(TestEventData *data,
78 const void *user_data))
80 g_test_add(testpath, TestEventData, NULL, event_prepare, test_func,
81 event_teardown);
85 /* Test cases */
87 static void test_event_a(TestEventData *data,
88 const void *unused)
90 data->expect = qdict_from_jsonf_nofail("{ 'event': 'EVENT_A' }");
91 qapi_event_send_event_a();
92 g_assert(data->emitted);
93 qobject_unref(data->expect);
96 static void test_event_b(TestEventData *data,
97 const void *unused)
99 data->expect = qdict_from_jsonf_nofail("{ 'event': 'EVENT_B' }");
100 qapi_event_send_event_b();
101 g_assert(data->emitted);
102 qobject_unref(data->expect);
105 static void test_event_c(TestEventData *data,
106 const void *unused)
108 UserDefOne b = { .integer = 2, .string = (char *)"test1" };
110 data->expect = qdict_from_jsonf_nofail(
111 "{ 'event': 'EVENT_C', 'data': {"
112 " 'a': 1, 'b': { 'integer': 2, 'string': 'test1' }, 'c': 'test2' } }");
113 qapi_event_send_event_c(true, 1, true, &b, "test2");
114 g_assert(data->emitted);
115 qobject_unref(data->expect);
118 /* Complex type */
119 static void test_event_d(TestEventData *data,
120 const void *unused)
122 UserDefOne struct1 = {
123 .integer = 2, .string = (char *)"test1",
124 .has_enum1 = true, .enum1 = ENUM_ONE_VALUE1,
126 EventStructOne a = {
127 .struct1 = &struct1,
128 .string = (char *)"test2",
129 .has_enum2 = true,
130 .enum2 = ENUM_ONE_VALUE2,
133 data->expect = qdict_from_jsonf_nofail(
134 "{ 'event': 'EVENT_D', 'data': {"
135 " 'a': {"
136 " 'struct1': { 'integer': 2, 'string': 'test1', 'enum1': 'value1' },"
137 " 'string': 'test2', 'enum2': 'value2' },"
138 " 'b': 'test3', 'enum3': 'value3' } }");
139 qapi_event_send_event_d(&a, "test3", false, NULL, true, ENUM_ONE_VALUE3);
140 g_assert(data->emitted);
141 qobject_unref(data->expect);
144 static void test_event_deprecated(TestEventData *data, const void *unused)
146 data->expect = qdict_from_jsonf_nofail("{ 'event': 'TEST_EVENT_FEATURES1' }");
148 memset(&compat_policy, 0, sizeof(compat_policy));
150 qapi_event_send_test_event_features1();
151 g_assert(data->emitted);
153 compat_policy.has_deprecated_output = true;
154 compat_policy.deprecated_output = COMPAT_POLICY_OUTPUT_HIDE;
155 data->emitted = false;
156 qapi_event_send_test_event_features1();
157 g_assert(!data->emitted);
159 qobject_unref(data->expect);
162 static void test_event_deprecated_data(TestEventData *data, const void *unused)
164 memset(&compat_policy, 0, sizeof(compat_policy));
166 data->expect = qdict_from_jsonf_nofail("{ 'event': 'TEST_EVENT_FEATURES0',"
167 " 'data': { 'foo': 42 } }");
168 qapi_event_send_test_event_features0(42);
169 g_assert(data->emitted);
171 qobject_unref(data->expect);
173 compat_policy.has_deprecated_output = true;
174 compat_policy.deprecated_output = COMPAT_POLICY_OUTPUT_HIDE;
175 data->expect = qdict_from_jsonf_nofail("{ 'event': 'TEST_EVENT_FEATURES0' }");
176 qapi_event_send_test_event_features0(42);
177 g_assert(data->emitted);
179 qobject_unref(data->expect);
182 int main(int argc, char **argv)
184 g_test_init(&argc, &argv, NULL);
186 event_test_add("/event/event_a", test_event_a);
187 event_test_add("/event/event_b", test_event_b);
188 event_test_add("/event/event_c", test_event_c);
189 event_test_add("/event/event_d", test_event_d);
190 event_test_add("/event/deprecated", test_event_deprecated);
191 event_test_add("/event/deprecated_data", test_event_deprecated_data);
192 g_test_run();
194 return 0;