1 | /*
|
---|
2 | * Copyright (c) 2023 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 <stdbool.h>
|
---|
31 | #include <ui/list.h>
|
---|
32 | #include <ui/pbutton.h>
|
---|
33 | #include <ui/ui.h>
|
---|
34 | #include <ui/selectdialog.h>
|
---|
35 | #include "../private/list.h"
|
---|
36 | #include "../private/selectdialog.h"
|
---|
37 | #include "../private/window.h"
|
---|
38 |
|
---|
39 | PCUT_INIT;
|
---|
40 |
|
---|
41 | PCUT_TEST_SUITE(select_dialog);
|
---|
42 |
|
---|
43 | static void test_dialog_bok(ui_select_dialog_t *, void *, void *);
|
---|
44 | static void test_dialog_bcancel(ui_select_dialog_t *, void *);
|
---|
45 | static void test_dialog_close(ui_select_dialog_t *, void *);
|
---|
46 |
|
---|
47 | static ui_select_dialog_cb_t test_select_dialog_cb = {
|
---|
48 | .bok = test_dialog_bok,
|
---|
49 | .bcancel = test_dialog_bcancel,
|
---|
50 | .close = test_dialog_close
|
---|
51 | };
|
---|
52 |
|
---|
53 | static ui_select_dialog_cb_t dummy_select_dialog_cb = {
|
---|
54 | };
|
---|
55 |
|
---|
56 | typedef struct {
|
---|
57 | bool bok;
|
---|
58 | const char *fname;
|
---|
59 | bool bcancel;
|
---|
60 | bool close;
|
---|
61 | } test_cb_resp_t;
|
---|
62 |
|
---|
63 | /** Create and destroy select dialog */
|
---|
64 | PCUT_TEST(create_destroy)
|
---|
65 | {
|
---|
66 | errno_t rc;
|
---|
67 | ui_t *ui = NULL;
|
---|
68 | ui_select_dialog_params_t params;
|
---|
69 | ui_select_dialog_t *dialog = NULL;
|
---|
70 |
|
---|
71 | rc = ui_create_disp(NULL, &ui);
|
---|
72 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
73 |
|
---|
74 | ui_select_dialog_params_init(¶ms);
|
---|
75 | params.caption = "Select one";
|
---|
76 | params.prompt = "Please select";
|
---|
77 |
|
---|
78 | rc = ui_select_dialog_create(ui, ¶ms, &dialog);
|
---|
79 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
80 | PCUT_ASSERT_NOT_NULL(dialog);
|
---|
81 |
|
---|
82 | ui_select_dialog_destroy(dialog);
|
---|
83 | ui_destroy(ui);
|
---|
84 | }
|
---|
85 |
|
---|
86 | /** ui_select_dialog_destroy() can take NULL argument (no-op) */
|
---|
87 | PCUT_TEST(destroy_null)
|
---|
88 | {
|
---|
89 | ui_select_dialog_destroy(NULL);
|
---|
90 | }
|
---|
91 |
|
---|
92 | /** Clicking OK invokes callback set via ui_select_dialog_set_cb() */
|
---|
93 | PCUT_TEST(bok_cb)
|
---|
94 | {
|
---|
95 | errno_t rc;
|
---|
96 | ui_t *ui = NULL;
|
---|
97 | ui_select_dialog_params_t params;
|
---|
98 | ui_select_dialog_t *dialog = NULL;
|
---|
99 | test_cb_resp_t resp;
|
---|
100 |
|
---|
101 | rc = ui_create_disp(NULL, &ui);
|
---|
102 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
103 |
|
---|
104 | ui_select_dialog_params_init(¶ms);
|
---|
105 | params.caption = "Select one";
|
---|
106 | params.prompt = "Please select";
|
---|
107 |
|
---|
108 | rc = ui_select_dialog_create(ui, ¶ms, &dialog);
|
---|
109 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
110 | PCUT_ASSERT_NOT_NULL(dialog);
|
---|
111 |
|
---|
112 | /* OK button callback with no callbacks set */
|
---|
113 | ui_pbutton_clicked(dialog->bok);
|
---|
114 |
|
---|
115 | /* OK button callback with callback not implemented */
|
---|
116 | ui_select_dialog_set_cb(dialog, &dummy_select_dialog_cb, NULL);
|
---|
117 | ui_pbutton_clicked(dialog->bok);
|
---|
118 |
|
---|
119 | /* OK button callback with real callback set */
|
---|
120 | resp.bok = false;
|
---|
121 | ui_select_dialog_set_cb(dialog, &test_select_dialog_cb, &resp);
|
---|
122 | ui_pbutton_clicked(dialog->bok);
|
---|
123 | PCUT_ASSERT_TRUE(resp.bok);
|
---|
124 |
|
---|
125 | ui_select_dialog_destroy(dialog);
|
---|
126 | ui_destroy(ui);
|
---|
127 | }
|
---|
128 |
|
---|
129 | /** Clicking Cancel invokes callback set via ui_select_dialog_set_cb() */
|
---|
130 | PCUT_TEST(bcancel_cb)
|
---|
131 | {
|
---|
132 | errno_t rc;
|
---|
133 | ui_t *ui = NULL;
|
---|
134 | ui_select_dialog_params_t params;
|
---|
135 | ui_select_dialog_t *dialog = NULL;
|
---|
136 | test_cb_resp_t resp;
|
---|
137 |
|
---|
138 | rc = ui_create_disp(NULL, &ui);
|
---|
139 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
140 |
|
---|
141 | ui_select_dialog_params_init(¶ms);
|
---|
142 | params.caption = "Select one";
|
---|
143 | params.prompt = "Please select";
|
---|
144 |
|
---|
145 | rc = ui_select_dialog_create(ui, ¶ms, &dialog);
|
---|
146 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
147 | PCUT_ASSERT_NOT_NULL(dialog);
|
---|
148 |
|
---|
149 | /* Cancel button callback with no callbacks set */
|
---|
150 | ui_pbutton_clicked(dialog->bcancel);
|
---|
151 |
|
---|
152 | /* Cancel button callback with callback not implemented */
|
---|
153 | ui_select_dialog_set_cb(dialog, &dummy_select_dialog_cb, NULL);
|
---|
154 | ui_pbutton_clicked(dialog->bcancel);
|
---|
155 |
|
---|
156 | /* OK button callback with real callback set */
|
---|
157 | resp.bcancel = false;
|
---|
158 | ui_select_dialog_set_cb(dialog, &test_select_dialog_cb, &resp);
|
---|
159 | ui_pbutton_clicked(dialog->bcancel);
|
---|
160 | PCUT_ASSERT_TRUE(resp.bcancel);
|
---|
161 |
|
---|
162 | ui_select_dialog_destroy(dialog);
|
---|
163 | ui_destroy(ui);
|
---|
164 | }
|
---|
165 |
|
---|
166 | /** Selecting a list entry invokes bok callback set via ui_select_dialog_set_cb() */
|
---|
167 | PCUT_TEST(lselect_cb)
|
---|
168 | {
|
---|
169 | errno_t rc;
|
---|
170 | ui_t *ui = NULL;
|
---|
171 | ui_list_entry_t *entry;
|
---|
172 | ui_select_dialog_params_t params;
|
---|
173 | ui_select_dialog_t *dialog = NULL;
|
---|
174 | ui_list_entry_attr_t attr;
|
---|
175 | test_cb_resp_t resp;
|
---|
176 |
|
---|
177 | rc = ui_create_disp(NULL, &ui);
|
---|
178 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
179 |
|
---|
180 | ui_select_dialog_params_init(¶ms);
|
---|
181 | params.caption = "Select one";
|
---|
182 | params.prompt = "Please select";
|
---|
183 |
|
---|
184 | rc = ui_select_dialog_create(ui, ¶ms, &dialog);
|
---|
185 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
186 | PCUT_ASSERT_NOT_NULL(dialog);
|
---|
187 |
|
---|
188 | /* Need an entry to select */
|
---|
189 | ui_list_entry_attr_init(&attr);
|
---|
190 | attr.caption = "Entry";
|
---|
191 | rc = ui_select_dialog_append(dialog, &attr);
|
---|
192 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
193 |
|
---|
194 | entry = ui_list_first(dialog->list);
|
---|
195 |
|
---|
196 | /* Select entry with no callbacks set */
|
---|
197 | ui_list_selected(entry);
|
---|
198 |
|
---|
199 | /* Select entry with callback not implemented */
|
---|
200 | ui_select_dialog_set_cb(dialog, &dummy_select_dialog_cb, NULL);
|
---|
201 | ui_list_selected(entry);
|
---|
202 |
|
---|
203 | /* Select entry with real callback set */
|
---|
204 | resp.bok = false;
|
---|
205 | ui_select_dialog_set_cb(dialog, &test_select_dialog_cb, &resp);
|
---|
206 | ui_list_selected(entry);
|
---|
207 | PCUT_ASSERT_TRUE(resp.bok);
|
---|
208 |
|
---|
209 | ui_select_dialog_destroy(dialog);
|
---|
210 | ui_destroy(ui);
|
---|
211 | }
|
---|
212 |
|
---|
213 | /** Sending window close request invokes callback set via
|
---|
214 | * ui_select_dialog_set_cb()
|
---|
215 | */
|
---|
216 | PCUT_TEST(close_cb)
|
---|
217 | {
|
---|
218 | errno_t rc;
|
---|
219 | ui_t *ui = NULL;
|
---|
220 | ui_select_dialog_params_t params;
|
---|
221 | ui_select_dialog_t *dialog = NULL;
|
---|
222 | test_cb_resp_t resp;
|
---|
223 |
|
---|
224 | rc = ui_create_disp(NULL, &ui);
|
---|
225 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
226 |
|
---|
227 | ui_select_dialog_params_init(¶ms);
|
---|
228 | params.caption = "Select one";
|
---|
229 | params.prompt = "Please select";
|
---|
230 |
|
---|
231 | rc = ui_select_dialog_create(ui, ¶ms, &dialog);
|
---|
232 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
233 | PCUT_ASSERT_NOT_NULL(dialog);
|
---|
234 |
|
---|
235 | /* Button callback with no callbacks set */
|
---|
236 | ui_window_send_close(dialog->window);
|
---|
237 |
|
---|
238 | /* Button callback with unfocus callback not implemented */
|
---|
239 | ui_select_dialog_set_cb(dialog, &dummy_select_dialog_cb, NULL);
|
---|
240 | ui_window_send_close(dialog->window);
|
---|
241 |
|
---|
242 | /* Button callback with real callback set */
|
---|
243 | resp.close = false;
|
---|
244 | ui_select_dialog_set_cb(dialog, &test_select_dialog_cb, &resp);
|
---|
245 | ui_window_send_close(dialog->window);
|
---|
246 | PCUT_ASSERT_TRUE(resp.close);
|
---|
247 |
|
---|
248 | ui_select_dialog_destroy(dialog);
|
---|
249 | ui_destroy(ui);
|
---|
250 | }
|
---|
251 |
|
---|
252 | /** ui_select_dialog_append() appends entries TBD */
|
---|
253 | PCUT_TEST(append)
|
---|
254 | {
|
---|
255 | errno_t rc;
|
---|
256 | ui_t *ui = NULL;
|
---|
257 | ui_select_dialog_params_t params;
|
---|
258 | ui_select_dialog_t *dialog = NULL;
|
---|
259 | ui_list_entry_attr_t attr;
|
---|
260 |
|
---|
261 | rc = ui_create_disp(NULL, &ui);
|
---|
262 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
263 |
|
---|
264 | ui_select_dialog_params_init(¶ms);
|
---|
265 | params.caption = "Select one";
|
---|
266 | params.prompt = "Please select";
|
---|
267 |
|
---|
268 | rc = ui_select_dialog_create(ui, ¶ms, &dialog);
|
---|
269 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
270 | PCUT_ASSERT_NOT_NULL(dialog);
|
---|
271 |
|
---|
272 | PCUT_ASSERT_INT_EQUALS(0, dialog->list->entries_cnt);
|
---|
273 |
|
---|
274 | /* Add one entry */
|
---|
275 | ui_list_entry_attr_init(&attr);
|
---|
276 | attr.caption = "Entry";
|
---|
277 | rc = ui_select_dialog_append(dialog, &attr);
|
---|
278 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
279 |
|
---|
280 | PCUT_ASSERT_INT_EQUALS(1, dialog->list->entries_cnt);
|
---|
281 |
|
---|
282 | ui_select_dialog_destroy(dialog);
|
---|
283 | ui_destroy(ui);
|
---|
284 | }
|
---|
285 |
|
---|
286 | /** ui_select_dialog_paint() succeeds */
|
---|
287 | PCUT_TEST(paint)
|
---|
288 | {
|
---|
289 | errno_t rc;
|
---|
290 | ui_t *ui = NULL;
|
---|
291 | ui_select_dialog_params_t params;
|
---|
292 | ui_select_dialog_t *dialog = NULL;
|
---|
293 |
|
---|
294 | rc = ui_create_disp(NULL, &ui);
|
---|
295 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
296 |
|
---|
297 | ui_select_dialog_params_init(¶ms);
|
---|
298 | params.caption = "Select one";
|
---|
299 | params.prompt = "Please select";
|
---|
300 |
|
---|
301 | rc = ui_select_dialog_create(ui, ¶ms, &dialog);
|
---|
302 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
303 | PCUT_ASSERT_NOT_NULL(dialog);
|
---|
304 |
|
---|
305 | rc = ui_select_dialog_paint(dialog);
|
---|
306 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
307 |
|
---|
308 | ui_select_dialog_destroy(dialog);
|
---|
309 | ui_destroy(ui);
|
---|
310 | }
|
---|
311 |
|
---|
312 | /** ui_select_dialog_list() returns the UI list */
|
---|
313 | PCUT_TEST(list)
|
---|
314 | {
|
---|
315 | errno_t rc;
|
---|
316 | ui_t *ui = NULL;
|
---|
317 | ui_select_dialog_params_t params;
|
---|
318 | ui_select_dialog_t *dialog = NULL;
|
---|
319 | ui_list_t *list;
|
---|
320 | ui_list_entry_attr_t attr;
|
---|
321 |
|
---|
322 | rc = ui_create_disp(NULL, &ui);
|
---|
323 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
324 |
|
---|
325 | ui_select_dialog_params_init(¶ms);
|
---|
326 | params.caption = "Select one";
|
---|
327 | params.prompt = "Please select";
|
---|
328 |
|
---|
329 | rc = ui_select_dialog_create(ui, ¶ms, &dialog);
|
---|
330 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
331 | PCUT_ASSERT_NOT_NULL(dialog);
|
---|
332 |
|
---|
333 | list = ui_select_dialog_list(dialog);
|
---|
334 | PCUT_ASSERT_NOT_NULL(list);
|
---|
335 |
|
---|
336 | PCUT_ASSERT_INT_EQUALS(0, ui_list_entries_cnt(list));
|
---|
337 |
|
---|
338 | /* Add one entry */
|
---|
339 | ui_list_entry_attr_init(&attr);
|
---|
340 | attr.caption = "Entry";
|
---|
341 | rc = ui_select_dialog_append(dialog, &attr);
|
---|
342 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
343 |
|
---|
344 | PCUT_ASSERT_INT_EQUALS(1, ui_list_entries_cnt(list));
|
---|
345 |
|
---|
346 | ui_select_dialog_destroy(dialog);
|
---|
347 | ui_destroy(ui);
|
---|
348 | }
|
---|
349 |
|
---|
350 | static void test_dialog_bok(ui_select_dialog_t *dialog, void *arg,
|
---|
351 | void *earg)
|
---|
352 | {
|
---|
353 | test_cb_resp_t *resp = (test_cb_resp_t *) arg;
|
---|
354 |
|
---|
355 | resp->bok = true;
|
---|
356 | }
|
---|
357 |
|
---|
358 | static void test_dialog_bcancel(ui_select_dialog_t *dialog, void *arg)
|
---|
359 | {
|
---|
360 | test_cb_resp_t *resp = (test_cb_resp_t *) arg;
|
---|
361 |
|
---|
362 | resp->bcancel = true;
|
---|
363 | }
|
---|
364 |
|
---|
365 | static void test_dialog_close(ui_select_dialog_t *dialog, void *arg)
|
---|
366 | {
|
---|
367 | test_cb_resp_t *resp = (test_cb_resp_t *) arg;
|
---|
368 |
|
---|
369 | resp->close = true;
|
---|
370 | }
|
---|
371 |
|
---|
372 | PCUT_EXPORT(select_dialog);
|
---|