From 267cde299147e7f1f93e68a9829872a34bb53bda Mon Sep 17 00:00:00 2001 From: Pierre Pronchery Date: Fri, 20 Dec 2013 07:09:57 +0100 Subject: [PATCH] Added some tests --- Makefile | 1 + tests/Makefile | 16 ++++- tests/applets2.c | 172 +++++++++++++++++++++++++++++++++++++++++++++ tests/project.conf | 8 ++- tests/tests.sh | 2 +- 5 files changed, 193 insertions(+), 6 deletions(-) create mode 100644 tests/applets2.c diff --git a/Makefile b/Makefile index 971440b..2d75137 100644 --- a/Makefile +++ b/Makefile @@ -117,6 +117,7 @@ dist: $(PACKAGE)-$(VERSION)/src/applets/tasks.atoms \ $(PACKAGE)-$(VERSION)/src/applets/project.conf \ $(PACKAGE)-$(VERSION)/tests/applets.c \ + $(PACKAGE)-$(VERSION)/tests/applets2.c \ $(PACKAGE)-$(VERSION)/tests/Makefile \ $(PACKAGE)-$(VERSION)/tests/tests.sh \ $(PACKAGE)-$(VERSION)/tests/project.conf \ diff --git a/tests/Makefile b/tests/Makefile index aec7814..effd48d 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -1,4 +1,4 @@ -TARGETS = applets tests.log +TARGETS = applets applets2 tests.log PREFIX = /usr/local DESTDIR = BINDIR = $(PREFIX)/bin @@ -24,14 +24,24 @@ applets_LDFLAGS = $(LDFLAGSF) $(LDFLAGS) applets: $(applets_OBJS) $(CC) -o applets $(applets_OBJS) $(applets_LDFLAGS) -tests.log: applets tests.sh +applets2_OBJS = applets2.o +applets2_CFLAGS = $(CPPFLAGSF) $(CPPFLAGS) $(CFLAGSF) $(CFLAGS) +applets2_LDFLAGS = $(LDFLAGSF) $(LDFLAGS) + +applets2: $(applets2_OBJS) + $(CC) -o applets2 $(applets2_OBJS) $(applets2_LDFLAGS) + +tests.log: applets applets2 tests.sh ./tests.sh -P "$(PREFIX)" -- "tests.log" applets.o: applets.c $(CC) $(applets_CFLAGS) -c applets.c +applets2.o: applets2.c + $(CC) $(applets2_CFLAGS) -c applets2.c + clean: - $(RM) -- $(applets_OBJS) $(tests.log_OBJS) + $(RM) -- $(applets_OBJS) $(applets2_OBJS) $(tests.log_OBJS) ./tests.sh -c -P "$(PREFIX)" -- "tests.log" distclean: clean diff --git a/tests/applets2.c b/tests/applets2.c new file mode 100644 index 0000000..eb57763 --- /dev/null +++ b/tests/applets2.c @@ -0,0 +1,172 @@ +/* $Id$ */ +/* Copyright (c) 2013 Pierre Pronchery */ +/* This file is part of DeforaOS Desktop Panel */ +/* 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, version 3 of the License. + * + * 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 . */ + + + +#include +#include +#include +#include +#include +#include "Panel.h" + + +/* private */ +/* prototypes */ +static gboolean _applets2(gpointer data); + +static int _dlerror(char const * message, int ret); +static int _error(char const * message, char const * error, int ret); +static int _perror(char const * message, int ret); + + +/* functions */ +/* applets2 */ +/* callbacks */ +static char const * _applets2_helper_config_get(Panel * panel, + char const * section, char const * variable); +static int _applets2_helper_error(Panel * panel, char const * message, int ret); + +static gboolean _applets2(gpointer data) +{ + int * ret = data; + const char path[] = "../src/applets"; +#ifdef __APPLE__ + const char ext[] = ".dylib"; +#else + const char ext[] = ".so"; +#endif + DIR * dir; + struct dirent * de; + size_t len; + char * s; + void * p; + PanelAppletHelper helper; + PanelAppletDefinition * pad; + PanelApplet * pa; + GtkWidget * widget; + + if((dir = opendir(path)) == NULL) + { + *ret = _perror(path, 1); + gtk_main_quit(); + return FALSE; + } + *ret = 0; + memset(&helper, 0, sizeof(helper)); + helper.type = PANEL_APPLET_TYPE_NORMAL; + helper.icon_size = GTK_ICON_SIZE_LARGE_TOOLBAR; + helper.config_get = _applets2_helper_config_get; + helper.error = _applets2_helper_error; + while((de = readdir(dir)) != NULL) + { + if((len = strlen(de->d_name)) < sizeof(ext)) + continue; + if(strcmp(&de->d_name[len - sizeof(ext) + 1], ext) != 0) + continue; + if((s = malloc(sizeof(path) + len + 1)) == NULL) + { + *ret += _perror(de->d_name, 1); + continue; + } + snprintf(s, sizeof(path) + len + 1, "%s/%s", path, de->d_name); + if((p = dlopen(s, RTLD_LAZY)) == NULL) + { + *ret += _dlerror(s, 1); + free(s); + continue; + } + widget = NULL; + if((pad = dlsym(p, "applet")) == NULL) + *ret += _dlerror(s, 1); + else if(pad->init == NULL) + *ret += _error(s, "Missing initializer", 1); + else if(pad->destroy == NULL) + *ret += _error(s, "Missing destructor", 1); + else if((pa = pad->init(&helper, &widget)) == NULL) + _error(s, "Could not initialize", 0); + else if(widget == NULL) + { + _error(s, "No widget returned", 0); + pad->destroy(pa); + } + else + pad->destroy(pa); + free(s); + dlclose(p); + } + closedir(dir); + gtk_main_quit(); + return FALSE; +} + +static char const * _applets2_helper_config_get(Panel * panel, + char const * section, char const * variable) +{ + printf("%s: config_get(\"%s\", \"%s\")\n", "applets2", section, + variable); + return NULL; +} + +static int _applets2_helper_error(Panel * panel, char const * message, int ret) +{ + fprintf(stderr, "%s: %s\n", "applets2", message); + return ret; +} + + +/* dlerror */ +static int _dlerror(char const * message, int ret) +{ + fputs("applets2: ", stderr); + fprintf(stderr, "%s: %s\n", message, dlerror()); + return ret; +} + + +/* error */ +static int _error(char const * message, char const * error, int ret) +{ + fputs("applets2: ", stderr); + fprintf(stderr, "%s: %s\n", message, error); + return ret; +} + + +/* perror */ +static int _perror(char const * message, int ret) +{ + fputs("applets2: ", stderr); + perror(message); + return ret; +} + + +/* public */ +/* functions */ +/* main */ +int main(int argc, char * argv[]) +{ + int ret = 0; + guint source; + + if(getenv("DISPLAY") == NULL) + /* XXX ignore this test */ + return 0; + gtk_init(&argc, &argv); + source = g_idle_add(_applets2, &ret); + gtk_main(); + return (ret == 0) ? 0 : 2; +} diff --git a/tests/project.conf b/tests/project.conf index dc0b911..5c0bebf 100644 --- a/tests/project.conf +++ b/tests/project.conf @@ -1,4 +1,4 @@ -targets=applets,tests.log +targets=applets,applets2,tests.log cppflags_force=-I ../include cflags_force=-W `pkg-config --cflags libDesktop` cflags=-Wall -g -O2 @@ -9,7 +9,11 @@ dist=Makefile,tests.sh type=binary sources=applets.c +[applets2] +type=binary +sources=applets2.c + [tests.log] type=script script=./tests.sh -depends=applets,tests.sh +depends=applets,applets2,tests.sh diff --git a/tests/tests.sh b/tests/tests.sh index 528c0cb..1b415a6 100755 --- a/tests/tests.sh +++ b/tests/tests.sh @@ -98,7 +98,7 @@ target="$1" $DATE > "$target" FAILED= echo "Performing tests:" 1>&2 -#_test "applets" +_test "applets2" echo "Expected failures:" 1>&2 _fail "applets" if [ -n "$FAILED" ]; then