File: popup_window_placement.cpp

package info (click to toggle)
mir 2.25.2-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 22,104 kB
  • sloc: cpp: 192,777; xml: 13,784; ansic: 8,207; python: 1,304; sh: 794; makefile: 258
file content (592 lines) | stat: -rw-r--r-- 25,284 bytes parent folder | download | duplicates (3)
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
/*
 * 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 "test_window_manager_tools.h"

using namespace miral;
using namespace testing;
namespace mt = mir::test;

namespace
{
X const display_left{0};
Y const display_top{0};
Width  const display_width{640};
Height const display_height{480};

Rectangle const display_area{{display_left, display_top}, {display_width, display_height}};

auto const null_window = Window{};

mir::shell::SurfaceSpecification edge_attachment(Rectangle const& aux_rect, MirEdgeAttachment attachment)
{
    mir::shell::SurfaceSpecification result;
    result.aux_rect = aux_rect;
    result.edge_attachment = attachment;
    return result;
}

struct PopupWindowPlacement : mt::TestWindowManagerTools
{
    Size const initial_parent_size{600, 400};
    Size const initial_child_size{300, 300};
    Rectangle const rectangle_away_from_rhs{{20, 20}, {20, 20}};
    Rectangle const rectangle_near_rhs{{590, 20}, {10, 20}};
    Rectangle const rectangle_away_from_bottom{{20, 20}, {20, 20}};
    Rectangle const rectangle_near_bottom{{20, 380}, {20, 20}};
    Rectangle const rectangle_near_both_sides{{0, 20}, {600, 20}};
    Rectangle const rectangle_near_both_sides_and_bottom{{0, 380}, {600, 20}};
    Rectangle const rectangle_near_all_sides{{0, 20}, {600, 380}};
    Rectangle const rectangle_near_both_bottom_right{{400, 380}, {200, 20}};

    Window parent;
    Window child;

    WindowSpecification modification;

    void SetUp() override
    {
        notify_configuration_applied(create_fake_display_configuration({display_area}));
//
        mir::shell::SurfaceSpecification creation_parameters;
        basic_window_manager.add_session(session);

        EXPECT_CALL(*window_manager_policy, advise_new_window(_))
            .WillOnce(Invoke([this](WindowInfo const& window_info){ parent = window_info.window(); }))
            .WillOnce(Invoke([this](WindowInfo const& window_info){ child = window_info.window(); }));

        creation_parameters.set_size(initial_parent_size);
        basic_window_manager.add_surface(session, creation_parameters, &create_surface);

        creation_parameters.type = mir_window_type_menu;
        creation_parameters.parent = parent;
        creation_parameters.set_size(initial_child_size);
        basic_window_manager.add_surface(session, creation_parameters, &create_surface);

        // Clear the expectations used to capture parent & child
        Mock::VerifyAndClearExpectations(window_manager_policy);
    }

    void TearDown() override
    {
//        std::cerr << "DEBUG parent position:" << Rectangle{parent.top_left(), parent.size()} << '\n';
//        std::cerr << "DEBUG child position :" << Rectangle{child.top_left(), child.size()} << '\n';
    }

    auto aux_rect_position() -> Rectangle
    {
        auto rectangle = modification.aux_rect().value();
        std::shared_ptr<mir::scene::Surface> const parent_scene_surface{parent};
        rectangle.top_left += as_displacement(parent_scene_surface->top_left());
        rectangle.top_left += parent_scene_surface->content_offset();
        return rectangle;
    }

    auto on_top_edge() -> Point
    {
        return aux_rect_position().top_left - as_displacement(child.size()).dy;
    }

    auto on_right_edge() -> Point
    {
        return aux_rect_position().top_right();
    }

    auto on_left_edge() -> Point
    {
        return aux_rect_position().top_left - as_displacement(child.size()).dx;
    }

    auto on_bottom_edge() -> Point
    {
        return aux_rect_position().bottom_left();
    }
};
}

TEST_F(PopupWindowPlacement, fixture_sets_up_parent_and_child)
{
    ASSERT_THAT(parent, Ne(null_window));
    ASSERT_THAT(parent.size(), Eq(initial_parent_size));
    ASSERT_THAT(basic_window_manager.info_for(parent).children(), ElementsAre(child));
    ASSERT_THAT(basic_window_manager.info_for(parent).type(), Eq(mir_window_type_normal));

    ASSERT_THAT(child, Ne(null_window));
    ASSERT_THAT(child.size(), Eq(initial_child_size));
    ASSERT_THAT(basic_window_manager.info_for(child).parent(), Eq(parent));
    ASSERT_THAT(basic_window_manager.info_for(child).type(), Eq(mir_window_type_menu));
}


/* From the Mir client API:
 * Positioning of the surface is specified with respect to the parent surface
 * via an adjacency rectangle. The server will attempt to choose an edge of the
 * adjacency rectangle on which to place the surface taking in to account
 * screen-edge proximity or similar constraints. In addition, the server can use
 * the edge affinity hint to consider only horizontal or only vertical adjacency
 * edges in the given rectangle.
 */

