File: test_simulated_secondary_click_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 (335 lines) | stat: -rw-r--r-- 12,864 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
/*
 * 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/events/event.h>
#include <mir/events/event_builders.h>
#include <mir/events/input_event.h>
#include <mir/events/pointer_event.h>
#include "src/server/input/default_event_builder.h"
#include "src/server/shell/basic_simulated_secondary_click_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 <gmock/gmock.h>
#include <gtest/gtest.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_hold_time = 1000ms;
static auto constexpr test_displacement_threshold = 20.0f;
}

struct TestSimulatedSecondaryClickTransformer : Test
{
    static auto constexpr virtual_event_builder_device_id = 1;

    TestSimulatedSecondaryClickTransformer() :
        main_loop{std::make_shared<mtd::QueuedAlarmStubMainLoop>(mt::fake_shared(clock))},
        transformer{
            std::make_shared<mir::shell::BasicSimulatedSecondaryClickTransformer>(main_loop),
        },
        real_event_builder{0, mt::fake_shared(clock)},
        virtual_event_builder{virtual_event_builder_device_id, mt::fake_shared(clock)},
        dispatch{[&](std::shared_ptr<MirEvent> const& event)
                 {
                     this->on_dispatch(event);
                 }}
    {
        transformer->hold_duration(test_hold_time);
        transformer->displacement_threshold(test_displacement_threshold);
    }

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

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

    MirPointerButtons buttons{0};

    auto pointer_down_event(MirPointerButton button) -> mir::EventUPtr
    {
        buttons |= button;
        return real_event_builder.pointer_event(
            std::nullopt,
            mir_pointer_action_button_down,
            buttons,
            0.0f,
            0.0f,
            0.0f,
            0.0f,
            0.0f,
            0.0f);
    }

    auto pointer_up_event(MirPointerButton button) -> mir::EventUPtr
    {
        buttons &= ~button;
        return real_event_builder.pointer_event(
            std::nullopt,
            mir_pointer_action_button_up,
            buttons,
            0.0f,
            0.0f,
            0.0f,
            0.0f,
            0.0f,
            0.0f);
    }
};

struct TestDifferentReleaseTimings :
    public TestSimulatedSecondaryClickTransformer,
    public WithParamInterface<std::tuple<std::chrono::milliseconds, MirPointerButton>>
{
};

TEST_P(
    TestDifferentReleaseTimings,
    releasing_primary_button_before_secondary_click_delay_dispatches_a_primary_click)
{
    auto const [release_delay, expected_button] = GetParam();

    // If we cancel a simulated secondary click, we dispatch a synthesized primary click. The original primary click is
    // consumed and should not be forwarded to `dispatch`
    EXPECT_CALL(*this, on_dispatch(_))
        .WillOnce(
            [expected_button](auto const& event)
            {
                EXPECT_THAT(event->to_input()->to_pointer()->action(), Eq(mir_pointer_action_button_down));
                EXPECT_THAT(event->to_input()->to_pointer()->buttons() & expected_button, Ne(0));
            })
        .WillOnce(
            [expected_button](auto const& event)
            {
                EXPECT_THAT(event->to_input()->to_pointer()->action(), Eq(mir_pointer_action_button_up));
                EXPECT_THAT(event->to_input()->to_pointer()->buttons() & expected_button, Eq(0));
            });

    transformer->transform_input_event(dispatch, &virtual_event_builder, *pointer_down_event(mir_pointer_button_primary));
    clock.advance_by(release_delay);
    main_loop->call_queued();
    transformer->transform_input_event(dispatch, &virtual_event_builder, *pointer_up_event(mir_pointer_button_primary));
    main_loop->call_queued();
}

INSTANTIATE_TEST_SUITE_P(
    TestSimulatedSecondaryClickTransformer,
    TestDifferentReleaseTimings,
    ::Values(
        std::tuple(test_hold_time - 1ms, mir_pointer_button_primary),
        std::tuple(test_hold_time + 1ms, mir_pointer_button_secondary)));

struct TestCallbacksParameters
{
    std::chrono::milliseconds hold_duration;
    // The secondary click succeeding implies that it wasn't cancelled.
    // Thus, the cancel callback will not be called.
    bool start_called{false}, secondary_click_called{false};
};

struct TestCallbacks :
    public TestSimulatedSecondaryClickTransformer,
    public WithParamInterface<TestCallbacksParameters>
{
};

TEST_P(TestCallbacks, releasing_left_pointer_button_at_different_times_calls_the_expected_callbacks)
{
    bool hold_start_called = false;
    bool hold_cancel_called = false;
    bool secondary_click_called = false;

    transformer->on_hold_start([&hold_start_called] { hold_start_called = true; });
    transformer->on_hold_cancel([&hold_cancel_called] { hold_cancel_called = true; });
    transformer->on_secondary_click([&secondary_click_called] { secondary_click_called = true; });

    auto const [release_delay, start_called, cancel_called] = GetParam();

    transformer->transform_input_event(dispatch, &virtual_event_builder, *pointer_down_event(mir_pointer_button_primary));
    clock.advance_by(release_delay);
    main_loop->call_queued();
    transformer->transform_input_event(dispatch, &virtual_event_builder, *pointer_up_event(mir_pointer_button_primary));
    main_loop->call_queued();

    EXPECT_THAT(hold_start_called, Eq(start_called));
    EXPECT_THAT(hold_cancel_called, Eq(cancel_called));
    EXPECT_THAT(secondary_click_called, Ne(cancel_called));
}

INSTANTIATE_TEST_SUITE_P(
    TestSimulatedSecondaryClickTransformer,
    TestCallbacks,
    ::Values(
        TestCallbacksParameters{test_hold_time - 1ms, true, true},
        TestCallbacksParameters{test_hold_time + 1ms, true, false}));

struct TestInterferenceParameters {
    std::chrono::milliseconds hold_duration;
    MirPointerButton interfering_button;
};

struct TestInterference :
    public TestSimulatedSecondaryClickTransformer,
    public WithParamInterface<TestInterferenceParameters>
{
};

TEST_P(TestInterference, pressing_other_buttons_during_simulated_secondary_click_does_not_interfere)
{
    auto const [release_delay, interfering_button] = GetParam();
    auto const expected_button =
        release_delay < 1000ms ? mir_pointer_button_primary : mir_pointer_button_secondary;

    // If we cancel a simulated secondary click, we dispatch a synthesized primary click. The original primary click is
    // consumed and should not be forwarded to `dispatch`
    EXPECT_CALL(*this, on_dispatch(_))
        .WillOnce(
            [expected_button](auto const& event)
            {
                EXPECT_THAT(event->to_input()->to_pointer()->action(), Eq(mir_pointer_action_button_down));
                EXPECT_THAT(event->to_input()->to_pointer()->buttons() & expected_button, Ne(0));
            })
        .WillOnce(
            [expected_button](auto const& event)
            {
                EXPECT_THAT(event->to_input()->to_pointer()->action(), Eq(mir_pointer_action_button_up));
                EXPECT_THAT(event->to_input()->to_pointer()->buttons() & expected_button, Eq(0));
            });

    transformer->transform_input_event(dispatch, &virtual_event_builder, *pointer_down_event(mir_pointer_button_primary));
    transformer->transform_input_event(dispatch, &virtual_event_builder, *pointer_down_event(interfering_button));

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

    transformer->transform_input_event(dispatch, &virtual_event_builder, *pointer_up_event(interfering_button));
    transformer->transform_input_event(dispatch, &virtual_event_builder, *pointer_up_event(mir_pointer_button_primary));

    main_loop->call_queued();
}

INSTANTIATE_TEST_SUITE_P(
    TestSimulatedSecondaryClickTransformer,
    TestInterference,
    ::Values(
        TestInterferenceParameters{test_hold_time - 1ms, mir_pointer_button_primary},
        TestInterferenceParameters{test_hold_time + 1ms, mir_pointer_button_primary},

        TestInterferenceParameters{test_hold_time - 1ms, mir_pointer_button_secondary},
        TestInterferenceParameters{test_hold_time + 1ms, mir_pointer_button_secondary},

        TestInterferenceParameters{test_hold_time - 1ms, mir_pointer_button_tertiary},
        TestInterferenceParameters{test_hold_time + 1ms, mir_pointer_button_tertiary}));

TEST_F(TestSimulatedSecondaryClickTransformer, low_displacement_doesnt_cancel_simulated_secondary_click)
{
    EXPECT_CALL(*this, on_dispatch(_))
        .WillOnce(
            [](auto const& event)
            {
                EXPECT_THAT(event->to_input()->to_pointer()->action(), Eq(mir_pointer_action_button_down));
                EXPECT_THAT(event->to_input()->to_pointer()->buttons() & mir_pointer_button_secondary, Ne(0));
            })
        .WillOnce(
            [](auto const& event)
            {
                EXPECT_THAT(event->to_input()->to_pointer()->action(), Eq(mir_pointer_action_button_up));
                EXPECT_THAT(event->to_input()->to_pointer()->buttons() & mir_pointer_button_secondary, Eq(0));
            });

    transformer->transform_input_event(
        dispatch, &virtual_event_builder, *pointer_down_event(mir_pointer_button_primary));

    // Doesn't actually matter which axis we displace on as long as the displacement magnitude matches.
    // disp = sqrt(x*x + y*y) assuming initial_position = (0, 0)
    // 20 = sqrt(2x^2) assuming disp=test_displacement_threshold=20
    // 400 = 2x^2
    // 200 = x^2
    // x = sqrt(200) = sqrt((disp^2) / 2)
    auto const displacement = std::sqrt(std::pow(test_displacement_threshold, 2) / 2.0f);
    transformer->transform_input_event(
        dispatch,
        &virtual_event_builder,
        *real_event_builder.pointer_event(
            std::nullopt,
            mir_pointer_action_motion,
            buttons,
            displacement - 1.0f,
            displacement - 1.0f,
            0.0f,
            0.0f,
            0.0f,
            0.0f));

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

    transformer->transform_input_event(dispatch, &virtual_event_builder, *pointer_up_event(mir_pointer_button_primary));
    main_loop->call_queued();
}

TEST_F(TestSimulatedSecondaryClickTransformer, high_displacement_cancels_simulated_secondary_click)
{
    EXPECT_CALL(*this, on_dispatch(_))
        .WillOnce(
            [](auto const& event)
            {
                EXPECT_THAT(event->to_input()->to_pointer()->action(), Eq(mir_pointer_action_button_down));
                EXPECT_THAT(event->to_input()->to_pointer()->buttons() & mir_pointer_button_primary, Ne(0));
            })
        .WillOnce(
            [](auto const& event)
            {
                EXPECT_THAT(event->to_input()->to_pointer()->action(), Eq(mir_pointer_action_motion));
            });

    transformer->transform_input_event(
        dispatch, &virtual_event_builder, *pointer_down_event(mir_pointer_button_primary));

    // Doesn't actually matter which axis we displace on as long as the displacement magnitude matches.
    auto const displacement = std::sqrt(std::pow(test_displacement_threshold, 2) / 2.0f);
    transformer->transform_input_event(
        dispatch,
        &virtual_event_builder,
        *real_event_builder.pointer_event(
            std::nullopt,
            mir_pointer_action_motion,
            buttons,
            displacement + 1.0f,
            displacement + 1.0f,
            0.0f,
            0.0f,
            0.0f,
            0.0f));

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

    transformer->transform_input_event(dispatch, &virtual_event_builder, *pointer_up_event(mir_pointer_button_primary));
    main_loop->call_queued();
}