File: primary_selection.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 (231 lines) | stat: -rw-r--r-- 7,348 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
/*
 * Copyright © 2019 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 "primary_selection.h"

#include "in_process_server.h"
#include "version_specifier.h"

#include <gmock/gmock.h>
#include <sys/socket.h>

using namespace wlcs;
using namespace testing;

namespace
{
char const any_mime_type[] = "AnyMimeType";
char const any_mime_data[] = "AnyMimeData";

struct SourceApp : Client
{
    using Client::Client;

    WlHandle<zwp_primary_selection_device_manager_v1> const manager{
        this->bind_if_supported<zwp_primary_selection_device_manager_v1>(AnyVersion)};
    PrimarySelectionSource source{manager};
    PrimarySelectionDevice device{manager, seat()};

    void set_selection()
    {
        zwp_primary_selection_device_v1_set_selection(device, source, 0);
        roundtrip();
    }

    void offer(char const* mime_type)
    {
        zwp_primary_selection_source_v1_offer(source, mime_type);
        roundtrip();
    }
};

struct SinkApp : Client
{
    explicit SinkApp(Server& server) : Client{server} { roundtrip(); }

    WlHandle<zwp_primary_selection_device_manager_v1> const manager{
        this->bind_if_supported<zwp_primary_selection_device_manager_v1>(AnyVersion)};
    PrimarySelectionDevice device{manager, seat()};
};

struct PrimarySelection : StartedInProcessServer
{
    SourceApp   source_app{the_server()};
    SinkApp     sink_app{the_server()};
    Surface     surface{sink_app.create_visible_surface(10, 10)};

    void TearDown() override
    {
        source_app.roundtrip();
        sink_app.roundtrip();
        StartedInProcessServer::TearDown();
    }
};

struct MockPrimarySelectionDeviceListener : PrimarySelectionDeviceListener
{
    using PrimarySelectionDeviceListener::PrimarySelectionDeviceListener;

    MOCK_METHOD(void, data_offer, (zwp_primary_selection_device_v1* device, zwp_primary_selection_offer_v1* offer), (override));
    MOCK_METHOD(void, selection, (zwp_primary_selection_device_v1* device, zwp_primary_selection_offer_v1* offer), (override));
};

struct MockPrimarySelectionOfferListener : PrimarySelectionOfferListener
{
    using PrimarySelectionOfferListener::PrimarySelectionOfferListener;

    MOCK_METHOD(void, offer, (zwp_primary_selection_offer_v1* offer, const char* mime_type), (override));
};

struct MockPrimarySelectionSourceListener : PrimarySelectionSourceListener
{
    using PrimarySelectionSourceListener::PrimarySelectionSourceListener;

    MOCK_METHOD(void, send, (zwp_primary_selection_source_v1* source, const char* mime_type, int32_t fd), (override));

    MOCK_METHOD(void, cancelled, (zwp_primary_selection_source_v1*), (override));
};

struct StubPrimarySelectionDeviceListener : PrimarySelectionDeviceListener
{
    StubPrimarySelectionDeviceListener(
        zwp_primary_selection_device_v1* device,
        PrimarySelectionOfferListener& offer_listener) :
        PrimarySelectionDeviceListener{device},
        offer_listener{offer_listener}
    {
    }

    void data_offer(zwp_primary_selection_device_v1* device, zwp_primary_selection_offer_v1* offer) override
    {
        offer_listener.listen_to(offer);
        PrimarySelectionDeviceListener::data_offer(device, offer);
    }

    void selection(zwp_primary_selection_device_v1* device, zwp_primary_selection_offer_v1* offer) override
    {
        selected = offer;
        PrimarySelectionDeviceListener::selection(device, offer);
    }

    PrimarySelectionOfferListener& offer_listener;
    zwp_primary_selection_offer_v1* selected = nullptr;
};

struct StubPrimarySelectionSourceListener : PrimarySelectionSourceListener
{
    using PrimarySelectionSourceListener::PrimarySelectionSourceListener;

    void send(zwp_primary_selection_source_v1*, const char*, int32_t fd)
    {
        ASSERT_THAT(write(fd, any_mime_data, sizeof any_mime_data), Eq(ssize_t(sizeof any_mime_data)));
        close(fd);
    }
};

struct Pipe
{
    int source;
    int sink;

    Pipe() { socketpair(AF_LOCAL, SOCK_STREAM, 0, &source); }
    Pipe(Pipe const&) = delete;
    Pipe& operator=(Pipe const&) = delete;

    ~Pipe() { close(source); close(sink); }
};
}

TEST_F(PrimarySelection, source_can_offer)
{
    source_app.offer(any_mime_type);
    source_app.set_selection();
}

TEST_F(PrimarySelection, sink_can_listen)
{
    MockPrimarySelectionDeviceListener  device_listener{sink_app.device};
    MockPrimarySelectionOfferListener   offer_listener;

    InSequence seq;
    EXPECT_CALL(device_listener, data_offer(_, _))
        .WillOnce(Invoke([&](auto*, auto* id) { offer_listener.listen_to(id); }));

    EXPECT_CALL(offer_listener, offer(_, StrEq(any_mime_type)));

    EXPECT_CALL(device_listener, selection(_, _));

    source_app.offer(any_mime_type);
    source_app.set_selection();

    sink_app.roundtrip();
}

TEST_F(PrimarySelection, sink_can_request)
{
    PrimarySelectionOfferListener   offer_listener;
    StubPrimarySelectionDeviceListener  device_listener{sink_app.device, offer_listener};
    source_app.offer(any_mime_type);
    source_app.set_selection();
    sink_app.roundtrip();
    ASSERT_THAT(device_listener.selected, NotNull());

    Pipe pipe;
    zwp_primary_selection_offer_v1_receive(device_listener.selected, any_mime_type, pipe.source);
    sink_app.roundtrip();
}

TEST_F(PrimarySelection, source_sees_request)
{
    MockPrimarySelectionSourceListener  source_listener{source_app.source};
    PrimarySelectionOfferListener   offer_listener;
    StubPrimarySelectionDeviceListener  device_listener{sink_app.device, offer_listener};
    source_app.offer(any_mime_type);
    source_app.set_selection();
    sink_app.roundtrip();
    ASSERT_THAT(device_listener.selected, NotNull());

    EXPECT_CALL(source_listener, send(_, _, _))
        .Times(1)
        .WillRepeatedly(Invoke([&](auto*, auto*, int fd) { close(fd); }));

    Pipe pipe;
    zwp_primary_selection_offer_v1_receive(device_listener.selected, any_mime_type, pipe.source);
    sink_app.roundtrip();
    source_app.roundtrip();
}

TEST_F(PrimarySelection, source_can_supply_request)
{
    StubPrimarySelectionSourceListener source_listener{source_app.source};
    PrimarySelectionOfferListener   offer_listener;
    StubPrimarySelectionDeviceListener  device_listener{sink_app.device, offer_listener};
    source_app.offer(any_mime_type);
    source_app.set_selection();
    sink_app.roundtrip();
    ASSERT_THAT(device_listener.selected, NotNull());

    Pipe pipe;
    zwp_primary_selection_offer_v1_receive(device_listener.selected, any_mime_type, pipe.source);
    sink_app.roundtrip();
    source_app.roundtrip();

    char buffer[128];
    EXPECT_THAT(read(pipe.sink, buffer, sizeof buffer), Eq(ssize_t(sizeof any_mime_data)));
    EXPECT_THAT(buffer, StrEq(any_mime_data));
}