File: test_mouse_keys_transformer.cpp

package info (click to toggle)
mir 2.25.2-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 22,080 kB
  • sloc: cpp: 192,777; xml: 13,784; ansic: 8,207; python: 1,304; sh: 794; makefile: 258
file content (524 lines) | stat: -rw-r--r-- 17,339 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
/*
 * 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 <mir/test/doubles/queued_alarm_stub_main_loop.h>

#include <mir/events/event_builders.h>
#include <mir/events/pointer_event.h>
#include <mir/geometry/forward.h>
#include <mir/input/input_event_transformer.h>
#include <mir/shell/mousekeys_transformer.h>
#include "src/server/input/default_event_builder.h"
#include "src/server/shell/basic_mousekeys_transformer.h"

#include <mir/test/doubles/advanceable_clock.h>
#include <mir/test/fake_shared.h>

#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <pthread.h>
#include <xkbcommon/xkbcommon-keysyms.h>

#include <memory>

namespace mev = mir::events;
namespace mi = mir::input;
namespace mt = mir::test;
namespace mtd = mt::doubles;

using namespace ::testing;
using namespace std::chrono_literals;

struct TestMouseKeysTransformer : testing::Test
{
    TestMouseKeysTransformer() :
        main_loop{std::make_shared<mtd::QueuedAlarmStubMainLoop>(mt::fake_shared(clock))},
        transformer{
            std::make_shared<mir::shell::BasicMouseKeysTransformer>(main_loop, mt::fake_shared(clock)),
        },
        default_event_builder{0, mt::fake_shared(clock)},
        dispatch{[&](std::shared_ptr<MirEvent> const& event)
        {
            this->on_dispatch(event);
        }}
    {
    }

    mtd::AdvanceableClock clock;
    std::shared_ptr<mtd::QueuedAlarmStubMainLoop> const main_loop;
    std::shared_ptr<mir::shell::MouseKeysTransformer> const transformer;
    mi::DefaultEventBuilder default_event_builder;
    std::function<void(std::shared_ptr<MirEvent> const& event)> dispatch;

    MOCK_METHOD(void, on_dispatch, (std::shared_ptr<MirEvent> const& event), ());

    auto down_event(int button) const -> mir::EventUPtr
    {
        return mev::make_key_event(
            MirInputDeviceId{0},
            clock.now().time_since_epoch(),
            mir_keyboard_action_down,
            button,
            0,
            mir_input_event_modifier_none);
    }

    auto up_event(int button) const -> mir::EventUPtr
    {
        return mev::make_key_event(
            MirInputDeviceId{0},
            clock.now().time_since_epoch(),
            mir_keyboard_action_up,
            button,
            0,
            mir_input_event_modifier_none);
    }
};

namespace
{
auto data_from_pointer_event(std::shared_ptr<MirEvent const> const& event)
    -> std::tuple<MirPointerAction, mir::geometry::DisplacementF, MirPointerButtons>
{
    auto* const pointer_event = event->to_input()->to_pointer();
    return {pointer_event->action(), pointer_event->motion(), pointer_event->buttons()};
}
}

struct TestOneAxisMovement : public TestMouseKeysTransformer, public WithParamInterface<mir::input::XkbSymkey>
{
};

TEST_P(TestOneAxisMovement, single_keyboard_event_to_single_pointer_motion_event)
{
    EXPECT_CALL(*this, on_dispatch(_))
        .Times(3)
        .WillRepeatedly(
            [](std::shared_ptr<MirEvent> const& event)
            {
                auto const [pointer_action, pointer_relative_motion, _] = data_from_pointer_event(event);

                EXPECT_EQ(pointer_action, mir_pointer_action_motion);

                // Make sure the movement is on one axis
                if (pointer_relative_motion.dx.as_value() != 0.0f)
                {
                    EXPECT_EQ(pointer_relative_motion.dy.as_value(), 0.0f);
                }

                if (pointer_relative_motion.dy.as_value() != 0.0f)
                {
                    EXPECT_EQ(pointer_relative_motion.dx.as_value(), 0.0f);
                }
            });

    auto const button = GetParam();
    transformer->transform_input_event(dispatch, &default_event_builder, *down_event(button));

    for (auto i = 0; i < 3; i++)
    {
        clock.advance_by(2ms);
        main_loop->call_queued();
    }

    transformer->transform_input_event(dispatch, &default_event_builder, *up_event(button));
}

INSTANTIATE_TEST_SUITE_P(
    TestMouseKeysTransformer, TestOneAxisMovement, ::Values(XKB_KEY_KP_2, XKB_KEY_KP_4, XKB_KEY_KP_6, XKB_KEY_KP_8));

struct TestTwoKeyMovement :
    public TestMouseKeysTransformer,
    public WithParamInterface<std::pair<mir::input::XkbSymkey, mir::input::XkbSymkey>>
{
    void press_keys()
    {
        auto const [key1, key2] = GetParam();

        for (auto key : {key1, key2})
        {
            auto down = down_event(key);
            transformer->transform_input_event(dispatch, &default_event_builder, *down);
        }
    }

    void release_keys()
    {
        auto const [key1, key2] = GetParam();

        for (auto key : {key1, key2})
        {
            auto up = up_event(key);
            transformer->transform_input_event(dispatch, &default_event_builder, *up);
        }
    }
};

struct TestDiagonalMovement: public TestTwoKeyMovement
{
};

TEST_P(TestDiagonalMovement, multiple_keys_result_in_diagonal_movement)
{
    auto count_diagonal = 0;
    InSequence seq;
    EXPECT_CALL(*this, on_dispatch(_))
        .Times(3)
        .WillOnce(Return()) // Skip the first event where one button is pressed
        .WillRepeatedly(
            [&](std::shared_ptr<MirEvent> const& event)
            {
                auto const [pointer_action, pointer_relative_motion, _] = data_from_pointer_event(event);

                ASSERT_EQ(pointer_action, mir_pointer_action_motion);

                // Make sure there's movement on both axes
                if (std::abs(pointer_relative_motion.dx.as_value()) > 0.0f &&
                    std::abs(pointer_relative_motion.dy.as_value()) > 0.0f)
                    count_diagonal += 1;
            });

    press_keys();

    for (auto i = 0; i < 3; i++)
    {
        clock.advance_by(2ms);
        main_loop->call_queued();
    }

    ASSERT_GT(count_diagonal, 0);
    release_keys();
}

INSTANTIATE_TEST_SUITE_P(
    TestMouseKeysTransformer,
    TestDiagonalMovement,
    ::Values(
        std::pair{XKB_KEY_KP_2, XKB_KEY_KP_4},
        std::pair{XKB_KEY_KP_2, XKB_KEY_KP_6},
        std::pair{XKB_KEY_KP_4, XKB_KEY_KP_8},
        std::pair{XKB_KEY_KP_4, XKB_KEY_KP_2},
        std::pair{XKB_KEY_KP_6, XKB_KEY_KP_8},
        std::pair{XKB_KEY_KP_6, XKB_KEY_KP_2},
        std::pair{XKB_KEY_KP_8, XKB_KEY_KP_4},
        std::pair{XKB_KEY_KP_8, XKB_KEY_KP_6}));

struct TestOppositeDiagonalMovement :
    TestTwoKeyMovement
{
};

TEST_P(TestOppositeDiagonalMovement, opposite_keys_result_in_no_movement)
{
    InSequence seq;
    EXPECT_CALL(*this, on_dispatch(_))
        .Times(3)
        .WillOnce(Return())
        .WillRepeatedly(
            [&](std::shared_ptr<MirEvent> const& event)
            {
                auto const [pointer_action, pointer_relative_motion, _] = data_from_pointer_event(event);

                ASSERT_EQ(pointer_action, mir_pointer_action_motion);

                // Make sure there is no movement
                EXPECT_FLOAT_EQ(pointer_relative_motion.length_squared(), 0.0);
            });

    press_keys();

    for (auto i = 0; i < 3; i++)
    {
        clock.advance_by(2ms);
        main_loop->call_queued();
    }

    release_keys();
}

INSTANTIATE_TEST_SUITE_P(
    TestMouseKeysTransformer,
    TestOppositeDiagonalMovement,
    ::Values(std::pair{XKB_KEY_KP_2, XKB_KEY_KP_8}, std::pair{XKB_KEY_KP_4, XKB_KEY_KP_6}));

TEST_F(TestMouseKeysTransformer, pressing_all_movement_buttons_generates_no_motion)
{
    InSequence seq;
    EXPECT_CALL(*this, on_dispatch(_))
        .Times(10)
        .WillOnce(Return())
        .WillOnce(Return())
        .WillOnce(Return())
        .WillRepeatedly(
            [&](std::shared_ptr<MirEvent> const& event)
            {
                auto const [pointer_action, pointer_relative_motion, _] = data_from_pointer_event(event);

                ASSERT_EQ(pointer_action, mir_pointer_action_motion);

                // Make sure there is no movement
                EXPECT_FLOAT_EQ(pointer_relative_motion.length_squared(), 0.0);
            });

    auto const all_movement_keys = {XKB_KEY_KP_2, XKB_KEY_KP_4, XKB_KEY_KP_6, XKB_KEY_KP_8};
    for (auto key : all_movement_keys)
    {
        auto down = down_event(key);
        transformer->transform_input_event(dispatch, &default_event_builder, *down);
    }


    // 3 iterations with the keys partially down, followed by 7 iterations
    // where all the keys are down
    for(auto i = 0; i < 10; i++)
    {
        clock.advance_by(2ms);
        main_loop->call_queued();
    }

    for (auto key : all_movement_keys)
    {
        auto up = up_event(key);
        transformer->transform_input_event(dispatch, &default_event_builder, *up);
    }
}

struct ClicksDispatchDownAndUpEvents :
    public TestMouseKeysTransformer,
    public WithParamInterface<std::pair<mir::input::XkbSymkey, MirPointerButton>>
{
};

TEST_P(ClicksDispatchDownAndUpEvents, clicks_dispatch_pointer_down_and_up_events)
{
    auto const [switching_button, expected_action] = GetParam();

    auto const check_up = [](auto const& event)
    {
        auto const [pointer_action, _, pointer_buttons] = data_from_pointer_event(event);

        EXPECT_EQ(pointer_action, mir_pointer_action_button_up);
        EXPECT_EQ(pointer_buttons, 0);
    };

    InSequence seq;
    EXPECT_CALL(*this, on_dispatch(_))
        .Times(3) // up (switching), down, up
        .WillOnce(
            [&check_up](std::shared_ptr<MirEvent> const& event)
            {
                check_up(event);
            })
        .WillOnce(
            [expected_action](std::shared_ptr<MirEvent> const& event)
            {
                auto const [pointer_action, _, pointer_buttons] = data_from_pointer_event(event);
                EXPECT_EQ(pointer_action, mir_pointer_action_button_down);
                EXPECT_EQ(pointer_buttons, expected_action);
            })
        .WillOnce(
            [&check_up](std::shared_ptr<MirEvent> const& event) mutable
            {
                check_up(event);
            });

    transformer->transform_input_event(dispatch, &default_event_builder, *up_event(switching_button));
    transformer->transform_input_event(dispatch, &default_event_builder, *up_event(XKB_KEY_KP_5));

    main_loop->call_queued();
}

INSTANTIATE_TEST_SUITE_P(
    TestMouseKeysTransformer,
    ClicksDispatchDownAndUpEvents,
    ::Values(
        std::pair{XKB_KEY_KP_Divide, mir_pointer_button_primary},
        std::pair{XKB_KEY_KP_Multiply, mir_pointer_button_tertiary},
        std::pair{XKB_KEY_KP_Subtract, mir_pointer_button_secondary}));

TEST_F(TestMouseKeysTransformer, double_click_dispatch_four_events)
{
    auto const expect_down =
        [](auto const& event)
    {
        auto const [pointer_action, _, __] = data_from_pointer_event(event);
        EXPECT_EQ(pointer_action, mir_pointer_action_button_down);
    };

    auto const expect_up = [](auto const& event)
    {
        auto const [pointer_action, _, pointer_buttons] = data_from_pointer_event(event);
        EXPECT_EQ(pointer_action, mir_pointer_action_button_up);
        EXPECT_EQ(pointer_buttons, 0);
    };

    InSequence seq;
    EXPECT_CALL(*this, on_dispatch(_))
        .Times(4)
        .WillOnce(expect_down)
        .WillOnce(expect_up)
        .WillOnce(expect_down)
        .WillOnce(expect_up);

    transformer->transform_input_event(dispatch, &default_event_builder, *up_event(XKB_KEY_KP_Add));
    main_loop->call_queued();
}

TEST_F(TestMouseKeysTransformer, drag_start_and_end_dispatch_down_and_up_events)
{
    InSequence seq;
    EXPECT_CALL(*this, on_dispatch(_))
        .Times(2)
        .WillOnce(
            [](auto const& event)
            {
                auto const [pointer_action, _, __] = data_from_pointer_event(event);
                EXPECT_EQ(pointer_action, mir_pointer_action_button_down);
            })
        .WillOnce(
            [](auto const& event)
            {
                auto const [pointer_action, _, pointer_buttons] = data_from_pointer_event(event);
                EXPECT_EQ(pointer_action, mir_pointer_action_button_up);
                EXPECT_EQ(pointer_buttons, 0);
            });

    transformer->transform_input_event(dispatch, &default_event_builder, *up_event(XKB_KEY_KP_0));
    transformer->transform_input_event(dispatch, &default_event_builder, *up_event(XKB_KEY_KP_Decimal));
    main_loop->call_queued();
}

TEST_F(TestMouseKeysTransformer, receiving_a_key_not_in_keymap_doesnt_dispatch_event)
{
    EXPECT_CALL(*this, on_dispatch(_)).Times(0);
    transformer->keymap(mir::input::MouseKeysKeymap{});

    using enum mir::input::MouseKeysKeymap::Action;
    auto const test_keymap = mir::input::MouseKeysKeymap{
        {XKB_KEY_w, move_up},
        {XKB_KEY_s, move_down},
        {XKB_KEY_a, move_left},
        {XKB_KEY_d, move_right},
        {XKB_KEY_y, button_primary},
        {XKB_KEY_u, button_tertiary},
        {XKB_KEY_i, button_secondary},
        {XKB_KEY_j, click},
    };
    test_keymap.for_each_key_action_pair(
        [&](auto key, auto)
        {
            transformer->transform_input_event(dispatch, &default_event_builder, *down_event(key));
            main_loop->call_queued();
        });
}

struct TestAccelerationCurveParams
{
    struct
    {
        double quadratic, linear, constant;
    } curve;
    double expected_speed;
};
struct TestAccelerationCurve: public TestMouseKeysTransformer, WithParamInterface<TestAccelerationCurveParams>
{
};

TEST_P(TestAccelerationCurve, acceleration_curve_constants_evaluate_properly)
{
    auto const [curve, expected_speed_squared] = GetParam();
    EXPECT_CALL(*this, on_dispatch(_))
        .Times(1)
        .WillOnce(
            [expected_speed_squared](std::shared_ptr<MirEvent> const& event)
            {
                auto [_, pointer_motion, __] = data_from_pointer_event(event);
                ASSERT_FLOAT_EQ(std::sqrt(pointer_motion.length_squared()), expected_speed_squared);
            });

    // Don't want speed limits interfering with our test case
    transformer->max_speed(0, 0);

    transformer->acceleration_factors(curve.constant, curve.linear, curve.quadratic);
    transformer->transform_input_event(dispatch, &default_event_builder, *down_event(XKB_KEY_KP_6));

    clock.advance_by(2ms);
    main_loop->call_queued();

    transformer->transform_input_event(dispatch, &default_event_builder, *up_event(XKB_KEY_KP_6));
}

// Speed is computed as:
//              (
//              constant factor
//              + linear factor * time since motion start
//              + quadratic factor * time since motion started ^ 2
//              )  * time between alarm invocations
//
// time between alarm invocations is hardcoded as 2ms
// time since motion start is also 2ms
INSTANTIATE_TEST_SUITE_P(
    TestMouseKeysTransformer,
    TestAccelerationCurve,
    ::Values(
        TestAccelerationCurveParams{{0, 0, 0}, 0.0},
        TestAccelerationCurveParams{{0, 0, 500}, 1.0},
        TestAccelerationCurveParams{{0, 500, 0}, 500 * 0.002 * 0.002},
        TestAccelerationCurveParams{{500, 0, 0}, 500 * (0.002 * 0.002) * 0.002}));

using TestMaxSpeedParams = std::pair<mir::geometry::Displacement, mir::geometry::DisplacementF>;
struct TestMaxSpeed : public TestMouseKeysTransformer, WithParamInterface<TestMaxSpeedParams>
{
};

TEST_P(TestMaxSpeed, max_speed_caps_speed_properly)
{
    auto constexpr dt = 0.002;
    auto const [max_speed, expected_speed] = GetParam();

    EXPECT_CALL(*this, on_dispatch(_))
        .Times(1)
        .WillOnce(
            [expected_speed](std::shared_ptr<MirEvent> const& event)
            {
                // Motion events are in pixels/alarm invocation
                // Which occurs about every 2ms
                auto const motion = event->to_input()->to_pointer()->motion();
                EXPECT_EQ(motion.dx, expected_speed.dx * dt);
                EXPECT_EQ(motion.dy, expected_speed.dy * dt);
            });

    // Immediately force clamping
    transformer->acceleration_factors(1000000, 1000000, 1000000);

    transformer->max_speed(max_speed.dx.as_value(), max_speed.dy.as_value());
    transformer->transform_input_event(dispatch, &default_event_builder, *down_event(XKB_KEY_KP_6));
    transformer->transform_input_event(dispatch, &default_event_builder, *down_event(XKB_KEY_KP_2));

    clock.advance_by(1s);
    main_loop->call_queued();

    transformer->transform_input_event(dispatch, &default_event_builder, *up_event(XKB_KEY_KP_6));
    transformer->transform_input_event(dispatch, &default_event_builder, *up_event(XKB_KEY_KP_2));
}

INSTANTIATE_TEST_SUITE_P(
    TestMouseKeysTransformer,
    TestMaxSpeed,
    ::Values(
        TestMaxSpeedParams{{100, 1}, {100, 1}},
        TestMaxSpeedParams{{1, 100}, {1, 100}},
        TestMaxSpeedParams{{100, 100}, {100, 100}}));