File: test-dialog.c

package info (click to toggle)
libgnomeprintui 2.12.1-4
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 5,084 kB
  • ctags: 1,546
  • sloc: ansic: 13,158; sh: 8,490; xml: 1,755; makefile: 272
file content (984 lines) | stat: -rw-r--r-- 24,630 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 *  test-dialog.c:
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Library General Public License
 *  as published by the Free Software Foundation; either version 2 of
 *  the License, or (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Library General Public License for more details.
 *
 *  You should have received a copy of the GNU Library General Public
 *  License along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 *  Authors:
 *    Chema Celorio <chema@ximian.com>
 *
 *  Copyright (C) 2002-2003 Ximian Inc.
 *
 */

#include "config.h"

#include <popt.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include <stdlib.h>

#include <gtk/gtk.h>
#include <libgnomeprint/gnome-print-config.h>
#include <libgnomeprintui/gnome-print-dialog.h>
#include <libgnomeprintui/gnome-printer-selector.h>
#include <libgnomeprintui/gpaui/gpa-tree-viewer.h>
#include <libgnomeprintui/gpaui/gpa-printer-selector.h>
#include <libgnomeprint/private/gnome-print-config-private.h>

#define MAGIC_NUMBER -100

gboolean option_list_tests;
gint     num_test = MAGIC_NUMBER;
gchar *  num_test_str = NULL;
gboolean debug = FALSE;
gboolean tree = FALSE;
gint     callback_count = 0;

typedef enum {
	TEST_RETVAL_CRASH = -3,
	TEST_RETVAL_ERROR = -2,
	TEST_RETVAL_BAD_PARAMETERS = -1,
	TEST_RETVAL_SUCCESS = 0,
	TEST_RETVAL_SUCCESS_LAST = 99,
} TestRetval;

poptContext popt;

static struct poptOption options[] = {
	{ "num", '\0', POPT_ARG_STRING, &num_test_str,   0,
	  "Test number to run", "#"},
	{ "list-tests", '\0', POPT_ARG_NONE, &option_list_tests,   0,
	  "List on the console the description of the test cases", NULL},
	{ "tree",     '\0', POPT_ARG_NONE, &tree,   0,
	  "Show the tree of GPANodes",  NULL},
	{ "debug",    '\0', POPT_ARG_NONE, &debug, 0,
	  "Print debugging output",          NULL},
	POPT_AUTOHELP
	{ NULL }
};

typedef struct _TestCase TestCase;

struct _TestCase {
	TestRetval (*function) (void);
	const gchar *description;
};

#define max_num_test ((sizeof (test_cases) / sizeof (test_cases[0]))-1)

static TestRetval test_simple (void);
static TestRetval test_show (void);
static TestRetval test_dialog_flags (void);
static TestRetval test_paper_changes (void);
static TestRetval test_printer_changes (void);
static TestRetval test_multiple (void);
static TestRetval test_print_ps (void);
static TestRetval test_print_multiple_ps (void);
static TestRetval test_print_pdf (void);
static TestRetval test_print_multiple_pdf (void);
static TestRetval test_print_multiple (void);
static TestRetval test_paper_changes_print (void);

static const TestCase test_cases[] = {
	{ &test_simple,              "Simple checks"},
	{ &test_show,                "Create and show a GnomePrintDialog"},
	{ &test_dialog_flags,        "Shows dialogs with different flag options"},
	{ &test_paper_changes,       "Change paper parameters and monitor dialog for changes"},
	{ &test_printer_changes,     "Change the selected printer"},
	{ &test_multiple,            "Create and destroy multiple GnomePrintDialogs."},
	{ &test_print_ps,            "Print to PS"},
	{ &test_print_multiple_ps,   "Print multiple times to PS with the same GnomePrintConfig"},
	{ &test_print_pdf,           "Print to PDF"},
	{ &test_print_multiple_pdf,  "Print multiple times to PDF with the same GnomePrintConfig"},
	{ &test_print_multiple,      "Print multiple times alternating between PS & PDF"},
	{ &test_paper_changes_print, "Change paper parameters and print"},
};

/* Helper functions */
static gint
test_dialog_get_num_pages (GtkWidget *dialog)
{
	GtkNotebook *notebook;

	g_object_get (G_OBJECT (dialog), "notebook", &notebook, NULL);
	return g_list_length (notebook->children);
}

static void
test_dialog_show_page (GtkWidget *dialog, gint page)
{
	GtkNotebook *notebook;

	g_object_get (G_OBJECT (dialog), "notebook", &notebook, NULL);
	gtk_notebook_set_current_page (notebook, page);
	
}

/**
 * test_pos_window:
 * @window: 
 * 
 * Position de window so that it doens't overlap with other windows
 * this allows us to see several windows on the screen
 **/
static void
test_pos_window (GtkWidget *window)
{
#define PANEL_HEIGHT 40
#define TOP_BOTTOM_FRAME 36
#define RIGHT_LEFT_FRAME 16
	static gint x = 0;
	static gint y = PANEL_HEIGHT;
	static gint new_x = 0;
	gint width;
	gint height;

	gtk_window_get_size (GTK_WINDOW (window), &width, &height);
	
	if ((y + height) > gdk_screen_get_height (gdk_screen_get_default ())) {
		x += new_x + RIGHT_LEFT_FRAME;
		new_x = 0;
		y = PANEL_HEIGHT;
	}

	if (x + width > gdk_screen_get_width (gdk_screen_get_default ()))
		x = 0;
	if (y + height > gdk_screen_get_height (gdk_screen_get_default ()))
		y = 0;
	
	new_x = (width > new_x) ? width : new_x;
	
	gtk_window_move (GTK_WINDOW (window), x, y);
	y += height + TOP_BOTTOM_FRAME; 
}

static GtkWidget *
test_dialog_show_all_pages (GnomePrintConfig *config)
{
	GnomePrintJob *job;
	GtkWidget *dialog;
	
	job = gnome_print_job_new (config);
	dialog = gnome_print_dialog_new (job, (const guchar *) "Sample Print Dialog",
					 GNOME_PRINT_DIALOG_RANGE | GNOME_PRINT_DIALOG_COPIES);
	gtk_widget_show (dialog);
	test_pos_window (dialog);
	if (test_dialog_get_num_pages (dialog) != 3) {
		g_print ("Invalid number of pages\n");
		exit (TEST_RETVAL_ERROR);
	}

	job = gnome_print_job_new (config);
	dialog = gnome_print_dialog_new (job, (const guchar *) "Sample Print Dialog",
					 GNOME_PRINT_DIALOG_RANGE | GNOME_PRINT_DIALOG_COPIES);
	gtk_widget_show (dialog);
	test_dialog_show_page (dialog, 1);
	test_pos_window (dialog);
	if (test_dialog_get_num_pages (dialog) != 3) {
		g_print ("Invalid number of pages\n");
		exit (TEST_RETVAL_ERROR);
	}

	job = gnome_print_job_new (config);
	dialog = gnome_print_dialog_new (job, (const guchar *) "Sample Print Dialog",
					 GNOME_PRINT_DIALOG_RANGE | GNOME_PRINT_DIALOG_COPIES);
	gtk_widget_show (dialog);
	test_pos_window (dialog);
	test_dialog_show_page (dialog, 2);	
	if (test_dialog_get_num_pages (dialog) != 3) {
		g_print ("Invalid number of pages\n");
		exit (TEST_RETVAL_ERROR);
	}
		
	return dialog;
}

guint tag = 0;

static gboolean
test_main_quit_real (void)
{
	gtk_main_quit ();
	return FALSE;
}

static gboolean
test_main_quit (void)
{
	gtk_timeout_remove (tag);
	gtk_idle_add ((GtkFunction) test_main_quit_real, NULL);
	return TRUE;
}

static void
test_run (gint msecs)
{
	tag = gtk_timeout_add (msecs, (GSourceFunc) test_main_quit, NULL);
	gtk_main ();
	return;
}

static void
increment_callback_count (void)
{
	callback_count++;
}

/* Tests */
static TestRetval
test_printer_changes (void)
{
	GnomePrintConfig *config;
	GPANode *node, *printers, *child;
	GSList *list, *l;
	GtkWidget *dialog;
	GtkTreeSelection *sel;
	gint len;
	GnomePrinterSelector *printer;

	config = gnome_print_config_default ();
	
	dialog = test_dialog_show_all_pages (config);

	node = GNOME_PRINT_CONFIG_NODE (config);
	g_return_val_if_fail (GPA_IS_NODE (node), TEST_RETVAL_ERROR);
	printers = gpa_node_get_child_from_path (node, (const guchar *) "Globals.Printers");
	g_return_val_if_fail (GPA_IS_NODE (printers), TEST_RETVAL_ERROR);	

	/* Make sure the gtkoptionmenu changes */
	callback_count = 0;
	g_object_get (G_OBJECT (dialog), "printer_selector", &printer, NULL);
	sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (
			GPA_PRINTER_SELECTOR (printer->printers)->treeview));
	g_return_val_if_fail (GTK_IS_TREE_SELECTION (sel), TEST_RETVAL_ERROR);
	g_signal_connect (G_OBJECT (sel), "changed",
			  (GCallback) increment_callback_count, NULL);

	test_run (1000);
	
	/* Create list */
	list = NULL;
	child = gpa_node_get_child (printers, NULL);
	for (; child != NULL; child = gpa_node_get_child (printers, child))
		list = g_slist_prepend (list, child);
	
	l = list;
	while (l) {
		if (!gnome_print_config_set (config, (const guchar *) "Printer", gpa_node_id (l->data))) {
			g_print ("Could not set the Printer to %s\n", gpa_node_id (l->data));
			return TEST_RETVAL_ERROR;
		}
		test_run (1000);
		l = l->next;
	}

	len = g_slist_length (list);
	
	if (callback_count < len) {
		g_warning ("The printers GtkOptionMenu didn't changed "
			   "as expected, expected: %d changed: %d\n",
			 len, callback_count);
		return TEST_RETVAL_ERROR;
	}
	if (callback_count > len) {
		g_warning ("The printers GtkOptionMenu didn't changed "
			   "as expected, expected: %d changed: %d\n",
			   len, callback_count);
#ifdef __GNUC__
//#warning Disabled test
#endif
		return TEST_RETVAL_SUCCESS;
		return TEST_RETVAL_ERROR;
	}

	g_slist_free (list);
	
	g_print ("Callback count %d\n", callback_count);

	return TEST_RETVAL_SUCCESS;
}

