state: allow different modes for calculating consumed modifiers
[platform/upstream/libxkbcommon.git] / test / rules-file.c
1 /*
2  * Copyright © 2012 Ran Benita <ran234@gmail.com>
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21  * DEALINGS IN THE SOFTWARE.
22  */
23
24 #include "test.h"
25 #include "xkbcomp-priv.h"
26 #include "rules.h"
27
28 struct test_data {
29     /* Rules file */
30     const char *rules;
31
32     /* Input */
33     const char *model;
34     const char *layout;
35     const char *variant;
36     const char *options;
37
38     /* Expected output */
39     const char *keycodes;
40     const char *types;
41     const char *compat;
42     const char *symbols;
43
44     /* Or set this if xkb_components_from_rules() should fail. */
45     bool should_fail;
46 };
47
48 static bool
49 test_rules(struct xkb_context *ctx, struct test_data *data)
50 {
51     bool passed;
52     const struct xkb_rule_names rmlvo = {
53         data->rules, data->model, data->layout, data->variant, data->options
54     };
55     struct xkb_component_names kccgst;
56
57     fprintf(stderr, "\n\nChecking : %s\t%s\t%s\t%s\t%s\n", data->rules,
58             data->model, data->layout, data->variant, data->options);
59
60     if (data->should_fail)
61         fprintf(stderr, "Expecting: FAILURE\n");
62     else
63         fprintf(stderr, "Expecting: %s\t%s\t%s\t%s\n",
64                 data->keycodes, data->types, data->compat, data->symbols);
65
66     if (!xkb_components_from_rules(ctx, &rmlvo, &kccgst)) {
67         fprintf(stderr, "Received : FAILURE\n");
68         return data->should_fail;
69     }
70
71     fprintf(stderr, "Received : %s\t%s\t%s\t%s\n",
72             kccgst.keycodes, kccgst.types, kccgst.compat, kccgst.symbols);
73
74     passed = streq(kccgst.keycodes, data->keycodes) &&
75              streq(kccgst.types, data->types) &&
76              streq(kccgst.compat, data->compat) &&
77              streq(kccgst.symbols, data->symbols);
78
79     free(kccgst.keycodes);
80     free(kccgst.types);
81     free(kccgst.compat);
82     free(kccgst.symbols);
83
84     return passed;
85 }
86
87 int
88 main(int argc, char *argv[])
89 {
90     struct xkb_context *ctx;
91
92     ctx = test_get_context(0);
93     assert(ctx);
94
95     struct test_data test1 = {
96         .rules = "simple",
97
98         .model = "my_model", .layout = "my_layout", .variant = "my_variant",
99         .options = "my_option",
100
101         .keycodes = "my_keycodes", .types = "my_types",
102         .compat = "my_compat|some:compat",
103         .symbols = "my_symbols+extra_variant",
104     };
105     assert(test_rules(ctx, &test1));
106
107     struct test_data test2 = {
108         .rules = "simple",
109
110         .model = "", .layout = "", .variant = "", .options = "",
111
112         .keycodes = "default_keycodes", .types = "default_types",
113         .compat = "default_compat", .symbols = "default_symbols",
114     };
115     assert(test_rules(ctx, &test2));
116
117     struct test_data test3 = {
118         .rules = "groups",
119
120         .model = "pc104", .layout = "foo", .variant = "", .options = "",
121
122         .keycodes = "something(pc104)", .types = "default_types",
123         .compat = "default_compat", .symbols = "default_symbols",
124     };
125     assert(test_rules(ctx, &test3));
126
127     struct test_data test4 = {
128         .rules = "groups",
129
130         .model = "foo", .layout = "ar", .variant = "bar", .options = "",
131
132         .keycodes = "default_keycodes", .types = "default_types",
133         .compat = "default_compat", .symbols = "my_symbols+(bar)",
134     };
135     assert(test_rules(ctx, &test4));
136
137     struct test_data test5 = {
138         .rules = "simple",
139
140         .model = NULL, .layout = "my_layout,second_layout", .variant = "my_variant",
141         .options = "my_option",
142
143         .should_fail = true
144     };
145     assert(test_rules(ctx, &test5));
146
147     struct test_data test6 = {
148         .rules = "index",
149
150         .model = "", .layout = "br,al,cn,az", .variant = "",
151         .options = "some:opt",
152
153         .keycodes = "default_keycodes", .types = "default_types",
154         .compat = "default_compat",
155         .symbols = "default_symbols+extra:1+extra:2+extra:3+extra:4",
156     };
157     assert(test_rules(ctx, &test6));
158
159     struct test_data test7 = {
160         .rules = "multiple-options",
161
162         .model = "my_model", .layout = "my_layout", .variant = "my_variant",
163         .options = "option3,option1,colon:opt,option11",
164
165         .keycodes = "my_keycodes", .types = "my_types",
166         .compat = "my_compat+some:compat+group(bla)",
167         .symbols = "my_symbols+extra_variant+compose(foo)+keypad(bar)+altwin(menu)",
168     };
169     assert(test_rules(ctx, &test7));
170
171     xkb_context_unref(ctx);
172     return 0;
173 }