File: windowmodel_test.cpp

package info (click to toggle)
qtmir 0.8.0~git20230223.bd21224-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 4,568 kB
  • sloc: cpp: 22,847; python: 304; xml: 271; ansic: 87; makefile: 20; sh: 12
file content (790 lines) | stat: -rw-r--r-- 24,987 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
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
/*
 * Copyright (C) 2016 Canonical Ltd.
 *
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU Lesser 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 warranties of MERCHANTABILITY,
 * SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "qtmir_test.h"

#include <QLoggingCategory>
#include <QTest>
#include <QSignalSpy>

#include "mirqtconversion.h"
#include "windowmodelnotifier.h"
#include "QtMir/Application/mirsurface.h"
#include "QtMir/Application/windowmodel.h"

#include <mir/test/doubles/stub_surface.h>
#include <mir/test/doubles/stub_session.h>

#include <mir/scene/surface_creation_parameters.h>

using namespace qtmir;

namespace ms = mir::scene;
namespace mg = mir::graphics;
using StubSession = mir::test::doubles::StubSession;
using namespace testing;

struct SizedStubSurface : public mir::test::doubles::StubSurface
{
#if MIR_SERVER_VERSION >= MIR_VERSION_NUMBER(1, 6, 0)
    mir::geometry::Size window_size() const override { return toMirSize(m_size); }
#else
    mir::geometry::Size size() const override { return toMirSize(m_size); }
#endif

    void setSize(QSize size) { m_size = size; }

private:
    QSize m_size;
};


class WindowModelTest : public ::testing::Test
{
public:
    WindowModelTest()
    {
        // We don't want the logging spam cluttering the test results
        QLoggingCategory::setFilterRules(QStringLiteral("qtmir.surfaces=false"));
    }

    NewWindow createNewWindow(QPoint position = {160, 320}, QSize size = {100, 200})
    {
        const miral::Application app{stubSession};
        stubSurface->setSize(size);
        const miral::Window window{app, stubSurface};

        ms::SurfaceCreationParameters windowSpec;
//        windowSpec.of_size(toMirSize(size)); // useless, Window/Surface has the size actually used
        windowSpec.of_position(toMirPoint(position));
        miral::WindowInfo windowInfo{window, windowSpec};
        return NewWindow{windowInfo};
    }

    NewWindow createNewWindowForInputMethod()
    {
        const miral::Application app{stubSession};
        const miral::Window window{app, stubSurface};

        ms::SurfaceCreationParameters windowSpec;
        windowSpec.of_type(mir_window_type_inputmethod);
        miral::WindowInfo windowInfo{window, windowSpec};
        return NewWindow{windowInfo};
    }

    NewWindow createNewWindowWithState(Mir::State state)
    {
        const miral::Application app{stubSession};
        const miral::Window window{app, stubSurface};

        ms::SurfaceCreationParameters windowSpec;
        windowSpec.with_state(qtmir::toMirState(state));
        miral::WindowInfo windowInfo{window, windowSpec};
        return NewWindow{windowInfo};
    }

    qtmir::MirSurface *getMirSurfaceFromModel(const WindowModel &model, int index)
    {
        flushEvents();
        return model.data(model.index(index, 0), WindowModel::SurfaceRole).value<qtmir::MirSurface*>();
    }

    miral::Window getMirALWindowFromModel(const WindowModel &model, int index)
    {
        return getMirSurfaceFromModel(model, index)->window();
    }

    void SetUp() override
    {
        int argc = 0;
        char* argv[0];
        qtApp = new QCoreApplication(argc, argv); // needed for event loop
    }

    void TearDown() override
    {
        delete qtApp;
    }

    void flushEvents()
    {
        qtApp->sendPostedEvents();
    }

    const std::shared_ptr<StubSession> stubSession{std::make_shared<StubSession>()};
    const std::shared_ptr<SizedStubSurface> stubSurface{std::make_shared<SizedStubSurface>()};
    QCoreApplication *qtApp;
};

/*
 * Test: that the WindowModelNotifier.windowAdded causes the Qt-side WindowModel to
 * increment model count
 */
