source: mainline/uspace/app/nav/test/panel.c@ 0e80e40

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

Read and display directory contents

  • Property mode set to 100644
File size: 7.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 <errno.h>
30#include <pcut/pcut.h>
31#include <stdio.h>
32#include <vfs/vfs.h>
33#include "../panel.h"
34
35PCUT_INIT;
36
37PCUT_TEST_SUITE(panel);
38
39/** Create and destroy panel. */
40PCUT_TEST(create_destroy)
41{
42 panel_t *panel;
43 errno_t rc;
44
45 rc = panel_create(NULL, &panel);
46 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
47
48 panel_destroy(panel);
49}
50
51/** Test panel_paint() */
52PCUT_TEST(paint)
53{
54 ui_t *ui;
55 ui_window_t *window;
56 ui_wnd_params_t params;
57 panel_t *panel;
58 errno_t rc;
59
60 rc = ui_create_disp(NULL, &ui);
61 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
62
63 ui_wnd_params_init(&params);
64 params.caption = "Test";
65
66 rc = ui_window_create(ui, &params, &window);
67 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
68
69 rc = panel_create(window, &panel);
70 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
71
72 rc = panel_paint(panel);
73 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
74
75 panel_destroy(panel);
76 ui_window_destroy(window);
77 ui_destroy(ui);
78}
79
80/** panel_ctl() returns a valid UI control */
81PCUT_TEST(ctl)
82{
83 panel_t *panel;
84 ui_control_t *control;
85 errno_t rc;
86
87 rc = panel_create(NULL, &panel);
88 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
89
90 control = panel_ctl(panel);
91 PCUT_ASSERT_NOT_NULL(control);
92
93 panel_destroy(panel);
94}
95
96/** Test panel_pos_event() */
97PCUT_TEST(pos_event)
98{
99 panel_t *panel;
100 ui_control_t *control;
101 ui_evclaim_t claimed;
102 pos_event_t event;
103 errno_t rc;
104
105 rc = panel_create(NULL, &panel);
106 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
107
108 control = panel_ctl(panel);
109 PCUT_ASSERT_NOT_NULL(control);
110
111 claimed = panel_pos_event(panel, &event);
112 PCUT_ASSERT_EQUALS(ui_unclaimed, claimed);
113
114 panel_destroy(panel);
115}
116
117/** panel_set_rect() sets internal field */
118PCUT_TEST(set_rect)
119{
120 panel_t *panel;
121 ui_control_t *control;
122 gfx_rect_t rect;
123 errno_t rc;
124
125 rc = panel_create(NULL, &panel);
126 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
127
128 control = panel_ctl(panel);
129 PCUT_ASSERT_NOT_NULL(control);
130
131 rect.p0.x = 1;
132 rect.p0.y = 2;
133 rect.p1.x = 3;
134 rect.p1.y = 4;
135
136 panel_set_rect(panel, &rect);
137 PCUT_ASSERT_INT_EQUALS(rect.p0.x, panel->rect.p0.x);
138 PCUT_ASSERT_INT_EQUALS(rect.p0.y, panel->rect.p0.y);
139 PCUT_ASSERT_INT_EQUALS(rect.p1.x, panel->rect.p1.x);
140 PCUT_ASSERT_INT_EQUALS(rect.p1.y, panel->rect.p1.y);
141
142 panel_destroy(panel);
143}
144
145/** panel_entry_append() appends new entry */
146PCUT_TEST(entry_append)
147{
148 panel_t *panel;
149 errno_t rc;
150
151 rc = panel_create(NULL, &panel);
152 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
153
154 rc = panel_entry_append(panel, "a", 1);
155 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
156
157 PCUT_ASSERT_INT_EQUALS(1, list_count(&panel->entries));
158
159 rc = panel_entry_append(panel, "b", 2);
160 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
161
162 PCUT_ASSERT_INT_EQUALS(2, list_count(&panel->entries));
163
164 panel_destroy(panel);
165}
166
167/** panel_entry_delete() deletes entry */
168PCUT_TEST(entry_delete)
169{
170 panel_t *panel;
171 panel_entry_t *entry;
172 errno_t rc;
173
174 rc = panel_create(NULL, &panel);
175 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
176
177 rc = panel_entry_append(panel, "a", 1);
178 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
179
180 rc = panel_entry_append(panel, "b", 2);
181 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
182
183 PCUT_ASSERT_INT_EQUALS(2, list_count(&panel->entries));
184
185 entry = panel_first(panel);
186 panel_entry_delete(entry);
187
188 PCUT_ASSERT_INT_EQUALS(1, list_count(&panel->entries));
189
190 entry = panel_first(panel);
191 panel_entry_delete(entry);
192
193 PCUT_ASSERT_INT_EQUALS(0, list_count(&panel->entries));
194
195 panel_destroy(panel);
196}
197
198/** panel_clear_entries() removes all entries from panel */
199PCUT_TEST(clear_entries)
200{
201 panel_t *panel;
202 errno_t rc;
203
204 rc = panel_create(NULL, &panel);
205 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
206
207 rc = panel_entry_append(panel, "a", 1);
208 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
209
210 rc = panel_entry_append(panel, "b", 2);
211 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
212
213 PCUT_ASSERT_INT_EQUALS(2, list_count(&panel->entries));
214
215 panel_clear_entries(panel);
216 PCUT_ASSERT_INT_EQUALS(0, list_count(&panel->entries));
217
218 panel_destroy(panel);
219}
220
221/** panel_read_dir() reads the contents of a directory */
222PCUT_TEST(read_dir)
223{
224 panel_t *panel;
225 panel_entry_t *entry;
226 char buf[L_tmpnam];
227 char *fname;
228 char *p;
229 errno_t rc;
230 FILE *f;
231 int rv;
232
233 /* Create name for temporary directory */
234 p = tmpnam(buf);
235 PCUT_ASSERT_NOT_NULL(p);
236
237 /* Create temporary directory */
238 rc = vfs_link_path(p, KIND_DIRECTORY, NULL);
239 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
240
241 rv = asprintf(&fname, "%s/%s", p, "a");
242 PCUT_ASSERT_TRUE(rv >= 0);
243
244 f = fopen(fname, "wb");
245 PCUT_ASSERT_NOT_NULL(f);
246
247 rv = fprintf(f, "X");
248 PCUT_ASSERT_TRUE(rv >= 0);
249
250 rv = fclose(f);
251 PCUT_ASSERT_INT_EQUALS(0, rv);
252
253 rc = panel_create(NULL, &panel);
254 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
255
256 rc = panel_read_dir(panel, p);
257 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
258
259 PCUT_ASSERT_INT_EQUALS(1, list_count(&panel->entries));
260
261 entry = panel_first(panel);
262 PCUT_ASSERT_NOT_NULL(entry);
263 PCUT_ASSERT_STR_EQUALS("a", entry->name);
264 // PCUT_ASSERT_INT_EQUALS(1, entry->size);
265
266 panel_destroy(panel);
267
268 rv = remove(fname);
269 PCUT_ASSERT_INT_EQUALS(0, rv);
270
271 rv = remove(p);
272 PCUT_ASSERT_INT_EQUALS(0, rv);
273 free(fname);
274}
275
276/** panel_first() returns valid entry or @c NULL as appropriate */
277PCUT_TEST(first)
278{
279 panel_t *panel;
280 panel_entry_t *entry;
281 errno_t rc;
282
283 rc = panel_create(NULL, &panel);
284 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
285
286 entry = panel_first(panel);
287 PCUT_ASSERT_NULL(entry);
288
289 /* Add one entry */
290 rc = panel_entry_append(panel, "a", 1);
291 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
292
293 /* Now try getting it */
294 entry = panel_first(panel);
295 PCUT_ASSERT_NOT_NULL(entry);
296 PCUT_ASSERT_STR_EQUALS("a", entry->name);
297 PCUT_ASSERT_INT_EQUALS(1, entry->size);
298
299 /* Add another entry */
300 rc = panel_entry_append(panel, "b", 2);
301 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
302
303 /* We should still get the first entry */
304 entry = panel_first(panel);
305 PCUT_ASSERT_NOT_NULL(entry);
306 PCUT_ASSERT_STR_EQUALS("a", entry->name);
307 PCUT_ASSERT_INT_EQUALS(1, entry->size);
308
309 panel_destroy(panel);
310}
311
312/** panel_next() returns the next entry or @c NULL as appropriate */
313PCUT_TEST(next)
314{
315 panel_t *panel;
316 panel_entry_t *entry;
317 errno_t rc;
318
319 rc = panel_create(NULL, &panel);
320 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
321
322 /* Add one entry */
323 rc = panel_entry_append(panel, "a", 1);
324 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
325
326 /* Now try getting its successor */
327 entry = panel_first(panel);
328 PCUT_ASSERT_NOT_NULL(entry);
329
330 entry = panel_next(entry);
331 PCUT_ASSERT_NULL(entry);
332
333 /* Add another entry */
334 rc = panel_entry_append(panel, "b", 2);
335 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
336
337 /* Try getting the successor of first entry again */
338 entry = panel_first(panel);
339 PCUT_ASSERT_NOT_NULL(entry);
340
341 entry = panel_next(entry);
342 PCUT_ASSERT_NOT_NULL(entry);
343 PCUT_ASSERT_STR_EQUALS("b", entry->name);
344 PCUT_ASSERT_INT_EQUALS(2, entry->size);
345
346 panel_destroy(panel);
347}
348
349PCUT_EXPORT(panel);
Note: See TracBrowser for help on using the repository browser.