TIVI-320: Add dbus-python to repos to support connman-test
[profile/ivi/dbus-python.git] / test / dbus_py_test.c
1 /* Test fixtures for dbus-python, based on _dbus_glib_bindings.
2  *
3  * Copyright (C) 2007 Collabora Ltd. <http://www.collabora.co.uk/>
4  *
5  * Permission is hereby granted, free of charge, to any person
6  * obtaining a copy of this software and associated documentation
7  * files (the "Software"), to deal in the Software without
8  * restriction, including without limitation the rights to use, copy,
9  * modify, merge, publish, distribute, sublicense, and/or sell copies
10  * 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
14  * included in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20  * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23  * DEALINGS IN THE SOFTWARE.
24  */
25
26 #include <Python.h>
27 #include "dbus-python.h"
28
29 PyMODINIT_FUNC initdbus_py_test(void);
30
31 #if defined(__GNUC__)
32 #   if __GNUC__ >= 3
33 #       define UNUSED __attribute__((__unused__))
34 #   else
35 #       define UNUSED /*nothing*/
36 #   endif
37 #else
38 #   define UNUSED /*nothing*/
39 #endif
40
41 static dbus_bool_t
42 dbus_py_test_set_up_conn(DBusConnection *conn UNUSED, void *data UNUSED)
43 {
44     PyErr_SetString(PyExc_ValueError, "Dummy error from UnusableMainLoop");
45     return 0;
46 }
47
48 static dbus_bool_t
49 dbus_py_test_set_up_srv(DBusServer *srv UNUSED, void *data UNUSED)
50 {
51     PyErr_SetString(PyExc_ValueError, "Dummy error from UnusableMainLoop");
52     return 0;
53 }
54
55 static void
56 dbus_py_test_free(void *data UNUSED)
57 {
58 }
59
60 static PyObject *
61 dbus_test_native_mainloop(void)
62 {
63     PyObject *loop = DBusPyNativeMainLoop_New4(dbus_py_test_set_up_conn,
64                                                dbus_py_test_set_up_srv,
65                                                dbus_py_test_free,
66                                                NULL);
67     return loop;
68 }
69
70 static PyObject *
71 UnusableMainLoop (PyObject *always_null UNUSED, PyObject *args, PyObject *kwargs)
72 {
73     PyObject *mainloop, *function, *result;
74     int set_as_default = 0;
75     static char *argnames[] = {"set_as_default", NULL};
76
77     if (PyTuple_Size(args) != 0) {
78         PyErr_SetString(PyExc_TypeError, "UnusableMainLoop() takes no "
79                                          "positional arguments");
80         return NULL;
81     }
82     if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|i", argnames,
83                                      &set_as_default)) {
84         return NULL;
85     }
86
87     mainloop = dbus_test_native_mainloop();
88     if (mainloop && set_as_default) {
89         if (!_dbus_bindings_module) {
90             PyErr_SetString(PyExc_ImportError, "_dbus_bindings not imported");
91             Py_DECREF(mainloop);
92             return NULL;
93         }
94         function = PyObject_GetAttrString(_dbus_bindings_module,
95                                           "set_default_main_loop");
96         if (!function) {
97             Py_DECREF(mainloop);
98             return NULL;
99         }
100         result = PyObject_CallFunctionObjArgs(function, mainloop, NULL);
101         Py_DECREF(function);
102         if (!result) {
103             Py_DECREF(mainloop);
104             return NULL;
105         }
106     }
107     return mainloop;
108 }
109
110 static PyMethodDef module_functions[] = {
111     {"UnusableMainLoop", (PyCFunction)UnusableMainLoop,
112      METH_VARARGS|METH_KEYWORDS, "Return a main loop that fails to attach"},
113     {NULL, NULL, 0, NULL}
114 };
115
116 PyMODINIT_FUNC
117 initdbus_py_test(void)
118 {
119     PyObject *this_module;
120
121     if (import_dbus_bindings("dbus_py_test") < 0) return;
122     this_module = Py_InitModule3 ("dbus_py_test", module_functions, "");
123     if (!this_module) return;
124 }
125
126 /* vim:set ft=c cino< sw=4 sts=4 et: */