source: mainline/uspace/lib/ui/test/control.c@ c68c18b9

serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since c68c18b9 was 62223ec, checked in by jxsvoboda <5887334+jxsvoboda@…>, 4 years ago

Close menu when window is unfocused

This of course means we need to do all the plumbing for delivering
unfocus event to UI controls.

  • Property mode set to 100644
File size: 5.4 KB
Line 
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 INTvvhhzccgggrERRUPTION) 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
37PCUT_INIT;
38
39PCUT_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;
74
75/** Allocate and deallocate control */
76PCUT_TEST(new_delete)
77{
78 ui_control_t *control = NULL;
79 errno_t rc;
80
81 rc = ui_control_new(&test_ctl_ops, NULL, &control);
82 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
83 PCUT_ASSERT_NOT_NULL(control);
84
85 ui_control_delete(control);
86}
87
88/** ui_control_delete() can take NULL argument (no-op) */
89PCUT_TEST(delete_null)
90{
91 ui_control_delete(NULL);
92}
93
94/** Test sending destroy request to control */
95PCUT_TEST(destroy)
96{
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);
104
105 resp.rc = EOK;
106 resp.destroy = false;
107
108 ui_control_destroy(control);
109 PCUT_ASSERT_TRUE(resp.destroy);
110}
111
112/** Test sending paint request to control */
113PCUT_TEST(paint)
114{
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);
122
123 resp.rc = EOK;
124 resp.paint = false;
125
126 rc = ui_control_paint(control);
127 PCUT_ASSERT_ERRNO_VAL(resp.rc, rc);
128 PCUT_ASSERT_TRUE(resp.paint);
129
130 resp.rc = EINVAL;
131 resp.paint = false;
132
133 rc = ui_control_paint(control);
134 PCUT_ASSERT_ERRNO_VAL(resp.rc, rc);
135 PCUT_ASSERT_TRUE(resp.paint);
136
137 ui_control_delete(control);
138}
139
140/** Test sending position event to control */
141PCUT_TEST(pos_event)
142{
143 ui_control_t *control = NULL;
144 test_resp_t resp;
145 pos_event_t event;
146 ui_evclaim_t claim;
147 errno_t rc;
148
149 rc = ui_control_new(&test_ctl_ops, &resp, &control);
150 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
151 PCUT_ASSERT_NOT_NULL(control);
152
153 resp.claim = ui_claimed;
154 resp.pos = false;
155 event.pos_id = 1;
156 event.type = POS_PRESS;
157 event.btn_num = 2;
158 event.hpos = 3;
159 event.vpos = 4;
160
161 claim = ui_control_pos_event(control, &event);
162 PCUT_ASSERT_EQUALS(resp.claim, claim);
163 PCUT_ASSERT_TRUE(resp.pos);
164 PCUT_ASSERT_INT_EQUALS(resp.pevent.pos_id, event.pos_id);
165 PCUT_ASSERT_EQUALS(resp.pevent.type, event.type);
166 PCUT_ASSERT_INT_EQUALS(resp.pevent.btn_num, event.btn_num);
167 PCUT_ASSERT_INT_EQUALS(resp.pevent.hpos, event.hpos);
168 PCUT_ASSERT_INT_EQUALS(resp.pevent.vpos, event.vpos);
169
170 ui_control_delete(control);
171}
172
173/** Test sending unfocus to control */
174PCUT_TEST(unfocus)
175{
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);
183
184 resp.unfocus = false;
185
186 ui_control_unfocus(control);
187 PCUT_ASSERT_TRUE(resp.unfocus);
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;
222}
223
224PCUT_EXPORT(control);
Note: See TracBrowser for help on using the repository browser.