File: test_surface_input_dispatcher.cpp

package info (click to toggle)
mir 2.20.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 20,636 kB
  • sloc: cpp: 174,574; xml: 13,422; ansic: 8,221; python: 1,337; sh: 874; makefile: 216; javascript: 37
file content (670 lines) | stat: -rw-r--r-- 21,616 bytes parent folder | download
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
/*
 * Copyright © Canonical Ltd.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 or 3 as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "src/server/input/surface_input_dispatcher.h"

#include "mir/events/event_builders.h"
#include "mir/events/event_private.h"
#include "mir/scene/observer.h"
#include "mir/scene/surface_observer.h"
#include "mir/thread_safe_list.h"

#include "mir/test/event_matchers.h"
#include "mir/test/fake_shared.h"
#include "mir/test/doubles/stub_input_scene.h"
#include "mir/test/doubles/mock_surface.h"
#include "mir/test/doubles/explicit_executor.h"

#include <gtest/gtest.h>
#include <gmock/gmock.h>

#include <mutex>
#include <algorithm>

namespace ms = mir::scene;
namespace mi = mir::input;
namespace mev = mir::events;
namespace mt = mir::test;
namespace mtd = mt::doubles;
namespace geom = mir::geometry;

using namespace ::testing;

namespace
{

struct MockSurfaceWithGeometry : public mtd::MockSurface
{
    MockSurfaceWithGeometry(geom::Rectangle const& geom)
        : geom(geom)
    {
    }

    bool input_area_contains(geom::Point const& p) const override
    {
        return geom.contains(p);
    }

    geom::Rectangle input_bounds() const override
    {
        return geom;
    }
    
    geom::Rectangle const geom;
};

struct StubInputScene : public mtd::StubInputScene
{
    std::shared_ptr<mtd::MockSurface> add_surface(geom::Rectangle const& geometry)
    {
        auto surface = std::make_shared<NiceMock<MockSurfaceWithGeometry>>(geometry);
        surfaces.add(surface);

        observer->surface_added(surface);
        
        return surface;
    }

    void remove_surface(std::shared_ptr<ms::Surface> const& surface)
    {
        surfaces.remove(surface);
        observer->surface_removed(surface);
    }

    std::shared_ptr<mtd::MockSurface> add_surface()
    {
        return add_surface({{0, 0}, {1, 1}});
    }

    auto input_surface_at(geom::Point point) const -> std::shared_ptr<mi::Surface> override
    {
        std::shared_ptr<mi::Surface> result;
        surfaces.for_each([&](auto surface)
            {
                if (surface->input_area_contains(point))
                {
                    result = surface;
                }
            });
        return result;
    }

    void add_observer(std::shared_ptr<ms::Observer> const& new_observer) override
    {
        assert(observer == nullptr);
        observer = new_observer;
	surfaces.for_each([this](std::shared_ptr<ms::Surface> const& surface) {
		observer->surface_exists(surface);
        });
    }
    
    void remove_observer(std::weak_ptr<ms::Observer> const& /* remove_observer */) override
    {
        assert(observer != nullptr);
        observer->end_observation();
        observer.reset();
    }

    bool screen_is_locked() const override
    {
        return is_locked;
    }
    
    mir::ThreadSafeList<std::shared_ptr<ms::Surface>> mutable surfaces;

    std::shared_ptr<ms::Observer> observer;
    bool is_locked = false;
};

struct SurfaceInputDispatcher : public testing::Test
{
    SurfaceInputDispatcher()
        : dispatcher(mt::fake_shared(scene))
    {
    }

    void TearDown() override { dispatcher.stop(); }

    StubInputScene scene;
    mi::SurfaceInputDispatcher dispatcher;
};

struct FakeKeyboard
{
    FakeKeyboard(MirInputDeviceId id = 0)
        : id(id)
    {
    }
    mir::EventUPtr press(int scan_code = 7)
    {
        return mev::make_key_event(
            id, std::chrono::nanoseconds(0),
            mir_keyboard_action_down, 0, scan_code, mir_input_event_modifier_alt);
    }
    mir::EventUPtr release(int scan_code = 7)
    {
        return mev::make_key_event(
            id, std::chrono::nanoseconds(0),
            mir_keyboard_action_up, 0, scan_code, mir_input_event_modifier_alt);
    }
    MirInputDeviceId const id;
};

