Merge branch 'master' of git+ssh://git.codethink.co.uk/git/atspi-dbus
[platform/core/uifw/at-spi2-atk.git] / tests / cspi / classy-test.c
1 /* 
2  * Classy Test - Terrible framework for testing asyncronous interface
3  *
4  * Copyright (C) 2008 Codethink Ltd
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  */
21
22 #include <glib-object.h>
23
24 #include "classy-test.h"
25
26 static gchar *
27 classy_test_report(ClassyTest *test)
28 {
29         switch (classy_test_state(test)) {
30                 case CLASSY_TEST_FAIL:
31                         return g_strdup_printf("FAIL       : %s - %s\n ", test->name, test->failm);
32                 case CLASSY_TEST_WIN:
33                         return g_strdup_printf("PASS       : %s\n", test->name);
34                 default:
35                         return g_strdup_printf("INCOMPLETE : %s\n", test->name);
36         }
37 }
38
39 /*---------------------------------------------------------------------------*/
40
41 static void
42 classy_test_run(ClassyTest *test)
43 {
44         test->tstate = CLASSY_TEST_IN_PROGRESS;
45         (test->entry)(test, test->data);
46 }
47
48 /*---------------------------------------------------------------------------*/
49
50 G_DEFINE_TYPE (ClassyTest, classy_test, G_TYPE_OBJECT)
51
52 static void
53 classy_test_finalize (GObject *obj)
54 {
55         ClassyTest *test = CLASSY_TEST(obj);
56
57         g_free(test->name);
58         if (test->failm) {
59                 g_free(test->failm);
60                 test->failm = NULL;
61         }
62         G_OBJECT_CLASS (classy_test_parent_class)->finalize (obj);
63 }
64
65 static void 
66 classy_test_class_init (ClassyTestClass *klass)
67 {
68         GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
69
70         gobject_class->finalize = classy_test_finalize;
71
72         klass->report = classy_test_report;
73         klass->run = classy_test_run;
74
75         /*Signals*/
76         klass->finished = g_signal_newv("finished",
77                                         TYPE_CLASSY_TEST,
78                                         G_SIGNAL_RUN_LAST | G_SIGNAL_NO_HOOKS,
79                                         NULL,
80                                         NULL,
81                                         NULL,
82                                         g_cclosure_marshal_VOID__VOID,
83                                         G_TYPE_NONE,
84                                         0,
85                                         NULL);
86 }
87
88 static void
89 classy_test_init (ClassyTest *test)
90 {
91         test->failm = NULL;
92         test->tstate = CLASSY_TEST_NOT_STARTED;
93 }
94
95 /*---------------------------------------------------------------------------*/
96
97 ClassyTest *
98 classy_test_new(gchar *name,
99                 void (*entry) (ClassyTest*, gpointer data),
100                 gint istate,
101                 gpointer data)
102 {
103         ClassyTest *test;
104
105         test = g_object_new(TYPE_CLASSY_TEST, NULL);
106
107         test->name = g_strdup(name);
108         test->entry = entry;
109         test->data = data;
110         
111         return test;
112 }
113
114 /*---------------------------------------------------------------------------*/
115
116 void
117 classy_test_pass(ClassyTest *test)
118 {
119         test->tstate = CLASSY_TEST_WIN;
120         g_signal_emit (test, CLASSY_TEST_CLASS(test)->finished, 0, NULL);
121 }
122
123 /*---------------------------------------------------------------------------*/
124
125 void
126 classy_test_fail(ClassyTest *test, gchar *fmt, ...)
127 {
128         va_list args;
129
130         va_start(args, fmt);
131         test->failm = g_strdup_vprintf(fmt, args);
132         va_end(args);
133         test->tstate = CLASSY_TEST_FAIL;
134         g_signal_emit (test, CLASSY_TEST_CLASS(test)->finished, 0, NULL);
135 }
136
137 /*---------------------------------------------------------------------------*/
138
139 gint
140 classy_test_state(ClassyTest *test)
141 {
142         return test->tstate;
143 }
144
145 /*---------------------------------------------------------------------------*/