~alan-griffiths/miral/fix-1621917

« back to all changes in this revision

Viewing changes to miral-qt/tests/modules/WindowManager/windowmodel_test.cpp

  • Committer: Alan Griffiths
  • Date: 2016-09-09 13:28:01 UTC
  • mfrom: (320.2.3 miral)
  • Revision ID: alam@octopull.co.uk-20160909132801-1rmrc2tl74qrhhkl
mergeĀ :parent

Show diffs side-by-side

added added

removed removed

Lines of Context:
34
34
 
35
35
namespace ms = mir::scene;
36
36
namespace mg = mir::graphics;
37
 
using StubSurface = mir::test::doubles::StubSurface;
38
37
using StubSession = mir::test::doubles::StubSession;
39
38
using namespace testing;
40
39
 
 
40
struct SizedStubSurface : public mir::test::doubles::StubSurface
 
41
{
 
42
    mir::geometry::Size size() const override { return toMirSize(m_size); }
 
43
 
 
44
    void setSize(QSize size) { m_size = size; }
 
45
 
 
46
private:
 
47
    QSize m_size;
 
48
};
 
49
 
41
50
 
42
51
class WindowModelTest : public ::testing::Test
43
52
{
51
60
    miral::WindowInfo createMirALWindowInfo(QPoint position = {160, 320}, QSize size = {100, 200})
52
61
    {
53
62
        const miral::Application app{stubSession};
 
63
        stubSurface->setSize(size);
54
64
        const miral::Window window{app, stubSurface};
55
65
 
56
66
        ms::SurfaceCreationParameters windowSpec;
57
 
        windowSpec.of_size(toMirSize(size));
 
67
//        windowSpec.of_size(toMirSize(size)); // useless, Window/Surface has the size actually used
58
68
        windowSpec.of_position(toMirPoint(position));
59
69
        return miral::WindowInfo{window, windowSpec};
60
70
    }
61
71
 
 
72
    miral::WindowInfo createMirALWindowInfoForInputMethod()
 
73
    {
 
74
        const miral::Application app{stubSession};
 
75
        const miral::Window window{app, stubSurface};
 
76
 
 
77
        ms::SurfaceCreationParameters windowSpec;
 
78
        windowSpec.of_type(mir_surface_type_inputmethod);
 
79
        return miral::WindowInfo{window, windowSpec};
 
80
    }
 
81
 
62
82
    MirSurface *getMirSurfaceFromModel(const WindowModel &model, int index)
63
83
    {
 
84
        flushEvents();
64
85
        return model.data(model.index(index, 0), WindowModel::SurfaceRole).value<MirSurface*>();
65
86
    }
66
87
 
69
90
        return getMirSurfaceFromModel(model, index)->window();
70
91
    }
71
92
 
 
93
    void SetUp() override
 
94
    {
 
95
        int argc = 0;
 
96
        char* argv[0];
 
97
        qtApp = new QCoreApplication(argc, argv); // needed for event loop
 
98
    }
 
99
 
 
100
    void TearDown() override
 
101
    {
 
102
        delete qtApp;
 
103
    }
 
104
 
 
105
    void flushEvents()
 
106
    {
 
107
        qtApp->sendPostedEvents();
 
108
    }
 
109
 
72
110
    const std::shared_ptr<StubSession> stubSession{std::make_shared<StubSession>()};
73
 
    const std::shared_ptr<StubSurface> stubSurface{std::make_shared<StubSurface>()};
 
111
    const std::shared_ptr<SizedStubSurface> stubSurface{std::make_shared<SizedStubSurface>()};
 
112
    QCoreApplication *qtApp;
74
113
};
75
114
 