TEST_F(WindowModelTest, WhenAddWindowNotifiedModelCountIncrements)
{
    WindowModelNotifier notifier;
    WindowModel model(&notifier, nullptr); // no need for controller in this testcase

    auto newWindow = createNewWindow();

    notifier.windowAdded(newWindow);
    flushEvents();

    EXPECT_EQ(1, model.count());
}

/*
 * Test: that the WindowModelNotifier.windowAdded causes the Qt-side WindowModel to
 * emit the countChanged signal.
 */
TEST_F(WindowModelTest, WhenAddWindowNotifiedModelEmitsCountChangedSignal)
{
    WindowModelNotifier notifier;
    WindowModel model(&notifier, nullptr); // no need for controller in this testcase

    auto newWindow = createNewWindow();

    QSignalSpy spyCountChanged(&model, SIGNAL(countChanged()));

    notifier.windowAdded(newWindow);
    flushEvents();

    EXPECT_EQ(1, spyCountChanged.count());
}

/*
 * Test: that the WindowModelNotifier.windowAdded causes the Qt-side WindowModel to
 * gain an entry which has the correct miral::Window
 */
TEST_F(WindowModelTest, WhenAddWindowNotifiedNewModelEntryHasCorrectWindow)
{
    WindowModelNotifier notifier;
    WindowModel model(&notifier, nullptr); // no need for controller in this testcase

    auto newWindow = createNewWindow();

    notifier.windowAdded(newWindow);
    flushEvents();

    auto miralWindow = getMirALWindowFromModel(model, 0);
    EXPECT_EQ(newWindow.windowInfo.window(), miralWindow);
}

/*
 * Test: that the WindowModelNotifier.windowRemoved causes the Qt-side WindowModel to
 * remove the Window from the model, and emit the countChanged signal.
 */
TEST_F(WindowModelTest, WhenRemoveWindowNotifiedModelCountDecrements)
{
    WindowModelNotifier notifier;
    WindowModel model(&notifier, nullptr); // no need for controller in this testcase

    auto newWindow = createNewWindow();
    notifier.windowAdded(newWindow);

    // Test removing the window
    notifier.windowRemoved(newWindow.windowInfo);
    flushEvents();

    EXPECT_EQ(0, model.count());
}

/*
 * Test: that the WindowModelNotifier.windowRemoved causes the Qt-side WindowModel to
 * emit the countChanged signal.
 */
TEST_F(WindowModelTest, WhenRemoveWindowNotifiedModelEmitsCountChangedSignal)
{
    WindowModelNotifier notifier;
    WindowModel model(&notifier, nullptr); // no need for controller in this testcase

    auto newWindow = createNewWindow();
    notifier.windowAdded(newWindow);
    flushEvents();

    // Test removing the window
    QSignalSpy spyCountChanged(&model, SIGNAL(countChanged()));

    notifier.windowRemoved(newWindow.windowInfo);
    flushEvents();

    EXPECT_EQ(1, spyCountChanged.count());
}

/*
 * Test: that calling WindowModelNotifier.windowAdded causes Qt-side WindowModel to
 * have 2 windows in the correct order.
 */
TEST_F(WindowModelTest, WhenAddingTwoWindowsModelHasCorrectOrder)
{
    WindowModelNotifier notifier;
    WindowModel model(&notifier, nullptr); // no need for controller in this testcase

    auto newWindow1 = createNewWindow();
    auto newWindow2 = createNewWindow();

    notifier.windowAdded(newWindow1);
    notifier.windowAdded(newWindow2);
    flushEvents();

    ASSERT_EQ(2, model.count());
    auto miralWindow1 = getMirALWindowFromModel(model, 0);
    EXPECT_EQ(newWindow1.windowInfo.window(), miralWindow1);
    auto miralWindow2 = getMirALWindowFromModel(model, 1);
    EXPECT_EQ(newWindow2.windowInfo.window(), miralWindow2);
}

