File: test_surface_impl.cpp

package info (click to toggle)
mir 2.20.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 20,636 kB
  • sloc: cpp: 174,574; xml: 13,422; ansic: 8,221; python: 1,337; sh: 874; makefile: 216; javascript: 37
file content (294 lines) | stat: -rw-r--r-- 9,292 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
/*
 * 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_private.h"
#include "mir/events/event_builders.h"
#include "src/server/scene/basic_surface.h"
#include "mir/scene/surface_event_source.h"
#include "src/server/report/null_report_factory.h"
#include "mir/scene/output_properties_cache.h"

#include "mir/test/doubles/fake_display_configuration_observer_registrar.h"
#include "mir/test/doubles/mock_buffer_stream.h"
#include "mir/test/doubles/mock_event_sink.h"
#include "mir/test/doubles/explicit_executor.h"
#include "mir/test/fake_shared.h"
#include "mir/test/event_matchers.h"

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

namespace ms = mir::scene;
namespace msh = mir::shell;
namespace mf = mir::frontend;
namespace mw = mir::wayland;
namespace mc = mir::compositor;
namespace mg = mir::graphics;
namespace mi = mir::input;
namespace mev = mir::events;
namespace geom = mir::geometry;
namespace mt = mir::test;
namespace mtd = mt::doubles;
namespace mr = mir::report;

namespace
{

MATCHER_P(MirResizeEventEq, event, "")
{
    if (arg.type() != mir_event_type_resize ||
        event->type() != mir_event_type_resize)
    {
        return false;
    }

    return arg.to_resize()->width() == event->to_resize()->width() &&
           arg.to_resize()->height() == event->to_resize()->height() &&
           arg.to_resize()->surface_id() == event->to_resize()->surface_id();
}

typedef testing::NiceMock<mtd::MockBufferStream> StubBufferStream;

struct Surface : testing::Test
{
    std::shared_ptr<StubBufferStream> const buffer_stream = std::make_shared<StubBufferStream>();

    void SetUp()
    {
        using namespace testing;

        surface = std::make_shared<ms::BasicSurface>(
            nullptr /* session */,
            mw::Weak<mf::WlSurface>{},
            std::string("stub"),
            geom::Rectangle{{},{}},
            mir_pointer_unconfined,
            std::list<ms::StreamInfo> { { buffer_stream, {}} },
            nullptr,
            report,
            display_config_registrar);
    }

    mf::SurfaceId stub_id;
    std::shared_ptr<ms::SceneReport> const report = mr::null_scene_report();
    std::shared_ptr<ms::BasicSurface> surface;
    std::shared_ptr<mtd::FakeDisplayConfigurationObserverRegistrar> const display_config_registrar =
        std::make_shared<mtd::FakeDisplayConfigurationObserverRegistrar>();
};
}

TEST_F(Surface, attributes)
{
    using namespace testing;

    EXPECT_THROW({
        surface->configure(static_cast<MirWindowAttrib>(111), 222);
    }, std::logic_error);
}

TEST_F(Surface, types)
{
    using namespace testing;

    EXPECT_EQ(mir_window_type_normal, surface->type());

    EXPECT_EQ(mir_window_type_utility,
              surface->configure(mir_window_attrib_type,
                             mir_window_type_utility));
    EXPECT_EQ(mir_window_type_utility, surface->type());

    EXPECT_THROW({
        surface->configure(mir_window_attrib_type, 999);
    }, std::logic_error);
    EXPECT_THROW({
        surface->configure(mir_window_attrib_type, -1);
    }, std::logic_error);
    EXPECT_EQ(mir_window_type_utility, surface->type());

    EXPECT_EQ(mir_window_type_dialog,
              surface->configure(mir_window_attrib_type,
                             mir_window_type_dialog));
    EXPECT_EQ(mir_window_type_dialog, surface->type());

    EXPECT_EQ(mir_window_type_freestyle,
              surface->configure(mir_window_attrib_type,
                             mir_window_type_freestyle));
    EXPECT_EQ(mir_window_type_freestyle, surface->type());
}

TEST_F(Surface, states)
{
    using namespace testing;

    EXPECT_EQ(mir_window_state_restored, surface->state());

    EXPECT_EQ(mir_window_state_vertmaximized,
              surface->configure(mir_window_attrib_state,
                             mir_window_state_vertmaximized));
    EXPECT_EQ(mir_window_state_vertmaximized, surface->state());

    EXPECT_THROW({
        surface->configure(mir_window_attrib_state, 999);
    }, std::logic_error);
    EXPECT_THROW({
        surface->configure(mir_window_attrib_state, -1);
    }, std::logic_error);
    EXPECT_EQ(mir_window_state_vertmaximized, surface->state());

    EXPECT_EQ(mir_window_state_minimized,
              surface->configure(mir_window_attrib_state,
                             mir_window_state_minimized));
    EXPECT_EQ(mir_window_state_minimized, surface->state());

    EXPECT_EQ(mir_window_state_fullscreen,
              surface->configure(mir_window_attrib_state,
                             mir_window_state_fullscreen));
    EXPECT_EQ(mir_window_state_fullscreen, surface->state());
}