76
115
/*
85
124
    auto mirWindowInfo = createMirALWindowInfo();
86
125
 
87
126
    notifier.addWindow(mirWindowInfo);
 
127
    flushEvents();
88
128
 
89
129
    EXPECT_EQ(1, model.count());
90
130
}
103
143
    QSignalSpy spyCountChanged(&model, SIGNAL(countChanged()));
104
144
 
105
145
    notifier.addWindow(mirWindowInfo);
 
146
    flushEvents();
106
147
 
107
148
    EXPECT_EQ(1, spyCountChanged.count());
108
149
}
119
160
    auto mirWindowInfo = createMirALWindowInfo();
120
161
 
121
162
    notifier.addWindow(mirWindowInfo);
 
163
    flushEvents();
122
164
 
123
165
    auto miralWindow = getMirALWindowFromModel(model, 0);
124
166
    EXPECT_EQ(mirWindowInfo.window(), miralWindow);
138
180
 
139
181
    // Test removing the window
140
182
    notifier.removeWindow(mirWindowInfo);
 
183
    flushEvents();
141
184
 
142
185
    EXPECT_EQ(0, model.count());
143
186
}
153
196
 
154
197
    auto mirWindowInfo = createMirALWindowInfo();
155
198
    notifier.addWindow(mirWindowInfo);
 
199
    flushEvents();
156
200
 
157
201
    // Test removing the window
158
202
    QSignalSpy spyCountChanged(&model, SIGNAL(countChanged()));
159
203
 
160
204
    notifier.removeWindow(mirWindowInfo);
 
205
    flushEvents();
161
206
 
162
207
    EXPECT_EQ(1, spyCountChanged.count());
163
208
}
176
221
 
177
222
    notifier.addWindow(mirWindowInfo1);
178
223
    notifier.addWindow(mirWindowInfo2);
 
224
    flushEvents();
179
225
 
180
226
    ASSERT_EQ(2, model.count());
181
227
    auto miralWindow1 = getMirALWindowFromModel(model, 0);
200
246
 
201
247
    // Remove second window
202
248
    notifier.removeWindow(mirWindowInfo2);
 
249
    flushEvents();
203
250
 
204
251
    ASSERT_EQ(1, model.count());
205
252
    auto miralWindow = getMirALWindowFromModel(model, 0);
222
269
 
223
270
    // Remove first window
224
271
    notifier.removeWindow(mirWindowInfo1);
 
272
    flushEvents();
225
273
 
226
274
    ASSERT_EQ(1, model.count());
227
275
    auto miralWindow = getMirALWindowFromModel(model, 0);
245
293
    notifier.removeWindow(mirWindowInfo1);
246
294
 
247
295
    notifier.addWindow(mirWindowInfo3);
 
296
    flushEvents();
248
297
 
249
298
    ASSERT_EQ(2, model.count());
250
299
    auto miralWindow2 = getMirALWindowFromModel(model, 0);
270
319
    notifier.addWindow(mirWindowInfo3);
271
320
 
272
321
    notifier.removeWindow(mirWindowInfo2);
 
322
    flushEvents();
273
323
 
274
324
    ASSERT_EQ(2, model.count());
275
325
    auto miralWindow1 = getMirALWindowFromModel(model, 0);
291
341
 
292
342
    // Raise first window
293
343
    notifier.raiseWindows({mirWindowInfo1.window()});
 
344
    flushEvents();
294
345
 
295
346
    ASSERT_EQ(1, model.count());
296
347
    auto topWindow = getMirALWindowFromModel(model, 0);
312
363
 
313
364
    // Raise second window (currently on top)
314
365
    notifier.raiseWindows({mirWindowInfo2.window()});
 
366
    flushEvents();
315
367
 
316
368
    // Check second window still on top
317
369
    ASSERT_EQ(2, model.count());
334
386
 
335
387
    // Raise first window (currently at bottom)
336
388
    notifier.raiseWindows({mirWindowInfo1.window()});
 
389
    flushEvents();
337
390
 
338
391
    // Check first window now on top
339
392
    ASSERT_EQ(2, model.count());
368
421
    // 2:   Window1
369
422
    // 1:   Window2
370
423
    // 0:   Window3
 
424
    flushEvents();
 
425
 
371
426
    ASSERT_EQ(3, model.count());
372
427
    auto topWindow = getMirALWindowFromModel(model, 2);
373
428
    EXPECT_EQ(mirWindowInfo1.window(), topWindow);
400
455
    // Model should now be like this:
401
456
    // 1:   Window1
402
457
    // 0:   Window2
 
458
    flushEvents();
 
459
 
403
460
    ASSERT_EQ(2, model.count());
404
461
    auto topWindow = getMirALWindowFromModel(model, 1);
405
462
    EXPECT_EQ(mirWindowInfo1.window(), topWindow);
435
492
    // 2:   Window2
436
493
    // 1:   Window1
437
494
    // 0:   Window3
 
495
    flushEvents();
 
496
 
438
497
    ASSERT_EQ(3, model.count());
439
498
    auto topWindow = getMirALWindowFromModel(model, 2);
440
499
    EXPECT_EQ(mirWindowInfo2.window(), topWindow);
456
515
 
457
516
    auto mirWindowInfo = createMirALWindowInfo(position);
458
517
    notifier.addWindow(mirWindowInfo);
 
518
    flushEvents();
459
519
 
460
520
    auto surface = getMirSurfaceFromModel(model, 0);
461
521
    EXPECT_EQ(position, surface->position());
479
539
 
480
540
    // Move window, check new position set
481
541
    notifier.moveWindow(mirWindowInfo, toMirPoint(newPosition));
 
542
    flushEvents();
482
543
 
483
544
    EXPECT_EQ(newPosition, surface->position());
484
545
}
503
564
 
504
565
    // Move window, check new position set
505
566
    notifier.moveWindow(mirWindowInfo1, toMirPoint(newPosition));
 
567
    flushEvents();
506
568
 
507
569
    EXPECT_EQ(newPosition, surface->position());
508
570
}
526
588
 
527
589
    // Move window, check new position set
528
590
    notifier.moveWindow(mirWindowInfo1, toMirPoint(QPoint(350, 420)));
 
591
    flushEvents();
529
592
 
530
593
    // Ensure other window untouched
531
594
    EXPECT_EQ(fixedPosition, surface->position());
543
606
 
544
607
    auto mirWindowInfo1 = createMirALWindowInfo(QPoint(), size);
545
608
    notifier.addWindow(mirWindowInfo1);
 
609
    flushEvents();
546
610
 
547
611
    auto surface = getMirSurfaceFromModel(model, 0);
548
612
    EXPECT_EQ(size, surface->size());
563
627
 
564
628
    auto surface = getMirSurfaceFromModel(model, 0);
565
629
 
566
 
    // Move window, check new position set
 
630
    // Resize window, check new size set
567
631
    notifier.resizeWindow(mirWindowInfo1, toMirSize(newSize));
 
632
    flushEvents();
568
633
 
569
634
    EXPECT_EQ(newSize, surface->size());
570
635
}
586
651
 
587
652
    auto surface = getMirSurfaceFromModel(model, 0);
588
653
 
589
 
    // Move window, check new position set
 
654
    // Resize window, check new size set
590
655
    notifier.resizeWindow(mirWindowInfo1, toMirSize(newSize));
 
656
    flushEvents();
591
657
 
592
658
    EXPECT_EQ(newSize, surface->size());
593
659
}
595
661
/*
596
662
 * Test: with 2 windows, ensure window resize does not impact other MirSurfaces
597
663
 */
