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