TEST_F(PopupWindowPlacement, given_aux_rect_away_from_right_side_edge_attachment_vertical_attaches_to_right_edge)
{
    modification = edge_attachment(rectangle_away_from_rhs, mir_edge_attachment_vertical);

    auto const expected_position = on_right_edge();

    EXPECT_CALL(*window_manager_policy, advise_move_to(_, expected_position));
    basic_window_manager.modify_window(basic_window_manager.info_for(child), modification);
    ASSERT_THAT(child.top_left(), Eq(expected_position));
}

TEST_F(PopupWindowPlacement, given_aux_rect_near_right_side_edge_attachment_vertical_attaches_to_left_edge)
{
    modification = edge_attachment(rectangle_near_rhs, mir_edge_attachment_vertical);

    auto const expected_position = on_left_edge();

    EXPECT_CALL(*window_manager_policy, advise_move_to(_, expected_position));
    basic_window_manager.modify_window(basic_window_manager.info_for(child), modification);
    ASSERT_THAT(child.top_left(), Eq(expected_position));
}

TEST_F(PopupWindowPlacement, given_aux_rect_near_both_sides_edge_attachment_vertical_attaches_to_right_edge)
{
    modification = edge_attachment(rectangle_near_both_sides, mir_edge_attachment_vertical);

    auto const expected_position = on_right_edge();

    EXPECT_CALL(*window_manager_policy, advise_move_to(_, expected_position));
    basic_window_manager.modify_window(basic_window_manager.info_for(child), modification);
    ASSERT_THAT(child.top_left(), Eq(expected_position));
}

TEST_F(PopupWindowPlacement, given_aux_rect_away_from_bottom_edge_attachment_horizontal_attaches_to_bottom_edge)
{
    modification = edge_attachment(rectangle_away_from_bottom, mir_edge_attachment_horizontal);

    auto const expected_position = on_bottom_edge();

    EXPECT_CALL(*window_manager_policy, advise_move_to(_, expected_position));
    basic_window_manager.modify_window(basic_window_manager.info_for(child), modification);
    ASSERT_THAT(child.top_left(), Eq(expected_position));
}

TEST_F(PopupWindowPlacement, given_aux_rect_near_bottom_edge_attachment_horizontal_attaches_to_top_edge)
{
    modification = edge_attachment(rectangle_near_bottom, mir_edge_attachment_horizontal);

    auto const expected_position = on_top_edge();

    EXPECT_CALL(*window_manager_policy, advise_move_to(_, expected_position));
    basic_window_manager.modify_window(basic_window_manager.info_for(child), modification);
    ASSERT_THAT(child.top_left(), Eq(expected_position));
}

TEST_F(PopupWindowPlacement, given_aux_rect_near_both_sides_edge_attachment_any_attaches_to_bottom_edge)
{
    modification = edge_attachment(rectangle_near_both_sides, mir_edge_attachment_any);

    auto const expected_position = on_bottom_edge();

    EXPECT_CALL(*window_manager_policy, advise_move_to(_, expected_position));
    basic_window_manager.modify_window(basic_window_manager.info_for(child), modification);
    ASSERT_THAT(child.top_left(), Eq(expected_position));
}

TEST_F(PopupWindowPlacement, given_aux_rect_near_both_sides_and_bottom_edge_attachment_any_attaches_to_top_edge)
{
    modification = edge_attachment(rectangle_near_both_sides_and_bottom, mir_edge_attachment_any);

    auto const expected_position = on_top_edge();

    EXPECT_CALL(*window_manager_policy, advise_move_to(_, expected_position));
    basic_window_manager.modify_window(basic_window_manager.info_for(child), modification);
    ASSERT_THAT(child.top_left(), Eq(expected_position));
}

