test: Make relative pointer event test more accepting
authorJonas Ådahl <jadahl@gmail.com>
Mon, 26 May 2014 20:12:25 +0000 (22:12 +0200)
committerJonas Ådahl <jadahl@gmail.com>
Thu, 29 May 2014 11:06:23 +0000 (13:06 +0200)
A test cannot exactly predict the resulting motion event from a given
evdev event series without having to reimplement the acceleration
algorithm. To still be able to test that sane relative motion events are
produced, check that the length and direction of the resulting motion
event vectors are close to the same as the expected vectors.

Signed-off-by: Jonas Ådahl <jadahl@gmail.com>
Reviewed-by: Hans de Goede <hdegoede@redhat.com>
test/pointer.c

index cae9f09728e50479e542fdb83bf20e63fb299377..f47e09493fa9a575afb66ef99ae4f6ec677249c7 100644 (file)
@@ -27,6 +27,7 @@
 #include <errno.h>
 #include <fcntl.h>
 #include <libinput.h>
+#include <math.h>
 #include <unistd.h>
 
 #include "libinput-util.h"
@@ -38,7 +39,17 @@ test_relative_event(struct litest_device *dev, int dx, int dy)
        struct libinput *li = dev->libinput;
        struct libinput_event *event;
        struct libinput_event_pointer *ptrev;
-
+       double ev_dx, ev_dy;
+       double expected_dir;
+       double expected_length;
+       double actual_dir;
+       double actual_length;
+
+       /* Send two deltas, as the first one may be eaten up by an
+        * acceleration filter. */
+       litest_event(dev, EV_REL, REL_X, dx);
+       litest_event(dev, EV_REL, REL_Y, dy);
+       litest_event(dev, EV_SYN, SYN_REPORT, 0);
        litest_event(dev, EV_REL, REL_X, dx);
        litest_event(dev, EV_REL, REL_Y, dy);
        litest_event(dev, EV_SYN, SYN_REPORT, 0);
@@ -51,10 +62,25 @@ test_relative_event(struct litest_device *dev, int dx, int dy)
 
        ptrev = libinput_event_get_pointer_event(event);
        ck_assert(ptrev != NULL);
-       ck_assert_int_eq(libinput_event_pointer_get_dx(ptrev), li_fixed_from_int(dx));
-       ck_assert_int_eq(libinput_event_pointer_get_dy(ptrev), li_fixed_from_int(dy));
+
+       expected_length = sqrt(dx*dx + dy*dy);
+       expected_dir = atan2(dx, dy);
+
+       ev_dx = li_fixed_to_double(libinput_event_pointer_get_dx(ptrev));
+       ev_dy = li_fixed_to_double(libinput_event_pointer_get_dy(ptrev));
+       actual_length = sqrt(ev_dx*ev_dx + ev_dy*ev_dy);
+       actual_dir = atan2(ev_dx, ev_dy);
+
+       /* Check the length of the motion vector (tolerate 1.0 indifference). */
+       ck_assert(fabs(expected_length - actual_length) < 1.0);
+
+       /* Check the direction of the motion vector (tolerate 2π/4 radians
+        * indifference). */
+       ck_assert(fabs(expected_dir - actual_dir) < M_PI_2);
 
        libinput_event_destroy(event);
+
+       litest_drain_events(dev->libinput);
 }
 
 START_TEST(pointer_motion_relative)