File: mirsurfaceitem_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 (298 lines) | stat: -rw-r--r-- 11,347 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
/*
 * Copyright (C) 2014-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/>.
 */

#define MIR_INCLUDE_DEPRECATED_EVENT_HEADER

struct MirEvent {}; // otherwise won't compile otherwise due to incomplete type

#include <gtest/gtest.h>

#include <QLoggingCategory>
#include <QTest>
#include <private/qquickitem_p.h>

// the test subject
#include <QtMir/Application/mirsurfaceitem.h>
// and friends
#include <QtMir/Application/mirsurface.h>

// tests/framework
#include <fake_mirsurface.h>
#include <fake_session.h>
#include <mock_shell.h>
#include <fake_surface.h>

// tests/modules/common
#include <surfaceobserver.h>

using namespace qtmir;

class MirSurfaceItemTest : public ::testing::Test
{
public:
    MirSurfaceItemTest()
    {
        setenv("QT_QPA_PLATFORM", "minimal", 1);
        int argc = 0;
        char **argv = nullptr;
        m_app = new QGuiApplication(argc, argv);

        // We don't want the logging spam cluttering the test results
        QLoggingCategory::setFilterRules(QStringLiteral("qtmir.surfaces=false"));
    }
    virtual ~MirSurfaceItemTest()
    {
        delete m_app;
    }
    QGuiApplication *m_app;
};

/*
  Tests that even if Qt fails to finish a touch sequence, MirSurfaceItem will
  properly finish it when forwarding it to its mir::input::surface. So
  mir::input::surface will still consume a proper sequence of touch events
  (comprised of a begin, zero or more updates and an end).
 */
TEST_F(MirSurfaceItemTest, MissingTouchEnd)
{
    MirSurfaceItem *surfaceItem = new MirSurfaceItem;

    FakeMirSurface *fakeSurface = new FakeMirSurface;

    surfaceItem->setSurface(fakeSurface);
    surfaceItem->setConsumesInput(true);

    ulong timestamp = 1234;
    QList<QTouchEvent::TouchPoint> touchPoints;
    touchPoints.append(QTouchEvent::TouchPoint());

    touchPoints[0].setId(0);
    touchPoints[0].setState(Qt::TouchPointPressed);
    surfaceItem->processTouchEvent(QEvent::TouchBegin,
            timestamp, Qt::NoModifier, touchPoints, touchPoints[0].state());

    touchPoints[0].setState(Qt::TouchPointMoved);
    surfaceItem->processTouchEvent(QEvent::TouchUpdate,
            timestamp + 10, Qt::NoModifier, touchPoints, touchPoints[0].state());

    // Starting a new touch sequence (with touch 1) without ending the current one
    // (wich has touch 0).
    touchPoints[0].setId(1);
    touchPoints[0].setState(Qt::TouchPointPressed);
    surfaceItem->processTouchEvent(QEvent::TouchBegin,
            timestamp + 20, Qt::NoModifier, touchPoints, touchPoints[0].state());


    auto touchesReceived = fakeSurface->touchesReceived();

    // MirSurface should have received 4 events:
    // 1 - (id=0,down)
    // 2 - (id=0,move)
    // 3 - (id=0,up) <- that's the one MirSurfaceItem should have synthesized to keep the event stream sane.
    // 4 - (id=1,down)
    ASSERT_EQ(4, touchesReceived.count());

    ASSERT_EQ(0, touchesReceived[0].touchPoints[0].id());
    ASSERT_EQ(Qt::TouchPointPressed, touchesReceived[0].touchPoints[0].state());

    ASSERT_EQ(0, touchesReceived[1].touchPoints[0].id());
    ASSERT_EQ(Qt::TouchPointMoved, touchesReceived[1].touchPoints[0].state());

    ASSERT_EQ(0, touchesReceived[2].touchPoints[0].id());
    ASSERT_EQ(Qt::TouchPointReleased, touchesReceived[2].touchPoints[0].state());

    ASSERT_EQ(1, touchesReceived[3].touchPoints[0].id());
    ASSERT_EQ(Qt::TouchPointPressed, touchesReceived[3].touchPoints[0].state());

    delete surfaceItem;
    delete fakeSurface;
}

