File: pointer_constraints.cpp

package info (click to toggle)
wlcs 1.8.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,528 kB
  • sloc: cpp: 14,211; xml: 7,744; ansic: 952; sh: 164; makefile: 33
file content (301 lines) | stat: -rw-r--r-- 8,939 bytes parent folder | download | duplicates (3)
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
/*
 * Copyright © 2020 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 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/>.
 *
 * Authored by: Alan Griffiths <alan@octopull.co.uk>
 */

#include "pointer_constraints_unstable_v1.h"
#include "helpers.h"
#include "in_process_server.h"

#include <gmock/gmock.h>

#include <memory>

using testing::AnyNumber;
using testing::Eq;
using testing::Ne;
using testing::NotNull;

using namespace wlcs;

namespace
{
auto const any_width = 300;
auto const any_height = 300;
auto const nw_middle_x = any_width / 2;
auto const nw_middle_y = any_height / 2;
auto const se_middle_x = any_width + nw_middle_x;
auto const se_middle_y = any_height + nw_middle_y;

struct PointerConstraints : StartedInProcessServer
{
    // Create a client with two surface (ne & sw) and a cursor centered on the nw_surface
    Client client{the_server()};
    Surface se_surface{client.create_visible_surface(any_width, any_height)};
    Surface nw_surface{client.create_visible_surface(any_width, any_height)};
    wl_pointer* const pointer = client.the_pointer();

    Pointer cursor = the_server().create_pointer();

    ZwpPointerConstraintsV1 pointer_constraints{client};

    std::unique_ptr<ZwpLockedPointerV1> locked_ptr = nullptr;
    std::unique_ptr<ZwpConfinedPointerV1> confined_ptr = nullptr;

    void SetUp() override
    {
        StartedInProcessServer::SetUp();

        // Get the surface in a known position with the cursor over it
        the_server().move_surface_to(nw_surface, 0, 0);
        cursor.move_to(nw_middle_x, nw_middle_y);
        the_server().move_surface_to(se_surface, any_width, any_height);
    }

    void TearDown() override
    {
        locked_ptr.reset();
        confined_ptr.reset();
        client.roundtrip();
        StartedInProcessServer::TearDown();
    }

    void setup_locked_ptr_on(Surface& surface, zwp_pointer_constraints_v1_lifetime lifetime = ZWP_POINTER_CONSTRAINTS_V1_LIFETIME_ONESHOT)
    {
        locked_ptr = std::make_unique<ZwpLockedPointerV1>(pointer_constraints, surface, pointer, nullptr, lifetime);
    }

    void setup_confined_ptr_on(Surface& surface, zwp_pointer_constraints_v1_lifetime lifetime = ZWP_POINTER_CONSTRAINTS_V1_LIFETIME_ONESHOT)
    {
        confined_ptr = std::make_unique<ZwpConfinedPointerV1>(pointer_constraints, surface, pointer, nullptr, lifetime);
    }

    void setup_sync()
    {
        client.roundtrip();
        using namespace testing;
        if (locked_ptr) Mock::VerifyAndClearExpectations(locked_ptr.get());
        if (confined_ptr) Mock::VerifyAndClearExpectations(confined_ptr.get());
    }

    void select_se_window()
    {
        cursor.move_to(se_middle_x, se_middle_y);
        cursor.left_click();
        client.roundtrip();
    }

    void select_nw_window()
    {
        cursor.move_to(nw_middle_x, nw_middle_y);
        cursor.left_click();
        client.roundtrip();
    }
};
}

TEST_F(PointerConstraints, can_get_locked_pointer)
{
    setup_locked_ptr_on(nw_surface);

    EXPECT_THAT(*locked_ptr, NotNull());
}

TEST_F(PointerConstraints, locked_pointer_on_initially_focussed_surface_gets_locked_notification)
{
    setup_locked_ptr_on(nw_surface);

    EXPECT_CALL(*locked_ptr, locked()).Times(1);

    client.roundtrip();
}

TEST_F(PointerConstraints, locked_pointer_does_not_move)
{
    auto const initial_pointer_position = client.pointer_position();
    setup_locked_ptr_on(nw_surface);
    EXPECT_CALL(*locked_ptr, locked()).Times(AnyNumber());
    setup_sync();

    cursor.move_by(10, 10);
    client.roundtrip();

    EXPECT_THAT(client.pointer_position(), Eq(initial_pointer_position));
}

TEST_F(PointerConstraints, locked_pointer_on_initially_unfocussed_surface_gets_no_locked_notification)
{
    setup_locked_ptr_on(se_surface);

    EXPECT_CALL(*locked_ptr, locked()).Times(0);

    client.roundtrip();
}

TEST_F(PointerConstraints, when_surface_is_selected_locked_pointer_gets_locked_notification)
{
    setup_locked_ptr_on(se_surface);
    setup_sync();

    EXPECT_CALL(*locked_ptr, locked()).Times(1);

    select_se_window();
}

