~alan-griffiths/miral/debug

« back to all changes in this revision

Viewing changes to miral-qt/tests/modules/SurfaceManager/mirsurfaceitem_test.cpp

  • Committer: Gerry Boland
  • Date: 2016-06-01 22:06:51 UTC
  • mto: This revision was merged to the branch mainline in revision 178.
  • Revision ID: gerry.boland@canonical.com-20160601220651-ge508tffql4e7u7c
Import QtMir code into miral-qt subdirectory. Disabled by default, use -DMIRAL_ENABLE_QT=1 to build.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2014-2015 Canonical, Ltd.
 
3
 *
 
4
 * This program is free software: you can redistribute it and/or modify it under
 
5
 * the terms of the GNU Lesser General Public License version 3, as published by
 
6
 * the Free Software Foundation.
 
7
 *
 
8
 * This program is distributed in the hope that it will be useful, but WITHOUT
 
9
 * ANY WARRANTY; without even the implied warranties of MERCHANTABILITY,
 
10
 * SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
11
 * Lesser General Public License for more details.
 
12
 *
 
13
 * You should have received a copy of the GNU Lesser General Public License
 
14
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
15
 */
 
16
 
 
17
#define MIR_INCLUDE_DEPRECATED_EVENT_HEADER
 
18
 
 
19
#include <gtest/gtest.h>
 
20
 
 
21
#include <QLoggingCategory>
 
22
#include <QTest>
 
23
 
 
24
// the test subject
 
25
#include <Unity/Application/mirsurfaceitem.h>
 
26
 
 
27
// tests/framework
 
28
#include <fake_mirsurface.h>
 
29
 
 
30
using namespace qtmir;
 
31
 
 
32
class MirSurfaceItemTest : public ::testing::Test
 
33
{
 
34
public:
 
35
    MirSurfaceItemTest()
 
36
    {
 
37
        setenv("QT_QPA_PLATFORM", "minimal", 1);
 
38
        int argc = 0;
 
39
        char **argv = nullptr;
 
40
        m_app = new QGuiApplication(argc, argv);
 
41
 
 
42
        // We don't want the logging spam cluttering the test results
 
43
        QLoggingCategory::setFilterRules(QStringLiteral("qtmir.surfaces=false"));
 
44
    }
 
45
    virtual ~MirSurfaceItemTest()
 
46
    {
 
47
        delete m_app;
 
48
    }
 
49
    QGuiApplication *m_app;
 
50
};
 
51
 
 
52
/*
 
53
  Tests that even if Qt fails to finish a touch sequence, MirSurfaceItem will
 
54
  properly finish it when forwarding it to its mir::input::surface. So
 
55
  mir::input::surface will still consume a proper sequence of touch events
 
56
  (comprised of a begin, zero or more updates and an end).
 
57
 */
 
58
TEST_F(MirSurfaceItemTest, MissingTouchEnd)
 
