[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 |
|
---|
[c9722c1] | 29 | #include <clipboard.h>
|
---|
[03145ee] | 30 | #include <gfx/context.h>
|
---|
| 31 | #include <gfx/coord.h>
|
---|
| 32 | #include <mem.h>
|
---|
| 33 | #include <pcut/pcut.h>
|
---|
| 34 | #include <stdbool.h>
|
---|
| 35 | #include <ui/control.h>
|
---|
| 36 | #include <ui/entry.h>
|
---|
| 37 | #include <ui/resource.h>
|
---|
[db3895d] | 38 | #include <ui/ui.h>
|
---|
| 39 | #include <ui/window.h>
|
---|
[03145ee] | 40 | #include "../private/entry.h"
|
---|
| 41 |
|
---|
| 42 | PCUT_INIT;
|
---|
| 43 |
|
---|
| 44 | PCUT_TEST_SUITE(entry);
|
---|
| 45 |
|
---|
| 46 | /** Create and destroy text entry */
|
---|
| 47 | PCUT_TEST(create_destroy)
|
---|
| 48 | {
|
---|
| 49 | ui_entry_t *entry = NULL;
|
---|
| 50 | errno_t rc;
|
---|
| 51 |
|
---|
| 52 | rc = ui_entry_create(NULL, "Hello", &entry);
|
---|
| 53 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 54 | PCUT_ASSERT_NOT_NULL(entry);
|
---|
| 55 |
|
---|
| 56 | ui_entry_destroy(entry);
|
---|
| 57 | }
|
---|
| 58 |
|
---|
| 59 | /** ui_entry_destroy() can take NULL argument (no-op) */
|
---|
| 60 | PCUT_TEST(destroy_null)
|
---|
| 61 | {
|
---|
| 62 | ui_entry_destroy(NULL);
|
---|
| 63 | }
|
---|
| 64 |
|
---|
| 65 | /** ui_entry_ctl() returns control that has a working virtual destructor */
|
---|
| 66 | PCUT_TEST(ctl)
|
---|
| 67 | {
|
---|
| 68 | ui_entry_t *entry;
|
---|
| 69 | ui_control_t *control;
|
---|
| 70 | errno_t rc;
|
---|
| 71 |
|
---|
| 72 | rc = ui_entry_create(NULL, "Hello", &entry);
|
---|
| 73 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 74 |
|
---|
| 75 | control = ui_entry_ctl(entry);
|
---|
| 76 | PCUT_ASSERT_NOT_NULL(control);
|
---|
| 77 |
|
---|
| 78 | ui_control_destroy(control);
|
---|
| 79 | }
|
---|
| 80 |
|
---|
| 81 | /** Set text entry rectangle sets internal field */
|
---|
| 82 | PCUT_TEST(set_rect)
|
---|
| 83 | {
|
---|
| 84 | ui_entry_t *entry;
|
---|
| 85 | gfx_rect_t rect;
|
---|
| 86 | errno_t rc;
|
---|
| 87 |
|
---|
| 88 | rc = ui_entry_create(NULL, "Hello", &entry);
|
---|
| 89 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 90 |
|
---|
| 91 | rect.p0.x = 1;
|
---|
| 92 | rect.p0.y = 2;
|
---|
| 93 | rect.p1.x = 3;
|
---|
| 94 | rect.p1.y = 4;
|
---|
| 95 |
|
---|
| 96 | ui_entry_set_rect(entry, &rect);
|
---|
| 97 | PCUT_ASSERT_INT_EQUALS(rect.p0.x, entry->rect.p0.x);
|
---|
| 98 | PCUT_ASSERT_INT_EQUALS(rect.p0.y, entry->rect.p0.y);
|
---|
| 99 | PCUT_ASSERT_INT_EQUALS(rect.p1.x, entry->rect.p1.x);
|
---|
| 100 | PCUT_ASSERT_INT_EQUALS(rect.p1.y, entry->rect.p1.y);
|
---|
| 101 |
|
---|
| 102 | ui_entry_destroy(entry);
|
---|
| 103 | }
|
---|
| 104 |
|
---|
| 105 | /** Set entry text horizontal alignment sets internal field */
|
---|
| 106 | PCUT_TEST(set_halign)
|
---|
| 107 | {
|
---|
| 108 | errno_t rc;
|
---|
[f0155e4] | 109 | ui_t *ui = NULL;
|
---|
| 110 | ui_window_t *window = NULL;
|
---|
| 111 | ui_wnd_params_t params;
|
---|
| 112 | ui_entry_t *entry;
|
---|
[03145ee] | 113 |
|
---|
[f0155e4] | 114 | rc = ui_create_disp(NULL, &ui);
|
---|
| 115 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 116 |
|
---|
| 117 | ui_wnd_params_init(¶ms);
|
---|
| 118 | params.caption = "Hello";
|
---|
| 119 |
|
---|
| 120 | rc = ui_window_create(ui, ¶ms, &window);
|
---|
| 121 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 122 | PCUT_ASSERT_NOT_NULL(window);
|
---|
| 123 |
|
---|
| 124 | rc = ui_entry_create(window, "Hello", &entry);
|
---|
[03145ee] | 125 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 126 |
|
---|
| 127 | ui_entry_set_halign(entry, gfx_halign_left);
|
---|
| 128 | PCUT_ASSERT_EQUALS(gfx_halign_left, entry->halign);
|
---|
| 129 | ui_entry_set_halign(entry, gfx_halign_center);
|
---|
| 130 | PCUT_ASSERT_EQUALS(gfx_halign_center, entry->halign);
|
---|
| 131 |
|
---|
| 132 | ui_entry_destroy(entry);
|
---|
[f0155e4] | 133 | ui_window_destroy(window);
|
---|
| 134 | ui_destroy(ui);
|
---|
[03145ee] | 135 | }
|
---|
| 136 |
|
---|
[7481ee19] | 137 | /** Set entry read only flag sets internal field */
|
---|
| 138 | PCUT_TEST(set_read_only)
|
---|
| 139 | {
|
---|
| 140 | ui_entry_t *entry;
|
---|
| 141 | errno_t rc;
|
---|
| 142 |
|
---|
| 143 | rc = ui_entry_create(NULL, "Hello", &entry);
|
---|
| 144 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 145 |
|
---|
| 146 | ui_entry_set_read_only(entry, true);
|
---|
| 147 | PCUT_ASSERT_TRUE(entry->read_only);
|
---|
| 148 | ui_entry_set_read_only(entry, false);
|
---|
| 149 | PCUT_ASSERT_FALSE(entry->read_only);
|
---|
| 150 |
|
---|
| 151 | ui_entry_destroy(entry);
|
---|
| 152 | }
|
---|
| 153 |
|
---|
[03145ee] | 154 | /** Set text entry rectangle sets internal field */
|
---|
| 155 | PCUT_TEST(set_text)
|
---|
| 156 | {
|
---|
| 157 | ui_entry_t *entry;
|
---|
| 158 | gfx_rect_t rect;
|
---|
| 159 | errno_t rc;
|
---|
| 160 |
|
---|
| 161 | rc = ui_entry_create(NULL, "Hello", &entry);
|
---|
| 162 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 163 |
|
---|
| 164 | rect.p0.x = 1;
|
---|
| 165 | rect.p0.y = 2;
|
---|
| 166 | rect.p1.x = 3;
|
---|
| 167 | rect.p1.y = 4;
|
---|
| 168 |
|
---|
| 169 | ui_entry_set_rect(entry, &rect);
|
---|
| 170 | PCUT_ASSERT_INT_EQUALS(rect.p0.x, entry->rect.p0.x);
|
---|
| 171 | PCUT_ASSERT_INT_EQUALS(rect.p0.y, entry->rect.p0.y);
|
---|
| 172 | PCUT_ASSERT_INT_EQUALS(rect.p1.x, entry->rect.p1.x);
|
---|
| 173 | PCUT_ASSERT_INT_EQUALS(rect.p1.y, entry->rect.p1.y);
|
---|
| 174 |
|
---|
| 175 | ui_entry_destroy(entry);
|
---|
| 176 | }
|
---|
| 177 |
|
---|
| 178 | /** Paint text entry */
|
---|
| 179 | PCUT_TEST(paint)
|
---|
| 180 | {
|
---|
| 181 | errno_t rc;
|
---|
[db3895d] | 182 | ui_t *ui = NULL;
|
---|
| 183 | ui_window_t *window = NULL;
|
---|
| 184 | ui_wnd_params_t params;
|
---|
[03145ee] | 185 | ui_entry_t *entry;
|
---|
| 186 |
|
---|
[db3895d] | 187 | rc = ui_create_disp(NULL, &ui);
|
---|
[03145ee] | 188 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 189 |
|
---|
[db3895d] | 190 | ui_wnd_params_init(¶ms);
|
---|
| 191 | params.caption = "Hello";
|
---|
| 192 |
|
---|
| 193 | rc = ui_window_create(ui, ¶ms, &window);
|
---|
[03145ee] | 194 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
[db3895d] | 195 | PCUT_ASSERT_NOT_NULL(window);
|
---|
[03145ee] | 196 |
|
---|
[db3895d] | 197 | rc = ui_entry_create(window, "Hello", &entry);
|
---|
[03145ee] | 198 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 199 |
|
---|
| 200 | rc = ui_entry_paint(entry);
|
---|
| 201 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 202 |
|
---|
| 203 | ui_entry_destroy(entry);
|
---|
[db3895d] | 204 | ui_window_destroy(window);
|
---|
| 205 | ui_destroy(ui);
|
---|
[03145ee] | 206 | }
|
---|
| 207 |
|
---|
[9eb8d12] | 208 | /** ui_entry_delete_sel() deletes selected text */
|
---|
| 209 | PCUT_TEST(delete_sel)
|
---|
| 210 | {
|
---|
| 211 | errno_t rc;
|
---|
| 212 | ui_t *ui = NULL;
|
---|
| 213 | ui_window_t *window = NULL;
|
---|
| 214 | ui_wnd_params_t params;
|
---|
| 215 | ui_entry_t *entry;
|
---|
| 216 |
|
---|
| 217 | rc = ui_create_disp(NULL, &ui);
|
---|
| 218 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 219 |
|
---|
| 220 | ui_wnd_params_init(¶ms);
|
---|
| 221 | params.caption = "Hello";
|
---|
| 222 |
|
---|
| 223 | rc = ui_window_create(ui, ¶ms, &window);
|
---|
| 224 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 225 | PCUT_ASSERT_NOT_NULL(window);
|
---|
| 226 |
|
---|
| 227 | rc = ui_entry_create(window, "ABCDEF", &entry);
|
---|
| 228 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 229 |
|
---|
| 230 | PCUT_ASSERT_STR_EQUALS("ABCDEF", entry->text);
|
---|
| 231 |
|
---|
| 232 | ui_entry_activate(entry);
|
---|
| 233 |
|
---|
| 234 | /* Select all but first and last character */
|
---|
| 235 | ui_entry_seek_start(entry, false);
|
---|
| 236 | ui_entry_seek_next_char(entry, false);
|
---|
| 237 | ui_entry_seek_end(entry, true);
|
---|
| 238 | ui_entry_seek_prev_char(entry, true);
|
---|
| 239 |
|
---|
| 240 | ui_entry_delete_sel(entry);
|
---|
| 241 |
|
---|
| 242 | PCUT_ASSERT_STR_EQUALS("AF", entry->text);
|
---|
| 243 |
|
---|
| 244 | ui_entry_destroy(entry);
|
---|
| 245 | ui_window_destroy(window);
|
---|
| 246 | ui_destroy(ui);
|
---|
| 247 | }
|
---|
| 248 |
|
---|
[7481ee19] | 249 | /** ui_entry_insert_str() inserts string at cursor. */
|
---|
| 250 | PCUT_TEST(insert_str)
|
---|
| 251 | {
|
---|
| 252 | errno_t rc;
|
---|
| 253 | ui_t *ui = NULL;
|
---|
| 254 | ui_window_t *window = NULL;
|
---|
| 255 | ui_wnd_params_t params;
|
---|
| 256 | ui_entry_t *entry;
|
---|
| 257 |
|
---|
| 258 | rc = ui_create_disp(NULL, &ui);
|
---|
| 259 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 260 |
|
---|
| 261 | ui_wnd_params_init(¶ms);
|
---|
| 262 | params.caption = "Hello";
|
---|
| 263 |
|
---|
| 264 | rc = ui_window_create(ui, ¶ms, &window);
|
---|
| 265 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 266 | PCUT_ASSERT_NOT_NULL(window);
|
---|
| 267 |
|
---|
| 268 | rc = ui_entry_create(window, "A", &entry);
|
---|
| 269 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 270 |
|
---|
| 271 | PCUT_ASSERT_STR_EQUALS("A", entry->text);
|
---|
| 272 |
|
---|
[61bf9dd9] | 273 | ui_entry_activate(entry);
|
---|
[9eb8d12] | 274 | ui_entry_seek_end(entry, false);
|
---|
[61bf9dd9] | 275 |
|
---|
[7481ee19] | 276 | rc = ui_entry_insert_str(entry, "B");
|
---|
| 277 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 278 |
|
---|
| 279 | PCUT_ASSERT_STR_EQUALS("AB", entry->text);
|
---|
| 280 |
|
---|
[61bf9dd9] | 281 | rc = ui_entry_insert_str(entry, "EF");
|
---|
| 282 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 283 |
|
---|
| 284 | PCUT_ASSERT_STR_EQUALS("ABEF", entry->text);
|
---|
| 285 |
|
---|
| 286 | entry->pos = 2;
|
---|
[9eb8d12] | 287 | entry->sel_start = 2;
|
---|
[7481ee19] | 288 | rc = ui_entry_insert_str(entry, "CD");
|
---|
| 289 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 290 |
|
---|
[61bf9dd9] | 291 | PCUT_ASSERT_STR_EQUALS("ABCDEF", entry->text);
|
---|
[7481ee19] | 292 |
|
---|
| 293 | ui_entry_destroy(entry);
|
---|
| 294 | ui_window_destroy(window);
|
---|
| 295 | ui_destroy(ui);
|
---|
| 296 | }
|
---|
| 297 |
|
---|
[9eb8d12] | 298 | /** ui_entry_insert_str() deletes selection before inserting string */
|
---|
| 299 | PCUT_TEST(insert_str_with_sel)
|
---|
| 300 | {
|
---|
| 301 | errno_t rc;
|
---|
| 302 | ui_t *ui = NULL;
|
---|
| 303 | ui_window_t *window = NULL;
|
---|
| 304 | ui_wnd_params_t params;
|
---|
| 305 | ui_entry_t *entry;
|
---|
| 306 |
|
---|
| 307 | rc = ui_create_disp(NULL, &ui);
|
---|
| 308 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 309 |
|
---|
| 310 | ui_wnd_params_init(¶ms);
|
---|
| 311 | params.caption = "Hello";
|
---|
| 312 |
|
---|
| 313 | rc = ui_window_create(ui, ¶ms, &window);
|
---|
| 314 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 315 | PCUT_ASSERT_NOT_NULL(window);
|
---|
| 316 |
|
---|
| 317 | rc = ui_entry_create(window, "ABCDE", &entry);
|
---|
| 318 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 319 |
|
---|
| 320 | PCUT_ASSERT_STR_EQUALS("ABCDE", entry->text);
|
---|
| 321 |
|
---|
| 322 | /* Select all but the first and last character */
|
---|
| 323 | ui_entry_activate(entry);
|
---|
| 324 | ui_entry_seek_start(entry, false);
|
---|
| 325 | ui_entry_seek_next_char(entry, false);
|
---|
| 326 | ui_entry_seek_end(entry, true);
|
---|
| 327 | ui_entry_seek_prev_char(entry, true);
|
---|
| 328 |
|
---|
| 329 | rc = ui_entry_insert_str(entry, "123");
|
---|
| 330 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 331 |
|
---|
| 332 | PCUT_ASSERT_STR_EQUALS("A123E", entry->text);
|
---|
| 333 |
|
---|
| 334 | ui_entry_destroy(entry);
|
---|
| 335 | ui_window_destroy(window);
|
---|
| 336 | ui_destroy(ui);
|
---|
| 337 | }
|
---|
| 338 |
|
---|
[7481ee19] | 339 | /** ui_entry_backspace() deletes character before cursor. */
|
---|
| 340 | PCUT_TEST(backspace)
|
---|
| 341 | {
|
---|
| 342 | errno_t rc;
|
---|
| 343 | ui_t *ui = NULL;
|
---|
| 344 | ui_window_t *window = NULL;
|
---|
| 345 | ui_wnd_params_t params;
|
---|
| 346 | ui_entry_t *entry;
|
---|
| 347 |
|
---|
| 348 | rc = ui_create_disp(NULL, &ui);
|
---|
| 349 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 350 |
|
---|
| 351 | ui_wnd_params_init(¶ms);
|
---|
| 352 | params.caption = "Hello";
|
---|
| 353 |
|
---|
| 354 | rc = ui_window_create(ui, ¶ms, &window);
|
---|
| 355 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 356 | PCUT_ASSERT_NOT_NULL(window);
|
---|
| 357 |
|
---|
[61bf9dd9] | 358 | rc = ui_entry_create(window, "ABCD", &entry);
|
---|
[7481ee19] | 359 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 360 |
|
---|
[61bf9dd9] | 361 | PCUT_ASSERT_STR_EQUALS("ABCD", entry->text);
|
---|
| 362 | entry->pos = 3;
|
---|
[9eb8d12] | 363 | entry->sel_start = 3;
|
---|
[7481ee19] | 364 |
|
---|
| 365 | ui_entry_backspace(entry);
|
---|
[61bf9dd9] | 366 | PCUT_ASSERT_STR_EQUALS("ABD", entry->text);
|
---|
[7481ee19] | 367 |
|
---|
| 368 | ui_entry_backspace(entry);
|
---|
[61bf9dd9] | 369 | PCUT_ASSERT_STR_EQUALS("AD", entry->text);
|
---|
[7481ee19] | 370 |
|
---|
| 371 | ui_entry_backspace(entry);
|
---|
[61bf9dd9] | 372 | PCUT_ASSERT_STR_EQUALS("D", entry->text);
|
---|
[7481ee19] | 373 |
|
---|
| 374 | ui_entry_backspace(entry);
|
---|
[61bf9dd9] | 375 | PCUT_ASSERT_STR_EQUALS("D", entry->text);
|
---|
| 376 |
|
---|
| 377 | ui_entry_destroy(entry);
|
---|
| 378 | ui_window_destroy(window);
|
---|
| 379 | ui_destroy(ui);
|
---|
| 380 | }
|
---|
| 381 |
|
---|
[9eb8d12] | 382 | /** ui_entry_backspace() with selected text deletes selection. */
|
---|
| 383 | PCUT_TEST(backspace_with_sel)
|
---|
| 384 | {
|
---|
| 385 | errno_t rc;
|
---|
| 386 | ui_t *ui = NULL;
|
---|
| 387 | ui_window_t *window = NULL;
|
---|
| 388 | ui_wnd_params_t params;
|
---|
| 389 | ui_entry_t *entry;
|
---|
| 390 |
|
---|
| 391 | rc = ui_create_disp(NULL, &ui);
|
---|
| 392 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 393 |
|
---|
| 394 | ui_wnd_params_init(¶ms);
|
---|
| 395 | params.caption = "Hello";
|
---|
| 396 |
|
---|
| 397 | rc = ui_window_create(ui, ¶ms, &window);
|
---|
| 398 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 399 | PCUT_ASSERT_NOT_NULL(window);
|
---|
| 400 |
|
---|
| 401 | rc = ui_entry_create(window, "ABCDE", &entry);
|
---|
| 402 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 403 |
|
---|
| 404 | PCUT_ASSERT_STR_EQUALS("ABCDE", entry->text);
|
---|
| 405 |
|
---|
| 406 | /* Select all but the first and last character */
|
---|
| 407 | ui_entry_activate(entry);
|
---|
| 408 | ui_entry_seek_start(entry, false);
|
---|
| 409 | ui_entry_seek_next_char(entry, false);
|
---|
| 410 | ui_entry_seek_end(entry, true);
|
---|
| 411 | ui_entry_seek_prev_char(entry, true);
|
---|
| 412 |
|
---|
| 413 | ui_entry_backspace(entry);
|
---|
| 414 |
|
---|
| 415 | PCUT_ASSERT_STR_EQUALS("AE", entry->text);
|
---|
| 416 |
|
---|
| 417 | ui_entry_destroy(entry);
|
---|
| 418 | ui_window_destroy(window);
|
---|
| 419 | ui_destroy(ui);
|
---|
| 420 | }
|
---|
| 421 |
|
---|
[61bf9dd9] | 422 | /** ui_entry_delete() deletes character after cursor. */
|
---|
| 423 | PCUT_TEST(delete)
|
---|
| 424 | {
|
---|
| 425 | errno_t rc;
|
---|
| 426 | ui_t *ui = NULL;
|
---|
| 427 | ui_window_t *window = NULL;
|
---|
| 428 | ui_wnd_params_t params;
|
---|
| 429 | ui_entry_t *entry;
|
---|
| 430 |
|
---|
| 431 | rc = ui_create_disp(NULL, &ui);
|
---|
| 432 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 433 |
|
---|
| 434 | ui_wnd_params_init(¶ms);
|
---|
| 435 | params.caption = "Hello";
|
---|
| 436 |
|
---|
| 437 | rc = ui_window_create(ui, ¶ms, &window);
|
---|
| 438 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 439 | PCUT_ASSERT_NOT_NULL(window);
|
---|
| 440 |
|
---|
| 441 | rc = ui_entry_create(window, "ABCD", &entry);
|
---|
| 442 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 443 |
|
---|
| 444 | PCUT_ASSERT_STR_EQUALS("ABCD", entry->text);
|
---|
| 445 | entry->pos = 1;
|
---|
[9eb8d12] | 446 | entry->sel_start = 1;
|
---|
[61bf9dd9] | 447 |
|
---|
| 448 | ui_entry_delete(entry);
|
---|
| 449 | PCUT_ASSERT_STR_EQUALS("ACD", entry->text);
|
---|
| 450 |
|
---|
| 451 | ui_entry_delete(entry);
|
---|
| 452 | PCUT_ASSERT_STR_EQUALS("AD", entry->text);
|
---|
| 453 |
|
---|
| 454 | ui_entry_delete(entry);
|
---|
| 455 | PCUT_ASSERT_STR_EQUALS("A", entry->text);
|
---|
| 456 |
|
---|
| 457 | ui_entry_delete(entry);
|
---|
| 458 | PCUT_ASSERT_STR_EQUALS("A", entry->text);
|
---|
| 459 |
|
---|
| 460 | ui_entry_destroy(entry);
|
---|
| 461 | ui_window_destroy(window);
|
---|
| 462 | ui_destroy(ui);
|
---|
| 463 | }
|
---|
| 464 |
|
---|
[9eb8d12] | 465 | /** ui_entry_delete() with selected text deletes selection. */
|
---|
| 466 | PCUT_TEST(delete_with_sel)
|
---|
| 467 | {
|
---|
| 468 | errno_t rc;
|
---|
| 469 | ui_t *ui = NULL;
|
---|
| 470 | ui_window_t *window = NULL;
|
---|
| 471 | ui_wnd_params_t params;
|
---|
| 472 | ui_entry_t *entry;
|
---|
| 473 |
|
---|
| 474 | rc = ui_create_disp(NULL, &ui);
|
---|
| 475 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 476 |
|
---|
| 477 | ui_wnd_params_init(¶ms);
|
---|
| 478 | params.caption = "Hello";
|
---|
| 479 |
|
---|
| 480 | rc = ui_window_create(ui, ¶ms, &window);
|
---|
| 481 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 482 | PCUT_ASSERT_NOT_NULL(window);
|
---|
| 483 |
|
---|
| 484 | rc = ui_entry_create(window, "ABCDE", &entry);
|
---|
| 485 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 486 |
|
---|
| 487 | PCUT_ASSERT_STR_EQUALS("ABCDE", entry->text);
|
---|
| 488 |
|
---|
| 489 | /* Select all but the first and last character */
|
---|
| 490 | ui_entry_activate(entry);
|
---|
| 491 | ui_entry_seek_start(entry, false);
|
---|
| 492 | ui_entry_seek_next_char(entry, false);
|
---|
| 493 | ui_entry_seek_end(entry, true);
|
---|
| 494 | ui_entry_seek_prev_char(entry, true);
|
---|
| 495 |
|
---|
| 496 | ui_entry_delete(entry);
|
---|
| 497 |
|
---|
| 498 | PCUT_ASSERT_STR_EQUALS("AE", entry->text);
|
---|
| 499 |
|
---|
| 500 | ui_entry_destroy(entry);
|
---|
| 501 | ui_window_destroy(window);
|
---|
| 502 | ui_destroy(ui);
|
---|
| 503 | }
|
---|
| 504 |
|
---|
[c9722c1] | 505 | /** ui_entry_copy() copies selected text to clipboard. */
|
---|
| 506 | PCUT_TEST(copy)
|
---|
| 507 | {
|
---|
| 508 | errno_t rc;
|
---|
| 509 | ui_t *ui = NULL;
|
---|
| 510 | ui_window_t *window = NULL;
|
---|
| 511 | ui_wnd_params_t params;
|
---|
| 512 | ui_entry_t *entry;
|
---|
| 513 | char *str;
|
---|
| 514 |
|
---|
| 515 | rc = ui_create_disp(NULL, &ui);
|
---|
| 516 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 517 |
|
---|
| 518 | ui_wnd_params_init(¶ms);
|
---|
| 519 | params.caption = "Hello";
|
---|
| 520 |
|
---|
| 521 | rc = ui_window_create(ui, ¶ms, &window);
|
---|
| 522 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 523 | PCUT_ASSERT_NOT_NULL(window);
|
---|
| 524 |
|
---|
| 525 | rc = ui_entry_create(window, "ABCDEF", &entry);
|
---|
| 526 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 527 |
|
---|
| 528 | ui_entry_activate(entry);
|
---|
| 529 | ui_entry_seek_start(entry, false);
|
---|
| 530 | ui_entry_seek_next_char(entry, false);
|
---|
| 531 | ui_entry_seek_end(entry, true);
|
---|
| 532 | ui_entry_seek_prev_char(entry, true);
|
---|
| 533 |
|
---|
| 534 | // FIXME: This is not safe unless we could create a private
|
---|
| 535 | // test clipboard
|
---|
| 536 |
|
---|
| 537 | ui_entry_copy(entry);
|
---|
| 538 | rc = clipboard_get_str(&str);
|
---|
| 539 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 540 | PCUT_ASSERT_STR_EQUALS("BCDE", str);
|
---|
| 541 | PCUT_ASSERT_STR_EQUALS("ABCDEF", entry->text);
|
---|
| 542 | free(str);
|
---|
| 543 |
|
---|
| 544 | ui_entry_destroy(entry);
|
---|
| 545 | ui_window_destroy(window);
|
---|
| 546 | ui_destroy(ui);
|
---|
| 547 | }
|
---|
| 548 |
|
---|
| 549 | /** ui_entry_cut() cuts selected text to clipboard. */
|
---|
| 550 | PCUT_TEST(cut)
|
---|
| 551 | {
|
---|
| 552 | errno_t rc;
|
---|
| 553 | ui_t *ui = NULL;
|
---|
| 554 | ui_window_t *window = NULL;
|
---|
| 555 | ui_wnd_params_t params;
|
---|
| 556 | ui_entry_t *entry;
|
---|
| 557 | char *str;
|
---|
| 558 |
|
---|
| 559 | rc = ui_create_disp(NULL, &ui);
|
---|
| 560 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 561 |
|
---|
| 562 | ui_wnd_params_init(¶ms);
|
---|
| 563 | params.caption = "Hello";
|
---|
| 564 |
|
---|
| 565 | rc = ui_window_create(ui, ¶ms, &window);
|
---|
| 566 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 567 | PCUT_ASSERT_NOT_NULL(window);
|
---|
| 568 |
|
---|
| 569 | rc = ui_entry_create(window, "ABCDEF", &entry);
|
---|
| 570 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 571 |
|
---|
| 572 | ui_entry_activate(entry);
|
---|
| 573 | ui_entry_seek_start(entry, false);
|
---|
| 574 | ui_entry_seek_next_char(entry, false);
|
---|
| 575 | ui_entry_seek_end(entry, true);
|
---|
| 576 | ui_entry_seek_prev_char(entry, true);
|
---|
| 577 |
|
---|
| 578 | // FIXME: This is not safe unless we could create a private
|
---|
| 579 | // test clipboard
|
---|
| 580 |
|
---|
| 581 | ui_entry_cut(entry);
|
---|
| 582 | rc = clipboard_get_str(&str);
|
---|
| 583 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 584 | PCUT_ASSERT_STR_EQUALS("BCDE", str);
|
---|
| 585 | PCUT_ASSERT_STR_EQUALS("AF", entry->text);
|
---|
| 586 | free(str);
|
---|
| 587 |
|
---|
| 588 | ui_entry_destroy(entry);
|
---|
| 589 | ui_window_destroy(window);
|
---|
| 590 | ui_destroy(ui);
|
---|
| 591 | }
|
---|
| 592 |
|
---|
| 593 | /** ui_entry_paste() pastes text from clipboard. */
|
---|
| 594 | PCUT_TEST(paste)
|
---|
| 595 | {
|
---|
| 596 | errno_t rc;
|
---|
| 597 | ui_t *ui = NULL;
|
---|
| 598 | ui_window_t *window = NULL;
|
---|
| 599 | ui_wnd_params_t params;
|
---|
| 600 | ui_entry_t *entry;
|
---|
| 601 |
|
---|
| 602 | rc = ui_create_disp(NULL, &ui);
|
---|
| 603 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 604 |
|
---|
| 605 | ui_wnd_params_init(¶ms);
|
---|
| 606 | params.caption = "Hello";
|
---|
| 607 |
|
---|
| 608 | rc = ui_window_create(ui, ¶ms, &window);
|
---|
| 609 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 610 | PCUT_ASSERT_NOT_NULL(window);
|
---|
| 611 |
|
---|
| 612 | rc = ui_entry_create(window, "AB", &entry);
|
---|
| 613 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 614 |
|
---|
| 615 | ui_entry_activate(entry);
|
---|
| 616 | ui_entry_seek_start(entry, false);
|
---|
| 617 | ui_entry_seek_next_char(entry, false);
|
---|
| 618 |
|
---|
| 619 | // FIXME: This is not safe unless we could create a private
|
---|
| 620 | // test clipboard
|
---|
| 621 |
|
---|
| 622 | rc = clipboard_put_str("123");
|
---|
| 623 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 624 |
|
---|
| 625 | ui_entry_paste(entry);
|
---|
| 626 | PCUT_ASSERT_STR_EQUALS("A123B", entry->text);
|
---|
| 627 |
|
---|
| 628 | ui_entry_destroy(entry);
|
---|
| 629 | ui_window_destroy(window);
|
---|
| 630 | ui_destroy(ui);
|
---|
| 631 | }
|
---|
| 632 |
|
---|
[61bf9dd9] | 633 | /** ui_entry_seek_start() moves cursor to beginning of text */
|
---|
| 634 | PCUT_TEST(seek_start)
|
---|
| 635 | {
|
---|
| 636 | errno_t rc;
|
---|
| 637 | ui_t *ui = NULL;
|
---|
| 638 | ui_window_t *window = NULL;
|
---|
| 639 | ui_wnd_params_t params;
|
---|
| 640 | ui_entry_t *entry;
|
---|
| 641 |
|
---|
| 642 | rc = ui_create_disp(NULL, &ui);
|
---|
| 643 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 644 |
|
---|
| 645 | ui_wnd_params_init(¶ms);
|
---|
| 646 | params.caption = "Hello";
|
---|
| 647 |
|
---|
| 648 | rc = ui_window_create(ui, ¶ms, &window);
|
---|
| 649 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 650 | PCUT_ASSERT_NOT_NULL(window);
|
---|
| 651 |
|
---|
[c9722c1] | 652 | rc = ui_entry_create(window, "ABCDEF", &entry);
|
---|
[61bf9dd9] | 653 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 654 |
|
---|
[c9722c1] | 655 | ui_entry_activate(entry);
|
---|
| 656 |
|
---|
[61bf9dd9] | 657 | entry->pos = 2;
|
---|
[9eb8d12] | 658 | entry->sel_start = 2;
|
---|
[61bf9dd9] | 659 |
|
---|
[9eb8d12] | 660 | ui_entry_seek_start(entry, true);
|
---|
[61bf9dd9] | 661 | PCUT_ASSERT_INT_EQUALS(0, entry->pos);
|
---|
[9eb8d12] | 662 | PCUT_ASSERT_INT_EQUALS(2, entry->sel_start);
|
---|
| 663 |
|
---|
| 664 | ui_entry_seek_start(entry, false);
|
---|
| 665 | PCUT_ASSERT_INT_EQUALS(0, entry->pos);
|
---|
| 666 | PCUT_ASSERT_INT_EQUALS(0, entry->sel_start);
|
---|
[61bf9dd9] | 667 |
|
---|
| 668 | ui_entry_destroy(entry);
|
---|
| 669 | ui_window_destroy(window);
|
---|
| 670 | ui_destroy(ui);
|
---|
| 671 | }
|
---|
| 672 |
|
---|
| 673 | /** ui_entry_seek_end() moves cursor to the end of text */
|
---|
| 674 | PCUT_TEST(seek_end)
|
---|
| 675 | {
|
---|
| 676 | errno_t rc;
|
---|
| 677 | ui_t *ui = NULL;
|
---|
| 678 | ui_window_t *window = NULL;
|
---|
| 679 | ui_wnd_params_t params;
|
---|
| 680 | ui_entry_t *entry;
|
---|
| 681 |
|
---|
| 682 | rc = ui_create_disp(NULL, &ui);
|
---|
| 683 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 684 |
|
---|
| 685 | ui_wnd_params_init(¶ms);
|
---|
| 686 | params.caption = "Hello";
|
---|
| 687 |
|
---|
| 688 | rc = ui_window_create(ui, ¶ms, &window);
|
---|
| 689 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 690 | PCUT_ASSERT_NOT_NULL(window);
|
---|
| 691 |
|
---|
| 692 | rc = ui_entry_create(window, "ABCD", &entry);
|
---|
| 693 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 694 |
|
---|
| 695 | PCUT_ASSERT_STR_EQUALS("ABCD", entry->text);
|
---|
| 696 | entry->pos = 2;
|
---|
[9eb8d12] | 697 | entry->sel_start = 2;
|
---|
[61bf9dd9] | 698 |
|
---|
[9eb8d12] | 699 | ui_entry_seek_end(entry, true);
|
---|
[61bf9dd9] | 700 | PCUT_ASSERT_INT_EQUALS(4, entry->pos);
|
---|
[9eb8d12] | 701 | PCUT_ASSERT_INT_EQUALS(2, entry->sel_start);
|
---|
| 702 | ui_entry_seek_end(entry, false);
|
---|
| 703 | PCUT_ASSERT_INT_EQUALS(4, entry->pos);
|
---|
| 704 | PCUT_ASSERT_INT_EQUALS(4, entry->sel_start);
|
---|
[61bf9dd9] | 705 |
|
---|
| 706 | ui_entry_destroy(entry);
|
---|
| 707 | ui_window_destroy(window);
|
---|
| 708 | ui_destroy(ui);
|
---|
| 709 | }
|
---|
| 710 |
|
---|
| 711 | /** ui_entry_seek_prev_char() moves cursor to the previous character */
|
---|
| 712 | PCUT_TEST(seek_prev_char)
|
---|
| 713 | {
|
---|
| 714 | errno_t rc;
|
---|
| 715 | ui_t *ui = NULL;
|
---|
| 716 | ui_window_t *window = NULL;
|
---|
| 717 | ui_wnd_params_t params;
|
---|
| 718 | ui_entry_t *entry;
|
---|
| 719 |
|
---|
| 720 | rc = ui_create_disp(NULL, &ui);
|
---|
| 721 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 722 |
|
---|
| 723 | ui_wnd_params_init(¶ms);
|
---|
| 724 | params.caption = "Hello";
|
---|
| 725 |
|
---|
| 726 | rc = ui_window_create(ui, ¶ms, &window);
|
---|
| 727 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 728 | PCUT_ASSERT_NOT_NULL(window);
|
---|
| 729 |
|
---|
| 730 | rc = ui_entry_create(window, "ABCD", &entry);
|
---|
| 731 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 732 |
|
---|
| 733 | PCUT_ASSERT_STR_EQUALS("ABCD", entry->text);
|
---|
[9eb8d12] | 734 | entry->pos = 3;
|
---|
| 735 | entry->sel_start = 3;
|
---|
| 736 |
|
---|
| 737 | ui_entry_seek_prev_char(entry, true);
|
---|
| 738 | PCUT_ASSERT_INT_EQUALS(2, entry->pos);
|
---|
| 739 | PCUT_ASSERT_INT_EQUALS(3, entry->sel_start);
|
---|
[61bf9dd9] | 740 |
|
---|
[9eb8d12] | 741 | ui_entry_seek_prev_char(entry, false);
|
---|
[61bf9dd9] | 742 | PCUT_ASSERT_INT_EQUALS(1, entry->pos);
|
---|
[9eb8d12] | 743 | PCUT_ASSERT_INT_EQUALS(1, entry->sel_start);
|
---|
[61bf9dd9] | 744 |
|
---|
| 745 | ui_entry_destroy(entry);
|
---|
| 746 | ui_window_destroy(window);
|
---|
| 747 | ui_destroy(ui);
|
---|
| 748 | }
|
---|
| 749 |
|
---|
| 750 | /** ui_entry_seek_prev_char() moves cursor to the next character */
|
---|
| 751 | PCUT_TEST(seek_next_char)
|
---|
| 752 | {
|
---|
| 753 | errno_t rc;
|
---|
| 754 | ui_t *ui = NULL;
|
---|
| 755 | ui_window_t *window = NULL;
|
---|
| 756 | ui_wnd_params_t params;
|
---|
| 757 | ui_entry_t *entry;
|
---|
| 758 |
|
---|
| 759 | rc = ui_create_disp(NULL, &ui);
|
---|
| 760 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 761 |
|
---|
| 762 | ui_wnd_params_init(¶ms);
|
---|
| 763 | params.caption = "Hello";
|
---|
| 764 |
|
---|
| 765 | rc = ui_window_create(ui, ¶ms, &window);
|
---|
| 766 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 767 | PCUT_ASSERT_NOT_NULL(window);
|
---|
| 768 |
|
---|
| 769 | rc = ui_entry_create(window, "ABCD", &entry);
|
---|
| 770 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 771 |
|
---|
| 772 | PCUT_ASSERT_STR_EQUALS("ABCD", entry->text);
|
---|
[9eb8d12] | 773 | entry->pos = 1;
|
---|
| 774 | entry->sel_start = 1;
|
---|
[61bf9dd9] | 775 |
|
---|
[9eb8d12] | 776 | ui_entry_seek_next_char(entry, true);
|
---|
| 777 | PCUT_ASSERT_INT_EQUALS(2, entry->pos);
|
---|
| 778 | PCUT_ASSERT_INT_EQUALS(1, entry->sel_start);
|
---|
| 779 | ui_entry_seek_next_char(entry, false);
|
---|
[61bf9dd9] | 780 | PCUT_ASSERT_INT_EQUALS(3, entry->pos);
|
---|
[9eb8d12] | 781 | PCUT_ASSERT_INT_EQUALS(3, entry->sel_start);
|
---|
[7481ee19] | 782 |
|
---|
| 783 | ui_entry_destroy(entry);
|
---|
| 784 | ui_window_destroy(window);
|
---|
| 785 | ui_destroy(ui);
|
---|
| 786 | }
|
---|
| 787 |
|
---|
[bb14312] | 788 | /** ui_entry_activate() / ui_entry_deactivate() */
|
---|
| 789 | PCUT_TEST(activate_deactivate)
|
---|
| 790 | {
|
---|
| 791 | errno_t rc;
|
---|
| 792 | ui_t *ui = NULL;
|
---|
| 793 | ui_window_t *window = NULL;
|
---|
| 794 | ui_wnd_params_t params;
|
---|
| 795 | ui_entry_t *entry;
|
---|
| 796 |
|
---|
| 797 | rc = ui_create_disp(NULL, &ui);
|
---|
| 798 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 799 |
|
---|
| 800 | ui_wnd_params_init(¶ms);
|
---|
| 801 | params.caption = "Hello";
|
---|
| 802 |
|
---|
| 803 | rc = ui_window_create(ui, ¶ms, &window);
|
---|
| 804 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 805 | PCUT_ASSERT_NOT_NULL(window);
|
---|
| 806 |
|
---|
| 807 | rc = ui_entry_create(window, "ABC", &entry);
|
---|
| 808 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 809 |
|
---|
| 810 | PCUT_ASSERT_FALSE(entry->active);
|
---|
| 811 |
|
---|
| 812 | ui_entry_activate(entry);
|
---|
| 813 | PCUT_ASSERT_TRUE(entry->active);
|
---|
| 814 |
|
---|
| 815 | ui_entry_deactivate(entry);
|
---|
| 816 | PCUT_ASSERT_FALSE(entry->active);
|
---|
| 817 |
|
---|
| 818 | ui_entry_destroy(entry);
|
---|
| 819 | ui_window_destroy(window);
|
---|
| 820 | ui_destroy(ui);
|
---|
| 821 | }
|
---|
| 822 |
|
---|
[03145ee] | 823 | PCUT_EXPORT(entry);
|
---|