1
0
Fork 0
mirror of https://github.com/davatorium/rofi.git synced 2024-11-18 13:54:36 -05:00

add scrollbar test.

This commit is contained in:
Dave Davenport 2016-10-24 17:48:04 +02:00
parent 5f1cd75492
commit b19ab62e17
4 changed files with 95 additions and 5 deletions

3
.gitignore vendored
View file

@ -20,6 +20,8 @@
*.gcov *.gcov
*.gcda *.gcda
*.gcno *.gcno
/test-driver
/ar-lib
# generated files # generated files
/helper_test /helper_test
@ -48,3 +50,4 @@ core
*.*~ *.*~
*.unc-backup~ *.unc-backup~
*.unc-backup.md5~ *.unc-backup.md5~

View file

@ -152,7 +152,8 @@ check_PROGRAMS=\
helper_tokenize\ helper_tokenize\
helper_config_cmdline_parser\ helper_config_cmdline_parser\
widget_test\ widget_test\
box_test box_test\
scrollbar_test
history_test_CFLAGS=\ history_test_CFLAGS=\
@ -237,6 +238,14 @@ box_test_SOURCES=\
source/widgets/widget.c\ source/widgets/widget.c\
source/widgets/box.c\ source/widgets/box.c\
test/box-test.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=\ textbox_test_SOURCES=\
source/widgets/widget.c\ source/widgets/widget.c\
source/widgets/textbox.c\ source/widgets/textbox.c\
@ -323,7 +332,8 @@ TESTS=\
helper_config_cmdline_parser\ helper_config_cmdline_parser\
textbox_test\ textbox_test\
widget_test\ widget_test\
box_test box_test\
scrollbar_test
.PHONY: test-x .PHONY: test-x
test-x: $(bin_PROGRAMS) test-x: $(bin_PROGRAMS)

View file

@ -101,13 +101,15 @@ static void scrollbar_draw ( widget *wid, cairo_t *draw )
cairo_fill ( draw ); cairo_fill ( draw );
} }
// TODO
// This should behave more like a real scrollbar.
unsigned int scrollbar_clicked ( const scrollbar *sb, int y ) unsigned int scrollbar_clicked ( const scrollbar *sb, int y )
{ {
if ( sb != NULL ) { 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 -= sb->widget.y;
y = MIN ( MAX ( 1, y ), sb->widget.h - 1 ) - 1; y = MIN ( MAX ( 0, y ), sb->widget.h );
const short bh = sb->widget.h - 2; const short bh = sb->widget.h;
float sec = ( ( bh ) / (float) sb->length ); float sec = ( ( bh ) / (float) sb->length );
unsigned int sel = y / sec; unsigned int sel = y / sec;
return MIN ( sel, sb->length - 1 ); return MIN ( sel, sb->length - 1 );

75
test/scrollbar-test.c Normal file
View file

@ -0,0 +1,75 @@
#include <unistd.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>
#include <assert.h>
#include <glib.h>
#include <string.h>
#include <widgets/scrollbar.h>
#include <widgets/widget.h>
#include <widgets/widget-internal.h>
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 ) );
}