source: mainline/uspace/lib/ui/test/msgdialog.c@ 12dd36c

Last change on this file since 12dd36c was 9a07ee3, checked in by Jiri Svoboda <jiri@…>, 8 months ago

Add message dialog choice selection (OK, OK/Cancel)

  • Property mode set to 100644
File size: 5.0 KB
Line 
1/*
2 * Copyright (c) 2024 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/pbutton.h>
32#include <ui/ui.h>
33#include <ui/msgdialog.h>
34#include "../private/msgdialog.h"
35#include "../private/window.h"
36
37PCUT_INIT;
38
39PCUT_TEST_SUITE(msg_dialog);
40
41static void test_dialog_button(ui_msg_dialog_t *, void *, unsigned);
42static void test_dialog_close(ui_msg_dialog_t *, void *);
43
44static ui_msg_dialog_cb_t test_msg_dialog_cb = {
45 .button = test_dialog_button,
46 .close = test_dialog_close
47};
48
49static ui_msg_dialog_cb_t dummy_msg_dialog_cb = {
50};
51
52typedef struct {
53 bool button;
54 unsigned bnum;
55 bool close;
56} test_cb_resp_t;
57
58/** Create and destroy message dialog */
59PCUT_TEST(create_destroy)
60{
61 errno_t rc;
62 ui_t *ui = NULL;
63 ui_msg_dialog_params_t params;
64 ui_msg_dialog_t *dialog = NULL;
65
66 rc = ui_create_disp(NULL, &ui);
67 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
68
69 ui_msg_dialog_params_init(&params);
70 params.caption = "Message";
71 params.text = "Hello";
72
73 rc = ui_msg_dialog_create(ui, &params, &dialog);
74 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
75 PCUT_ASSERT_NOT_NULL(dialog);
76
77 ui_msg_dialog_destroy(dialog);
78 ui_destroy(ui);
79}
80
81/** ui_msg_dialog_destroy() can take NULL argument (no-op) */
82PCUT_TEST(destroy_null)
83{
84 ui_msg_dialog_destroy(NULL);
85}
86
87/** Button click invokes callback set via ui_msg_dialog_set_cb() */
88PCUT_TEST(button_cb)
89{
90 errno_t rc;
91 ui_t *ui = NULL;
92 ui_msg_dialog_params_t params;
93 ui_msg_dialog_t *dialog = NULL;
94 unsigned i;
95 test_cb_resp_t resp;
96
97 rc = ui_create_disp(NULL, &ui);
98 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
99
100 ui_msg_dialog_params_init(&params);
101 params.caption = "Message";
102 params.text = "Hello";
103 params.choice = umdc_ok_cancel;
104
105 rc = ui_msg_dialog_create(ui, &params, &dialog);
106 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
107 PCUT_ASSERT_NOT_NULL(dialog);
108
109 /* Button callback with no callbacks set */
110 ui_pbutton_clicked(dialog->btn[0]);
111
112 /* Button callback with callback not implemented */
113 ui_msg_dialog_set_cb(dialog, &dummy_msg_dialog_cb, NULL);
114 ui_pbutton_clicked(dialog->btn[0]);
115
116 for (i = 0; i < 2; i++) {
117 /* Button callback with real callback set */
118 resp.button = false;
119 resp.bnum = 123;
120 ui_msg_dialog_set_cb(dialog, &test_msg_dialog_cb, &resp);
121 ui_pbutton_clicked(dialog->btn[i]);
122 PCUT_ASSERT_TRUE(resp.button);
123 PCUT_ASSERT_INT_EQUALS(i, resp.bnum);
124 }
125
126 ui_msg_dialog_destroy(dialog);
127 ui_destroy(ui);
128}
129
130/** Sending window close request invokes callback set via
131 * ui_msg_dialog_set_cb()
132 */
133PCUT_TEST(close_cb)
134{
135 errno_t rc;
136 ui_t *ui = NULL;
137 ui_msg_dialog_params_t params;
138 ui_msg_dialog_t *dialog = NULL;
139 test_cb_resp_t resp;
140
141 rc = ui_create_disp(NULL, &ui);
142 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
143
144 ui_msg_dialog_params_init(&params);
145 params.caption = "Message";
146 params.text = "Hello";
147
148 rc = ui_msg_dialog_create(ui, &params, &dialog);
149 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
150 PCUT_ASSERT_NOT_NULL(dialog);
151
152 /* Button callback with no callbacks set */
153 ui_window_send_close(dialog->window);
154
155 /* Button callback with unfocus callback not implemented */
156 ui_msg_dialog_set_cb(dialog, &dummy_msg_dialog_cb, NULL);
157 ui_window_send_close(dialog->window);
158
159 /* Button callback with real callback set */
160 resp.close = false;
161 ui_msg_dialog_set_cb(dialog, &test_msg_dialog_cb, &resp);
162 ui_window_send_close(dialog->window);
163 PCUT_ASSERT_TRUE(resp.close);
164
165 ui_msg_dialog_destroy(dialog);
166 ui_destroy(ui);
167}
168
169static void test_dialog_button(ui_msg_dialog_t *dialog, void *arg,
170 unsigned bnum)
171{
172 test_cb_resp_t *resp = (test_cb_resp_t *) arg;
173
174 resp->button = true;
175 resp->bnum = bnum;
176}
177
178static void test_dialog_close(ui_msg_dialog_t *dialog, void *arg)
179{
180 test_cb_resp_t *resp = (test_cb_resp_t *) arg;
181
182 resp->close = true;
183}
184
185PCUT_EXPORT(msg_dialog);
Note: See TracBrowser for help on using the repository browser.