1 /*
2  * Copyright (c) 2024 TOKITA Hiroshi
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/drivers/display.h>
8 #include <zephyr/ztest.h>
9 #include <zephyr/device.h>
10 #include <zephyr/logging/log.h>
11 #include <zephyr/display/cfb.h>
12 
13 #include "testdata.h"
14 #include "utils.h"
15 
16 LOG_MODULE_REGISTER(print_rectspace1016, CONFIG_DISPLAY_LOG_LEVEL);
17 
18 static const struct device *dev = DEVICE_DT_GET(DT_CHOSEN(zephyr_display));
19 static const uint32_t display_width = DT_PROP(DT_CHOSEN(zephyr_display), width);
20 static const uint32_t display_height = DT_PROP(DT_CHOSEN(zephyr_display), height);
21 
22 /**
23  * Fill the buffer with 0 before running tests.
24  */
cfb_test_before(void * text_fixture)25 static void cfb_test_before(void *text_fixture)
26 {
27 	struct display_buffer_descriptor desc = {
28 		.height = display_height,
29 		.pitch = display_width,
30 		.width = display_width,
31 		.buf_size = display_height * display_width / 8,
32 	};
33 	uint8_t font_width;
34 	uint8_t font_height;
35 	bool font_found = false;
36 
37 	memset(read_buffer, 0, sizeof(read_buffer));
38 	zassert_ok(display_write(dev, 0, 0, &desc, read_buffer));
39 
40 	zassert_ok(display_blanking_off(dev));
41 
42 	zassert_ok(cfb_framebuffer_init(dev));
43 
44 	for (int idx = 0; idx < cfb_get_numof_fonts(dev); idx++) {
45 		if (cfb_get_font_size(dev, idx, &font_width, &font_height)) {
46 			break;
47 		}
48 
49 		if (font_width == 10 && font_height == 16) {
50 			cfb_framebuffer_set_font(dev, idx);
51 			font_found = true;
52 			break;
53 		}
54 	}
55 
56 	zassert_true(font_found);
57 }
58 
cfb_test_after(void * test_fixture)59 static void cfb_test_after(void *test_fixture)
60 {
61 	cfb_framebuffer_deinit(dev);
62 }
63 
64 /*
65  * normal rendering
66  */
ZTEST(print_rectspace1016,test_print_at_0_0)67 ZTEST(print_rectspace1016, test_print_at_0_0)
68 {
69 	zassert_ok(cfb_print(dev, " ", 0, 0));
70 	zassert_ok(cfb_framebuffer_finalize(dev));
71 
72 	zassert_true(verify_image_and_bg(0, 0, rectspace1016, 10, 16, 0));
73 }
74 
ZTEST(print_rectspace1016,test_print_at_1_1)75 ZTEST(print_rectspace1016, test_print_at_1_1)
76 {
77 	zassert_ok(cfb_print(dev, " ", 1, 1));
78 	zassert_ok(cfb_framebuffer_finalize(dev));
79 
80 	zassert_true(verify_image_and_bg(1, 1, rectspace1016, 10, 16, 0));
81 }
82 
83 /*
84  * around tile border
85  */
ZTEST(print_rectspace1016,test_print_at_9_15)86 ZTEST(print_rectspace1016, test_print_at_9_15)
87 {
88 	zassert_ok(cfb_print(dev, " ", 9, 15));
89 	zassert_ok(cfb_framebuffer_finalize(dev));
90 
91 	zassert_true(verify_image_and_bg(9, 15, rectspace1016, 10, 16, 0));
92 }
93 
ZTEST(print_rectspace1016,test_print_at_10_16)94 ZTEST(print_rectspace1016, test_print_at_10_16)
95 {
96 	zassert_ok(cfb_print(dev, " ", 10, 16));
97 	zassert_ok(cfb_framebuffer_finalize(dev));
98 
99 	zassert_true(verify_image_and_bg(10, 16, rectspace1016, 10, 16, 0));
100 }
101 
ZTEST(print_rectspace1016,test_print_at_11_17)102 ZTEST(print_rectspace1016, test_print_at_11_17)
103 {
104 	zassert_ok(cfb_print(dev, " ", 11, 17));
105 	zassert_ok(cfb_framebuffer_finalize(dev));
106 
107 	zassert_true(verify_image_and_bg(11, 17, rectspace1016, 10, 16, 0));
108 }
109 
110 /*
111  * kerning
112  */
ZTEST(print_rectspace1016,test_print_at_0_0_kerning_3)113 ZTEST(print_rectspace1016, test_print_at_0_0_kerning_3)
114 {
115 	cfb_set_kerning(dev, 3);
116 	zassert_ok(cfb_print(dev, "  ", 0, 0));
117 	zassert_ok(cfb_framebuffer_finalize(dev));
118 
119 	zassert_true(verify_image_and_bg(0, 0, kerning_3_2rectspace1016, 23, 16, 0));
120 }
121 
ZTEST(print_rectspace1016,test_print_at_1_1_kerning_3)122 ZTEST(print_rectspace1016, test_print_at_1_1_kerning_3)
123 {
124 	cfb_set_kerning(dev, 3);
125 	zassert_ok(cfb_print(dev, "  ", 1, 1));
126 	zassert_ok(cfb_framebuffer_finalize(dev));
127 
128 	zassert_true(verify_image_and_bg(1, 1, kerning_3_2rectspace1016, 23, 16, 0));
129 }
130 
ZTEST(print_rectspace1016,test_print_at_9_15_kerning_3)131 ZTEST(print_rectspace1016, test_print_at_9_15_kerning_3)
132 {
133 	cfb_set_kerning(dev, 3);
134 	zassert_ok(cfb_print(dev, "  ", 9, 15));
135 	zassert_ok(cfb_framebuffer_finalize(dev));
136 
137 	zassert_true(verify_image_and_bg(9, 15, kerning_3_2rectspace1016, 23, 16, 0));
138 }
139 
ZTEST(print_rectspace1016,test_print_at_10_16_kerning_3)140 ZTEST(print_rectspace1016, test_print_at_10_16_kerning_3)
141 {
142 	cfb_set_kerning(dev, 3);
143 	zassert_ok(cfb_print(dev, "  ", 10, 16));
144 	zassert_ok(cfb_framebuffer_finalize(dev));
145 
146 	zassert_true(verify_image_and_bg(10, 16, kerning_3_2rectspace1016, 23, 16, 0));
147 }
148 
ZTEST(print_rectspace1016,test_print_at_11_17_kerning_3)149 ZTEST(print_rectspace1016, test_print_at_11_17_kerning_3)
150 {
151 	cfb_set_kerning(dev, 3);
152 	zassert_ok(cfb_print(dev, "  ", 11, 17));
153 	zassert_ok(cfb_framebuffer_finalize(dev));
154 
155 	zassert_true(verify_image_and_bg(11, 17, kerning_3_2rectspace1016, 23, 16, 0));
156 }
157 
ZTEST(print_rectspace1016,test_print_kerning_3_within_right_border)158 ZTEST(print_rectspace1016, test_print_kerning_3_within_right_border)
159 {
160 	cfb_set_kerning(dev, 3);
161 	zassert_ok(cfb_print(dev, "  ", display_width - 23, 17));
162 	zassert_ok(cfb_framebuffer_finalize(dev));
163 
164 	zassert_true(verify_image(display_width - 23, 17, kerning_3_2rectspace1016, 23, 16));
165 }
166 
ZTEST(print_rectspace1016,test_print_kerning_3_text_wrap)167 ZTEST(print_rectspace1016, test_print_kerning_3_text_wrap)
168 {
169 	cfb_set_kerning(dev, 3);
170 	zassert_ok(cfb_print(dev, "  ", display_width - 22, 17));
171 	zassert_ok(cfb_framebuffer_finalize(dev));
172 
173 	zassert_true(verify_image(display_width - 22, 17, rectspace1016, 10, 16));
174 	zassert_true(verify_image(0, 33, rectspace1016, 10, 16));
175 }
176 
ZTEST(print_rectspace1016,test_print_outside_top_left)177 ZTEST(print_rectspace1016, test_print_outside_top_left)
178 {
179 	zassert_ok(cfb_print(dev, " ", -(10 - 3), -(16 - 4)));
180 	zassert_ok(cfb_framebuffer_finalize(dev));
181 
182 	zassert_true(verify_image_and_bg(0, 0, outside_top_left, 3, 4, 0));
183 }
184 
ZTEST(print_rectspace1016,test_print_outside_top_right)185 ZTEST(print_rectspace1016, test_print_outside_top_right)
186 {
187 	zassert_ok(cfb_print(dev, " ", display_width - 5, -8));
188 	zassert_ok(cfb_framebuffer_finalize(dev));
189 
190 	zassert_true(verify_image_and_bg(0, 8, rectspace1016, 10, 16, 0));
191 }
192 
ZTEST(print_rectspace1016,test_print_outside_bottom_right)193 ZTEST(print_rectspace1016, test_print_outside_bottom_right)
194 {
195 	zassert_ok(cfb_print(dev, " ", display_width - 3, display_height - 5));
196 	zassert_ok(cfb_framebuffer_finalize(dev));
197 
198 	zassert_true(verify_color_inside_rect(0, 0, display_width, display_height, 0));
199 }
200 
ZTEST(print_rectspace1016,test_print_outside_bottom_left)201 ZTEST(print_rectspace1016, test_print_outside_bottom_left)
202 {
203 	zassert_ok(cfb_print(dev, " ", -(10 - 3), display_height - 14));
204 	zassert_ok(cfb_framebuffer_finalize(dev));
205 
206 	zassert_true(verify_image(0, display_height - 14, outside_bottom_left, 3, 14));
207 }
208 
ZTEST(print_rectspace1016,test_print_wrap_to_3_lines)209 ZTEST(print_rectspace1016, test_print_wrap_to_3_lines)
210 {
211 	cfb_set_kerning(dev, 3);
212 	zassert_ok(cfb_print(dev, "                                                 ", 160, 17));
213 	zassert_ok(cfb_framebuffer_finalize(dev));
214 
215 	zassert_true(verify_image(160, 17, kerning_3_12rectspace1016, 153, 16));
216 	zassert_true(verify_image(0, 33, kerning_3_12rectspace1016, 153, 16));
217 	zassert_true(verify_image(156, 33, kerning_3_12rectspace1016, 153, 16));
218 	zassert_true(verify_image(0, 49, kerning_3_12rectspace1016, 153, 16));
219 	zassert_true(verify_image(13, 49, kerning_3_12rectspace1016, 153, 16));
220 }
221 
222 ZTEST_SUITE(print_rectspace1016, NULL, NULL, cfb_test_before, cfb_test_after, NULL);
223