1 #define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
2 #include "doctest.h"
3 #include <wayfire/touch/touch.hpp>
4 
5 using namespace wf::touch;
6 
7 TEST_CASE("wf::touch::gesture_t")
8 {
9     int completed = 0;
10     int cancelled = 0;
11     gesture_callback_t callback1 = [&] ()
__anonc7e8b90d0102() 12     {
13         ++completed;
14     };
15 
16     gesture_callback_t callback2 = [&] ()
__anonc7e8b90d0202() 17     {
18         ++cancelled;
19     };
20 
21     auto down = std::make_unique<touch_action_t>(1, true);
22     auto delay1 = std::make_unique<hold_action_t>(5);
23     auto drag1 = std::make_unique<drag_action_t>(MOVE_DIRECTION_LEFT, 10);
24     auto delay2 = std::make_unique<hold_action_t>(5);
25     auto drag2 = std::make_unique<drag_action_t>(MOVE_DIRECTION_RIGHT, 10);
26     std::vector<std::unique_ptr<gesture_action_t>> actions;
27     actions.emplace_back(std::move(down));
28     actions.emplace_back(std::move(delay1));
29     actions.emplace_back(std::move(drag1));
30     actions.emplace_back(std::move(delay2));
31     actions.emplace_back(std::move(drag2));
32     gesture_t swipe{std::move(actions), callback1, callback2};
33 
34     swipe.reset(0);
35     gesture_event_t touch_down;
36     touch_down.finger = 0;
37     touch_down.pos = {0, 0};
38     touch_down.type = EVENT_TYPE_TOUCH_DOWN;
39     touch_down.time = 0;
40     swipe.update_state(touch_down);
41     CHECK(swipe.get_progress() >= 0.2);
42 
43     SUBCASE("complete")
44     {
45         gesture_event_t motion_left;
46         motion_left.finger = 0;
47         motion_left.pos = {-10, 0};
48         motion_left.time = 10;
49         motion_left.type = EVENT_TYPE_MOTION;
50         swipe.update_state(motion_left);
51 
52         CHECK(cancelled == 0);
53         CHECK(completed == 0);
54         CHECK(swipe.get_progress() >= 0.6);
55 
56         gesture_event_t motion_right = motion_left;
57         motion_right.pos = {0, 0};
58         motion_right.time = 20;
59         swipe.update_state(motion_right);
60         CHECK(cancelled == 0);
61         CHECK(completed == 1);
62 
63         SUBCASE("restart")
64         {
65             swipe.reset(0);
66             CHECK(swipe.get_progress() == 0.0);
67             swipe.update_state(touch_down);
68             swipe.update_state(motion_left);
69             swipe.update_state(motion_right);
70             CHECK(cancelled == 0);
71             CHECK(completed == 2);
72         }
73     }
74 
75     SUBCASE("cancelled")
76     {
77         touch_down.finger = 1;
78         swipe.update_state(touch_down);
79         CHECK(cancelled == 1);
80         CHECK(completed == 0);
81     }
82 }
83