TEST_F(PopupWindowPlacement, given_aux_rect_near_all_sides_attachment_any_attaches_to_right_edge)
{
    modification = edge_attachment(rectangle_near_all_sides, mir_edge_attachment_any);

    auto const expected_position = on_right_edge();

    EXPECT_CALL(*window_manager_policy, advise_move_to(_, expected_position));
    basic_window_manager.modify_window(basic_window_manager.info_for(child), modification);
    ASSERT_THAT(child.top_left(), Eq(expected_position));
}

namespace
{
MirPlacementGravity const all_gravities[] =
{
    mir_placement_gravity_northwest,
    mir_placement_gravity_north,
    mir_placement_gravity_northeast,
    mir_placement_gravity_west,
    mir_placement_gravity_center,
    mir_placement_gravity_east,
    mir_placement_gravity_southwest,
    mir_placement_gravity_south,
    mir_placement_gravity_southeast,
};

auto position_of(MirPlacementGravity rect_gravity, Rectangle rectangle) -> Point
{
    auto const displacement = as_displacement(rectangle.size);

    switch (rect_gravity)
    {
    case mir_placement_gravity_northwest:
        return rectangle.top_left;

    case mir_placement_gravity_north:
        return rectangle.top_left + Displacement{0.5 * displacement.dx, 0};

    case mir_placement_gravity_northeast:
        return rectangle.top_right();

    case mir_placement_gravity_west:
        return rectangle.top_left + Displacement{0, 0.5 * displacement.dy};

    case mir_placement_gravity_center:
        return rectangle.top_left + 0.5 * displacement;

    case mir_placement_gravity_east:
        return rectangle.top_right() + Displacement{0, 0.5 * displacement.dy};

    case mir_placement_gravity_southwest:
        return rectangle.bottom_left();

    case mir_placement_gravity_south:
        return rectangle.bottom_left() + Displacement{0.5 * displacement.dx, 0};

    case mir_placement_gravity_southeast:
        return rectangle.bottom_right();

    default:
        throw std::runtime_error("bad placement gravity");
    }

}
}

TEST_F(PopupWindowPlacement, given_no_hints_can_attach_by_every_gravity)
{
    modification.aux_rect() = Rectangle{{100, 50}, { 20, 20}};
    modification.placement_hints() = MirPlacementHints{};

    for (auto const rect_gravity : all_gravities)
    {
        modification.aux_rect_placement_gravity() = rect_gravity;

        auto const rect_anchor = position_of(rect_gravity, aux_rect_position());

        for (auto const window_gravity : all_gravities)
        {
            modification.window_placement_gravity() = window_gravity;

            EXPECT_CALL(*window_manager_policy, advise_move_to(_, _));
            basic_window_manager.modify_window(basic_window_manager.info_for(child), modification);

            Rectangle child_rect{child.top_left(), child.size()};

            EXPECT_THAT(position_of(window_gravity, child_rect), Eq(rect_anchor))
                        << "rect_gravity=" << rect_gravity << ", window_gravity=" << window_gravity;
            Mock::VerifyAndClearExpectations(window_manager_policy);
        }
    }
}

TEST_F(PopupWindowPlacement, given_no_hints_can_attach_by_offset_at_every_gravity)
{
    auto const offset = Displacement{42, 13};

    modification.aux_rect() = Rectangle{{100, 50}, { 20, 20}};
    modification.placement_hints() = MirPlacementHints{};
    modification.aux_rect_placement_offset() = offset;

    for (auto const rect_gravity : all_gravities)
    {
        modification.aux_rect_placement_gravity() = rect_gravity;

        auto const rect_anchor = position_of(rect_gravity, aux_rect_position()) + offset;

        for (auto const window_gravity : all_gravities)
        {
            modification.window_placement_gravity() = window_gravity;

            EXPECT_CALL(*window_manager_policy, advise_move_to(_, _));
            basic_window_manager.modify_window(basic_window_manager.info_for(child), modification);

            Rectangle child_rect{child.top_left(), child.size()};

            EXPECT_THAT(position_of(window_gravity, child_rect), Eq(rect_anchor))
                        << "rect_gravity=" << rect_gravity << ", window_gravity=" << window_gravity;
            Mock::VerifyAndClearExpectations(window_manager_policy);
        }
    }
}