/*
 * Test: that adding 2 windows, then removing the second, leaves the first.
 */
TEST_F(WindowModelTest, WhenAddingTwoWindowsAndRemoveSecondModelPreservesFirst)
{
    WindowModelNotifier notifier;
    WindowModel model(&notifier, nullptr); // no need for controller in this testcase

    auto newWindow1 = createNewWindow();
    auto newWindow2 = createNewWindow();

    notifier.windowAdded(newWindow1);
    notifier.windowAdded(newWindow2);

    // Remove second window
    notifier.windowRemoved(newWindow2.windowInfo);
    flushEvents();

    ASSERT_EQ(1, model.count());
    auto miralWindow = getMirALWindowFromModel(model, 0);
    EXPECT_EQ(newWindow1.windowInfo.window(), miralWindow);
}

/*
 * Test: that adding 2 windows, then removing the first, leaves the second.
 */
TEST_F(WindowModelTest, WhenAddingTwoWindowsAndRemoveFirstModelPreservesSecond)
{
    WindowModelNotifier notifier;
    WindowModel model(&notifier, nullptr); // no need for controller in this testcase

    auto newWindow1 = createNewWindow();
    auto newWindow2 = createNewWindow();

    notifier.windowAdded(newWindow1);
    notifier.windowAdded(newWindow2);

    // Remove first window
    notifier.windowRemoved(newWindow1.windowInfo);
    flushEvents();

    ASSERT_EQ(1, model.count());
    auto miralWindow = getMirALWindowFromModel(model, 0);
    EXPECT_EQ(newWindow2.windowInfo.window(), miralWindow);
}

/*
 * Test: add 2 windows, remove first, add another window - ensure model order correct
 */
TEST_F(WindowModelTest, WhenAddingTwoWindowsRemoveFirstAddAnotherResultsInCorrectModel)
{
    WindowModelNotifier notifier;
    WindowModel model(&notifier, nullptr); // no need for controller in this testcase

    auto newWindow1 = createNewWindow();
    auto newWindow2 = createNewWindow();
    auto newWindow3 = createNewWindow();

    notifier.windowAdded(newWindow1);
    notifier.windowAdded(newWindow2);
    notifier.windowRemoved(newWindow1.windowInfo);

    notifier.windowAdded(newWindow3);
    flushEvents();

    ASSERT_EQ(2, model.count());
    auto miralWindow2 = getMirALWindowFromModel(model, 0);
    EXPECT_EQ(newWindow2.windowInfo.window(), miralWindow2);
    auto miralWindow3 = getMirALWindowFromModel(model, 1);
    EXPECT_EQ(newWindow3.windowInfo.window(), miralWindow3);
}

/*
 * Test: add 3 windows, remove second - ensure model order correct
 */
TEST_F(WindowModelTest, WhenAddingThreeWindowsRemoveSecondResultsInCorrectModel)
{
    WindowModelNotifier notifier;
    WindowModel model(&notifier, nullptr); // no need for controller in this testcase

    auto newWindow1 = createNewWindow();
    auto newWindow2 = createNewWindow();
    auto newWindow3 = createNewWindow();

    notifier.windowAdded(newWindow1);
    notifier.windowAdded(newWindow2);
    notifier.windowAdded(newWindow3);

    notifier.windowRemoved(newWindow2.windowInfo);
    flushEvents();

    ASSERT_EQ(2, model.count());
    auto miralWindow1 = getMirALWindowFromModel(model, 0);
    EXPECT_EQ(newWindow1.windowInfo.window(), miralWindow1);
    auto miralWindow3 = getMirALWindowFromModel(model, 1);
    EXPECT_EQ(newWindow3.windowInfo.window(), miralWindow3);
}

/*
 * Test: with 1 window, raise does nothing
 */