static TestRetval
test_paper_changes (void)
{
	GnomePrintConfig *config;
	const gchar *papers [] = {
		"USLetter", "USLegal", "Executive", "A0", "A1", "A2", "A3", "A4", "A5",
		"A6", "A7", "A8", "A9", "A10", "B0", "B1", "B2", "B3", "B4", "B5", "B6",
		"B7", "B8", "B9", "B10", "C0", "C1", "C2", "C3", "C4", "C5", "C6", "C7",
		"C8", "C9", "C10", "A4_3", "A4_4", "A4_8", "A3_4", "A5_3", "DL", "C6_C5",
		"Envelope_No10", "Envelope_6x9", "USLetter"};
	const gchar *orientations [] = {"R0", "R90", "R180", "R270", "R0"};
	const gchar *layouts [] = {"Plain", "2_1", "4_1", "I2_1", "IM2_1", "Plain"};
	gint max, i;
		
	config = gnome_print_config_default ();
	test_dialog_show_all_pages (config);

	gnome_print_config_set (config, (const guchar *) "Printer", (const guchar *) "GENERIC");

	test_run (1000);

	max = sizeof (papers) / sizeof (gchar *);
	for (i = 0; i < max; i++) {
		if (!gnome_print_config_set (config, (const guchar *) GNOME_PRINT_KEY_PAPER_SIZE, (const guchar *) papers[i])) {
			g_print ("Could not set paper size to %s\n", papers[i]);
			return TEST_RETVAL_ERROR;
		}
		test_run (50);
	}

	max = sizeof (orientations) / sizeof (gchar *);
	for (i = 0; i < max; i++) {
		if (!gnome_print_config_set (config, (const guchar *) GNOME_PRINT_KEY_PAGE_ORIENTATION, (const guchar *) orientations[i])) {
			g_print ("Could not set the page orientation to %s\n", orientations[i]);
			return TEST_RETVAL_ERROR;
		}
		test_run (150);
	}
	for (i = 0; i < max; i++) {
		if (!gnome_print_config_set (config, (const guchar *) GNOME_PRINT_KEY_PAPER_ORIENTATION, (const guchar *) orientations[i])) {
			g_print ("Could not set the paper orientation to %s\n", orientations[i]);
			return TEST_RETVAL_ERROR;
		}
		test_run (150);
	}

	max = sizeof (layouts) / sizeof (gchar *);
	for (i = 0; i < max; i++) {
		if (!gnome_print_config_set (config, (const guchar *) GNOME_PRINT_KEY_LAYOUT, (const guchar *) layouts[i])) {
			g_print ("Could not set the Layout to %s\n", layouts[i]);
			return TEST_RETVAL_ERROR;
		}
		test_run (150);
	}

	
	test_run (100);

	return TEST_RETVAL_SUCCESS;
}