TEST_F(PopupWindowPlacement, given_aux_rect_near_right_side_and_offset_placement_is_flipped)
{
    DeltaX const x_offset{42};
    DeltaY const y_offset{13};

    modification.aux_rect() = rectangle_near_rhs;
    modification.placement_hints() = mir_placement_hints_flip_x;
    modification.aux_rect_placement_offset() = Displacement{x_offset, y_offset};
    modification.window_placement_gravity() = mir_placement_gravity_northwest;
    modification.aux_rect_placement_gravity() = mir_placement_gravity_northeast;

    auto const expected_position = on_left_edge() + Displacement{-1*x_offset, y_offset};

    EXPECT_CALL(*window_manager_policy, advise_move_to(_, expected_position));
    basic_window_manager.modify_window(basic_window_manager.info_for(child), modification);
    ASSERT_THAT(child.top_left(), Eq(expected_position));
}

TEST_F(PopupWindowPlacement, given_aux_rect_near_bottom_and_offset_placement_is_flipped)
{
    DeltaX const x_offset{42};
    DeltaY const y_offset{13};

    modification.aux_rect() = rectangle_near_bottom;
    modification.placement_hints() = mir_placement_hints_flip_y;
    modification.aux_rect_placement_offset() = Displacement{x_offset, y_offset};
    modification.window_placement_gravity() = mir_placement_gravity_northwest;
    modification.aux_rect_placement_gravity() = mir_placement_gravity_southwest;

    auto const expected_position = on_top_edge() + Displacement{x_offset, -1*y_offset};

    EXPECT_CALL(*window_manager_policy, advise_move_to(_, expected_position));
    basic_window_manager.modify_window(basic_window_manager.info_for(child), modification);
    ASSERT_THAT(child.top_left(), Eq(expected_position));
}

TEST_F(PopupWindowPlacement, given_aux_rect_near_bottom_right_and_offset_placement_is_flipped_both_ways)
{
    Displacement const displacement{42, 13};

    modification.aux_rect() = rectangle_near_both_bottom_right;
    modification.placement_hints() = mir_placement_hints_flip_any;
    modification.aux_rect_placement_offset() = displacement;
    modification.window_placement_gravity() = mir_placement_gravity_northwest;
    modification.aux_rect_placement_gravity() = mir_placement_gravity_southeast;

    auto const expected_position = aux_rect_position().top_left - as_displacement(child.size()) - displacement;

    EXPECT_CALL(*window_manager_policy, advise_move_to(_, expected_position));
    basic_window_manager.modify_window(basic_window_manager.info_for(child), modification);
    ASSERT_THAT(child.top_left(), Eq(expected_position));
}

TEST_F(PopupWindowPlacement, given_aux_rect_near_right_side_placement_can_slide_in_x)
{
    modification.aux_rect() = rectangle_near_rhs;
    modification.placement_hints() = mir_placement_hints_slide_x;
    modification.window_placement_gravity() = mir_placement_gravity_northwest;
    modification.aux_rect_placement_gravity() = mir_placement_gravity_northeast;

    Point const expected_position{display_area.top_right().x - as_displacement(child.size()).dx, aux_rect_position().top_left.y};

    EXPECT_CALL(*window_manager_policy, advise_move_to(_, expected_position));
    basic_window_manager.modify_window(basic_window_manager.info_for(child), modification);
    ASSERT_THAT(child.top_left(), Eq(expected_position));
}

TEST_F(PopupWindowPlacement, given_aux_rect_near_left_side_placement_can_slide_in_x)
{
    Rectangle const rectangle_near_left_side{{0, 20}, {20, 20}};

    modification.aux_rect() = rectangle_near_left_side;
    modification.placement_hints() = mir_placement_hints_slide_x;
    modification.window_placement_gravity() = mir_placement_gravity_northeast;
    modification.aux_rect_placement_gravity() = mir_placement_gravity_northwest;

    Point const expected_position{display_area.top_left.x, aux_rect_position().top_left.y};

    EXPECT_CALL(*window_manager_policy, advise_move_to(_, expected_position));
    basic_window_manager.modify_window(basic_window_manager.info_for(child), modification);
    ASSERT_THAT(child.top_left(), Eq(expected_position));
}

