1ed14c71946f14d5df571689d516618f34b56f0b
[apps/core/preloaded/print-service.git] / TC / test-automation.c
1 /*
2 *       Printservice
3 *
4 * Copyright 2012  Samsung Electronics Co., Ltd
5
6 * Licensed under the Flora License, Version 1.1 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9
10 * http://floralicense.org/license/
11
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *
18 */
19
20 #include <stdbool.h>
21 #include <cups/cups.h>
22 #include <cups/ppd.h>
23 #include <pt_optionmapping.h>
24 #include <pt_debug.h>
25 #include <pt_test.h>
26 #include <pt_db.h>
27
28 #define GR "\E[32m"
29 #define RD "\E[31m"
30 #define YE "\E[33m"
31 #define RS "\E[0m"
32 #define BOLD ""
33 #define RS_BOLD ""
34
35 static int REAL_PAPERSIZE_MAX;
36
37 static counter X[] = {
38         { .id = PT_OPTION_ID_QUALITY, .desc = "QL", .current = PT_QUALITY_DRAFT,.min = PT_QUALITY_DRAFT, .max = PT_QUALITY_ANY },
39         { .id = PT_OPTION_ID_PAPERSIZE, .desc = "PS" , .current = PT_PAPER_A4, .min = PT_PAPER_A4, .max = PT_PAPER_SIZE_MAX },
40         { .id = PT_OPTION_ID_GRAYSCALE,.desc = "GL", .current = PT_GRAYSCALE_GRAYSCALE, .min = PT_GRAYSCALE_GRAYSCALE, .max = PT_GRAYSCALE_ANY },
41         { .id = PT_OPTION_ID_PAPER, .desc = "PP", .current = PT_PAPER_NORMAL, .min = PT_PAPER_NORMAL, .max = PT_PAPER_ANY },
42         {NULL, NULL, NULL, NULL, NULL}
43 };
44
45 const static int N = (sizeof(X) / sizeof(counter)) - 1; // Remove {NULL} value
46
47 bool reliable_set_choice(int id, int choice)
48 {
49         int old_choice = -1, new_choice = -1, result = -1;
50         old_choice = pt_get_selected(id);
51         result = pt_set_choice(id, choice);
52         new_choice = pt_get_selected(id);
53
54         if ((old_choice == -1) ||
55                         (result == -1) ||
56                         (new_choice == -1)) {
57                 PT_DEBUG("OptionMapping:");
58                 PT_DEBUG("Internal Error");
59                 exit(EXIT_FAILURE);
60         }
61
62         if ((result == choice) &&
63                         (result == new_choice)) { // choice is enabled
64                 return true;
65         } else if ((old_choice == result) &&
66                            (result == new_choice)) { //choice is disabled
67                 return false;
68         } else { //OM internal error
69                 PT_DEBUG("OptionMapping:");
70                 PT_DEBUG("[Error] pt_set_choice doesn't work correctly");
71                 exit(EXIT_FAILURE);
72         }
73 }
74
75 void check_state(counter *array, int count)
76 {
77         if (count == N) {
78                 check_all();
79         } else {
80                 int i;
81                 for (i = array[count].min; i < array[count].max; ++i) {
82                         //int old_choice = -1, new_choice = -1, result = -1;
83                         bool set_choice = false;
84                         array[count].current = i;
85                         state st = save_state();
86                         print_groups_before(count);
87
88                         set_choice = reliable_set_choice(array[count].id, i);
89                         if (set_choice) {
90                                 printf(YE "%s: " RS, array[count].desc);
91                                 printf(GR "%d " RS, array[count].current);
92                                 print_groups_after(count);
93                                 check_state(array, ++count);
94                         } else {
95                                 printf(YE "%s: " RS, array[count].desc);
96                                 printf(RD "%d " RS, i);
97                                 print_groups_after(count);
98                                 continue;
99                         }
100                         restore_state(st, &array[--count]);
101                 }
102         }
103 }
104
105 void swap(int a,int b)
106 {
107         counter t=X[a];
108         X[a]=X[b];
109         X[b]=t;
110 }
111
112 void generate(int k)
113 {
114         static int cnt = 0;
115         if (k==N) {
116                 cnt++;
117                 //int i;
118                 state st;
119                 //const char *string = BOLD YE "%s %s %s %s\n" RS RS_BOLD;
120                 printf("GROUP %d [DEFAULT VALUES]\n", cnt);
121                 st = get_current_state();
122                 // FIXME It's hack! As order of GROUPS can be changed in future
123                 printf(YE "%d %d %d %d\n" RS, st.quality,st.papersize,st.grayscale,st.paper);
124                 printf("\n");
125                 check_state(X, 0);
126         } else {
127                 int j;
128                 for (j=k; j<N; j++) {
129                         swap(k,j);
130                         generate(k+1);
131                         swap(k,j);
132                 }
133         }
134 }
135
136 void init_groups(counter *array)
137 {
138         int cnt = 0;
139         while (array[cnt].desc) {
140                 if (!strcmp(array[cnt].desc, "QL")) {
141                         array[cnt].current = pt_get_selected(PT_OPTION_ID_QUALITY);
142                 } else if (!strcmp(array[cnt].desc, "PS")) {
143                         array[cnt].max = pt_get_print_option_papersize_num();
144                         REAL_PAPERSIZE_MAX = array[cnt].max;
145                         if (REAL_PAPERSIZE_MAX < PT_PAPER_A4) {
146                                 PT_DEBUG("[papersize] max value [%d] is out of the correct values: [%d-%d]",\
147                                                  REAL_PAPERSIZE_MAX, PT_PAPER_A4, PT_PAPER_SIZE_MAX);
148                                 exit(EXIT_FAILURE);
149                         }
150                         array[cnt].current = pt_get_selected(PT_OPTION_ID_PAPERSIZE);
151                 } else if (!strcmp(array[cnt].desc, "GL")) {
152                         array[cnt].current = pt_get_selected(PT_OPTION_ID_GRAYSCALE);
153                 } else if (!strcmp(array[cnt].desc, "PP")) {
154                         array[cnt].current = pt_get_selected(PT_OPTION_ID_PAPER);
155                 } else {
156                         printf("Unknown description of option!");
157                         exit(EXIT_FAILURE);
158                 }
159                 cnt++;
160         }
161 }
162
163
164
165 int main(int argc, char **argv)
166 {
167         ppd_file_t *ppd;
168         //option_list *option_list;
169         if (!argv[1]) {
170                 PT_DEBUG("No any ppd file");
171                 exit(EXIT_FAILURE);
172         }
173         ppd = open_ppd_file(argv[1]);
174         pt_parse_options(ppd);
175         init_groups(X);
176
177         // Print current state of OptionMapping
178         // before any manipulation
179         state st = get_current_state();
180         printf(BOLD YE "DEFAULT:\n" RS RS_BOLD);
181
182         printf(BOLD YE "%s %s %s %s\n" RS RS_BOLD, X[0].desc,X[1].desc,X[2].desc,X[3].desc);
183         // FIXME It's hack! As order of GROUPS can be changed in future
184         printf(YE "%d %d %d %d\n" RS, st.quality,st.papersize,st.grayscale,st.paper);
185         generate(0);
186         exit(EXIT_SUCCESS);
187 }
188
189 state save_state()
190 {
191         state st;
192         st.quality = pt_get_selected(PT_OPTION_ID_QUALITY);
193         st.paper = pt_get_selected(PT_OPTION_ID_PAPER);
194         st.grayscale = pt_get_selected(PT_OPTION_ID_GRAYSCALE);
195         st.papersize = pt_get_selected(PT_OPTION_ID_PAPERSIZE);
196
197         PT_DEBUG("SAVED:");
198         print_state(st);
199
200         if ((st.quality == -1) || (st.paper == -1) ||
201                         (st.grayscale == -1) || (st.papersize == -1)) {
202                 PT_DEBUG("Returned value not correct:");
203                 PT_DEBUG("[GRAYSCALE]: %d", st.grayscale);
204                 PT_DEBUG("[QUALITY]: %d", st.quality);
205                 PT_DEBUG("[PAPER]: %d", st.paper);
206                 PT_DEBUG("[PAPERSIZE]: %d", st.papersize);
207                 exit(EXIT_FAILURE);
208         }
209         return st;
210 }
211
212 state get_current_state()
213 {
214         state st;
215         st.quality = pt_get_selected(PT_OPTION_ID_QUALITY);
216         st.paper = pt_get_selected(PT_OPTION_ID_PAPER);
217         st.grayscale = pt_get_selected(PT_OPTION_ID_GRAYSCALE);
218         st.papersize = pt_get_selected(PT_OPTION_ID_PAPERSIZE);
219
220         if ((st.quality < PT_QUALITY_DRAFT) || (st.quality >= PT_QUALITY_ANY)) {
221                 PT_DEBUG("[quality] choice [%d] is out of the correct values: [%d-%d]",\
222                                  st.quality, PT_QUALITY_DRAFT, (PT_QUALITY_ANY-1));
223                 exit(EXIT_FAILURE);
224         }
225         if ((st.paper < PT_PAPER_NORMAL) || (st.paper >= PT_PAPER_ANY)) {
226                 PT_DEBUG("[paper] choice [%d] is out of the correct values: [%d-%d]",\
227                                  st.paper, PT_PAPER_NORMAL, (PT_PAPER_ANY-1));
228                 exit(EXIT_FAILURE);
229         }
230         if ((st.grayscale < PT_GRAYSCALE_GRAYSCALE) || (st.grayscale >= PT_GRAYSCALE_ANY)) {
231                 PT_DEBUG("[grayscale] choice [%d] is out of the correct values: [%d-%d]",\
232                                  st.grayscale, PT_GRAYSCALE_GRAYSCALE, (PT_GRAYSCALE_ANY-1));
233                 exit(EXIT_FAILURE);
234         }
235         if ((st.papersize < PT_PAPER_A4) || (st.grayscale >= REAL_PAPERSIZE_MAX)) {
236                 PT_DEBUG("[papersize] choice [%d] is out of the correct values: [%d-%d]",\
237                                  st.papersize, PT_PAPER_A4, (REAL_PAPERSIZE_MAX-1));
238                 exit(EXIT_FAILURE);
239         }
240         return st;
241 }
242
243 void print_cur_state()
244 {
245         state st;
246         st.quality = pt_get_selected(PT_OPTION_ID_QUALITY);
247         st.paper = pt_get_selected(PT_OPTION_ID_PAPER);
248         st.grayscale = pt_get_selected(PT_OPTION_ID_GRAYSCALE);
249         st.papersize = pt_get_selected(PT_OPTION_ID_PAPERSIZE);
250         PT_DEBUG("CURRENT STATE");
251         print_state(st);
252 }
253
254 void print_groups_before(int num)
255 {
256         int val = 0;
257         while ((val < num) && (X[val].desc != NULL)) {
258                 printf(YE "%s: " RS, X[val].desc);
259                 printf(GR "%d: " RS, X[val].current);
260                 val++;
261         }
262 }
263
264 void print_groups_after(int num)
265 {
266         //int cnt = num;
267         while (X[++num].desc) {
268                 printf(YE "%s: " RS, X[num].desc);
269                 printf("%d: ", X[num].current);
270         }
271         printf("\n");
272 }
273
274 void print_state(state st)
275 {
276         PT_DEBUG("[GRAYSCALE]: %d", st.grayscale);
277         PT_DEBUG("[QUALITY]: %d", st.quality);
278         PT_DEBUG("[PAPER]: %d", st.paper);
279         PT_DEBUG("[PAPERSIZE]: %d, %s", st.papersize, \
280                          pt_get_print_option_papersize(st.papersize));
281         // TEST
282         int i = 0;
283         for (i = 0; i < pt_get_print_option_papersize_num(); ++i) {
284                 PT_DEBUG("TEST [PAPERSIZE]: %d, %s", i, \
285                                  pt_get_print_option_papersize(i));
286         }
287 }
288
289 void restore_state(state st, counter *group)
290 {
291         int set_choice = false;
292         pt_print_option_e option = group->id;
293
294         switch (option) {
295         case PT_OPTION_ID_QUALITY:
296                 set_choice = reliable_set_choice(option, st.quality);
297                 group->current = st.quality;
298                 break;
299         case PT_OPTION_ID_PAPER:
300                 set_choice = reliable_set_choice(option, st.paper);
301                 group->current = st.paper;
302                 break;
303         case PT_OPTION_ID_PAPERSIZE:
304                 set_choice = reliable_set_choice(option, st.papersize);
305                 group->current = st.papersize;
306                 break;
307         case PT_OPTION_ID_GRAYSCALE:
308                 set_choice = reliable_set_choice(option, st.grayscale);
309                 group->current = st.grayscale;
310                 break;
311         default:
312                 PT_DEBUG("[Error] Got unknown state");
313                 exit(EXIT_FAILURE);
314         }
315
316         if (!set_choice) {
317                 PT_DEBUG("Can't restore choice back");
318                 exit(EXIT_FAILURE);
319         }
320         PT_DEBUG("RESTORED:");
321         print_state(st);
322 }
323
324 void check_all()
325 {
326         check_selected_choice();
327         check_selected_choice_is_enabled();
328         check_all_choices_disabled();
329 }
330
331 void check_selected_choice()
332 {
333         if (!pt_selected_choice(PT_OPTION_ID_PAPERSIZE, PT_ORIENTATION_PORTRAIT)) {
334                 PT_DEBUG("No any selected choice in PT_OPTION_ID_PAPERSIZE option");
335                 exit(EXIT_FAILURE);
336         }
337         if (!pt_selected_choice(PT_OPTION_ID_QUALITY, PT_ORIENTATION_PORTRAIT)) {
338                 PT_DEBUG("No any selected choice in PT_OPTION_ID_QUALITY option");
339                 exit(EXIT_FAILURE);
340         }
341         if (!pt_selected_choice(PT_OPTION_ID_PAPER, PT_ORIENTATION_PORTRAIT)) {
342                 PT_DEBUG("No any selected choice in PT_OPTION_ID_PAPER option");
343                 exit(EXIT_FAILURE);
344         }
345         if (!pt_selected_choice(PT_OPTION_ID_GRAYSCALE, PT_ORIENTATION_PORTRAIT)) {
346                 PT_DEBUG("No any selected choice in PT_OPTION_ID_GRAYSCALE option");
347                 exit(EXIT_FAILURE);
348         }
349 }
350
351 void check_selected_choice_is_enabled()
352 {
353         // TEST PAPERSIZE
354         int choice = -1;
355         choice = pt_get_selected(PT_OPTION_ID_PAPERSIZE);
356         if (choice == -1) {
357                 PT_DEBUG("function pt_get_selected returned -1");
358                 exit(EXIT_FAILURE);
359         }
360         if (!pt_is_enabled(PT_OPTION_ID_PAPERSIZE, choice)) {
361                 PT_DEBUG("We've got selected choice which is not enabled");
362                 exit(EXIT_FAILURE);
363         }
364
365         // TEST QUALITY
366         choice = -1;
367         choice = pt_get_selected(PT_OPTION_ID_QUALITY);
368         if (choice == -1) {
369                 PT_DEBUG("function pt_get_selected returned -1");
370                 exit(EXIT_FAILURE);
371         }
372         if (!pt_is_enabled(PT_OPTION_ID_QUALITY, choice)) {
373                 PT_DEBUG("We've got selected choice which is not enabled");
374                 exit(EXIT_FAILURE);
375         }
376
377         // TEST PAPER
378         choice = -1;
379         choice = pt_get_selected(PT_OPTION_ID_PAPER);
380         if (choice == -1) {
381                 PT_DEBUG("function pt_get_selected returned -1");
382                 exit(EXIT_FAILURE);
383         }
384         if (!pt_is_enabled(PT_OPTION_ID_PAPER, choice)) {
385                 PT_DEBUG("We've got selected choice which is not enabled");
386                 exit(EXIT_FAILURE);
387         }
388
389         // TEST GRAYSCALE
390         choice = -1;
391         choice = pt_get_selected(PT_OPTION_ID_GRAYSCALE);
392         if (choice == -1) {
393                 PT_DEBUG("function pt_get_selected returned -1");
394                 exit(EXIT_FAILURE);
395         }
396         if (!pt_is_enabled(PT_OPTION_ID_GRAYSCALE, choice)) {
397                 PT_DEBUG("We've got selected choice which is not enabled");
398                 exit(EXIT_FAILURE);
399         }
400 }
401
402 bool check_all_choices_disabled()
403 {
404         bool papersize = true, quality = true;
405         bool grayscale = true, paper = true;
406         bool general = true;
407         papersize  = check_all_choices_disabled_papersize();
408         grayscale = check_all_choices_disabled_grayscale();
409         quality =  check_all_choices_disabled_quality();
410         paper =  check_all_choices_disabled_paper();
411         if (papersize || quality || grayscale || paper) {
412                 PT_DEBUG("TEST [check_all_choices_disabled] failed");
413         } else {
414                 general = false;
415         }
416         return (general);
417 }
418
419 int check_all_choices_disabled_papersize()
420 {
421         bool enabled = false;
422         int i;
423         for (i = PT_PAPER_A4; i < PT_PAPER_SIZE_MAX; ++i) {
424                 if (pt_is_enabled(PT_OPTION_ID_PAPERSIZE, i)) {
425                         enabled = true;
426                 }
427         }
428         if (!enabled) {
429                 PT_DEBUG("All choices are DISABLED in PAPERSIZE");
430                 exit(EXIT_FAILURE);
431         }
432         return (!enabled);
433 }
434
435 int check_all_choices_disabled_grayscale()
436 {
437         bool enabled = false;
438         int i;
439         for (i = PT_GRAYSCALE_GRAYSCALE; i < PT_GRAYSCALE_MAX; ++i) {
440                 if (pt_is_enabled(PT_OPTION_ID_GRAYSCALE, i)) {
441                         enabled = true;
442                 }
443         }
444         if (!enabled) {
445                 PT_DEBUG("All choices are DISABLED in GRAYSCALE");
446                 exit(EXIT_FAILURE);
447         }
448         return (!enabled);
449 }
450
451 int check_all_choices_disabled_quality()
452 {
453         bool enabled = false;
454         int i;
455         for (i = PT_QUALITY_DRAFT; i < PT_QUALITY_MAX; ++i) {
456                 if (pt_is_enabled(PT_OPTION_ID_QUALITY, i)) {
457                         enabled = true;
458                 }
459         }
460         if (!enabled) {
461                 PT_DEBUG("All choices are DISABLED in QUALITY");
462                 exit(EXIT_FAILURE);
463         }
464         return (!enabled);
465 }
466
467 int check_all_choices_disabled_paper()
468 {
469         bool enabled = false;
470         int i;
471         for (i = PT_PAPER_NORMAL; i < PT_PAPER_MAX; ++i) {
472                 if (pt_is_enabled(PT_OPTION_ID_PAPER, i)) {
473                         enabled = true;
474                 }
475         }
476         if (!enabled) {
477                 PT_DEBUG("All choices are DISABLED in PAPER");
478                 exit(EXIT_FAILURE);
479         }
480         return (!enabled);
481 }
482
483 void set_custom_choice(int qual, int paper, int gr_scale, int paper_size)
484 {
485         pt_set_choice(PT_OPTION_ID_PAPERSIZE, paper);
486         check_all();
487         pt_set_choice(PT_OPTION_ID_QUALITY, qual);
488         check_all();
489         pt_set_choice(PT_OPTION_ID_GRAYSCALE, gr_scale);
490         check_all();
491         pt_set_choice(PT_OPTION_ID_PAPERSIZE, paper_size);
492 }
493
494 ppd_file_t *open_ppd_file(const char *filename)
495 {
496         ppd_file_t *ppd = ppdOpenFile(filename);
497         if (!ppd) {
498                 PT_DEBUG("PPD file can't be loaded");
499                 exit(EXIT_FAILURE);
500         }
501         return ppd;
502 }