1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright © 2020 Google LLC
3bf215546Sopenharmony_ci *
4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
5bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
6bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation
7bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
9bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
10bf215546Sopenharmony_ci *
11bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next
12bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the
13bf215546Sopenharmony_ci * Software.
14bf215546Sopenharmony_ci *
15bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20bf215546Sopenharmony_ci * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21bf215546Sopenharmony_ci * IN THE SOFTWARE.
22bf215546Sopenharmony_ci */
23bf215546Sopenharmony_ci
24bf215546Sopenharmony_ci#include <gtest/gtest.h>
25bf215546Sopenharmony_ci#include <driconf.h>
26bf215546Sopenharmony_ci#include <xmlconfig.h>
27bf215546Sopenharmony_ci
28bf215546Sopenharmony_ciclass xmlconfig_test : public ::testing::Test {
29bf215546Sopenharmony_ciprotected:
30bf215546Sopenharmony_ci   xmlconfig_test();
31bf215546Sopenharmony_ci   ~xmlconfig_test();
32bf215546Sopenharmony_ci
33bf215546Sopenharmony_ci   driOptionCache drirc_init(const char *driver, const char *drm,
34bf215546Sopenharmony_ci                             const char *exec_name,
35bf215546Sopenharmony_ci                             const char *app, int appver,
36bf215546Sopenharmony_ci                             const char *engine, int enginever);
37bf215546Sopenharmony_ci
38bf215546Sopenharmony_ci   driOptionCache options;
39bf215546Sopenharmony_ci};
40bf215546Sopenharmony_ci
41bf215546Sopenharmony_cixmlconfig_test::xmlconfig_test()
42bf215546Sopenharmony_ci{
43bf215546Sopenharmony_ci   /* Unset variables from the envrionment to prevent user settings from
44bf215546Sopenharmony_ci    * impacting the tests.
45bf215546Sopenharmony_ci    */
46bf215546Sopenharmony_ci   unsetenv("glsl_zero_init");
47bf215546Sopenharmony_ci   unsetenv("always_have_depth_buffer");
48bf215546Sopenharmony_ci   unsetenv("opt");
49bf215546Sopenharmony_ci   unsetenv("vblank_mode");
50bf215546Sopenharmony_ci   unsetenv("not_present");
51bf215546Sopenharmony_ci   unsetenv("mesa_b_option");
52bf215546Sopenharmony_ci   unsetenv("mesa_s_option");
53bf215546Sopenharmony_ci   unsetenv("mest_test_unknown_option");
54bf215546Sopenharmony_ci   unsetenv("mest_drirc_option");
55bf215546Sopenharmony_ci
56bf215546Sopenharmony_ci   options = {};
57bf215546Sopenharmony_ci}
58bf215546Sopenharmony_ci
59bf215546Sopenharmony_cixmlconfig_test::~xmlconfig_test()
60bf215546Sopenharmony_ci{
61bf215546Sopenharmony_ci   driDestroyOptionInfo(&options);
62bf215546Sopenharmony_ci}
63bf215546Sopenharmony_ci
64bf215546Sopenharmony_ci/* wraps a DRI_CONF_OPT_* in the required xml bits */
65bf215546Sopenharmony_ci#define DRI_CONF_TEST_OPT(x) x
66bf215546Sopenharmony_ci
67bf215546Sopenharmony_ciTEST_F(xmlconfig_test, bools)
68bf215546Sopenharmony_ci{
69bf215546Sopenharmony_ci   driOptionDescription driconf[] = {
70bf215546Sopenharmony_ci      DRI_CONF_SECTION_MISCELLANEOUS
71bf215546Sopenharmony_ci      DRI_CONF_GLSL_ZERO_INIT(false)
72bf215546Sopenharmony_ci      DRI_CONF_ALWAYS_HAVE_DEPTH_BUFFER(true)
73bf215546Sopenharmony_ci   };
74bf215546Sopenharmony_ci   driParseOptionInfo(&options, driconf, ARRAY_SIZE(driconf));
75bf215546Sopenharmony_ci
76bf215546Sopenharmony_ci   EXPECT_EQ(driQueryOptionb(&options, "glsl_zero_init"), false);
77bf215546Sopenharmony_ci   EXPECT_EQ(driQueryOptionb(&options, "always_have_depth_buffer"), true);
78bf215546Sopenharmony_ci}
79bf215546Sopenharmony_ci
80bf215546Sopenharmony_ciTEST_F(xmlconfig_test, ints)
81bf215546Sopenharmony_ci{
82bf215546Sopenharmony_ci   driOptionDescription driconf[] = {
83bf215546Sopenharmony_ci      DRI_CONF_SECTION_MISCELLANEOUS
84bf215546Sopenharmony_ci      DRI_CONF_OPT_I(opt, 2, 0, 999, "option")
85bf215546Sopenharmony_ci   };
86bf215546Sopenharmony_ci   driParseOptionInfo(&options, driconf, ARRAY_SIZE(driconf));
87bf215546Sopenharmony_ci
88bf215546Sopenharmony_ci   EXPECT_EQ(driQueryOptioni(&options, "opt"), 2);
89bf215546Sopenharmony_ci}
90bf215546Sopenharmony_ci
91bf215546Sopenharmony_ciTEST_F(xmlconfig_test, floats)
92bf215546Sopenharmony_ci{
93bf215546Sopenharmony_ci   driOptionDescription driconf[] = {
94bf215546Sopenharmony_ci      DRI_CONF_SECTION_MISCELLANEOUS
95bf215546Sopenharmony_ci      DRI_CONF_OPT_F(opt, 2.0, 1.0, 2.0, "option")
96bf215546Sopenharmony_ci   };
97bf215546Sopenharmony_ci   driParseOptionInfo(&options, driconf, ARRAY_SIZE(driconf));
98bf215546Sopenharmony_ci
99bf215546Sopenharmony_ci   EXPECT_EQ(driQueryOptionf(&options, "opt"), 2.0);
100bf215546Sopenharmony_ci}
101bf215546Sopenharmony_ci
102bf215546Sopenharmony_ciTEST_F(xmlconfig_test, enums)
103bf215546Sopenharmony_ci{
104bf215546Sopenharmony_ci   driOptionDescription driconf[] = {
105bf215546Sopenharmony_ci      DRI_CONF_SECTION_MISCELLANEOUS
106bf215546Sopenharmony_ci      DRI_CONF_VBLANK_MODE(DRI_CONF_VBLANK_DEF_INTERVAL_1)
107bf215546Sopenharmony_ci   };
108bf215546Sopenharmony_ci   driParseOptionInfo(&options, driconf, ARRAY_SIZE(driconf));
109bf215546Sopenharmony_ci
110bf215546Sopenharmony_ci   EXPECT_EQ(driQueryOptioni(&options, "vblank_mode"), DRI_CONF_VBLANK_DEF_INTERVAL_1);
111bf215546Sopenharmony_ci}
112bf215546Sopenharmony_ci
113bf215546Sopenharmony_ciTEST_F(xmlconfig_test, enums_from_env)
114bf215546Sopenharmony_ci{
115bf215546Sopenharmony_ci   driOptionDescription driconf[] = {
116bf215546Sopenharmony_ci      DRI_CONF_SECTION_MISCELLANEOUS
117bf215546Sopenharmony_ci      DRI_CONF_VBLANK_MODE(DRI_CONF_VBLANK_DEF_INTERVAL_1)
118bf215546Sopenharmony_ci   };
119bf215546Sopenharmony_ci
120bf215546Sopenharmony_ci   setenv("vblank_mode", "0", 1);
121bf215546Sopenharmony_ci   driParseOptionInfo(&options, driconf, ARRAY_SIZE(driconf));
122bf215546Sopenharmony_ci
123bf215546Sopenharmony_ci   EXPECT_EQ(0, driQueryOptioni(&options, "vblank_mode"));
124bf215546Sopenharmony_ci}
125bf215546Sopenharmony_ci
126bf215546Sopenharmony_ciTEST_F(xmlconfig_test, string)
127bf215546Sopenharmony_ci{
128bf215546Sopenharmony_ci   driOptionDescription driconf[] = {
129bf215546Sopenharmony_ci      DRI_CONF_SECTION_MISCELLANEOUS
130bf215546Sopenharmony_ci      DRI_CONF_OPT_S(opt, value, "option")
131bf215546Sopenharmony_ci   };
132bf215546Sopenharmony_ci   driParseOptionInfo(&options, driconf, ARRAY_SIZE(driconf));
133bf215546Sopenharmony_ci
134bf215546Sopenharmony_ci   EXPECT_STREQ(driQueryOptionstr(&options, "opt"), "value");
135bf215546Sopenharmony_ci}
136bf215546Sopenharmony_ci
137bf215546Sopenharmony_ciTEST_F(xmlconfig_test, check_option)
138bf215546Sopenharmony_ci{
139bf215546Sopenharmony_ci   driOptionDescription driconf[] = {
140bf215546Sopenharmony_ci      DRI_CONF_SECTION_MISCELLANEOUS
141bf215546Sopenharmony_ci      DRI_CONF_GLSL_ZERO_INIT(true)
142bf215546Sopenharmony_ci      DRI_CONF_ALWAYS_HAVE_DEPTH_BUFFER(true)
143bf215546Sopenharmony_ci   };
144bf215546Sopenharmony_ci   driParseOptionInfo(&options, driconf, ARRAY_SIZE(driconf));
145bf215546Sopenharmony_ci
146bf215546Sopenharmony_ci   EXPECT_EQ(driCheckOption(&options, "glsl_zero_init", DRI_BOOL), true);
147bf215546Sopenharmony_ci
148bf215546Sopenharmony_ci   EXPECT_EQ(driCheckOption(&options, "glsl_zero_init", DRI_ENUM), false);
149bf215546Sopenharmony_ci   EXPECT_EQ(driCheckOption(&options, "glsl_zero_init", DRI_INT), false);
150bf215546Sopenharmony_ci   EXPECT_EQ(driCheckOption(&options, "glsl_zero_init", DRI_FLOAT), false);
151bf215546Sopenharmony_ci   EXPECT_EQ(driCheckOption(&options, "glsl_zero_init", DRI_STRING), false);
152bf215546Sopenharmony_ci
153bf215546Sopenharmony_ci   EXPECT_EQ(driCheckOption(&options, "not_present", DRI_BOOL), false);
154bf215546Sopenharmony_ci}
155bf215546Sopenharmony_ci
156bf215546Sopenharmony_ciTEST_F(xmlconfig_test, copy_cache)
157bf215546Sopenharmony_ci{
158bf215546Sopenharmony_ci   driOptionDescription driconf[] = {
159bf215546Sopenharmony_ci      DRI_CONF_SECTION_MISCELLANEOUS
160bf215546Sopenharmony_ci      DRI_CONF_OPT_B(mesa_b_option, true, "description")
161bf215546Sopenharmony_ci      DRI_CONF_OPT_S(mesa_s_option, value, "description")
162bf215546Sopenharmony_ci   };
163bf215546Sopenharmony_ci   driParseOptionInfo(&options, driconf, ARRAY_SIZE(driconf));
164bf215546Sopenharmony_ci
165bf215546Sopenharmony_ci   driOptionCache cache;
166bf215546Sopenharmony_ci
167bf215546Sopenharmony_ci   /* This tries to parse user config files.  We've called our option
168bf215546Sopenharmony_ci    * "mesa_test_option" so the test shouldn't end up with something from the
169bf215546Sopenharmony_ci    * user's homedir/environment that would override us.
170bf215546Sopenharmony_ci    */
171bf215546Sopenharmony_ci   driParseConfigFiles(&cache, &options,
172bf215546Sopenharmony_ci                       0, "driver", "drm", NULL,
173bf215546Sopenharmony_ci                       NULL, 0,
174bf215546Sopenharmony_ci                       NULL, 0);
175bf215546Sopenharmony_ci
176bf215546Sopenharmony_ci   /* Can we inspect the cache? */
177bf215546Sopenharmony_ci   EXPECT_EQ(driCheckOption(&cache, "mesa_b_option", DRI_BOOL), true);
178bf215546Sopenharmony_ci   EXPECT_EQ(driCheckOption(&cache, "mesa_s_option", DRI_STRING), true);
179bf215546Sopenharmony_ci   EXPECT_EQ(driCheckOption(&cache, "mesa_test_unknown_option", DRI_BOOL), false);
180bf215546Sopenharmony_ci
181bf215546Sopenharmony_ci   /* Did the value get copied? */
182bf215546Sopenharmony_ci   EXPECT_EQ(driQueryOptionb(&cache, "mesa_b_option"), true);
183bf215546Sopenharmony_ci   EXPECT_STREQ(driQueryOptionstr(&cache, "mesa_s_option"), "value");
184bf215546Sopenharmony_ci
185bf215546Sopenharmony_ci   driDestroyOptionCache(&cache);
186bf215546Sopenharmony_ci}
187bf215546Sopenharmony_ci
188bf215546Sopenharmony_cidriOptionCache
189bf215546Sopenharmony_cixmlconfig_test::drirc_init(const char *driver, const char *drm,
190bf215546Sopenharmony_ci                           const char *exec_name,
191bf215546Sopenharmony_ci                           const char *app, int appver,
192bf215546Sopenharmony_ci                           const char *engine, int enginever)
193bf215546Sopenharmony_ci{
194bf215546Sopenharmony_ci   /* Make the parser look in the directory of config files for the test,
195bf215546Sopenharmony_ci    * passed in by meson.build.
196bf215546Sopenharmony_ci    */
197bf215546Sopenharmony_ci   driInjectDataDir(getenv("DRIRC_CONFIGDIR"));
198bf215546Sopenharmony_ci
199bf215546Sopenharmony_ci   driInjectExecName(exec_name);
200bf215546Sopenharmony_ci
201bf215546Sopenharmony_ci   driOptionDescription driconf[] = {
202bf215546Sopenharmony_ci      DRI_CONF_SECTION_MISCELLANEOUS
203bf215546Sopenharmony_ci      DRI_CONF_OPT_I(mesa_drirc_option, 0, 0, 200, "description")
204bf215546Sopenharmony_ci   };
205bf215546Sopenharmony_ci   driParseOptionInfo(&options, driconf, ARRAY_SIZE(driconf));
206bf215546Sopenharmony_ci
207bf215546Sopenharmony_ci   driOptionCache cache;
208bf215546Sopenharmony_ci
209bf215546Sopenharmony_ci   /* This should parse the "user" drirc files under ./tests/drirc_test/,
210bf215546Sopenharmony_ci    * based on the setting of $HOME by meson.build.
211bf215546Sopenharmony_ci    */
212bf215546Sopenharmony_ci   driParseConfigFiles(&cache, &options,
213bf215546Sopenharmony_ci                       0, driver, drm, NULL,
214bf215546Sopenharmony_ci                       app, appver,
215bf215546Sopenharmony_ci                       engine, enginever);
216bf215546Sopenharmony_ci
217bf215546Sopenharmony_ci   return cache;
218bf215546Sopenharmony_ci}
219bf215546Sopenharmony_ci
220bf215546Sopenharmony_ciTEST_F(xmlconfig_test, drirc_app)
221bf215546Sopenharmony_ci{
222bf215546Sopenharmony_ci   driOptionCache cache = drirc_init("driver", "drm",
223bf215546Sopenharmony_ci                                     "app1",
224bf215546Sopenharmony_ci                                     NULL, 0,
225bf215546Sopenharmony_ci                                     NULL, 0);
226bf215546Sopenharmony_ci#if WITH_XMLCONFIG
227bf215546Sopenharmony_ci   EXPECT_EQ(driQueryOptioni(&cache, "mesa_drirc_option"), 1);
228bf215546Sopenharmony_ci#else
229bf215546Sopenharmony_ci   EXPECT_EQ(driQueryOptioni(&cache, "mesa_drirc_option"), 0);
230bf215546Sopenharmony_ci#endif
231bf215546Sopenharmony_ci   driDestroyOptionCache(&cache);
232bf215546Sopenharmony_ci}
233bf215546Sopenharmony_ci
234bf215546Sopenharmony_ciTEST_F(xmlconfig_test, drirc_user_app)
235bf215546Sopenharmony_ci{
236bf215546Sopenharmony_ci   driOptionCache cache = drirc_init("driver", "drm",
237bf215546Sopenharmony_ci                                     "app3",
238bf215546Sopenharmony_ci                                     NULL, 0,
239bf215546Sopenharmony_ci                                     NULL, 0);
240bf215546Sopenharmony_ci#if WITH_XMLCONFIG
241bf215546Sopenharmony_ci   EXPECT_EQ(driQueryOptioni(&cache, "mesa_drirc_option"), 10);
242bf215546Sopenharmony_ci#else
243bf215546Sopenharmony_ci   EXPECT_EQ(driQueryOptioni(&cache, "mesa_drirc_option"), 0);
244bf215546Sopenharmony_ci#endif
245bf215546Sopenharmony_ci   driDestroyOptionCache(&cache);
246bf215546Sopenharmony_ci}
247bf215546Sopenharmony_ci
248bf215546Sopenharmony_ciTEST_F(xmlconfig_test, drirc_env_override)
249bf215546Sopenharmony_ci{
250bf215546Sopenharmony_ci   setenv("mesa_drirc_option", "7", 1);
251bf215546Sopenharmony_ci   driOptionCache cache = drirc_init("driver", "drm",
252bf215546Sopenharmony_ci                                     "app1",
253bf215546Sopenharmony_ci                                     NULL, 0,
254bf215546Sopenharmony_ci                                     NULL, 0);
255bf215546Sopenharmony_ci   /* env var takes precedence over config files */
256bf215546Sopenharmony_ci   EXPECT_EQ(driQueryOptioni(&cache, "mesa_drirc_option"), 7);
257bf215546Sopenharmony_ci   unsetenv("mesa_drirc_option");
258bf215546Sopenharmony_ci   driDestroyOptionCache(&cache);
259bf215546Sopenharmony_ci}
260bf215546Sopenharmony_ci
261bf215546Sopenharmony_ci#if WITH_XMLCONFIG
262bf215546Sopenharmony_ciTEST_F(xmlconfig_test, drirc_app_versioned)
263bf215546Sopenharmony_ci{
264bf215546Sopenharmony_ci   driOptionCache cache = drirc_init("driver", "drm",
265bf215546Sopenharmony_ci                                     NULL,
266bf215546Sopenharmony_ci                                     "Versioned App Name", 1,
267bf215546Sopenharmony_ci                                     NULL, 0);
268bf215546Sopenharmony_ci   EXPECT_EQ(driQueryOptioni(&cache, "mesa_drirc_option"), 3);
269bf215546Sopenharmony_ci   driDestroyOptionCache(&cache);
270bf215546Sopenharmony_ci}
271bf215546Sopenharmony_ci
272bf215546Sopenharmony_ciTEST_F(xmlconfig_test, drirc_engine_versioned)
273bf215546Sopenharmony_ci{
274bf215546Sopenharmony_ci   driOptionCache cache = drirc_init("driver", "drm",
275bf215546Sopenharmony_ci                                     NULL,
276bf215546Sopenharmony_ci                                     "unknownapp", 0,
277bf215546Sopenharmony_ci                                     "Versioned Engine Name", 1);
278bf215546Sopenharmony_ci   EXPECT_EQ(driQueryOptioni(&cache, "mesa_drirc_option"), 5);
279bf215546Sopenharmony_ci   driDestroyOptionCache(&cache);
280bf215546Sopenharmony_ci}
281bf215546Sopenharmony_ci
282bf215546Sopenharmony_ciTEST_F(xmlconfig_test, drirc_exec_regexp)
283bf215546Sopenharmony_ci{
284bf215546Sopenharmony_ci   driOptionCache cache = drirc_init("driver", "drm",
285bf215546Sopenharmony_ci                                     "app2v4",
286bf215546Sopenharmony_ci                                     NULL, 0,
287bf215546Sopenharmony_ci                                     NULL, 0);
288bf215546Sopenharmony_ci   EXPECT_EQ(driQueryOptioni(&cache, "mesa_drirc_option"), 7);
289bf215546Sopenharmony_ci   driDestroyOptionCache(&cache);
290bf215546Sopenharmony_ci}
291bf215546Sopenharmony_ci#endif
292