static TestRetval
test_dialog_flags (void)
{
	GnomePrintConfig *config;
	GnomePrintJob *job;
	GtkWidget *dialog;

	config = gnome_print_config_default ();

	job = gnome_print_job_new (config);
	dialog = gnome_print_dialog_new (job, (const guchar *) "Sample Print Dialog",
					 0);
	gtk_widget_show (dialog);
	test_pos_window (dialog);

	job = gnome_print_job_new (config);
	dialog = gnome_print_dialog_new (job, (const guchar *) "Sample Print Dialog",
					 GNOME_PRINT_DIALOG_RANGE);
	gtk_widget_show (dialog);
	test_pos_window (dialog);
	
	job = gnome_print_job_new (config);
	dialog = gnome_print_dialog_new (job, (const guchar *) "Sample Print Dialog",
					 GNOME_PRINT_DIALOG_COPIES);
	gtk_widget_show (dialog);
	test_pos_window (dialog);

	test_dialog_show_all_pages (config);

	test_run (1500);

	return TEST_RETVAL_SUCCESS;
}

static TestRetval
test_show (void)
{
	GtkWidget *dialog;

	dialog = gnome_print_dialog_new (NULL, (const guchar *) "Sample Print Dialog", 0);

	gtk_widget_show (dialog);

	test_run (500);

	return TEST_RETVAL_SUCCESS;
}

