File: test_session_manager.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 (222 lines) | stat: -rw-r--r-- 8,879 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
/*
 * 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 "src/server/scene/session_manager.h"

#include "mir/scene/session.h"
#include "mir/scene/session_container.h"
#include "mir/scene/null_session_listener.h"
#include "mir/graphics/display_configuration_observer.h"
#include "mir/compositor/buffer_stream.h"
#include "mir/scene/null_surface_observer.h"
#include "src/server/scene/basic_surface.h"
#include "src/include/server/mir/scene/session_event_sink.h"
#include "src/server/report/null_report_factory.h"

#include "mir/test/doubles/mock_surface_stack.h"
#include "mir/test/doubles/mock_session_listener.h"
#include "mir/test/doubles/stub_buffer_stream.h"
#include "mir/test/doubles/null_session_event_sink.h"
#include "mir/test/doubles/null_event_sink.h"
#include "mir/test/doubles/stub_surface_factory.h"
#include "mir/test/doubles/null_application_not_responding_detector.h"
#include "mir/test/doubles/stub_display.h"
#include "mir/test/doubles/stub_buffer_allocator.h"
#include "mir/test/doubles/stub_observer_registrar.h"

#include "mir/test/fake_shared.h"
#include "mir/test/make_surface_spec.h"

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

namespace mf = mir::frontend;
namespace mw = mir::wayland;
namespace mi = mir::input;
namespace ms = mir::scene;
namespace mg = mir::graphics;
namespace mc = mir::compositor;
namespace geom = mir::geometry;
namespace mt = mir::test;
namespace mtd = mir::test::doubles;

namespace
{
struct MockSessionEventSink : public ms::SessionEventSink
{
    MOCK_METHOD1(handle_focus_change, void(std::shared_ptr<ms::Session> const& session));
    MOCK_METHOD0(handle_no_focus, void());
    MOCK_METHOD1(handle_session_stopping, void(std::shared_ptr<ms::Session> const& session));
};

struct SessionManagerSetup : public testing::Test
{
    std::shared_ptr<ms::Surface> dummy_surface = std::make_shared<ms::BasicSurface>(
        nullptr /* session */,
        mw::Weak<mf::WlSurface>{},
        std::string("stub"),
        geom::Rectangle{{},{}},
        mir_pointer_unconfined,
        std::list<ms::StreamInfo> { { std::make_shared<mtd::StubBufferStream>(), {}} },
        std::shared_ptr<mg::CursorImage>(),
        mir::report::null_scene_report(),
        std::make_shared<mtd::FakeDisplayConfigurationObserverRegistrar>());
    testing::NiceMock<mtd::MockSurfaceStack> surface_stack;
    ms::SessionContainer container;
    ms::NullSessionListener session_listener;
    mtd::StubSurfaceFactory stub_surface_factory;
    mtd::StubDisplay display{2};
    mtd::NullEventSink event_sink;
    mtd::StubBufferAllocator allocator;
    mtd::StubObserverRegistrar<mir::graphics::DisplayConfigurationObserver> display_config_registrar;

    ms::SessionManager session_manager{mt::fake_shared(surface_stack),
        mt::fake_shared(stub_surface_factory),
        mt::fake_shared(container),
        std::make_shared<mtd::NullSessionEventSink>(),
        mt::fake_shared(session_listener),
        mt::fake_shared(display),
        std::make_shared<mtd::NullANRDetector>(),
        mt::fake_shared(allocator),
        mt::fake_shared(display_config_registrar)};
};

}

namespace
{
struct SessionManagerSessionListenerSetup : public testing::Test
{
    mtd::MockSurfaceStack surface_stack;
    ms::SessionContainer container;
    testing::NiceMock<mtd::MockSessionListener> session_listener;
    mtd::StubSurfaceFactory stub_surface_factory;
    mtd::StubDisplay display{2};
    mtd::StubBufferAllocator allocator;
    mtd::StubObserverRegistrar<mir::graphics::DisplayConfigurationObserver> display_config_registrar;

    ms::SessionManager session_manager{
        mt::fake_shared(surface_stack),
        mt::fake_shared(stub_surface_factory),
        mt::fake_shared(container),
        std::make_shared<mtd::NullSessionEventSink>(),
        mt::fake_shared(session_listener),
        mt::fake_shared(display),
        std::make_shared<mtd::NullANRDetector>(),
        mt::fake_shared(allocator),
        mt::fake_shared(display_config_registrar)};
};
}