598
 
TEST_F(WindowModelTest, DISABLED_WindowResizeDoesNotTouchOtherMirSurfaces)
 
664
TEST_F(WindowModelTest, WindowResizeDoesNotTouchOtherMirSurfaces)
599
665
{
600
666
    WindowModelNotifier notifier;
601
667
    WindowModel model(&notifier, nullptr); // no need for controller in this testcase
602
668
 
603
 
    QSize fixedPosition(300, 400);
 
669
    QSize fixedSize(300, 400);
604
670
 
605
671
    auto mirWindowInfo1 = createMirALWindowInfo(QPoint(), QSize(100, 200));
606
 
    auto mirWindowInfo2 = createMirALWindowInfo(QPoint(), fixedPosition);
 
672
    auto mirWindowInfo2 = createMirALWindowInfo(QPoint(), fixedSize);
607
673
    notifier.addWindow(mirWindowInfo1);
608
674
    notifier.addWindow(mirWindowInfo2);
609
675
 
610
676
    auto surface = getMirSurfaceFromModel(model, 1);
611
677
 
612
 
    // Move window
 
678
    // Resize window
613
679
    notifier.resizeWindow(mirWindowInfo1, toMirSize(QSize(150, 220)));
 
680
    flushEvents();
614
681
 
615
682
    // Ensure other window untouched
616
 
    EXPECT_EQ(fixedPosition, surface->size());
 
683
    EXPECT_EQ(fixedSize, surface->size());
 
684
}
 
