Merge branch 'master' of ssh://git.codethink.co.uk/git/atspi-dbus
[platform/core/uifw/at-spi2-atk.git] / tests / cspi / classy-test-suite.c
1 /*
2  * Copyright (C) 2008 Codethink Ltd.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 #include <glib-object.h>
21 #include <string.h>
22 #include <stdio.h>
23
24 #include "classy-test.h"
25 #include "classy-test-suite.h"
26
27 /*---------------------------------------------------------------------------*/
28
29 static gboolean
30 run_test_app(gchar *module_name, gchar *dbus_name)
31 {
32   gboolean result;
33   GPid pid;
34   GError *err = NULL;
35
36   const gchar *test_data_directory;
37   const gchar *test_modules_directory;
38   const gchar *test_atspi_library;
39   const gchar *test_application;
40   gchar *test_module;
41   gchar *command_line;
42
43   test_data_directory = g_getenv("TEST_DATA_DIRECTORY");
44   test_modules_directory = g_getenv("TEST_MODULES_DIRECTORY");
45   test_atspi_library = g_getenv("TEST_ATSPI_LIBRARY");
46   test_application = g_getenv("TEST_APPLICATION");
47
48   test_module = g_build_path("/", test_modules_directory, module_name, NULL); 
49
50   command_line = g_build_path(" ", test_application,
51                                    "--atspi-dbus-name", dbus_name,
52                                    "--test-atspi-library", test_atspi_library,
53                                    "--test-module", test_module,
54                                    "--test-data-directory", test_data_directory,
55                                    NULL); 
56
57   if (!(result = g_spawn_command_line_async(NULL, &err)))
58     {
59       fprintf(stderr, "\nCould not spawn test application - %s", err->message);
60     }
61
62   g_free(test_module);
63   g_free(command_line);
64
65   return result;
66 }
67
68 /*---------------------------------------------------------------------------*/
69
70 static gboolean
71 suite_idle_handler(gpointer data);
72
73 static void
74 suite_finished_handler(gpointer data)
75 {
76         ClassyTestSuite *suite = CLASSY_TEST_SUITE(data);
77         ClassyTest *test = CLASSY_TEST(g_array_index(suite->cases, gpointer, suite->current));
78
79         if ((classy_test_state(test) == CLASSY_TEST_WIN) || suite->cont) {
80                 g_idle_add(suite_idle_handler, suite);
81         }
82         /* TODO If test has failed remember to send signal saying so */
83 }
84
85 static gboolean
86 suite_idle_handler(gpointer data)
87 {
88         ClassyTestSuite *suite = CLASSY_TEST_SUITE(data);
89
90         suite->current++;
91         if (suite->current >= suite->cases->len) {
92                 /* No more tests, check for success or fail */
93                 gboolean succeeded = TRUE;
94                 gint i;
95                 for (i=0; i < suite->cases->len; i++) {
96                         ClassyTest *test;
97                         test = CLASSY_TEST(g_array_index(suite->cases, gpointer, i));
98                         succeeded = succeeded && (classy_test_state(test) == CLASSY_TEST_WIN);
99                 }
100                 if (succeeded == TRUE)
101                         classy_test_pass(CLASSY_TEST(suite));
102                 else
103                         classy_test_fail(CLASSY_TEST(suite), "Sub-test has failed");
104         } else {
105                 /* More tests, run this one*/
106                 ClassyTest *test;
107                 test = CLASSY_TEST(g_array_index(suite->cases, gpointer, suite->current));
108                 g_signal_connect(test, "finished", (GCallback) suite_finished_handler, data);
109                 classy_test_run(test);
110         }
111         return FALSE;
112 }
113
114 static void
115 suite_run(ClassyTest *test)
116 {
117         g_idle_add(suite_idle_handler, test);
118 }
119
120 /*---------------------------------------------------------------------------*/
121
122 static gchar *
123 suite_report(ClassyTest *test)
124 {
125         ClassyTestSuite *suite = CLASSY_TEST_SUITE(test);
126         GString *report;
127         gint i;
128
129         report = g_string_new("");
130
131         switch (classy_test_state(test)) {
132                 case CLASSY_TEST_FAIL:
133                         g_string_printf(report, "FAIL       : %s\n           : %s\n ", test->name);
134                 case CLASSY_TEST_WIN:
135                         g_string_printf(report, "PASS       : %s\n", test->name);
136                 default:
137                         g_string_printf(report, "INCOMPLETE : %s\n", test->name);
138         }
139
140         for (i=0; i < suite->cases->len; i++) {
141                 ClassyTest *subtest = CLASSY_TEST(g_array_index(suite->cases, gpointer, i));
142                 g_string_append_printf(report, "             ");
143                 g_string_append_printf(report, "%s", classy_test_report(subtest));
144         }
145         return g_string_free(report, FALSE);
146 }
147
148 /*---------------------------------------------------------------------------*/
149
150 G_DEFINE_TYPE (ClassyTestSuite, classy_test_suite, TYPE_CLASSY_TEST)
151
152 static void
153 classy_test_suite_finalize (GObject *obj)
154 {
155         ClassyTestSuite *suite = CLASSY_TEST_SUITE(obj);
156         gint i;
157
158         for (i=0; i < suite->cases->len; i++) {
159                 ClassyTest *test;
160                 test = CLASSY_TEST(g_array_index(suite->cases, gpointer, i));
161                 g_object_unref(test);
162         }
163         g_free(suite->data);
164
165         G_OBJECT_CLASS (classy_test_suite_parent_class)->finalize (obj);
166 }
167
168 static void 
169 classy_test_suite_class_init (ClassyTestSuiteClass *klass)
170 {
171         GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
172         ClassyTestClass *test_class = CLASSY_TEST_CLASS(klass);
173
174         gobject_class->finalize = classy_test_suite_finalize;
175
176         test_class->report = suite_report;
177         test_class->run = suite_run;
178 }
179
180 static void
181 classy_test_suite_init (ClassyTestSuite *suite)
182 {
183         suite->cases = g_array_new(FALSE, FALSE, sizeof(gpointer));     
184         suite->current = -1;
185 }
186
187 /*---------------------------------------------------------------------------*/
188
189 ClassyTestSuite *
190 classy_test_suite_new(gchar *name, gint dsize, gboolean cont)
191 {
192         ClassyTestSuite *suite;
193         ClassyTest *test;
194
195         suite = g_object_new(TYPE_CLASSY_TEST_SUITE, NULL);
196         suite->cont = cont;
197         suite->data = g_malloc0(dsize);
198
199         test = CLASSY_TEST(suite);
200         test->name = g_strdup(name);
201         
202         return suite;
203 }
204
205 void
206 classy_test_suite_add(ClassyTestSuite *suite, ClassyTest *test)
207 {
208         g_array_append(suite->cases, test);
209 }
210
211 /*---------------------------------------------------------------------------*/