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(draw_text_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(draw_text_rectspace1016,test_draw_text_at_0_0)67 ZTEST(draw_text_rectspace1016, test_draw_text_at_0_0)
68 {
69 zassert_ok(cfb_draw_text(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(draw_text_rectspace1016,test_draw_text_at_1_1)75 ZTEST(draw_text_rectspace1016, test_draw_text_at_1_1)
76 {
77 zassert_ok(cfb_draw_text(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(draw_text_rectspace1016,test_draw_text_at_9_15)86 ZTEST(draw_text_rectspace1016, test_draw_text_at_9_15)
87 {
88 zassert_ok(cfb_draw_text(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(draw_text_rectspace1016,test_draw_text_at_10_16)94 ZTEST(draw_text_rectspace1016, test_draw_text_at_10_16)
95 {
96 zassert_ok(cfb_draw_text(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(draw_text_rectspace1016,test_draw_text_at_11_17)102 ZTEST(draw_text_rectspace1016, test_draw_text_at_11_17)
103 {
104 zassert_ok(cfb_draw_text(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 * with kerning
112 */
ZTEST(draw_text_rectspace1016,test_draw_text_at_0_0_kerning_3)113 ZTEST(draw_text_rectspace1016, test_draw_text_at_0_0_kerning_3)
114 {
115 cfb_set_kerning(dev, 3);
116 zassert_ok(cfb_draw_text(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(draw_text_rectspace1016,test_draw_text_at_1_1_kerning_3)122 ZTEST(draw_text_rectspace1016, test_draw_text_at_1_1_kerning_3)
123 {
124 cfb_set_kerning(dev, 3);
125 zassert_ok(cfb_draw_text(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(draw_text_rectspace1016,test_draw_text_at_9_15_kerning_3)131 ZTEST(draw_text_rectspace1016, test_draw_text_at_9_15_kerning_3)
132 {
133 cfb_set_kerning(dev, 3);
134 zassert_ok(cfb_draw_text(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(draw_text_rectspace1016,test_draw_text_at_10_16_kerning_3)140 ZTEST(draw_text_rectspace1016, test_draw_text_at_10_16_kerning_3)
141 {
142 cfb_set_kerning(dev, 3);
143 zassert_ok(cfb_draw_text(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(draw_text_rectspace1016,test_draw_text_at_11_17_kerning_3)149 ZTEST(draw_text_rectspace1016, test_draw_text_at_11_17_kerning_3)
150 {
151 cfb_set_kerning(dev, 3);
152 zassert_ok(cfb_draw_text(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(draw_text_rectspace1016,test_draw_text_kerning_3_within_right_border)158 ZTEST(draw_text_rectspace1016, test_draw_text_kerning_3_within_right_border)
159 {
160 cfb_set_kerning(dev, 3);
161 zassert_ok(cfb_draw_text(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(draw_text_rectspace1016,test_draw_text_kerning_3_over_right_border)167 ZTEST(draw_text_rectspace1016, test_draw_text_kerning_3_over_right_border)
168 {
169 cfb_set_kerning(dev, 3);
170 zassert_ok(cfb_draw_text(dev, " ", display_width - 22, 17));
171 zassert_ok(cfb_framebuffer_finalize(dev));
172
173 zassert_true(
174 verify_image(display_width - 22, 17, kerning_3_rightclip_1_2rectspace1016, 22, 16));
175 }
176
ZTEST(draw_text_rectspace1016,test_draw_text_outside_top_left)177 ZTEST(draw_text_rectspace1016, test_draw_text_outside_top_left)
178 {
179 zassert_ok(cfb_draw_text(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(draw_text_rectspace1016,test_draw_text_outside_top_right)185 ZTEST(draw_text_rectspace1016, test_draw_text_outside_top_right)
186 {
187 zassert_ok(cfb_draw_text(dev, " ", display_width - 5, -(16 - 8)));
188 zassert_ok(cfb_framebuffer_finalize(dev));
189
190 zassert_true(verify_image(display_width - 5, 0, outside_top_right, 5, 8));
191 }
192
ZTEST(draw_text_rectspace1016,test_draw_text_outside_bottom_right)193 ZTEST(draw_text_rectspace1016, test_draw_text_outside_bottom_right)
194 {
195 zassert_ok(cfb_draw_text(dev, " ", display_width - 3, display_height - 5));
196 zassert_ok(cfb_framebuffer_finalize(dev));
197
198 zassert_true(
199 verify_image(display_width - 3, display_height - 5, outside_bottom_right, 3, 5));
200 }
201
ZTEST(draw_text_rectspace1016,test_draw_text_outside_bottom_left)202 ZTEST(draw_text_rectspace1016, test_draw_text_outside_bottom_left)
203 {
204 zassert_ok(cfb_draw_text(dev, " ", -(10 - 3), display_height - 14));
205 zassert_ok(cfb_framebuffer_finalize(dev));
206
207 zassert_true(verify_image(0, display_height - 14, outside_bottom_left, 3, 14));
208 }
209
210 ZTEST_SUITE(draw_text_rectspace1016, NULL, NULL, cfb_test_before, cfb_test_after, NULL);
211