summaryrefslogtreecommitdiffhomepage
path: root/packages/ext-xdg-shell/tests/test_policy.cpp
blob: 3a95e6b873192d5662124eda199f5c54e936fd5e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
#include <doctest/doctest.h>

#include "policy.hpp"

#include <cstdint>

// Pure decision core — strict, no wlroots, no kernel running. Exercises the
// interactive-grab state machine (the user-observed drag bug).
// Keybinding tests removed: compositor keybindings now live in ext-keybindings.

using namespace unbox::ext_xdg_shell::policy;

// ---- interactive grab state machine (the user-observed move bug) -----------

TEST_CASE("the exact user scenario: press -> request_move -> motion follows; "
          "release -> motion is passthrough") {
    GrabMachine g;
    // Titlebar press: button goes down. No grab yet (client hasn't asked).
    CHECK(g.on_button(/*pressed=*/true) == GrabAction::none);
    CHECK_FALSE(g.grabbing());
    CHECK(g.on_motion() == GrabAction::none); // nothing requested: passthrough

    // Client responds to the press with xdg_toplevel.move while button HELD.
    CHECK(g.on_request_move());
    CHECK(g.grabbing());

    // Motion WHILE HELD must move the toplevel (the bug: it didn't).
    CHECK(g.on_motion() == GrabAction::move_toplevel);
    CHECK(g.on_motion() == GrabAction::move_toplevel); // every held motion

    // Release ends the grab.
    CHECK(g.on_button(/*pressed=*/false) == GrabAction::end_grab);
    CHECK_FALSE(g.grabbing());

    // Motion WITHOUT clicking after release must NOT move (the bug: it did).
    CHECK(g.on_motion() == GrabAction::none);
}

TEST_CASE("a move request that arrives with NO button down does not grab") {
    // The deferred-request race: if request_move lands after the release, the
    // button is already up, so no unclicked drag may start.
    GrabMachine g;
    g.on_button(true);
    g.on_button(false);             // pressed then released, no request yet
    CHECK_FALSE(g.on_request_move()); // late request: button is up
    CHECK_FALSE(g.grabbing());
    CHECK(g.on_motion() == GrabAction::none);
}

TEST_CASE("resize grab: held motion resizes, release ends it") {
    GrabMachine g;
    g.on_button(true);
    CHECK(g.on_request_resize());
    CHECK(g.mode() == GrabMode::resize);
    CHECK(g.on_motion() == GrabAction::resize_toplevel);
    CHECK(g.on_button(false) == GrabAction::end_grab);
    CHECK(g.on_motion() == GrabAction::none);
}

TEST_CASE("grab target lost (unmap/destroy mid-drag) drops the grab silently") {
    GrabMachine g;
    g.on_button(true);
    g.on_request_move();
    CHECK(g.grabbing());
    g.on_grab_target_lost();
    CHECK_FALSE(g.grabbing());
    CHECK(g.on_motion() == GrabAction::none);
    // A later release with no active grab is a no-op (not an end_grab).
    CHECK(g.on_button(false) == GrabAction::none);
}

TEST_CASE("button-down state tracks across presses without a grab") {
    GrabMachine g;
    CHECK_FALSE(g.button_down());
    g.on_button(true);
    CHECK(g.button_down());
    g.on_button(false);
    CHECK_FALSE(g.button_down());
}

// ---- touch-initiated grab (the user-found gap) -----------------------------

TEST_CASE("touch titlebar drag: down -> request_move -> touch motion follows -> "
          "up ends") {
    GrabMachine g;
    constexpr std::int32_t id = 7;
    // Touch lands on the client's CSD titlebar.
    g.on_touch_down(id);
    CHECK_FALSE(g.grabbing());
    // Client responds with xdg_toplevel.move (no POINTER button down).
    CHECK(g.on_request_move());
    CHECK(g.grabbing());
    CHECK(g.touch_driven());
    // Pointer motion must NOT drive a touch grab (source isolation).
    CHECK(g.on_motion() == GrabAction::none);
    // Motion of the originating touch point moves the toplevel.
    CHECK(g.on_touch_motion(id) == GrabAction::move_toplevel);
    CHECK(g.on_touch_motion(id) == GrabAction::move_toplevel);
    // The originating point lifting ends the grab.
    CHECK(g.on_touch_up(id) == GrabAction::end_grab);
    CHECK_FALSE(g.grabbing());
    CHECK(g.on_touch_motion(id) == GrabAction::none);
}

