[03145ee] | 1 | /*
|
---|
[f0ccb2ab] | 2 | * Copyright (c) 2021 Jiri Svoboda
|
---|
[03145ee] | 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/entry.h>
|
---|
| 36 | #include <ui/resource.h>
|
---|
[db3895d] | 37 | #include <ui/ui.h>
|
---|
| 38 | #include <ui/window.h>
|
---|
[03145ee] | 39 | #include "../private/entry.h"
|
---|
| 40 |
|
---|
| 41 | PCUT_INIT;
|
---|
| 42 |
|
---|
| 43 | PCUT_TEST_SUITE(entry);
|
---|
| 44 |
|
---|
| 45 | /** Create and destroy text entry */
|
---|
| 46 | PCUT_TEST(create_destroy)
|
---|
| 47 | {
|
---|
| 48 | ui_entry_t *entry = NULL;
|
---|
| 49 | errno_t rc;
|
---|
| 50 |
|
---|
| 51 | rc = ui_entry_create(NULL, "Hello", &entry);
|
---|
| 52 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 53 | PCUT_ASSERT_NOT_NULL(entry);
|
---|
| 54 |
|
---|
| 55 | ui_entry_destroy(entry);
|
---|
| 56 | }
|
---|
| 57 |
|
---|
| 58 | /** ui_entry_destroy() can take NULL argument (no-op) */
|
---|
| 59 | PCUT_TEST(destroy_null)
|
---|
| 60 | {
|
---|
| 61 | ui_entry_destroy(NULL);
|
---|
| 62 | }
|
---|
| 63 |
|
---|
| 64 | /** ui_entry_ctl() returns control that has a working virtual destructor */
|
---|
| 65 | PCUT_TEST(ctl)
|
---|
| 66 | {
|
---|
| 67 | ui_entry_t *entry;
|
---|
| 68 | ui_control_t *control;
|
---|
| 69 | errno_t rc;
|
---|
| 70 |
|
---|
| 71 | rc = ui_entry_create(NULL, "Hello", &entry);
|
---|
| 72 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 73 |
|
---|
| 74 | control = ui_entry_ctl(entry);
|
---|
| 75 | PCUT_ASSERT_NOT_NULL(control);
|
---|
| 76 |
|
---|
| 77 | ui_control_destroy(control);
|
---|
| 78 | }
|
---|
| 79 |
|
---|
| 80 | /** Set text entry rectangle sets internal field */
|
---|
| 81 | PCUT_TEST(set_rect)
|
---|
| 82 | {
|
---|
| 83 | ui_entry_t *entry;
|
---|
| 84 | gfx_rect_t rect;
|
---|
| 85 | errno_t rc;
|
---|
| 86 |
|
---|
| 87 | rc = ui_entry_create(NULL, "Hello", &entry);
|
---|
| 88 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 89 |
|
---|
| 90 | rect.p0.x = 1;
|
---|
| 91 | rect.p0.y = 2;
|
---|
| 92 | rect.p1.x = 3;
|
---|
| 93 | rect.p1.y = 4;
|
---|
| 94 |
|
---|
| 95 | ui_entry_set_rect(entry, &rect);
|
---|
| 96 | PCUT_ASSERT_INT_EQUALS(rect.p0.x, entry->rect.p0.x);
|
---|
| 97 | PCUT_ASSERT_INT_EQUALS(rect.p0.y, entry->rect.p0.y);
|
---|
| 98 | PCUT_ASSERT_INT_EQUALS(rect.p1.x, entry->rect.p1.x);
|
---|
| 99 | PCUT_ASSERT_INT_EQUALS(rect.p1.y, entry->rect.p1.y);
|
---|
| 100 |
|
---|
| 101 | ui_entry_destroy(entry);
|
---|
| 102 | }
|
---|
| 103 |
|
---|
| 104 | /** Set entry text horizontal alignment sets internal field */
|
---|
| 105 | PCUT_TEST(set_halign)
|
---|
| 106 | {
|
---|
| 107 | ui_entry_t *entry;
|
---|
| 108 | errno_t rc;
|
---|
| 109 |
|
---|
| 110 | rc = ui_entry_create(NULL, "Hello", &entry);
|
---|
| 111 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 112 |
|
---|
| 113 | ui_entry_set_halign(entry, gfx_halign_left);
|
---|
| 114 | PCUT_ASSERT_EQUALS(gfx_halign_left, entry->halign);
|
---|
| 115 | ui_entry_set_halign(entry, gfx_halign_center);
|
---|
| 116 | PCUT_ASSERT_EQUALS(gfx_halign_center, entry->halign);
|
---|
| 117 |
|
---|
| 118 | ui_entry_destroy(entry);
|
---|
| 119 | }
|
---|
| 120 |
|
---|
[7481ee19] | 121 | /** Set entry read only flag sets internal field */
|
---|
| 122 | PCUT_TEST(set_read_only)
|
---|
| 123 | {
|
---|
| 124 | ui_entry_t *entry;
|
---|
| 125 | errno_t rc;
|
---|
| 126 |
|
---|
| 127 | rc = ui_entry_create(NULL, "Hello", &entry);
|
---|
| 128 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 129 |
|
---|
| 130 | ui_entry_set_read_only(entry, true);
|
---|
| 131 | PCUT_ASSERT_TRUE(entry->read_only);
|
---|
| 132 | ui_entry_set_read_only(entry, false);
|
---|
| 133 | PCUT_ASSERT_FALSE(entry->read_only);
|
---|
| 134 |
|
---|
| 135 | ui_entry_destroy(entry);
|
---|
| 136 | }
|
---|
| 137 |
|
---|
[03145ee] | 138 | /** Set text entry rectangle sets internal field */
|
---|
| 139 | PCUT_TEST(set_text)
|
---|
| 140 | {
|
---|
| 141 | ui_entry_t *entry;
|
---|
| 142 | gfx_rect_t rect;
|
---|
| 143 | errno_t rc;
|
---|
| 144 |
|
---|
| 145 | rc = ui_entry_create(NULL, "Hello", &entry);
|
---|
| 146 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 147 |
|
---|
| 148 | rect.p0.x = 1;
|
---|
| 149 | rect.p0.y = 2;
|
---|
| 150 | rect.p1.x = 3;
|
---|
| 151 | rect.p1.y = 4;
|
---|
| 152 |
|
---|
| 153 | ui_entry_set_rect(entry, &rect);
|
---|
| 154 | PCUT_ASSERT_INT_EQUALS(rect.p0.x, entry->rect.p0.x);
|
---|
| 155 | PCUT_ASSERT_INT_EQUALS(rect.p0.y, entry->rect.p0.y);
|
---|
| 156 | PCUT_ASSERT_INT_EQUALS(rect.p1.x, entry->rect.p1.x);
|
---|
| 157 | PCUT_ASSERT_INT_EQUALS(rect.p1.y, entry->rect.p1.y);
|
---|
| 158 |
|
---|
| 159 | ui_entry_destroy(entry);
|
---|
| 160 | }
|
---|
| 161 |
|
---|
| 162 | /** Paint text entry */
|
---|
| 163 | PCUT_TEST(paint)
|
---|
| 164 | {
|
---|
| 165 | errno_t rc;
|
---|
[db3895d] | 166 | ui_t *ui = NULL;
|
---|
| 167 | ui_window_t *window = NULL;
|
---|
| 168 | ui_wnd_params_t params;
|
---|
[03145ee] | 169 | ui_entry_t *entry;
|
---|
| 170 |
|
---|
[db3895d] | 171 | rc = ui_create_disp(NULL, &ui);
|
---|
[03145ee] | 172 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 173 |
|
---|
[db3895d] | 174 | ui_wnd_params_init(¶ms);
|
---|
| 175 | params.caption = "Hello";
|
---|
| 176 |
|
---|
| 177 | rc = ui_window_create(ui, ¶ms, &window);
|
---|
[03145ee] | 178 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
[db3895d] | 179 | PCUT_ASSERT_NOT_NULL(window);
|
---|
[03145ee] | 180 |
|
---|
[db3895d] | 181 | rc = ui_entry_create(window, "Hello", &entry);
|
---|
[03145ee] | 182 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 183 |
|
---|
| 184 | rc = ui_entry_paint(entry);
|
---|
| 185 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 186 |
|
---|
| 187 | ui_entry_destroy(entry);
|
---|
[db3895d] | 188 | ui_window_destroy(window);
|
---|
| 189 | ui_destroy(ui);
|
---|
[03145ee] | 190 | }
|
---|
| 191 |
|
---|
[7481ee19] | 192 | /** ui_entry_insert_str() inserts string at cursor. */
|
---|
| 193 | PCUT_TEST(insert_str)
|
---|
| 194 | {
|
---|
| 195 | errno_t rc;
|
---|
| 196 | ui_t *ui = NULL;
|
---|
| 197 | ui_window_t *window = NULL;
|
---|
| 198 | ui_wnd_params_t params;
|
---|
| 199 | ui_entry_t *entry;
|
---|
| 200 |
|
---|
| 201 | rc = ui_create_disp(NULL, &ui);
|
---|
| 202 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 203 |
|
---|
| 204 | ui_wnd_params_init(¶ms);
|
---|
| 205 | params.caption = "Hello";
|
---|
| 206 |
|
---|
| 207 | rc = ui_window_create(ui, ¶ms, &window);
|
---|
| 208 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 209 | PCUT_ASSERT_NOT_NULL(window);
|
---|
| 210 |
|
---|
| 211 | rc = ui_entry_create(window, "A", &entry);
|
---|
| 212 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 213 |
|
---|
| 214 | PCUT_ASSERT_STR_EQUALS("A", entry->text);
|
---|
| 215 |
|
---|
| 216 | rc = ui_entry_insert_str(entry, "B");
|
---|
| 217 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 218 |
|
---|
| 219 | PCUT_ASSERT_STR_EQUALS("AB", entry->text);
|
---|
| 220 |
|
---|
| 221 | rc = ui_entry_insert_str(entry, "CD");
|
---|
| 222 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 223 |
|
---|
| 224 | PCUT_ASSERT_STR_EQUALS("ABCD", entry->text);
|
---|
| 225 |
|
---|
| 226 | ui_entry_destroy(entry);
|
---|
| 227 | ui_window_destroy(window);
|
---|
| 228 | ui_destroy(ui);
|
---|
| 229 | }
|
---|
| 230 |
|
---|
| 231 | /** ui_entry_backspace() deletes character before cursor. */
|
---|
| 232 | PCUT_TEST(backspace)
|
---|
| 233 | {
|
---|
| 234 | errno_t rc;
|
---|
| 235 | ui_t *ui = NULL;
|
---|
| 236 | ui_window_t *window = NULL;
|
---|
| 237 | ui_wnd_params_t params;
|
---|
| 238 | ui_entry_t *entry;
|
---|
| 239 |
|
---|
| 240 | rc = ui_create_disp(NULL, &ui);
|
---|
| 241 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 242 |
|
---|
| 243 | ui_wnd_params_init(¶ms);
|
---|
| 244 | params.caption = "Hello";
|
---|
| 245 |
|
---|
| 246 | rc = ui_window_create(ui, ¶ms, &window);
|
---|
| 247 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 248 | PCUT_ASSERT_NOT_NULL(window);
|
---|
| 249 |
|
---|
| 250 | rc = ui_entry_create(window, "ABC", &entry);
|
---|
| 251 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 252 |
|
---|
| 253 | PCUT_ASSERT_STR_EQUALS("ABC", entry->text);
|
---|
| 254 |
|
---|
| 255 | ui_entry_backspace(entry);
|
---|
| 256 | PCUT_ASSERT_STR_EQUALS("AB", entry->text);
|
---|
| 257 |
|
---|
| 258 | ui_entry_backspace(entry);
|
---|
| 259 | PCUT_ASSERT_STR_EQUALS("A", entry->text);
|
---|
| 260 |
|
---|
| 261 | ui_entry_backspace(entry);
|
---|
| 262 | PCUT_ASSERT_STR_EQUALS("", entry->text);
|
---|
| 263 |
|
---|
| 264 | ui_entry_backspace(entry);
|
---|
| 265 | PCUT_ASSERT_STR_EQUALS("", entry->text);
|
---|
| 266 |
|
---|
| 267 | ui_entry_destroy(entry);
|
---|
| 268 | ui_window_destroy(window);
|
---|
| 269 | ui_destroy(ui);
|
---|
| 270 | }
|
---|
| 271 |
|
---|
[03145ee] | 272 | PCUT_EXPORT(entry);
|
---|