1 | /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */ |
---|
2 | /* |
---|
3 | * test-dialog.c: |
---|
4 | * |
---|
5 | * This program is free software; you can redistribute it and/or |
---|
6 | * modify it under the terms of the GNU Library General Public License |
---|
7 | * as published by the Free Software Foundation; either version 2 of |
---|
8 | * the License, or (at your option) any later version. |
---|
9 | * |
---|
10 | * This program is distributed in the hope that it will be useful, |
---|
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
13 | * GNU Library General Public License for more details. |
---|
14 | * |
---|
15 | * You should have received a copy of the GNU Library General Public |
---|
16 | * License along with this program; if not, write to the Free Software |
---|
17 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
---|
18 | * |
---|
19 | * Authors: |
---|
20 | * Chema Celorio <chema@ximian.com> |
---|
21 | * |
---|
22 | * Copyright (C) 2002-2003 Ximian Inc. |
---|
23 | * |
---|
24 | */ |
---|
25 | |
---|
26 | #include "config.h" |
---|
27 | |
---|
28 | #include <popt.h> |
---|
29 | #include <string.h> |
---|
30 | #include <unistd.h> |
---|
31 | #include <signal.h> |
---|
32 | #include <stdlib.h> |
---|
33 | |
---|
34 | #include <gtk/gtk.h> |
---|
35 | #include <libgnomeprint/gnome-print-config.h> |
---|
36 | #include <libgnomeprintui/gnome-print-dialog.h> |
---|
37 | #include <libgnomeprintui/gnome-print-dialog-private.h> |
---|
38 | #include <libgnomeprintui/gnome-printer-selector.h> |
---|
39 | #include <libgnomeprintui/gpaui/gpa-tree-viewer.h> |
---|
40 | #include <libgnomeprintui/gpaui/gpa-printer-selector.h> |
---|
41 | #include <libgnomeprint/private/gnome-print-config-private.h> |
---|
42 | |
---|
43 | #define MAGIC_NUMBER -100 |
---|
44 | |
---|
45 | gboolean option_list_tests; |
---|
46 | gint num_test = MAGIC_NUMBER; |
---|
47 | gchar * num_test_str = NULL; |
---|
48 | gboolean debug = FALSE; |
---|
49 | gboolean tree = FALSE; |
---|
50 | gint callback_count = 0; |
---|
51 | |
---|
52 | typedef enum { |
---|
53 | TEST_RETVAL_CRASH = -3, |
---|
54 | TEST_RETVAL_ERROR = -2, |
---|
55 | TEST_RETVAL_BAD_PARAMETERS = -1, |
---|
56 | TEST_RETVAL_SUCCESS = 0, |
---|
57 | TEST_RETVAL_SUCCESS_LAST = 99, |
---|
58 | } TestRetval; |
---|
59 | |
---|
60 | poptContext popt; |
---|
61 | |
---|
62 | static struct poptOption options[] = { |
---|
63 | { "num", '\0', POPT_ARG_STRING, &num_test_str, 0, |
---|
64 | "Test number to run", "#"}, |
---|
65 | { "list-tests", '\0', POPT_ARG_NONE, &option_list_tests, 0, |
---|
66 | "List on the console the description of the test cases", NULL}, |
---|
67 | { "tree", '\0', POPT_ARG_NONE, &tree, 0, |
---|
68 | "Show the tree of GPANodes", NULL}, |
---|
69 | { "debug", '\0', POPT_ARG_NONE, &debug, 0, |
---|
70 | "Print debugging output", NULL}, |
---|
71 | POPT_AUTOHELP |
---|
72 | { NULL } |
---|
73 | }; |
---|
74 | |
---|
75 | typedef struct _TestCase TestCase; |
---|
76 | |
---|
77 | struct _TestCase { |
---|
78 | TestRetval (*function) (void); |
---|
79 | const guchar *description; |
---|
80 | }; |
---|
81 | |
---|
82 | #define max_num_test ((sizeof (test_cases) / sizeof (test_cases[0]))-1) |
---|
83 | |
---|
84 | static TestRetval test_simple (void); |
---|
85 | static TestRetval test_show (void); |
---|
86 | static TestRetval test_dialog_flags (void); |
---|
87 | static TestRetval test_paper_changes (void); |
---|
88 | static TestRetval test_printer_changes (void); |
---|
89 | static TestRetval test_multiple (void); |
---|
90 | static TestRetval test_print_ps (void); |
---|
91 | static TestRetval test_print_multiple_ps (void); |
---|
92 | static TestRetval test_print_pdf (void); |
---|
93 | static TestRetval test_print_multiple_pdf (void); |
---|
94 | static TestRetval test_print_multiple (void); |
---|
95 | static TestRetval test_paper_changes_print (void); |
---|
96 | |
---|
97 | static const TestCase test_cases[] = { |
---|
98 | { &test_simple, "Simple checks"}, |
---|
99 | { &test_show, "Create and show a GnomePrintDialog"}, |
---|
100 | { &test_dialog_flags, "Shows dialogs with different flag options"}, |
---|
101 | { &test_paper_changes, "Change paper parameters and monitor dialog for changes"}, |
---|
102 | { &test_printer_changes, "Change the selected printer"}, |
---|
103 | { &test_multiple, "Create and destroy multiple GnomePrintDialogs."}, |
---|
104 | { &test_print_ps, "Print to PS"}, |
---|
105 | { &test_print_multiple_ps, "Print multiple times to PS with the same GnomePrintConfig"}, |
---|
106 | { &test_print_pdf, "Print to PDF"}, |
---|
107 | { &test_print_multiple_pdf, "Print multiple times to PDF with the same GnomePrintConfig"}, |
---|
108 | { &test_print_multiple, "Print multiple times alternating between PS & PDF"}, |
---|
109 | { &test_paper_changes_print, "Change paper parameters and print"}, |
---|
110 | }; |
---|
111 | |
---|
112 | /* Helper functions */ |
---|
113 | static gint |
---|
114 | test_dialog_get_num_pages (GtkWidget *dialog) |
---|
115 | { |
---|
116 | return g_list_length (GTK_NOTEBOOK (GNOME_PRINT_DIALOG (dialog)->notebook)->children); |
---|
117 | } |
---|
118 | |
---|
119 | static void |
---|
120 | test_dialog_show_page (GtkWidget *dialog, gint page) |
---|
121 | { |
---|
122 | gtk_notebook_set_current_page (GTK_NOTEBOOK (GNOME_PRINT_DIALOG (dialog)->notebook), page); |
---|
123 | |
---|
124 | } |
---|
125 | |
---|
126 | /** |
---|
127 | * test_pos_window: |
---|
128 | * @window: |
---|
129 | * |
---|
130 | * Position de window so that it doens't overlap with other windows |
---|
131 | * this allows us to see several windows on the screen |
---|
132 | **/ |
---|
133 | static void |
---|
134 | test_pos_window (GtkWidget *window) |
---|
135 | { |
---|
136 | #define PANEL_HEIGHT 40 |
---|
137 | #define TOP_BOTTOM_FRAME 36 |
---|
138 | #define RIGHT_LEFT_FRAME 16 |
---|
139 | static gint x = 0; |
---|
140 | static gint y = PANEL_HEIGHT; |
---|
141 | static gint new_x = 0; |
---|
142 | gint width; |
---|
143 | gint height; |
---|
144 | |
---|
145 | gtk_window_get_size (GTK_WINDOW (window), &width, &height); |
---|
146 | |
---|
147 | if ((y + height) > gdk_screen_height ()) { |
---|
148 | x += new_x + RIGHT_LEFT_FRAME; |
---|
149 | new_x = 0; |
---|
150 | y = PANEL_HEIGHT; |
---|
151 | } |
---|
152 | |
---|
153 | if (x + width > gdk_screen_width ()) |
---|
154 | x = 0; |
---|
155 | if (y + height > gdk_screen_height ()) |
---|
156 | y = 0; |
---|
157 | |
---|
158 | new_x = (width > new_x) ? width : new_x; |
---|
159 | |
---|
160 | gtk_window_move (GTK_WINDOW (window), x, y); |
---|
161 | y += height + TOP_BOTTOM_FRAME; |
---|
162 | } |
---|
163 | |
---|
164 | static GtkWidget * |
---|
165 | test_dialog_show_all_pages (GnomePrintConfig *config) |
---|
166 | { |
---|
167 | GnomePrintJob *job; |
---|
168 | GtkWidget *dialog; |
---|
169 | |
---|
170 | job = gnome_print_job_new (config); |
---|
171 | dialog = gnome_print_dialog_new (job, "Sample Print Dialog", |
---|
172 | GNOME_PRINT_DIALOG_RANGE | GNOME_PRINT_DIALOG_COPIES); |
---|
173 | gtk_widget_show (dialog); |
---|
174 | test_pos_window (dialog); |
---|
175 | if (test_dialog_get_num_pages (dialog) != 3) { |
---|
176 | g_print ("Invalid number of pages\n"); |
---|
177 | exit (TEST_RETVAL_ERROR); |
---|
178 | } |
---|
179 | |
---|
180 | job = gnome_print_job_new (config); |
---|
181 | dialog = gnome_print_dialog_new (job, "Sample Print Dialog", |
---|
182 | GNOME_PRINT_DIALOG_RANGE | GNOME_PRINT_DIALOG_COPIES); |
---|
183 | gtk_widget_show (dialog); |
---|
184 | test_dialog_show_page (dialog, 1); |
---|
185 | test_pos_window (dialog); |
---|
186 | if (test_dialog_get_num_pages (dialog) != 3) { |
---|
187 | g_print ("Invalid number of pages\n"); |
---|
188 | exit (TEST_RETVAL_ERROR); |
---|
189 | } |
---|
190 | |
---|
191 | job = gnome_print_job_new (config); |
---|
192 | dialog = gnome_print_dialog_new (job, "Sample Print Dialog", |
---|
193 | GNOME_PRINT_DIALOG_RANGE | GNOME_PRINT_DIALOG_COPIES); |
---|
194 | gtk_widget_show (dialog); |
---|
195 | test_pos_window (dialog); |
---|
196 | test_dialog_show_page (dialog, 2); |
---|
197 | if (test_dialog_get_num_pages (dialog) != 3) { |
---|
198 | g_print ("Invalid number of pages\n"); |
---|
199 | exit (TEST_RETVAL_ERROR); |
---|
200 | } |
---|
201 | |
---|
202 | return dialog; |
---|
203 | } |
---|
204 | |
---|
205 | guint tag = 0; |
---|
206 | |
---|
207 | static gboolean |
---|
208 | test_main_quit_real (void) |
---|
209 | { |
---|
210 | gtk_main_quit (); |
---|
211 | return FALSE; |
---|
212 | } |
---|
213 | |
---|
214 | static gboolean |
---|
215 | test_main_quit (void) |
---|
216 | { |
---|
217 | gtk_timeout_remove (tag); |
---|
218 | gtk_idle_add ((GtkFunction) test_main_quit_real, NULL); |
---|
219 | return TRUE; |
---|
220 | } |
---|
221 | |
---|
222 | static void |
---|
223 | test_run (gint msecs) |
---|
224 | { |
---|
225 | tag = gtk_timeout_add (msecs, (GSourceFunc) test_main_quit, NULL); |
---|
226 | gtk_main (); |
---|
227 | return; |
---|
228 | } |
---|
229 | |
---|
230 | static void |
---|
231 | increment_callback_count (void) |
---|
232 | { |
---|
233 | callback_count++; |
---|
234 | } |
---|
235 | |
---|
236 | /* Tests */ |
---|
237 | static TestRetval |
---|
238 | test_printer_changes (void) |
---|
239 | { |
---|
240 | GnomePrintConfig *config; |
---|
241 | GPANode *node, *printers, *child; |
---|
242 | GSList *list, *l; |
---|
243 | GtkWidget *dialog; |
---|
244 | GtkWidget *option_menu; |
---|
245 | gint len; |
---|
246 | |
---|
247 | config = gnome_print_config_default (); |
---|
248 | |
---|
249 | dialog = test_dialog_show_all_pages (config); |
---|
250 | |
---|
251 | node = GNOME_PRINT_CONFIG_NODE (config); |
---|
252 | g_return_val_if_fail (GPA_IS_NODE (node), TEST_RETVAL_ERROR); |
---|
253 | printers = gpa_node_get_child_from_path (node, "Globals.Printers"); |
---|
254 | g_return_val_if_fail (GPA_IS_NODE (printers), TEST_RETVAL_ERROR); |
---|
255 | |
---|
256 | /* Make sure the gtkoptionmenu changes */ |
---|
257 | callback_count = 0; |
---|
258 | option_menu = GPA_PRINTER_SELECTOR (GNOME_PRINTER_SELECTOR (GNOME_PRINT_DIALOG (dialog)->printer)->printers)->menu; |
---|
259 | g_return_val_if_fail (GTK_IS_OPTION_MENU (option_menu), TEST_RETVAL_ERROR); |
---|
260 | g_signal_connect (G_OBJECT (option_menu), "changed", |
---|
261 | (GCallback) increment_callback_count, NULL); |
---|
262 | |
---|
263 | test_run (1000); |
---|
264 | |
---|
265 | /* Create list */ |
---|
266 | list = NULL; |
---|
267 | child = gpa_node_get_child (printers, NULL); |
---|
268 | for (; child != NULL; child = gpa_node_get_child (printers, child)) |
---|
269 | list = g_slist_prepend (list, child); |
---|
270 | |
---|
271 | l = list; |
---|
272 | while (l) { |
---|
273 | if (!gnome_print_config_set (config, "Printer", gpa_node_id (l->data))) { |
---|
274 | g_print ("Could not set the Printer to %s\n", gpa_node_id (l->data)); |
---|
275 | return TEST_RETVAL_ERROR; |
---|
276 | } |
---|
277 | test_run (1000); |
---|
278 | l = l->next; |
---|
279 | } |
---|
280 | |
---|
281 | len = g_slist_length (list); |
---|
282 | |
---|
283 | if (callback_count < len) { |
---|
284 | g_warning ("The printers GtkOptionMenu didn't changed " |
---|
285 | "as expected, expected: %d changed: %d\n", |
---|
286 | len, callback_count); |
---|
287 | return TEST_RETVAL_ERROR; |
---|
288 | } |
---|
289 | if (callback_count > len) { |
---|
290 | g_warning ("The printers GtkOptionMenu didn't changed " |
---|
291 | "as expected, expected: %d changed: %d\n", |
---|
292 | len, callback_count); |
---|
293 | #ifdef __GNUC__ |
---|
294 | //#warning Disabled test |
---|
295 | #endif |
---|
296 | return TEST_RETVAL_SUCCESS; |
---|
297 | return TEST_RETVAL_ERROR; |
---|
298 | } |
---|
299 | |
---|
300 | g_slist_free (list); |
---|
301 | |
---|
302 | g_print ("Callback count %d\n", callback_count); |
---|
303 | |
---|
304 | return TEST_RETVAL_SUCCESS; |
---|
305 | } |
---|
306 | |
---|
307 | static TestRetval |
---|
308 | test_paper_changes (void) |
---|
309 | { |
---|
310 | GnomePrintConfig *config; |
---|
311 | const gchar *papers [] = { |
---|
312 | "USLetter", "USLegal", "Executive", "A0", "A1", "A2", "A3", "A4", "A5", |
---|
313 | "A6", "A7", "A8", "A9", "A10", "B0", "B1", "B2", "B3", "B4", "B5", "B6", |
---|
314 | "B7", "B8", "B9", "B10", "C0", "C1", "C2", "C3", "C4", "C5", "C6", "C7", |
---|
315 | "C8", "C9", "C10", "A4_3", "A4_4", "A4_8", "A3_4", "A5_3", "DL", "C6_C5", |
---|
316 | "Envelope_No10", "Envelope_6x9", "USLetter"}; |
---|
317 | const gchar *orientations [] = {"R0", "R90", "R180", "R270", "R0"}; |
---|
318 | const gchar *layouts [] = {"Plain", "2_1", "4_1", "I2_1", "IM2_1", "Plain"}; |
---|
319 | gint max, i; |
---|
320 | |
---|
321 | config = gnome_print_config_default (); |
---|
322 | test_dialog_show_all_pages (config); |
---|
323 | |
---|
324 | gnome_print_config_set (config, "Printer", "GENERIC"); |
---|
325 | |
---|
326 | test_run (1000); |
---|
327 | |
---|
328 | max = sizeof (papers) / sizeof (gchar *); |
---|
329 | for (i = 0; i < max; i++) { |
---|
330 | if (!gnome_print_config_set (config, GNOME_PRINT_KEY_PAPER_SIZE, papers[i])) { |
---|
331 | g_print ("Could not set paper size to %s\n", papers[i]); |
---|
332 | return TEST_RETVAL_ERROR; |
---|
333 | } |
---|
334 | test_run (50); |
---|
335 | } |
---|
336 | |
---|
337 | max = sizeof (orientations) / sizeof (gchar *); |
---|
338 | for (i = 0; i < max; i++) { |
---|
339 | if (!gnome_print_config_set (config, GNOME_PRINT_KEY_PAGE_ORIENTATION, orientations[i])) { |
---|
340 | g_print ("Could not set the page orientation to %s\n", orientations[i]); |
---|
341 | return TEST_RETVAL_ERROR; |
---|
342 | } |
---|
343 | test_run (150); |
---|
344 | } |
---|
345 | for (i = 0; i < max; i++) { |
---|
346 | if (!gnome_print_config_set (config, GNOME_PRINT_KEY_PAPER_ORIENTATION, orientations[i])) { |
---|
347 | g_print ("Could not set the paper orientation to %s\n", orientations[i]); |
---|
348 | return TEST_RETVAL_ERROR; |
---|
349 | } |
---|
350 | test_run (150); |
---|
351 | } |
---|
352 | |
---|
353 | max = sizeof (layouts) / sizeof (gchar *); |
---|
354 | for (i = 0; i < max; i++) { |
---|
355 | if (!gnome_print_config_set (config, GNOME_PRINT_KEY_LAYOUT, layouts[i])) { |
---|
356 | g_print ("Could not set the Layout to %s\n", layouts[i]); |
---|
357 | return TEST_RETVAL_ERROR; |
---|
358 | } |
---|
359 | test_run (150); |
---|
360 | } |
---|
361 | |
---|
362 | |
---|
363 | test_run (100); |
---|
364 | |
---|
365 | return TEST_RETVAL_SUCCESS; |
---|
366 | } |
---|
367 | |
---|
368 | static TestRetval |
---|
369 | test_dialog_flags (void) |
---|
370 | { |
---|
371 | GnomePrintConfig *config; |
---|
372 | GnomePrintJob *job; |
---|
373 | GtkWidget *dialog; |
---|
374 | |
---|
375 | config = gnome_print_config_default (); |
---|
376 | |
---|
377 | job = gnome_print_job_new (config); |
---|
378 | dialog = gnome_print_dialog_new (job, "Sample Print Dialog", |
---|
379 | 0); |
---|
380 | gtk_widget_show (dialog); |
---|
381 | test_pos_window (dialog); |
---|
382 | |
---|
383 | job = gnome_print_job_new (config); |
---|
384 | dialog = gnome_print_dialog_new (job, "Sample Print Dialog", |
---|
385 | GNOME_PRINT_DIALOG_RANGE); |
---|
386 | gtk_widget_show (dialog); |
---|
387 | test_pos_window (dialog); |
---|
388 | |
---|
389 | job = gnome_print_job_new (config); |
---|
390 | dialog = gnome_print_dialog_new (job, "Sample Print Dialog", |
---|
391 | GNOME_PRINT_DIALOG_COPIES); |
---|
392 | gtk_widget_show (dialog); |
---|
393 | test_pos_window (dialog); |
---|
394 | |
---|
395 | test_dialog_show_all_pages (config); |
---|
396 | |
---|
397 | test_run (1500); |
---|
398 | |
---|
399 | return TEST_RETVAL_SUCCESS; |
---|
400 | } |
---|
401 | |
---|
402 | static TestRetval |
---|
403 | test_show (void) |
---|
404 | { |
---|
405 | GtkWidget *dialog; |
---|
406 | |
---|
407 | dialog = gnome_print_dialog_new (NULL, "Sample Print Dialog", 0); |
---|
408 | |
---|
409 | gtk_widget_show (dialog); |
---|
410 | |
---|
411 | test_run (500); |
---|
412 | |
---|
413 | return TEST_RETVAL_SUCCESS; |
---|
414 | } |
---|
415 | |
---|
416 | static TestRetval |
---|
417 | test_multiple (void) |
---|
418 | { |
---|
419 | GnomePrintJob *job; |
---|
420 | GnomePrintConfig *config; |
---|
421 | GtkWidget *dialog; |
---|
422 | gint i, j; |
---|
423 | gint max = 3; |
---|
424 | gint run_for; |
---|
425 | |
---|
426 | job = gnome_print_job_new (NULL); |
---|
427 | config = gnome_print_job_get_config (job); |
---|
428 | dialog = gnome_print_dialog_new (job, "Sample Print Dialog", 0); |
---|
429 | |
---|
430 | if (!job || !config || !dialog) |
---|
431 | return TEST_RETVAL_ERROR; |
---|
432 | |
---|
433 | run_for = 1; |
---|
434 | |
---|
435 | for (j = 0; j < 3; j++) { |
---|
436 | |
---|
437 | run_for = (j == 0 ? 1 : (j == 1 ? 100 : 400)); |
---|
438 | |
---|
439 | for (i = 0; i < max; i++) { |
---|
440 | gtk_widget_show (dialog); |
---|
441 | test_run (run_for); |
---|
442 | gtk_widget_destroy (dialog); |
---|
443 | dialog = gnome_print_dialog_new (job, "Sample Print Dialog", 0); |
---|
444 | } |
---|
445 | |
---|
446 | for (i = 0; i < max; i++) { |
---|
447 | gtk_widget_show (dialog); |
---|
448 | test_run (run_for); |
---|
449 | gtk_widget_destroy (dialog); |
---|
450 | g_object_unref (config); |
---|
451 | g_object_unref (job); |
---|
452 | config = gnome_print_config_default (); |
---|
453 | job = gnome_print_job_new (config); |
---|
454 | dialog = gnome_print_dialog_new (job, "Sample Print Dialog", 0); |
---|
455 | } |
---|
456 | } |
---|
457 | |
---|
458 | return TEST_RETVAL_SUCCESS; |
---|
459 | } |
---|
460 | |
---|
461 | static TestRetval |
---|
462 | test_paper_changes_print (void) |
---|
463 | { |
---|
464 | GnomePrintConfig *config; |
---|
465 | GnomePrintJob *job; |
---|
466 | GtkWidget *dialog; |
---|
467 | gint i; |
---|
468 | gint n = 4; |
---|
469 | |
---|
470 | job = gnome_print_job_new (NULL); |
---|
471 | config = gnome_print_job_get_config (job); |
---|
472 | |
---|
473 | if (config == NULL) |
---|
474 | return TEST_RETVAL_ERROR; |
---|
475 | |
---|
476 | gnome_print_config_set (config, "Printer", "GENERIC"); |
---|
477 | |
---|
478 | if (!gnome_print_config_set (config, GNOME_PRINT_KEY_PAPER_SIZE, "B3")) { |
---|
479 | g_warning ("Could not change the Paper Size\n"); |
---|
480 | return TEST_RETVAL_ERROR; |
---|
481 | } |
---|
482 | if (!gnome_print_config_set (config, GNOME_PRINT_KEY_PAPER_ORIENTATION, "R270")) { |
---|
483 | g_warning ("Could not change the Paper Orientation\n"); |
---|
484 | return TEST_RETVAL_ERROR; |
---|
485 | } |
---|
486 | if (!gnome_print_config_set (config, GNOME_PRINT_KEY_PAGE_ORIENTATION, "R90")) { |
---|
487 | g_warning ("Could not change the Page Orientation\n"); |
---|
488 | return TEST_RETVAL_ERROR; |
---|
489 | } |
---|
490 | |
---|
491 | dialog = gnome_print_dialog_new (job, "Sample Print Dialog", 0); |
---|
492 | for (i = 0; i < n; i++) { |
---|
493 | gtk_widget_destroy (dialog); |
---|
494 | dialog = gnome_print_dialog_new (job, "Sample Print Dialog", 0); |
---|
495 | if (!dialog) |
---|
496 | return TEST_RETVAL_ERROR; |
---|
497 | gtk_widget_show (dialog); |
---|
498 | test_run (100); |
---|
499 | } |
---|
500 | |
---|
501 | gtk_widget_destroy (dialog); |
---|
502 | |
---|
503 | return TEST_RETVAL_SUCCESS; |
---|
504 | } |
---|
505 | |
---|
506 | static TestRetval |
---|
507 | test_simple (void) |
---|
508 | { |
---|
509 | GnomePrintJob *job; |
---|
510 | GnomePrintConfig *config; |
---|
511 | GtkWidget *dialog; |
---|
512 | |
---|
513 | config = gnome_print_config_default (); |
---|
514 | if (!config) |
---|
515 | return TEST_RETVAL_ERROR; |
---|
516 | job = gnome_print_job_new (NULL); |
---|
517 | if (!job) |
---|
518 | return TEST_RETVAL_ERROR; |
---|
519 | job = gnome_print_job_new (config); |
---|
520 | if (!job) |
---|
521 | return TEST_RETVAL_ERROR; |
---|
522 | dialog = gnome_print_dialog_new (NULL, "Sample Print Dialog", 0); |
---|
523 | if (!dialog) |
---|
524 | return TEST_RETVAL_ERROR; |
---|
525 | |
---|
526 | return TEST_RETVAL_SUCCESS; |
---|
527 | } |
---|
528 | |
---|
529 | |
---|
530 | /* From gtkdialog.c. Nasty but is a gui test suite, what else do you expect? */ |
---|
531 | typedef struct _ResponseData ResponseData; |
---|
532 | struct _ResponseData |
---|
533 | { |
---|
534 | gint response_id; |
---|
535 | }; |
---|
536 | |
---|
537 | static gboolean |
---|
538 | dialog_click (GtkWidget *dialog, gint action) |
---|
539 | { |
---|
540 | GtkWidget *button = NULL; |
---|
541 | GtkBox *box; |
---|
542 | GList *list; |
---|
543 | |
---|
544 | box = GTK_BOX (GTK_DIALOG (dialog)->action_area); |
---|
545 | list = box->children; |
---|
546 | while (list) { |
---|
547 | GtkBoxChild *child; |
---|
548 | GtkWidget *widget; |
---|
549 | child = list->data; |
---|
550 | widget = child->widget; |
---|
551 | if (GTK_IS_BUTTON (widget)) { |
---|
552 | ResponseData *ad = g_object_get_data (G_OBJECT (widget), |
---|
553 | "gtk-dialog-response-data"); |
---|
554 | if (ad->response_id == action) |
---|
555 | button = widget; |
---|
556 | } |
---|
557 | list = list->next; |
---|
558 | } |
---|
559 | |
---|
560 | if (!button) { |
---|
561 | g_warning ("Could not find button to click\n"); |
---|
562 | return FALSE; |
---|
563 | } |
---|
564 | |
---|
565 | g_signal_emit_by_name (button, "clicked", 0); |
---|
566 | |
---|
567 | return FALSE; |
---|
568 | } |
---|
569 | |
---|
570 | static gboolean |
---|
571 | dialog_click_print_cb (gpointer dialog, gint action) |
---|
572 | { |
---|
573 | return dialog_click (dialog, GNOME_PRINT_DIALOG_RESPONSE_PRINT); |
---|
574 | } |
---|
575 | |
---|
576 | static TestRetval |
---|
577 | dialog_click_print (GtkWidget *dialog, gint msecs) |
---|
578 | { |
---|
579 | tag = gtk_timeout_add (msecs, (GSourceFunc) dialog_click_print_cb, dialog); |
---|
580 | |
---|
581 | return TEST_RETVAL_SUCCESS; |
---|
582 | } |
---|
583 | |
---|
584 | static void |
---|
585 | my_draw (GnomePrintContext *gpc) |
---|
586 | { |
---|
587 | gnome_print_beginpage (gpc, "1"); |
---|
588 | gnome_print_moveto (gpc, 1, 1); |
---|
589 | gnome_print_lineto (gpc, 200, 200); |
---|
590 | gnome_print_stroke (gpc); |
---|
591 | gnome_print_showpage (gpc); |
---|
592 | } |
---|
593 | |
---|
594 | |
---|
595 | static void |
---|
596 | my_print (GnomePrintJob *job, gboolean preview) |
---|
597 | { |
---|
598 | GnomePrintContext *gpc; |
---|
599 | |
---|
600 | gpc = gnome_print_job_get_context (job); |
---|
601 | my_draw (gpc); |
---|
602 | g_object_unref (G_OBJECT (gpc)); |
---|
603 | |
---|
604 | gnome_print_job_close (job); |
---|
605 | |
---|
606 | if (!preview) { |
---|
607 | gnome_print_job_print (job); |
---|
608 | } |
---|
609 | #if 0 |
---|
610 | else { |
---|
611 | gtk_widget_show (gnome_print_job_preview_new (job, "Title goes here")); |
---|
612 | } |
---|
613 | #endif |
---|
614 | } |
---|
615 | |
---|
616 | static TestRetval |
---|
617 | gui_print (GnomePrintJob *job, gint time) |
---|
618 | { |
---|
619 | GtkWidget *dialog; |
---|
620 | gint response; |
---|
621 | |
---|
622 | dialog = gnome_print_dialog_new (job, "Sample Print Dialog", 0); |
---|
623 | if (!dialog) |
---|
624 | return TEST_RETVAL_ERROR; |
---|
625 | |
---|
626 | gtk_widget_show (dialog); |
---|
627 | test_run (2 * time); |
---|
628 | |
---|
629 | if (dialog_click_print (dialog, 5 * time) != TEST_RETVAL_SUCCESS) |
---|
630 | return TEST_RETVAL_ERROR; |
---|
631 | |
---|
632 | response = gtk_dialog_run (GTK_DIALOG (dialog)); |
---|
633 | gtk_widget_destroy (dialog); |
---|
634 | switch (response) { |
---|
635 | case GNOME_PRINT_DIALOG_RESPONSE_PRINT: |
---|
636 | my_print (job, FALSE); |
---|
637 | break; |
---|
638 | case GNOME_PRINT_DIALOG_RESPONSE_PREVIEW: |
---|
639 | my_print (job, TRUE); |
---|
640 | break; |
---|
641 | default: |
---|
642 | return TEST_RETVAL_ERROR; |
---|
643 | } |
---|
644 | |
---|
645 | test_run (2 * time); |
---|
646 | |
---|
647 | return TEST_RETVAL_SUCCESS; |
---|
648 | } |
---|
649 | |
---|
650 | static TestRetval |
---|
651 | test_print_ps (void) |
---|
652 | { |
---|
653 | GnomePrintConfig *config; |
---|
654 | GnomePrintJob *job; |
---|
655 | TestRetval retval; |
---|
656 | |
---|
657 | config = gnome_print_config_default (); |
---|
658 | job = gnome_print_job_new (config); |
---|
659 | if (!job || !config) |
---|
660 | return TEST_RETVAL_ERROR; |
---|
661 | |
---|
662 | gnome_print_config_set (config, "Printer", "GENERIC"); |
---|
663 | retval = gui_print (job, 100); |
---|
664 | |
---|
665 | return retval; |
---|
666 | } |
---|
667 | |
---|
668 | static TestRetval |
---|
669 | test_print_pdf (void) |
---|
670 | { |
---|
671 | GnomePrintConfig *config; |
---|
672 | GnomePrintJob *job; |
---|
673 | TestRetval retval; |
---|
674 | |
---|
675 | config = gnome_print_config_default (); |
---|
676 | job = gnome_print_job_new (config); |
---|
677 | if (!job || !config) |
---|
678 | return TEST_RETVAL_ERROR; |
---|
679 | |
---|
680 | gnome_print_config_set (config, "Printer", "PDF"); |
---|
681 | |
---|
682 | retval = gui_print (job, 300); |
---|
683 | |
---|
684 | return retval; |
---|
685 | } |
---|
686 | |
---|
687 | static TestRetval |
---|
688 | test_print_multiple_ps (void) |
---|
689 | { |
---|
690 | GnomePrintConfig *config; |
---|
691 | GnomePrintJob *job; |
---|
692 | TestRetval retval; |
---|
693 | gint i; |
---|
694 | gint max = 4; |
---|
695 | |
---|
696 | config = gnome_print_config_default (); |
---|
697 | job = gnome_print_job_new (config); |
---|
698 | if (!job || !config) |
---|
699 | return TEST_RETVAL_ERROR; |
---|
700 | |
---|
701 | gnome_print_config_set (config, "Printer", "GENERIC"); |
---|
702 | |
---|
703 | for (i = 0; i < max; i++) { |
---|
704 | retval = gui_print (job, 100); |
---|
705 | if (retval != TEST_RETVAL_SUCCESS) |
---|
706 | return retval; |
---|
707 | } |
---|
708 | |
---|
709 | return retval; |
---|
710 | } |
---|
711 | |
---|
712 | static TestRetval |
---|
713 | test_print_multiple_pdf (void) |
---|
714 | { |
---|
715 | GnomePrintConfig *config; |
---|
716 | GnomePrintJob *job; |
---|
717 | TestRetval retval; |
---|
718 | gint i; |
---|
719 | gint max = 4; |
---|
720 | |
---|
721 | /* First do it non-gui */ |
---|
722 | config = gnome_print_config_default (); |
---|
723 | job = gnome_print_job_new (config); |
---|
724 | if (!job || !config) |
---|
725 | return TEST_RETVAL_ERROR; |
---|
726 | |
---|
727 | gnome_print_config_set (config, "Printer", "PDF"); |
---|
728 | |
---|
729 | for (i = 0; i < max; i++) { |
---|
730 | my_print (job, FALSE); |
---|
731 | } |
---|
732 | |
---|
733 | for (i = 0; i < max; i++) { |
---|
734 | retval = gui_print (job, 100); |
---|
735 | if (retval != TEST_RETVAL_SUCCESS) |
---|
736 | return retval; |
---|
737 | } |
---|
738 | |
---|
739 | return retval; |
---|
740 | } |
---|
741 | |
---|
742 | static TestRetval |
---|
743 | test_print_multiple (void) |
---|
744 | { |
---|
745 | GnomePrintConfig *config; |
---|
746 | GnomePrintJob *job; |
---|
747 | TestRetval retval; |
---|
748 | gint i; |
---|
749 | gint max = 2; |
---|
750 | |
---|
751 | config = gnome_print_config_default (); |
---|
752 | job = gnome_print_job_new (config); |
---|
753 | if (!job || !config) |
---|
754 | return TEST_RETVAL_ERROR; |
---|
755 | |
---|
756 | for (i = 0; i < max; i++) { |
---|
757 | gnome_print_config_set (config, "Printer", "PDF"); |
---|
758 | retval = gui_print (job, 100); |
---|
759 | if (retval != TEST_RETVAL_SUCCESS) |
---|
760 | return retval; |
---|
761 | |
---|
762 | gnome_print_config_set (config, "Printer", "GENERIC"); |
---|
763 | retval = gui_print (job, 100); |
---|
764 | if (retval != TEST_RETVAL_SUCCESS) |
---|
765 | return retval; |
---|
766 | } |
---|
767 | |
---|
768 | g_object_unref (config); |
---|
769 | g_object_unref (job); |
---|
770 | |
---|
771 | config = gnome_print_config_default (); |
---|
772 | job = gnome_print_job_new (config); |
---|
773 | |
---|
774 | for (i = 0; i < max; i++) { |
---|
775 | gnome_print_config_set (config, "Printer", "PDF"); |
---|
776 | retval = gui_print (job, 100); |
---|
777 | if (retval != TEST_RETVAL_SUCCESS) |
---|
778 | return retval; |
---|
779 | |
---|
780 | gnome_print_config_set (config, "Printer", "GENERIC"); |
---|
781 | retval = gui_print (job, 100); |
---|
782 | if (retval != TEST_RETVAL_SUCCESS) |
---|
783 | return retval; |
---|
784 | } |
---|
785 | |
---|
786 | g_object_unref (config); |
---|
787 | g_object_unref (job); |
---|
788 | |
---|
789 | return retval; |
---|
790 | } |
---|
791 | |
---|
792 | static TestRetval |
---|
793 | test_dispatch (gint num_test) |
---|
794 | { |
---|
795 | TestRetval ret; |
---|
796 | |
---|
797 | g_print ("Running num_test %d\n[%s]\n", num_test, test_cases[num_test].description); |
---|
798 | |
---|
799 | ret = (test_cases[num_test].function ()); |
---|
800 | |
---|
801 | switch (ret) { |
---|
802 | case TEST_RETVAL_SUCCESS: |
---|
803 | if (num_test == max_num_test) |
---|
804 | ret = TEST_RETVAL_SUCCESS_LAST; |
---|
805 | g_print (" Pass..\n"); |
---|
806 | break; |
---|
807 | case TEST_RETVAL_ERROR: |
---|
808 | g_print (" Fail..\n"); |
---|
809 | break; |
---|
810 | default: |
---|
811 | g_assert_not_reached (); |
---|
812 | break; |
---|
813 | |
---|
814 | } |
---|
815 | |
---|
816 | return ret; |
---|
817 | } |
---|
818 | |
---|
819 | static void |
---|
820 | test_dialog_tree () |
---|
821 | { |
---|
822 | GnomePrintConfig *config; |
---|
823 | GPANode *node; |
---|
824 | GtkWidget *tree; |
---|
825 | |
---|
826 | config = gnome_print_config_default (); |
---|
827 | node = gnome_print_config_get_node (config); |
---|
828 | tree = gpa_tree_viewer_new (node); |
---|
829 | |
---|
830 | g_signal_connect (G_OBJECT (tree), "delete_event", |
---|
831 | (GCallback) gtk_main_quit, NULL); |
---|
832 | gtk_main (); |
---|
833 | } |
---|
834 | |
---|
835 | static void |
---|
836 | usage (gchar *error) |
---|
837 | { |
---|
838 | g_print ("Error: %s\n\n", error); |
---|
839 | g_print ("Usage: test-dialog --num=[num_test] [--tree]\n\n"); |
---|
840 | poptPrintHelp (popt, stdout, FALSE); |
---|
841 | exit (TEST_RETVAL_BAD_PARAMETERS); |
---|
842 | exit (TEST_RETVAL_BAD_PARAMETERS); |
---|
843 | } |
---|
844 | |
---|
845 | static void |
---|
846 | parse_command_line (int argc, const char ** argv, gint *num_test) |
---|
847 | { |
---|
848 | gchar *env_test; |
---|
849 | |
---|
850 | popt = poptGetContext ("test_gpa", argc, argv, options, 0); |
---|
851 | poptGetNextOpt (popt); |
---|
852 | |
---|
853 | if (option_list_tests) |
---|
854 | return; |
---|
855 | |
---|
856 | if (!num_test_str || num_test_str[0] == '\0') { |
---|
857 | if (tree) |
---|
858 | return; |
---|
859 | env_test = getenv ("TEST_NUM"); |
---|
860 | if (env_test && env_test[0]) { |
---|
861 | g_print ("TEST_NUM env variable = %s\n", env_test); |
---|
862 | num_test_str = g_strdup (env_test); |
---|
863 | } else { |
---|
864 | return; |
---|
865 | } |
---|
866 | } |
---|
867 | |
---|
868 | *num_test = atoi (num_test_str); |
---|
869 | |
---|
870 | if (*num_test == -1) { |
---|
871 | GList *list = NULL; |
---|
872 | /* We crash, this is part of the sanity check that allows us verify that crashes |
---|
873 | * are treated as errors when they happen when running ./run-tests |
---|
874 | * (Chema) |
---|
875 | */ |
---|
876 | g_print ("Crashing ...\n"); |
---|
877 | list->next = NULL; |
---|
878 | } |
---|
879 | |
---|
880 | if (*num_test < 0 || (*num_test > max_num_test)) { |
---|
881 | gchar *error; |
---|
882 | error = g_strdup_printf ("num_test number out of range. Valid range is -1 to %d", |
---|
883 | max_num_test); |
---|
884 | usage (error); |
---|
885 | } |
---|
886 | |
---|
887 | if (debug) |
---|
888 | g_print ("num_test is %d\n", *num_test); |
---|
889 | } |
---|
890 | |
---|
891 | static void |
---|
892 | list_tests (void) |
---|
893 | { |
---|
894 | gint i; |
---|
895 | gint max = max_num_test + 1; |
---|
896 | |
---|
897 | for (i = 0; i < max; i++) |
---|
898 | g_print ("%s\n", test_cases[i].description); |
---|
899 | } |
---|
900 | |
---|
901 | static void |
---|
902 | handle_sigsegv (int i) |
---|
903 | { |
---|
904 | g_print ("\n\ntest-dialog crashed while running num_test %d [%s]\n", |
---|
905 | num_test, test_cases[num_test].description); |
---|
906 | exit (TEST_RETVAL_CRASH); |
---|
907 | } |
---|
908 | |
---|
909 | int |
---|
910 | main (int argc, const char * argv[]) |
---|
911 | { |
---|
912 | TestRetval ret = TEST_RETVAL_SUCCESS; |
---|
913 | struct sigaction sig; |
---|
914 | |
---|
915 | gtk_init (&argc, (char ***) &argv); |
---|
916 | |
---|
917 | parse_command_line (argc, argv, &num_test); |
---|
918 | |
---|
919 | /* Catch sigsegv signals */ |
---|
920 | memset (&sig, 0, sizeof(sigaction)); |
---|
921 | sig.sa_handler = handle_sigsegv; |
---|
922 | sigaction (SIGSEGV, &sig, NULL); |
---|
923 | |
---|
924 | if (option_list_tests) { |
---|
925 | list_tests (); |
---|
926 | return ret; |
---|
927 | } |
---|
928 | |
---|
929 | if (tree) { |
---|
930 | test_dialog_tree (); |
---|
931 | return 0; |
---|
932 | } |
---|
933 | |
---|
934 | if (num_test != MAGIC_NUMBER) |
---|
935 | ret = test_dispatch (num_test); |
---|
936 | else { |
---|
937 | GnomePrintConfig *config; |
---|
938 | GnomePrintJob *job; |
---|
939 | GtkWidget *dialog; |
---|
940 | gchar *s; |
---|
941 | job = gnome_print_job_new (NULL); |
---|
942 | config = gnome_print_job_get_config (job); |
---|
943 | if (FALSE) { |
---|
944 | dialog = gnome_print_dialog_new (job, "Test", 0); |
---|
945 | gtk_dialog_run (GTK_DIALOG (dialog)); |
---|
946 | gtk_widget_destroy (dialog); |
---|
947 | } else { |
---|
948 | dialog = gnome_print_dialog_new (job, "Test dialog 1", 0); |
---|
949 | gtk_widget_show (dialog); |
---|
950 | dialog = gnome_print_dialog_new (job, "Test dialog 2", 0); |
---|
951 | gtk_widget_show (dialog); |
---|
952 | gtk_main (); |
---|
953 | } |
---|
954 | |
---|
955 | s = gnome_print_config_to_string (config, 0); |
---|
956 | |
---|
957 | g_object_unref (G_OBJECT (config)); |
---|
958 | g_object_unref (G_OBJECT (job)); |
---|
959 | |
---|
960 | g_print ("-->%s<--\n", s); |
---|
961 | |
---|
962 | g_free (s); |
---|
963 | } |
---|
964 | |
---|
965 | return ret; |
---|
966 | } |
---|
967 | |
---|