1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
|
/*
* Copyright (C) 2022 Red Hat
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
* 02111-1307, USA.
*
*/
#include "config.h"
#include "backends/meta-monitor.h"
static void
assert_matches_none (MetaMonitorModeSpec *mode_spec,
MetaMonitorModeSpec *mode_specs,
int n_mode_specs)
{
int i;
for (i = 0; i < n_mode_specs; i++)
{
g_assert_false (meta_monitor_mode_spec_has_similar_size (mode_spec,
&mode_specs[i]));
}
}
static void
meta_test_monitor_mode_spec_similar_size (void)
{
MetaMonitorModeSpec matching_4k_specs[] = {
{ .width = 4096, .height = 2560 }, /* 16:10 */
{ .width = 4096, .height = 2304 }, /* 16:9 */
{ .width = 3840, .height = 2400 }, /* 16:10 */
{ .width = 3840, .height = 2160 }, /* 16:9 */
};
MetaMonitorModeSpec matching_uhd_specs[] = {
{ .width = 1920, .height = 1200 }, /* 16:10 */
{ .width = 1920, .height = 1080 }, /* 16:9 */
{ .width = 2048, .height = 1152 }, /* 16:9 */
};
MetaMonitorModeSpec matching_hd_specs[] = {
{ .width = 1366, .height = 768 }, /* ~16:9 */
{ .width = 1280, .height = 720 }, /* 16:9 */
};
MetaMonitorModeSpec nonmatching_specs[] = {
{ .width = 1024, .height = 768 },
{ .width = 800, .height = 600 },
{ .width = 640, .height = 480 },
};
int i;
/* Test that 4K modes only matches other 4K modes */
for (i = 0; i < G_N_ELEMENTS (matching_4k_specs); i++)
{
MetaMonitorModeSpec *mode_spec = &matching_4k_specs[i];
MetaMonitorModeSpec *prev_mode_spec = &matching_4k_specs[i - 1];
if (i != 0)
{
g_assert_true (meta_monitor_mode_spec_has_similar_size (mode_spec,
prev_mode_spec));
}
assert_matches_none (mode_spec,
matching_uhd_specs, G_N_ELEMENTS (matching_uhd_specs));
assert_matches_none (mode_spec,
matching_hd_specs, G_N_ELEMENTS (matching_hd_specs));
assert_matches_none (mode_spec,
nonmatching_specs, G_N_ELEMENTS (nonmatching_specs));
}
/* Test that FHD modes only matches other FHD modes */
for (i = 0; i < G_N_ELEMENTS (matching_uhd_specs); i++)
{
MetaMonitorModeSpec *mode_spec = &matching_uhd_specs[i];
MetaMonitorModeSpec *prev_mode_spec = &matching_uhd_specs[i - 1];
if (i != 0)
{
g_assert_true (meta_monitor_mode_spec_has_similar_size (mode_spec,
prev_mode_spec));
}
assert_matches_none (mode_spec,
matching_hd_specs, G_N_ELEMENTS (matching_hd_specs));
assert_matches_none (mode_spec,
nonmatching_specs, G_N_ELEMENTS (nonmatching_specs));
}
/* Test that HD modes only matches other HD modes */
for (i = 0; i < G_N_ELEMENTS (matching_hd_specs); i++)
{
MetaMonitorModeSpec *mode_spec = &matching_hd_specs[i];
MetaMonitorModeSpec *prev_mode_spec = &matching_hd_specs[i - 1];
if (i != 0)
{
g_assert_true (meta_monitor_mode_spec_has_similar_size (mode_spec,
prev_mode_spec));
}
assert_matches_none (mode_spec,
nonmatching_specs, G_N_ELEMENTS (nonmatching_specs));
}
/* Test that the other modes doesn't match each other. */
for (i = 0; i < G_N_ELEMENTS (nonmatching_specs) - 1; i++)
{
MetaMonitorModeSpec *mode_spec = &nonmatching_specs[i];
MetaMonitorModeSpec *next_mode_spec = &nonmatching_specs[i + 1];
g_assert_false (meta_monitor_mode_spec_has_similar_size (mode_spec,
next_mode_spec));
}
}
static void
meta_test_monitor_parse_mode (void)
{
const float fallback_refresh_rate = 60.0;
struct {
const char *string;
gboolean expected_pass;
int expected_width;
int expected_height;
float expected_refresh_rate;
} test_cases[] = {
{
.string = "800x600",
.expected_pass = TRUE,
.expected_width = 800,
.expected_height = 600,
.expected_refresh_rate = fallback_refresh_rate,
},
{
.string = "1280x720@30",
.expected_pass = TRUE,
.expected_width = 1280,
.expected_height = 720,
.expected_refresh_rate = 30.0,
},
{
.string = "1920x1080@120.50",
.expected_pass = TRUE,
.expected_width = 1920,
.expected_height = 1080,
.expected_refresh_rate = 120.5,
},
{
.string = "800X600",
.expected_pass = FALSE,
},
{
.string = "800x",
.expected_pass = FALSE,
},
{
.string = "800x600@",
.expected_pass = FALSE,
},
{
.string = "800x600@notanumber",
.expected_pass = FALSE,
},
{
.string = "nonsense",
.expected_pass = FALSE,
},
};
int i;
for (i = 0; i < G_N_ELEMENTS (test_cases); i++)
{
int width, height;
float refresh_rate;
if (meta_parse_monitor_mode (test_cases[i].string,
&width, &height, &refresh_rate,
fallback_refresh_rate))
{
g_assert_true (test_cases[i].expected_pass);
g_assert_cmpint (width, ==, test_cases[i].expected_width);
g_assert_cmpint (height, ==, test_cases[i].expected_height);
g_assert_cmpfloat_with_epsilon (refresh_rate,
test_cases[i].expected_refresh_rate,
FLT_EPSILON);
}
else
{
g_assert_false (test_cases[i].expected_pass);
}
}
}
int
main (int argc,
char **argv)
{
g_test_init (&argc, &argv, NULL);
g_test_add_func ("/backends/monitor/spec/similar-size",
meta_test_monitor_mode_spec_similar_size);
g_test_add_func ("/backends/monitor/parse-mode",
meta_test_monitor_parse_mode);
return g_test_run ();
}
|