Ignore:
File:
1 edited

Legend:

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

    r1eaead4 r7481ee19  
    11/*
    2  * Copyright (c) 2023 Jiri Svoboda
     2 * Copyright (c) 2021 Jiri Svoboda
    33 * All rights reserved.
    44 *
     
    3333#include <pcut/pcut.h>
    3434#include <ui/control.h>
    35 #include <ui/testctl.h>
    3635#include <stdbool.h>
    3736#include <types/ui/event.h>
    38 #include "../private/testctl.h"
    3937
    4038PCUT_INIT;
    4139
    4240PCUT_TEST_SUITE(control);
     41
     42static void test_ctl_destroy(void *);
     43static errno_t test_ctl_paint(void *);
     44static ui_evclaim_t test_ctl_kbd_event(void *, kbd_event_t *);
     45static ui_evclaim_t test_ctl_pos_event(void *, pos_event_t *);
     46static void test_ctl_unfocus(void *);
     47
     48static ui_control_ops_t test_ctl_ops = {
     49        .destroy = test_ctl_destroy,
     50        .paint = test_ctl_paint,
     51        .kbd_event = test_ctl_kbd_event,
     52        .pos_event = test_ctl_pos_event,
     53        .unfocus = test_ctl_unfocus
     54};
     55
     56/** Test response */
     57typedef struct {
     58        /** Claim to return */
     59        ui_evclaim_t claim;
     60        /** Result code to return */
     61        errno_t rc;
     62
     63        /** @c true iff destroy was called */
     64        bool destroy;
     65
     66        /** @c true iff paint was called */
     67        bool paint;
     68
     69        /** @c true iff kbd_event was called */
     70        bool kbd;
     71        /** Keyboard event that was sent */
     72        kbd_event_t kevent;
     73
     74        /** @c true iff pos_event was called */
     75        bool pos;
     76        /** Position event that was sent */
     77        pos_event_t pevent;
     78
     79        /** @c true iff unfocus was called */
     80        bool unfocus;
     81} test_resp_t;
    4382
    4483/** Allocate and deallocate control */
     
    4887        errno_t rc;
    4988
    50         rc = ui_control_new(&ui_test_ctl_ops, NULL, &control);
     89        rc = ui_control_new(&test_ctl_ops, NULL, &control);
    5190        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    5291        PCUT_ASSERT_NOT_NULL(control);
     
    64103PCUT_TEST(destroy)
    65104{
    66         ui_test_ctl_t *testctl = NULL;
    67         ui_tc_resp_t resp;
    68         errno_t rc;
    69 
    70         rc = ui_test_ctl_create(&resp, &testctl);
    71         PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    72         PCUT_ASSERT_NOT_NULL(testctl);
     105        ui_control_t *control = NULL;
     106        test_resp_t resp;
     107        errno_t rc;
     108
     109        rc = ui_control_new(&test_ctl_ops, &resp, &control);
     110        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     111        PCUT_ASSERT_NOT_NULL(control);
    73112
    74113        resp.rc = EOK;
    75114        resp.destroy = false;
    76115
    77         ui_control_destroy(ui_test_ctl_ctl(testctl));
     116        ui_control_destroy(control);
    78117        PCUT_ASSERT_TRUE(resp.destroy);
    79118}
     
    82121PCUT_TEST(paint)
    83122{
    84         ui_test_ctl_t *testctl = NULL;
    85         ui_tc_resp_t resp;
    86         errno_t rc;
    87 
    88         rc = ui_test_ctl_create(&resp, &testctl);
    89         PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    90         PCUT_ASSERT_NOT_NULL(testctl);
     123        ui_control_t *control = NULL;
     124        test_resp_t resp;
     125        errno_t rc;
     126
     127        rc = ui_control_new(&test_ctl_ops, &resp, &control);
     128        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     129        PCUT_ASSERT_NOT_NULL(control);
    91130
    92131        resp.rc = EOK;
    93132        resp.paint = false;
    94133
    95         rc = ui_control_paint(ui_test_ctl_ctl(testctl));
     134        rc = ui_control_paint(control);
    96135        PCUT_ASSERT_ERRNO_VAL(resp.rc, rc);
    97136        PCUT_ASSERT_TRUE(resp.paint);
     
    100139        resp.paint = false;
    101140
    102         rc = ui_control_paint(ui_test_ctl_ctl(testctl));
     141        rc = ui_control_paint(control);
    103142        PCUT_ASSERT_ERRNO_VAL(resp.rc, rc);
    104143        PCUT_ASSERT_TRUE(resp.paint);
    105144
    106         ui_test_ctl_destroy(testctl);
     145        ui_control_delete(control);
    107146}
    108147
     
    110149PCUT_TEST(kbd_event)
    111150{
    112         ui_test_ctl_t *testctl = NULL;
    113         ui_tc_resp_t resp;
     151        ui_control_t *control = NULL;
     152        test_resp_t resp;
    114153        kbd_event_t event;
    115154        ui_evclaim_t claim;
    116155        errno_t rc;
    117156
    118         rc = ui_test_ctl_create(&resp, &testctl);
    119         PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    120         PCUT_ASSERT_NOT_NULL(testctl);
     157        rc = ui_control_new(&test_ctl_ops, &resp, &control);
     158        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     159        PCUT_ASSERT_NOT_NULL(control);
    121160
    122161        resp.claim = ui_claimed;
     
    127166        event.c = '@';
    128167
    129         claim = ui_control_kbd_event(ui_test_ctl_ctl(testctl), &event);
     168        claim = ui_control_kbd_event(control, &event);
    130169        PCUT_ASSERT_EQUALS(resp.claim, claim);
    131170        PCUT_ASSERT_TRUE(resp.kbd);
     
    135174        PCUT_ASSERT_INT_EQUALS(resp.kevent.c, event.c);
    136175
    137         ui_test_ctl_destroy(testctl);
     176        ui_control_delete(control);
    138177}
    139178
     
    141180PCUT_TEST(pos_event)
    142181{
    143         ui_test_ctl_t *testctl = NULL;
    144         ui_tc_resp_t resp;
     182        ui_control_t *control = NULL;
     183        test_resp_t resp;
    145184        pos_event_t event;
    146185        ui_evclaim_t claim;
    147186        errno_t rc;
    148187
    149         rc = ui_test_ctl_create(&resp, &testctl);
    150         PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    151         PCUT_ASSERT_NOT_NULL(testctl);
     188        rc = ui_control_new(&test_ctl_ops, &resp, &control);
     189        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     190        PCUT_ASSERT_NOT_NULL(control);
    152191
    153192        resp.claim = ui_claimed;
     
    159198        event.vpos = 4;
    160199
    161         claim = ui_control_pos_event(ui_test_ctl_ctl(testctl), &event);
     200        claim = ui_control_pos_event(control, &event);
    162201        PCUT_ASSERT_EQUALS(resp.claim, claim);
    163202        PCUT_ASSERT_TRUE(resp.pos);
     
    168207        PCUT_ASSERT_INT_EQUALS(resp.pevent.vpos, event.vpos);
    169208
    170         ui_test_ctl_destroy(testctl);
     209        ui_control_delete(control);
    171210}
    172211
     
    174213PCUT_TEST(unfocus)
    175214{
    176         ui_test_ctl_t *testctl = NULL;
    177         ui_tc_resp_t resp;
    178         errno_t rc;
    179 
    180         rc = ui_test_ctl_create(&resp, &testctl);
    181         PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    182         PCUT_ASSERT_NOT_NULL(testctl);
     215        ui_control_t *control = NULL;
     216        test_resp_t resp;
     217        errno_t rc;
     218
     219        rc = ui_control_new(&test_ctl_ops, &resp, &control);
     220        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     221        PCUT_ASSERT_NOT_NULL(control);
    183222
    184223        resp.unfocus = false;
    185224
    186         ui_control_unfocus(ui_test_ctl_ctl(testctl), 42);
     225        ui_control_unfocus(control);
    187226        PCUT_ASSERT_TRUE(resp.unfocus);
    188         PCUT_ASSERT_INT_EQUALS(42, resp.unfocus_nfocus);
    189 
    190         ui_test_ctl_destroy(testctl);
     227
     228        ui_control_delete(control);
     229}
     230
     231static void test_ctl_destroy(void *arg)
     232{
     233        test_resp_t *resp = (test_resp_t *) arg;
     234
     235        resp->destroy = true;
     236}
     237
     238static errno_t test_ctl_paint(void *arg)
     239{
     240        test_resp_t *resp = (test_resp_t *) arg;
     241
     242        resp->paint = true;
     243        return resp->rc;
     244}
     245
     246static ui_evclaim_t test_ctl_kbd_event(void *arg, kbd_event_t *event)
     247{
     248        test_resp_t *resp = (test_resp_t *) arg;
     249
     250        resp->kbd = true;
     251        resp->kevent = *event;
     252
     253        return resp->claim;
     254}
     255
     256static ui_evclaim_t test_ctl_pos_event(void *arg, pos_event_t *event)
     257{
     258        test_resp_t *resp = (test_resp_t *) arg;
     259
     260        resp->pos = true;
     261        resp->pevent = *event;
     262
     263        return resp->claim;
     264}
     265
     266static void test_ctl_unfocus(void *arg)
     267{
     268        test_resp_t *resp = (test_resp_t *) arg;
     269
     270        resp->unfocus = true;
    191271}
    192272
Note: See TracChangeset for help on using the changeset viewer.