2 * Copyright (c) 2014 Google, Inc
3 * Written by Simon Glass <sjg@chromium.org>
5 * SPDX-License-Identifier: GPL-2.0+
14 #include <video_console.h>
16 #include <dm/uclass-internal.h>
20 * These tests use the standard sandbox frame buffer, the resolution of which
21 * is defined in the device tree. This only supports 16bpp so the tests only
22 * test that code path. It would be possible to adjust this fairly easily,
23 * by adjusting the bpix value in struct sandbox_sdl_plat. However the code
24 * in sandbox_sdl_sync() would also need to change to handle the different
27 DECLARE_GLOBAL_DATA_PTR;
29 /* Basic test of the video uclass */
30 static int dm_test_video_base(struct unit_test_state *uts)
32 struct video_priv *priv;
35 ut_assertok(uclass_get_device(UCLASS_VIDEO, 0, &dev));
36 ut_asserteq(1366, video_get_xsize(dev));
37 ut_asserteq(768, video_get_ysize(dev));
38 priv = dev_get_uclass_priv(dev);
39 ut_asserteq(priv->fb_size, 1366 * 768 * 2);
43 DM_TEST(dm_test_video_base, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
46 * compress_frame_buffer() - Compress the frame buffer and return its size
48 * We want to write tests which perform operations on the video console and
49 * check that the frame buffer ends up with the correct contents. But it is
50 * painful to store 'known good' images for comparison with the frame
51 * buffer. As an alternative, we can compress the frame buffer and check the
52 * size of the compressed data. This provides a pretty good level of
53 * certainty and the resulting tests need only check a single value.
56 * @return compressed size of the frame buffer, or -ve on error
58 static int compress_frame_buffer(struct udevice *dev)
60 struct video_priv *priv = dev_get_uclass_priv(dev);
65 destlen = priv->fb_size;
66 dest = malloc(priv->fb_size);
69 ret = BZ2_bzBuffToBuffCompress(dest, &destlen,
70 priv->fb, priv->fb_size,
80 * Call this function at any point to halt and show the current display. Be
81 * sure to run the test with the -l flag.
83 static void __maybe_unused see_output(void)
89 /* Select the video console driver to use for a video device */
90 static int select_vidconsole(struct unit_test_state *uts, const char *drv_name)
92 struct sandbox_sdl_plat *plat;
95 ut_assertok(uclass_find_device(UCLASS_VIDEO, 0, &dev));
96 ut_assert(!device_active(dev));
97 plat = dev_get_platdata(dev);
98 plat->vidconsole_drv_name = "vidconsole0";
103 /* Test text output works on the video console */
104 static int dm_test_video_text(struct unit_test_state *uts)
106 struct udevice *dev, *con;
110 #define SCROLL_LINES 100
112 ut_assertok(select_vidconsole(uts, "vidconsole0"));
113 ut_assertok(uclass_get_device(UCLASS_VIDEO, 0, &dev));
114 ut_asserteq(46, compress_frame_buffer(dev));
116 ut_assertok(uclass_get_device(UCLASS_VIDEO_CONSOLE, 0, &con));
117 vidconsole_putc_xy(con, 0, 0, 'a');
118 ut_asserteq(79, compress_frame_buffer(dev));
120 vidconsole_putc_xy(con, 0, 0, ' ');
121 ut_asserteq(46, compress_frame_buffer(dev));
123 for (i = 0; i < 20; i++)
124 vidconsole_putc_xy(con, VID_TO_POS(i * 8), 0, ' ' + i);
125 ut_asserteq(273, compress_frame_buffer(dev));
127 vidconsole_set_row(con, 0, WHITE);
128 ut_asserteq(46, compress_frame_buffer(dev));
130 for (i = 0; i < 20; i++)
131 vidconsole_putc_xy(con, VID_TO_POS(i * 8), 0, ' ' + i);
132 ut_asserteq(273, compress_frame_buffer(dev));
136 DM_TEST(dm_test_video_text, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
138 /* Test handling of special characters in the console */
139 static int dm_test_video_chars(struct unit_test_state *uts)
141 struct udevice *dev, *con;
142 const char *test_string = "Well\b\b\b\bxhe is\r \n\ta very \amodest \bman\n\t\tand Has much to\b\bto be modest about.";
145 ut_assertok(select_vidconsole(uts, "vidconsole0"));
146 ut_assertok(uclass_get_device(UCLASS_VIDEO, 0, &dev));
147 ut_assertok(uclass_get_device(UCLASS_VIDEO_CONSOLE, 0, &con));
148 for (s = test_string; *s; s++)
149 vidconsole_put_char(con, *s);
150 ut_asserteq(466, compress_frame_buffer(dev));
154 DM_TEST(dm_test_video_chars, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
157 * check_vidconsole_output() - Run a text console test
160 * @rot: Console rotation (0, 90, 180, 270)
161 * @wrap_size: Expected size of compressed frame buffer for the wrap test
162 * @scroll_size: Same for the scroll test
163 * @return 0 on success
165 static int check_vidconsole_output(struct unit_test_state *uts, int rot,
166 int wrap_size, int scroll_size)
168 struct udevice *dev, *con;
169 struct sandbox_sdl_plat *plat;
172 ut_assertok(uclass_find_device(UCLASS_VIDEO, 0, &dev));
173 ut_assert(!device_active(dev));
174 plat = dev_get_platdata(dev);
177 ut_assertok(uclass_get_device(UCLASS_VIDEO, 0, &dev));
178 ut_assertok(uclass_get_device(UCLASS_VIDEO_CONSOLE, 0, &con));
179 ut_asserteq(46, compress_frame_buffer(dev));
181 /* Check display wrap */
182 for (i = 0; i < 120; i++)
183 vidconsole_put_char(con, 'A' + i % 50);
184 ut_asserteq(wrap_size, compress_frame_buffer(dev));
186 /* Check display scrolling */
187 for (i = 0; i < SCROLL_LINES; i++) {
188 vidconsole_put_char(con, 'A' + i % 50);
189 vidconsole_put_char(con, '\n');
191 ut_asserteq(scroll_size, compress_frame_buffer(dev));
193 /* If we scroll enough, the screen becomes blank again */
194 for (i = 0; i < SCROLL_LINES; i++)
195 vidconsole_put_char(con, '\n');
196 ut_asserteq(46, compress_frame_buffer(dev));
201 /* Test text output through the console uclass */
202 static int dm_test_video_context(struct unit_test_state *uts)
204 ut_assertok(select_vidconsole(uts, "vidconsole0"));
205 ut_assertok(check_vidconsole_output(uts, 0, 788, 453));
209 DM_TEST(dm_test_video_context, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
211 /* Test rotated text output through the console uclass */
212 static int dm_test_video_rotation1(struct unit_test_state *uts)
214 ut_assertok(check_vidconsole_output(uts, 1, 1112, 680));
218 DM_TEST(dm_test_video_rotation1, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
220 /* Test rotated text output through the console uclass */
221 static int dm_test_video_rotation2(struct unit_test_state *uts)
223 ut_assertok(check_vidconsole_output(uts, 2, 785, 446));
227 DM_TEST(dm_test_video_rotation2, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
229 /* Test rotated text output through the console uclass */
230 static int dm_test_video_rotation3(struct unit_test_state *uts)
232 ut_assertok(check_vidconsole_output(uts, 3, 1134, 681));
236 DM_TEST(dm_test_video_rotation3, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
238 /* Read a file into memory and return a pointer to it */
239 static int read_file(struct unit_test_state *uts, const char *fname,
242 int buf_size = 100000;
247 buf = map_sysmem(addr, 0);
248 ut_assert(buf != NULL);
249 fd = os_open(fname, OS_O_RDONLY);
251 size = os_read(fd, buf, buf_size);
253 ut_assert(size >= 0);
254 ut_assert(size < buf_size);
260 /* Test drawing a bitmap file */
261 static int dm_test_video_bmp(struct unit_test_state *uts)
266 ut_assertok(uclass_get_device(UCLASS_VIDEO, 0, &dev));
267 ut_assertok(read_file(uts, "tools/logos/denx.bmp", &addr));
269 ut_assertok(video_bmp_display(dev, addr, 0, 0, false));
270 ut_asserteq(1368, compress_frame_buffer(dev));
274 DM_TEST(dm_test_video_bmp, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
276 /* Test drawing a compressed bitmap file */
277 static int dm_test_video_bmp_comp(struct unit_test_state *uts)
282 ut_assertok(uclass_get_device(UCLASS_VIDEO, 0, &dev));
283 ut_assertok(read_file(uts, "tools/logos/denx-comp.bmp", &addr));
285 ut_assertok(video_bmp_display(dev, addr, 0, 0, false));
286 ut_asserteq(1368, compress_frame_buffer(dev));
290 DM_TEST(dm_test_video_bmp_comp, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
292 /* Test TrueType console */
293 static int dm_test_video_truetype(struct unit_test_state *uts)
295 struct udevice *dev, *con;
296 const char *test_string = "Criticism may not be agreeable, but it is necessary. It fulfils the same function as pain in the human body. It calls attention to an unhealthy state of things. Some see private enterprise as a predatory target to be shot, others as a cow to be milked, but few are those who see it as a sturdy horse pulling the wagon. The \aprice OF\b\bof greatness\n\tis responsibility.\n\nBye";
299 ut_assertok(uclass_get_device(UCLASS_VIDEO, 0, &dev));
300 ut_assertok(uclass_get_device(UCLASS_VIDEO_CONSOLE, 0, &con));
301 for (s = test_string; *s; s++)
302 vidconsole_put_char(con, *s);
303 ut_asserteq(12619, compress_frame_buffer(dev));
307 DM_TEST(dm_test_video_truetype, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
309 /* Test scrolling TrueType console */
310 static int dm_test_video_truetype_scroll(struct unit_test_state *uts)
312 struct sandbox_sdl_plat *plat;
313 struct udevice *dev, *con;
314 const char *test_string = "Criticism may not be agreeable, but it is necessary. It fulfils the same function as pain in the human body. It calls attention to an unhealthy state of things. Some see private enterprise as a predatory target to be shot, others as a cow to be milked, but few are those who see it as a sturdy horse pulling the wagon. The \aprice OF\b\bof greatness\n\tis responsibility.\n\nBye";
317 ut_assertok(uclass_find_device(UCLASS_VIDEO, 0, &dev));
318 ut_assert(!device_active(dev));
319 plat = dev_get_platdata(dev);
320 plat->font_size = 100;
322 ut_assertok(uclass_get_device(UCLASS_VIDEO, 0, &dev));
323 ut_assertok(uclass_get_device(UCLASS_VIDEO_CONSOLE, 0, &con));
324 for (s = test_string; *s; s++)
325 vidconsole_put_char(con, *s);
326 ut_asserteq(33849, compress_frame_buffer(dev));
330 DM_TEST(dm_test_video_truetype_scroll, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
332 /* Test TrueType backspace, within and across lines */
333 static int dm_test_video_truetype_bs(struct unit_test_state *uts)
335 struct sandbox_sdl_plat *plat;
336 struct udevice *dev, *con;
337 const char *test_string = "...Criticism may or may\b\b\b\b\b\bnot be agreeable, but seldom it is necessary\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\bit is necessary. It fulfils the same function as pain in the human body. It calls attention to an unhealthy state of things.";
340 ut_assertok(uclass_find_device(UCLASS_VIDEO, 0, &dev));
341 ut_assert(!device_active(dev));
342 plat = dev_get_platdata(dev);
343 plat->font_size = 100;
345 ut_assertok(uclass_get_device(UCLASS_VIDEO, 0, &dev));
346 ut_assertok(uclass_get_device(UCLASS_VIDEO_CONSOLE, 0, &con));
347 for (s = test_string; *s; s++)
348 vidconsole_put_char(con, *s);
349 ut_asserteq(34871, compress_frame_buffer(dev));
353 DM_TEST(dm_test_video_truetype_bs, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);