| 1 | /*
|
|---|
| 2 | * Copyright (c) 2020 Jiri Svoboda
|
|---|
| 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 |
|
|---|
| 29 | #include <mem.h>
|
|---|
| 30 | #include <io/pos_event.h>
|
|---|
| 31 | #include <pcut/pcut.h>
|
|---|
| 32 | #include <ui/control.h>
|
|---|
| 33 | #include <stdbool.h>
|
|---|
| 34 | #include <types/ui/event.h>
|
|---|
| 35 |
|
|---|
| 36 | PCUT_INIT;
|
|---|
| 37 |
|
|---|
| 38 | PCUT_TEST_SUITE(control);
|
|---|
| 39 |
|
|---|
| 40 | static ui_evclaim_t test_ctl_pos_event(void *, pos_event_t *);
|
|---|
| 41 |
|
|---|
| 42 | static ui_control_ops_t test_ctl_ops = {
|
|---|
| 43 | .pos_event = test_ctl_pos_event
|
|---|
| 44 | };
|
|---|
| 45 |
|
|---|
| 46 | /** Test response */
|
|---|
| 47 | typedef struct {
|
|---|
| 48 | /** Claim to return */
|
|---|
| 49 | ui_evclaim_t claim;
|
|---|
| 50 |
|
|---|
| 51 | /** @c true iff pos_event was called */
|
|---|
| 52 | bool pos;
|
|---|
| 53 | /** Position event that was sent */
|
|---|
| 54 | pos_event_t pevent;
|
|---|
| 55 | } test_resp_t;
|
|---|
| 56 |
|
|---|
| 57 | /** Allocate and deallocate control */
|
|---|
| 58 | PCUT_TEST(new_delete)
|
|---|
| 59 | {
|
|---|
| 60 | ui_control_t *control = NULL;
|
|---|
| 61 | errno_t rc;
|
|---|
| 62 |
|
|---|
| 63 | rc = ui_control_new(&test_ctl_ops, NULL, &control);
|
|---|
| 64 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 65 | PCUT_ASSERT_NOT_NULL(control);
|
|---|
| 66 |
|
|---|
| 67 | ui_control_delete(control);
|
|---|
| 68 | }
|
|---|
| 69 |
|
|---|
| 70 | /** ui_control_delete() can take NULL argument (no-op) */
|
|---|
| 71 | PCUT_TEST(delete_null)
|
|---|
| 72 | {
|
|---|
| 73 | ui_control_delete(NULL);
|
|---|
| 74 | }
|
|---|
| 75 |
|
|---|
| 76 | /** Test sending position event to control */
|
|---|
| 77 | PCUT_TEST(pos_event)
|
|---|
| 78 | {
|
|---|
| 79 | ui_control_t *control = NULL;
|
|---|
| 80 | test_resp_t resp;
|
|---|
| 81 | pos_event_t event;
|
|---|
| 82 | ui_evclaim_t claim;
|
|---|
| 83 | errno_t rc;
|
|---|
| 84 |
|
|---|
| 85 | rc = ui_control_new(&test_ctl_ops, &resp, &control);
|
|---|
| 86 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 87 | PCUT_ASSERT_NOT_NULL(control);
|
|---|
| 88 |
|
|---|
| 89 | resp.claim = ui_claimed;
|
|---|
| 90 | resp.pos = false;
|
|---|
| 91 | event.pos_id = 1;
|
|---|
| 92 | event.type = POS_PRESS;
|
|---|
| 93 | event.btn_num = 2;
|
|---|
| 94 | event.hpos = 3;
|
|---|
| 95 | event.vpos = 4;
|
|---|
| 96 |
|
|---|
| 97 | claim = ui_control_pos_event(control, &event);
|
|---|
| 98 | PCUT_ASSERT_EQUALS(resp.claim, claim);
|
|---|
| 99 | PCUT_ASSERT_TRUE(resp.pos);
|
|---|
| 100 | PCUT_ASSERT_INT_EQUALS(resp.pevent.pos_id, event.pos_id);
|
|---|
| 101 | PCUT_ASSERT_EQUALS(resp.pevent.type, event.type);
|
|---|
| 102 | PCUT_ASSERT_INT_EQUALS(resp.pevent.btn_num, event.btn_num);
|
|---|
| 103 | PCUT_ASSERT_INT_EQUALS(resp.pevent.hpos, event.hpos);
|
|---|
| 104 | PCUT_ASSERT_INT_EQUALS(resp.pevent.vpos, event.vpos);
|
|---|
| 105 |
|
|---|
| 106 | ui_control_delete(control);
|
|---|
| 107 | }
|
|---|
| 108 |
|
|---|
| 109 | static ui_evclaim_t test_ctl_pos_event(void *arg, pos_event_t *event)
|
|---|
| 110 | {
|
|---|
| 111 | test_resp_t *resp = (test_resp_t *) arg;
|
|---|
| 112 |
|
|---|
| 113 | resp->pos = true;
|
|---|
| 114 | resp->pevent = *event;
|
|---|
| 115 |
|
|---|
| 116 | return resp->claim;
|
|---|
| 117 | }
|
|---|
| 118 |
|
|---|
| 119 | PCUT_EXPORT(control);
|
|---|