Added test for handler feature.
authorDaniel Carl <danielcarl@gmx.de>
Sat, 7 Jun 2014 12:07:39 +0000 (14:07 +0200)
committerDaniel Carl <danielcarl@gmx.de>
Sat, 7 Jun 2014 13:08:13 +0000 (15:08 +0200)
src/handlers.c
src/handlers.h
tests/Makefile
tests/test-handlers.c [new file with mode: 0644]

index 4b049ba..a0c9c6a 100644 (file)
@@ -21,8 +21,6 @@
 #include "handlers.h"
 #include "util.h"
 
-extern VbCore vb;
-
 static GHashTable *handlers = NULL;
 
 static char *handler_lookup(const char *uri);
@@ -40,9 +38,9 @@ void handlers_cleanup(void)
     }
 }
 
-gboolean handler_add(const char *key, const char *uri)
+gboolean handler_add(const char *key, const char *cmd)
 {
-    g_hash_table_insert(handlers, g_strdup(key), g_strdup(uri));
+    g_hash_table_insert(handlers, g_strdup(key), g_strdup(cmd));
 
     return true;
 }
@@ -86,12 +84,12 @@ gboolean handler_fill_completion(GtkListStore *store, const char *input)
 
 static char *handler_lookup(const char *uri)
 {
-    char *p, *handler = NULL;
+    char *p, *schema, *handler = NULL;
 
     if ((p = strchr(uri, ':'))) {
-        *p = '\0';
-        handler = g_hash_table_lookup(handlers, uri);
-        *p = ':';
+        schema  = g_strndup(uri, p - uri);
+        handler = g_hash_table_lookup(handlers, schema);
+        g_free(schema);
     }
 
     return handler;
index 39b4c20..5e19784 100644 (file)
@@ -22,7 +22,7 @@
 
 void handlers_init(void);
 void handlers_cleanup(void);
-gboolean handler_add(const char *key, const char *uri);
+gboolean handler_add(const char *key, const char *cmd);
 gboolean handler_remove(const char *key);
 gboolean handle_uri(const char *uri);
 gboolean handler_fill_completion(GtkListStore *store, const char *input);
index 21fdd72..7b26bb2 100644 (file)
@@ -3,7 +3,7 @@ include ../config.mk
 CPPFLAGS += -I ../
 CFLAGS   += -fPIC
 
-TEST_PROGS = test-util
+TEST_PROGS = test-util test-handlers
 
 all: $(TEST_PROGS)
        LD_LIBRARY_PATH="$(LD_LIBRARY_PATH):." gtester --verbose $(TEST_PROGS)
diff --git a/tests/test-handlers.c b/tests/test-handlers.c
new file mode 100644 (file)
index 0000000..9d4c844
--- /dev/null
@@ -0,0 +1,80 @@
+/**
+ * vimb - a webkit based vim like browser.
+ *
+ * Copyright (C) 2012-2014 Daniel Carl
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see http://www.gnu.org/licenses/.
+ */
+
+#include <gtk/gtk.h>
+#include <src/handlers.h>
+
+#define TEST_URI "http://fanglingsu.github.io/vimb/"
+
+static void test_handler_add(void)
+{
+    /* check none handled http first */
+    g_assert_true(handler_add("http", "echo -n 'handled uri %s'"));
+}
+
+static void test_handler_remove(void)
+{
+    handler_add("http", "e");
+
+    g_assert_true(handler_remove("http"));
+    g_assert_false(handler_remove("http"));
+}
+
+static void test_handler_run_success(void)
+{
+    if (g_test_subprocess()) {
+        handler_add("http", "echo -n 'handled uri %s'");
+        handle_uri(TEST_URI);
+        return;
+    }
+    g_test_trap_subprocess(NULL, 0, 0);
+    g_test_trap_assert_passed();
+    g_test_trap_assert_stdout("handled uri " TEST_URI);
+}
+
+static void test_handler_run_failed(void)
+{
+    if (g_test_subprocess()) {
+        handler_add("http", "unknown-program %s");
+        handle_uri(TEST_URI);
+        return;
+    }
+    g_test_trap_subprocess(NULL, 0, 0);
+    g_test_trap_assert_failed();
+    g_test_trap_assert_stderr("*Can't run *unknown-program*");
+}
+
+int main(int argc, char *argv[])
+{
+    int result;
+    handlers_init();
+
+    g_test_init(&argc, &argv, NULL);
+
+    g_test_add_func("/test-handlers/add", test_handler_add);
+    g_test_add_func("/test-handlers/remove", test_handler_remove);
+    g_test_add_func("/test-handlers/handle_uri/success", test_handler_run_success);
+    g_test_add_func("/test-handlers/handle_uri/failed", test_handler_run_failed);
+
+    result = g_test_run();
+
+    handlers_cleanup();
+
+    return result;
+}