2019-06-08 20:38:32 -05:00
|
|
|
// 8 june 2019
|
|
|
|
#include "test.h"
|
|
|
|
|
2019-06-18 09:48:10 -05:00
|
|
|
struct counts {
|
|
|
|
unsigned long countInit;
|
|
|
|
unsigned long countFree;
|
2019-06-18 09:59:56 -05:00
|
|
|
|
|
|
|
unsigned long countEventOnFree;
|
2019-06-16 20:44:49 -05:00
|
|
|
};
|
|
|
|
|
2019-06-18 09:48:10 -05:00
|
|
|
static void checkCountsFull(testingT *t, const char *file, long line, const struct counts *got, const struct counts *want)
|
|
|
|
{
|
|
|
|
#define check(method) \
|
|
|
|
if (got->count ## method != want->count ## method) \
|
|
|
|
testingTErrorfFull(t, file, line, "wrong number of calls to " #method "():" diff("%lu"), \
|
|
|
|
got->count ## method, want->count ## method)
|
|
|
|
check(Init);
|
|
|
|
check(Free);
|
|
|
|
#undef check
|
2019-06-18 09:59:56 -05:00
|
|
|
|
|
|
|
#define check(event) \
|
|
|
|
if (got->countEvent ## event != want->countEvent ## event) \
|
|
|
|
testingTErrorfFull(t, file, line, "wrong number of fires of uiControl" #event "():" diff("%lu"), \
|
|
|
|
got->countEvent ## event, want->countEvent ## event)
|
|
|
|
check(OnFree);
|
|
|
|
#undef check
|
2019-06-18 09:48:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#define checkCounts(t, got, want) checkCountsFull(t, __FILE__, __LINE__, got, want)
|
|
|
|
|
2019-06-11 20:23:56 -05:00
|
|
|
struct testImplData {
|
2019-06-18 09:48:10 -05:00
|
|
|
struct counts *counts;
|
2019-06-08 20:38:32 -05:00
|
|
|
};
|
|
|
|
|
2019-06-18 09:48:10 -05:00
|
|
|
static struct counts failInit;
|
2019-06-16 12:06:28 -05:00
|
|
|
void *testControlFailInit = &failInit;
|
|
|
|
|
2019-06-17 19:03:57 -05:00
|
|
|
// TODO document that impl data is zero-initialized before this is called
|
2019-06-18 09:48:10 -05:00
|
|
|
// TODO we'll also have to eventually deal with the fact that NULL is not required to be 0... or at least confirm that
|
2019-06-08 20:38:32 -05:00
|
|
|
static bool testVtableInit(uiControl *c, void *implData, void *initData)
|
|
|
|
{
|
2019-06-17 19:03:57 -05:00
|
|
|
struct testImplData *d = (struct testImplData *) implData;
|
2019-06-18 09:48:10 -05:00
|
|
|
struct counts *counts = (struct counts *) initData;
|
2019-06-16 20:44:49 -05:00
|
|
|
|
2019-06-18 09:48:10 -05:00
|
|
|
if (initData == testControlFailInit)
|
2019-06-16 20:44:49 -05:00
|
|
|
return false;
|
2019-06-18 09:48:10 -05:00
|
|
|
if (initData == NULL)
|
2019-06-16 20:44:49 -05:00
|
|
|
return true;
|
2019-06-18 09:48:10 -05:00
|
|
|
if (d->counts == NULL)
|
|
|
|
d->counts = counts;
|
|
|
|
d->counts->countInit++;
|
2019-06-16 20:44:49 -05:00
|
|
|
return true;
|
2019-06-08 20:38:32 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static void testVtableFree(uiControl *c, void *implData)
|
|
|
|
{
|
2019-06-17 19:03:57 -05:00
|
|
|
struct testImplData *d = (struct testImplData *) implData;
|
2019-06-16 20:44:49 -05:00
|
|
|
|
2019-06-18 09:48:10 -05:00
|
|
|
if (d->counts != NULL)
|
|
|
|
d->counts->countFree++;
|
2019-06-08 20:38:32 -05:00
|
|
|
}
|
|
|
|
|
2019-06-15 20:33:53 -05:00
|
|
|
static const uiControlVtable vtable = {
|
2019-06-15 19:48:20 -05:00
|
|
|
.Size = sizeof (uiControlVtable),
|
|
|
|
.Init = testVtableInit,
|
|
|
|
.Free = testVtableFree,
|
2019-06-11 20:23:56 -05:00
|
|
|
};
|
2019-06-15 20:33:53 -05:00
|
|
|
|
|
|
|
const uiControlVtable *testVtable(void)
|
|
|
|
{
|
|
|
|
return &vtable;
|
|
|
|
}
|
2019-06-16 04:45:16 -05:00
|
|
|
|
|
|
|
size_t testImplDataSize(void)
|
|
|
|
{
|
|
|
|
return sizeof (struct testImplData);
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO explicitly make/document 0 as always invalid
|
|
|
|
uint32_t testControlType = 0;
|
2019-06-16 12:06:28 -05:00
|
|
|
uint32_t testControlType2 = 0;
|
2019-06-16 20:44:49 -05:00
|
|
|
|
2019-06-18 09:59:56 -05:00
|
|
|
static void eventFreeHandler(void *sender, void *args, void *data)
|
|
|
|
{
|
|
|
|
struct counts *counts = (struct counts *) data;
|
|
|
|
|
|
|
|
counts->countEventOnFree++;
|
|
|
|
}
|
|
|
|
|
2019-06-16 20:44:49 -05:00
|
|
|
testingTest(ControlMethodsCalled)
|
|
|
|
{
|
|
|
|
uiControl *c;
|
2019-06-18 09:48:10 -05:00
|
|
|
struct counts counts;
|
|
|
|
struct counts want;
|
2019-06-18 09:59:56 -05:00
|
|
|
int handler;
|
2019-06-18 09:48:10 -05:00
|
|
|
|
|
|
|
testingTLogf(t, "*** uiNewControl()");
|
|
|
|
memset(&counts, 0, sizeof (struct counts));
|
|
|
|
c = uiNewControl(testControlType, &counts);
|
|
|
|
memset(&want, 0, sizeof (struct counts));
|
|
|
|
want.countInit = 1;
|
|
|
|
checkCounts(t, &counts, &want);
|
|
|
|
|
|
|
|
testingTLogf(t, "*** uiControlFree()");
|
|
|
|
memset(&counts, 0, sizeof (struct counts));
|
2019-06-18 09:59:56 -05:00
|
|
|
handler = uiEventAddHandler(uiControlOnFree(), eventFreeHandler, c, &counts);
|
2019-06-16 20:44:49 -05:00
|
|
|
uiControlFree(c);
|
2019-06-18 09:59:56 -05:00
|
|
|
uiEventDeleteHandler(uiControlOnFree(), handler);
|
2019-06-18 09:48:10 -05:00
|
|
|
memset(&want, 0, sizeof (struct counts));
|
|
|
|
want.countFree = 1;
|
2019-06-18 09:59:56 -05:00
|
|
|
want.countEventOnFree = 1;
|
2019-06-18 09:48:10 -05:00
|
|
|
checkCounts(t, &counts, &want);
|
2019-06-16 20:44:49 -05:00
|
|
|
}
|