aboutsummaryrefslogtreecommitdiffstats
path: root/tests/auto/quick/qquicklayouts/data/tst_stacklayout.qml
blob: 3455dd5cc1640013a62c16f4d809ba623aa8f6f4 (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
// Copyright (C) 2020 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause

import QtQuick 2.15
import QtTest 1.15
import QtQuick.Layouts 1.15

Item {
    id: container
    width: 200
    height: 200
    TestCase {
        id: testCase
        name: "Tests_StackLayout"
        when: windowShown
        width: 200
        height: 200

        Component {
            id: layout_rearrange_Component

            StackLayout {
                width: 640
                height: 480

                property alias testRectangle: testRectangle

                RowLayout {
                    spacing: 0

                    Rectangle {
                        Layout.preferredWidth: 100
                        Layout.preferredHeight: 100
                    }

                    Rectangle {
                        id: testRectangle
                        Layout.preferredWidth: 100
                        Layout.preferredHeight: 100
                        visible: false
                    }

                    Item {
                        Layout.fillWidth: true
                    }
                }
            }
        }

        Component {
            id: stackLayoutComponent

            StackLayout {}
        }

        Component {
            id: itemComponent

            Item {}
        }

        Component {
            id: signalSpyComponent

            SignalSpy {}
        }

        function test_rearrange()
        {
            var layout = layout_rearrange_Component.createObject(container)
            compare(layout.testRectangle.x, 0)
            layout.testRectangle.visible = true
            tryCompare(layout.testRectangle, "x", 100)

            layout.destroy()
        }

        // Test that items added dynamically to the StackLayout
        // have valid attached properties when accessed imperatively.
        function test_attachedDynamicImperative() {
            let layout = createTemporaryObject(stackLayoutComponent, container, { "anchors.fill": parent })
            verify(layout)

            let item1 = itemComponent.createObject(layout, { objectName: "item1" })
            verify(item1)
            compare(item1.StackLayout.index, 0)
            compare(item1.StackLayout.isCurrentItem, true)
            compare(item1.StackLayout.layout, layout)

            let item2 = itemComponent.createObject(layout, { objectName: "item2" })
            verify(item2)
            compare(item2.StackLayout.index, 1)
            compare(item2.StackLayout.isCurrentItem, false)
            compare(item2.StackLayout.layout, layout)

            // Test creating an item without a parent, accessing the attached properties,
            // and _then_ add it to the StackLayout and check its attached properties again.
            let item3 = itemComponent.createObject(null, { objectName: "item3" })
            verify(item3)
            compare(item3.StackLayout.index, -1)
            compare(item3.StackLayout.isCurrentItem, false)
            compare(item3.StackLayout.layout, null)

            let signalSpy = signalSpyComponent.createObject(item3,
                { target: item3.StackLayout, signalName: "indexChanged" })
            verify(signalSpy)
            verify(signalSpy.valid)

            item3.parent = layout
            compare(item3.StackLayout.index, 2)
            compare(item3.StackLayout.isCurrentItem, false)
            compare(item3.StackLayout.layout, layout)
            compare(signalSpy.count, 1)
        }

        Component {
            id: attachedPropertiesItemComponent

            Item {
                readonly property int index: StackLayout.index
                readonly property bool isCurrentItem: StackLayout.isCurrentItem
                readonly property StackLayout layout: StackLayout.layout
            }
        }

        // Test that items added dynamically to the StackLayout
        // have valid attached properties when accessed declaratively.
        function test_attachedDynamicDeclarative() {
            let layout = createTemporaryObject(stackLayoutComponent, container, { "anchors.fill": parent })
            verify(layout)

            let item1 = attachedPropertiesItemComponent.createObject(layout, { objectName: "item1" })
            verify(item1)
            compare(item1.index, 0)
            compare(item1.isCurrentItem, true)
            compare(item1.layout, layout)

            let item2 = attachedPropertiesItemComponent.createObject(layout, { objectName: "item2" })
            verify(item2)
            compare(item2.index, 1)
            compare(item2.isCurrentItem, false)
            compare(item2.layout, layout)
        }

        Component {
            id: tabComponent
            Rectangle {
                color: "#ff0000"
            }
        }

        function test_attachedDynamicRendered() {
            let layout = createTemporaryObject(stackLayoutComponent, container, { "anchors.fill": parent })
            verify(layout)

            let item1 = tabComponent.createObject(layout, { objectName: "item1" })
            verify(item1)
            compare(item1.StackLayout.index, 0)
            compare(item1.StackLayout.isCurrentItem, true)
            compare(item1.StackLayout.layout, layout)

            tryCompare(item1, "width", 200)
            tryCompare(item1, "height", 200)
        }

        Component {
            id: attachedStackLayoutComponent

            StackLayout {
                anchors.fill: parent

                property alias item1: item1
                property alias item2: item2

                Item {
                    id: item1
                    readonly property int index: StackLayout.index
                    readonly property bool isCurrentItem: StackLayout.isCurrentItem
                    readonly property StackLayout layout: StackLayout.layout
                }
                Item {
                    id: item2
                    readonly property int index: StackLayout.index
                    readonly property bool isCurrentItem: StackLayout.isCurrentItem
                    readonly property StackLayout layout: StackLayout.layout
                }
            }
        }

        // Test that items that are declared statically within StackLayout
        // have valid attached properties when accessed declaratively.
        function test_attachedStaticDeclarative() {
            let layout = createTemporaryObject(attachedStackLayoutComponent, container)
            verify(layout)
            compare(layout.item1.index, 0)
            compare(layout.item1.isCurrentItem, true)
            compare(layout.item1.layout, layout)
            compare(layout.item2.index, 1)
            compare(layout.item2.isCurrentItem, false)
            compare(layout.item2.layout, layout)
        }

        // Tests attached properties after adding and removing items.
        function test_attachedAddAndRemove() {
            let layout = createTemporaryObject(attachedStackLayoutComponent, container)
            verify(layout)
            compare(layout.item1.index, 0)
            compare(layout.item1.isCurrentItem, true)
            compare(layout.item1.layout, layout)
            compare(layout.item2.index, 1)
            compare(layout.item2.isCurrentItem, false)
            compare(layout.item2.layout, layout)

            // Remove item1. It's index should become -1.
            layout.item1.parent = null
            compare(layout.item1.index, -1)
            compare(layout.item1.isCurrentItem, false)
            compare(layout.item1.layout, null)
            compare(layout.item2.index, 0)
            compare(layout.item2.isCurrentItem, true)
            compare(layout.item2.layout, layout)

            // Add it back. Since it's appended, and item2 took index 0, its index should now be 1.
            layout.item1.parent = layout
            compare(layout.item1.index, 1)
            compare(layout.item1.isCurrentItem, false)
            compare(layout.item1.layout, layout)
            compare(layout.item2.index, 0)
            compare(layout.item2.isCurrentItem, true)
            compare(layout.item2.layout, layout)

            // Do the same with item2.
            layout.item2.parent = null
            compare(layout.item1.index, 0)
            compare(layout.item1.isCurrentItem, true)
            compare(layout.item1.layout, layout)
            compare(layout.item2.index, -1)
            compare(layout.item2.isCurrentItem, false)
            compare(layout.item2.layout, null)

            layout.item2.parent = layout
            compare(layout.item1.index, 0)
            compare(layout.item1.isCurrentItem, true)
            compare(layout.item1.layout, layout)
            compare(layout.item2.index, 1)
            compare(layout.item2.isCurrentItem, false)
            compare(layout.item2.layout, layout)
        }

        function test_implicitSize() {
            let layout = createTemporaryObject(stackLayoutComponent, container)
            verify(layout)
            let item1 = itemComponent.createObject(layout, { objectName: "item1", implicitWidth: 10, implicitHeight: 10 })
            verify(item1)
            compare(item1.implicitWidth, 10)
            compare(item1.implicitHeight, 10)
            let item2 = itemComponent.createObject(layout, { objectName: "item2", implicitWidth: 20, implicitHeight: 20 })
            verify(item2)
            compare(item2.implicitWidth, 20)
            compare(item2.implicitHeight, 20)
            verify(isPolishScheduled(layout))
            verify(waitForItemPolished(layout))
            compare(layout.implicitWidth, 20)
            compare(layout.implicitHeight, 20)
        }

        Component {
            id: layout_setCurrentIndex_Component

            StackLayout {
                width: 200
                height: 200

                property alias firstItem : rect
                property alias secondItem: rowLayout

                Rectangle {
                    id: rect
                    color: "red"
                    implicitWidth: 10
                    implicitHeight: 10
                }
                RowLayout {
                    id: rowLayout
                    spacing: 0
                    Rectangle {
                        color: "green"
                        implicitWidth: 10
                        implicitHeight: 10
                        Layout.fillWidth: true
                        Layout.fillHeight: true
                    }
                    Rectangle {
                        color: "blue"
                        implicitWidth: 10
                        implicitHeight: 10
                        Layout.fillWidth: true
                        Layout.fillHeight: true
                    }
                }
            }
        }

        function test_setCurrentIndex()
        {
            var layout = layout_setCurrentIndex_Component.createObject(container)
            compare(layout.firstItem.width, 200)

            // Invalidate the StackLayout (and its cached size hints)
            layout.firstItem.implicitWidth = 42

            layout.currentIndex = 1
            compare(layout.secondItem.width, 200)   // width should not be -1
            layout.destroy()
        }

    }
}