| 1 | /*
|
|---|
| 2 | * Copyright (c) 2020 Jiri Svoboda
|
|---|
| 3 | * All rights reserved.
|
|---|
| 4 | *
|
|---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
|---|
| 6 | * modification, are permitted provided that the following conditions
|
|---|
| 7 | * are met:
|
|---|
| 8 | *
|
|---|
| 9 | * - Redistributions of source code must retain the above copyright
|
|---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
|---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
|---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
|---|
| 13 | * documentation and/or other materials provided with the distribution.
|
|---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
|---|
| 15 | * derived from this software without specific prior written permission.
|
|---|
| 16 | *
|
|---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|---|
| 27 | */
|
|---|
| 28 |
|
|---|
| 29 | #include <gfx/context.h>
|
|---|
| 30 | #include <gfx/coord.h>
|
|---|
| 31 | #include <mem.h>
|
|---|
| 32 | #include <pcut/pcut.h>
|
|---|
| 33 | #include <stdbool.h>
|
|---|
| 34 | #include <ui/control.h>
|
|---|
| 35 | #include <ui/image.h>
|
|---|
| 36 | #include <ui/ui.h>
|
|---|
| 37 | #include "../private/dummygc.h"
|
|---|
| 38 | #include "../private/image.h"
|
|---|
| 39 |
|
|---|
| 40 | PCUT_INIT;
|
|---|
| 41 |
|
|---|
| 42 | PCUT_TEST_SUITE(image);
|
|---|
| 43 |
|
|---|
| 44 | typedef struct {
|
|---|
| 45 | bool clicked;
|
|---|
| 46 | } test_cb_resp_t;
|
|---|
| 47 |
|
|---|
| 48 | /** Create and destroy image */
|
|---|
| 49 | PCUT_TEST(create_destroy)
|
|---|
| 50 | {
|
|---|
| 51 | ui_image_t *image = NULL;
|
|---|
| 52 | gfx_rect_t brect;
|
|---|
| 53 | errno_t rc;
|
|---|
| 54 |
|
|---|
| 55 | rc = ui_image_create(NULL, NULL, &brect, &image);
|
|---|
| 56 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 57 | PCUT_ASSERT_NOT_NULL(image);
|
|---|
| 58 |
|
|---|
| 59 | ui_image_destroy(image);
|
|---|
| 60 | }
|
|---|
| 61 |
|
|---|
| 62 | /** ui_image_destroy() can take NULL argument (no-op) */
|
|---|
| 63 | PCUT_TEST(destroy_null)
|
|---|
| 64 | {
|
|---|
| 65 | ui_image_destroy(NULL);
|
|---|
| 66 | }
|
|---|
| 67 |
|
|---|
| 68 | /** ui_image_ctl() returns control that has a working virtual destructor */
|
|---|
| 69 | PCUT_TEST(ctl)
|
|---|
| 70 | {
|
|---|
| 71 | ui_image_t *image = NULL;
|
|---|
| 72 | ui_control_t *control;
|
|---|
| 73 | gfx_rect_t brect;
|
|---|
| 74 | errno_t rc;
|
|---|
| 75 |
|
|---|
| 76 | rc = ui_image_create(NULL, NULL, &brect, &image);
|
|---|
| 77 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 78 | PCUT_ASSERT_NOT_NULL(image);
|
|---|
| 79 |
|
|---|
| 80 | control = ui_image_ctl(image);
|
|---|
| 81 | PCUT_ASSERT_NOT_NULL(control);
|
|---|
| 82 |
|
|---|
| 83 | ui_control_destroy(control);
|
|---|
| 84 | }
|
|---|
| 85 |
|
|---|
| 86 | /** Set image rectangle sets internal field */
|
|---|
| 87 | PCUT_TEST(set_rect)
|
|---|
| 88 | {
|
|---|
| 89 | ui_image_t *image = NULL;
|
|---|
| 90 | gfx_rect_t brect;
|
|---|
| 91 | gfx_rect_t rect;
|
|---|
| 92 | errno_t rc;
|
|---|
| 93 |
|
|---|
| 94 | rc = ui_image_create(NULL, NULL, &brect, &image);
|
|---|
| 95 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 96 | PCUT_ASSERT_NOT_NULL(image);
|
|---|
| 97 |
|
|---|
| 98 | rect.p0.x = 1;
|
|---|
| 99 | rect.p0.y = 2;
|
|---|
| 100 | rect.p1.x = 3;
|
|---|
| 101 | rect.p1.y = 4;
|
|---|
| 102 |
|
|---|
| 103 | ui_image_set_rect(image, &rect);
|
|---|
| 104 | PCUT_ASSERT_INT_EQUALS(rect.p0.x, image->rect.p0.x);
|
|---|
| 105 | PCUT_ASSERT_INT_EQUALS(rect.p0.y, image->rect.p0.y);
|
|---|
| 106 | PCUT_ASSERT_INT_EQUALS(rect.p1.x, image->rect.p1.x);
|
|---|
| 107 | PCUT_ASSERT_INT_EQUALS(rect.p1.y, image->rect.p1.y);
|
|---|
| 108 |
|
|---|
| 109 | ui_image_destroy(image);
|
|---|
| 110 | }
|
|---|
| 111 |
|
|---|
| 112 | /** Set image flags sets internal field */
|
|---|
| 113 | PCUT_TEST(set_flags)
|
|---|
| 114 | {
|
|---|
| 115 | ui_image_t *image = NULL;
|
|---|
| 116 | gfx_rect_t brect;
|
|---|
| 117 | errno_t rc;
|
|---|
| 118 |
|
|---|
| 119 | rc = ui_image_create(NULL, NULL, &brect, &image);
|
|---|
| 120 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 121 | PCUT_ASSERT_NOT_NULL(image);
|
|---|
| 122 |
|
|---|
| 123 | PCUT_ASSERT_INT_EQUALS(0, image->flags);
|
|---|
| 124 |
|
|---|
| 125 | ui_image_set_flags(image, ui_imgf_frame);
|
|---|
| 126 | PCUT_ASSERT_INT_EQUALS(ui_imgf_frame, image->flags);
|
|---|
| 127 |
|
|---|
| 128 | ui_image_destroy(image);
|
|---|
| 129 | }
|
|---|
| 130 |
|
|---|
| 131 | /** Set image bitmap */
|
|---|
| 132 | PCUT_TEST(set_bmp)
|
|---|
| 133 | {
|
|---|
| 134 | ui_image_t *image = NULL;
|
|---|
| 135 | gfx_rect_t brect;
|
|---|
| 136 | gfx_rect_t rect;
|
|---|
| 137 | gfx_bitmap_t *bitmap;
|
|---|
| 138 | gfx_bitmap_params_t params;
|
|---|
| 139 | dummy_gc_t *dgc;
|
|---|
| 140 | gfx_context_t *gc;
|
|---|
| 141 | errno_t rc;
|
|---|
| 142 |
|
|---|
| 143 | rc = dummygc_create(&dgc);
|
|---|
| 144 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 145 |
|
|---|
| 146 | gc = dummygc_get_ctx(dgc);
|
|---|
| 147 |
|
|---|
| 148 | rc = ui_image_create(NULL, NULL, &brect, &image);
|
|---|
| 149 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 150 | PCUT_ASSERT_NOT_NULL(image);
|
|---|
| 151 |
|
|---|
| 152 | rect.p0.x = 1;
|
|---|
| 153 | rect.p0.y = 2;
|
|---|
| 154 | rect.p1.x = 3;
|
|---|
| 155 | rect.p1.y = 4;
|
|---|
| 156 |
|
|---|
| 157 | ui_image_set_rect(image, &rect);
|
|---|
| 158 | PCUT_ASSERT_INT_EQUALS(rect.p0.x, image->rect.p0.x);
|
|---|
| 159 | PCUT_ASSERT_INT_EQUALS(rect.p0.y, image->rect.p0.y);
|
|---|
| 160 | PCUT_ASSERT_INT_EQUALS(rect.p1.x, image->rect.p1.x);
|
|---|
| 161 | PCUT_ASSERT_INT_EQUALS(rect.p1.y, image->rect.p1.y);
|
|---|
| 162 |
|
|---|
| 163 | gfx_bitmap_params_init(¶ms);
|
|---|
| 164 | rc = gfx_bitmap_create(gc, ¶ms, NULL, &bitmap);
|
|---|
| 165 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 166 |
|
|---|
| 167 | ui_image_set_bmp(image, bitmap, &brect);
|
|---|
| 168 | PCUT_ASSERT_EQUALS(bitmap, image->bitmap);
|
|---|
| 169 |
|
|---|
| 170 | rc = ui_image_paint(image);
|
|---|
| 171 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 172 |
|
|---|
| 173 | ui_image_destroy(image);
|
|---|
| 174 | }
|
|---|
| 175 |
|
|---|
| 176 | /** Paint image */
|
|---|
| 177 | PCUT_TEST(paint)
|
|---|
| 178 | {
|
|---|
| 179 | dummy_gc_t *dgc;
|
|---|
| 180 | gfx_context_t *gc;
|
|---|
| 181 | gfx_bitmap_params_t params;
|
|---|
| 182 | gfx_bitmap_t *bitmap;
|
|---|
| 183 | ui_image_t *image = NULL;
|
|---|
| 184 | gfx_rect_t brect;
|
|---|
| 185 | errno_t rc;
|
|---|
| 186 |
|
|---|
| 187 | rc = dummygc_create(&dgc);
|
|---|
| 188 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 189 |
|
|---|
| 190 | gc = dummygc_get_ctx(dgc);
|
|---|
| 191 |
|
|---|
| 192 | gfx_bitmap_params_init(¶ms);
|
|---|
| 193 | rc = gfx_bitmap_create(gc, ¶ms, NULL, &bitmap);
|
|---|
| 194 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 195 |
|
|---|
| 196 | rc = ui_image_create(NULL, bitmap, &brect, &image);
|
|---|
| 197 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 198 | PCUT_ASSERT_NOT_NULL(image);
|
|---|
| 199 |
|
|---|
| 200 | rc = ui_image_paint(image);
|
|---|
| 201 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 202 |
|
|---|
| 203 | /* Check that we can paint image after setting bitmap to NULL */
|
|---|
| 204 |
|
|---|
| 205 | ui_image_set_bmp(image, NULL, &brect);
|
|---|
| 206 |
|
|---|
| 207 | rc = ui_image_paint(image);
|
|---|
| 208 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 209 |
|
|---|
| 210 | ui_image_destroy(image);
|
|---|
| 211 | }
|
|---|
| 212 |
|
|---|
| 213 | PCUT_EXPORT(image);
|
|---|