From 229bb6931b278d455a35359db8361c1b9aa6c013 Mon Sep 17 00:00:00 2001 From: Dave Davenport Date: Sat, 30 Aug 2014 20:45:08 +0200 Subject: [PATCH] Play with adding some more test, bug fixes * delete an invalid memmove in textbox --- Makefile.am | 5 -- include/textbox.h | 9 +++ source/textbox.c | 8 +- test/Makefile.am | 19 ++++- test/run_dmenu_custom_test.sh | 26 ++----- test/run_dmenu_test.sh | 15 +--- test/run_errormsg_test.sh | 10 +-- test/run_run_test.sh | 21 ++---- test/run_switchdialog_test.sh | 15 ++-- test/run_test.sh | 35 +++++++++ test/textbox-test.c | 136 ++++++++++++++++++++++++++++++++++ 11 files changed, 228 insertions(+), 71 deletions(-) create mode 100755 test/run_test.sh create mode 100644 test/textbox-test.c diff --git a/Makefile.am b/Makefile.am index efc4ef15..def70dec 100644 --- a/Makefile.am +++ b/Makefile.am @@ -88,8 +88,3 @@ update-manpage: ${top_srcdir}/doc/rofi-manpage.markdown .PHONY: test test: rofi make -C test/ test - ./test/run_errormsg_test.sh - ./test/run_switchdialog_test.sh - ./test/run_dmenu_test.sh - ./test/run_dmenu_custom_test.sh - ./test/run_run_test.sh diff --git a/include/textbox.h b/include/textbox.h index c992fe4b..ff331afb 100644 --- a/include/textbox.h +++ b/include/textbox.h @@ -92,4 +92,13 @@ int textbox_get_font_height ( textbox *tb ); int textbox_get_font_width ( textbox *tb ); double textbox_get_estimated_char_width ( ); + + +void textbox_cursor_bkspc ( textbox *tb ); +void textbox_cursor_del ( textbox *tb ); +void textbox_cursor_dec ( textbox *tb ); +void textbox_cursor_inc ( textbox *tb ); + +void textbox_delete ( textbox *tb, int pos, int dlen ); + #endif //__TEXTBOX_H__ diff --git a/source/textbox.c b/source/textbox.c index c30eb965..9c1dc78e 100644 --- a/source/textbox.c +++ b/source/textbox.c @@ -320,7 +320,13 @@ void textbox_delete ( textbox *tb, int pos, int dlen ) pos = MAX ( 0, MIN ( len, pos ) ); // move everything after pos+dlen down char *at = tb->text + pos; - memmove ( at, at + dlen, len - pos ); + // Move remainder + closing \0 + memmove ( at, at + dlen, len - pos-dlen+1 ); + if ( tb->cursor >= pos && tb->cursor < (pos+dlen) ) { + tb->cursor = pos; + } else if ( tb->cursor >= (pos+dlen) ) { + tb->cursor -= dlen; + } } // delete on character diff --git a/test/Makefile.am b/test/Makefile.am index 0b4be914..3627af70 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -1,7 +1,7 @@ ## # Rofi the program ## -bin_PROGRAMS=rofi_test +bin_PROGRAMS=rofi_test textbox_test LIBS=\ @xft_LIBS@\ @@ -26,6 +26,19 @@ rofi_test_SOURCES=\ ../include/history.h\ history-test.c +textbox_test_SOURCES=\ + ../source/textbox.c\ + ../config/config.c\ + ../include/rofi.h\ + ../include/textbox.h\ + textbox-test.c + .PHONY: test -test: rofi_test - ./$^ +test: ${bin_PROGRAMS} + ./rofi_test + $(top_srcdir)/test/run_test.sh 123 $(top_builddir)/test/textbox_test $(top_builddir) + $(top_srcdir)/test/run_test.sh 200 $(top_srcdir)/test/run_errormsg_test.sh $(top_builddir) + $(top_srcdir)/test/run_test.sh 201 $(top_srcdir)/test/run_switchdialog_test.sh $(top_builddir) + $(top_srcdir)/test/run_test.sh 202 $(top_srcdir)/test/run_dmenu_test.sh $(top_builddir) + $(top_srcdir)/test/run_test.sh 203 $(top_srcdir)/test/run_dmenu_custom_test.sh $(top_builddir) + $(top_srcdir)/test/run_test.sh 204 $(top_srcdir)/test/run_run_test.sh $(top_builddir) diff --git a/test/run_dmenu_custom_test.sh b/test/run_dmenu_custom_test.sh index 95f7710d..039d5ab8 100755 --- a/test/run_dmenu_custom_test.sh +++ b/test/run_dmenu_custom_test.sh @@ -1,37 +1,27 @@ #!/usr/bin/env bash -# Create fake X -Xvfb :202 & -XPID=$! - -# wait till it is up, run rofi with error message -sleep 1; -xrdb -display :202 -load doc/example.xresources -echo -e -n "aap\nnoot\nmies" | ./rofi -width -30 -dmenu -display :202 > output.txt & +echo -e -n "aap\nnoot\nmies" | rofi -width -30 -dmenu > output.txt & RPID=$! # send enter. sleep 5; -DISPLAY=:202 xdotool key 'c' +xdotool key 'c' sleep 0.2 -DISPLAY=:202 xdotool key 'o' +xdotool key 'o' sleep 0.2 -DISPLAY=:202 xdotool key 'f' +xdotool key 'f' sleep 0.2 -DISPLAY=:202 xdotool key 'f' +xdotool key 'f' sleep 0.2 -DISPLAY=:202 xdotool key 'e' +xdotool key 'e' sleep 0.2 -DISPLAY=:202 xdotool key 'e' +xdotool key 'e' sleep 0.2 -DISPLAY=:202 xdotool key Return +xdotool key Return # Get result, kill xvfb wait ${RPID} RETV=$? -kill ${XPID} - -sleep 1 if [ `cat output.txt` != 'coffee' ] then diff --git a/test/run_dmenu_test.sh b/test/run_dmenu_test.sh index 06232768..3cedd65e 100755 --- a/test/run_dmenu_test.sh +++ b/test/run_dmenu_test.sh @@ -1,28 +1,21 @@ #!/usr/bin/env bash -# Create fake X -Xvfb :202 & -XPID=$! - # wait till it is up, run rofi with error message sleep 1; -echo -e -n "aap\nnoot\nmies" | ./rofi -dmenu -display :202 > output.txt & +echo -e -n "aap\nnoot\nmies" | rofi -dmenu > output.txt & RPID=$! # send enter. sleep 5; -DISPLAY=:202 xdotool key 'Down' +xdotool key 'Down' sleep 0.4 -DISPLAY=:202 xdotool key 'Down' +xdotool key 'Down' sleep 0.4 -DISPLAY=:202 xdotool key Return +xdotool key Return # Get result, kill xvfb wait ${RPID} RETV=$? -kill ${XPID} - -sleep 1 if [ `cat output.txt` != 'mies' ] then diff --git a/test/run_errormsg_test.sh b/test/run_errormsg_test.sh index 606dadf9..b2bfc6a2 100755 --- a/test/run_errormsg_test.sh +++ b/test/run_errormsg_test.sh @@ -1,19 +1,13 @@ #!/usr/bin/env bash -# Create fake X -Xvfb :200 & -XPID=$! - # wait till it is up, run rofi with error message -sleep 1 && ./rofi -e "Printing error message" -display :200 & +sleep 1 && rofi -e "Printing error message" & RPID=$! # send enter. -sleep 5 && DISPLAY=:200 xdotool key Return +sleep 5 && xdotool key Return # Get result, kill xvfb wait ${RPID} RETV=$? -kill ${XPID} -sleep 1 exit ${RETV} diff --git a/test/run_run_test.sh b/test/run_run_test.sh index 92f2e2ac..8b220a76 100755 --- a/test/run_run_test.sh +++ b/test/run_run_test.sh @@ -1,31 +1,22 @@ #!/usr/bin/env bash -# Create fake X -Xvfb :203 & -XPID=$! - -# wait till it is up, run rofi with error message -sleep 1; -./rofi -rnow -display :203 & +rofi -rnow & RPID=$! # send enter. sleep 5; -DISPLAY=:203 xdotool key 't' +xdotool key 't' sleep 0.4 -DISPLAY=:203 xdotool key 'r' +xdotool key 'r' sleep 0.4 -DISPLAY=:203 xdotool key 'u' +xdotool key 'u' sleep 0.4 -DISPLAY=:203 xdotool key 'e' +xdotool key 'e' sleep 0.4 -DISPLAY=:203 xdotool key Return +xdotool key Return # Get result, kill xvfb wait ${RPID} RETV=$? -kill ${XPID} - -sleep 1 exit ${RETV} diff --git a/test/run_switchdialog_test.sh b/test/run_switchdialog_test.sh index 820937e0..e6394d81 100755 --- a/test/run_switchdialog_test.sh +++ b/test/run_switchdialog_test.sh @@ -1,27 +1,22 @@ #!/usr/bin/env bash -# Create fake X -Xvfb :201 & -XPID=$! - # wait till it is up, run rofi with error message -sleep 1 && ./rofi -rnow -display :201 & +sleep 1 && rofi -rnow -display :201 & RPID=$! # send enter. sleep 5; -DISPLAY=:201 xdotool key 'shift+slash' +xdotool key 'shift+slash' sleep 0.4 -DISPLAY=:201 xdotool key 'shift+slash' +xdotool key 'shift+slash' sleep 0.4 -DISPLAY=:201 xdotool key 'shift+slash' +xdotool key 'shift+slash' sleep 0.4 -DISPLAY=:201 xdotool key Escape +xdotool key Escape # Get result, kill xvfb wait ${RPID} RETV=$? -kill ${XPID} sleep 1 diff --git a/test/run_test.sh b/test/run_test.sh new file mode 100755 index 00000000..eab3c838 --- /dev/null +++ b/test/run_test.sh @@ -0,0 +1,35 @@ +#!/usr/bin/env bash + +XPID= +DISPLAY=":0" +function create_fake_x ( ) +{ + DISPLAY=":$1" + echo "Starting fake X: ${DISPLAY}" + Xvfb ${DISPLAY} & + XPID=$! + sleep 1 +} + +function destroy_fake_x ( ) +{ + if [ -n "${XPID}" ] + then + echo "Stopping fake X: ${XPID}" + kill ${XPID} + wait ${XPID} + fi +} + +if [ -n "$3" ] +then + PATH=$3:$PATH +fi + +create_fake_x "$1" +$2 +RES=$? + +destroy_fake_x + +exit ${RES} diff --git a/test/textbox-test.c b/test/textbox-test.c new file mode 100644 index 00000000..09ba9973 --- /dev/null +++ b/test/textbox-test.c @@ -0,0 +1,136 @@ +#include +#include + +#include +#include +#include +#include +#include +#include +#include + +#include +#include + + +static int test = 0; + +#define TASSERT(a) {\ + assert ( a );\ + printf("Test %3i passed (%s)\n", ++test, #a);\ +} + +Display *display = NULL; + +static unsigned int color_get ( Display *display, const char *const name ) +{ + int screen_id = DefaultScreen ( display ); + XColor color; + Colormap map = DefaultColormap ( display, screen_id ); + return XAllocNamedColor ( display, map, name, &color, &color ) ? color.pixel : None; +} + +int main ( int argc, char **argv ) +{ + + // Get DISPLAY + const char *display_str = getenv ( "DISPLAY" ); + if ( !( display = XOpenDisplay ( display_str ) ) ) { + fprintf ( stderr, "cannot open display!\n" ); + return EXIT_FAILURE; + } + + TASSERT( display != NULL ); + Screen *screen = DefaultScreenOfDisplay ( display ); + Window root = RootWindow ( display, XScreenNumberOfScreen ( screen ) ); + Window mw = XCreateSimpleWindow ( display, root, 0, 0, 200, 100, + config.menu_bw, + color_get ( display, config.menu_bc ), + color_get ( display, config.menu_bg ) ); + TASSERT( mw != None ); + + textbox_setup ( config.menu_bg, config.menu_fg, + config.menu_hlbg, config.menu_hlfg ); + textbox *box = textbox_create(mw , TB_EDITABLE|TB_AUTOWIDTH|TB_AUTOHEIGHT, 0,0, -1, -1, NORMAL, "test"); + TASSERT( box != NULL ); + + textbox_cursor_end ( box ); + TASSERT ( box->cursor == 4); + textbox_cursor ( box, -1 ); + TASSERT ( box->cursor == 0 ); + textbox_cursor ( box, 8 ); + TASSERT ( box->cursor == 4 ); + textbox_cursor ( box, 2 ); + TASSERT ( box->cursor == 2 ); + textbox_insert ( box, 3, "bo"); + TASSERT ( strcmp(box->text, "tesbot") == 0 ); + textbox_cursor_end ( box ); + TASSERT ( box->cursor == 6); + + TASSERT( textbox_get_width( box) > 0 ); + TASSERT( textbox_get_height( box) > 0 ); + + TASSERT( textbox_get_width( box) >= textbox_get_font_width( box) ); + TASSERT( textbox_get_height( box) >= textbox_get_font_height( box) ); + + TASSERT( textbox_get_estimated_char_width ( box) > 0 ); + + textbox_cursor_bkspc ( box ); + TASSERT ( strcmp(box->text, "tesbo") == 0 ); + TASSERT ( box->cursor == 5); + + textbox_cursor_dec ( box ); + TASSERT ( box->cursor == 4); + textbox_cursor_del ( box ); + TASSERT ( strcmp(box->text, "tesb") == 0 ); + textbox_cursor_dec ( box ); + TASSERT ( box->cursor == 3); + textbox_cursor_inc ( box ); + TASSERT ( box->cursor == 4); + textbox_cursor_inc ( box ); + TASSERT ( box->cursor == 4); + // Cursor after delete section. + textbox_delete ( box, 0, 1 ); + TASSERT ( strcmp(box->text, "esb") == 0 ); + TASSERT ( box->cursor == 3); + // Cursor before delete. + textbox_text( box, "aap noot mies"); + TASSERT ( strcmp(box->text, "aap noot mies") == 0 ); + textbox_cursor( box, 3 ); + TASSERT ( box->cursor == 3); + textbox_delete ( box, 3, 6 ); + TASSERT ( strcmp(box->text, "aapmies") == 0 ); + TASSERT ( box->cursor == 3); + + // Cursor within delete + textbox_text( box, "aap noot mies"); + TASSERT ( strcmp(box->text, "aap noot mies") == 0 ); + textbox_cursor( box, 5 ); + TASSERT ( box->cursor == 5); + textbox_delete ( box, 3, 6 ); + TASSERT ( strcmp(box->text, "aapmies") == 0 ); + TASSERT ( box->cursor == 3); + // Cursor after delete. + textbox_text( box, "aap noot mies"); + TASSERT ( strcmp(box->text, "aap noot mies") == 0 ); + textbox_cursor( box, 11 ); + TASSERT ( box->cursor == 11); + textbox_delete ( box, 3, 6 ); + TASSERT ( strcmp(box->text, "aapmies") == 0 ); + TASSERT ( box->cursor == 5); + + + textbox_font ( box, HIGHLIGHT ); + textbox_draw( box ); + + textbox_show( box ); + textbox_move ( box, 12, 13); + TASSERT ( box->x == 12 ); + TASSERT ( box->y == 13 ); + textbox_hide( box ); + + textbox_free(box); + textbox_cleanup(); + XDestroyWindow ( display, mw); + XCloseDisplay ( display ); +}