static TestRetval
test_multiple (void)
{
	GnomePrintJob *job;
	GnomePrintConfig *config;
	GtkWidget *dialog;
	gint i, j;
	gint max = 3;
	gint run_for;

	job = gnome_print_job_new (NULL);
	config = gnome_print_job_get_config (job);
	dialog = gnome_print_dialog_new (job, (const guchar *) "Sample Print Dialog", 0);

	if (!job || !config || !dialog)
		return TEST_RETVAL_ERROR;

	run_for = 1;

	for (j = 0; j < 3; j++) {
		
		run_for = (j == 0 ? 1 : (j == 1 ? 100 : 400));
		
		for (i = 0; i < max; i++) {
			gtk_widget_show (dialog);
			test_run (run_for);
			gtk_widget_destroy (dialog);
			dialog = gnome_print_dialog_new (job, (const guchar *) "Sample Print Dialog", 0);
		}
		
		for (i = 0; i < max; i++) {
			gtk_widget_show (dialog);
			test_run (run_for);
			gtk_widget_destroy (dialog);
			g_object_unref (config);
			g_object_unref (job);
			config = gnome_print_config_default ();
			job = gnome_print_job_new (config);
			dialog = gnome_print_dialog_new (job, (const guchar *) "Sample Print Dialog", 0);
		}
	}
	
	return TEST_RETVAL_SUCCESS;
}

