Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/ui/test/entry.c

    r03145ee rf0155e4  
    11/*
    2  * Copyright (c) 2020 Jiri Svoboda
     2 * Copyright (c) 2021 Jiri Svoboda
    33 * All rights reserved.
    44 *
     
    2727 */
    2828
     29#include <clipboard.h>
    2930#include <gfx/context.h>
    3031#include <gfx/coord.h>
     
    3536#include <ui/entry.h>
    3637#include <ui/resource.h>
     38#include <ui/ui.h>
     39#include <ui/window.h>
    3740#include "../private/entry.h"
    3841
     
    4043
    4144PCUT_TEST_SUITE(entry);
    42 
    43 static errno_t testgc_set_color(void *, gfx_color_t *);
    44 static errno_t testgc_fill_rect(void *, gfx_rect_t *);
    45 static errno_t testgc_bitmap_create(void *, gfx_bitmap_params_t *,
    46     gfx_bitmap_alloc_t *, void **);
    47 static errno_t testgc_bitmap_destroy(void *);
    48 static errno_t testgc_bitmap_render(void *, gfx_rect_t *, gfx_coord2_t *);
    49 static errno_t testgc_bitmap_get_alloc(void *, gfx_bitmap_alloc_t *);
    50 
    51 static gfx_context_ops_t ops = {
    52         .set_color = testgc_set_color,
    53         .fill_rect = testgc_fill_rect,
    54         .bitmap_create = testgc_bitmap_create,
    55         .bitmap_destroy = testgc_bitmap_destroy,
    56         .bitmap_render = testgc_bitmap_render,
    57         .bitmap_get_alloc = testgc_bitmap_get_alloc
    58 };
    59 
    60 typedef struct {
    61         bool bm_created;
    62         bool bm_destroyed;
    63         gfx_bitmap_params_t bm_params;
    64         void *bm_pixels;
    65         gfx_rect_t bm_srect;
    66         gfx_coord2_t bm_offs;
    67         bool bm_rendered;
    68         bool bm_got_alloc;
    69 } test_gc_t;
    70 
    71 typedef struct {
    72         test_gc_t *tgc;
    73         gfx_bitmap_alloc_t alloc;
    74         bool myalloc;
    75 } testgc_bitmap_t;
    76 
    77 typedef struct {
    78         bool clicked;
    79 } test_cb_resp_t;
    8045
    8146/** Create and destroy text entry */
     
    141106PCUT_TEST(set_halign)
    142107{
    143         ui_entry_t *entry;
    144         errno_t rc;
    145 
    146         rc = ui_entry_create(NULL, "Hello", &entry);
     108        errno_t rc;
     109        ui_t *ui = NULL;
     110        ui_window_t *window = NULL;
     111        ui_wnd_params_t params;
     112        ui_entry_t *entry;
     113
     114        rc = ui_create_disp(NULL, &ui);
     115        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     116
     117        ui_wnd_params_init(&params);
     118        params.caption = "Hello";
     119
     120        rc = ui_window_create(ui, &params, &window);
     121        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     122        PCUT_ASSERT_NOT_NULL(window);
     123
     124        rc = ui_entry_create(window, "Hello", &entry);
    147125        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    148126
     
    151129        ui_entry_set_halign(entry, gfx_halign_center);
    152130        PCUT_ASSERT_EQUALS(gfx_halign_center, entry->halign);
     131
     132        ui_entry_destroy(entry);
     133        ui_window_destroy(window);
     134        ui_destroy(ui);
     135}
     136
     137/** Set entry read only flag sets internal field */
     138PCUT_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);
    153150
    154151        ui_entry_destroy(entry);
     
    183180{
    184181        errno_t rc;
    185         gfx_context_t *gc = NULL;
    186         test_gc_t tgc;
    187         ui_resource_t *resource = NULL;
    188         ui_entry_t *entry;
    189 
    190         memset(&tgc, 0, sizeof(tgc));
    191         rc = gfx_context_new(&ops, &tgc, &gc);
    192         PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    193 
    194         rc = ui_resource_create(gc, &resource);
    195         PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    196         PCUT_ASSERT_NOT_NULL(resource);
    197 
    198         rc = ui_entry_create(resource, "Hello", &entry);
     182        ui_t *ui = NULL;
     183        ui_window_t *window = NULL;
     184        ui_wnd_params_t params;
     185        ui_entry_t *entry;
     186
     187        rc = ui_create_disp(NULL, &ui);
     188        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     189
     190        ui_wnd_params_init(&params);
     191        params.caption = "Hello";
     192
     193        rc = ui_window_create(ui, &params, &window);
     194        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     195        PCUT_ASSERT_NOT_NULL(window);
     196
     197        rc = ui_entry_create(window, "Hello", &entry);
    199198        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    200199
     
    203202
    204203        ui_entry_destroy(entry);
    205         ui_resource_destroy(resource);
    206 
    207         rc = gfx_context_delete(gc);
    208         PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    209 }
    210 
    211 static errno_t testgc_set_color(void *arg, gfx_color_t *color)
    212 {
    213         (void) arg;
    214         (void) color;
    215         return EOK;
    216 }
    217 
    218 static errno_t testgc_fill_rect(void *arg, gfx_rect_t *rect)
    219 {
    220         (void) arg;
    221         (void) rect;
    222         return EOK;
    223 }
    224 
    225 static errno_t testgc_bitmap_create(void *arg, gfx_bitmap_params_t *params,
    226     gfx_bitmap_alloc_t *alloc, void **rbm)
    227 {
    228         test_gc_t *tgc = (test_gc_t *) arg;
    229         testgc_bitmap_t *tbm;
    230 
    231         tbm = calloc(1, sizeof(testgc_bitmap_t));
    232         if (tbm == NULL)
    233                 return ENOMEM;
    234 
    235         if (alloc == NULL) {
    236                 tbm->alloc.pitch = (params->rect.p1.x - params->rect.p0.x) *
    237                     sizeof(uint32_t);
    238                 tbm->alloc.off0 = 0;
    239                 tbm->alloc.pixels = calloc(sizeof(uint32_t),
    240                     (params->rect.p1.x - params->rect.p0.x) *
    241                     (params->rect.p1.y - params->rect.p0.y));
    242                 tbm->myalloc = true;
    243                 if (tbm->alloc.pixels == NULL) {
    244                         free(tbm);
    245                         return ENOMEM;
    246                 }
    247         } else {
    248                 tbm->alloc = *alloc;
    249         }
    250 
    251         tbm->tgc = tgc;
    252         tgc->bm_created = true;
    253         tgc->bm_params = *params;
    254         tgc->bm_pixels = tbm->alloc.pixels;
    255         *rbm = (void *)tbm;
    256         return EOK;
    257 }
    258 
    259 static errno_t testgc_bitmap_destroy(void *bm)
    260 {
    261         testgc_bitmap_t *tbm = (testgc_bitmap_t *)bm;
    262         if (tbm->myalloc)
    263                 free(tbm->alloc.pixels);
    264         tbm->tgc->bm_destroyed = true;
    265         free(tbm);
    266         return EOK;
    267 }
    268 
    269 static errno_t testgc_bitmap_render(void *bm, gfx_rect_t *srect,
    270     gfx_coord2_t *offs)
    271 {
    272         testgc_bitmap_t *tbm = (testgc_bitmap_t *)bm;
    273         tbm->tgc->bm_rendered = true;
    274         tbm->tgc->bm_srect = *srect;
    275         tbm->tgc->bm_offs = *offs;
    276         return EOK;
    277 }
    278 
    279 static errno_t testgc_bitmap_get_alloc(void *bm, gfx_bitmap_alloc_t *alloc)
    280 {
    281         testgc_bitmap_t *tbm = (testgc_bitmap_t *)bm;
    282         *alloc = tbm->alloc;
    283         tbm->tgc->bm_got_alloc = true;
    284         return EOK;
     204        ui_window_destroy(window);
     205        ui_destroy(ui);
     206}
     207
     208/** ui_entry_delete_sel() deletes selected text */
     209PCUT_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(&params);
     221        params.caption = "Hello";
     222
     223        rc = ui_window_create(ui, &params, &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
     249/** ui_entry_insert_str() inserts string at cursor. */
     250PCUT_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(&params);
     262        params.caption = "Hello";
     263
     264        rc = ui_window_create(ui, &params, &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
     273        ui_entry_activate(entry);
     274        ui_entry_seek_end(entry, false);
     275
     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
     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;
     287        entry->sel_start = 2;
     288        rc = ui_entry_insert_str(entry, "CD");
     289        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     290
     291        PCUT_ASSERT_STR_EQUALS("ABCDEF", entry->text);
     292
     293        ui_entry_destroy(entry);
     294        ui_window_destroy(window);
     295        ui_destroy(ui);
     296}
     297
     298/** ui_entry_insert_str() deletes selection before inserting string */
     299PCUT_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(&params);
     311        params.caption = "Hello";
     312
     313        rc = ui_window_create(ui, &params, &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
     339/** ui_entry_backspace() deletes character before cursor. */
     340PCUT_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(&params);
     352        params.caption = "Hello";
     353
     354        rc = ui_window_create(ui, &params, &window);
     355        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     356        PCUT_ASSERT_NOT_NULL(window);
     357
     358        rc = ui_entry_create(window, "ABCD", &entry);
     359        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     360
     361        PCUT_ASSERT_STR_EQUALS("ABCD", entry->text);
     362        entry->pos = 3;
     363        entry->sel_start = 3;
     364
     365        ui_entry_backspace(entry);
     366        PCUT_ASSERT_STR_EQUALS("ABD", entry->text);
     367
     368        ui_entry_backspace(entry);
     369        PCUT_ASSERT_STR_EQUALS("AD", entry->text);
     370
     371        ui_entry_backspace(entry);
     372        PCUT_ASSERT_STR_EQUALS("D", entry->text);
     373
     374        ui_entry_backspace(entry);
     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
     382/** ui_entry_backspace() with selected text deletes selection. */
     383PCUT_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(&params);
     395        params.caption = "Hello";
     396
     397        rc = ui_window_create(ui, &params, &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
     422/** ui_entry_delete() deletes character after cursor. */
     423PCUT_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(&params);
     435        params.caption = "Hello";
     436
     437        rc = ui_window_create(ui, &params, &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;
     446        entry->sel_start = 1;
     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
     465/** ui_entry_delete() with selected text deletes selection. */
     466PCUT_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(&params);
     478        params.caption = "Hello";
     479
     480        rc = ui_window_create(ui, &params, &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
     505/** ui_entry_copy() copies selected text to clipboard. */
     506PCUT_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(&params);
     519        params.caption = "Hello";
     520
     521        rc = ui_window_create(ui, &params, &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. */
     550PCUT_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(&params);
     563        params.caption = "Hello";
     564
     565        rc = ui_window_create(ui, &params, &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. */
     594PCUT_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(&params);
     606        params.caption = "Hello";
     607
     608        rc = ui_window_create(ui, &params, &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
     633/** ui_entry_seek_start() moves cursor to beginning of text */
     634PCUT_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(&params);
     646        params.caption = "Hello";
     647
     648        rc = ui_window_create(ui, &params, &window);
     649        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     650        PCUT_ASSERT_NOT_NULL(window);
     651
     652        rc = ui_entry_create(window, "ABCDEF", &entry);
     653        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     654
     655        ui_entry_activate(entry);
     656
     657        entry->pos = 2;
     658        entry->sel_start = 2;
     659
     660        ui_entry_seek_start(entry, true);
     661        PCUT_ASSERT_INT_EQUALS(0, entry->pos);
     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);
     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 */
     674PCUT_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(&params);
     686        params.caption = "Hello";
     687
     688        rc = ui_window_create(ui, &params, &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;
     697        entry->sel_start = 2;
     698
     699        ui_entry_seek_end(entry, true);
     700        PCUT_ASSERT_INT_EQUALS(4, entry->pos);
     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);
     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 */
     712PCUT_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(&params);
     724        params.caption = "Hello";
     725
     726        rc = ui_window_create(ui, &params, &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);
     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);
     740
     741        ui_entry_seek_prev_char(entry, false);
     742        PCUT_ASSERT_INT_EQUALS(1, entry->pos);
     743        PCUT_ASSERT_INT_EQUALS(1, entry->sel_start);
     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 */
     751PCUT_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(&params);
     763        params.caption = "Hello";
     764
     765        rc = ui_window_create(ui, &params, &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);
     773        entry->pos = 1;
     774        entry->sel_start = 1;
     775
     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);
     780        PCUT_ASSERT_INT_EQUALS(3, entry->pos);
     781        PCUT_ASSERT_INT_EQUALS(3, entry->sel_start);
     782
     783        ui_entry_destroy(entry);
     784        ui_window_destroy(window);
     785        ui_destroy(ui);
     786}
     787
     788/** ui_entry_activate() / ui_entry_deactivate() */
     789PCUT_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(&params);
     801        params.caption = "Hello";
     802
     803        rc = ui_window_create(ui, &params, &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);
    285821}
    286822
Note: See TracChangeset for help on using the changeset viewer.