From b19ab62e1796a0570e6943e04aed904970f9e6e8 Mon Sep 17 00:00:00 2001 From: Dave Davenport Date: Mon, 24 Oct 2016 17:48:04 +0200 Subject: [PATCH] add scrollbar test. --- .gitignore | 3 ++ Makefile.am | 14 ++++++- source/widgets/scrollbar.c | 8 ++-- test/scrollbar-test.c | 75 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 95 insertions(+), 5 deletions(-) create mode 100644 test/scrollbar-test.c diff --git a/.gitignore b/.gitignore index c5665dbd..11798675 100644 --- a/.gitignore +++ b/.gitignore @@ -20,6 +20,8 @@ *.gcov *.gcda *.gcno +/test-driver +/ar-lib # generated files /helper_test @@ -48,3 +50,4 @@ core *.*~ *.unc-backup~ *.unc-backup.md5~ + diff --git a/Makefile.am b/Makefile.am index b63b1b15..5698e5f6 100644 --- a/Makefile.am +++ b/Makefile.am @@ -152,7 +152,8 @@ check_PROGRAMS=\ helper_tokenize\ helper_config_cmdline_parser\ widget_test\ - box_test + box_test\ + scrollbar_test history_test_CFLAGS=\ @@ -237,6 +238,14 @@ box_test_SOURCES=\ source/widgets/widget.c\ source/widgets/box.c\ test/box-test.c + +scrollbar_test_LDADD=$(textbox_test_LDADD) +scrollbar_test_CFLAGS=$(textbox_test_CFLAGS) +scrollbar_test_SOURCES=\ + source/widgets/widget.c\ + source/widgets/scrollbar.c\ + test/scrollbar-test.c + textbox_test_SOURCES=\ source/widgets/widget.c\ source/widgets/textbox.c\ @@ -323,7 +332,8 @@ TESTS=\ helper_config_cmdline_parser\ textbox_test\ widget_test\ - box_test + box_test\ + scrollbar_test .PHONY: test-x test-x: $(bin_PROGRAMS) diff --git a/source/widgets/scrollbar.c b/source/widgets/scrollbar.c index 73c217e2..0d06145f 100644 --- a/source/widgets/scrollbar.c +++ b/source/widgets/scrollbar.c @@ -101,13 +101,15 @@ static void scrollbar_draw ( widget *wid, cairo_t *draw ) cairo_fill ( draw ); } +// TODO +// This should behave more like a real scrollbar. unsigned int scrollbar_clicked ( const scrollbar *sb, int y ) { if ( sb != NULL ) { - if ( y >= sb->widget.y && y < ( sb->widget.y + sb->widget.h ) ) { + if ( y >= sb->widget.y && y <= ( sb->widget.y + sb->widget.h ) ) { y -= sb->widget.y; - y = MIN ( MAX ( 1, y ), sb->widget.h - 1 ) - 1; - const short bh = sb->widget.h - 2; + y = MIN ( MAX ( 0, y ), sb->widget.h ); + const short bh = sb->widget.h; float sec = ( ( bh ) / (float) sb->length ); unsigned int sel = y / sec; return MIN ( sel, sb->length - 1 ); diff --git a/test/scrollbar-test.c b/test/scrollbar-test.c new file mode 100644 index 00000000..9511de63 --- /dev/null +++ b/test/scrollbar-test.c @@ -0,0 +1,75 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +unsigned int test =0; +#define TASSERT( a ) { \ + assert ( a ); \ + printf ( "Test %3i passed (%s)\n", ++test, # a ); \ +} + +#define TASSERTE( a, b ) { \ + if ( ( a ) == ( b ) ) { \ + printf ( "Test %i passed (%s == %s) (%u == %u)\n", ++test, # a, # b, a, b ); \ + }else { \ + printf ( "Test %i failed (%s == %s) (%u != %u)\n", ++test, # a, # b, a, b ); \ + abort ( ); \ + } \ +} + +void color_separator ( G_GNUC_UNUSED void *d ) +{ + +} + +int main ( G_GNUC_UNUSED int argc, G_GNUC_UNUSED char **argv ) +{ + scrollbar * sb = scrollbar_create ( 0, 0, 10, 100); + + scrollbar_set_handle ( NULL, 10213); + scrollbar_set_max_value ( NULL, 10 ); + scrollbar_set_handle_length ( NULL , 1000); + + scrollbar_set_max_value ( sb, 10000); + TASSERTE ( sb->length, 10000 ); + scrollbar_set_handle_length ( sb, 10); + TASSERTE ( sb->pos_length, 10 ); + scrollbar_set_handle ( sb , 5000 ); + TASSERTE ( sb->pos, 5000 ); + scrollbar_set_handle ( sb , 15000 ); + TASSERTE ( sb->pos, 10000 ); + scrollbar_set_handle ( sb , UINT32_MAX ); + TASSERTE ( sb->pos, 10000 ); + scrollbar_set_handle_length ( sb, 15000); + TASSERTE ( sb->pos_length, 10000 ); + scrollbar_set_handle_length ( sb, 0); + TASSERTE ( sb->pos_length, 1 ); + + unsigned int cl = scrollbar_clicked ( sb, 10 ); + TASSERTE ( cl, 1000); + cl = scrollbar_clicked ( sb, 20 ); + TASSERTE ( cl, 2000); + cl = scrollbar_clicked ( sb, 0 ); + TASSERTE ( cl, 0); + cl = scrollbar_clicked ( sb, 99 ); + TASSERTE ( cl, 9900); + + scrollbar_set_max_value ( sb, 100 ); + for ( unsigned int i = 0; i < 100; i++ ){ + cl = scrollbar_clicked ( sb, i ); + TASSERTE ( cl, i); + } + + scrollbar_set_max_value ( sb, 200 ); + for ( unsigned int i = 0; i < 100; i++ ){ + cl = scrollbar_clicked ( sb, i ); + TASSERTE ( cl, i*2); + } + widget_free( WIDGET (sb ) ); +}