static TestRetval
test_paper_changes_print (void)
{
	GnomePrintConfig *config;
	GnomePrintJob *job;
	GtkWidget *dialog;
	gint i;
	gint n = 4;

	job = gnome_print_job_new (NULL);
	config = gnome_print_job_get_config (job);

	if (config == NULL)
		return TEST_RETVAL_ERROR;

 	gnome_print_config_set (config, (const guchar *) "Printer", (const guchar *) "GENERIC");
				
	if (!gnome_print_config_set (config, (const guchar *) GNOME_PRINT_KEY_PAPER_SIZE, (const guchar *) "B3")) {
		g_warning ("Could not change the Paper Size\n");
		return TEST_RETVAL_ERROR;
	}
	if (!gnome_print_config_set (config, (const guchar *) GNOME_PRINT_KEY_PAPER_ORIENTATION, (const guchar *) "R270")) {
		g_warning ("Could not change the Paper Orientation\n");
		return TEST_RETVAL_ERROR;
	}
	if (!gnome_print_config_set (config, (const guchar *) GNOME_PRINT_KEY_PAGE_ORIENTATION, (const guchar *) "R90")) {
		g_warning ("Could not change the Page Orientation\n");
		return TEST_RETVAL_ERROR;
	}

	dialog = gnome_print_dialog_new (job, (const guchar *) "Sample Print Dialog", 0);
	for (i = 0; i < n; i++) {
		gtk_widget_destroy (dialog);
		dialog = gnome_print_dialog_new (job, (const guchar *) "Sample Print Dialog", 0);
		if (!dialog)
			return TEST_RETVAL_ERROR;
		gtk_widget_show (dialog);
		test_run (100);
	}

	gtk_widget_destroy (dialog);
	
	return TEST_RETVAL_SUCCESS;
}

static TestRetval
test_simple (void)
{
	GnomePrintJob *job;
	GnomePrintConfig *config;
	GtkWidget *dialog;

	config = gnome_print_config_default ();
	if (!config)
		return TEST_RETVAL_ERROR;
	job = gnome_print_job_new (NULL);
	if (!job)
		return TEST_RETVAL_ERROR;
	job = gnome_print_job_new (config);
	if (!job)
		return TEST_RETVAL_ERROR;
	dialog = gnome_print_dialog_new (NULL, (const guchar *) "Sample Print Dialog", 0);
	if (!dialog)
		return TEST_RETVAL_ERROR;
	
	return TEST_RETVAL_SUCCESS;
}


/* From gtkdialog.c. Nasty but is a gui test suite, what else do you expect? */
typedef struct _ResponseData ResponseData;
struct _ResponseData
{
  gint response_id;
};

static gboolean
dialog_click (GtkWidget *dialog, gint action)
{
	GtkWidget *button = NULL;
	GtkBox *box;
	GList *list;
	
	box = GTK_BOX (GTK_DIALOG (dialog)->action_area);
	list = box->children;
	while (list) {
		GtkBoxChild *child;
		GtkWidget *widget;
		child = list->data;
		widget = child->widget;
		if (GTK_IS_BUTTON (widget)) {
			ResponseData *ad = g_object_get_data (G_OBJECT (widget),
							      "gtk-dialog-response-data");
			if (ad->response_id == action)
				button = widget;
		}
		list = list->next;
	}

	if (!button) {
		g_warning ("Could not find button to click\n");
		return FALSE;
	}

	g_signal_emit_by_name (button, "clicked", 0);
	
	return FALSE;
}

