Test: make sure the macros ref/unref are tested

Signed-off-by: Benjamin Tissoires <benjamin.tissoires@gmail.com>
This commit is contained in:
Benjamin Tissoires
2016-03-14 15:58:20 +01:00
parent 090851fce1
commit 8ede0b734f
3 changed files with 67 additions and 1 deletions

View File

@@ -24,6 +24,7 @@
#include "config.h"
#include <errno.h>
#include <libevdev/libevdev.h>
#include "libratbag-private.h"
#include "libratbag-test.h"
@@ -73,6 +74,54 @@ test_set_active_profile(struct ratbag_device *device, unsigned int index)
return 0;
}
static void
test_read_button(struct ratbag_button *button)
{
struct ratbag_device *device = button->profile->device;
struct ratbag_test_device *d = ratbag_get_drv_data(device);
struct ratbag_test_profile *p = &d->profiles[button->profile->index];
struct ratbag_button_macro *m;
const char data[] = "TEST";
const char *c;
switch (p->buttons[button->index].type) {
case RATBAG_BUTTON_ACTION_TYPE_BUTTON:
button->action.type = RATBAG_BUTTON_ACTION_TYPE_BUTTON;
button->action.action.button = p->buttons[button->index].button;
break;
case RATBAG_BUTTON_ACTION_TYPE_KEY:
button->action.type = RATBAG_BUTTON_ACTION_TYPE_KEY;
button->action.action.key.key = p->buttons[button->index].key;
break;
case RATBAG_BUTTON_ACTION_TYPE_MACRO:
button->action.type = RATBAG_BUTTON_ACTION_TYPE_MACRO;
m = ratbag_button_macro_new("test macro");
ARRAY_FOR_EACH(data, c) {
char str[6];
snprintf_safe(str, 6, "KEY_%c", *c);
ratbag_button_macro_set_event(m,
0,
RATBAG_MACRO_EVENT_KEY_PRESSED,
libevdev_event_code_from_name(EV_KEY, str));
ratbag_button_macro_set_event(m,
0,
RATBAG_MACRO_EVENT_KEY_RELEASED,
libevdev_event_code_from_name(EV_KEY, str));
}
ratbag_button_copy_macro(button, m);
ratbag_button_macro_unref(m);
break;
default:
button->action.type = RATBAG_BUTTON_ACTION_TYPE_UNKNOWN;
}
ratbag_button_enable_action_type(button, RATBAG_BUTTON_ACTION_TYPE_BUTTON);
ratbag_button_enable_action_type(button, RATBAG_BUTTON_ACTION_TYPE_KEY);
ratbag_button_enable_action_type(button, RATBAG_BUTTON_ACTION_TYPE_SPECIAL);
ratbag_button_enable_action_type(button, RATBAG_BUTTON_ACTION_TYPE_MACRO);
}
static int
test_fake_probe(struct ratbag_device *device)
{
@@ -116,7 +165,7 @@ struct ratbag_driver test_driver = {
.read_profile = test_read_profile,
.write_profile = test_write_profile,
.set_active_profile = test_set_active_profile,
.read_button = NULL,
.read_button = test_read_button,
.write_button = NULL,
.write_resolution_dpi = NULL,
};

View File

@@ -32,6 +32,12 @@
#define RATBAG_TEST_MAX_RESOLUTIONS 8
struct ratbag_test_button {
enum ratbag_button_action_type type;
union {
int button;
int key;
enum ratbag_button_action_special special;
};
};
struct ratbag_test_resolution {

View File

@@ -655,6 +655,7 @@ START_TEST(device_buttons)
struct ratbag_device *d;
struct ratbag_profile *p;
struct ratbag_button *b;
struct ratbag_button_macro *m;
int nprofiles, nbuttons;
int i, j;
int device_freed_count = 0;
@@ -662,6 +663,8 @@ START_TEST(device_buttons)
struct ratbag_test_device td = sane_device;
td.num_buttons = 10;
td.profiles[0].buttons[8].type = RATBAG_BUTTON_ACTION_TYPE_MACRO;
td.destroyed_data = &device_freed_count;
r = ratbag_create_context(&abort_iface, NULL);
@@ -681,6 +684,14 @@ START_TEST(device_buttons)
b = ratbag_profile_get_button(p, j);
ck_assert(b != NULL);
if (ratbag_button_get_action_type(b) ==
RATBAG_BUTTON_ACTION_TYPE_MACRO) {
m = ratbag_button_get_macro(b);
ck_assert(m != NULL);
ratbag_button_macro_unref(m);
}
ratbag_button_unref(b);
}
ratbag_profile_unref(p);