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 <errno.h>
|
---|
30 | #include <mem.h>
|
---|
31 | #include <io/pos_event.h>
|
---|
32 | #include <pcut/pcut.h>
|
---|
33 | #include <ui/control.h>
|
---|
34 | #include <stdbool.h>
|
---|
35 | #include <types/ui/event.h>
|
---|
36 |
|
---|
37 | PCUT_INIT;
|
---|
38 |
|
---|
39 | PCUT_TEST_SUITE(control);
|
---|
40 |
|
---|
41 | static errno_t test_ctl_paint(void *);
|
---|
42 | static ui_evclaim_t test_ctl_pos_event(void *, pos_event_t *);
|
---|
43 |
|
---|
44 | static ui_control_ops_t test_ctl_ops = {
|
---|
45 | .paint = test_ctl_paint,
|
---|
46 | .pos_event = test_ctl_pos_event
|
---|
47 | };
|
---|
48 |
|
---|
49 | /** Test response */
|
---|
50 | typedef struct {
|
---|
51 | /** Claim to return */
|
---|
52 | ui_evclaim_t claim;
|
---|
53 | /** Result code to return */
|
---|
54 | errno_t rc;
|
---|
55 |
|
---|
56 | /** @c true iff paint was called */
|
---|
57 | bool paint;
|
---|
58 |
|
---|
59 | /** @c true iff pos_event was called */
|
---|
60 | bool pos;
|
---|
61 | /** Position event that was sent */
|
---|
62 | pos_event_t pevent;
|
---|
63 | } test_resp_t;
|
---|
64 |
|
---|
65 | /** Allocate and deallocate control */
|
---|
66 | PCUT_TEST(new_delete)
|
---|
67 | {
|
---|
68 | ui_control_t *control = NULL;
|
---|
69 | errno_t rc;
|
---|
70 |
|
---|
71 | rc = ui_control_new(&test_ctl_ops, NULL, &control);
|
---|
72 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
73 | PCUT_ASSERT_NOT_NULL(control);
|
---|
74 |
|
---|
75 | ui_control_delete(control);
|
---|
76 | }
|
---|
77 |
|
---|
78 | /** ui_control_delete() can take NULL argument (no-op) */
|
---|
79 | PCUT_TEST(delete_null)
|
---|
80 | {
|
---|
81 | ui_control_delete(NULL);
|
---|
82 | }
|
---|
83 |
|
---|
84 | /** Test sending paint request to control */
|
---|
85 | PCUT_TEST(paint)
|
---|
86 | {
|
---|
87 | ui_control_t *control = NULL;
|
---|
88 | test_resp_t resp;
|
---|
89 | errno_t rc;
|
---|
90 |
|
---|
91 | rc = ui_control_new(&test_ctl_ops, &resp, &control);
|
---|
92 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
93 | PCUT_ASSERT_NOT_NULL(control);
|
---|
94 |
|
---|
95 | resp.rc = EOK;
|
---|
96 | resp.paint = false;
|
---|
97 |
|
---|
98 | rc = ui_control_paint(control);
|
---|
99 | PCUT_ASSERT_ERRNO_VAL(resp.rc, rc);
|
---|
100 | PCUT_ASSERT_TRUE(resp.paint);
|
---|
101 |
|
---|
102 | resp.rc = EINVAL;
|
---|
103 | resp.paint = false;
|
---|
104 |
|
---|
105 | rc = ui_control_paint(control);
|
---|
106 | PCUT_ASSERT_ERRNO_VAL(resp.rc, rc);
|
---|
107 | PCUT_ASSERT_TRUE(resp.paint);
|
---|
108 |
|
---|
109 | ui_control_delete(control);
|
---|
110 | }
|
---|
111 |
|
---|
112 | /** Test sending position event to control */
|
---|
113 | PCUT_TEST(pos_event)
|
---|
114 | {
|
---|
115 | ui_control_t *control = NULL;
|
---|
116 | test_resp_t resp;
|
---|
117 | pos_event_t event;
|
---|
118 | ui_evclaim_t claim;
|
---|
119 | errno_t rc;
|
---|
120 |
|
---|
121 | rc = ui_control_new(&test_ctl_ops, &resp, &control);
|
---|
122 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
123 | PCUT_ASSERT_NOT_NULL(control);
|
---|
124 |
|
---|
125 | resp.claim = ui_claimed;
|
---|
126 | resp.pos = false;
|
---|
127 | event.pos_id = 1;
|
---|
128 | event.type = POS_PRESS;
|
---|
129 | event.btn_num = 2;
|
---|
130 | event.hpos = 3;
|
---|
131 | event.vpos = 4;
|
---|
132 |
|
---|
133 | claim = ui_control_pos_event(control, &event);
|
---|
134 | PCUT_ASSERT_EQUALS(resp.claim, claim);
|
---|
135 | PCUT_ASSERT_TRUE(resp.pos);
|
---|
136 | PCUT_ASSERT_INT_EQUALS(resp.pevent.pos_id, event.pos_id);
|
---|
137 | PCUT_ASSERT_EQUALS(resp.pevent.type, event.type);
|
---|
138 | PCUT_ASSERT_INT_EQUALS(resp.pevent.btn_num, event.btn_num);
|
---|
139 | PCUT_ASSERT_INT_EQUALS(resp.pevent.hpos, event.hpos);
|
---|
140 | PCUT_ASSERT_INT_EQUALS(resp.pevent.vpos, event.vpos);
|
---|
141 |
|
---|
142 | ui_control_delete(control);
|
---|
143 | }
|
---|
144 |
|
---|
145 | static errno_t test_ctl_paint(void *arg)
|
---|
146 | {
|
---|
147 | test_resp_t *resp = (test_resp_t *) arg;
|
---|
148 |
|
---|
149 | resp->paint = true;
|
---|
150 | return resp->rc;
|
---|
151 | }
|
---|
152 |
|
---|
153 | static ui_evclaim_t test_ctl_pos_event(void *arg, pos_event_t *event)
|
---|
154 | {
|
---|
155 | test_resp_t *resp = (test_resp_t *) arg;
|
---|
156 |
|
---|
157 | resp->pos = true;
|
---|
158 | resp->pevent = *event;
|
---|
159 |
|
---|
160 | return resp->claim;
|
---|
161 | }
|
---|
162 |
|
---|
163 | PCUT_EXPORT(control);
|
---|