TEST_F(WindowModelTest, RaisingOneWindowDoesNothing)
{
    WindowModelNotifier notifier;
    WindowModel model(&notifier, nullptr); // no need for controller in this testcase

    auto newWindow1 = createNewWindow();
    notifier.windowAdded(newWindow1);

    // Raise first window
    notifier.windowsRaised({newWindow1.windowInfo.window()});
    flushEvents();

    ASSERT_EQ(1, model.count());
    auto topWindow = getMirALWindowFromModel(model, 0);
    EXPECT_EQ(newWindow1.windowInfo.window(), topWindow);
}

/*
 * Test: with 2 window, raising top window does nothing
 */
TEST_F(WindowModelTest, RaisingTopWindowDoesNothing)
{
    WindowModelNotifier notifier;
    WindowModel model(&notifier, nullptr); // no need for controller in this testcase

    auto newWindow1 = createNewWindow();
    auto newWindow2 = createNewWindow();
    notifier.windowAdded(newWindow1);
    notifier.windowAdded(newWindow2);

    // Raise second window (currently on top)
    notifier.windowsRaised({newWindow2.windowInfo.window()});
    flushEvents();

    // Check second window still on top
    ASSERT_EQ(2, model.count());
    auto topWindow = getMirALWindowFromModel(model, 1);
    EXPECT_EQ(newWindow2.windowInfo.window(), topWindow);
}

/*
 * Test: with 2 window, raising bottom window brings it to the top
 */
TEST_F(WindowModelTest, RaisingBottomWindowBringsItToTheTop)
{
    WindowModelNotifier notifier;
    WindowModel model(&notifier, nullptr); // no need for controller in this testcase

    auto newWindow1 = createNewWindow();
    auto newWindow2 = createNewWindow();
    notifier.windowAdded(newWindow1);
    notifier.windowAdded(newWindow2);

    // Raise first window (currently at bottom)
    notifier.windowsRaised({newWindow1.windowInfo.window()});
    flushEvents();

    // Check first window now on top
    ASSERT_EQ(2, model.count());
    auto topWindow = getMirALWindowFromModel(model, 1);
    EXPECT_EQ(newWindow1.windowInfo.window(), topWindow);
}

/*
 * Test: with 3 windows, raising bottom 2 windows brings them to the top in order
 */
TEST_F(WindowModelTest, Raising2BottomWindowsBringsThemToTheTop)
{
    WindowModelNotifier notifier;
    WindowModel model(&notifier, nullptr); // no need for controller in this testcase

    auto newWindow1 = createNewWindow();
    auto newWindow2 = createNewWindow();
    auto newWindow3 = createNewWindow();
    notifier.windowAdded(newWindow1);
    notifier.windowAdded(newWindow2);
    notifier.windowAdded(newWindow3);

    // Current model state
    // 2:   Window3
    // 1:   Window2
    // 0:   Window1

    // Raise windows 1 & 2 (currently at bottom) so that window 1 is top
    notifier.windowsRaised({newWindow2.windowInfo.window(), newWindow1.windowInfo.window()});

    // Model should now be like this:
    // 2:   Window1
    // 1:   Window2
    // 0:   Window3
    flushEvents();

    ASSERT_EQ(3, model.count());
    auto topWindow = getMirALWindowFromModel(model, 2);
    EXPECT_EQ(newWindow1.windowInfo.window(), topWindow);
    auto middleWindow = getMirALWindowFromModel(model, 1);
    EXPECT_EQ(newWindow2.windowInfo.window(), middleWindow);
    auto bottomWindow = getMirALWindowFromModel(model, 0);
    EXPECT_EQ(newWindow3.windowInfo.window(), bottomWindow);
}

/*
 * Test: with 2 window, raise the 2 windows in swapped order reorders the model
 */
