From 0525da2d325b99fcb73db6385d7ad72e3c5d1038 Mon Sep 17 00:00:00 2001 From: Daniel Carl Date: Sat, 7 Jun 2014 14:07:39 +0200 Subject: [PATCH] Added test for handler feature. --- src/handlers.c | 14 ++++---- src/handlers.h | 2 +- tests/Makefile | 2 +- tests/test-handlers.c | 80 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 88 insertions(+), 10 deletions(-) create mode 100644 tests/test-handlers.c diff --git a/src/handlers.c b/src/handlers.c index 4b049ba..a0c9c6a 100644 --- a/src/handlers.c +++ b/src/handlers.c @@ -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; diff --git a/src/handlers.h b/src/handlers.h index 39b4c20..5e19784 100644 --- a/src/handlers.h +++ b/src/handlers.h @@ -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); diff --git a/tests/Makefile b/tests/Makefile index 21fdd72..7b26bb2 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -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 index 0000000..9d4c844 --- /dev/null +++ b/tests/test-handlers.c @@ -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 +#include + +#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; +} -- 2.20.1