TEST_F(SessionManagerSessionListenerSetup, session_listener_is_notified_of_lifecycle)
{
    using namespace ::testing;

    EXPECT_CALL(session_listener, starting(_)).Times(1);
    EXPECT_CALL(session_listener, stopping(_)).Times(1);

    auto session = session_manager.open_session(__LINE__, mir::Fd{mir::Fd::invalid}, "XPlane", std::shared_ptr<mf::EventSink>());
    session_manager.close_session(session);
}

TEST_F(SessionManagerSessionListenerSetup, additional_listeners_receive_session_callbacks)
{
    using namespace ::testing;

    auto additional_listener = std::make_shared<testing::NiceMock<mtd::MockSessionListener>>();
    EXPECT_CALL(*additional_listener, starting(_)).Times(1);
    EXPECT_CALL(*additional_listener, stopping(_)).Times(1);

    session_manager.add_listener(additional_listener);
    auto session = session_manager.open_session(__LINE__, mir::Fd{mir::Fd::invalid}, "XPlane", std::shared_ptr<mf::EventSink>());
    session_manager.close_session(session);
}

TEST_F(SessionManagerSessionListenerSetup, additional_listeners_receive_focus_changes)
{
    using namespace ::testing;

    auto additional_listener = std::make_shared<testing::NiceMock<mtd::MockSessionListener>>();
    EXPECT_CALL(*additional_listener, starting(_)).Times(1);
    EXPECT_CALL(*additional_listener, focused(_)).Times(1);
    EXPECT_CALL(*additional_listener, unfocused()).Times(1);

    session_manager.add_listener(additional_listener);
    auto session = session_manager.open_session(__LINE__, mir::Fd{mir::Fd::invalid}, "XPlane", std::shared_ptr<mf::EventSink>());
    session_manager.set_focus_to(session);
    session_manager.unset_focus();
}

TEST_F(SessionManagerSessionListenerSetup, additional_listeners_receive_surface_creation)
{
    using namespace ::testing;
    ms::NullSurfaceObserver observer;
    auto additional_listener = std::make_shared<testing::NiceMock<mtd::MockSessionListener>>();
    EXPECT_CALL(*additional_listener, starting(_)).Times(1);
    EXPECT_CALL(*additional_listener, surface_created(_,_)).Times(1);

    session_manager.add_listener(additional_listener);
    auto session = session_manager.open_session(__LINE__, mir::Fd{mir::Fd::invalid}, "XPlane", std::shared_ptr<mf::EventSink>());
    auto bs = std::dynamic_pointer_cast<mc::BufferStream>(session->create_buffer_stream(
        mg::BufferProperties{{640, 480}, mir_pixel_format_abgr_8888, mg::BufferUsage::hardware}));
    session->create_surface(nullptr, {}, mt::make_surface_spec(bs), mt::fake_shared(observer), nullptr);
}

namespace
{
struct SessionManagerSessionEventsSetup : public testing::Test
{
    mtd::MockSurfaceStack surface_stack;
    ms::SessionContainer container;
    MockSessionEventSink session_event_sink;
    testing::NiceMock<mtd::MockSessionListener> session_listener;
    mtd::StubSurfaceFactory stub_surface_factory;
    mtd::StubDisplay display{3};
    mtd::StubBufferAllocator allocator;
    mtd::StubObserverRegistrar<mir::graphics::DisplayConfigurationObserver> display_config_registrar;

    ms::SessionManager session_manager{
        mt::fake_shared(surface_stack),
        mt::fake_shared(stub_surface_factory),
        mt::fake_shared(container),
        mt::fake_shared(session_event_sink),
        mt::fake_shared(session_listener),
        mt::fake_shared(display),
        std::make_shared<mtd::NullANRDetector>(),
        mt::fake_shared(allocator),
        mt::fake_shared(display_config_registrar)};
};
}

TEST_F(SessionManagerSessionEventsSetup, session_event_sink_is_notified_of_lifecycle)
{
    using namespace ::testing;

    auto session = session_manager.open_session(__LINE__, mir::Fd{mir::Fd::invalid}, "XPlane", std::shared_ptr<mf::EventSink>());
    auto session1 = session_manager.open_session(__LINE__, mir::Fd{mir::Fd::invalid}, "Bla", std::shared_ptr<mf::EventSink>());

    Mock::VerifyAndClearExpectations(&session_event_sink);

    EXPECT_CALL(session_event_sink, handle_focus_change(_)).Times(AnyNumber());
    EXPECT_CALL(session_event_sink, handle_no_focus()).Times(AnyNumber());

    InSequence s;
    EXPECT_CALL(session_event_sink, handle_session_stopping(_)).Times(1);
    EXPECT_CALL(session_event_sink, handle_session_stopping(_)).Times(1);

    session_manager.close_session(session1);
    session_manager.close_session(session);
}