TEST_F(WindowModelTest, Raising2WindowsInSwappedOrderReordersTheModel)
{
    WindowModelNotifier notifier;
    WindowModel model(&notifier, nullptr); // no need for controller in this testcase

    auto newWindow1 = createNewWindow();
    auto newWindow2 = createNewWindow();
    notifier.windowAdded(newWindow1);
    notifier.windowAdded(newWindow2);

    // Current model state
    // 1:   Window2
    // 0:   Window1

    // Raise windows 1 & 2 (i.e. flip the order) - 1 should be on top
    notifier.windowsRaised({newWindow2.windowInfo.window(), newWindow1.windowInfo.window()});

    // Model should now be like this:
    // 1:   Window1
    // 0:   Window2
    flushEvents();

    ASSERT_EQ(2, model.count());
    auto topWindow = getMirALWindowFromModel(model, 1);
    EXPECT_EQ(newWindow1.windowInfo.window(), topWindow);
    auto bottomWindow = getMirALWindowFromModel(model, 0);
    EXPECT_EQ(newWindow2.windowInfo.window(), bottomWindow);
}

/*
 * Test: with 3 windows, raise the bottom 2 windows in swapped order reorders the model
 * so that the bottom window is at the top, and middle window remains in place.
 */
TEST_F(WindowModelTest, With3WindowsRaising2BottomWindowsInSwappedOrderReordersTheModel)
{
    WindowModelNotifier notifier;
    WindowModel model(&notifier, nullptr); // no need for controller in this testcase

    auto newWindow1 = createNewWindow();
    auto newWindow2 = createNewWindow();
    auto newWindow3 = createNewWindow();
    notifier.windowAdded(newWindow1);
    notifier.windowAdded(newWindow2);
    notifier.windowAdded(newWindow3);

    // Current model state
    // 2:   Window3
    // 1:   Window2
    // 0:   Window1

    // Raise windows 2 & 1 (i.e. bottom two, but in opposite order) so that window 2 is top
    notifier.windowsRaised({newWindow1.windowInfo.window(), newWindow2.windowInfo.window()});

    // Model should now be like this:
    // 2:   Window2
    // 1:   Window1
    // 0:   Window3
    flushEvents();

    ASSERT_EQ(3, model.count());
    auto topWindow = getMirALWindowFromModel(model, 2);
    EXPECT_EQ(newWindow2.windowInfo.window(), topWindow);
    auto middleWindow = getMirALWindowFromModel(model, 1);
    EXPECT_EQ(newWindow1.windowInfo.window(), middleWindow);
    auto bottomWindow = getMirALWindowFromModel(model, 0);
    EXPECT_EQ(newWindow3.windowInfo.window(), bottomWindow);
}

/*
 * Test: MirSurface has inital position set correctly from miral::WindowInfo
 */
TEST_F(WindowModelTest, DISABLED_MirSurfacePositionSetCorrectlyAtCreation)
{
    WindowModelNotifier notifier;
    WindowModel model(&notifier, nullptr); // no need for controller in this testcase

    QPoint position(100, 200);

    auto newWindow = createNewWindow(position);
    notifier.windowAdded(newWindow);
    flushEvents();

    auto surface = getMirSurfaceFromModel(model, 0);
    EXPECT_EQ(position, surface->position());
}

/*
 * Test: Mir moving a window updates MirSurface position
 */
TEST_F(WindowModelTest, WindowMoveUpdatesMirSurface)
{
    WindowModelNotifier notifier;
    WindowModel model(&notifier, nullptr); // no need for controller in this testcase

    QPoint oldPosition(100, 200),
           newPosition(150, 220);

    auto newWindow = createNewWindow(oldPosition);
    notifier.windowAdded(newWindow);

    auto surface = getMirSurfaceFromModel(model, 0);

    // Move window, check new position set
    notifier.windowMoved(newWindow.windowInfo, newPosition);
    flushEvents();

    EXPECT_EQ(newPosition, surface->position());
}

/*
 * Test: with 2 windows, ensure window move impacts the correct MirSurface
 */
