Handle Enter/Escape keys in message dialog.
[helenos.git] / uspace / lib / ui / test / promptdialog.c
blob80cb90a9f9bfdca2ea19b5b512ddc8ef5803e058
1 /*
2 * Copyright (c) 2021 Jiri Svoboda
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
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.
29 #include <pcut/pcut.h>
30 #include <stdbool.h>
31 #include <ui/pbutton.h>
32 #include <ui/ui.h>
33 #include <ui/promptdialog.h>
34 #include "../private/promptdialog.h"
35 #include "../private/window.h"
37 PCUT_INIT;
39 PCUT_TEST_SUITE(prompt_dialog);
41 static void test_dialog_bok(ui_prompt_dialog_t *, void *, const char *);
42 static void test_dialog_bcancel(ui_prompt_dialog_t *, void *);
43 static void test_dialog_close(ui_prompt_dialog_t *, void *);
45 static ui_prompt_dialog_cb_t test_prompt_dialog_cb = {
46 .bok = test_dialog_bok,
47 .bcancel = test_dialog_bcancel,
48 .close = test_dialog_close
51 static ui_prompt_dialog_cb_t dummy_prompt_dialog_cb = {
54 typedef struct {
55 bool bok;
56 const char *fname;
57 bool bcancel;
58 bool close;
59 } test_cb_resp_t;
61 /** Create and destroy prompt dialog */
62 PCUT_TEST(create_destroy)
64 errno_t rc;
65 ui_t *ui = NULL;
66 ui_prompt_dialog_params_t params;
67 ui_prompt_dialog_t *dialog = NULL;
69 rc = ui_create_disp(NULL, &ui);
70 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
72 ui_prompt_dialog_params_init(&params);
73 params.caption = "Open";
75 rc = ui_prompt_dialog_create(ui, &params, &dialog);
76 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
77 PCUT_ASSERT_NOT_NULL(dialog);
79 ui_prompt_dialog_destroy(dialog);
80 ui_destroy(ui);
83 /** ui_prompt_dialog_destroy() can take NULL argument (no-op) */
84 PCUT_TEST(destroy_null)
86 ui_prompt_dialog_destroy(NULL);
89 /** Button click invokes callback set via ui_prompt_dialog_set_cb() */
90 PCUT_TEST(button_cb)
92 errno_t rc;
93 ui_t *ui = NULL;
94 ui_prompt_dialog_params_t params;
95 ui_prompt_dialog_t *dialog = NULL;
96 test_cb_resp_t resp;
98 rc = ui_create_disp(NULL, &ui);
99 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
101 ui_prompt_dialog_params_init(&params);
102 params.caption = "Open";
104 rc = ui_prompt_dialog_create(ui, &params, &dialog);
105 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
106 PCUT_ASSERT_NOT_NULL(dialog);
108 /* OK button callback with no callbacks set */
109 ui_pbutton_clicked(dialog->bok);
111 /* OK button callback with callback not implemented */
112 ui_prompt_dialog_set_cb(dialog, &dummy_prompt_dialog_cb, NULL);
113 ui_pbutton_clicked(dialog->bok);
115 /* OK button callback with real callback set */
116 resp.bok = false;
117 ui_prompt_dialog_set_cb(dialog, &test_prompt_dialog_cb, &resp);
118 ui_pbutton_clicked(dialog->bok);
119 PCUT_ASSERT_TRUE(resp.bok);
121 ui_prompt_dialog_destroy(dialog);
122 ui_destroy(ui);
125 /** Sending window close request invokes callback set via
126 * ui_prompt_dialog_set_cb()
128 PCUT_TEST(close_cb)
130 errno_t rc;
131 ui_t *ui = NULL;
132 ui_prompt_dialog_params_t params;
133 ui_prompt_dialog_t *dialog = NULL;
134 test_cb_resp_t resp;
136 rc = ui_create_disp(NULL, &ui);
137 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
139 ui_prompt_dialog_params_init(&params);
140 params.caption = "Open";
142 rc = ui_prompt_dialog_create(ui, &params, &dialog);
143 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
144 PCUT_ASSERT_NOT_NULL(dialog);
146 /* Button callback with no callbacks set */
147 ui_window_send_close(dialog->window);
149 /* Button callback with unfocus callback not implemented */
150 ui_prompt_dialog_set_cb(dialog, &dummy_prompt_dialog_cb, NULL);
151 ui_window_send_close(dialog->window);
153 /* Button callback with real callback set */
154 resp.close = false;
155 ui_prompt_dialog_set_cb(dialog, &test_prompt_dialog_cb, &resp);
156 ui_window_send_close(dialog->window);
157 PCUT_ASSERT_TRUE(resp.close);
159 ui_prompt_dialog_destroy(dialog);
160 ui_destroy(ui);
163 static void test_dialog_bok(ui_prompt_dialog_t *dialog, void *arg,
164 const char *fname)
166 test_cb_resp_t *resp = (test_cb_resp_t *) arg;
168 resp->bok = true;
171 static void test_dialog_bcancel(ui_prompt_dialog_t *dialog, void *arg)
173 test_cb_resp_t *resp = (test_cb_resp_t *) arg;
175 resp->bcancel = true;
178 static void test_dialog_close(ui_prompt_dialog_t *dialog, void *arg)
180 test_cb_resp_t *resp = (test_cb_resp_t *) arg;
182 resp->close = true;
185 PCUT_EXPORT(prompt_dialog);