rofi/test/textbox-test.c

187 lines
5.9 KiB
C
Raw Normal View History

#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include <glib.h>
#include <history.h>
#include <string.h>
#include <X11/X.h>
#include <X11/Xlib.h>
#include <textbox.h>
#include <rofi.h>
#include <cairo-xlib.h>
2016-01-07 15:01:56 +00:00
#include "settings.h"
2015-09-16 19:01:40 +00:00
static int test = 0;
unsigned int normal_window_mode = 0;
2015-07-28 20:14:21 +00:00
#define TASSERT( a ) { \
assert ( a ); \
printf ( "Test %3i passed (%s)\n", ++test, # a ); \
}
2015-07-28 20:14:21 +00:00
Display *display = NULL;
Colormap map = None;
2015-01-21 09:04:15 +00:00
XVisualInfo vinfo;
2015-07-28 20:22:18 +00:00
void error_dialog ( const char *msg, G_GNUC_UNUSED int markup )
{
fputs ( msg, stderr );
}
int show_error_message ( const char *msg, int markup )
2015-07-28 20:22:18 +00:00
{
error_dialog ( msg, markup );
return 0;
2015-07-28 20:22:18 +00:00
}
static unsigned int color_get ( Display *display, const char *const name )
{
2015-01-21 09:04:15 +00:00
XColor color;
// Special format.
if ( strncmp ( name, "argb:", 5 ) == 0 ) {
return strtoul ( &name[5], NULL, 16 );
}
else {
return XAllocNamedColor ( display, map, name, &color, &color ) ? color.pixel : None;
}
}
2015-07-28 20:14:21 +00:00
static void create_visual_and_colormap ()
{
map = None;
// Try to create TrueColor map
2015-07-28 20:14:21 +00:00
if ( XMatchVisualInfo ( display, DefaultScreen ( display ), 32, TrueColor, &vinfo ) ) {
// Visual found, lets try to create map.
map = XCreateColormap ( display, DefaultRootWindow ( display ), vinfo.visual, AllocNone );
}
// Failed to create map.
2015-07-28 20:14:21 +00:00
if ( map == None ) {
// Two fields we use.
2015-07-28 20:14:21 +00:00
vinfo.visual = DefaultVisual ( display, DefaultScreen ( display ) );
vinfo.depth = DefaultDepth ( display, DefaultScreen ( display ) );
map = DefaultColormap ( display, DefaultScreen ( display ) );
}
}
2015-02-09 18:58:17 +00:00
int main ( G_GNUC_UNUSED int argc, G_GNUC_UNUSED char **argv )
{
// Get DISPLAY
const char *display_str = getenv ( "DISPLAY" );
if ( !( display = XOpenDisplay ( display_str ) ) ) {
fprintf ( stderr, "cannot open display!\n" );
return EXIT_FAILURE;
}
2015-07-28 20:14:21 +00:00
create_visual_and_colormap ();
2015-07-28 20:14:21 +00:00
setup_abe ();
TASSERT ( display != NULL );
2015-01-21 09:04:15 +00:00
XSetWindowAttributes attr;
attr.colormap = map;
2015-04-06 18:55:33 +00:00
attr.border_pixel = color_get ( display, "white" );
2015-07-28 20:14:21 +00:00
attr.background_pixel = color_get ( display, "black" );
2015-01-21 09:04:15 +00:00
Window mw = XCreateWindow ( display, DefaultRootWindow ( display ),
2015-07-28 20:14:21 +00:00
0, 0, 200, 100, config.menu_bw, vinfo.depth, InputOutput,
vinfo.visual, CWColormap | CWBorderPixel | CWBackPixel, &attr );
TASSERT ( mw != None );
2015-09-26 18:34:34 +00:00
cairo_surface_t *surface = cairo_xlib_surface_create ( display, mw, vinfo.visual, 200, 100 );
// Create a drawable.
cairo_t *draw = cairo_create ( surface );
cairo_set_operator ( draw, CAIRO_OPERATOR_SOURCE );
2015-01-12 19:50:39 +00:00
// Set alternate row to normal row.
config.menu_bg_alt = config.menu_bg;
2015-09-28 19:41:58 +00:00
textbox_setup ( display );
PangoContext *p = pango_cairo_create_context ( draw );
textbox_set_pango_context ( p );
// cleanup
g_object_unref ( p );
2015-12-23 18:16:47 +00:00
textbox *box = textbox_create ( TB_EDITABLE | TB_AUTOWIDTH | TB_AUTOHEIGHT, 0, 0, -1, -1,
NORMAL, "test" );
2015-07-28 20:14:21 +00:00
TASSERT ( box != NULL );
textbox_cursor_end ( box );
2015-07-28 20:14:21 +00:00
TASSERT ( box->cursor == 4 );
textbox_cursor ( box, -1 );
2015-07-28 19:54:08 +00:00
TASSERT ( box->cursor == 0 );
textbox_cursor ( box, 8 );
2015-07-28 19:54:08 +00:00
TASSERT ( box->cursor == 4 );
textbox_cursor ( box, 2 );
2015-07-28 19:54:08 +00:00
TASSERT ( box->cursor == 2 );
textbox_insert ( box, 3, "bo", 2 );
2015-07-28 20:14:21 +00:00
TASSERT ( strcmp ( box->text, "tesbot" ) == 0 );
textbox_cursor_end ( box );
2015-07-28 20:14:21 +00:00
TASSERT ( box->cursor == 6 );
2015-07-28 20:14:21 +00:00
TASSERT ( textbox_get_width ( box ) > 0 );
TASSERT ( textbox_get_height ( box ) > 0 );
2015-07-28 20:14:21 +00:00
TASSERT ( textbox_get_width ( box ) >= textbox_get_font_width ( box ) );
TASSERT ( textbox_get_height ( box ) >= textbox_get_font_height ( box ) );
2015-07-28 20:14:21 +00:00
TASSERT ( textbox_get_estimated_char_width ( ) > 0 );
textbox_cursor_bkspc ( box );
2015-07-28 20:14:21 +00:00
TASSERT ( strcmp ( box->text, "tesbo" ) == 0 );
TASSERT ( box->cursor == 5 );
textbox_cursor_dec ( box );
2015-07-28 20:14:21 +00:00
TASSERT ( box->cursor == 4 );
textbox_cursor_del ( box );
2015-07-28 20:14:21 +00:00
TASSERT ( strcmp ( box->text, "tesb" ) == 0 );
textbox_cursor_dec ( box );
2015-07-28 20:14:21 +00:00
TASSERT ( box->cursor == 3 );
textbox_cursor_inc ( box );
2015-07-28 20:14:21 +00:00
TASSERT ( box->cursor == 4 );
textbox_cursor_inc ( box );
2015-07-28 20:14:21 +00:00
TASSERT ( box->cursor == 4 );
// Cursor after delete section.
textbox_delete ( box, 0, 1 );
2015-07-28 20:14:21 +00:00
TASSERT ( strcmp ( box->text, "esb" ) == 0 );
TASSERT ( box->cursor == 3 );
// Cursor before delete.
2015-07-28 20:14:21 +00:00
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 );
2015-07-28 20:14:21 +00:00
TASSERT ( strcmp ( box->text, "aapmies" ) == 0 );
TASSERT ( box->cursor == 3 );
// Cursor within delete
2015-07-28 20:14:21 +00:00
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 );
2015-07-28 20:14:21 +00:00
TASSERT ( strcmp ( box->text, "aapmies" ) == 0 );
TASSERT ( box->cursor == 3 );
2015-07-28 19:54:08 +00:00
// Cursor after delete.
2015-07-28 20:14:21 +00:00
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 );
2015-07-28 20:14:21 +00:00
TASSERT ( strcmp ( box->text, "aapmies" ) == 0 );
TASSERT ( box->cursor == 5 );
textbox_font ( box, HIGHLIGHT );
2015-09-26 18:34:34 +00:00
textbox_draw ( box, draw );
widget_move ( WIDGET(box), 12, 13 );
TASSERT ( box->widget.x == 12 );
TASSERT ( box->widget.y == 13 );
2015-07-28 20:14:21 +00:00
textbox_free ( box );
textbox_cleanup ( );
2015-04-30 20:42:04 +00:00
2015-07-28 20:14:21 +00:00
cleanup_abe ();
2015-04-30 20:42:04 +00:00
2015-09-26 18:34:34 +00:00
cairo_destroy ( draw );
cairo_surface_destroy ( surface );
2015-07-28 20:14:21 +00:00
XDestroyWindow ( display, mw );
XCloseDisplay ( display );
}