[80d4aea] | 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/promptdialog.h>
|
---|
| 34 | #include "../private/promptdialog.h"
|
---|
| 35 | #include "../private/window.h"
|
---|
| 36 |
|
---|
| 37 | PCUT_INIT;
|
---|
| 38 |
|
---|
| 39 | PCUT_TEST_SUITE(prompt_dialog);
|
---|
| 40 |
|
---|
| 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 *);
|
---|
| 44 |
|
---|
| 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
|
---|
| 49 | };
|
---|
| 50 |
|
---|
| 51 | static ui_prompt_dialog_cb_t dummy_prompt_dialog_cb = {
|
---|
| 52 | };
|
---|
| 53 |
|
---|
| 54 | typedef struct {
|
---|
| 55 | bool bok;
|
---|
| 56 | const char *fname;
|
---|
| 57 | bool bcancel;
|
---|
| 58 | bool close;
|
---|
| 59 | } test_cb_resp_t;
|
---|
| 60 |
|
---|
| 61 | /** Create and destroy prompt dialog */
|
---|
| 62 | PCUT_TEST(create_destroy)
|
---|
| 63 | {
|
---|
| 64 | errno_t rc;
|
---|
| 65 | ui_t *ui = NULL;
|
---|
| 66 | ui_prompt_dialog_params_t params;
|
---|
| 67 | ui_prompt_dialog_t *dialog = NULL;
|
---|
| 68 |
|
---|
| 69 | rc = ui_create_disp(NULL, &ui);
|
---|
| 70 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 71 |
|
---|
| 72 | ui_prompt_dialog_params_init(¶ms);
|
---|
| 73 | params.caption = "Open";
|
---|
| 74 |
|
---|
| 75 | rc = ui_prompt_dialog_create(ui, ¶ms, &dialog);
|
---|
| 76 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 77 | PCUT_ASSERT_NOT_NULL(dialog);
|
---|
| 78 |
|
---|
| 79 | ui_prompt_dialog_destroy(dialog);
|
---|
| 80 | ui_destroy(ui);
|
---|
| 81 | }
|
---|
| 82 |
|
---|
| 83 | /** ui_prompt_dialog_destroy() can take NULL argument (no-op) */
|
---|
| 84 | PCUT_TEST(destroy_null)
|
---|
| 85 | {
|
---|
| 86 | ui_prompt_dialog_destroy(NULL);
|
---|
| 87 | }
|
---|
| 88 |
|
---|
| 89 | /** Button click invokes callback set via ui_prompt_dialog_set_cb() */
|
---|
| 90 | PCUT_TEST(button_cb)
|
---|
| 91 | {
|
---|
| 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;
|
---|
| 97 |
|
---|
| 98 | rc = ui_create_disp(NULL, &ui);
|
---|
| 99 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 100 |
|
---|
| 101 | ui_prompt_dialog_params_init(¶ms);
|
---|
| 102 | params.caption = "Open";
|
---|
| 103 |
|
---|
| 104 | rc = ui_prompt_dialog_create(ui, ¶ms, &dialog);
|
---|
| 105 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 106 | PCUT_ASSERT_NOT_NULL(dialog);
|
---|
| 107 |
|
---|
| 108 | /* OK button callback with no callbacks set */
|
---|
| 109 | ui_pbutton_clicked(dialog->bok);
|
---|
| 110 |
|
---|
| 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);
|
---|
| 114 |
|
---|
| 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);
|
---|
| 120 |
|
---|
| 121 | ui_prompt_dialog_destroy(dialog);
|
---|
| 122 | ui_destroy(ui);
|
---|
| 123 | }
|
---|
| 124 |
|
---|
| 125 | /** Sending window close request invokes callback set via
|
---|
| 126 | * ui_prompt_dialog_set_cb()
|
---|
| 127 | */
|
---|
| 128 | PCUT_TEST(close_cb)
|
---|
| 129 | {
|
---|
| 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;
|
---|
| 135 |
|
---|
| 136 | rc = ui_create_disp(NULL, &ui);
|
---|
| 137 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 138 |
|
---|
| 139 | ui_prompt_dialog_params_init(¶ms);
|
---|
| 140 | params.caption = "Open";
|
---|
| 141 |
|
---|
| 142 | rc = ui_prompt_dialog_create(ui, ¶ms, &dialog);
|
---|
| 143 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 144 | PCUT_ASSERT_NOT_NULL(dialog);
|
---|
| 145 |
|
---|
| 146 | /* Button callback with no callbacks set */
|
---|
| 147 | ui_window_send_close(dialog->window);
|
---|
| 148 |
|
---|
| 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);
|
---|
| 152 |
|
---|
| 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);
|
---|
| 158 |
|
---|
| 159 | ui_prompt_dialog_destroy(dialog);
|
---|
| 160 | ui_destroy(ui);
|
---|
| 161 | }
|
---|
| 162 |
|
---|
| 163 | static void test_dialog_bok(ui_prompt_dialog_t *dialog, void *arg,
|
---|
| 164 | const char *fname)
|
---|
| 165 | {
|
---|
| 166 | test_cb_resp_t *resp = (test_cb_resp_t *) arg;
|
---|
| 167 |
|
---|
| 168 | resp->bok = true;
|
---|
| 169 | }
|
---|
| 170 |
|
---|
| 171 | static void test_dialog_bcancel(ui_prompt_dialog_t *dialog, void *arg)
|
---|
| 172 | {
|
---|
| 173 | test_cb_resp_t *resp = (test_cb_resp_t *) arg;
|
---|
| 174 |
|
---|
| 175 | resp->bcancel = true;
|
---|
| 176 | }
|
---|
| 177 |
|
---|
| 178 | static void test_dialog_close(ui_prompt_dialog_t *dialog, void *arg)
|
---|
| 179 | {
|
---|
| 180 | test_cb_resp_t *resp = (test_cb_resp_t *) arg;
|
---|
| 181 |
|
---|
| 182 | resp->close = true;
|
---|
| 183 | }
|
---|
| 184 |
|
---|
| 185 | PCUT_EXPORT(prompt_dialog);
|
---|