TEST_CASE("touch resize grab follows the originating point and ends on its up") {
    GrabMachine g;
    constexpr std::int32_t id = 3;
    g.on_touch_down(id);
    CHECK(g.on_request_resize());
    CHECK(g.mode() == GrabMode::resize);
    CHECK(g.on_touch_motion(id) == GrabAction::resize_toplevel);
    CHECK(g.on_touch_up(id) == GrabAction::end_grab);
}

TEST_CASE("a move request after the touch point lifted is ignored (no late drag)") {
    GrabMachine g;
    constexpr std::int32_t id = 1;
    g.on_touch_down(id);
    g.on_touch_up(id);                 // lifted before the request arrives
    CHECK_FALSE(g.on_request_move());  // nothing down -> no grab
    CHECK_FALSE(g.grabbing());
    CHECK(g.on_touch_motion(id) == GrabAction::none);
}

TEST_CASE("a second touch point does not steer or end a touch-driven grab") {
    GrabMachine g;
    constexpr std::int32_t origin = 10;
    constexpr std::int32_t other = 20;
    g.on_touch_down(origin);
    CHECK(g.on_request_move());
    CHECK(g.touch_driven());
    // A second finger goes down and moves: it must NOT drive the grab.
    g.on_touch_down(other);
    CHECK(g.on_touch_motion(other) == GrabAction::none);
    // The originating point still drives it.
    CHECK(g.on_touch_motion(origin) == GrabAction::move_toplevel);
    // The second point lifting must NOT end the grab.
    CHECK(g.on_touch_up(other) == GrabAction::none);
    CHECK(g.grabbing());
    // Only the originating point's up ends it.
    CHECK(g.on_touch_up(origin) == GrabAction::end_grab);
    CHECK_FALSE(g.grabbing());
}

TEST_CASE("touch cancel of the originating point ends the grab") {
    GrabMachine g;
    constexpr std::int32_t id = 5;
    g.on_touch_down(id);
    g.on_request_move();
    CHECK(g.grabbing());
    CHECK(g.on_touch_cancel(id) == GrabAction::end_grab);
    CHECK_FALSE(g.grabbing());
}

TEST_CASE("source isolation: a pointer release does not end a touch-driven grab") {
    GrabMachine g;
    constexpr std::int32_t id = 9;
    // Pointer happens to be down too, but touch is preferred and pins the grab.
    g.on_button(true);
    g.on_touch_down(id);
    CHECK(g.on_request_move());
    CHECK(g.touch_driven());
    // Releasing the pointer button must NOT end a touch-driven grab.
    CHECK(g.on_button(false) == GrabAction::none);
    CHECK(g.grabbing());
    // The touch point's up ends it.
    CHECK(g.on_touch_up(id) == GrabAction::end_grab);
}

TEST_CASE("pointer grab still works and touch motion does not drive it") {
    GrabMachine g;
    constexpr std::int32_t id = 2;
    g.on_button(true);
    CHECK(g.on_request_move());
    CHECK_FALSE(g.touch_driven());
    // A stray touch point's motion must not steer a pointer-driven grab.
    g.on_touch_down(id);
    CHECK(g.on_touch_motion(id) == GrabAction::none);
    CHECK(g.on_motion() == GrabAction::move_toplevel);
    CHECK(g.on_button(false) == GrabAction::end_grab);
}

// ---- the regression: a grab must never poison the NEXT grab ----------------
// (The user repro lived in the GLUE — a missing wlr_seat button-release left
// the seat's implicit pointer grab stuck, swallowing later touch-downs. The
// MACHINE-level sequence below proves the pure state carries nothing forward;
// the glue fix is regression-noted in the package doc.)

