diff options
author | Mitch Curtis <mitch.curtis@qt.io> | 2019-12-09 15:13:33 +0100 |
---|---|---|
committer | Mitch Curtis <mitch.curtis@qt.io> | 2020-03-23 14:01:16 +0100 |
commit | 619dfbe8d02347cedb93a949cde627ec4424e0ae (patch) | |
tree | 670edc69d8c746fc5c3d73a7eccc0a84a7ea27d4 /tests/auto/quick/qquicklayouts | |
parent | b9c7f8989b979374ab0528827d69050415a424e0 (diff) |
StackLayout: add attached index, isCurrentItem, and layout properties
These properties are useful for items within StackLayout to get access
to their index within it, especially when those items are
declared in their own QML files. Similar API already exists for e.g.
ListView and SwipeView.
[ChangeLog][StackLayout] Added attached index, isCurrentItem,
and layout properties.
Change-Id: I648d4434ab21573b56edd9a0f8399463946fd571
Fixes: QTBUG-76999
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
Reviewed-by: Shawn Rutledge <shawn.rutledge@qt.io>
Diffstat (limited to 'tests/auto/quick/qquicklayouts')
-rw-r--r-- | tests/auto/quick/qquicklayouts/data/tst_stacklayout.qml | 175 |
1 files changed, 172 insertions, 3 deletions
diff --git a/tests/auto/quick/qquicklayouts/data/tst_stacklayout.qml b/tests/auto/quick/qquicklayouts/data/tst_stacklayout.qml index 8234ac6ef7..9c0b3f8635 100644 --- a/tests/auto/quick/qquicklayouts/data/tst_stacklayout.qml +++ b/tests/auto/quick/qquicklayouts/data/tst_stacklayout.qml @@ -48,9 +48,9 @@ ** ****************************************************************************/ -import QtQuick 2.2 -import QtTest 1.0 -import QtQuick.Layouts 1.3 +import QtQuick 2.15 +import QtTest 1.15 +import QtQuick.Layouts 1.15 Item { id: container @@ -94,6 +94,24 @@ Item { } } + Component { + id: stackLayoutComponent + + StackLayout {} + } + + Component { + id: itemComponent + + Item {} + } + + Component { + id: signalSpyComponent + + SignalSpy {} + } + function test_rearrange() { var layout = layout_rearrange_Component.createObject(container) @@ -103,5 +121,156 @@ Item { 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: 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) + } } } |