1 | /*
|
---|
2 | * Copyright (c) 2019 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 <str.h>
|
---|
33 |
|
---|
34 | #include "../client.h"
|
---|
35 | #include "../display.h"
|
---|
36 | #include "../window.h"
|
---|
37 |
|
---|
38 | PCUT_INIT;
|
---|
39 |
|
---|
40 | PCUT_TEST_SUITE(client);
|
---|
41 |
|
---|
42 | static void test_ds_ev_pending(void *);
|
---|
43 |
|
---|
44 | static ds_client_cb_t test_ds_client_cb = {
|
---|
45 | .ev_pending = test_ds_ev_pending
|
---|
46 | };
|
---|
47 |
|
---|
48 | static void test_ds_ev_pending(void *arg)
|
---|
49 | {
|
---|
50 | bool *called_cb = (bool *) arg;
|
---|
51 | printf("test_ds_ev_pending\n");
|
---|
52 | *called_cb = true;
|
---|
53 |
|
---|
54 | }
|
---|
55 |
|
---|
56 | /** Client creation and destruction. */
|
---|
57 | PCUT_TEST(client_create_destroy)
|
---|
58 | {
|
---|
59 | ds_display_t *disp;
|
---|
60 | ds_client_t *client;
|
---|
61 | errno_t rc;
|
---|
62 |
|
---|
63 | rc = ds_display_create(NULL, &disp);
|
---|
64 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
65 |
|
---|
66 | rc = ds_client_create(disp, &test_ds_client_cb, NULL, &client);
|
---|
67 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
68 |
|
---|
69 | ds_client_destroy(client);
|
---|
70 | ds_display_destroy(disp);
|
---|
71 | }
|
---|
72 |
|
---|
73 | /** Test ds_client_find_window().
|
---|
74 | *
|
---|
75 | * ds_client_add_window() and ds_client_remove_window() are indirectly
|
---|
76 | * tested too as part of creating and destroying the window
|
---|
77 | */
|
---|
78 | PCUT_TEST(client_find_window)
|
---|
79 | {
|
---|
80 | ds_display_t *disp;
|
---|
81 | ds_client_t *client;
|
---|
82 | ds_window_t *w0;
|
---|
83 | ds_window_t *w1;
|
---|
84 | ds_window_t *wnd;
|
---|
85 | errno_t rc;
|
---|
86 |
|
---|
87 | rc = ds_display_create(NULL, &disp);
|
---|
88 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
89 |
|
---|
90 | rc = ds_client_create(disp, &test_ds_client_cb, NULL, &client);
|
---|
91 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
92 |
|
---|
93 | rc = ds_window_create(client, &w0);
|
---|
94 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
95 |
|
---|
96 | rc = ds_window_create(client, &w1);
|
---|
97 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
98 |
|
---|
99 | wnd = ds_client_find_window(client, w0->id);
|
---|
100 | PCUT_ASSERT_EQUALS(w0, wnd);
|
---|
101 |
|
---|
102 | wnd = ds_client_find_window(client, w1->id);
|
---|
103 | PCUT_ASSERT_EQUALS(w1, wnd);
|
---|
104 |
|
---|
105 | wnd = ds_client_find_window(client, 0);
|
---|
106 | PCUT_ASSERT_NULL(wnd);
|
---|
107 |
|
---|
108 | wnd = ds_client_find_window(client, w1->id + 1);
|
---|
109 | PCUT_ASSERT_NULL(wnd);
|
---|
110 |
|
---|
111 | ds_window_destroy(w0);
|
---|
112 | ds_window_destroy(w1);
|
---|
113 | ds_client_destroy(client);
|
---|
114 | ds_display_destroy(disp);
|
---|
115 | }
|
---|
116 |
|
---|
117 | /** Test ds_client_first_window() / ds_client_next_window. */
|
---|
118 | PCUT_TEST(client_first_next_window)
|
---|
119 | {
|
---|
120 | ds_display_t *disp;
|
---|
121 | ds_client_t *client;
|
---|
122 | ds_window_t *w0;
|
---|
123 | ds_window_t *w1;
|
---|
124 | ds_window_t *wnd;
|
---|
125 | errno_t rc;
|
---|
126 |
|
---|
127 | rc = ds_display_create(NULL, &disp);
|
---|
128 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
129 |
|
---|
130 | rc = ds_client_create(disp, &test_ds_client_cb, NULL, &client);
|
---|
131 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
132 |
|
---|
133 | rc = ds_window_create(client, &w0);
|
---|
134 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
135 |
|
---|
136 | rc = ds_window_create(client, &w1);
|
---|
137 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
138 |
|
---|
139 | wnd = ds_client_first_window(client);
|
---|
140 | PCUT_ASSERT_EQUALS(w0, wnd);
|
---|
141 |
|
---|
142 | wnd = ds_client_next_window(w0);
|
---|
143 | PCUT_ASSERT_EQUALS(w1, wnd);
|
---|
144 |
|
---|
145 | wnd = ds_client_next_window(w1);
|
---|
146 | PCUT_ASSERT_NULL(wnd);
|
---|
147 |
|
---|
148 | ds_window_destroy(w0);
|
---|
149 | ds_window_destroy(w1);
|
---|
150 | ds_client_destroy(client);
|
---|
151 | ds_display_destroy(disp);
|
---|
152 | }
|
---|
153 |
|
---|
154 | /** Test ds_client_get_event(), ds_client_post_kbd_event(). */
|
---|
155 | PCUT_TEST(display_get_post_kbd_event)
|
---|
156 | {
|
---|
157 | ds_display_t *disp;
|
---|
158 | ds_client_t *client;
|
---|
159 | ds_window_t *wnd;
|
---|
160 | kbd_event_t event;
|
---|
161 | ds_window_t *rwindow;
|
---|
162 | display_wnd_ev_t revent;
|
---|
163 | bool called_cb = NULL;
|
---|
164 | errno_t rc;
|
---|
165 |
|
---|
166 | rc = ds_display_create(NULL, &disp);
|
---|
167 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
168 |
|
---|
169 | rc = ds_client_create(disp, &test_ds_client_cb, &called_cb, &client);
|
---|
170 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
171 |
|
---|
172 | rc = ds_window_create(client, &wnd);
|
---|
173 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
174 |
|
---|
175 | event.type = KEY_PRESS;
|
---|
176 | event.key = KC_ENTER;
|
---|
177 | event.mods = 0;
|
---|
178 | event.c = L'\0';
|
---|
179 |
|
---|
180 | PCUT_ASSERT_FALSE(called_cb);
|
---|
181 |
|
---|
182 | #if 0
|
---|
183 | // XXX Forgot to change ds_client_get_event not to block
|
---|
184 | rc = ds_client_get_event(client, &rwindow, &revent);
|
---|
185 | PCUT_ASSERT_ERRNO_VAL(ENOENT, rc);
|
---|
186 | #endif
|
---|
187 |
|
---|
188 | rc = ds_client_post_kbd_event(client, wnd, &event);
|
---|
189 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
190 | PCUT_ASSERT_TRUE(called_cb);
|
---|
191 |
|
---|
192 | rc = ds_client_get_event(client, &rwindow, &revent);
|
---|
193 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
194 | PCUT_ASSERT_EQUALS(wnd, rwindow);
|
---|
195 | PCUT_ASSERT_EQUALS(event.type, revent.kbd_event.type);
|
---|
196 | PCUT_ASSERT_EQUALS(event.key, revent.kbd_event.key);
|
---|
197 | PCUT_ASSERT_EQUALS(event.mods, revent.kbd_event.mods);
|
---|
198 | PCUT_ASSERT_EQUALS(event.c, revent.kbd_event.c);
|
---|
199 |
|
---|
200 | ds_window_destroy(wnd);
|
---|
201 | ds_client_destroy(client);
|
---|
202 | ds_display_destroy(disp);
|
---|
203 | }
|
---|
204 |
|
---|
205 | PCUT_EXPORT(client);
|
---|