struct FakePointer
{
    FakePointer(MirInputDeviceId id = 0)
        : id(id),
          buttons(0)
    {
    }

    mir::EventUPtr move_to(geom::Point const& location)
    {
        return mev::make_pointer_event(
            id, std::chrono::nanoseconds(0),
            0, mir_pointer_action_motion, buttons,
            location.x.as_int(), location.y.as_int(),
            0, 0, 0, 0);
    }
    mir::EventUPtr release_button(geom::Point const& location, MirPointerButton button = mir_pointer_button_primary)
    {
        buttons &= ~button;

        return mev::make_pointer_event(
            id, std::chrono::nanoseconds(0),
            0, mir_pointer_action_button_up, buttons,
            location.x.as_int(), location.y.as_int(),
            0, 0, 0, 0);
    }
    mir::EventUPtr press_button(geom::Point const& location, MirPointerButton button = mir_pointer_button_primary)
    {
        buttons |= button;
        
        return mev::make_pointer_event(
            id, std::chrono::nanoseconds(0),
            0, mir_pointer_action_button_down, buttons,
            location.x.as_int(), location.y.as_int(),
            0, 0, 0, 0);
    }

    MirInputDeviceId const id;
    MirPointerButtons buttons;
};

struct FakeToucher
{
    FakeToucher(MirInputDeviceId id = 0)
        : id(id)
    {
    }
    
    mir::EventUPtr move_to(geom::Point const& point)
    {
        auto ev = mev::make_touch_event(id, std::chrono::nanoseconds(0), 0);
        mev::add_touch(*ev, 0, mir_touch_action_change,
                       mir_touch_tooltype_finger, point.x.as_int(), point.y.as_int(),
                       touched ? 1.0 : 0.0,
                       touched ? 1.0 : 0.0,
                       touched ? 1.0 : 0.0,
                       touched ? 1.0 : 0.0);
        return ev;
    }
    mir::EventUPtr touch_at(geom::Point const& point)
    {
        touched = true;
        
        auto ev = mev::make_touch_event(id, std::chrono::nanoseconds(0), 0);
        mev::add_touch(*ev, 0, mir_touch_action_down,
                       mir_touch_tooltype_finger, point.x.as_int(), point.y.as_int(),
                       1.0, 1.0, 1.0, 1.0);
        return ev;
    }
    mir::EventUPtr touches_at(geom::Point const& point1, geom::Point const& point2)
    {
        touched = true;

        auto ev = mev::make_touch_event(id, std::chrono::nanoseconds(0), 0);
        mev::add_touch(*ev, 0, mir_touch_action_down,
                       mir_touch_tooltype_finger, point1.x.as_int(), point1.y.as_int(),
                       1.0, 1.0, 1.0, 1.0);
        mev::add_touch(*ev, 0, mir_touch_action_down,
                       mir_touch_tooltype_finger, point2.x.as_int(), point2.y.as_int(),
                       1.0, 1.0, 1.0, 1.0);
        return ev;
    }
    mir::EventUPtr release_at(geom::Point const& point)
    {
        touched = false;
        
        auto ev = mev::make_touch_event(id, std::chrono::nanoseconds(0), 0);
        mev::add_touch(*ev, 0, mir_touch_action_up,
                       mir_touch_tooltype_finger, point.x.as_int(), point.y.as_int(),
                       0.0, 0.0, 0.0, 0.0);
        return ev;
    }
    mir::EventUPtr releases_at(geom::Point const& point1, geom::Point const& point2)
    {
        touched = false;

        auto ev = mev::make_touch_event(id, std::chrono::nanoseconds(0), 0);
        mev::add_touch(*ev, 0, mir_touch_action_up,
                       mir_touch_tooltype_finger, point1.x.as_int(), point1.y.as_int(),
                       1.0, 1.0, 1.0, 1.0);
        mev::add_touch(*ev, 0, mir_touch_action_up,
                       mir_touch_tooltype_finger, point2.x.as_int(), point2.y.as_int(),
                       1.0, 1.0, 1.0, 1.0);
        return ev;
    }
    bool touched = false;
    MirInputDeviceId const id;
};

struct MockKeyboardObserver: mi::KeyboardObserver
{
    MOCK_METHOD1(keyboard_event, void(std::shared_ptr<MirEvent const> const& event));
    MOCK_METHOD1(keyboard_focus_set, void(std::shared_ptr<mi::Surface> const& surface));
};

}