TEST_F(Surface, clamps_undersized_resize)
{
    using namespace testing;

    geom::Size const try_size{-123, -456};
    geom::Size const expect_size{1, 1};

    surface->resize(try_size);
    EXPECT_EQ(expect_size, surface->window_size());
}

TEST_F(Surface, emits_resize_events)
{
    using namespace testing;

    geom::Size const new_size{123, 456};
    auto sink = std::make_shared<mtd::MockEventSink>();
    ms::OutputPropertiesCache cache;
    auto const observer = std::make_shared<ms::SurfaceEventSource>(stub_id, cache, sink);

    mtd::ExplicitExecutor executor;
    surface->register_interest(observer, executor);

    auto e = mev::make_window_resize_event(stub_id, new_size);
    EXPECT_CALL(*sink, handle_event(MirResizeEventEq(e.get())))
        .Times(1);

    surface->resize(new_size);
    executor.execute();
    EXPECT_EQ(new_size, surface->window_size());
}

TEST_F(Surface, emits_resize_events_only_on_change)
{
    using namespace testing;

    geom::Size const new_size{123, 456};
    geom::Size const new_size2{789, 1011};
    auto sink = std::make_shared<mtd::MockEventSink>();
    ms::OutputPropertiesCache cache;
    auto const observer = std::make_shared<ms::SurfaceEventSource>(stub_id, cache, sink);

    mtd::ExplicitExecutor executor;
    surface->register_interest(observer, executor);

    auto e = mev::make_window_resize_event(stub_id, new_size);
    EXPECT_CALL(*sink, handle_event(MirResizeEventEq(e.get())))
        .Times(1);

    auto e2 = mev::make_window_resize_event(stub_id, new_size2);
    EXPECT_CALL(*sink, handle_event(MirResizeEventEq(e2.get())))
        .Times(1);

    surface->resize(new_size);
    EXPECT_EQ(new_size, surface->window_size());
    surface->resize(new_size);
    EXPECT_EQ(new_size, surface->window_size());

    surface->resize(new_size2);
    EXPECT_EQ(new_size2, surface->window_size());
    surface->resize(new_size2);
    EXPECT_EQ(new_size2, surface->window_size());

    executor.execute();
}

TEST_F(Surface, sends_focus_notifications_when_focus_gained_and_lost)
{
    using namespace testing;

    mtd::MockEventSink sink;

    {
        InSequence seq;
        EXPECT_CALL(sink, handle_event(mt::WindowEvent(mir_window_attrib_focus, mir_window_focus_state_focused)))
            .Times(1);
        EXPECT_CALL(sink, handle_event(mt::WindowEvent(mir_window_attrib_focus, mir_window_focus_state_unfocused)))
            .Times(1);
    }

    ms::OutputPropertiesCache cache;
    auto const observer = std::make_shared<ms::SurfaceEventSource>(stub_id, cache, mt::fake_shared(sink));

    mtd::ExplicitExecutor executor;
    surface->register_interest(observer, executor);

    surface->configure(mir_window_attrib_focus, mir_window_focus_state_focused);
    surface->configure(mir_window_attrib_focus, mir_window_focus_state_unfocused);
    executor.execute();
}
MATCHER_P(MirCloseSurfaceEventMatches, event, "")
{
    return arg.type() == event->type();
}

TEST_F(Surface, emits_client_close_events)
{
    using namespace testing;

    auto sink = std::make_shared<mtd::MockEventSink>();
    ms::OutputPropertiesCache cache;
    auto const observer = std::make_shared<ms::SurfaceEventSource>(stub_id, cache, sink);

    mtd::ExplicitExecutor executor;
    surface->register_interest(observer, executor);

    MirCloseWindowEvent e;
    e.to_close_window()->set_surface_id(stub_id.as_value());

    EXPECT_CALL(*sink, handle_event(MirCloseSurfaceEventMatches(&e))).Times(1);

    surface->request_client_surface_close();
    executor.execute();
}

TEST_F(Surface, preferred_orientation_mode_defaults_to_any)
{
    using namespace testing;

    ms::BasicSurface surf(
        nullptr /* session */,
        {} /* wayland_surface */,
        std::string("stub"),
        geom::Rectangle{{},{}},
        mir_pointer_unconfined,
        std::list<ms::StreamInfo> { { buffer_stream, {}} },
        std::shared_ptr<mg::CursorImage>(),
        report,
        display_config_registrar);

    EXPECT_EQ(mir_orientation_mode_any, surf.query(mir_window_attrib_preferred_orientation));
}