TEST_F(PopupWindowPlacement, given_aux_rect_near_bottom_placement_can_slide_in_y)
{
    modification.aux_rect() = rectangle_near_bottom;
    modification.placement_hints() = mir_placement_hints_slide_y;
    modification.window_placement_gravity() = mir_placement_gravity_northwest;
    modification.aux_rect_placement_gravity() = mir_placement_gravity_southwest;

    Point const expected_position{
        aux_rect_position().top_left.x,
        (display_area.bottom_left() - as_displacement(child.size())).y};

    EXPECT_CALL(*window_manager_policy, advise_move_to(_, expected_position));
    basic_window_manager.modify_window(basic_window_manager.info_for(child), modification);
    ASSERT_THAT(child.top_left(), Eq(expected_position));
}

TEST_F(PopupWindowPlacement, given_aux_rect_near_top_placement_can_slide_in_y)
{
    modification.aux_rect() = rectangle_near_all_sides;
    modification.placement_hints() = mir_placement_hints_slide_y;
    modification.window_placement_gravity() = mir_placement_gravity_southwest;
    modification.aux_rect_placement_gravity() = mir_placement_gravity_northwest;

    Point const expected_position{aux_rect_position().top_left.x, display_area.top_left.y};

    EXPECT_CALL(*window_manager_policy, advise_move_to(_, expected_position));
    basic_window_manager.modify_window(basic_window_manager.info_for(child), modification);
    ASSERT_THAT(child.top_left(), Eq(expected_position));
}

TEST_F(PopupWindowPlacement, given_aux_rect_near_bottom_right_and_offset_placement_can_slide_in_x_and_y)
{
    modification.aux_rect() = rectangle_near_both_bottom_right;
    modification.placement_hints() = mir_placement_hints_slide_any;
    modification.window_placement_gravity() = mir_placement_gravity_northwest;
    modification.aux_rect_placement_gravity() = mir_placement_gravity_southwest;

    auto const expected_position = display_area.bottom_right() - as_displacement(child.size());

    EXPECT_CALL(*window_manager_policy, advise_move_to(_, expected_position));
    basic_window_manager.modify_window(basic_window_manager.info_for(child), modification);
    ASSERT_THAT(child.top_left(), Eq(expected_position));
}

TEST_F(PopupWindowPlacement, given_aux_rect_near_right_side_placement_can_resize_in_x)
{
    modification.aux_rect() = rectangle_near_rhs;
    modification.placement_hints() = mir_placement_hints_resize_x;
    modification.window_placement_gravity() = mir_placement_gravity_northwest;
    modification.aux_rect_placement_gravity() = mir_placement_gravity_northeast;

    auto const expected_position = aux_rect_position().top_right();
    Size const expected_size{as_size(display_area.bottom_right()-aux_rect_position().bottom_right()).width, child.size().height};

    EXPECT_CALL(*window_manager_policy, advise_move_to(_, expected_position));
    EXPECT_CALL(*window_manager_policy, advise_resize(_, expected_size));
    basic_window_manager.modify_window(basic_window_manager.info_for(child), modification);
    ASSERT_THAT(child.top_left(), Eq(expected_position));
    ASSERT_THAT(child.size(), Eq(expected_size));
}

TEST_F(PopupWindowPlacement, given_aux_rect_near_left_side_placement_can_resize_in_x)
{
    Rectangle const rectangle_near_left_side{{0, 20}, {20, 20}};

    modification.aux_rect() = rectangle_near_left_side;
    modification.placement_hints() = mir_placement_hints_resize_x;
    modification.window_placement_gravity() = mir_placement_gravity_northeast;
    modification.aux_rect_placement_gravity() = mir_placement_gravity_northwest;

    Point const expected_position{display_area.top_left.x, aux_rect_position().top_left.y};
    Size const expected_size{as_size(aux_rect_position().top_left-display_area.top_left).width, child.size().height};

    EXPECT_CALL(*window_manager_policy, advise_move_to(_, expected_position));
    EXPECT_CALL(*window_manager_policy, advise_resize(_, expected_size));
    basic_window_manager.modify_window(basic_window_manager.info_for(child), modification);
    ASSERT_THAT(child.top_left(), Eq(expected_position));
    ASSERT_THAT(child.size(), Eq(expected_size));
}

