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(invert_area, 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
34 memset(read_buffer, 0, sizeof(read_buffer));
35 zassert_ok(display_write(dev, 0, 0, &desc, read_buffer));
36
37 zassert_ok(display_blanking_off(dev));
38
39 zassert_ok(cfb_framebuffer_init(dev));
40 }
41
cfb_test_after(void * test_fixture)42 static void cfb_test_after(void *test_fixture)
43 {
44 cfb_framebuffer_deinit(dev);
45 }
46
ZTEST(invert_area,test_invert_area_whole_screen)47 ZTEST(invert_area, test_invert_area_whole_screen)
48 {
49 zassert_ok(cfb_invert_area(dev, 0, 0, 320, 240));
50 zassert_ok(cfb_framebuffer_finalize(dev), "cfb_framebuffer_finalize failed");
51
52 zassert_true(verify_color_inside_rect(0, 0, 320, 240, 0xFFFFFF));
53 }
54
ZTEST(invert_area,test_invert_area_overlapped_2times)55 ZTEST(invert_area, test_invert_area_overlapped_2times)
56 {
57 zassert_ok(cfb_invert_area(dev, 33, 37, 79, 77));
58 zassert_ok(cfb_invert_area(dev, 100, 37, 53, 77));
59 zassert_ok(cfb_framebuffer_finalize(dev), "cfb_framebuffer_finalize failed");
60
61 zassert_true(verify_color_inside_rect(33, 37, 67, 77, 0xFFFFFF));
62 zassert_true(verify_color_inside_rect(100, 37, 12, 77, 0x0));
63 zassert_true(verify_color_inside_rect(112, 37, 41, 77, 0xFFFFFF));
64 zassert_true(verify_color_outside_rect(33, 37, 120, 77, 0x0));
65 }
66
ZTEST(invert_area,test_invert_area_overlap_top_left)67 ZTEST(invert_area, test_invert_area_overlap_top_left)
68 {
69 int err;
70
71 err = cfb_invert_area(dev, -10, -10, 20, 20);
72 zassert_not_ok(err, "out of rect");
73 }
74
ZTEST(invert_area,test_invert_area_overlap_top_right)75 ZTEST(invert_area, test_invert_area_overlap_top_right)
76 {
77 int err;
78
79 err = cfb_invert_area(dev, 230, -10, 20, 20);
80 zassert_not_ok(err, "out of rect");
81 }
82
ZTEST(invert_area,test_invert_area_overlap_bottom_left)83 ZTEST(invert_area, test_invert_area_overlap_bottom_left)
84 {
85 int err;
86
87 err = cfb_invert_area(dev, -10, display_height - 10, 20, 20);
88 zassert_not_ok(err, "out of rect");
89 }
90
ZTEST(invert_area,test_invert_area_overlap_bottom_right)91 ZTEST(invert_area, test_invert_area_overlap_bottom_right)
92 {
93 zassert_ok(cfb_invert_area(dev, display_width - 10, display_height - 10, 20, 20));
94 zassert_ok(cfb_framebuffer_finalize(dev));
95
96 zassert_true(verify_color_inside_rect(display_width - 10, display_height - 10, 10, 10,
97 0xFFFFFF));
98 zassert_true(
99 verify_color_outside_rect(display_width - 10, display_height - 10, 10, 10, 0x0));
100 }
101
ZTEST(invert_area,test_invert_area_outside_top_left)102 ZTEST(invert_area, test_invert_area_outside_top_left)
103 {
104 zassert_not_ok(cfb_invert_area(dev, -10, -10, 10, 10), "out of rect");
105 }
106
ZTEST(invert_area,test_invert_area_outside_bottom_right)107 ZTEST(invert_area, test_invert_area_outside_bottom_right)
108 {
109 zassert_not_ok(cfb_invert_area(dev, display_width, display_height, 20, 20), "out of rect");
110 }
111
112 ZTEST_SUITE(invert_area, NULL, NULL, cfb_test_before, cfb_test_after, NULL);
113