namespace {
// Helper: one full grab cycle of each kind, asserting it engages and ends and
// leaves the machine idle.
void touch_grab_cycle(GrabMachine& g, std::int32_t id) {
    g.on_touch_down(id);
    REQUIRE(g.on_request_move());
    REQUIRE(g.touch_driven());
    REQUIRE(g.on_touch_motion(id) == GrabAction::move_toplevel);
    REQUIRE(g.on_touch_up(id) == GrabAction::end_grab);
    REQUIRE_FALSE(g.grabbing());
}
void pointer_grab_cycle(GrabMachine& g) {
    g.on_button(true);
    REQUIRE(g.on_request_move());
    REQUIRE_FALSE(g.touch_driven());
    REQUIRE(g.on_motion() == GrabAction::move_toplevel);
    REQUIRE(g.on_button(false) == GrabAction::end_grab);
    REQUIRE_FALSE(g.grabbing());
}
} // namespace

TEST_CASE("EXACT user repro: touch grab -> pointer grab -> touch grab engages again") {
    GrabMachine g;
    touch_grab_cycle(g, 1);
    pointer_grab_cycle(g);
    // The third grab — a touch grab again — MUST engage (the regression).
    g.on_touch_down(2);
    CHECK(g.on_request_move());
    CHECK(g.touch_driven());
    CHECK(g.on_touch_motion(2) == GrabAction::move_toplevel);
    CHECK(g.on_touch_up(2) == GrabAction::end_grab);
}

TEST_CASE("mirrored order: pointer grab -> touch grab -> pointer grab engages again") {
    GrabMachine g;
    pointer_grab_cycle(g);
    touch_grab_cycle(g, 5);
    g.on_button(true);
    CHECK(g.on_request_move());
    CHECK_FALSE(g.touch_driven());
    CHECK(g.on_motion() == GrabAction::move_toplevel);
    CHECK(g.on_button(false) == GrabAction::end_grab);
}

TEST_CASE("many alternating grabs leave no residue (count never poisons engage)") {
    GrabMachine g;
    for (int i = 0; i < 5; ++i) {
        touch_grab_cycle(g, 100 + i);
        pointer_grab_cycle(g);
    }
    // Still works after the loop.
    touch_grab_cycle(g, 999);
}

// ---- interleaving rule: the OTHER input pressed during an active grab -------
// Rule (defined here): an input event from the NON-driving source while a grab
// is active never changes the grab — the grab stays pinned to its originator
// and ends only on the originator's release/up. The newly-pressed other input
// becomes available to drive the NEXT grab once this one ends.

TEST_CASE("pointer press DURING an active touch grab does not hijack or end it") {
    GrabMachine g;
    constexpr std::int32_t id = 7;
    g.on_touch_down(id);
    REQUIRE(g.on_request_move());
    REQUIRE(g.touch_driven());

    // Pointer goes down mid-touch-grab: must not change the grab.
    CHECK(g.on_button(true) == GrabAction::none);
    CHECK(g.touch_driven());
    CHECK(g.on_touch_motion(id) == GrabAction::move_toplevel); // touch still drives
    CHECK(g.on_motion() == GrabAction::none);                  // pointer does not

    // Pointer release mid-touch-grab must NOT end the touch grab.
    CHECK(g.on_button(false) == GrabAction::none);
    CHECK(g.grabbing());

    // Only the originating touch up ends it.
    CHECK(g.on_touch_up(id) == GrabAction::end_grab);
    CHECK_FALSE(g.grabbing());
}

TEST_CASE("touch down DURING an active pointer grab does not hijack or end it") {
    GrabMachine g;
    constexpr std::int32_t id = 8;
    g.on_button(true);
    REQUIRE(g.on_request_move());
    REQUIRE_FALSE(g.touch_driven());

    // A finger touches down mid-pointer-grab: must not change the grab.
    g.on_touch_down(id);
    CHECK_FALSE(g.touch_driven());
    CHECK(g.on_motion() == GrabAction::move_toplevel);          // pointer still drives
    CHECK(g.on_touch_motion(id) == GrabAction::none);           // touch does not

    // That touch lifting must NOT end the pointer grab.
    CHECK(g.on_touch_up(id) == GrabAction::none);
    CHECK(g.grabbing());

    // Only the pointer release ends it.
    CHECK(g.on_button(false) == GrabAction::end_grab);
    CHECK_FALSE(g.grabbing());

    // And a fresh touch grab engages right after.
    touch_grab_cycle(g, id + 1);
}