TEST_F(PopupWindowPlacement, given_aux_rect_near_bottom_placement_can_resize_in_y)
{
    modification.aux_rect() = rectangle_near_bottom;
    modification.placement_hints() = mir_placement_hints_resize_y;
    modification.window_placement_gravity() = mir_placement_gravity_northwest;
    modification.aux_rect_placement_gravity() = mir_placement_gravity_southwest;

    auto const expected_position = aux_rect_position().bottom_left();
    Size const expected_size{child.size().width, as_size(display_area.bottom_left()-aux_rect_position().bottom_left()).height};

    EXPECT_CALL(*window_manager_policy, advise_move_to(_, expected_position));
    EXPECT_CALL(*window_manager_policy, advise_resize(_, expected_size));
    basic_window_manager.modify_window(basic_window_manager.info_for(child), modification);
    ASSERT_THAT(child.top_left(), Eq(expected_position));
    ASSERT_THAT(child.size(), Eq(expected_size));
}

TEST_F(PopupWindowPlacement, given_aux_rect_near_top_placement_can_resize_in_y)
{
    modification.aux_rect() = rectangle_near_all_sides;
    modification.placement_hints() = mir_placement_hints_resize_y;
    modification.window_placement_gravity() = mir_placement_gravity_southwest;
    modification.aux_rect_placement_gravity() = mir_placement_gravity_northwest;

    Point const expected_position{aux_rect_position().top_left.x, display_area.top_left.y};
    Size const expected_size{child.size().width, as_size(aux_rect_position().top_left-display_area.top_left).height};

    EXPECT_CALL(*window_manager_policy, advise_move_to(_, expected_position));
    EXPECT_CALL(*window_manager_policy, advise_resize(_, expected_size));
    basic_window_manager.modify_window(basic_window_manager.info_for(child), modification);
    ASSERT_THAT(child.top_left(), Eq(expected_position));
    ASSERT_THAT(child.size(), Eq(expected_size));
}

TEST_F(PopupWindowPlacement, given_aux_rect_near_bottom_right_and_offset_placement_can_resize_in_x_and_y)
{
    modification.aux_rect() = rectangle_near_both_bottom_right;
    modification.placement_hints() = mir_placement_hints_resize_any;
    modification.window_placement_gravity() = mir_placement_gravity_northwest;
    modification.aux_rect_placement_gravity() = mir_placement_gravity_southeast;

    auto const expected_position = aux_rect_position().bottom_right();
    auto const expected_size = as_size(display_area.bottom_right()-expected_position);

    EXPECT_CALL(*window_manager_policy, advise_move_to(_, expected_position));
    EXPECT_CALL(*window_manager_policy, advise_resize(_, expected_size));
    basic_window_manager.modify_window(basic_window_manager.info_for(child), modification);
    ASSERT_THAT(child.top_left(), Eq(expected_position));
    ASSERT_THAT(child.size(), Eq(expected_size));
}

TEST_F(PopupWindowPlacement, correctly_placed_when_surface_has_content_offset)
{
    Displacement const content_offset{20, 25};
    std::shared_ptr<mir::scene::Surface> const parent_scene_surface{parent};
    parent_scene_surface->set_window_margins(content_offset.dy, content_offset.dx, DeltaY{10}, DeltaX{35});

    Point const aux_rect_top_left{100, 50};
    modification.aux_rect() = Rectangle{aux_rect_top_left, {20, 20}};
    modification.placement_hints() = MirPlacementHints{};

    for (auto const rect_gravity : all_gravities)
    {
        modification.aux_rect_placement_gravity() = rect_gravity;

        ASSERT_THAT(
            aux_rect_position().top_left,
            Eq(aux_rect_top_left + as_displacement(parent.top_left()) + content_offset))
                << "Test did not correctly account for content offset";

        auto const rect_anchor = position_of(rect_gravity, aux_rect_position());

        for (auto const window_gravity : all_gravities)
        {
            modification.window_placement_gravity() = window_gravity;

            EXPECT_CALL(*window_manager_policy, advise_move_to(_, _));
            basic_window_manager.modify_window(basic_window_manager.info_for(child), modification);

            Rectangle child_rect{child.top_left(), child.size()};

            EXPECT_THAT(position_of(window_gravity, child_rect), Eq(rect_anchor))
                        << "rect_gravity=" << rect_gravity << ", window_gravity=" << window_gravity;
            Mock::VerifyAndClearExpectations(window_manager_policy);
        }
    }
}