TEST_F(SurfaceInputDispatcher, key_event_not_delivered_to_surface)
{
    auto surface = scene.add_surface();

    FakeKeyboard keyboard;
    auto event = keyboard.press();

    EXPECT_CALL(*surface, consume(_)).Times(0);

    dispatcher.start();

    dispatcher.set_focus(surface);
    dispatcher.dispatch(std::move(event));
}

TEST_F(SurfaceInputDispatcher, key_event_delivered_to_keyboard_observer)
{
    auto surface = scene.add_surface();
    mtd::ExplicitExecutor executor;

    FakeKeyboard keyboard;
    auto event = keyboard.press();

    auto const kb_observer = std::make_shared<MockKeyboardObserver>();
    EXPECT_CALL(*kb_observer, keyboard_event(mt::MirKeyboardEventMatches(event.get())));

    dispatcher.start();
    dispatcher.register_interest(kb_observer, executor);
    dispatcher.dispatch(std::move(event));
    executor.execute();
}

TEST_F(SurfaceInputDispatcher, keyboard_focus_delivered_to_keyboard_observer)
{
    auto surface = scene.add_surface();
    mtd::ExplicitExecutor executor;

    auto const kb_observer = std::make_shared<MockKeyboardObserver>();
    EXPECT_CALL(*kb_observer, keyboard_focus_set(Eq(surface)));

    dispatcher.start();
    dispatcher.register_interest(kb_observer, executor);
    dispatcher.set_focus(surface);
    executor.execute();
}

TEST_F(SurfaceInputDispatcher, keyboard_focus_clear_delivered_to_keyboard_observer)
{
    auto surface = scene.add_surface();
    mtd::ExplicitExecutor executor;

    auto const kb_observer = std::make_shared<NiceMock<MockKeyboardObserver>>();

    dispatcher.start();
    dispatcher.register_interest(kb_observer, executor);
    dispatcher.set_focus(surface);
    executor.execute();

    EXPECT_CALL(*kb_observer, keyboard_focus_set(Eq(nullptr)));
    dispatcher.set_focus(nullptr);
    executor.execute();
}

TEST_F(SurfaceInputDispatcher, keyboard_focus_clear_delivered_to_keyboard_observer_when_screen_is_locked)
{
    auto surface = scene.add_surface();
    mtd::ExplicitExecutor executor;

    auto const kb_observer = std::make_shared<NiceMock<MockKeyboardObserver>>();

    dispatcher.start();
    dispatcher.register_interest(kb_observer, executor);
    dispatcher.set_focus(surface);
    executor.execute();

    EXPECT_CALL(*kb_observer, keyboard_focus_set(Eq(nullptr)));
    scene.is_locked = true;
    scene.observer->scene_changed();
    executor.execute();
}

TEST_F(SurfaceInputDispatcher, keyboard_focus_not_set_when_screen_is_locked)
{
    auto surface = scene.add_surface();
    mtd::ExplicitExecutor executor;

    auto const kb_observer = std::make_shared<NiceMock<MockKeyboardObserver>>();

    dispatcher.start();
    dispatcher.register_interest(kb_observer, executor);
    executor.execute();

    scene.is_locked = true;
    scene.observer->scene_changed();
    executor.execute();

    EXPECT_CALL(*kb_observer, keyboard_focus_set(_)).Times(0);
    dispatcher.set_focus(surface);
    executor.execute();
}

TEST_F(SurfaceInputDispatcher, keyboard_focus_can_be_set_after_screen_is_unlocked)
{
    auto surface = scene.add_surface();
    mtd::ExplicitExecutor executor;

    auto const kb_observer = std::make_shared<NiceMock<MockKeyboardObserver>>();

    dispatcher.start();
    dispatcher.register_interest(kb_observer, executor);
    executor.execute();

    scene.is_locked = true;
    scene.observer->scene_changed();
    executor.execute();

    scene.is_locked = false;
    scene.observer->scene_changed();
    executor.execute();

    EXPECT_CALL(*kb_observer, keyboard_focus_set(Eq(surface)));
    dispatcher.set_focus(surface);
    executor.execute();
}

