From 6722d5be3c2311cea0380477f0a60d15721b8c60 Mon Sep 17 00:00:00 2001 From: "Michael D. Lowis" Date: Tue, 26 Jun 2018 14:07:10 -0400 Subject: [PATCH] added tests for win.c event handling --- Makefile | 2 +- config.mk | 3 +- inc/win.h | 1 + lib/view.c | 2 +- lib/x11.c | 15 +++--- tests/lib/win.c | 132 ++++++++++++++++++++++++++++++++++++++++++++++++ tests/libedit.c | 12 +---- 7 files changed, 146 insertions(+), 21 deletions(-) create mode 100644 tests/lib/win.c diff --git a/Makefile b/Makefile index b01ff21..bf4c604 100644 --- a/Makefile +++ b/Makefile @@ -53,7 +53,7 @@ libedit.a: $(LIBEDIT_OBJS) tide: tide.o libedit.a xcpd: xcpd.o libedit.a -tests/libedit: tests/libedit.o tests/lib/buf.o tests/lib/utf8.o libedit.a +tests/libedit: tests/libedit.o tests/lib/buf.o tests/lib/utf8.o tests/lib/win.o libedit.a # define implicit rule for building binaries %: %.o diff --git a/config.mk b/config.mk index 16a1cff..39acedd 100644 --- a/config.mk +++ b/config.mk @@ -15,8 +15,9 @@ INCS += -I/usr/include/freetype2 # Compiler Setup CC = cc -CFLAGS = -g --std=c99 -MMD $(INCS) +CFLAGS = -g --std=c99 -MMD $(INCS) CFLAGS += -Wall -Werror +CFLAGS += -Wno-unused-function # Linker Setup LD = $(CC) diff --git a/inc/win.h b/inc/win.h index 4caff41..ff80b3e 100644 --- a/inc/win.h +++ b/inc/win.h @@ -133,6 +133,7 @@ typedef struct { void win_init(char* title, KeyBinding* bindings); void win_prop_set(char* xname, char* ename, char* value); void win_save(char* path); +void win_update(int ms); void win_loop(void); void win_quit(void); View* win_view(WinRegion id); diff --git a/lib/view.c b/lib/view.c index ae36cf3..30168c1 100644 --- a/lib/view.c +++ b/lib/view.c @@ -37,7 +37,7 @@ static void move_to(View* view, bool extsel, size_t off) { } static bool selection_visible(View* view) { - if (!view->nrows) return true; + if (!view->rows || !view->nrows) return true; size_t csr = CSRPOS; size_t beg = view->rows[0]->off; size_t end = view->rows[view->nrows-1]->off + diff --git a/lib/x11.c b/lib/x11.c index 0993524..5b30afe 100644 --- a/lib/x11.c +++ b/lib/x11.c @@ -10,12 +10,9 @@ #include #include #include - -#define Region _Region #include #include #include -#undef Region struct XSel { char* name; @@ -45,7 +42,7 @@ struct XWin { }; /******************************************************************************/ -static struct XWin X; +struct XWin X; static int KeyBtnState; static WinRegion Focused = EDIT; static View Regions[NREGIONS]; @@ -347,7 +344,7 @@ static void draw_scroll(drawcsr* csr) { View* view = win_view(EDIT); size_t bend = buf_end(win_buf(EDIT)); if (bend == 0) bend = 1; - if (!view->rows) return; + if (!view->rows || !view->nrows) return; size_t vbeg = view->rows[0]->off; size_t vend = view->rows[view->nrows-1]->off + view->rows[view->nrows-1]->len; double scroll_vis = (double)(vend - vbeg) / (double)bend; @@ -579,13 +576,17 @@ void win_save(char* path) { win_prop_set("TIDE_FILE", "file", path); } +void win_update(int ms) { + job_poll(Timeout); + xupdate(NULL); +} + void win_loop(void) { XMapWindow(X.display, X.self); tide_send("ADD"); job_spawn(ConnectionNumber(X.display), xupdate, 0, 0); while (1) { - job_poll(Timeout); - xupdate(NULL); + win_update(Timeout); } } diff --git a/tests/lib/win.c b/tests/lib/win.c new file mode 100644 index 0000000..c4a2881 --- /dev/null +++ b/tests/lib/win.c @@ -0,0 +1,132 @@ +#define _XOPEN_SOURCE 700 +#include +#include "tide.c" + +#include +#include +#include + +struct XWin { + Time now; + Window root; + Display* display; + Visual* visual; + Colormap colormap; + unsigned depth; + int screen; + /* assume a single window for now. these are its attributes */ + Window self; + XftDraw* xft; + Pixmap pixmap; + int width; + int height; + XIC xic; + XIM xim; + GC gc; + XftFont* font; +}; + +extern struct XWin X; + +static void load_file(char* opath) { + char* path = realpath(opath, NULL); + view_init(win_view(EDIT), path); + win_prop_set("TIDE_FILE", "file", path); +} + +static void send_focus(bool focused) { + XSendEvent(X.display, X.self, False, 0, (XEvent*)&(XFocusChangeEvent){ + .type = (focused ? FocusIn : FocusOut), + .window = X.self, + }); +} + +static void send_key(int region, int mods, int key) { +} + +static void send_btn_dn(int mods, int button, int x, int y) { +} + +static void send_btn_up(int mods, int button, int x, int y) { +} + +static void send_btn_drag(int mods, int button, int x, int y) { +} + +#if 0 +static void send_sel_clear() { +} + +static void send_sel_notify() { +} + +static void send_sel_request() { +} + +static void send_client_msg() { +} +#endif + +static void send_resize(int width, int height) { + XResizeWindow(X.display, X.self, width, height); + XSync(X.display, False); +} + +TEST_SUITE(WinTests) { + /* Suite Setup + *************************************************************************/ + win_init("tide", Bindings); + + /* Focus Events + *************************************************************************/ + TEST(window should handle gaining focus) { + send_focus(true); + win_update(0); + } + + TEST(window should handle losing focus) { + send_focus(false); + win_update(0); + } + + TEST(window should handle gaining then losing focus) { + send_focus(true); + send_focus(false); + win_update(0); + } + + TEST(window should handle gaining then losing then gaining focus) { + send_focus(false); + send_focus(true); + win_update(0); + } + + /* Resize Events + *************************************************************************/ + TEST(window should resize if requested) { + send_resize(800, 600); + win_update(0); + CHECK(X.width == 800); + CHECK(X.height == 600); + } + + TEST(window should not resize if unnecessary) { + int width = X.width, height = X.height; + send_resize(X.width, X.height); + win_update(0); + CHECK(X.width == width); + CHECK(X.height == height); + } + + TEST(window should handle sizes that are too small) { + IGNORE("We don't handle tiny windows yet"); + send_resize(1, 1); + win_update(0); + CHECK(X.width == 1); + CHECK(X.height == 1); + } + + /* Suite TearDown + *************************************************************************/ + /* nothing to do yet */ +} diff --git a/tests/libedit.c b/tests/libedit.c index bbe0552..a2e04b4 100644 --- a/tests/libedit.c +++ b/tests/libedit.c @@ -4,20 +4,10 @@ #define INCLUDE_DEFS #include -Buf Buffer; -unsigned CursorPos; -unsigned TargetCol; -unsigned SelBeg; -unsigned SelEnd; - -void move_pointer(unsigned x, unsigned y) { - (void)x; - (void)y; -} - int main(int argc, char** argv) { atf_init(argc,argv); RUN_EXTERN_TEST_SUITE(BufferTests); RUN_EXTERN_TEST_SUITE(Utf8Tests); + RUN_EXTERN_TEST_SUITE(WinTests); return atf_print_results(); } -- 2.49.0