Change copyrights from Nokia to Digia
[profile/ivi/qtwayland.git] / tests / auto / compositor / tst_compositor.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the test suite of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.  For licensing terms and
14 ** conditions see http://qt.digia.com/licensing.  For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file.  Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights.  These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file.  Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include "mockclient.h"
43 #include "testcompositor.h"
44
45 #include <QtTest/QtTest>
46
47 class tst_WaylandCompositor : public QObject
48 {
49     Q_OBJECT
50
51 public:
52     tst_WaylandCompositor() {
53         setenv("XDG_RUNTIME_DIR", ".", 1);
54     }
55
56 private slots:
57     void singleClient();
58     void multipleClients();
59     void geometry();
60     void mapSurface();
61     void frameCallback();
62 };
63
64 void tst_WaylandCompositor::singleClient()
65 {
66     TestCompositor compositor;
67
68     MockClient client;
69
70     wl_surface *sa = client.createSurface();
71     QTRY_COMPARE(compositor.surfaces.size(), 1);
72
73     wl_surface *sb = client.createSurface();
74     QTRY_COMPARE(compositor.surfaces.size(), 2);
75
76     WaylandClient *ca = compositor.surfaces.at(0)->client();
77     WaylandClient *cb = compositor.surfaces.at(1)->client();
78
79     QCOMPARE(ca, cb);
80
81     QList<WaylandSurface *> surfaces = compositor.surfacesForClient(ca);
82     QCOMPARE(surfaces.size(), 2);
83     QVERIFY((surfaces.at(0) == compositor.surfaces.at(0) && surfaces.at(1) == compositor.surfaces.at(1))
84             || (surfaces.at(0) == compositor.surfaces.at(1) && surfaces.at(1) == compositor.surfaces.at(0)));
85
86     wl_surface_destroy(sa);
87     QTRY_COMPARE(compositor.surfaces.size(), 1);
88
89     wl_surface_destroy(sb);
90     QTRY_COMPARE(compositor.surfaces.size(), 0);
91 }
92
93 void tst_WaylandCompositor::multipleClients()
94 {
95     TestCompositor compositor;
96
97     MockClient a;
98     MockClient b;
99     MockClient c;
100
101     wl_surface *sa = a.createSurface();
102     wl_surface *sb = b.createSurface();
103     wl_surface *sc = c.createSurface();
104
105     QTRY_COMPARE(compositor.surfaces.size(), 3);
106
107     WaylandClient *ca = compositor.surfaces.at(0)->client();
108     WaylandClient *cb = compositor.surfaces.at(1)->client();
109     WaylandClient *cc = compositor.surfaces.at(2)->client();
110
111     QVERIFY(ca != cb);
112     QVERIFY(ca != cc);
113     QVERIFY(cb != cc);
114
115     QCOMPARE(compositor.surfacesForClient(ca).size(), 1);
116     QCOMPARE(compositor.surfacesForClient(ca).at(0), compositor.surfaces.at(0));
117
118     QCOMPARE(compositor.surfacesForClient(cb).size(), 1);
119     QCOMPARE(compositor.surfacesForClient(cb).at(0), compositor.surfaces.at(1));
120
121     QCOMPARE(compositor.surfacesForClient(cc).size(), 1);
122     QCOMPARE(compositor.surfacesForClient(cc).at(0), compositor.surfaces.at(2));
123
124     wl_surface_destroy(sa);
125     wl_surface_destroy(sb);
126     wl_surface_destroy(sc);
127
128     QTRY_COMPARE(compositor.surfaces.size(), 0);
129 }
130
131 void tst_WaylandCompositor::geometry()
132 {
133     TestCompositor compositor;
134
135     QRect geometry(0, 0, 4096, 3072);
136     compositor.setOutputGeometry(geometry);
137
138     MockClient client;
139
140     QTRY_COMPARE(client.geometry, geometry);
141 }
142
143 void tst_WaylandCompositor::mapSurface()
144 {
145     TestCompositor compositor;
146
147     MockClient client;
148
149     wl_surface *surface = client.createSurface();
150     QTRY_COMPARE(compositor.surfaces.size(), 1);
151
152     WaylandSurface *waylandSurface = compositor.surfaces.at(0);
153
154     QSignalSpy mappedSpy(waylandSurface, SIGNAL(mapped()));
155
156     QCOMPARE(waylandSurface->size(), QSize());
157     QCOMPARE(waylandSurface->type(), WaylandSurface::Invalid);
158
159     QSize size(256, 256);
160     ShmBuffer buffer(size, client.shm);
161
162     wl_surface_attach(surface, buffer.handle, 0, 0);
163     wl_surface_damage(surface, 0, 0, size.width(), size.height());
164
165     QTRY_COMPARE(waylandSurface->size(), size);
166     QTRY_COMPARE(waylandSurface->type(), WaylandSurface::Shm);
167     QTRY_COMPARE(mappedSpy.count(), 1);
168
169     wl_surface_destroy(surface);
170 }
171
172 static void frameCallbackFunc(void *data, wl_callback *callback, uint32_t)
173 {
174     ++*static_cast<int *>(data);
175     wl_callback_destroy(callback);
176 }
177
178 static void registerFrameCallback(wl_surface *surface, int *counter)
179 {
180     static const wl_callback_listener frameCallbackListener = {
181         frameCallbackFunc
182     };
183
184     wl_callback_add_listener(wl_surface_frame(surface), &frameCallbackListener, counter);
185 }
186
187 void tst_WaylandCompositor::frameCallback()
188 {
189     TestCompositor compositor;
190
191     MockClient client;
192
193     QSize size(8, 8);
194     ShmBuffer buffer(size, client.shm);
195
196     wl_surface *surface = client.createSurface();
197     wl_surface_attach(surface, buffer.handle, 0, 0);
198
199     int frameCounter = 0;
200
201     QTRY_COMPARE(compositor.surfaces.size(), 1);
202     WaylandSurface *waylandSurface = compositor.surfaces.at(0);
203     QSignalSpy damagedSpy(waylandSurface, SIGNAL(damaged(const QRect &)));
204
205     for (int i = 0; i < 10; ++i) {
206         registerFrameCallback(surface, &frameCounter);
207         wl_surface_damage(surface, 0, 0, size.width(), size.height());
208
209         QTRY_COMPARE(waylandSurface->type(), WaylandSurface::Shm);
210         QTRY_COMPARE(damagedSpy.count(), i + 1);
211
212         QCOMPARE(waylandSurface->image(), buffer.image);
213         waylandSurface->frameFinished();
214
215         QTRY_COMPARE(frameCounter, i + 1);
216     }
217
218     wl_surface_destroy(surface);
219 }
220
221 #include <tst_compositor.moc>
222 QTEST_MAIN(tst_WaylandCompositor);