TEST_F(WindowModelTest, WindowMoveUpdatesCorrectMirSurface)
{
    WindowModelNotifier notifier;
    WindowModel model(&notifier, nullptr); // no need for controller in this testcase

    QPoint oldPosition(100, 200),
           newPosition(150, 220);

    auto newWindow1 = createNewWindow(oldPosition);
    auto newWindow2 = createNewWindow(QPoint(300, 400));
    notifier.windowAdded(newWindow1);
    notifier.windowAdded(newWindow2);

    auto surface = getMirSurfaceFromModel(model, 0); // will be MirSurface for newWindow1

    // Move window, check new position set
    notifier.windowMoved(newWindow1.windowInfo, newPosition);
    flushEvents();

    EXPECT_EQ(newPosition, surface->position());
}

/*
 * Test: with 2 windows, ensure window move does not impact other MirSurfaces
 */
TEST_F(WindowModelTest, DISABLED_WindowMoveDoesNotTouchOtherMirSurfaces)
{
    WindowModelNotifier notifier;
    WindowModel model(&notifier, nullptr); // no need for controller in this testcase

    QPoint fixedPosition(300, 400);

    auto newWindow1 = createNewWindow(QPoint(100, 200));
    auto newWindow2 = createNewWindow(fixedPosition);
    notifier.windowAdded(newWindow1);
    notifier.windowAdded(newWindow2);

    auto surface = getMirSurfaceFromModel(model, 1); // will be MirSurface for newWindow2

    // Move window, check new position set
    notifier.windowMoved(newWindow1.windowInfo, QPoint(350, 420));
    flushEvents();

    // Ensure other window untouched
    EXPECT_EQ(fixedPosition, surface->position());
}

/*
 * Test: MirSurface has inital size set correctly from miral::WindowInfo
 */
TEST_F(WindowModelTest, DISABLED_MirSurfaceSizeSetCorrectlyAtCreation)
{
    WindowModelNotifier notifier;
    WindowModel model(&notifier, nullptr); // no need for controller in this testcase

    QSize size(300, 200);

    auto newWindow1 = createNewWindow(QPoint(), size);
    notifier.windowAdded(newWindow1);
    flushEvents();

    auto surface = getMirSurfaceFromModel(model, 0);
    EXPECT_EQ(size, surface->size());
}

/*
 * Test: that the WindowModelNotifier.windowAdded for an Input Method Window causes
 * the Qt-side WindowModel to register the input method surface
 */
TEST_F(WindowModelTest, WhenAddInputMethodWindowNotifiedModelEmitsInputMethodChangedSignal)
{
    WindowModelNotifier notifier;
    WindowModel model(&notifier, nullptr); // no need for controller in this testcase

    auto newWindow = createNewWindowForInputMethod();

    QSignalSpy spyCountChanged(&model, SIGNAL(inputMethodSurfaceChanged(MirSurfaceInterface*)));

    notifier.windowAdded(newWindow);
    flushEvents();

    EXPECT_EQ(1, spyCountChanged.count());
}

/*
 * Test: that the WindowModelNotifier.windowAdded for an Input Method Window causes
 * the Qt-side WindowModel::inputMethodSurface property to be correctly set
 */
TEST_F(WindowModelTest, WhenAddInputMethodWindowNotifiedModelPropertyHasCorrectWindow)
{
    WindowModelNotifier notifier;
    WindowModel model(&notifier, nullptr); // no need for controller in this testcase

    auto newWindow = createNewWindowForInputMethod();

    notifier.windowAdded(newWindow);
    flushEvents();

    auto miralWindow = static_cast<qtmir::MirSurface*>(model.inputMethodSurface())->window();
    EXPECT_EQ(newWindow.windowInfo.window(), miralWindow);
}

/*
 * Test: that the WindowModelNotifier.windowRemoved for an Input Method Window causes
 * the Qt-side WindowModel to reset the WindowModel::inputMethodSurface property to null
 */
TEST_F(WindowModelTest, WhenRemoveInputMethodWindowNotifiedModelPropertyReset)
{
    WindowModelNotifier notifier;
    WindowModel model(&notifier, nullptr); // no need for controller in this testcase

    auto newWindow = createNewWindowForInputMethod();
    notifier.windowAdded(newWindow);

    // Test removing the window
    notifier.windowRemoved(newWindow.windowInfo);
    flushEvents();

    EXPECT_EQ(nullptr, model.inputMethodSurface());
}