TEST_F(SurfaceInputDispatcher, pointer_motion_delivered_to_client_under_pointer)
{
    auto surface = scene.add_surface({{0, 0}, {5, 5}});

    FakePointer pointer;
    auto ev_1 = pointer.move_to({1, 0});
    auto ev_2 = pointer.move_to({5, 0});

    InSequence seq;
    EXPECT_CALL(*surface, consume(mt::PointerEnterEvent())).Times(1);
    EXPECT_CALL(*surface, consume(mt::PointerLeaveEvent())).Times(1);

    dispatcher.start();

    EXPECT_TRUE(dispatcher.dispatch(pointer.move_to({1, 0})));
    EXPECT_TRUE(dispatcher.dispatch(pointer.move_to({5, 0})));
}

TEST_F(SurfaceInputDispatcher, pointer_delivered_only_to_top_surface)
{
    auto surface = scene.add_surface({{0, 0}, {5, 5}});
    auto top_surface = scene.add_surface({{0, 0}, {5, 5}});

    FakePointer pointer;

    InSequence seq;
    EXPECT_CALL(*top_surface, consume(mt::PointerEnterEvent())).Times(1);
    EXPECT_CALL(*surface, consume(mt::PointerEnterEvent())).Times(1);

    dispatcher.start();

    EXPECT_TRUE(dispatcher.dispatch(pointer.move_to({1, 0})));
    scene.remove_surface(top_surface);
    EXPECT_TRUE(dispatcher.dispatch(pointer.move_to({1, 0})));
}

TEST_F(SurfaceInputDispatcher, pointer_may_move_between_adjacent_surfaces)
{
    auto surface = scene.add_surface({{0, 0}, {5, 5}});
    auto another_surface = scene.add_surface({{5, 5}, {5, 5}});

    FakePointer pointer;
    auto ev_1 = pointer.move_to({6, 6});
    auto ev_2 = pointer.move_to({7, 7});

    InSequence seq;
    EXPECT_CALL(*surface, consume(mt::PointerEnterEvent())).Times(1);
    EXPECT_CALL(*surface, consume(mt::PointerLeaveEvent())).Times(1);
    EXPECT_CALL(*another_surface, consume(mt::PointerEnterEvent())).Times(1);
    EXPECT_CALL(*another_surface, consume(mt::PointerLeaveEvent())).Times(1);

    dispatcher.start();

    EXPECT_TRUE(dispatcher.dispatch(pointer.move_to({1, 1})));
    EXPECT_TRUE(dispatcher.dispatch(pointer.move_to({6, 6})));
    EXPECT_TRUE(dispatcher.dispatch(pointer.move_to({11, 11})));
}

// We test that a client will receive pointer events following a button down
// until the pointer comes up.
TEST_F(SurfaceInputDispatcher, gestures_persist_over_button_down)
{
    auto surface = scene.add_surface({{0, 0}, {5, 5}});
    auto another_surface = scene.add_surface({{5, 5}, {5, 5}});

    FakePointer pointer;
    auto ev_1 = pointer.press_button({0, 0});
    auto ev_2 = pointer.move_to({6, 6});
    auto ev_3 = pointer.release_button({6, 6});

    InSequence seq;
    EXPECT_CALL(*surface, consume(mt::PointerEnterEvent())).Times(1);
    EXPECT_CALL(*surface, consume(mt::ButtonDownEvent(0,0))).Times(1);
    EXPECT_CALL(*surface, consume(mt::PointerEventWithPosition(6, 6))).Times(1);
    EXPECT_CALL(*surface, consume(mt::ButtonUpEvent(6,6))).Times(1);
    EXPECT_CALL(*surface, consume(mt::PointerLeaveEvent())).Times(1);
    EXPECT_CALL(*another_surface, consume(mt::PointerEnterEvent())).Times(1);
    
    dispatcher.start();

    EXPECT_TRUE(dispatcher.dispatch(std::move(ev_1)));
    EXPECT_TRUE(dispatcher.dispatch(std::move(ev_2)));
    EXPECT_TRUE(dispatcher.dispatch(std::move(ev_3)));
}

