source: mainline/uspace/app/nav/test/menu.c@ 901b302

ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 901b302 was f59212cc, checked in by jxsvoboda <5887334+jxsvoboda@…>, 4 years ago

Add File / Open, properly deliver menu events to Navigator

  • Property mode set to 100644
File size: 4.1 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 <errno.h>
30#include <pcut/pcut.h>
31#include <stdbool.h>
32#include "../menu.h"
33
34PCUT_INIT;
35
36PCUT_TEST_SUITE(menu);
37
38static void test_menu_file_open(void *);
39static void test_menu_file_exit(void *);
40
41static nav_menu_cb_t dummy_cb;
42static nav_menu_cb_t test_cb = {
43 .file_open = test_menu_file_open,
44 .file_exit = test_menu_file_exit
45};
46
47/** Test response */
48typedef struct {
49 bool file_open;
50 bool file_exit;
51} test_resp_t;
52
53/** Create and destroy menu. */
54PCUT_TEST(create_destroy)
55{
56 ui_t *ui;
57 ui_window_t *window;
58 ui_wnd_params_t params;
59 nav_menu_t *menu;
60 errno_t rc;
61
62 rc = ui_create_disp(NULL, &ui);
63 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
64
65 ui_wnd_params_init(&params);
66 params.caption = "Test";
67
68 rc = ui_window_create(ui, &params, &window);
69 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
70
71 rc = nav_menu_create(window, &menu);
72 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
73
74 nav_menu_destroy(menu);
75 ui_window_destroy(window);
76 ui_destroy(ui);
77}
78
79/** nav_menu_set_cb() */
80PCUT_TEST(set_cb)
81{
82 ui_t *ui;
83 ui_window_t *window;
84 ui_wnd_params_t params;
85 nav_menu_t *menu;
86 int foo;
87 errno_t rc;
88
89 rc = ui_create_disp(NULL, &ui);
90 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
91
92 ui_wnd_params_init(&params);
93 params.caption = "Test";
94
95 rc = ui_window_create(ui, &params, &window);
96 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
97
98 rc = nav_menu_create(window, &menu);
99 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
100
101 nav_menu_set_cb(menu, &test_cb, &foo);
102 PCUT_ASSERT_EQUALS(&test_cb, menu->cb);
103 PCUT_ASSERT_EQUALS(&foo, menu->cb_arg);
104
105 nav_menu_destroy(menu);
106 ui_window_destroy(window);
107 ui_destroy(ui);
108}
109
110/** File / Open callback */
111PCUT_TEST(file_open)
112{
113 ui_t *ui;
114 ui_window_t *window;
115 ui_wnd_params_t params;
116 nav_menu_t *menu;
117 test_resp_t resp;
118 errno_t rc;
119
120 rc = ui_create_disp(NULL, &ui);
121 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
122
123 ui_wnd_params_init(&params);
124 params.caption = "Test";
125
126 rc = ui_window_create(ui, &params, &window);
127 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
128
129 rc = nav_menu_create(window, &menu);
130 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
131
132 /* Call back with no callbacks set */
133 nav_menu_file_open(NULL, menu);
134
135 /* Call back with dummy callbacks set */
136 nav_menu_set_cb(menu, &dummy_cb, &resp);
137 nav_menu_file_open(NULL, menu);
138
139 /* Call back with test callbacks set */
140 resp.file_open = false;
141 nav_menu_set_cb(menu, &test_cb, &resp);
142 nav_menu_file_open(NULL, menu);
143 PCUT_ASSERT_TRUE(resp.file_open);
144
145 nav_menu_destroy(menu);
146 ui_window_destroy(window);
147 ui_destroy(ui);
148}
149
150/** Testing File / Open callback */
151static void test_menu_file_open(void *arg)
152{
153 test_resp_t *resp = (test_resp_t *)arg;
154
155 resp->file_open = true;
156}
157
158/** Testing File / Exit callback */
159static void test_menu_file_exit(void *arg)
160{
161 test_resp_t *resp = (test_resp_t *)arg;
162
163 resp->file_exit = true;
164}
165
166PCUT_EXPORT(menu);
Note: See TracBrowser for help on using the repository browser.