~alan-griffiths/miral/fix-1645284

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
/*
 * Copyright © 2016 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 <miral/toolkit/persistent_id.h>
#include <miral/toolkit/surface.h>
#include <miral/toolkit/surface_spec.h>

#include <miral/application_info.h>

#include <mir/version.h>

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

#include "test_server.h"

using namespace testing;


struct PersistentSurfaceId : public miral::TestServer
{
    auto get_first_window(miral::WindowManagerTools& tools) -> miral::Window
    {
        auto app = tools.find_application([&](miral::ApplicationInfo const& /*info*/){return true;});
        auto app_info = tools.info_for(app);
        return app_info.windows().at(0);
    }
};

#include <mir/scene/surface.h>

#if MIR_SERVER_VERSION >= MIR_VERSION_NUMBER(0, 24, 0)
TEST_F(PersistentSurfaceId, server_can_identify_window_specified_by_client)
{
    char const* const test_name = __PRETTY_FUNCTION__;
    using namespace miral::toolkit;

    auto const connection = connect_client(test_name);
    auto const spec = SurfaceSpec::for_normal_surface(connection, 50, 50, mir_pixel_format_argb_8888)
        .set_name(test_name);

    Surface const surface{spec.create_surface()};

    miral::toolkit::PersistentId client_surface_id{surface};

    invoke_tools([&](miral::WindowManagerTools& tools)
        {
            auto const& window_info = tools.info_for_window_id(client_surface_id.c_str());

            ASSERT_TRUE(window_info.window());
            ASSERT_THAT(window_info.name(), Eq(test_name));
        });
}

TEST_F(PersistentSurfaceId, server_returns_correct_id_for_window)
{
    char const* const test_name = __PRETTY_FUNCTION__;
    using namespace miral::toolkit;

    auto const connection = connect_client(test_name);
    auto const spec = SurfaceSpec::for_normal_surface(connection, 50, 50, mir_pixel_format_argb_8888)
        .set_name(test_name);

    Surface const surface{spec.create_surface()};

    miral::toolkit::PersistentId client_surface_id{surface};

    invoke_tools([&](miral::WindowManagerTools& tools)
        {
            auto window = get_first_window(tools);
            auto id = tools.id_for_window(window);

            ASSERT_THAT(client_surface_id.c_str(), Eq(id));
        });
}
#else
TEST_F(PersistentSurfaceId, server_fails_gracefully_to_identify_window_specified_by_client)
{
    char const* const test_name = __PRETTY_FUNCTION__;
    using namespace miral::toolkit;

    auto const connection = connect_client(test_name);
    auto const spec = SurfaceSpec::for_normal_surface(connection, 50, 50, mir_pixel_format_argb_8888)
        .set_name(test_name);

    Surface const surface{spec.create_surface()};

    miral::toolkit::PersistentId client_surface_id{surface};

    invoke_tools([&](miral::WindowManagerTools& tools)
        {
            EXPECT_THROW(tools.info_for_window_id(client_surface_id.c_str()), std::runtime_error);
        });
}

TEST_F(PersistentSurfaceId, server_fails_gracefully_to_return_id_for_window)
{
    char const* const test_name = __PRETTY_FUNCTION__;
    using namespace miral::toolkit;

    auto const connection = connect_client(test_name);
    auto const spec = SurfaceSpec::for_normal_surface(connection, 50, 50, mir_pixel_format_argb_8888)
        .set_name(test_name);

    Surface const surface{spec.create_surface()};

    miral::toolkit::PersistentId client_surface_id{surface};

    invoke_tools([](miral::WindowManagerTools& tools)
        {
            auto window = get_first_window(tools);
            EXPECT_THROW(tools.id_for_window(window), std::runtime_error);
        });
}
#endif

TEST_F(PersistentSurfaceId, server_fails_gracefully_to_identify_window_from_garbage_id)
{
    char const* const test_name = __PRETTY_FUNCTION__;
    using namespace miral::toolkit;

    auto const connection = connect_client(test_name);
    auto const spec = SurfaceSpec::for_normal_surface(connection, 50, 50, mir_pixel_format_argb_8888)
        .set_name(test_name);

    Surface const surface{spec.create_surface()};

    miral::toolkit::PersistentId client_surface_id{surface};

    invoke_tools([](miral::WindowManagerTools& tools)
        {
            EXPECT_THROW(tools.info_for_window_id("garbage"), std::exception);
        });
}

TEST_F(PersistentSurfaceId, server_fails_gracefully_when_id_for_null_window_requested)
{
    invoke_tools([](miral::WindowManagerTools& tools)
        {
            miral::Window window;
            EXPECT_THROW(tools.id_for_window(window), std::runtime_error);
        });
}