TEST_F(SurfaceInputDispatcher, pointer_gestures_may_transfer_over_buttons)
{
    auto surface = scene.add_surface({{0, 0}, {5, 5}});
    auto another_surface = scene.add_surface({{5, 5}, {5, 5}});

    FakePointer pointer;
    auto ev_1 = pointer.press_button({0, 0}, mir_pointer_button_primary);
    auto ev_2 = pointer.press_button({0, 0}, mir_pointer_button_secondary);
    auto ev_3 = pointer.release_button({0, 0}, mir_pointer_button_primary);
    auto ev_4 = pointer.move_to({6, 6});
    auto ev_5 = pointer.release_button({6, 6}, mir_pointer_button_secondary);

    InSequence seq;
    EXPECT_CALL(*surface, consume(mt::PointerEnterEvent())).Times(1);
    EXPECT_CALL(*surface, consume(mt::ButtonDownEvent(0,0))).Times(1);
    EXPECT_CALL(*surface, consume(mt::ButtonDownEvent(0,0))).Times(1);
    EXPECT_CALL(*surface, consume(mt::ButtonUpEvent(0,0))).Times(1);
    EXPECT_CALL(*surface, consume(mt::PointerEventWithPosition(6, 6))).Times(1);
    EXPECT_CALL(*surface, consume(mt::ButtonUpEvent(6,6))).Times(1);
    EXPECT_CALL(*surface, consume(mt::PointerLeaveEvent())).Times(1);
    EXPECT_CALL(*another_surface, consume(mt::PointerEnterEvent())).Times(1);

    dispatcher.start();

    EXPECT_TRUE(dispatcher.dispatch(std::move(ev_1)));
    EXPECT_TRUE(dispatcher.dispatch(std::move(ev_2)));
    EXPECT_TRUE(dispatcher.dispatch(std::move(ev_3)));
    EXPECT_TRUE(dispatcher.dispatch(std::move(ev_4)));
    EXPECT_TRUE(dispatcher.dispatch(std::move(ev_5)));
}

TEST_F(SurfaceInputDispatcher, pointer_gesture_target_may_vanish_and_the_situation_remains_hunky_dorey)
{
    auto surface = scene.add_surface({{0, 0}, {5, 5}});
    auto another_surface = scene.add_surface({{5, 5}, {5, 5}});

    FakePointer pointer;
    auto ev_1 = pointer.press_button({0, 0});
    auto ev_2 = pointer.release_button({0, 0});
    auto ev_3 = pointer.move_to({6, 6});

    InSequence seq;
    EXPECT_CALL(*surface, consume(mt::PointerEnterEvent())).Times(1);
    EXPECT_CALL(*surface, consume(mt::ButtonDownEvent(0,0))).Times(1);
    EXPECT_CALL(*another_surface, consume(mt::PointerEnterEvent())).Times(1);

    dispatcher.start();

    EXPECT_TRUE(dispatcher.dispatch(std::move(ev_1)));
    scene.remove_surface(surface);
    EXPECT_FALSE(dispatcher.dispatch(std::move(ev_2)));
    EXPECT_TRUE(dispatcher.dispatch(std::move(ev_3)));
}

TEST_F(SurfaceInputDispatcher, touch_delivered_to_surface)
{
    auto surface = scene.add_surface({{1, 1}, {1, 1}});

    InSequence seq;
    EXPECT_CALL(*surface, consume(mt::TouchEvent(1,1))).Times(1);
    EXPECT_CALL(*surface, consume(mt::TouchUpEvent(1,1))).Times(1);

    dispatcher.start();

    FakeToucher toucher;
    EXPECT_TRUE(dispatcher.dispatch(toucher.touch_at({1,1})));
    EXPECT_TRUE(dispatcher.dispatch(toucher.release_at({1,1})));
}

TEST_F(SurfaceInputDispatcher, touch_delivered_only_to_top_surface)
{
    auto bottom_surface = scene.add_surface({{1, 1}, {3, 3}});
    auto surface = scene.add_surface({{1, 1}, {3, 3}});

    InSequence seq;
    EXPECT_CALL(*surface, consume(mt::TouchEvent(1,1))).Times(1);
    EXPECT_CALL(*surface, consume(mt::TouchUpEvent(1,1))).Times(1);
    EXPECT_CALL(*bottom_surface, consume(mt::TouchEvent(1,1))).Times(0);
    EXPECT_CALL(*bottom_surface, consume(mt::TouchUpEvent(1,1))).Times(0);

    dispatcher.start();

    FakeToucher toucher;
    EXPECT_TRUE(dispatcher.dispatch(toucher.touch_at({1,1})));
    EXPECT_TRUE(dispatcher.dispatch(toucher.release_at({1,1})));
}

