"Initial commit to Gerrit"
[profile/ivi/libcroco.git] / tests / test4-main.c
1 /* -*- Mode: C; indent-tabs-mode: nil; c-basic-offset: 8 -*- */
2
3 /*
4  * This file is part of The Croco Library
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of version 2.1 of the GNU Lesser General Public
8  * License as published by the Free Software Foundation.
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 General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18  * USA
19  *
20  * Author: Dodji Seketeli
21  * See COPYRIGHTS file for copyrights information.
22  */
23
24
25 #include "cr-test-utils.h"
26 #include "libcroco.h"
27
28 /**
29  *@file
30  *Some test facilities for the #CROMParser class.
31  */
32
33 CRDocHandler *gv_test_handler = { 0 };
34
35 const guchar *gv_term_buf = "106";
36
37 const guchar *gv_decl_buf = "toto: tutu, tata";
38
39 const guchar *gv_decl_list_buf = "toto: titi; prop1:val1 ; prop2:val2;";
40
41 const guchar *gv_ruleset_buf = "s1 > s2 {toto: tutu, tata} ";
42
43 const guchar *gv_at_media_buf =
44         "@media print, toto {" "  BODY { font-size: 10pt }" "  }";
45
46 const guchar *gv_at_page_buf = "@page { size :8.5in 11in; margin: 2cm }";
47
48 const guchar *gv_at_charset_buf = "@charset \"ISO-8859-1\" ; ";
49
50 const guchar *gv_at_font_face_buf =
51         "@font-face {"
52         " font-family: \"Robson Celtic\";"
53         " src: url(\"http://site/fonts/rob-celt\")" "}";
54
55 const guchar *gv_at_import_buf = "@import \"subs.css\";";
56
57 static void display_help (char *prg_name);
58
59 static  void display_about (char *prg_name);
60
61 static enum  CRStatus test_cr_parser_parse (guchar * a_file_uri);
62
63 /**
64  *Displays the usage of the test
65  *facility.
66  *@param a_argc the argc variable passed to the main function.
67  *@param a_argv the argv variable passed to the main function.
68  */
69 static void
70 display_help (char *prg_name)
71 {
72         g_print ("\n\n");
73         g_print ("usage: %s <file-to-parse>\n", prg_name);
74         g_print ("\t <file-to-parse>: the file to parse\n");
75         g_print ("\n\n");
76         g_print ("Tests the cr_parser_parse () method.\n");
77         g_print ("Tests the parsing following the css core syntax\n");
78         g_print ("Returns OK if the status is CR_OK, KO otherwise\n");
79         g_print ("\n\n");
80 }
81
82 /**
83  *Displays the about text.
84  *@param a_argc the argc variable passed to the main function.
85  *@param a_argv the argv variable passed to the main function.
86  */
87 static void
88 display_about (char *prg_name)
89 {
90         g_print ("\n\n");
91         g_print ("%s is a libcroco CROMParser class test program.\n",
92                  prg_name);
93         g_print ("%s Parses a file and builds a CSS object model", prg_name);
94         g_print ("It should run on GNU compliants systems.\n");
95         g_print ("\n\n");
96         g_print ("Initial author: Dodji Seketeli <dodji@seketeli.org>.\n");
97         g_print ("\n\n");
98 }
99
100 /**
101  *The test of the cr_input_read_byte() method.
102  *Reads the each byte of a_file_uri using the
103  *cr_input_read_byte() method. Each byte is send to
104  *stdout.
105  *@param a_file_uri the file to read.
106  *@return CR_OK upon successfull completion of the
107  *function, an error code otherwise.
108  */
109 static enum CRStatus
110 test_cr_parser_parse (guchar * a_file_uri)
111 {
112         enum CRStatus status = CR_OK;
113         CROMParser *parser = NULL;
114         CRStyleSheet *stylesheet = NULL;
115
116         g_return_val_if_fail (a_file_uri, CR_BAD_PARAM_ERROR);
117
118         parser = cr_om_parser_new (NULL);
119         status = cr_om_parser_parse_file (parser, a_file_uri, CR_ASCII,
120                                           &stylesheet);
121         if (status == CR_OK && stylesheet) {
122                 cr_stylesheet_dump (stylesheet, stdout);
123                 cr_stylesheet_destroy (stylesheet);
124         }
125         cr_om_parser_destroy (parser);
126
127         return status;
128 }
129
130 static enum CRStatus
131 test_cr_statement_at_page_rule_parse_from_buf (void)
132 {
133         CRStatement *stmt = NULL ;
134         
135         stmt = cr_statement_at_page_rule_parse_from_buf 
136                 (gv_at_page_buf, CR_UTF_8) ;
137         if (!stmt) {
138                 return CR_ERROR ;                
139         }
140         cr_statement_destroy (stmt) ;        
141         return CR_OK ;
142 }
143
144 static enum CRStatus
145 test_cr_term_parse_expression_from_buf (void)
146 {
147         guchar *tmp_str = NULL;
148         CRTerm *term = NULL;
149
150         term = cr_term_parse_expression_from_buf (gv_term_buf, CR_UTF_8);
151
152         if (!term)
153                 return CR_ERROR;
154         tmp_str = cr_term_to_string (term);
155         if (term) {
156                 cr_term_destroy (term);
157                 term = NULL;
158         }
159         if (tmp_str) {
160                 g_free (tmp_str);
161                 tmp_str = NULL;
162         }
163         return CR_OK;
164 }
165
166 static enum CRStatus
167 test_cr_declaration_parse (void)
168 {
169         guchar *tmp_str = NULL;
170         CRDeclaration *decl = NULL;
171
172         decl = cr_declaration_parse_from_buf (NULL, gv_decl_buf, CR_UTF_8);
173         if (!decl)
174                 return CR_ERROR;
175         tmp_str = cr_declaration_to_string (decl, 2);
176
177         if (decl) {
178                 cr_declaration_destroy (decl);
179         }
180
181         if (tmp_str) {
182                 g_free (tmp_str);
183                 tmp_str = NULL;
184         }
185
186         return CR_OK;
187 }
188
189 static enum CRStatus
190 test_cr_declaration_parse_list (void)
191 {
192         GString *str = NULL;
193         guchar *tmp_str = NULL;
194         CRDeclaration *decl = NULL,
195                 *cur_decl = NULL;
196
197         decl = cr_declaration_parse_list_from_buf (gv_decl_list_buf,
198                                                    CR_UTF_8);
199         if (!decl)
200                 return CR_ERROR;
201         str = g_string_new (NULL);
202         for (cur_decl = decl; cur_decl; cur_decl = cur_decl->next) {
203                 tmp_str = cr_declaration_to_string (cur_decl, 2);
204                 if (tmp_str) {
205                         g_string_append_printf (str, "%s;", tmp_str);
206                         g_free (tmp_str);
207                         tmp_str = NULL;
208                 }
209
210         }
211         if (decl) {
212                 cr_declaration_destroy (decl);
213         }
214
215         if (str) {
216                 g_string_free (str, TRUE);
217                 str = NULL;
218         }
219
220         return CR_OK;
221 }
222
223 static enum CRStatus
224 test_cr_statement_ruleset_parse (void)
225 {
226         CRStatement *stmt = NULL;
227
228         stmt = cr_statement_ruleset_parse_from_buf (gv_ruleset_buf, CR_UTF_8);
229         g_return_val_if_fail (stmt, CR_ERROR);
230
231         if (stmt) {
232                 cr_statement_destroy (stmt);
233                 stmt = NULL;
234         }
235         return CR_OK;
236 }
237
238 static enum CRStatus
239 test_cr_statement_at_media_rule_parse (void)
240 {
241         CRStatement *stmt = NULL;
242
243         stmt = cr_statement_at_media_rule_parse_from_buf (gv_at_media_buf,
244                                                           CR_UTF_8);
245         g_return_val_if_fail (stmt, CR_ERROR);
246
247         if (stmt) {
248                 cr_statement_destroy (stmt);
249                 stmt = NULL;
250         }
251
252         return CR_OK;
253 }
254
255
256 static enum CRStatus
257 test_cr_statement_at_charset_rule_parse (void)
258 {
259         CRStatement *stmt = NULL;
260
261         stmt = cr_statement_at_charset_rule_parse_from_buf (gv_at_charset_buf,
262                                                             CR_UTF_8);
263         g_return_val_if_fail (stmt, CR_ERROR);
264         if (stmt) {
265                 cr_statement_destroy (stmt);
266                 stmt = NULL;
267         }
268
269         return CR_OK;
270 }
271
272 static enum CRStatus
273 test_cr_statement_font_face_rule_parse_from_buf (void)
274 {
275         CRStatement *stmt = NULL;
276
277         stmt = cr_statement_font_face_rule_parse_from_buf
278                 (gv_at_font_face_buf, CR_UTF_8);
279         g_return_val_if_fail (stmt, CR_ERROR);
280         if (stmt) {
281                 cr_statement_destroy (stmt);
282                 stmt = NULL;
283         }
284
285         return CR_OK;
286 }
287
288 static enum CRStatus
289 test_cr_statement_at_import_rule_parse_from_buf (void)
290 {
291         CRStatement *stmt = NULL;
292
293         stmt = cr_statement_at_import_rule_parse_from_buf (gv_at_import_buf,
294                                                            CR_UTF_8);
295         g_return_val_if_fail (stmt, CR_ERROR);
296         if (stmt) {
297                 cr_statement_destroy (stmt);
298                 stmt = NULL;
299         }
300
301         return CR_OK;
302 }
303
304 static enum CRStatus
305 test_cr_statement_parse_from_buf (void)
306 {
307         CRStatement *stmt = NULL;
308
309         stmt = cr_statement_parse_from_buf (gv_ruleset_buf, CR_UTF_8);
310         if (stmt) {
311                 cr_statement_destroy (stmt);
312                 stmt = NULL;
313         } else {
314                 return CR_ERROR;
315         }
316         stmt = cr_statement_parse_from_buf (gv_at_media_buf, CR_UTF_8);
317         if (stmt) {
318                 cr_statement_destroy (stmt);
319                 stmt = NULL;
320         } else {
321                 return CR_ERROR;
322         }
323         stmt = cr_statement_parse_from_buf (gv_at_page_buf, CR_UTF_8);
324         if (stmt) {
325                 cr_statement_destroy (stmt);
326                 stmt = NULL;
327         } else {
328                 return CR_ERROR;
329         }
330         stmt = cr_statement_parse_from_buf (gv_at_charset_buf, CR_UTF_8);
331         if (stmt) {
332                 cr_statement_destroy (stmt);
333                 stmt = NULL;
334         } else {
335                 return CR_ERROR;
336         }
337         stmt = cr_statement_parse_from_buf (gv_at_font_face_buf, CR_UTF_8);
338         if (stmt) {
339                 cr_statement_destroy (stmt);
340                 stmt = NULL;
341         } else {
342                 return CR_ERROR;
343         }
344         stmt = cr_statement_parse_from_buf (gv_at_import_buf, CR_UTF_8);
345         if (stmt) {
346                 cr_statement_destroy (stmt);
347                 stmt = NULL;
348         } else {
349                 return CR_ERROR;
350         }
351
352         return CR_OK;
353 }
354
355 /**
356  *The entry point of the testing routine.
357  */
358 int
359 main (int argc, char **argv)
360 {
361         struct Options options;
362         enum CRStatus status = CR_OK;
363
364         status = test_cr_term_parse_expression_from_buf ();
365         if (status != CR_OK) {
366                 g_print ("\ntest \"cr_term_parse_expression_from_buf failed\"");
367         }
368         status = test_cr_declaration_parse ();
369         if (status != CR_OK) {
370                 g_print ("\n test \"cr_declaration_parse() failed\"\n");
371         }
372
373         status = test_cr_declaration_parse_list ();
374         if (status != CR_OK) {
375                 g_print ("\ntest cr_declaration_parse_list() failed\n");
376         }
377         status = test_cr_statement_ruleset_parse ();
378         if (status != CR_OK) {
379                 g_print ("\ntest cr_statement_ruleset_parse() failed\n");
380         }
381
382         status = test_cr_statement_at_media_rule_parse ();
383         if (status != CR_OK) {
384                 g_print ("\ntest cr_statement_at_media_rule_parse() failed\n");
385         }
386         test_cr_statement_at_page_rule_parse_from_buf ();
387         if (status != CR_OK) {
388                 g_print ("\ntest cr_statement_at_page_rule_parse() failed\n");
389                 return 0;
390         }
391
392         status = test_cr_statement_at_charset_rule_parse ();
393         if (status != CR_OK) {
394                 g_print ("\ntest cr_statement_at_charset_rule_parse() failed\n");
395         }
396
397         status = test_cr_statement_font_face_rule_parse_from_buf ();
398         if (status != CR_OK) {
399                 g_print ("\ntest cr_statement_font_face_rule_parse_from_buf() failed\n");
400         }
401
402         test_cr_statement_at_import_rule_parse_from_buf ();
403         if (status != CR_OK) {
404                 g_print ("\ntest cr_statement_at_import_rule() failed\n");
405         }
406
407         status = test_cr_statement_parse_from_buf ();
408         if (status != CR_OK) {
409                 g_print ("\ntest cr_statement_parse_from_buf() failed\n");
410         }
411
412         cr_test_utils_parse_cmd_line (argc, argv, &options);
413
414         if (options.display_help == TRUE) {
415                 display_help (argv[0]);
416                 return 0;
417         }
418
419         if (options.display_about == TRUE) {
420                 display_about (argv[0]);
421                 return 0;
422         }
423
424         if (options.files_list == NULL) {
425                 display_help (argv[0]);
426                 return 0;
427         }
428
429         status = test_cr_parser_parse (options.files_list[0]);
430         if (status != CR_OK) {
431                 g_print ("\nKO\n");
432         }
433
434         return 0;
435 }