2 * Copyright (c) 2021 Jiri Svoboda
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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>
31 #include <ui/pbutton.h>
33 #include <ui/promptdialog.h>
34 #include "../private/promptdialog.h"
35 #include "../private/window.h"
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
= {
61 /** Create and destroy prompt dialog */
62 PCUT_TEST(create_destroy
)
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(¶ms
);
73 params
.caption
= "Open";
75 rc
= ui_prompt_dialog_create(ui
, ¶ms
, &dialog
);
76 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
77 PCUT_ASSERT_NOT_NULL(dialog
);
79 ui_prompt_dialog_destroy(dialog
);
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() */
94 ui_prompt_dialog_params_t params
;
95 ui_prompt_dialog_t
*dialog
= NULL
;
98 rc
= ui_create_disp(NULL
, &ui
);
99 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
101 ui_prompt_dialog_params_init(¶ms
);
102 params
.caption
= "Open";
104 rc
= ui_prompt_dialog_create(ui
, ¶ms
, &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 */
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
);
125 /** Sending window close request invokes callback set via
126 * ui_prompt_dialog_set_cb()
132 ui_prompt_dialog_params_t params
;
133 ui_prompt_dialog_t
*dialog
= NULL
;
136 rc
= ui_create_disp(NULL
, &ui
);
137 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
139 ui_prompt_dialog_params_init(¶ms
);
140 params
.caption
= "Open";
142 rc
= ui_prompt_dialog_create(ui
, ¶ms
, &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 */
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
);
163 static void test_dialog_bok(ui_prompt_dialog_t
*dialog
, void *arg
,
166 test_cb_resp_t
*resp
= (test_cb_resp_t
*) arg
;
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
;
185 PCUT_EXPORT(prompt_dialog
);