Added some tests

This commit is contained in:
Pierre Pronchery 2013-12-20 07:09:57 +01:00
parent 4d89a82a8b
commit 267cde2991
5 changed files with 193 additions and 6 deletions

View File

@ -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 \

View File

@ -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

172
tests/applets2.c Normal file
View File

@ -0,0 +1,172 @@
/* $Id$ */
/* Copyright (c) 2013 Pierre Pronchery <khorben@defora.org> */
/* 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 <http://www.gnu.org/licenses/>. */
#include <dirent.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <dlfcn.h>
#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;
}

View File

@ -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

View File

@ -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