File: test_slow_keys_transformer.cpp

package info (click to toggle)
mir 2.25.2-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 22,104 kB
  • sloc: cpp: 192,777; xml: 13,784; ansic: 8,207; python: 1,304; sh: 794; makefile: 258
file content (144 lines) | stat: -rw-r--r-- 5,033 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
/*
 * 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_toolkit/event.h>
#include <mir/shell/accessibility_manager.h>
#include "src/server/input/default_event_builder.h"
#include "src/server/shell/basic_slow_keys_transformer.h"

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

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

namespace mi = mir::input;
namespace mt = mir::test;
namespace mtd = mt::doubles;
using namespace ::testing;
using namespace std::chrono_literals;

namespace
{
static auto constexpr test_slow_keys_delay = 100ms;
}

struct TestSlowKeysTransformer : Test
{
    TestSlowKeysTransformer() :
        main_loop{std::make_shared<mtd::QueuedAlarmStubMainLoop>(mt::fake_shared(clock))},
        transformer{
            std::make_shared<mir::shell::BasicSlowKeysTransformer>(main_loop),
        },
        event_builder{0, mt::fake_shared(clock)},
        dispatch{[&](std::shared_ptr<MirEvent> const&) {}}
    {
        transformer->delay(test_slow_keys_delay);
    }

    auto key_down(unsigned int keysym, unsigned int scancode) -> mir::EventUPtr
    {
        return event_builder.key_event(std::nullopt, mir_keyboard_action_down, keysym, scancode);
    }

    auto key_up(unsigned int keysym, unsigned int scancode) -> mir::EventUPtr
    {
        return event_builder.key_event(std::nullopt, mir_keyboard_action_up, keysym, scancode);
    }

    mtd::AdvanceableClock clock;
    std::shared_ptr<mtd::QueuedAlarmStubMainLoop> const main_loop;
    std::shared_ptr<mir::shell::SlowKeysTransformer> const transformer;
    mi::DefaultEventBuilder event_builder;

    std::function<void(std::shared_ptr<MirEvent> const& event)> dispatch;
};

struct TestSlowKeysTransformerPressDelays: public TestSlowKeysTransformer, public WithParamInterface<std::chrono::milliseconds>
{
};

TEST_P(TestSlowKeysTransformerPressDelays, only_presses_held_for_slow_keys_delay_are_accepted)
{
    auto const press_delay = GetParam();

    auto accepted = 0, rejected = 0, down = 0;
    transformer->on_key_accepted([&accepted](auto) { accepted += 1; });
    transformer->on_key_rejected([&rejected](auto) { rejected += 1; });
    transformer->on_key_down([&down](auto) { down += 1; });

    auto const attempts = 5;
    for(auto i = 0; i < attempts; i++)
    {
        transformer->transform_input_event(dispatch, &event_builder, *key_down(XKB_KEY_d, 32));

        clock.advance_by(press_delay);
        main_loop->call_queued();

        transformer->transform_input_event(dispatch, &event_builder, *key_up(XKB_KEY_d, 32));
    }

    EXPECT_THAT(down, Eq(attempts));
    EXPECT_THAT(press_delay < test_slow_keys_delay ? rejected : accepted, Eq(attempts));
}

INSTANTIATE_TEST_SUITE_P(
    TestSlowKeysTransformer, TestSlowKeysTransformerPressDelays, Values(test_slow_keys_delay - 1ms, test_slow_keys_delay + 1ms));


struct TestSlowKeysTransformerWithKeyInterference: public TestSlowKeysTransformer, public WithParamInterface<std::chrono::milliseconds>
{
};

TEST_P(TestSlowKeysTransformerWithKeyInterference, different_buttons_dont_interfere_with_each_other)
{
    auto const press_delay = GetParam();

    auto accepted = 0, rejected = 0, down = 0;
    transformer->on_key_accepted([&accepted](auto) { accepted += 1; });
    transformer->on_key_rejected([&rejected](auto) { rejected += 1; });
    transformer->on_key_down([&down](auto) { down += 1; });

    auto const attempts = 5;
    for(auto i = 0; i < attempts; i++)
    {
        transformer->transform_input_event(dispatch, &event_builder, *key_down(XKB_KEY_d, 32));
        transformer->transform_input_event(dispatch, &event_builder, *key_down(XKB_KEY_f, 33));

        clock.advance_by(press_delay);
        main_loop->call_queued();

        transformer->transform_input_event(dispatch, &event_builder, *key_up(XKB_KEY_d, 32));
        transformer->transform_input_event(dispatch, &event_builder, *key_up(XKB_KEY_f, 33));
    }

    EXPECT_THAT(down, Eq(attempts * 2));

    if (press_delay < test_slow_keys_delay)
    {
        EXPECT_THAT(accepted, Eq(0));
        EXPECT_THAT(rejected, Eq(attempts * 2));
    }
    else
    {
        EXPECT_THAT(accepted, Eq(attempts * 2));
        EXPECT_THAT(rejected, Eq(0));
    }
}

INSTANTIATE_TEST_SUITE_P(
    TestSlowKeysTransformer, TestSlowKeysTransformerWithKeyInterference, Values(test_slow_keys_delay - 1ms, test_slow_keys_delay + 1ms));