]> git.mdlowis.com Git - projs/tide.git/commitdiff
added tests for win.c event handling
authorMichael D. Lowis <mike.lowis@gentex.com>
Tue, 26 Jun 2018 18:07:10 +0000 (14:07 -0400)
committerMichael D. Lowis <mike.lowis@gentex.com>
Tue, 26 Jun 2018 18:07:10 +0000 (14:07 -0400)
Makefile
config.mk
inc/win.h
lib/view.c
lib/x11.c
tests/lib/win.c [new file with mode: 0644]
tests/libedit.c

index b01ff21cc842311774980f4aca120531312a0bac..bf4c604150a1155d0764bc762d9dbb1a23c5deb5 100644 (file)
--- 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
index 16a1cff462c99d4d814851cf57d29fb94d77b9bb..39acedd6f4ee74d4948701eee9494ecff1242da3 100644 (file)
--- 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)
index 4caff414dba249e057f3b75577289ebe21c3bfb7..ff80b3e76f9c6dd6aad93d1099df1e5396f2b728 100644 (file)
--- 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);
index ae36cf3e10b1a646a1a08af2a5e6ab5bf1c91267..30168c1ce38a5c6c9ce15d65a0305a8014e4ab5f 100644 (file)
@@ -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 +
index 0993524f755a65322cc483597eb061494bc961f7..5b30afe0fb7c68610d04b0363a2c4bf6c843cee9 100644 (file)
--- a/lib/x11.c
+++ b/lib/x11.c
 #include <sys/time.h>
 #include <sys/types.h>
 #include <ctype.h>
-
-#define Region _Region
 #include <X11/Xlib.h>
 #include <X11/Xatom.h>
 #include <X11/Xft/Xft.h>
-#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 (file)
index 0000000..c4a2881
--- /dev/null
@@ -0,0 +1,132 @@
+#define _XOPEN_SOURCE 700
+#include <atf.h>
+#include "tide.c"
+
+#include <X11/Xlib.h>
+#include <X11/Xatom.h>
+#include <X11/Xft/Xft.h>
+
+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 */
+}
index bbe0552af7a980de023649f955817a226095a28f..a2e04b45d8b10cce1e2e47f574c2ecb5b19caf0e 100644 (file)
@@ -4,20 +4,10 @@
 #define INCLUDE_DEFS
 #include <atf.h>
 
-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();
 }