forked from drath/libratbag
testdrv: make sure .remove() is called only once
And be sure to not call it twice if the sanity check fails. Signed-off-by: Benjamin Tissoires <benjamin.tissoires@gmail.com>
This commit is contained in:
@@ -88,6 +88,13 @@ test_probe(struct ratbag_device *device, const struct ratbag_id id)
|
||||
static void
|
||||
test_remove(struct ratbag_device *device)
|
||||
{
|
||||
struct ratbag_test_device *d = ratbag_get_drv_data(device);
|
||||
|
||||
/* remove must be called only once */
|
||||
assert(d != NULL);
|
||||
if (d->destroyed)
|
||||
d->destroyed(device, d->destroyed_data);
|
||||
ratbag_set_drv_data(device, NULL);
|
||||
}
|
||||
|
||||
struct ratbag_driver test_driver = {
|
||||
|
||||
@@ -54,6 +54,8 @@ struct ratbag_test_device {
|
||||
unsigned int num_profiles;
|
||||
unsigned int num_buttons;
|
||||
struct ratbag_test_profile profiles[RATBAG_TEST_MAX_PROFILES];
|
||||
void (*destroyed)(struct ratbag_device *device, void *data);
|
||||
void *destroyed_data;
|
||||
};
|
||||
|
||||
struct ratbag_device* ratbag_device_new_test_device(struct ratbag *ratbag,
|
||||
|
||||
@@ -412,7 +412,6 @@ ratbag_find_driver(struct ratbag_device *device,
|
||||
rc = driver->probe(device, matched_id);
|
||||
if (rc == 0) {
|
||||
if (!ratbag_sanity_check_device(device)) {
|
||||
driver->remove(device);
|
||||
return NULL;
|
||||
} else {
|
||||
log_debug(ratbag, "driver match found\n");
|
||||
|
||||
@@ -35,6 +35,17 @@
|
||||
#include "libratbag.h"
|
||||
#include "libratbag-test.h"
|
||||
|
||||
static void
|
||||
device_destroyed(struct ratbag_device *device, void *data)
|
||||
{
|
||||
int *count = data;
|
||||
|
||||
if (!data)
|
||||
return;
|
||||
|
||||
++*count;
|
||||
}
|
||||
|
||||
/* A pre-setup sane device. Use for sanity testing by toggling the various
|
||||
* error conditions.
|
||||
*/
|
||||
@@ -73,6 +84,8 @@ const struct ratbag_test_device sane_device = {
|
||||
.dflt = false,
|
||||
},
|
||||
},
|
||||
.destroyed = device_destroyed,
|
||||
.destroyed_data = NULL,
|
||||
};
|
||||
|
||||
static int
|
||||
@@ -101,6 +114,9 @@ START_TEST(device_init)
|
||||
struct ratbag_device *d;
|
||||
int nprofiles, nbuttons;
|
||||
struct ratbag_test_device td = sane_device;
|
||||
int device_freed_count = 0;
|
||||
|
||||
td.destroyed_data = &device_freed_count;
|
||||
|
||||
r = ratbag_create_context(&abort_iface, NULL);
|
||||
d = ratbag_device_new_test_device(r, &td);
|
||||
@@ -113,6 +129,7 @@ START_TEST(device_init)
|
||||
|
||||
ratbag_device_unref(d);
|
||||
ratbag_unref(r);
|
||||
ck_assert_int_eq(device_freed_count, 1);
|
||||
}
|
||||
END_TEST
|
||||
|
||||
@@ -124,9 +141,12 @@ START_TEST(device_profiles)
|
||||
int nprofiles;
|
||||
int i;
|
||||
bool is_active, is_default;
|
||||
int device_freed_count = 0;
|
||||
|
||||
struct ratbag_test_device td = sane_device;
|
||||
|
||||
td.destroyed_data = &device_freed_count;
|
||||
|
||||
r = ratbag_create_context(&abort_iface, NULL);
|
||||
d = ratbag_device_new_test_device(r, &td);
|
||||
|
||||
@@ -145,6 +165,7 @@ START_TEST(device_profiles)
|
||||
}
|
||||
ratbag_device_unref(d);
|
||||
ratbag_unref(r);
|
||||
ck_assert_int_eq(device_freed_count, 1);
|
||||
}
|
||||
END_TEST
|
||||
|
||||
@@ -188,9 +209,12 @@ START_TEST(device_profiles_get_invalid)
|
||||
struct ratbag_device *d;
|
||||
struct ratbag_profile *p;
|
||||
int nprofiles;
|
||||
int device_freed_count = 0;
|
||||
|
||||
struct ratbag_test_device td = sane_device;
|
||||
|
||||
td.destroyed_data = &device_freed_count;
|
||||
|
||||
r = ratbag_create_context(&abort_iface, NULL);
|
||||
d = ratbag_device_new_test_device(r, &td);
|
||||
|
||||
@@ -210,6 +234,7 @@ START_TEST(device_profiles_get_invalid)
|
||||
|
||||
ratbag_device_unref(d);
|
||||
ratbag_unref(r);
|
||||
ck_assert_int_eq(device_freed_count, 1);
|
||||
}
|
||||
END_TEST
|
||||
|
||||
@@ -222,6 +247,7 @@ START_TEST(device_resolutions)
|
||||
int nprofiles, nresolutions;
|
||||
int i, j;
|
||||
int xres, yres, rate;
|
||||
int device_freed_count = 0;
|
||||
|
||||
struct ratbag_test_device td = {
|
||||
.num_profiles = 3,
|
||||
@@ -253,6 +279,8 @@ START_TEST(device_resolutions)
|
||||
},
|
||||
},
|
||||
},
|
||||
.destroyed = device_destroyed,
|
||||
.destroyed_data = &device_freed_count,
|
||||
};
|
||||
|
||||
r = ratbag_create_context(&abort_iface, NULL);
|
||||
@@ -284,6 +312,7 @@ START_TEST(device_resolutions)
|
||||
}
|
||||
ratbag_device_unref(d);
|
||||
ratbag_unref(r);
|
||||
ck_assert_int_eq(device_freed_count, 1);
|
||||
}
|
||||
END_TEST
|
||||
|
||||
|
||||
Reference in New Issue
Block a user