TEST_F(PointerConstraints, when_surface_is_unselected_locked_pointer_gets_unlocked_notification)
{
    setup_locked_ptr_on(nw_surface);
    EXPECT_CALL(*locked_ptr, locked()).Times(AnyNumber());
    setup_sync();

    EXPECT_CALL(*locked_ptr, unlocked()).Times(1);

    // A new surface will be given focus
    Surface a_new_surface{client.create_visible_surface(any_width, any_height)};
    client.roundtrip();
}

TEST_F(PointerConstraints, when_surface_is_reselected_persistent_locked_pointer_gets_notifications)
{
    setup_locked_ptr_on(nw_surface, ZWP_POINTER_CONSTRAINTS_V1_LIFETIME_PERSISTENT);
    EXPECT_CALL(*locked_ptr, locked()).Times(AnyNumber());
    setup_sync();

    for (auto i = 3; i-- != 0;)
    {
        {
            EXPECT_CALL(*locked_ptr, unlocked()).Times(1);
            // A new surface will be given focus
            Surface a_new_surface{client.create_visible_surface(any_width, any_height)};
            setup_sync();

            EXPECT_CALL(*locked_ptr, locked()).Times(1);
        }
        client.roundtrip();
    }
}

TEST_F(PointerConstraints, can_get_confined_pointer)
{
    setup_confined_ptr_on(nw_surface);
    EXPECT_THAT(*confined_ptr, NotNull());
}

TEST_F(PointerConstraints, confined_pointer_on_initially_focussed_surface_gets_confined_notification)
{
    setup_confined_ptr_on(nw_surface);

    EXPECT_CALL(*confined_ptr, confined()).Times(1);

    client.roundtrip();
}

TEST_F(PointerConstraints, confined_pointer_does_move)
{
    auto const initial_pointer_position = client.pointer_position();
    setup_confined_ptr_on(nw_surface);
    EXPECT_CALL(*confined_ptr, confined()).Times(AnyNumber());
    setup_sync();

    cursor.move_by(10, 10);
    client.roundtrip();

    EXPECT_THAT(client.pointer_position(), Ne(initial_pointer_position));
}

TEST_F(PointerConstraints, confined_pointer_movement_is_constrained)
{
    auto const top = wl_fixed_from_int(0);
    auto const left = wl_fixed_from_int(0);
    auto const bottom = wl_fixed_from_int(any_height-1);
    auto const right = wl_fixed_from_int(any_width-1);

    setup_confined_ptr_on(nw_surface);
    EXPECT_CALL(*confined_ptr, confined()).Times(AnyNumber());
    setup_sync();

    cursor.move_by(2*any_width, 2*any_height);
    client.roundtrip();

    EXPECT_THAT(client.pointer_position(), Eq(std::make_pair(bottom, right)));

    cursor.move_by(-2*any_width, -2*any_height);
    client.roundtrip();

    EXPECT_THAT(client.pointer_position(), Eq(std::make_pair(top, left)));

    cursor.move_by(-2*any_width, 2*any_height);
    client.roundtrip();

    EXPECT_THAT(client.pointer_position(), Eq(std::make_pair(top, right)));

    cursor.move_by(2*any_width, -2*any_height);
    client.roundtrip();

    EXPECT_THAT(client.pointer_position(), Eq(std::make_pair(bottom, left)));
}

TEST_F(PointerConstraints, confined_pointer_on_initially_unfocussed_surface_gets_no_confined_notification)
{
    setup_confined_ptr_on(se_surface);

    EXPECT_CALL(*confined_ptr, confined()).Times(0);

    client.roundtrip();
}

TEST_F(PointerConstraints, when_surface_is_selected_confined_pointer_gets_confined_notification)
{
    setup_confined_ptr_on(se_surface);
    setup_sync();

    EXPECT_CALL(*confined_ptr, confined()).Times(1);

    select_se_window();
}

TEST_F(PointerConstraints, when_surface_is_unselected_confined_pointer_gets_unconfined_notification)
{
    setup_confined_ptr_on(nw_surface);
    EXPECT_CALL(*confined_ptr, confined()).Times(AnyNumber());
    setup_sync();

    EXPECT_CALL(*confined_ptr, unconfined()).Times(1);

    // A new surface will be given focus
    Surface a_new_surface{client.create_visible_surface(any_width, any_height)};
    client.roundtrip();
}

TEST_F(PointerConstraints, when_surface_is_reselected_persistent_confined_pointer_gets_notifications)
{
    setup_confined_ptr_on(nw_surface, ZWP_POINTER_CONSTRAINTS_V1_LIFETIME_PERSISTENT);
    EXPECT_CALL(*confined_ptr, confined()).Times(AnyNumber());
    setup_sync();

    for (auto i = 3; i-- != 0;)
    {
        {
            EXPECT_CALL(*confined_ptr, unconfined()).Times(1);
            // A new surface will be given focus
            Surface a_new_surface{client.create_visible_surface(any_width, any_height)};
            setup_sync();

            EXPECT_CALL(*confined_ptr, confined()).Times(1);
        }
        client.roundtrip();
    }
}