summaryrefslogtreecommitdiffstats
path: root/tests/qml/windowitem/tst_windowitem.qml
blob: ea835267131db5f573bf0d7cb732951d812d0b07 (plain)
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
/****************************************************************************
**
** Copyright (C) 2019 Luxoft Sweden AB
** Copyright (C) 2018 Pelagicore AG
** Contact: https://www.qt.io/licensing/
**
** This file is part of the Luxoft Application Manager.
**
** $QT_BEGIN_LICENSE:LGPL-QTAS$
** Commercial License Usage
** Licensees holding valid commercial Qt Automotive Suite licenses may use
** this file in accordance with the commercial license agreement provided
** with the Software or, alternatively, in accordance with the terms
** contained in a written agreement between you and The Qt Company.  For
** licensing terms and conditions see https://www.qt.io/terms-conditions.
** For further information use the contact form at https://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 3 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL3 included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 3 requirements
** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 2.0 or (at your option) the GNU General
** Public license version 3 or any later version approved by the KDE Free
** Qt Foundation. The licenses are as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-2.0.html and
** https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
** SPDX-License-Identifier: LGPL-3.0
**
****************************************************************************/

import QtQuick 2.3
import QtTest 1.0
import QtApplicationManager.SystemUI 2.0

Item {
    id: root
    width: 500
    height: 500
    visible: true

    Repeater {
        id: windowItemsRepeater
        model: ListModel { id: windowItemsModel }
        delegate: WindowItem {
            id: windowItem
            window: model.window

            property int clickCount: 0
            property alias mouseAreaVisible: mouseArea.visible

            MouseArea {
                id: mouseArea
                anchors.fill: parent
                onClicked: windowItem.clickCount += 1;
                z: -100 // some arbitrary, negative, Z
            }
        }
    }
    Repeater {
        id: sizedWindowItemsRepeater
        model: ListModel { id: sizedWindowItemsModel }
        delegate: WindowItem {
            width: 200
            height: 100
            window: model.window
        }
    }
    Repeater {
        id: noResizeWindowItemsRepeater
        model: ListModel { id: noResizeWindowItemsModel }
        delegate: WindowItem {
            width: 200
            height: 100
            objectFollowsItemSize: false
            window: model.window
        }
    }
    property var chosenModel
    Connections {
        target: WindowManager
        onWindowAdded:  {
            root.chosenModel.append({"window":window});
        }
    }

    // Force redraws so that pings and other events are quickly processed between
    // wayland client and server.
    Rectangle {
        width: 10
        height: 10
        color: "brown"
        RotationAnimator on rotation {
                from: 0; to: 360; duration: 1000
                loops: Animation.Infinite
                running: true
        }
    }

    SignalSpy {
        id: spyDestroyed
        signalName: "_windowDestroyed"
    }

    TestCase {
        id: testCase
        when: windowShown
        name: "WindowItem"

        property var app: null

        function init() {
            compare(windowItemsModel.count, 0);
            compare(sizedWindowItemsModel.count, 0);
            compare(noResizeWindowItemsRepeater.count, 0);
            compare(WindowManager.count, 0);
       }

        function cleanup() {
            windowItemsModel.clear();
            sizedWindowItemsModel.clear();
            noResizeWindowItemsModel.clear();

            if (app)
                app.stop();

            waitUntilAllAppsAreStopped();
        }

        function waitUntilAllAppsAreStopped() {
            while (true) {
                var numRunningApps = 0;
                for (var i = 0; i < ApplicationManager.count; i++) {
                    var app = ApplicationManager.application(i);
                    if (app.runState !== Am.NotRunning)
                        numRunningApps += 1;
                }

                if (numRunningApps > 0) {
                    wait(50);
                } else
                    break;
            }
        }

        function initWindowItemsModel() {
            root.chosenModel = windowItemsModel;
            startAppAndCheckWindow();
        }

        function initSizedWindowItemsModel() {
            root.chosenModel = sizedWindowItemsModel;
            startAppAndCheckWindow();
        }

        function startAppAndCheckWindow() {
            app = ApplicationManager.application("test.windowitem.app");
            app.start();

            tryCompare(root.chosenModel, "count", 1);
            tryCompare(WindowManager, "count", 1);
        }

        /*
            The first WindowItem showing a window have primary==true, from the
            second onwards they should have primary==false
         */
        function test_onlyFirstItemIsPrimary() {
            initWindowItemsModel();
            var firstWindowItem = windowItemsRepeater.itemAt(0);
            compare(firstWindowItem.primary, true);

            // Add a second view for the same WindowObject
            windowItemsModel.append({"window":firstWindowItem.window});

            var secondWindowItem = windowItemsRepeater.itemAt(1);
            compare(secondWindowItem.primary, false);
        }

        /*
            Turn a secondary WindowItem (primary == false) into the primary one.

            Check that the previously primary is now secondary and vice-versa.
         */
        function test_turnSecondaryIntoPrimary() {
            initWindowItemsModel();
            var firstWindowItem = windowItemsRepeater.itemAt(0);

            // Add a second view for the same WindowObject
            windowItemsModel.append({"window":firstWindowItem.window});

            var secondWindowItem = windowItemsRepeater.itemAt(1);

            compare(firstWindowItem.primary, true);
            compare(secondWindowItem.primary, false);

            // give primary role to the second WindowItem
            secondWindowItem.makePrimary();

            compare(firstWindowItem.primary, false);
            compare(secondWindowItem.primary, true);

            // and take it back
            firstWindowItem.makePrimary();

            compare(firstWindowItem.primary, true);
            compare(secondWindowItem.primary, false);
        }

        /*
            You have two WindowItems for the same WindowObject

            Check that once the primary WindowItem is destroyed,
            the remaining one takes over the primary role.
         */
        function test_destroyPrimaryRemainingTakesOver() {
            initWindowItemsModel();
            var firstWindowItem = windowItemsRepeater.itemAt(0);

            // Add a second view for the same WindowObject
            windowItemsModel.append({"window":firstWindowItem.window});

            var secondWindowItem = windowItemsRepeater.itemAt(1);

            compare(firstWindowItem.primary, true);
            compare(secondWindowItem.primary, false);

            compare(windowItemsModel.count, 2);

            // destroy the first WindowItem
            windowItemsModel.remove(0 /*index*/, 1 /*count*/);
            firstWindowItem = null;

            compare(windowItemsModel.count, 1);

            // And the remaining item takes over the primary role.
            tryCompare(secondWindowItem, "primary", true);
        }

        /*
            Check that a WindowObject with state NoSurface is destroyed
            only once all WindowItems using it are gone.
         */
        function test_surfacelessObjectStaysUntilAllItemsAreGone() {
            initWindowItemsModel();
            var firstWindowItem = windowItemsRepeater.itemAt(0);

            // Add a second view for the same WindowObject
            windowItemsModel.append({"window":firstWindowItem.window});

            var secondWindowItem = windowItemsRepeater.itemAt(1);

            var window = WindowManager.get(0).window;
            spyDestroyed.target = window;
            spyDestroyed.clear();

            compare(window.contentState, WindowObject.SurfaceWithContent);
            app.stop();

            // The WindowObject should still exist, albeit without a surface, even though
            // no longer present in WindowManager's model.
            tryCompare(WindowManager, "count", 0);
            compare(spyDestroyed.count, 0);
            tryCompare(window, "contentState", WindowObject.NoSurface);

            // Destroy all WindowItems
            firstWindowItem = null;
            secondWindowItem = null;
            windowItemsModel.clear();

            // Now that there are no WindowItems using that WindowObject anymore, it should
            // eventually be deleted by WindowManager
            spyDestroyed.wait();
            compare(spyDestroyed.count, 1);
        }

        /*
            Checks that the implicit size of a WindowItem matches the explicit size of the client's ApplicationManagerWindow
         */
        function test_implicitSize() {
            initWindowItemsModel();
            var windowItem = windowItemsRepeater.itemAt(0);
            var window = windowItem.window

            // Must match the ApplicationManagerWindow size defined in apps/test.windowitem.app/mail.qml
            compare(window.size.width, 123);
            compare(window.size.height, 321);
            compare(windowItem.width, 123);
            compare(windowItem.height, 321);

            // Avoid a race condition where the item's implicit size (and therefore the item's
            // size itself as no explicit width or height was assigned) would be set to match
            // the window's size while at the same time an item's resize would make the item resize the window.
            // In short: item size depending on window size while at the same time window size is
            // depending on item size.
            windowItem.objectFollowsItemSize = false;

            var width = 130;
            var height = 330;
            var i;
            for (i = 0; i < 20; i += 1) {
                window.setWindowProperty("requestedWidth", width);
                window.setWindowProperty("requestedHeight", height);

                tryCompare(window, "size", Qt.size(width,height));
                tryCompare(windowItem, "width", width);
                tryCompare(windowItem, "height", height);

                width += 5;
                height += 5;
                wait(10);
            }
        }

        /*
            Checks that once a Window is assinged to a WindowItem its underlying surface
            gets resized to match that WindowItem's size (considering it's the first,
            primary, one)
         */
        function test_initialResize() {
            initSizedWindowItemsModel();
            var windowItem = sizedWindowItemsRepeater.itemAt(0);
            var window = windowItem.window

            tryCompare(window, "size", Qt.size(windowItem.width, windowItem.height));
        }

        /*
            By default a WindowItem will resize the WindowObject it's displaying to match his own size.
            So resizing a WindowItem will cause his WindowObject to follow suit, so that both always
            have matching sizes.
         */
        function test_objectFollowsItemSize() {
            initSizedWindowItemsModel();
            var windowItem = sizedWindowItemsRepeater.itemAt(0);
            var window = windowItem.window;

            windowItem.width = 200;
            windowItem.height = 100;
            tryCompare(window, "size", Qt.size(200, 100));

            windowItem.width = 201;
            windowItem.height = 101;
            tryCompare(window, "size", Qt.size(201, 101));

            windowItem.width = 202;
            windowItem.height = 102;
            tryCompare(window, "size", Qt.size(202, 102));
        }

        /*
            When WindowItem.objectFollowsItemSize is false, resizing the WindowItem will have no effect
            over the WindowObject's size.
         */
        function test_windowDoesNotFolowItemSize() {
            root.chosenModel = noResizeWindowItemsModel;
            startAppAndCheckWindow();

            var windowItem = noResizeWindowItemsRepeater.itemAt(0);
            var window = windowItem.window;

            windowItem.width = 200;
            windowItem.height = 100;
            tryCompare(window, "size", Qt.size(123, 321));

            windowItem.width = 201;
            windowItem.height = 101;
            tryCompare(window, "size", Qt.size(123, 321));

            windowItem.width = 202;
            windowItem.height = 102;
            tryCompare(window, "size", Qt.size(123, 321));
        }

        /*
            Checks that calling close() on an empty WindowObject won't cause a crash (particularly
            in multi-process)
         */
        function test_closeEmptyWindow() {
            initWindowItemsModel();

            var windowItem = windowItemsRepeater.itemAt(0);
            var window = windowItem.window;

            app.stop();

            tryCompare(window, "contentState", WindowObject.NoSurface);

            window.close();
        }

        /*
            Regression test for https://bugreports.qt.io/browse/AUTOSUITE-652

            - Start an application that has two windows.
            - Call close() on the first one. It should vanish. Application should keep running normally.
            - Call close() on the second one. It should vanish as well and, being the app's last window, it should
              also cause the application to quit.
         */
        function test_closeWindows() {
            root.chosenModel = windowItemsModel;

            app = ApplicationManager.application("test.windowitem.multiwin");
            app.start();

            tryCompare(windowItemsModel, "count", 2);
            tryCompare(WindowManager, "count", 2);

            var firstWindow = windowItemsModel.get(0).window;
            var secondWindow = windowItemsModel.get(1).window;

            compare(app.runState, Am.Running);
            compare(firstWindow.contentState, WindowObject.SurfaceWithContent);

            firstWindow.close();

            tryCompare(firstWindow, "contentState", WindowObject.NoSurface);
            windowItemsModel.remove(0);
            firstWindow = null;

            wait(100);

            compare(app.runState, Am.Running);
            compare(secondWindow.contentState, WindowObject.SurfaceWithContent);

            secondWindow.close();

            tryCompare(secondWindow, "contentState", WindowObject.NoSurface);
            tryCompare(app, "runState", Am.NotRunning);
        }

        /*
            Children added by System-UI code must always stay in front of WindowItem's own private children.
         */
        function test_childrenZOrder() {
            initWindowItemsModel();

            var windowItem = windowItemsRepeater.itemAt(0);
            var window = windowItem.window;

            windowItem.mouseAreaVisible = false;

            touchEvent(windowItem).press(0).commit();
            touchEvent(windowItem).release(0).commit();

            // There's nothing in front of the wayland item (at least nothing visible).
            // The touch event will reach it.
            tryVerify(function() { return window.windowProperty("clickCount") === 1; });
            compare(windowItem.clickCount, 0);

            windowItem.mouseAreaVisible = true;

            touchEvent(windowItem).press(0).commit();
            touchEvent(windowItem).release(0).commit();

            // Since a visible MouseArea is now in front of WindowItem's internal wayland item
            // the second touch event was caught by that MouseArea instead.
            tryCompare(windowItem, "clickCount", 1);
            compare(window.windowProperty("clickCount"), 1);

        }

        // Checks that window properties are kept even when contentState is WindowObject.NoSurface
        // Regression test for https://bugreports.qt.io/browse/AUTOSUITE-694
        function test_window_keep_properties_when_nosurface() {
            initWindowItemsModel();

            var window = windowItemsModel.get(0).window;

            compare(window.contentState, WindowObject.SurfaceWithContent);

            window.setWindowProperty("foo", "bar");
            compare(window.windowProperty("foo"), "bar");

            app.stop();

            tryCompare(window, "contentState", WindowObject.NoSurface);
            compare(window.windowProperty("foo"), "bar");
        }
    }
}