~mir-team/mir/trunk

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
/*
 * Copyright © 2013 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 "mir_toolkit/mir_client_library_lightdm.h"

#include "mir/frontend/surface_id.h"
#include "mir/frontend/surface_creation_parameters.h"
#include "mir/frontend/shell.h"

#include "mir_test_doubles/mock_display.h"
#include "mir_test_framework/display_server_test_fixture.h"

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

#include <chrono>
#include <thread>
#include <fcntl.h>

namespace mf = mir::frontend;
namespace mg = mir::graphics;
namespace geom = mir::geometry;
namespace mtd = mir::test::doubles;
namespace mtf = mir_test_framework;

namespace
{
    char const* const mir_test_socket = mtf::test_socket_file().c_str();
}

namespace mir
{
namespace
{
struct ClientConfigCommon : TestingClientConfiguration
{
    ClientConfigCommon() :
        connection(0),
        surface(0)
    {
    }

    static void connection_callback(MirConnection* connection, void* context)
    {
        ClientConfigCommon* config = reinterpret_cast<ClientConfigCommon *>(context);
        config->connection = connection;
    }

    static void create_surface_callback(MirSurface* surface, void* context)
    {
        ClientConfigCommon* config = reinterpret_cast<ClientConfigCommon *>(context);
        config->surface_created(surface);
    }

    static void next_buffer_callback(MirSurface* surface, void* context)
    {
        ClientConfigCommon* config = reinterpret_cast<ClientConfigCommon *>(context);
        config->next_buffer(surface);
    }

    static void release_surface_callback(MirSurface* surface, void* context)
    {
        ClientConfigCommon* config = reinterpret_cast<ClientConfigCommon *>(context);
        config->surface_released(surface);
    }

    virtual void connected(MirConnection* new_connection)
    {
        connection = new_connection;
    }

    virtual void surface_created(MirSurface* new_surface)
    {
        surface = new_surface;
    }

    virtual void next_buffer(MirSurface*)
    {
    }

    virtual void surface_released(MirSurface* /*released_surface*/)
    {
        surface = NULL;
    }

    MirConnection* connection;
    MirSurface* surface;

    void set_flag(const char* const flag_file)
    {
        close(open(flag_file, O_CREAT, S_IWUSR | S_IRUSR));
    }

    void wait_for(const char* const flag_file)
    {
        int fd = -1;
        while (-1 == (fd = open(flag_file, O_RDONLY, S_IWUSR | S_IRUSR)))
        {
            // Wait for focus_config process to init
            std::this_thread::sleep_for(std::chrono::milliseconds(1));
        }
        close(fd);
    }
};

class MockShell : public frontend::Shell
{
public:
    MockShell(std::shared_ptr<Shell> const& impl) :
        impl(impl)
    {
        using namespace ::testing;
        using frontend::Shell;
        ON_CALL(*this, open_session(_, _)).WillByDefault(Invoke(impl.get(), &Shell::open_session));
        ON_CALL(*this, close_session(_)).WillByDefault(Invoke(impl.get(), &Shell::close_session));

        ON_CALL(*this, tag_session_with_lightdm_id(_, _)).WillByDefault(Invoke(impl.get(), &Shell::tag_session_with_lightdm_id));
        ON_CALL(*this, focus_session_with_lightdm_id(_)).WillByDefault(Invoke(impl.get(), &Shell::focus_session_with_lightdm_id));

        ON_CALL(*this, create_surface_for(_, _)).WillByDefault(Invoke(impl.get(), &Shell::create_surface_for));
    }

    MOCK_METHOD2(open_session, std::shared_ptr<mf::Session> (std::string const& name, std::shared_ptr<mir::events::EventSink> const&));
    MOCK_METHOD1(close_session, void (std::shared_ptr<mf::Session> const& session));

    MOCK_METHOD2(tag_session_with_lightdm_id, void (std::shared_ptr<mf::Session> const& session, int id));
    MOCK_METHOD1(focus_session_with_lightdm_id, void (int id));

    MOCK_METHOD2(create_surface_for, frontend::SurfaceId(std::shared_ptr<mf::Session> const&, mf::SurfaceCreationParameters const&));

private:
    std::shared_ptr<frontend::Shell> const impl;
};
}

TEST_F(BespokeDisplayServerTestFixture, focus_management)
{
    struct ServerConfig : TestingServerConfiguration
    {
        mir::CachedPtr<mf::Shell> frontend_shell;

        std::shared_ptr<mf::Shell>
        the_frontend_shell() override
        {
            return frontend_shell(
                [this]() -> std::shared_ptr<mf::Shell>
                {
                    using namespace ::testing;

                    auto const& mock_shell = std::make_shared<MockShell>(
                        DefaultServerConfiguration::the_frontend_shell());

                    Sequence s1, s2;

                    EXPECT_CALL(*mock_shell, open_session(_, _))
                        .InSequence(s1, s2);

                    EXPECT_CALL(*mock_shell, create_surface_for(_,_))
                        .InSequence(s1);

                    EXPECT_CALL(*mock_shell, open_session(_, _))
                        .InSequence(s2);

                    EXPECT_CALL(*mock_shell, close_session(_))
                        .Times(2)
                        .InSequence(s1, s2);

                    {
                        using namespace testing;
                        InSequence test;

                        EXPECT_CALL(*mock_shell, tag_session_with_lightdm_id(_, _));
                        EXPECT_CALL(*mock_shell, focus_session_with_lightdm_id(_));
                    }

                    return mock_shell;
                });
        }
    } server_config;

    launch_server_process(server_config);

    int const lightdm_id = __LINE__;
    static char const* const focus_ready = "focus_management_focus_client_ready.tmp";
    static char const* const manager_done = "focus_management_focus_manager_done.tmp";
    std::remove(focus_ready);
    std::remove(manager_done);

    struct ClientConfig : ClientConfigCommon
    {
        void exec()
        {
            mir_wait_for(mir_connect_with_lightdm_id(
                mir_test_socket,
                lightdm_id,
                __PRETTY_FUNCTION__,
                connection_callback,
                this));

            ASSERT_TRUE(connection != NULL);

            MirSurfaceParameters const request_params =
            {
                __PRETTY_FUNCTION__,
                640, 480,
                mir_pixel_format_abgr_8888,
                mir_buffer_usage_hardware
            };

            mir_wait_for(mir_connection_create_surface(connection, &request_params, create_surface_callback, this));

            set_flag(focus_ready);

            wait_for(manager_done);
            mir_connection_release(connection);
        }
    } focus_config;

    struct LightdmConfig : ClientConfigCommon
    {
        void exec()
        {
            mir_wait_for(mir_connect(
                mir_test_socket,
                __PRETTY_FUNCTION__,
                connection_callback,
                this));

            wait_for(focus_ready);
            mir_select_focus_by_lightdm_id(connection, lightdm_id);

            mir_connection_release(connection);
            set_flag(manager_done);
        }
    } lightdm_config;

    launch_client_process(focus_config);
    launch_client_process(lightdm_config);
}
}