TEST_F(SurfaceInputDispatcher, gestures_persist_over_touch_down)
{
    auto left_surface = scene.add_surface({{0, 0}, {1, 1}});
    auto right_surface = scene.add_surface({{1, 1}, {1, 1}});

    InSequence seq;
    EXPECT_CALL(*left_surface, consume(mt::TouchEvent(0, 0))).Times(1);
    EXPECT_CALL(*left_surface, consume(mt::TouchMovementEvent())).Times(1);
    EXPECT_CALL(*left_surface, consume(mt::TouchUpEvent(2, 2))).Times(1);
    EXPECT_CALL(*right_surface, consume(_)).Times(0);

    dispatcher.start();
    
    FakeToucher toucher;
    EXPECT_TRUE(dispatcher.dispatch(toucher.touch_at({0, 0})));
    EXPECT_TRUE(dispatcher.dispatch(toucher.move_to({2, 2})));
    EXPECT_TRUE(dispatcher.dispatch(toucher.release_at({2, 2})));
}

TEST_F(SurfaceInputDispatcher, touch_target_switches_on_finger_down)
{   // Regression test for LP: #1480654

    auto left_surface = scene.add_surface({{0, 0}, {1, 1}});
    auto right_surface = scene.add_surface({{5, 5}, {1, 1}});

    InSequence seq;

    EXPECT_CALL(*left_surface, consume(_)).Times(1);
    // Note: No TouchUpEvent expected
    EXPECT_CALL(*right_surface, consume(_)).Times(1);

    dispatcher.start();
    
    FakeToucher toucher;
    EXPECT_TRUE(dispatcher.dispatch(toucher.touch_at({0, 0})));
    // Note: No touch release event produced
    EXPECT_TRUE(dispatcher.dispatch(toucher.touch_at({5, 5})));
}

TEST_F(SurfaceInputDispatcher, touch_target_switches_on_fingers_down)
{
    auto left_surface = scene.add_surface({{0, 0}, {1, 1}});
    auto right_surface = scene.add_surface({{5, 5}, {2, 2}});

    InSequence seq;

    EXPECT_CALL(*left_surface, consume(_)).Times(1);
    // Note: No TouchUpEvent expected
    EXPECT_CALL(*right_surface, consume(_)).Times(1);

    dispatcher.start();

    FakeToucher toucher;
    EXPECT_TRUE(dispatcher.dispatch(toucher.touch_at({0, 0})));
    // Note: No touch release event produced
    EXPECT_TRUE(dispatcher.dispatch(toucher.touches_at({5, 5}, {6, 6})));
}

TEST_F(SurfaceInputDispatcher, touch_gestures_terminated_by_release_all_touches)
{
    auto right_surface = scene.add_surface({{5, 5}, {2, 2}});

    InSequence seq;

    EXPECT_CALL(*right_surface, consume(_)).Times(2);

    dispatcher.start();

    FakeToucher toucher;
    EXPECT_TRUE(dispatcher.dispatch(toucher.touches_at({5, 5}, {6, 6})));
    EXPECT_TRUE(dispatcher.dispatch(toucher.releases_at({5, 5}, {6, 6})));
    EXPECT_FALSE(dispatcher.dispatch(toucher.move_to({5, 6})));
}

TEST_F(SurfaceInputDispatcher, touch_gesture_target_may_vanish_but_things_continue_to_function_as_intended)
{
    auto surface_1 = scene.add_surface({{0, 0}, {1, 1}});
    auto surface_2 = scene.add_surface({{0, 0}, {1, 1}});

    InSequence seq;
    EXPECT_CALL(*surface_2, consume(mt::TouchEvent(0, 0))).Times(1);
    EXPECT_CALL(*surface_1, consume(mt::TouchEvent(0, 0))).Times(1);

    dispatcher.start();
    
    FakeToucher toucher;
    EXPECT_TRUE(dispatcher.dispatch(toucher.touch_at({0, 0})));
    scene.remove_surface(surface_2);
    EXPECT_FALSE(dispatcher.dispatch(toucher.release_at({0, 0})));
    EXPECT_TRUE(dispatcher.dispatch(toucher.touch_at({0, 0})));
}