source: mainline/uspace/lib/ui/test/msgdialog.c@ 252d03c

serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 252d03c was de227aba, checked in by Jiri Svoboda <jiri@…>, 4 years ago

Message dialog class

This simplifies the task of creating as simple dialog window with text
and button(s). I am deliberately not providing an example to
UI demo at this point, given that an application cannot create
two windows at the same time (yet).

  • Property mode set to 100644
File size: 4.9 KB
Line 
1/*
2 * Copyright (c) 2021 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 test_cb_resp_t resp;
95
96 rc = ui_create_disp(NULL, &ui);
97 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
98
99 ui_msg_dialog_params_init(&params);
100 params.caption = "Message";
101 params.text = "Hello";
102
103 rc = ui_msg_dialog_create(ui, &params, &dialog);
104 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
105 PCUT_ASSERT_NOT_NULL(dialog);
106
107 /* Button callback with no callbacks set */
108 ui_pbutton_clicked(dialog->bok);
109
110 /* Button callback with unfocus callback not implemented */
111 ui_msg_dialog_set_cb(dialog, &dummy_msg_dialog_cb, NULL);
112 ui_pbutton_clicked(dialog->bok);
113
114 /* Button callback with real callback set */
115 resp.button = false;
116 resp.bnum = 123;
117 ui_msg_dialog_set_cb(dialog, &test_msg_dialog_cb, &resp);
118 ui_pbutton_clicked(dialog->bok);
119 PCUT_ASSERT_TRUE(resp.button);
120 PCUT_ASSERT_INT_EQUALS(0, resp.bnum);
121
122 ui_msg_dialog_destroy(dialog);
123 ui_destroy(ui);
124}
125
126/** Sending window close request invokes callback set via
127 * ui_msg_dialog_set_cb()
128 */
129PCUT_TEST(close_cb)
130{
131 errno_t rc;
132 ui_t *ui = NULL;
133 ui_msg_dialog_params_t params;
134 ui_msg_dialog_t *dialog = NULL;
135 test_cb_resp_t resp;
136
137 rc = ui_create_disp(NULL, &ui);
138 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
139
140 ui_msg_dialog_params_init(&params);
141 params.caption = "Message";
142 params.text = "Hello";
143
144 rc = ui_msg_dialog_create(ui, &params, &dialog);
145 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
146 PCUT_ASSERT_NOT_NULL(dialog);
147
148 /* Button callback with no callbacks set */
149 ui_window_send_close(dialog->window);
150
151 /* Button callback with unfocus callback not implemented */
152 ui_msg_dialog_set_cb(dialog, &dummy_msg_dialog_cb, NULL);
153 ui_window_send_close(dialog->window);
154
155 /* Button callback with real callback set */
156 resp.close = false;
157 ui_msg_dialog_set_cb(dialog, &test_msg_dialog_cb, &resp);
158 ui_window_send_close(dialog->window);
159 PCUT_ASSERT_TRUE(resp.close);
160
161 ui_msg_dialog_destroy(dialog);
162 ui_destroy(ui);
163}
164
165static void test_dialog_button(ui_msg_dialog_t *dialog, void *arg,
166 unsigned bnum)
167{
168 test_cb_resp_t *resp = (test_cb_resp_t *) arg;
169
170 resp->button = true;
171 resp->bnum = bnum;
172}
173
174static void test_dialog_close(ui_msg_dialog_t *dialog, void *arg)
175{
176 test_cb_resp_t *resp = (test_cb_resp_t *) arg;
177
178 resp->close = true;
179}
180
181PCUT_EXPORT(msg_dialog);
Note: See TracBrowser for help on using the repository browser.