Ignore:
File:
1 edited

Legend:

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

    r1eaead4 rc6f00b40  
    11/*
    2  * Copyright (c) 2023 Jiri Svoboda
     2 * Copyright (c) 2020 Jiri Svoboda
    33 * All rights reserved.
    44 *
     
    2929#include <errno.h>
    3030#include <mem.h>
    31 #include <io/kbd_event.h>
    3231#include <io/pos_event.h>
    3332#include <pcut/pcut.h>
    3433#include <ui/control.h>
    35 #include <ui/testctl.h>
    3634#include <stdbool.h>
    3735#include <types/ui/event.h>
    38 #include "../private/testctl.h"
    3936
    4037PCUT_INIT;
    4138
    4239PCUT_TEST_SUITE(control);
     40
     41static void test_ctl_destroy(void *);
     42static errno_t test_ctl_paint(void *);
     43static ui_evclaim_t test_ctl_pos_event(void *, pos_event_t *);
     44
     45static ui_control_ops_t test_ctl_ops = {
     46        .destroy = test_ctl_destroy,
     47        .paint = test_ctl_paint,
     48        .pos_event = test_ctl_pos_event
     49};
     50
     51/** Test response */
     52typedef struct {
     53        /** Claim to return */
     54        ui_evclaim_t claim;
     55        /** Result code to return */
     56        errno_t rc;
     57
     58        /** @c true iff destroy was called */
     59        bool destroy;
     60
     61        /** @c true iff paint was called */
     62        bool paint;
     63
     64        /** @c true iff pos_event was called */
     65        bool pos;
     66        /** Position event that was sent */
     67        pos_event_t pevent;
     68} test_resp_t;
    4369
    4470/** Allocate and deallocate control */
     
    4874        errno_t rc;
    4975
    50         rc = ui_control_new(&ui_test_ctl_ops, NULL, &control);
     76        rc = ui_control_new(&test_ctl_ops, NULL, &control);
    5177        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    5278        PCUT_ASSERT_NOT_NULL(control);
     
    6490PCUT_TEST(destroy)
    6591{
    66         ui_test_ctl_t *testctl = NULL;
    67         ui_tc_resp_t resp;
     92        ui_control_t *control = NULL;
     93        test_resp_t resp;
    6894        errno_t rc;
    6995
    70         rc = ui_test_ctl_create(&resp, &testctl);
     96        rc = ui_control_new(&test_ctl_ops, &resp, &control);
    7197        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    72         PCUT_ASSERT_NOT_NULL(testctl);
     98        PCUT_ASSERT_NOT_NULL(control);
    7399
    74100        resp.rc = EOK;
    75101        resp.destroy = false;
    76102
    77         ui_control_destroy(ui_test_ctl_ctl(testctl));
     103        ui_control_destroy(control);
    78104        PCUT_ASSERT_TRUE(resp.destroy);
    79105}
     
    82108PCUT_TEST(paint)
    83109{
    84         ui_test_ctl_t *testctl = NULL;
    85         ui_tc_resp_t resp;
     110        ui_control_t *control = NULL;
     111        test_resp_t resp;
    86112        errno_t rc;
    87113
    88         rc = ui_test_ctl_create(&resp, &testctl);
     114        rc = ui_control_new(&test_ctl_ops, &resp, &control);
    89115        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    90         PCUT_ASSERT_NOT_NULL(testctl);
     116        PCUT_ASSERT_NOT_NULL(control);
    91117
    92118        resp.rc = EOK;
    93119        resp.paint = false;
    94120
    95         rc = ui_control_paint(ui_test_ctl_ctl(testctl));
     121        rc = ui_control_paint(control);
    96122        PCUT_ASSERT_ERRNO_VAL(resp.rc, rc);
    97123        PCUT_ASSERT_TRUE(resp.paint);
     
    100126        resp.paint = false;
    101127
    102         rc = ui_control_paint(ui_test_ctl_ctl(testctl));
     128        rc = ui_control_paint(control);
    103129        PCUT_ASSERT_ERRNO_VAL(resp.rc, rc);
    104130        PCUT_ASSERT_TRUE(resp.paint);
    105131
    106         ui_test_ctl_destroy(testctl);
    107 }
    108 
    109 /** Test sending keyboard event to control */
    110 PCUT_TEST(kbd_event)
    111 {
    112         ui_test_ctl_t *testctl = NULL;
    113         ui_tc_resp_t resp;
    114         kbd_event_t event;
    115         ui_evclaim_t claim;
    116         errno_t rc;
    117 
    118         rc = ui_test_ctl_create(&resp, &testctl);
    119         PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    120         PCUT_ASSERT_NOT_NULL(testctl);
    121 
    122         resp.claim = ui_claimed;
    123         resp.kbd = false;
    124         event.type = KEY_PRESS;
    125         event.key = KC_2;
    126         event.mods = KM_LSHIFT;
    127         event.c = '@';
    128 
    129         claim = ui_control_kbd_event(ui_test_ctl_ctl(testctl), &event);
    130         PCUT_ASSERT_EQUALS(resp.claim, claim);
    131         PCUT_ASSERT_TRUE(resp.kbd);
    132         PCUT_ASSERT_EQUALS(resp.kevent.type, event.type);
    133         PCUT_ASSERT_INT_EQUALS(resp.kevent.key, event.key);
    134         PCUT_ASSERT_INT_EQUALS(resp.kevent.mods, event.mods);
    135         PCUT_ASSERT_INT_EQUALS(resp.kevent.c, event.c);
    136 
    137         ui_test_ctl_destroy(testctl);
     132        ui_control_delete(control);
    138133}
    139134
     
    141136PCUT_TEST(pos_event)
    142137{
    143         ui_test_ctl_t *testctl = NULL;
    144         ui_tc_resp_t resp;
     138        ui_control_t *control = NULL;
     139        test_resp_t resp;
    145140        pos_event_t event;
    146141        ui_evclaim_t claim;
    147142        errno_t rc;
    148143
    149         rc = ui_test_ctl_create(&resp, &testctl);
     144        rc = ui_control_new(&test_ctl_ops, &resp, &control);
    150145        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    151         PCUT_ASSERT_NOT_NULL(testctl);
     146        PCUT_ASSERT_NOT_NULL(control);
    152147
    153148        resp.claim = ui_claimed;
     
    159154        event.vpos = 4;
    160155
    161         claim = ui_control_pos_event(ui_test_ctl_ctl(testctl), &event);
     156        claim = ui_control_pos_event(control, &event);
    162157        PCUT_ASSERT_EQUALS(resp.claim, claim);
    163158        PCUT_ASSERT_TRUE(resp.pos);
     
    168163        PCUT_ASSERT_INT_EQUALS(resp.pevent.vpos, event.vpos);
    169164
    170         ui_test_ctl_destroy(testctl);
     165        ui_control_delete(control);
    171166}
    172167
    173 /** Test sending unfocus to control */
    174 PCUT_TEST(unfocus)
     168static void test_ctl_destroy(void *arg)
    175169{
    176         ui_test_ctl_t *testctl = NULL;
    177         ui_tc_resp_t resp;
    178         errno_t rc;
     170        test_resp_t *resp = (test_resp_t *) arg;
    179171
    180         rc = ui_test_ctl_create(&resp, &testctl);
    181         PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    182         PCUT_ASSERT_NOT_NULL(testctl);
     172        resp->destroy = true;
     173}
    183174
    184         resp.unfocus = false;
     175static errno_t test_ctl_paint(void *arg)
     176{
     177        test_resp_t *resp = (test_resp_t *) arg;
    185178
    186         ui_control_unfocus(ui_test_ctl_ctl(testctl), 42);
    187         PCUT_ASSERT_TRUE(resp.unfocus);
    188         PCUT_ASSERT_INT_EQUALS(42, resp.unfocus_nfocus);
     179        resp->paint = true;
     180        return resp->rc;
     181}
    189182
    190         ui_test_ctl_destroy(testctl);
     183static ui_evclaim_t test_ctl_pos_event(void *arg, pos_event_t *event)
     184{
     185        test_resp_t *resp = (test_resp_t *) arg;
     186
     187        resp->pos = true;
     188        resp->pevent = *event;
     189
     190        return resp->claim;
    191191}
    192192
Note: See TracChangeset for help on using the changeset viewer.