1 #include <glib.h>
2 #include <glib/gstdio.h>
3 #include <unistd.h>
4 #include <config.h>
5 #include "common.h"
6 
7 static gchar* data_dir = NULL;
8 static gchar* temp_dir = NULL;
9 static gchar* example_dir = NULL;
10 
11 /*******************************************************************************
12  * SETUP AND TEARDOWN
13  ******************************************************************************/
14 
15 static void
setup(gpointer fixture,gconstpointer data)16 setup(gpointer fixture, gconstpointer data)
17 {
18   if(!g_file_test(temp_dir, G_FILE_TEST_EXISTS)){
19     if(g_mkdir(temp_dir, 0777) < 0)
20       g_warning("Could not create %s", temp_dir);
21   }
22 
23   else{
24     GDir* dir = g_dir_open(temp_dir, 0, NULL);
25     const gchar* filename = NULL;
26     while (filename = g_dir_read_name(dir))
27       g_remove (g_build_filename(temp_dir, filename, NULL));
28   }
29 }
30 
31 static void
teardown(gpointer fixture,gconstpointer data)32 teardown(gpointer fixture, gconstpointer data)
33 {
34   delete_if_exists(temp_dir);
35 }
36 
37 /*******************************************************************************
38  * TEST FUNCTIONS
39  ******************************************************************************/
40 
41 /** test_run_and_quit:
42  * This is the simpliest test. It just launches denemo and quit.
43  */
44 static void
test_run_and_quit(gpointer fixture,gconstpointer data)45 test_run_and_quit (gpointer fixture, gconstpointer data)
46 {
47   if (g_test_subprocess ())
48     {
49       execl(DENEMO, DENEMO, "-n", "-e", "-a", "(d-Quit)", NULL);
50       g_warn_if_reached ();
51     }
52   g_test_trap_subprocess (NULL, 0, 0);
53   g_test_trap_assert_passed ();
54 }
55 
56 /** test_invalid_scheme
57  * Tests the --fatal-scheme-errors program argument.
58  */
59 static void
test_invalid_scheme(gpointer fixture,gconstpointer data)60 test_invalid_scheme(gpointer fixture, gconstpointer data)
61 {
62   if (g_test_subprocess ())
63     {
64       execl(DENEMO, DENEMO, "-n", "--fatal-scheme-errors", "-a", "(d-InvalidSchemeFunction)(d-Quit)", NULL);
65       g_warn_if_reached ();
66     }
67   g_test_trap_subprocess (NULL, 0, 0);
68   g_test_trap_assert_failed ();
69 }
70 
71 /** test_scheme_log
72  * Tests (d-LogError) scheme function
73  */
74 static void
test_scheme_log(gpointer fixture,gconstpointer data)75 test_scheme_log(gpointer fixture, gconstpointer data)
76 {
77   if (g_test_subprocess ())
78     {
79       execl(DENEMO, DENEMO, "-n", "-e", "--verbose", "-a",
80             "(d-Debug \"This is debug\")"
81             "(d-Info \"This is info\")"
82             "(d-Message \"This is message\")"
83             "(d-Warning \"This is warning\")"
84             "(d-Critical \"This is critical\")"
85             "(d-Quit)",
86             NULL);
87       g_warn_if_reached ();
88     }
89   g_test_trap_subprocess (NULL, 0, 0);
90   g_test_trap_assert_passed ();
91 }
92 
93 /** test_scheme_log_error
94  * Tests (d-LogError) scheme function
95  */
96 static void
test_scheme_log_error(gpointer fixture,gconstpointer data)97 test_scheme_log_error(gpointer fixture, gconstpointer data)
98 {
99   if (g_test_subprocess ())
100     {
101       execl(DENEMO, DENEMO, "-n", "--fatal-scheme-errors", "-a", "(d-Error \"This error is fatal\")(d-Quit)", NULL);
102       g_warn_if_reached ();
103     }
104   g_test_trap_subprocess (NULL, 0, 0);
105   g_test_trap_assert_failed ();
106 }
107 
108 /** test_thumbnailer
109  * Tries to create a thumbnail from a file and check that its exists
110  */
111 static void
test_thumbnailer(gpointer fixture,gconstpointer data)112 test_thumbnailer(gpointer fixture, gconstpointer data)
113 {
114   gchar* thumbnail = g_build_filename(temp_dir, "thumbnail.png", NULL);
115   gchar* scheme = g_strdup_printf( "(d-CreateThumbnail #f \"%s\")(d-Exit)", thumbnail);
116   gchar* input = g_build_filename(data_dir, "denemo", "blank.denemo", NULL);
117 
118   g_test_print("Running scheme: %s %s\n", scheme, input);
119   if (g_test_subprocess ())
120     {
121       execl(DENEMO, DENEMO, "-n", "-e", "-V", "-a", scheme, input, NULL);
122       g_warn_if_reached ();
123     }
124 
125   g_test_trap_subprocess (NULL, 0, 0);
126   g_test_trap_assert_passed ();
127   g_assert(g_file_test(thumbnail, G_FILE_TEST_EXISTS));
128   g_assert(g_remove(thumbnail) >= 0);
129 }
130 
131 /*******************************************************************************
132  * MAIN
133  ******************************************************************************/
134 
135 int
main(int argc,char * argv[])136 main (int argc, char *argv[])
137 {
138   g_test_init (&argc, &argv, NULL);
139 
140   if(!g_file_test(DENEMO, G_FILE_TEST_EXISTS))
141     g_error("Denemo has not been compiled successfully");
142 
143   if(!data_dir)
144     data_dir = g_build_filename(PACKAGE_SOURCE_DIR, "tests", FIXTURES_DIR, NULL);
145 
146   if(!temp_dir)
147     temp_dir = g_build_filename(g_get_current_dir (), TEMP_DIR, NULL);
148 
149   if(!example_dir)
150     example_dir = g_build_filename(PACKAGE_SOURCE_DIR, EXAMPLE_DIR, NULL);
151 
152   g_test_add ("/unit/run-and-quit", void, NULL, setup, test_run_and_quit, teardown);
153 
154   //g_test_add ("/unit/invalid-scheme", void, NULL, setup, test_invalid_scheme, teardown);
155   g_test_add ("/unit/scheme-log", void, NULL, setup, test_scheme_log, teardown);
156   g_test_add ("/unit/scheme-log-error", void, NULL, setup, test_scheme_log_error, teardown);
157   g_test_add ("/unit/thumbnailer", void, NULL, setup, test_thumbnailer, teardown);
158 
159   return g_test_run ();
160 }
161