File: tst_TopLevelWindowModel.cpp

package info (click to toggle)
lomiri 0.5.0-7
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 24,088 kB
  • sloc: cpp: 39,498; python: 2,559; javascript: 1,426; ansic: 1,012; sh: 289; xml: 252; makefile: 69
file content (205 lines) | stat: -rw-r--r-- 7,687 bytes parent folder | download | duplicates (4)
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
/*
 * Copyright (C) 2017 Canonical Ltd.
 * Copyright (C) 2019 UBports Foundation
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 3.
 *
 * 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/>.
 */

// There is a companion set of QMLTests for TLWM in tst_QMLTopLevelWindowModel.qml.

#include <QtTest/QtTest>
#include <QSignalSpy>

// WindowManager plugin
#include <TopLevelWindowModel.h>
#include <Window.h>
#include <WindowManagerObjects.h>
#include <Workspace.h>
#include <WorkspaceManager.h>

#include "QtMirApplicationMocks.h"
#include "wmpolicyinterface.h"


class tst_TopLevelWindowModel : public QObject
{
    Q_OBJECT

private Q_SLOTS:
    void init(); // called right before each and every test function is executed
    void cleanup(); // called right after each and every test function is executed

    void singleSurfaceStartsHidden();
    void secondSurfaceIsHidden();
    void rootFocus();
    void rootFocusInhibit();

private:
    Workspace* workspace{nullptr};
    ApplicationManager *applicationManager{nullptr};
    SurfaceManager *surfaceManager{nullptr};
    TopLevelWindowModel *topLevelWindowModel{nullptr};
};

void tst_TopLevelWindowModel::init()
{
    wmPolicyInterface = new WindowManagementPolicy();

    applicationManager = new ApplicationManager;
    surfaceManager = new SurfaceManager;
    WindowManagerObjects::instance()->setApplicationManager(applicationManager);
    WindowManagerObjects::instance()->setSurfaceManager(surfaceManager);

    workspace = WorkspaceManager::instance()->createWorkspace();
    topLevelWindowModel = workspace->windowModel();
}

void tst_TopLevelWindowModel::cleanup()
{
    delete workspace;
    workspace = nullptr;

    delete surfaceManager;
    surfaceManager = nullptr;

    delete applicationManager;
    applicationManager = nullptr;

    delete wmPolicyInterface;
    wmPolicyInterface = nullptr;
}

void tst_TopLevelWindowModel::singleSurfaceStartsHidden()
{
    QCOMPARE(topLevelWindowModel->rowCount(), 0);

    auto application = static_cast<Application*>(applicationManager->startApplication(QString("hello-world"), QStringList()));

    QCOMPARE(topLevelWindowModel->rowCount(), 1);
    QCOMPARE((void*)topLevelWindowModel->windowAt(0)->surface(), (void*)nullptr);

    auto surface = new MirSurface;
    surface->m_state = Mir::HiddenState;
    application->m_surfaceList.addSurface(surface);
    Q_EMIT surfaceManager->surfacesAddedToWorkspace(workspace->workspace(), {surface});

    QCOMPARE(topLevelWindowModel->rowCount(), 1);
    // not showing the surface as it's still hidden
    QCOMPARE((void*)topLevelWindowModel->windowAt(0)->surface(), (void*)nullptr);

    surface->requestState(Mir::RestoredState);

    QCOMPARE(topLevelWindowModel->rowCount(), 1);
    // Now that the surface is no longer hidden, TopLevelWindowModel should expose it.
    QCOMPARE((void*)topLevelWindowModel->windowAt(0)->surface(), (void*)surface);
}

