2
* Copyright © 2016 Canonical Ltd.
4
* This program is free software: you can redistribute it and/or modify it
5
* under the terms of the GNU General Public License version 3,
6
* as published by the Free Software Foundation.
8
* This program is distributed in the hope that it will be useful,
9
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
* GNU General Public License for more details.
13
* You should have received a copy of the GNU General Public License
14
* along with this program. If not, see <http://www.gnu.org/licenses/>.
16
* Authored by: Alan Griffiths <alan@octopull.co.uk>
19
#include <mir/version.h>
21
#if MIR_SERVER_VERSION >= MIR_VERSION_NUMBER(0, 25, 0)
22
#include <mir_toolkit/events/surface_placement.h>
25
#include <miral/toolkit/surface_spec.h>
26
#include <miral/toolkit/surface.h>
28
#include <mir/test/signal.h>
29
#include "test_server.h"
31
#include <gtest/gtest.h>
32
#include <gmock/gmock.h>
34
using namespace std::literals::chrono_literals;
36
using namespace testing;
37
namespace mt = mir::test;
38
namespace mtf = mir_test_framework;
40
using namespace miral::toolkit;
44
struct WindowPlacementClientAPI : miral::TestServer
48
miral::TestServer::SetUp();
50
char const* const test_name = __PRETTY_FUNCTION__;
52
connection = connect_client(test_name);
53
auto spec = SurfaceSpec::for_normal_surface(connection, 400, 400, mir_pixel_format_argb_8888)
56
parent = spec.create_surface();
59
void TearDown() override
65
miral::TestServer::TearDown();
68
Connection connection;
74
#if MIR_SERVER_VERSION >= MIR_VERSION_NUMBER(0, 25, 0)
80
CheckPlacement(int left, int top, unsigned int width, unsigned int height) :
81
expected{left, top, width, height} {}
83
void check(MirSurfacePlacementEvent const* placement_event)
85
EXPECT_THAT(mir_surface_placement_get_relative_position(placement_event).top, Eq(expected.top));
86
EXPECT_THAT(mir_surface_placement_get_relative_position(placement_event).left, Eq(expected.left));
87
EXPECT_THAT(mir_surface_placement_get_relative_position(placement_event).height, Eq(expected.height));
88
EXPECT_THAT(mir_surface_placement_get_relative_position(placement_event).width, Eq(expected.width));
93
static void callback(MirSurface* /*surface*/, MirEvent const* event, void* context)
95
if (mir_event_get_type(event) == mir_event_type_surface_placement)
97
auto const placement_event = mir_event_get_surface_placement_event(event);
98
static_cast<CheckPlacement*>(context)->check(placement_event);
104
EXPECT_TRUE(received.wait_for(400ms));
108
MirRectangle const expected;
113
// It would be nice to verify creation and movement placement notifications in separate tests,
114
// However, to avoid a racy test, we need to detect both anyway. This seems like a good trade-off.
115
TEST_F(WindowPlacementClientAPI, given_menu_placements_away_from_edges_when_notified_result_is_as_requested)
117
char const* const test_name = __PRETTY_FUNCTION__;
123
MirRectangle aux_rect{10, 20, 3, 4};
124
CheckPlacement expected{aux_rect.left+(int)aux_rect.width, aux_rect.top, dx, dy};
126
auto const spec = SurfaceSpec::
127
for_menu(connection, dx, dy, mir_pixel_format_argb_8888, parent, &aux_rect, mir_edge_attachment_any)
128
.set_event_handler(&CheckPlacement::callback, &expected)
129
.set_name(test_name);
131
child = spec.create_surface();
134
// subsequent movement
136
MirRectangle aux_rect{50, 60, 5, 7};
137
CheckPlacement expected{aux_rect.left-dx, aux_rect.top, dx, dy};
139
auto const spec = SurfaceSpec::for_changes(connection)
140
.set_event_handler(&CheckPlacement::callback, &expected)
141
.set_placement(&aux_rect, mir_placement_gravity_northwest, mir_placement_gravity_northeast, mir_placement_hints_flip_x, 0, 0);
143
spec.apply_to(child);