static gboolean
dialog_click_print_cb (gpointer dialog, gint action)
{
	return dialog_click (dialog, GNOME_PRINT_DIALOG_RESPONSE_PRINT);
}
		
static TestRetval
dialog_click_print (GtkWidget *dialog, gint msecs)
{
	tag = gtk_timeout_add (msecs, (GSourceFunc) dialog_click_print_cb, dialog);

	return TEST_RETVAL_SUCCESS;
}

static void
my_draw (GnomePrintContext *gpc)
{
	gnome_print_beginpage (gpc, (const guchar *) "1");
	gnome_print_moveto (gpc, 1, 1);
	gnome_print_lineto (gpc, 200, 200);
	gnome_print_stroke (gpc);
	gnome_print_showpage (gpc);
}


static void
my_print (GnomePrintJob *job, gboolean preview)
{
	GnomePrintContext *gpc;

	gpc = gnome_print_job_get_context (job);
	my_draw (gpc);
	g_object_unref (G_OBJECT (gpc));
	
	gnome_print_job_close (job);

	if (!preview) {
		gnome_print_job_print (job);
	}
#if 0
	else {
		gtk_widget_show (gnome_print_job_preview_new (job, "Title goes here"));
	}
#endif
}

static TestRetval
gui_print (GnomePrintJob *job, gint time)
{
	GtkWidget *dialog;
	gint response;

	dialog = gnome_print_dialog_new (job, (const guchar *) "Sample Print Dialog", 0);
	if (!dialog)
		return TEST_RETVAL_ERROR;

	gtk_widget_show (dialog);
	test_run (2 * time);

	if (dialog_click_print (dialog, 5 * time) != TEST_RETVAL_SUCCESS)
		return TEST_RETVAL_ERROR;
	
	response = gtk_dialog_run (GTK_DIALOG (dialog));
	gtk_widget_destroy (dialog);
	switch (response) {
	case GNOME_PRINT_DIALOG_RESPONSE_PRINT:
		my_print (job, FALSE);
		break;
	case GNOME_PRINT_DIALOG_RESPONSE_PREVIEW:
		my_print (job, TRUE);
		break;
	default:
		return TEST_RETVAL_ERROR;
	}

	test_run (2 * time);
	
	return TEST_RETVAL_SUCCESS;
}

static TestRetval
test_print_ps (void)
{
	GnomePrintConfig *config;
	GnomePrintJob *job;
	TestRetval retval;

	config = gnome_print_config_default ();
	job = gnome_print_job_new (config);
	if (!job || !config)
		return TEST_RETVAL_ERROR;

	gnome_print_config_set (config, (const guchar *) "Printer", (const guchar *) "GENERIC");
	retval = gui_print (job, 100);

	return retval;
}

static TestRetval
test_print_pdf (void)
{
	GnomePrintConfig *config;
	GnomePrintJob *job;
	TestRetval retval;

	config = gnome_print_config_default ();
	job = gnome_print_job_new (config);
	if (!job || !config)
		return TEST_RETVAL_ERROR;

	gnome_print_config_set (config, (const guchar *) "Printer", (const guchar *) "PDF");

	retval = gui_print (job, 300);
	
	return retval;
}

static TestRetval
test_print_multiple_ps (void)
{
	GnomePrintConfig *config;
	GnomePrintJob *job;
	TestRetval retval;
	gint i;
	gint max = 4;

	config = gnome_print_config_default ();
	job = gnome_print_job_new (config);
	if (!job || !config)
		return TEST_RETVAL_ERROR;
	
	gnome_print_config_set (config, (const guchar *) "Printer", (const guchar *) "GENERIC");
	
	for (i = 0; i < max; i++) {
		retval = gui_print (job, 100);
		if (retval != TEST_RETVAL_SUCCESS)
			return retval;
	}
		
	return retval;
}

