12
|
1 |
/* -*- c-basic-offset: 4; -*- */
|
|
2 |
#define _POSIX_SOURCE
|
|
3 |
|
|
4 |
#include <gtk/gtk.h>
|
|
5 |
#include <gdk/gdkx.h>
|
|
6 |
#include <gdk/gdkkeysyms.h>
|
|
7 |
#include <sys/socket.h>
|
|
8 |
#include <sys/stat.h>
|
|
9 |
#include <sys/types.h>
|
|
10 |
#include <sys/un.h>
|
|
11 |
#include <sys/utsname.h>
|
|
12 |
#include <sys/time.h>
|
|
13 |
#include <webkit/webkit.h>
|
|
14 |
#include <libsoup/soup.h>
|
|
15 |
#include <JavaScriptCore/JavaScript.h>
|
|
16 |
|
|
17 |
#include <stdio.h>
|
|
18 |
#include <string.h>
|
|
19 |
#include <unistd.h>
|
|
20 |
#include <stdlib.h>
|
|
21 |
#include <errno.h>
|
|
22 |
#include <fcntl.h>
|
|
23 |
#include <signal.h>
|
|
24 |
|
|
25 |
#include <src/uzbl-core.h>
|
|
26 |
#include <src/config.h>
|
|
27 |
|
|
28 |
extern UzblCore uzbl;
|
|
29 |
|
|
30 |
#define INSTANCE_NAME "testing"
|
|
31 |
|
|
32 |
#define ASSERT_EVENT(EF, STR) { read_event(ef); \
|
|
33 |
g_assert_cmpstr("EVENT [" INSTANCE_NAME "] " STR "\n", ==, ef->event_buffer); }
|
|
34 |
|
|
35 |
struct EventFixture
|
|
36 |
{
|
|
37 |
/* uzbl's end of the socketpair */
|
|
38 |
int uzbl_sock;
|
|
39 |
|
|
40 |
/* the test framework's end of the socketpair */
|
|
41 |
int test_sock;
|
|
42 |
char event_buffer[1024];
|
|
43 |
};
|
|
44 |
|
|
45 |
void
|
|
46 |
read_event (struct EventFixture *ef) {
|
|
47 |
int r = read(ef->test_sock, ef->event_buffer, 1023); \
|
|
48 |
ef->event_buffer[r] = 0;
|
|
49 |
}
|
|
50 |
|
|
51 |
void
|
|
52 |
assert_no_event (struct EventFixture *ef) {
|
|
53 |
fd_set rfds;
|
|
54 |
|
|
55 |
FD_ZERO(&rfds);
|
|
56 |
FD_SET(ef->test_sock, &rfds);
|
|
57 |
|
|
58 |
struct timeval timeout;
|
|
59 |
timeout.tv_sec = 0;
|
|
60 |
timeout.tv_usec = 0;
|
|
61 |
|
|
62 |
/* check if there's any data waiting */
|
|
63 |
int res = select(ef->test_sock + 1, &rfds, NULL, NULL, &timeout);
|
|
64 |
|
|
65 |
if(res == 0) {
|
|
66 |
/* timeout expired, there was no event */
|
|
67 |
|
|
68 |
/* success */
|
|
69 |
return;
|
|
70 |
} else if(res == -1) {
|
|
71 |
/* mechanical failure */
|
|
72 |
perror("select():");
|
|
73 |
assert(0);
|
|
74 |
} else {
|
|
75 |
/* there was an event. display it. */
|
|
76 |
read_event(ef);
|
|
77 |
g_assert_cmpstr("", ==, ef->event_buffer);
|
|
78 |
}
|
|
79 |
}
|
|
80 |
|
|
81 |
void
|
|
82 |
event_fixture_setup(struct EventFixture *ef, const void* data)
|
|
83 |
{
|
|
84 |
(void) data;
|
|
85 |
|
|
86 |
int socks[2];
|
|
87 |
|
|
88 |
/* make some sockets, fresh for every test */
|
|
89 |
if(socketpair(AF_UNIX, SOCK_STREAM, 0, socks) == -1)
|
|
90 |
{
|
|
91 |
perror("socketpair() failed");
|
|
92 |
g_assert(0);
|
|
93 |
}
|
|
94 |
|
|
95 |
ef->uzbl_sock = socks[0];
|
|
96 |
ef->test_sock = socks[1];
|
|
97 |
|
|
98 |
/* attach uzbl_sock to uzbl's event dispatcher. */
|
|
99 |
GIOChannel *iochan = g_io_channel_unix_new(ef->uzbl_sock);
|
|
100 |
g_io_channel_set_encoding(iochan, NULL, NULL);
|
|
101 |
|
|
102 |
if(!uzbl.comm.connect_chan)
|
|
103 |
uzbl.comm.connect_chan = g_ptr_array_new();
|
|
104 |
if(!uzbl.comm.client_chan)
|
|
105 |
uzbl.comm.client_chan = g_ptr_array_new();
|
|
106 |
g_ptr_array_add(uzbl.comm.client_chan, (gpointer)iochan);
|
|
107 |
}
|
|
108 |
|
|
109 |
void
|
|
110 |
event_fixture_teardown(struct EventFixture *ef, const void *data)
|
|
111 |
{
|
|
112 |
(void) data;
|
|
113 |
|
|
114 |
/* there should be no events left waiting */
|
|
115 |
assert_no_event(ef);
|
|
116 |
|
|
117 |
/* clean up the io channel we opened for uzbl */
|
|
118 |
GIOChannel *iochan = g_ptr_array_index(uzbl.comm.client_chan, 0);
|
|
119 |
remove_socket_from_array(iochan);
|
|
120 |
|
|
121 |
/* close the sockets so that nothing sticks around between tests */
|
|
122 |
close(ef->uzbl_sock);
|
|
123 |
close(ef->test_sock);
|
|
124 |
}
|
|
125 |
|
|
126 |
/* actual tests begin here */
|
|
127 |
|
|
128 |
void
|
|
129 |
test_event (struct EventFixture *ef, const void *data) {
|
|
130 |
(void) data;
|
|
131 |
|
|
132 |
parse_cmd_line("event", NULL);
|
|
133 |
assert_no_event(ef);
|
|
134 |
|
|
135 |
/* a simple event can be sent */
|
|
136 |
parse_cmd_line("event event_type arg u ments", NULL);
|
|
137 |
ASSERT_EVENT(ef, "EVENT_TYPE arg u ments");
|
|
138 |
|
|
139 |
/* arguments to event should be expanded */
|
|
140 |
parse_cmd_line("event event_type @(echo expansion)@ test", NULL);
|
|
141 |
ASSERT_EVENT(ef, "EVENT_TYPE expansion test");
|
|
142 |
|
|
143 |
/* "request" is just an alias for "event" */
|
|
144 |
parse_cmd_line("request event_type arg u ments", NULL);
|
|
145 |
ASSERT_EVENT(ef, "EVENT_TYPE arg u ments");
|
|
146 |
}
|
|
147 |
|
|
148 |
|
|
149 |
void
|
|
150 |
test_set_variable (struct EventFixture *ef, const void *data) {
|
|
151 |
(void) data;
|
|
152 |
|
|
153 |
/* set a string */
|
|
154 |
parse_cmd_line("set useragent = Uzbl browser kthxbye!", NULL);
|
|
155 |
ASSERT_EVENT(ef, "VARIABLE_SET useragent str 'Uzbl browser kthxbye!'");
|
|
156 |
g_assert_cmpstr("Uzbl browser kthxbye!", ==, uzbl.net.useragent);
|
|
157 |
|
|
158 |
/* set an int */
|
|
159 |
parse_cmd_line("set forward_keys = 0", NULL);
|
|
160 |
ASSERT_EVENT(ef, "VARIABLE_SET forward_keys int 0");
|
|
161 |
g_assert_cmpint(0, ==, uzbl.behave.forward_keys);
|
|
162 |
|
|
163 |
/* set a float */
|
|
164 |
/* we have to be careful about locales here */
|
|
165 |
GString *cmd, *ev;
|
|
166 |
cmd = g_string_new("set zoom_level = ");
|
|
167 |
g_string_append_printf(cmd, "%f", 0.25);
|
|
168 |
parse_cmd_line(g_string_free(cmd, FALSE), NULL);
|
|
169 |
|
|
170 |
ev = g_string_new("EVENT [" INSTANCE_NAME "] VARIABLE_SET zoom_level float ");
|
|
171 |
g_string_append_printf(ev, "%.2f\n", 0.25);
|
|
172 |
read_event(ef);
|
|
173 |
g_assert_cmpstr(g_string_free(ev, FALSE), ==, ef->event_buffer);
|
|
174 |
|
|
175 |
g_assert_cmpfloat(0.25, ==, uzbl.behave.zoom_level);
|
|
176 |
|
|
177 |
/* set a constant int (nothing should happen) */
|
|
178 |
int old_major = uzbl.info.webkit_major;
|
|
179 |
parse_cmd_line("set WEBKIT_MAJOR = 100", NULL);
|
|
180 |
assert_no_event(ef);
|
|
181 |
g_assert_cmpint(old_major, ==, uzbl.info.webkit_major);
|
|
182 |
|
|
183 |
/* set a constant str (nothing should happen) */
|
|
184 |
GString *old_arch = g_string_new(uzbl.info.arch);
|
|
185 |
parse_cmd_line("set ARCH_UZBL = A Lisp Machine", NULL);
|
|
186 |
assert_no_event(ef);
|
|
187 |
g_assert_cmpstr(g_string_free(old_arch, FALSE), ==, uzbl.info.arch);
|
|
188 |
|
|
189 |
/* set a custom variable */
|
|
190 |
parse_cmd_line("set nonexistant_variable = Some Value", NULL);
|
|
191 |
ASSERT_EVENT(ef, "VARIABLE_SET nonexistant_variable str 'Some Value'");
|
|
192 |
uzbl_cmdprop *c = g_hash_table_lookup(uzbl.comm.proto_var, "nonexistant_variable");
|
|
193 |
g_assert_cmpstr("Some Value", ==, *c->ptr.s);
|
|
194 |
|
|
195 |
/* set a custom variable with expansion */
|
|
196 |
parse_cmd_line("set an_expanded_variable = Test @(echo expansion)@", NULL);
|
|
197 |
ASSERT_EVENT(ef, "VARIABLE_SET an_expanded_variable str 'Test expansion'");
|
|
198 |
c = g_hash_table_lookup(uzbl.comm.proto_var, "an_expanded_variable");
|
|
199 |
g_assert_cmpstr("Test expansion", ==, *c->ptr.s);
|
|
200 |
}
|
|
201 |
|
|
202 |
void
|
|
203 |
test_print (void) {
|
|
204 |
GString *result = g_string_new("");
|
|
205 |
|
|
206 |
/* a simple message can be returned as a result */
|
|
207 |
parse_cmd_line("print A simple test", result);
|
|
208 |
g_assert_cmpstr("A simple test", ==, result->str);
|
|
209 |
|
|
210 |
/* arguments to print should be expanded */
|
|
211 |
parse_cmd_line("print A simple @(echo expansion)@ test", result);
|
|
212 |
g_assert_cmpstr("A simple expansion test", ==, result->str);
|
|
213 |
|
|
214 |
g_string_free(result, TRUE);
|
|
215 |
}
|
|
216 |
|
|
217 |
void
|
|
218 |
test_scroll (void) {
|
|
219 |
uzbl.gui.scbar_v = (GtkScrollbar*) gtk_vscrollbar_new (NULL);
|
|
220 |
uzbl.gui.bar_v = gtk_range_get_adjustment((GtkRange*) uzbl.gui.scbar_v);
|
|
221 |
|
|
222 |
gtk_adjustment_set_lower(uzbl.gui.bar_v, 0);
|
|
223 |
gtk_adjustment_set_upper(uzbl.gui.bar_v, 100);
|
|
224 |
gtk_adjustment_set_page_size(uzbl.gui.bar_v, 5);
|
|
225 |
|
|
226 |
/* scroll vertical end should scroll it to upper - page_size */
|
|
227 |
parse_cmd_line("scroll vertical end", NULL);
|
|
228 |
g_assert_cmpfloat(gtk_adjustment_get_value(uzbl.gui.bar_v), ==, 95);
|
|
229 |
|
|
230 |
/* scroll vertical begin should scroll it to lower */
|
|
231 |
parse_cmd_line("scroll vertical begin", NULL);
|
|
232 |
g_assert_cmpfloat(gtk_adjustment_get_value(uzbl.gui.bar_v), ==, 0);
|
|
233 |
|
|
234 |
/* scroll vertical can scroll by pixels */
|
|
235 |
parse_cmd_line("scroll vertical 15", NULL);
|
|
236 |
g_assert_cmpfloat(gtk_adjustment_get_value(uzbl.gui.bar_v), ==, 15);
|
|
237 |
|
|
238 |
parse_cmd_line("scroll vertical -10", NULL);
|
|
239 |
g_assert_cmpfloat(gtk_adjustment_get_value(uzbl.gui.bar_v), ==, 5);
|
|
240 |
|
|
241 |
/* scroll vertical can scroll by a percentage of the page size */
|
|
242 |
parse_cmd_line("scroll vertical 100%", NULL);
|
|
243 |
g_assert_cmpfloat(gtk_adjustment_get_value(uzbl.gui.bar_v), ==, 10);
|
|
244 |
|
|
245 |
parse_cmd_line("scroll vertical 150%", NULL);
|
|
246 |
g_assert_cmpfloat(gtk_adjustment_get_value(uzbl.gui.bar_v), ==, 17.5);
|
|
247 |
|
|
248 |
/* scroll_horz behaves basically the same way. */
|
|
249 |
}
|
|
250 |
|
|
251 |
void
|
|
252 |
test_toggle_status (void) {
|
|
253 |
g_assert(!uzbl.behave.show_status);
|
|
254 |
|
|
255 |
/* status bar can be toggled on */
|
|
256 |
parse_cmd_line("toggle_status", NULL);
|
|
257 |
g_assert(uzbl.behave.show_status);
|
|
258 |
|
|
259 |
/* status bar can be toggled back off */
|
|
260 |
parse_cmd_line("toggle_status", NULL);
|
|
261 |
g_assert(!uzbl.behave.show_status);
|
|
262 |
}
|
|
263 |
|
|
264 |
void
|
|
265 |
test_sync_sh (void) {
|
|
266 |
GString *result = g_string_new("");
|
|
267 |
|
|
268 |
parse_cmd_line("sync_sh 'echo Test echo.'", result);
|
|
269 |
g_assert_cmpstr("Test echo.\n", ==, result->str);
|
|
270 |
|
|
271 |
g_string_free(result, TRUE);
|
|
272 |
}
|
|
273 |
|
|
274 |
void
|
|
275 |
test_js (void) {
|
|
276 |
GString *result = g_string_new("");
|
|
277 |
|
|
278 |
/* simple javascript can be evaluated and returned */
|
|
279 |
parse_cmd_line("js ('x' + 345).toUpperCase()", result);
|
|
280 |
g_assert_cmpstr("X345", ==, result->str);
|
|
281 |
|
|
282 |
g_string_free(result, TRUE);
|
|
283 |
}
|
|
284 |
|
|
285 |
void test_uri(void) {
|
|
286 |
/* Testing for a crash, not crashing is a pass */
|
|
287 |
parse_cmd_line("uri", NULL);
|
|
288 |
}
|
|
289 |
|
|
290 |
void
|
|
291 |
test_last_result (void) {
|
|
292 |
GString *result = g_string_new("");
|
|
293 |
|
|
294 |
/* the last result gets set */
|
|
295 |
parse_cmd_line("js -1", result);
|
|
296 |
g_assert_cmpstr("-1", ==, uzbl.state.last_result);
|
|
297 |
|
|
298 |
/* the last result can be used in a chain */
|
|
299 |
parse_cmd_line("chain 'js 1' 'js \\@_ + 1'", result);
|
|
300 |
g_assert_cmpstr("2", ==, uzbl.state.last_result);
|
|
301 |
|
|
302 |
g_string_free(result, TRUE);
|
|
303 |
}
|
|
304 |
|
|
305 |
void
|
|
306 |
test_no_such_command (void) {
|
|
307 |
parse_cmd_line("no-such-command", NULL);
|
|
308 |
/* if we didn't crash then we're ok! */
|
|
309 |
}
|
|
310 |
|
|
311 |
int
|
|
312 |
main (int argc, char *argv[]) {
|
|
313 |
/* set up tests */
|
|
314 |
g_type_init();
|
|
315 |
g_test_init(&argc, &argv, NULL);
|
|
316 |
|
|
317 |
g_test_add("/test-command/set-variable", struct EventFixture, NULL, event_fixture_setup, test_set_variable, event_fixture_teardown);
|
|
318 |
g_test_add("/test-command/event", struct EventFixture, NULL, event_fixture_setup, test_event, event_fixture_teardown);
|
|
319 |
|
|
320 |
g_test_add_func("/test-command/print", test_print);
|
|
321 |
g_test_add_func("/test-command/uri", test_uri);
|
|
322 |
g_test_add_func("/test-command/scroll", test_scroll);
|
|
323 |
g_test_add_func("/test-command/toggle-status", test_toggle_status);
|
|
324 |
g_test_add_func("/test-command/sync-sh", test_sync_sh);
|
|
325 |
|
|
326 |
g_test_add_func("/test-command/js", test_js);
|
|
327 |
|
|
328 |
g_test_add_func("/test-command/last-result", test_last_result);
|
|
329 |
|
|
330 |
g_test_add_func("/test-command/no-such-command", test_no_such_command);
|
|
331 |
|
|
332 |
/* set up uzbl */
|
|
333 |
initialize(argc, argv);
|
|
334 |
|
|
335 |
uzbl.state.config_file = "/tmp/uzbl-config";
|
|
336 |
uzbl.comm.fifo_path = "/tmp/some-nonexistant-fifo";
|
|
337 |
uzbl.comm.socket_path = "/tmp/some-nonexistant-socket";
|
|
338 |
uzbl.state.uri = g_strdup("http://example.org/");
|
|
339 |
uzbl.gui.main_title = "Example.Org";
|
|
340 |
|
|
341 |
uzbl.state.instance_name = INSTANCE_NAME;
|
|
342 |
uzbl.behave.shell_cmd = "sh -c";
|
|
343 |
|
|
344 |
return g_test_run();
|
|
345 |
}
|
|
346 |
|
|
347 |
/* vi: set et ts=4: */
|