void tst_TopLevelWindowModel::secondSurfaceIsHidden()
{
    QCOMPARE(topLevelWindowModel->rowCount(), 0);

    auto application = static_cast<Application*>(applicationManager->startApplication(QString("hello-world"), QStringList()));

    QCOMPARE(topLevelWindowModel->rowCount(), 1);
    QCOMPARE((void*)topLevelWindowModel->windowAt(0)->surface(), (void*)nullptr);

    auto firstSurface = new MirSurface;
    application->m_surfaceList.addSurface(firstSurface);
    Q_EMIT surfaceManager->surfacesAddedToWorkspace(workspace->workspace(), {firstSurface});

    QCOMPARE(topLevelWindowModel->rowCount(), 1);
    QCOMPARE((void*)topLevelWindowModel->windowAt(0)->surface(), (void*)firstSurface);

    auto secondSurface = new MirSurface;
    secondSurface->m_state = Mir::HiddenState;
    application->m_surfaceList.addSurface(secondSurface);
    Q_EMIT surfaceManager->surfacesAddedToWorkspace(workspace->workspace(), {secondSurface});

    // still only the first surface is exposed by TopLevelWindowModel
    QCOMPARE(topLevelWindowModel->rowCount(), 1);
    QCOMPARE((void*)topLevelWindowModel->windowAt(0)->surface(), (void*)firstSurface);

    secondSurface->requestState(Mir::RestoredState);

    // now the second surface finally shows up
    QCOMPARE(topLevelWindowModel->rowCount(), 2);
    QCOMPARE((void*)topLevelWindowModel->windowAt(0)->surface(), (void*)secondSurface);
    QCOMPARE((void*)topLevelWindowModel->windowAt(1)->surface(), (void*)firstSurface);

    secondSurface->requestState(Mir::HiddenState);

    // and it's gone again
    QCOMPARE(topLevelWindowModel->rowCount(), 1);
    QCOMPARE((void*)topLevelWindowModel->windowAt(0)->surface(), (void*)firstSurface);
}

// Ensure that rootFocus unfocuses and refocuses windows
void tst_TopLevelWindowModel::rootFocus()
{
    applicationManager->startApplication(QString("hello-world"), QStringList());

    // We need to keep the window's surface null, unlike the other tests,
    // because then the TLWM assumes that the window is unknown to MirAL and
    // handles focus changes itself. If MirAL and QtMir are behaving properly,
    // they mirror this behavior for windows with surfaces.
    QCOMPARE(topLevelWindowModel->rowCount(), 1);
    QCOMPARE((void*)topLevelWindowModel->windowAt(0)->surface(), (void*)nullptr);
    auto dummyWindow = topLevelWindowModel->windowAt(0);

    auto rootFocusChangedSpy = new QSignalSpy(topLevelWindowModel, &TopLevelWindowModel::rootFocusChanged);

    // Unsetting rootFocus will remove focus from the dummy window
    topLevelWindowModel->setRootFocus(false);
    QVERIFY(!topLevelWindowModel->rootFocus());
    QVERIFY(!dummyWindow->focused());
    QCOMPARE(rootFocusChangedSpy->count(), 1);

    // Setting rootFocus will add focus back to the dummy window
    topLevelWindowModel->setRootFocus(true);
    QVERIFY(topLevelWindowModel->rootFocus());
    QCOMPARE((void*)topLevelWindowModel->focusedWindow(), (void*)dummyWindow);
    QCOMPARE(rootFocusChangedSpy->count(), 2);
}

// Ensure that rootFocus does not refocus a window if we focus another
void tst_TopLevelWindowModel::rootFocusInhibit()
{
    applicationManager->startApplication(QString("1"), QStringList());

    auto dummyWindow1 = topLevelWindowModel->windowAt(0);

    // Unsetting rootFocus will remove focus from the dummy window
    topLevelWindowModel->setRootFocus(false);
    QVERIFY(!topLevelWindowModel->rootFocus());
    QVERIFY(!dummyWindow1->focused());

    // Starting an Application will cause a pendingActivation, setting
    // rootFocus but preventing the original window from becoming refocused
    auto dummyWindow1FocusSpy = new QSignalSpy(dummyWindow1, &Window::focusedChanged);

    // Saying that dummyWindow1 is different than dummyWindow2 will have to do
    // since Applications don't know their Windows and we don't mock the other
    // way around.
    applicationManager->startApplication(QString("2"), QStringList());
    auto dummyWindow2 = topLevelWindowModel->windowAt(0);
    QVERIFY((void*)dummyWindow1 != (void*)dummyWindow2);

    QVERIFY(topLevelWindowModel->rootFocus());
    QVERIFY(!dummyWindow1->focused());
    QVERIFY(dummyWindow2->focused());
    QVERIFY(dummyWindow1FocusSpy->empty());
}

QTEST_MAIN(tst_TopLevelWindowModel)

#include "tst_TopLevelWindowModel.moc"