/*
 * Test: that the WindowModelNotifier.windowReady causes its associated MirSurface
 * to emit the ready signal.
 */
TEST_F(WindowModelTest, WindowReadyCausesMirSurfaceToEmitReadySignal)
{
    WindowModelNotifier notifier;
    WindowModel model(&notifier, nullptr); // no need for controller in this testcase

    auto newWindow = createNewWindow();
    notifier.windowAdded(newWindow);

    auto surface = getMirSurfaceFromModel(model, 0); // will be MirSurface for newWindow
    QSignalSpy readySpy(surface, &qtmir::MirSurface::ready);

    // Mark window ready
    notifier.windowReady(newWindow.windowInfo);
    flushEvents();

    EXPECT_EQ(1, readySpy.count());
}


class WindowModelTestTypes : public WindowModelTest, public ::testing::WithParamInterface<Mir::State>
{
public:
    NewWindow createNewWindowWithState(Mir::State state)
    {
        const miral::Application app{stubSession};
        const miral::Window window{app, stubSurface};

        ms::SurfaceCreationParameters windowSpec;
        windowSpec.with_state(qtmir::toMirState(state));
        miral::WindowInfo windowInfo{window, windowSpec};
        return NewWindow{windowInfo};
    }
};

/*
 * Test: that creating a Window with a particular state creates a MirSurface with the correct state.
 */
TEST_P(WindowModelTestTypes, WhenWindowCreatedMirSurfaceStateCorrect)
{
    auto const& param = GetParam();

    WindowModelNotifier notifier;
    WindowModel model(&notifier, nullptr); // no need for controller in this testcase

    auto newWindow = createNewWindowWithState(param);
    notifier.windowAdded(newWindow);

    auto surface = getMirSurfaceFromModel(model, 0);
    EXPECT_EQ(param, surface->state());
}

/*
 * Test: that WindowModelNotifier.windowStateChanged causes the Qt-side WindowModel to
 * update the MirSurface state correctly.
 */
TEST_P(WindowModelTestTypes, WhenWindowStateChangedMirSurfaceStateUpdated)
{
    auto const& param = GetParam();

    WindowModelNotifier notifier;
    WindowModel model(&notifier, nullptr); // no need for controller in this testcase

    auto newWindow = createNewWindowWithState(Mir::UnknownState);
    notifier.windowAdded(newWindow);

    // change the state
    notifier.windowStateChanged(newWindow.windowInfo, param);

    auto surface = getMirSurfaceFromModel(model, 0);
    EXPECT_EQ(param, surface->state());
}

/*
 * Test: that WindowModelNotifier.windowStateChanged causes the Qt-side MirSurface to
 * emit the stateChanged signal
 */
TEST_P(WindowModelTestTypes, WhenWindowStateChangedMirSurfaceEmitsStateChangedSignal)
{
    auto const& param = GetParam();

    WindowModelNotifier notifier;
    WindowModel model(&notifier, nullptr); // no need for controller in this testcase

    auto newWindow = createNewWindowWithState(Mir::UnknownState);
    notifier.windowAdded(newWindow);

    // Test removing the window
    QSignalSpy spyCountChanged(&model, SIGNAL(countChanged()));

    // change the state
    notifier.windowStateChanged(newWindow.windowInfo, param);
    flushEvents();

    EXPECT_EQ(1, spyCountChanged.count());
}

const Mir::State allKnownStates[] = {Mir::RestoredState, Mir::MinimizedState, Mir::MaximizedState, Mir::VertMaximizedState,
                                     Mir::FullscreenState, Mir::HorizMaximizedState, Mir::HiddenState};
INSTANTIATE_TEST_SUITE_P(WindowTypes, WindowModelTestTypes, ::testing::ValuesIn(allKnownStates));