static TestRetval
test_print_multiple_pdf (void)
{
	GnomePrintConfig *config;
	GnomePrintJob *job;
	TestRetval retval;
	gint i;
	gint max = 4;

	/* First do it non-gui */
	config = gnome_print_config_default ();
	job = gnome_print_job_new (config);
	if (!job || !config)
		return TEST_RETVAL_ERROR;

	gnome_print_config_set (config, (const guchar *) "Printer", (const guchar *) "PDF");
	
	for (i = 0; i < max; i++) {
		my_print (job, FALSE);		
	}
	
	for (i = 0; i < max; i++) {
		retval = gui_print (job, 100);
		if (retval != TEST_RETVAL_SUCCESS)
			return retval;
	}
		
	return retval;
}

static TestRetval
test_print_multiple (void)
{
	GnomePrintConfig *config;
	GnomePrintJob *job;
	TestRetval retval;
	gint i;
	gint max = 2;

	config = gnome_print_config_default ();
	job = gnome_print_job_new (config);
	if (!job || !config)
		return TEST_RETVAL_ERROR;
	
	for (i = 0; i < max; i++) {
		gnome_print_config_set (config, (const guchar *) "Printer", (const guchar *) "PDF");
		retval = gui_print (job, 100);
		if (retval != TEST_RETVAL_SUCCESS)
			return retval;
		
		gnome_print_config_set (config, (const guchar *) "Printer", (const guchar *) "GENERIC");
		retval = gui_print (job, 100);
		if (retval != TEST_RETVAL_SUCCESS)
			return retval;
	}

	g_object_unref (config);
	g_object_unref (job);
	
	config = gnome_print_config_default ();
	job = gnome_print_job_new (config);
	
	for (i = 0; i < max; i++) {
		gnome_print_config_set (config, (const guchar *) "Printer", (const guchar *) "PDF");
		retval = gui_print (job, 100);
		if (retval != TEST_RETVAL_SUCCESS)
			return retval;
		
		gnome_print_config_set (config, (const guchar *) "Printer", (const guchar *) "GENERIC");
		retval = gui_print (job, 100);
		if (retval != TEST_RETVAL_SUCCESS)
			return retval;
	}

	g_object_unref (config);
	g_object_unref (job);
	
	return retval;
}

static TestRetval
test_dispatch (gint num_test)
{
	TestRetval ret;

	g_print ("Running num_test %d\n[%s]\n", num_test, test_cases[num_test].description);

	ret = (test_cases[num_test].function ());

	switch (ret) {
	case TEST_RETVAL_SUCCESS:
		if (num_test == max_num_test)
			ret = TEST_RETVAL_SUCCESS_LAST;
		g_print (" Pass..\n");
		break;
	case TEST_RETVAL_ERROR:
		g_print (" Fail..\n");
		break;
	default:
		g_assert_not_reached ();
		break;
			
	}

	return ret;
}

static void
test_dialog_tree ()
{
	GnomePrintConfig *config;
	GPANode *node;
	GtkWidget *tree;

	config = gnome_print_config_default ();
	node = gnome_print_config_get_node (config);
	tree = gpa_tree_viewer_new (node);

	g_signal_connect (G_OBJECT (tree), "delete_event",
			  (GCallback) gtk_main_quit, NULL);
	gtk_main ();
}

static void
usage (gchar *error)
{
	g_print ("Error: %s\n\n", error);
	g_print ("Usage: test-dialog --num=[num_test] [--tree]\n\n");
	poptPrintHelp (popt, stdout, FALSE);
	exit (TEST_RETVAL_BAD_PARAMETERS);
	exit (TEST_RETVAL_BAD_PARAMETERS);
}