685
 
 
686
/*
 
687
 * Test: that the WindowModelNotifier.addWindow for an Input Method Window causes
 
688
 * the Qt-side WindowModel to register the input method surface
 
689
 */
 
690
TEST_F(WindowModelTest, WhenAddInputMethodWindowNotifiedModelEmitsInputMethodChangedSignal)
 
691
{
 
692
    WindowModelNotifier notifier;
 
693
    WindowModel model(&notifier, nullptr); // no need for controller in this testcase
 
694
 
 
695
    auto mirWindowInfo = createMirALWindowInfoForInputMethod();
 
696
 
 
697
    QSignalSpy spyCountChanged(&model, SIGNAL(inputMethodSurfaceChanged(MirSurfaceInterface*)));
 
698
 
 
699
    notifier.addWindow(mirWindowInfo);
 
700
    flushEvents();
 
701
 
 
702
    EXPECT_EQ(1, spyCountChanged.count());
 
703
}
 
704
 
 
705
/*
 
706
 * Test: that the WindowModelNotifier.addWindow for an Input Method Window causes
 
707
 * the Qt-side WindowModel::inputMethodSurface property to be correctly set
 
708
 */
 
709
TEST_F(WindowModelTest, WhenAddInputMethodWindowNotifiedModelPropertyHasCorrectWindow)
 
710
{
 
711
    WindowModelNotifier notifier;
 
712
    WindowModel model(&notifier, nullptr); // no need for controller in this testcase
 
713
 
 
714
    auto mirWindowInfo = createMirALWindowInfoForInputMethod();
 
715
 
 
716
    notifier.addWindow(mirWindowInfo);
 
717
    flushEvents();
 
718
 
 
719
    auto miralWindow = static_cast<MirSurface*>(model.inputMethodSurface())->window();
 
720
    EXPECT_EQ(mirWindowInfo.window(), miralWindow);
 
721
}
 
722
 
 
723
/*
 
724
 * Test: that the WindowModelNotifier.removeWindow for an Input Method Window causes
 
725
 * the Qt-side WindowModel to reset the WindowModel::inputMethodSurface property to null
 
726
 */
 
727
TEST_F(WindowModelTest, WhenRemoveInputMethodWindowNotifiedModelPropertyReset)
 
728
{
 
729
    WindowModelNotifier notifier;
 
730
    WindowModel model(&notifier, nullptr); // no need for controller in this testcase
 
731
 
 
732
    auto mirWindowInfo = createMirALWindowInfoForInputMethod();
 
733
    notifier.addWindow(mirWindowInfo);
 
734
 
 
735
    // Test removing the window
 
736
    notifier.removeWindow(mirWindowInfo);
 
737
    flushEvents();
 
738
 
 
739
    EXPECT_EQ(nullptr, model.inputMethodSurface());
617
740
}