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
|
/*
* 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 <gtest/gtest.h>
#include <gmock/gmock.h>
#include <mir/graphics/platform.h>
#include "src/platforms/x11/graphics/display.h"
#include "src/platforms/x11/graphics/platform.h"
#include "src/server/report/null/display_report.h"
#include <mir/graphics/display_configuration.h>
#include <mir/test/doubles/null_display_configuration_policy.h>
#include <mir/test/doubles/mock_egl.h>
#include <mir/test/doubles/mock_x11.h>
#include <mir/test/doubles/mock_x11_resources.h>
#include <mir/test/doubles/mock_gl_config.h>
#include <mir/test/fake_shared.h>
namespace mg=mir::graphics;
namespace mgx=mg::X;
namespace mt=mir::test;
namespace mtd=mt::doubles;
namespace geom=mir::geometry;
using namespace testing;
namespace
{
class X11DisplayTest : public ::testing::Test
{
public:
std::vector<mgx::X11OutputConfig> sizes{{geom::Size{1280, 1024}}};
X11DisplayTest()
{
using namespace testing;
EGLint const client_version = 2;
ON_CALL(mock_egl, eglQueryString(_, EGL_EXTENSIONS))
.WillByDefault(Return("other stuff and EGL_CHROMIUM_sync_control"));
ON_CALL(mock_egl, eglQueryContext(mock_egl.fake_egl_display,
mock_egl.fake_egl_context,
EGL_CONTEXT_CLIENT_VERSION,
_))
.WillByDefault(DoAll(SetArgPointee<3>(client_version),
Return(EGL_TRUE)));
ON_CALL(mock_egl, eglQuerySurface(mock_egl.fake_egl_display,
mock_egl.fake_egl_surface,
EGL_WIDTH,
_))
.WillByDefault(DoAll(SetArgPointee<3>(sizes[0].size.width.as_int()),
Return(EGL_TRUE)));
ON_CALL(mock_egl, eglQuerySurface(mock_egl.fake_egl_display,
mock_egl.fake_egl_surface,
EGL_HEIGHT,
_))
.WillByDefault(DoAll(SetArgPointee<3>(sizes[0].size.height.as_int()),
Return(EGL_TRUE)));
ON_CALL(mock_egl, eglGetConfigAttrib(mock_egl.fake_egl_display,
_,
_,
_))
.WillByDefault(DoAll(SetArgPointee<3>(EGL_WINDOW_BIT),
Return(EGL_TRUE)));
}
void setup_x11_screen(
geom::Size const& pixel,
geom::Size const& mm,
std::vector<mgx::X11OutputConfig> const& window_sizes)
{
auto const mock_xcb_conn = dynamic_cast<mtd::MockXCBConnection*>(x11_resources.conn.get());
mock_xcb_conn->fake_screen.width_in_pixels = pixel.width.as_int();
mock_xcb_conn->fake_screen.height_in_pixels = pixel.height.as_int();
mock_xcb_conn->fake_screen.width_in_millimeters = mm.width.as_int();
mock_xcb_conn->fake_screen.height_in_millimeters = mm.height.as_int();
sizes = window_sizes;
}
std::shared_ptr<mgx::Display> create_display()
{
return std::make_shared<mgx::Display>(
mt::fake_shared(x11_resources),
nullptr,
"Mir on X",
sizes,
mt::fake_shared(null_display_configuration_policy),
std::make_shared<mir::report::null::DisplayReport>());
}
mtd::MockX11Resources x11_resources;
mtd::NullDisplayConfigurationPolicy null_display_configuration_policy;
::testing::NiceMock<mtd::MockEGL> mock_egl;
::testing::NiceMock<mtd::MockX11> mock_x11;
::testing::NiceMock<mtd::MockGLConfig> mock_gl_config;
};
}
TEST_F(X11DisplayTest, calculates_physical_size_of_display_based_on_default_screen)
{
auto const pixel = geom::Size{2880, 1800};
auto const mm = geom::Size{677, 290};
auto const window = geom::Size{1280, 1024};
auto const pixel_width = float(mm.width.as_int()) / float(pixel.width.as_int());
auto const pixel_height = float(mm.height.as_int()) / float(pixel.height.as_int());
auto const expected_size = geom::Size{window.width * pixel_width, window.height * pixel_height};
setup_x11_screen(pixel, mm, {window});
auto display = create_display();
auto config = display->configuration();
geom::Size reported_size;
config->for_each_output(
[&reported_size](mg::DisplayConfigurationOutput const& output)
{
reported_size = output.physical_size_mm;
}
);
EXPECT_THAT(reported_size, Eq(expected_size));
}
TEST_F(X11DisplayTest, sets_output_scale)
{
auto const scale = 2.5f;
auto const pixel = geom::Size{2880, 1800};
auto const mm = geom::Size{677, 290};
auto const window = geom::Size{1280, 1024};
setup_x11_screen(pixel, mm, {{window, scale}});
auto display = create_display();
auto config = display->configuration();
float reported_scale = -10.0f;
config->for_each_output(
[&reported_scale](mg::DisplayConfigurationOutput const& output)
{
reported_scale = output.scale;
}
);
EXPECT_THAT(reported_scale, FloatEq(scale));
}
TEST_F(X11DisplayTest, reports_a_resolution_that_matches_the_window_size)
{
auto const pixel = geom::Size{2880, 1800};
auto const mm = geom::Size{677, 290};
auto const window = geom::Size{1280, 1024};
setup_x11_screen(pixel, mm, {window});
auto display = create_display();
auto config = display->configuration();
geom::Size reported_resolution;
config->for_each_output(
[&reported_resolution](mg::DisplayConfigurationOutput const& output)
{
reported_resolution = output.modes[0].size;
}
);
EXPECT_THAT(reported_resolution, Eq(window));
}
TEST_F(X11DisplayTest, reports_resolutions_that_match_multiple_window_sizes)
{
auto const pixel = geom::Size{2880, 1800};
auto const mm = geom::Size{677, 290};
auto const window_sizes = std::vector<mgx::X11OutputConfig>{{{1280, 1024}}, {{600, 500}}, {{20, 50}}, {{600, 500}}};
setup_x11_screen(pixel, mm, window_sizes);
auto display = create_display();
auto config = display->configuration();
auto remaining_sizes = window_sizes;
config->for_each_output(
[&remaining_sizes](mg::DisplayConfigurationOutput const& output)
{
bool output_found = false;
for (auto i = 0u; i < remaining_sizes.size(); i++)
{
if (remaining_sizes[i].size == output.modes[0].size)
{
output_found = true;
remaining_sizes.erase(remaining_sizes.begin() + i);
break;
}
}
EXPECT_THAT(output_found, Eq(true));
}
);
EXPECT_THAT(remaining_sizes.size(), Eq(0));
}
TEST_F(X11DisplayTest, multiple_outputs_are_organized_horizontally)
{
auto const pixel = geom::Size{2880, 1800};
auto const mm = geom::Size{677, 290};
auto const window_sizes = std::vector<mgx::X11OutputConfig>{{{1280, 1024}}, {{600, 500}}, {{20, 50}}, {{600, 500}}};
auto const top_lefts = std::vector<geom::Point>{{0, 0}, {1280, 0}, {1880, 0}, {1900, 0}};
setup_x11_screen(pixel, mm, window_sizes);
auto display = create_display();
auto config = display->configuration();
config->for_each_output(
[&window_sizes, &top_lefts](mg::DisplayConfigurationOutput const& output)
{
bool output_found = false;
for (auto i = 0u; i < window_sizes.size(); i++)
{
if (window_sizes[i].size == output.modes[0].size && top_lefts[i] == output.top_left)
{
output_found = true;
break;
}
}
EXPECT_THAT(output_found, Eq(true));
}
);
}
TEST_F(X11DisplayTest, updates_scale_property_correctly)
{
auto const scale = 2.2f;
auto display = create_display();
auto config = display->configuration();
config->for_each_output([&](mg::UserDisplayConfigurationOutput &conf_output)
{
conf_output.scale = scale;
});
display->configure(*config.get());
auto new_scale = 1.0f;
config = display->configuration();
config->for_each_output(
[&new_scale](mg::DisplayConfigurationOutput const& output)
{
new_scale = output.scale;
}
);
EXPECT_THAT(new_scale, Eq(scale));
}
|