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