TEST_F(MirSurfaceItemTest, SetSurfaceInitializesVisiblity)
{
    MirSurfaceItem *surfaceItem = new MirSurfaceItem;
    surfaceItem->setVisible(false);

    FakeMirSurface *fakeSurface = new FakeMirSurface;
    surfaceItem->setSurface(fakeSurface);

    EXPECT_FALSE(fakeSurface->visible());

    delete surfaceItem;
    delete fakeSurface;
}

TEST_F(MirSurfaceItemTest, AggregateSurfaceVisibility)
{
    MirSurfaceItem *surfaceItem1 = new MirSurfaceItem;
    surfaceItem1->setVisible(true);
    MirSurfaceItem *surfaceItem2 = new MirSurfaceItem;
    surfaceItem1->setVisible(true);

    FakeMirSurface *fakeSurface = new FakeMirSurface;
    surfaceItem1->setSurface(fakeSurface);
    surfaceItem2->setSurface(fakeSurface);

    EXPECT_TRUE(fakeSurface->visible());

    surfaceItem1->setVisible(false);
    EXPECT_TRUE(fakeSurface->visible());

    surfaceItem2->setVisible(false);
    EXPECT_FALSE(fakeSurface->visible());

    surfaceItem1->setVisible(true);
    EXPECT_TRUE(fakeSurface->visible());

    delete surfaceItem1;
    EXPECT_FALSE(fakeSurface->visible());

    delete surfaceItem2;
    delete fakeSurface;
}

TEST_F(MirSurfaceItemTest, NoSurfaceActiveFocusIfItemDoesNotConsumeInput)
{
    using namespace testing;

    // All the stuff qtmir::MirSurface needs
    auto fakeSession = new FakeSession();
    std::shared_ptr<mir::scene::Surface> mockSurface = std::make_shared<mir::scene::FakeSurface>();
    auto surfaceObserver = std::make_shared<SurfaceObserver>();
    mir::shell::MockShell mockShell;

    MirSurface *surface = new MirSurface(mockSurface, fakeSession, &mockShell, surfaceObserver, CreationHints());

    MirSurfaceItem *surfaceItem = new MirSurfaceItem;
    QQuickItemPrivate *surfaceItemPrivate = QQuickItemPrivate::get(surfaceItem);

    surfaceItem->setConsumesInput(true);
    surfaceItemPrivate->activeFocus = true;

    EXPECT_CALL(mockShell, set_surface_attribute(fakeSession->session(), mockSurface, mir_window_attrib_focus, (int)mir_window_focus_state_focused))
        .Times(AtLeast(1));
    EXPECT_CALL(mockShell, set_surface_attribute(fakeSession->session(), mockSurface, mir_window_attrib_focus, (int)mir_window_focus_state_unfocused))
        .Times(0);

    surfaceItem->setSurface(surface);

    Mock::VerifyAndClearExpectations(&mockShell);
    EXPECT_CALL(mockShell, set_surface_attribute(fakeSession->session(), mockSurface, mir_window_attrib_focus, (int)mir_window_focus_state_focused))
        .Times(0);
    EXPECT_CALL(mockShell, set_surface_attribute(fakeSession->session(), mockSurface, mir_window_attrib_focus, (int)mir_window_focus_state_unfocused))
        .Times(AtLeast(1));

    surfaceItem->setConsumesInput(false);

    Mock::VerifyAndClearExpectations(&mockShell);
    EXPECT_CALL(mockShell, set_surface_attribute(fakeSession->session(), mockSurface, mir_window_attrib_focus, (int)mir_window_focus_state_focused))
        .Times(AtLeast(1));
    EXPECT_CALL(mockShell, set_surface_attribute(fakeSession->session(), mockSurface, mir_window_attrib_focus, (int)mir_window_focus_state_unfocused))
        .Times(0);

    surfaceItem->setConsumesInput(true);

    Mock::VerifyAndClearExpectations(&mockShell);
    EXPECT_CALL(mockShell, set_surface_attribute(fakeSession->session(), mockSurface, mir_window_attrib_focus, (int)mir_window_focus_state_focused))
        .Times(0);
    EXPECT_CALL(mockShell, set_surface_attribute(fakeSession->session(), mockSurface, mir_window_attrib_focus, (int)mir_window_focus_state_unfocused))
        .Times(AtLeast(1));

    delete surfaceItem;

    Mock::VerifyAndClearExpectations(&mockShell);

    // clean up
    delete surface;
    delete fakeSession;
}

