1 /*
2 * Copyright © 2020 Google LLC
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 DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24 #include <gtest/gtest.h>
25 #include <driconf.h>
26 #include <xmlconfig.h>
27
28 class xmlconfig_test : public ::testing::Test {
29 protected:
30 xmlconfig_test();
31 ~xmlconfig_test();
32
33 driOptionCache options;
34 };
35
xmlconfig_test()36 xmlconfig_test::xmlconfig_test()
37 {
38 options = {};
39 }
40
~xmlconfig_test()41 xmlconfig_test::~xmlconfig_test()
42 {
43 driDestroyOptionInfo(&options);
44 }
45
46 /* wraps a DRI_CONF_OPT_* in the required xml bits */
47 #define DRI_CONF_TEST_OPT(x) x
48
TEST_F(xmlconfig_test,bools)49 TEST_F(xmlconfig_test, bools)
50 {
51 driOptionDescription driconf[] = {
52 DRI_CONF_SECTION_MISCELLANEOUS
53 DRI_CONF_GLSL_ZERO_INIT(false)
54 DRI_CONF_ALWAYS_HAVE_DEPTH_BUFFER(true)
55 };
56 driParseOptionInfo(&options, driconf, ARRAY_SIZE(driconf));
57
58 EXPECT_EQ(driQueryOptionb(&options, "glsl_zero_init"), false);
59 EXPECT_EQ(driQueryOptionb(&options, "always_have_depth_buffer"), true);
60 }
61
TEST_F(xmlconfig_test,ints)62 TEST_F(xmlconfig_test, ints)
63 {
64 driOptionDescription driconf[] = {
65 DRI_CONF_SECTION_MISCELLANEOUS
66 DRI_CONF_OPT_I(opt, 2, 0, 999, "option")
67 };
68 driParseOptionInfo(&options, driconf, ARRAY_SIZE(driconf));
69
70 EXPECT_EQ(driQueryOptioni(&options, "opt"), 2);
71 }
72
TEST_F(xmlconfig_test,floats)73 TEST_F(xmlconfig_test, floats)
74 {
75 driOptionDescription driconf[] = {
76 DRI_CONF_SECTION_MISCELLANEOUS
77 DRI_CONF_OPT_F(opt, 2.0, 1.0, 2.0, "option")
78 };
79 driParseOptionInfo(&options, driconf, ARRAY_SIZE(driconf));
80
81 EXPECT_EQ(driQueryOptionf(&options, "opt"), 2.0);
82 }
83
TEST_F(xmlconfig_test,enums)84 TEST_F(xmlconfig_test, enums)
85 {
86 driOptionDescription driconf[] = {
87 DRI_CONF_SECTION_MISCELLANEOUS
88 DRI_CONF_VBLANK_MODE(DRI_CONF_VBLANK_DEF_INTERVAL_1)
89 };
90 driParseOptionInfo(&options, driconf, ARRAY_SIZE(driconf));
91
92 EXPECT_EQ(driQueryOptioni(&options, "vblank_mode"), DRI_CONF_VBLANK_DEF_INTERVAL_1);
93 }
94
TEST_F(xmlconfig_test,string)95 TEST_F(xmlconfig_test, string)
96 {
97 driOptionDescription driconf[] = {
98 DRI_CONF_SECTION_MISCELLANEOUS
99 DRI_CONF_OPT_S(opt, value, "option")
100 };
101 driParseOptionInfo(&options, driconf, ARRAY_SIZE(driconf));
102
103 EXPECT_STREQ(driQueryOptionstr(&options, "opt"), "value");
104 }
105
TEST_F(xmlconfig_test,check_option)106 TEST_F(xmlconfig_test, check_option)
107 {
108 driOptionDescription driconf[] = {
109 DRI_CONF_SECTION_MISCELLANEOUS
110 DRI_CONF_GLSL_ZERO_INIT(true)
111 DRI_CONF_ALWAYS_HAVE_DEPTH_BUFFER(true)
112 };
113 driParseOptionInfo(&options, driconf, ARRAY_SIZE(driconf));
114
115 EXPECT_EQ(driCheckOption(&options, "glsl_zero_init", DRI_BOOL), true);
116
117 EXPECT_EQ(driCheckOption(&options, "glsl_zero_init", DRI_ENUM), false);
118 EXPECT_EQ(driCheckOption(&options, "glsl_zero_init", DRI_INT), false);
119 EXPECT_EQ(driCheckOption(&options, "glsl_zero_init", DRI_FLOAT), false);
120 EXPECT_EQ(driCheckOption(&options, "glsl_zero_init", DRI_STRING), false);
121
122 EXPECT_EQ(driCheckOption(&options, "not_present", DRI_BOOL), false);
123 }
124
TEST_F(xmlconfig_test,copy_cache)125 TEST_F(xmlconfig_test, copy_cache)
126 {
127 driOptionDescription driconf[] = {
128 DRI_CONF_SECTION_MISCELLANEOUS
129 DRI_CONF_OPT_B(mesa_b_option, true, "description")
130 DRI_CONF_OPT_S(mesa_s_option, value, "description")
131 };
132 driParseOptionInfo(&options, driconf, ARRAY_SIZE(driconf));
133
134 driOptionCache cache;
135
136 /* This tries to parse user config files. We've called our option
137 * "mesa_test_option" so the test shouldn't end up with something from the
138 * user's homedir/environment that would override us.
139 */
140 driParseConfigFiles(&cache, &options,
141 0, "driver", "drm",
142 NULL, 0,
143 NULL, 0);
144
145 /* Can we inspect the cache? */
146 EXPECT_EQ(driCheckOption(&cache, "mesa_b_option", DRI_BOOL), true);
147 EXPECT_EQ(driCheckOption(&cache, "mesa_s_option", DRI_STRING), true);
148 EXPECT_EQ(driCheckOption(&cache, "mesa_test_unknown_option", DRI_BOOL), false);
149
150 /* Did the value get copied? */
151 EXPECT_EQ(driQueryOptionb(&cache, "mesa_b_option"), true);
152 EXPECT_STREQ(driQueryOptionstr(&cache, "mesa_s_option"), "value");
153
154 driDestroyOptionCache(&cache);
155 }
156