Ignore:
File:
1 edited

Legend:

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

    r1eaead4 r62223ec  
    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 *);
     44static void test_ctl_unfocus(void *);
     45
     46static ui_control_ops_t test_ctl_ops = {
     47        .destroy = test_ctl_destroy,
     48        .paint = test_ctl_paint,
     49        .pos_event = test_ctl_pos_event,
     50        .unfocus = test_ctl_unfocus
     51};
     52
     53/** Test response */
     54typedef struct {
     55        /** Claim to return */
     56        ui_evclaim_t claim;
     57        /** Result code to return */
     58        errno_t rc;
     59
     60        /** @c true iff destroy was called */
     61        bool destroy;
     62
     63        /** @c true iff paint was called */
     64        bool paint;
     65
     66        /** @c true iff pos_event was called */
     67        bool pos;
     68        /** Position event that was sent */
     69        pos_event_t pevent;
     70
     71        /** @c true iff unfocus was called */
     72        bool unfocus;
     73} test_resp_t;
    4374
    4475/** Allocate and deallocate control */
     
    4879        errno_t rc;
    4980
    50         rc = ui_control_new(&ui_test_ctl_ops, NULL, &control);
     81        rc = ui_control_new(&test_ctl_ops, NULL, &control);
    5182        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    5283        PCUT_ASSERT_NOT_NULL(control);
     
    6495PCUT_TEST(destroy)
    6596{
    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);
     97        ui_control_t *control = NULL;
     98        test_resp_t resp;
     99        errno_t rc;
     100
     101        rc = ui_control_new(&test_ctl_ops, &resp, &control);
     102        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     103        PCUT_ASSERT_NOT_NULL(control);
    73104
    74105        resp.rc = EOK;
    75106        resp.destroy = false;
    76107
    77         ui_control_destroy(ui_test_ctl_ctl(testctl));
     108        ui_control_destroy(control);
    78109        PCUT_ASSERT_TRUE(resp.destroy);
    79110}
     
    82113PCUT_TEST(paint)
    83114{
    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);
     115        ui_control_t *control = NULL;
     116        test_resp_t resp;
     117        errno_t rc;
     118
     119        rc = ui_control_new(&test_ctl_ops, &resp, &control);
     120        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     121        PCUT_ASSERT_NOT_NULL(control);
    91122
    92123        resp.rc = EOK;
    93124        resp.paint = false;
    94125
    95         rc = ui_control_paint(ui_test_ctl_ctl(testctl));
     126        rc = ui_control_paint(control);
    96127        PCUT_ASSERT_ERRNO_VAL(resp.rc, rc);
    97128        PCUT_ASSERT_TRUE(resp.paint);
     
    100131        resp.paint = false;
    101132
    102         rc = ui_control_paint(ui_test_ctl_ctl(testctl));
     133        rc = ui_control_paint(control);
    103134        PCUT_ASSERT_ERRNO_VAL(resp.rc, rc);
    104135        PCUT_ASSERT_TRUE(resp.paint);
    105136
    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);
     137        ui_control_delete(control);
    138138}
    139139
     
    141141PCUT_TEST(pos_event)
    142142{
    143         ui_test_ctl_t *testctl = NULL;
    144         ui_tc_resp_t resp;
     143        ui_control_t *control = NULL;
     144        test_resp_t resp;
    145145        pos_event_t event;
    146146        ui_evclaim_t claim;
    147147        errno_t rc;
    148148
    149         rc = ui_test_ctl_create(&resp, &testctl);
    150         PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    151         PCUT_ASSERT_NOT_NULL(testctl);
     149        rc = ui_control_new(&test_ctl_ops, &resp, &control);
     150        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     151        PCUT_ASSERT_NOT_NULL(control);
    152152
    153153        resp.claim = ui_claimed;
     
    159159        event.vpos = 4;
    160160
    161         claim = ui_control_pos_event(ui_test_ctl_ctl(testctl), &event);
     161        claim = ui_control_pos_event(control, &event);
    162162        PCUT_ASSERT_EQUALS(resp.claim, claim);
    163163        PCUT_ASSERT_TRUE(resp.pos);
     
    168168        PCUT_ASSERT_INT_EQUALS(resp.pevent.vpos, event.vpos);
    169169
    170         ui_test_ctl_destroy(testctl);
     170        ui_control_delete(control);
    171171}
    172172
     
    174174PCUT_TEST(unfocus)
    175175{
    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);
     176        ui_control_t *control = NULL;
     177        test_resp_t resp;
     178        errno_t rc;
     179
     180        rc = ui_control_new(&test_ctl_ops, &resp, &control);
     181        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     182        PCUT_ASSERT_NOT_NULL(control);
    183183
    184184        resp.unfocus = false;
    185185
    186         ui_control_unfocus(ui_test_ctl_ctl(testctl), 42);
     186        ui_control_unfocus(control);
    187187        PCUT_ASSERT_TRUE(resp.unfocus);
    188         PCUT_ASSERT_INT_EQUALS(42, resp.unfocus_nfocus);
    189 
    190         ui_test_ctl_destroy(testctl);
     188
     189        ui_control_delete(control);
     190}
     191
     192static void test_ctl_destroy(void *arg)
     193{
     194        test_resp_t *resp = (test_resp_t *) arg;
     195
     196        resp->destroy = true;
     197}
     198
     199static errno_t test_ctl_paint(void *arg)
     200{
     201        test_resp_t *resp = (test_resp_t *) arg;
     202
     203        resp->paint = true;
     204        return resp->rc;
     205}
     206
     207static ui_evclaim_t test_ctl_pos_event(void *arg, pos_event_t *event)
     208{
     209        test_resp_t *resp = (test_resp_t *) arg;
     210
     211        resp->pos = true;
     212        resp->pevent = *event;
     213
     214        return resp->claim;
     215}
     216
     217static void test_ctl_unfocus(void *arg)
     218{
     219        test_resp_t *resp = (test_resp_t *) arg;
     220
     221        resp->unfocus = true;
    191222}
    192223
Note: See TracChangeset for help on using the changeset viewer.