59
{
 
60
    MirSurfaceItem *surfaceItem = new MirSurfaceItem;
 
61
 
 
62
    FakeMirSurface *fakeSurface = new FakeMirSurface;
 
63
 
 
64
    surfaceItem->setSurface(fakeSurface);
 
65
    surfaceItem->setConsumesInput(true);
 
66
 
 
67
    ulong timestamp = 1234;
 
68
    QList<QTouchEvent::TouchPoint> touchPoints;
 
69
    touchPoints.append(QTouchEvent::TouchPoint());
 
70
 
 
71
    touchPoints[0].setId(0);
 
72
    touchPoints[0].setState(Qt::TouchPointPressed);
 
73
    surfaceItem->processTouchEvent(QEvent::TouchBegin,
 
74
            timestamp, Qt::NoModifier, touchPoints, touchPoints[0].state());
 
75
 
 
76
    touchPoints[0].setState(Qt::TouchPointMoved);
 
77
    surfaceItem->processTouchEvent(QEvent::TouchUpdate,
 
78
            timestamp + 10, Qt::NoModifier, touchPoints, touchPoints[0].state());
 
79
 
 
80
    // Starting a new touch sequence (with touch 1) without ending the current one
 
81
    // (wich has touch 0).
 
82
    touchPoints[0].setId(1);
 
83
    touchPoints[0].setState(Qt::TouchPointPressed);
 
84
    surfaceItem->processTouchEvent(QEvent::TouchBegin,
 
85
            timestamp + 20, Qt::NoModifier, touchPoints, touchPoints[0].state());
 
86
 
 
87
 
 
88
    auto touchesReceived = fakeSurface->touchesReceived();
 
89
 
 
90
    // MirSurface should have received 4 events:
 
91
    // 1 - (id=0,down)
 
92
    // 2 - (id=0,move)
 
93
    // 3 - (id=0,up) <- that's the one MirSurfaceItem should have synthesized to keep the event stream sane.
 
94
    // 4 - (id=1,down)
 
95
    ASSERT_EQ(4, touchesReceived.count());
 
96
 
 
97
    ASSERT_EQ(0, touchesReceived[0].touchPoints[0].id());
 
98
    ASSERT_EQ(Qt::TouchPointPressed, touchesReceived[0].touchPoints[0].state());
 
99
 
 
100
    ASSERT_EQ(0, touchesReceived[1].touchPoints[0].id());
 
101
    ASSERT_EQ(Qt::TouchPointMoved, touchesReceived[1].touchPoints[0].state());
 
102
 
 
103
    ASSERT_EQ(0, touchesReceived[2].touchPoints[0].id());
 
104
    ASSERT_EQ(Qt::TouchPointReleased, touchesReceived[2].touchPoints[0].state());
 
105
 
 
106
    ASSERT_EQ(1, touchesReceived[3].touchPoints[0].id());
 
107
    ASSERT_EQ(Qt::TouchPointPressed, touchesReceived[3].touchPoints[0].state());
 
108
 
 
109
    delete surfaceItem;
 
110
    delete fakeSurface;
 
111
}
 
112
 
 
113
TEST_F(MirSurfaceItemTest, SetSurfaceInitializesVisiblity)
 
114
{
 
115
    MirSurfaceItem *surfaceItem = new MirSurfaceItem;
 
116
    surfaceItem->setVisible(false);
 
117
 
 
118
    FakeMirSurface *fakeSurface = new FakeMirSurface;
 
119
    surfaceItem->setSurface(fakeSurface);
 
120
 
 
121
    EXPECT_FALSE(fakeSurface->visible());
 
122
 
 
123
    delete surfaceItem;
 
124
    delete fakeSurface;
 
125
}
 
126
 
 
127
TEST_F(MirSurfaceItemTest, AggregateSurfaceVisibility)
 
128
{
 
129
    MirSurfaceItem *surfaceItem1 = new MirSurfaceItem;
 
130
    surfaceItem1->setVisible(true);
 
131
    MirSurfaceItem *surfaceItem2 = new MirSurfaceItem;
 
132
    surfaceItem1->setVisible(true);
 
133
 
 
134
    FakeMirSurface *fakeSurface = new FakeMirSurface;
 
135
    surfaceItem1->setSurface(fakeSurface);
 
136
    surfaceItem2->setSurface(fakeSurface);
 
137
 
 
138
    EXPECT_TRUE(fakeSurface->visible());
 
139
 
 
140
    surfaceItem1->setVisible(false);
 
141
    EXPECT_TRUE(fakeSurface->visible());
 
142
 
 
143
    surfaceItem2->setVisible(false);
 
144
    EXPECT_FALSE(fakeSurface->visible());
 
145
 
 
146
    surfaceItem1->setVisible(true);
 
147
    EXPECT_TRUE(fakeSurface->visible());
 
148
 
 
149
    delete surfaceItem1;
 
150
    EXPECT_FALSE(fakeSurface->visible());
 
151
 
 
152
    delete surfaceItem2;
 
153
    delete fakeSurface;
 
154
}