TEST_F(MirSurfaceItemTest, AggregateSurfaceActiveFocus)
{
    using namespace testing;

    // All the stuff qtmir::MirSurface needs
    auto fakeSession = new FakeSession();
    std::shared_ptr<mir::scene::Surface> mockSurface = std::make_shared<mir::scene::FakeSurface>();
    auto surfaceObserver = std::make_shared<SurfaceObserver>();
    mir::shell::MockShell mockShell;

    MirSurface *surface = new MirSurface(mockSurface, fakeSession, &mockShell, surfaceObserver, CreationHints());

    MirSurfaceItem *surfaceItem1 = new MirSurfaceItem;
    QQuickItemPrivate *surfaceItem1Private = QQuickItemPrivate::get(surfaceItem1);

    EXPECT_CALL(mockShell, set_surface_attribute(fakeSession->session(), mockSurface, mir_window_attrib_focus, (int)mir_window_focus_state_focused))
        .Times(0);
    EXPECT_CALL(mockShell, set_surface_attribute(fakeSession->session(), mockSurface, mir_window_attrib_focus, (int)mir_window_focus_state_unfocused));

    surfaceItem1->setConsumesInput(true);
    surfaceItem1Private->activeFocus = false;
    surfaceItem1->setSurface(surface);

    Mock::VerifyAndClearExpectations(&mockShell);
    EXPECT_CALL(mockShell, set_surface_attribute(fakeSession->session(), mockSurface, mir_window_attrib_focus, (int)mir_window_focus_state_focused))
        .Times(AtLeast(1));
    EXPECT_CALL(mockShell, set_surface_attribute(fakeSession->session(), mockSurface, mir_window_attrib_focus, (int)mir_window_focus_state_unfocused))
        .Times(0);

    surfaceItem1Private->activeFocus = true;
    Q_EMIT surfaceItem1->activeFocusChanged(true);

    Mock::VerifyAndClearExpectations(&mockShell);
    EXPECT_CALL(mockShell, set_surface_attribute(fakeSession->session(), mockSurface, mir_window_attrib_focus, (int)mir_window_focus_state_focused))
        .Times(AtLeast(0)); // no harm in calling it unnecessarily
    EXPECT_CALL(mockShell, set_surface_attribute(fakeSession->session(), mockSurface, mir_window_attrib_focus, (int)mir_window_focus_state_unfocused))
        .Times(0);

    MirSurfaceItem *surfaceItem2 = new MirSurfaceItem;
    QQuickItemPrivate *surfaceItem2Private = QQuickItemPrivate::get(surfaceItem2);

    surfaceItem2->setConsumesInput(true);
    surfaceItem2Private->activeFocus = false;
    surfaceItem2->setSurface(surface);

    surfaceItem2Private->activeFocus = true;
    Q_EMIT surfaceItem2->activeFocusChanged(true);
    surfaceItem1Private->activeFocus = false;
    Q_EMIT surfaceItem1->activeFocusChanged(false);

    Mock::VerifyAndClearExpectations(&mockShell);
    EXPECT_CALL(mockShell, set_surface_attribute(fakeSession->session(), mockSurface, mir_window_attrib_focus, (int)mir_window_focus_state_focused))
        .Times(AtLeast(0)); // no harm in calling it unnecessarily
    EXPECT_CALL(mockShell, set_surface_attribute(fakeSession->session(), mockSurface, mir_window_attrib_focus, (int)mir_window_focus_state_unfocused))
        .Times(0);

    surfaceItem1Private->activeFocus = true;
    Q_EMIT surfaceItem1->activeFocusChanged(true);
    surfaceItem1->setConsumesInput(false);

    delete surfaceItem1;

    Mock::VerifyAndClearExpectations(&mockShell);
    EXPECT_CALL(mockShell, set_surface_attribute(fakeSession->session(), mockSurface, mir_window_attrib_focus, (int)mir_window_focus_state_focused))
        .Times(0);
    EXPECT_CALL(mockShell, set_surface_attribute(fakeSession->session(), mockSurface, mir_window_attrib_focus, (int)mir_window_focus_state_unfocused))
        .Times(AtLeast(1));

    delete surfaceItem2;

    Mock::VerifyAndClearExpectations(&mockShell);

    // clean up
    delete surface;
    delete fakeSession;
}