static void
parse_command_line (int argc, const char ** argv, gint *num_test)
{
	gchar *env_test;
	
	popt = poptGetContext ("test_gpa", argc, argv, options, 0);
	poptGetNextOpt (popt);

	if (option_list_tests)
		return;
	
	if (!num_test_str || num_test_str[0] == '\0') {
		if (tree)
			return;
		env_test = getenv ("TEST_NUM");
		if (env_test && env_test[0]) {
			g_print ("TEST_NUM env variable = %s\n", env_test);
			num_test_str = g_strdup (env_test);
		} else {
			return;
		}
	} 

	*num_test = atoi (num_test_str);

	if (*num_test == -1) {
		GList *list = NULL;
		/* We crash, this is part of the sanity check that allows us verify that crashes
		 * are treated as errors when they happen when running ./run-tests
		 * (Chema)
		 */
		g_print ("Crashing ...\n");
		list->next = NULL;
	}
	
	if (*num_test < 0 || (*num_test > max_num_test)) {
		gchar *error;
		error = g_strdup_printf ("num_test number out of range. Valid range is -1 to %d",
					 max_num_test);
		usage (error);
	}
	
	if (debug)
		g_print ("num_test is %d\n", *num_test);
}

static void
list_tests (void)
{
	gint i;
	gint max = max_num_test + 1;

	for (i = 0; i < max; i++)
		g_print ("%s\n",  test_cases[i].description);
}

static void
handle_sigsegv (int i)
{
	g_print ("\n\ntest-dialog crashed while running num_test %d [%s]\n",
		 num_test, test_cases[num_test].description);
	exit (TEST_RETVAL_CRASH);
}

int
main (int argc, const char * argv[])
{
	TestRetval ret = TEST_RETVAL_SUCCESS;
	struct sigaction sig;

	gtk_init (&argc, (char ***) &argv);

	parse_command_line (argc, argv, &num_test);

	/* Catch sigsegv signals */
	memset (&sig, 0, sizeof (sig));
	sig.sa_handler = handle_sigsegv;
	sigaction (SIGSEGV, &sig, NULL);

	if (option_list_tests) {
		list_tests ();
		return ret;
	}
	
	if (tree) {
		test_dialog_tree ();
		return 0;
	}

	if (num_test != MAGIC_NUMBER)
		ret = test_dispatch (num_test);
	else {
		GnomePrintConfig *config;
		GnomePrintJob *job;
		GtkWidget *dialog;
		gchar *s;
		job = gnome_print_job_new (NULL);
		config = gnome_print_job_get_config (job);
		if (TRUE) {
			GnomePrintContext *pc = NULL;

			g_object_get (G_OBJECT (job), "context", &pc, NULL);
			my_draw (pc);
			my_draw (pc);
			dialog = gnome_print_dialog_new (job, (const guchar *) "Test",
					GNOME_PRINT_DIALOG_RANGE | GNOME_PRINT_DIALOG_COPIES);
			gnome_print_dialog_construct_range_any (GNOME_PRINT_DIALOG (dialog),
					GNOME_PRINT_RANGE_ALL |
					GNOME_PRINT_RANGE_SELECTION, NULL, NULL, NULL);
			gtk_dialog_run (GTK_DIALOG (dialog));
			gtk_widget_destroy (dialog);
		} else {
			dialog = gnome_print_dialog_new (job, (const guchar *) "Test dialog 1", 0);
			gtk_widget_show (dialog);
			dialog = gnome_print_dialog_new (job, (const guchar *) "Test dialog 2", 0);
			gtk_widget_show (dialog);
			gtk_main ();
		}

		s = gnome_print_config_to_string (config, 0);
		
		g_object_unref (G_OBJECT (config));
		g_object_unref (G_OBJECT (job));

		g_print ("-->%s<--\n", s);

		g_free (s);
	}
		
	return ret;
}