Warning, /frameworks/kirigami/autotests/tst_columnview.qml is written in an unsupported language. File is not indexed.

0001 /*
0002  *  SPDX-FileCopyrightText: 2023 ivan tkachenko <me@ratijas.tk>
0003  *
0004  *  SPDX-License-Identifier: LGPL-2.0-or-later
0005  */
0006 
0007 import QtQuick
0008 import QtQuick.Controls as QQC2
0009 import org.kde.kirigami as Kirigami
0010 import QtTest
0011 
0012 TestCase {
0013     name: "ColumnView"
0014     visible: true
0015     when: windowShown
0016 
0017     width: 500
0018     height: 500
0019 
0020     Component {
0021         id: columnViewComponent
0022         Kirigami.ColumnView {}
0023     }
0024 
0025     Component {
0026         id: emptyItemPageComponent
0027         Item {}
0028     }
0029 
0030     function createViewWith3Items() {
0031         const view = createTemporaryObject(columnViewComponent, this);
0032         verify(view);
0033 
0034         const zero = createTemporaryObject(emptyItemPageComponent, this, { objectName: "zero" });
0035         view.addItem(zero);
0036 
0037         const one = createTemporaryObject(emptyItemPageComponent, this, { objectName: "one" });
0038         view.addItem(one);
0039 
0040         const two = createTemporaryObject(emptyItemPageComponent, this, { objectName: "two" });
0041         view.addItem(two);
0042 
0043         compare(view.count, 3);
0044 
0045         return ({
0046             view,
0047             zero,
0048             one,
0049             two,
0050         });
0051     }
0052 
0053     function test_clear() {
0054         const { view } = createViewWith3Items();
0055         view.clear();
0056         compare(view.count, 0);
0057     }
0058 
0059     function test_contains() {
0060         const { view, zero, one, two } = createViewWith3Items();
0061 
0062         verify(view.containsItem(zero));
0063         verify(view.containsItem(one));
0064         verify(view.containsItem(two));
0065 
0066         view.removeItem(zero);
0067         verify(!view.containsItem(zero));
0068 
0069         view.addItem(zero);
0070         verify(view.containsItem(zero));
0071 
0072         verify(!view.containsItem(null));
0073     }
0074 
0075     function test_remove_by_index_leading() {
0076         const { view, zero: target } = createViewWith3Items();
0077         const item = view.removeItem(0);
0078         compare(item, target);
0079         compare(view.count, 2);
0080     }
0081 
0082     function test_remove_by_index_trailing() {
0083         const { view, two: target } = createViewWith3Items();
0084         compare(view.count - 1, 2);
0085         const item = view.removeItem(2);
0086         compare(item, target);
0087         compare(view.count, 2);
0088     }
0089 
0090     function test_remove_by_index_middle() {
0091         const { view, one: target } = createViewWith3Items();
0092         const item = view.removeItem(1);
0093         compare(item, target);
0094         compare(view.count, 2);
0095     }
0096 
0097     function test_remove_by_index_last() {
0098         const { view, zero, one, two } = createViewWith3Items();
0099         let item;
0100 
0101         item = view.removeItem(0);
0102         compare(item, zero);
0103         compare(view.count, 2);
0104 
0105         item = view.removeItem(1);
0106         compare(item, two);
0107         compare(view.count, 1);
0108 
0109         item = view.removeItem(0);
0110         compare(item, one);
0111         compare(view.count, 0);
0112     }
0113 
0114     function test_remove_by_index_negative() {
0115         const { view } = createViewWith3Items();
0116         const item = view.removeItem(-1);
0117         compare(item, null);
0118         compare(view.count, 3);
0119     }
0120 
0121     function test_remove_by_index_out_of_bounds() {
0122         const { view } = createViewWith3Items();
0123         const item = view.removeItem(view.count);
0124         compare(item, null);
0125         compare(view.count, 3);
0126     }
0127 
0128     function test_remove_by_item_leading() {
0129         const { view, zero: target } = createViewWith3Items();
0130         const item = view.removeItem(target);
0131         compare(item, target);
0132         compare(view.count, 2);
0133     }
0134 
0135     function test_remove_by_item_trailing() {
0136         const { view, two: target } = createViewWith3Items();
0137         const item = view.removeItem(target);
0138         compare(item, target);
0139         compare(view.count, 2);
0140     }
0141 
0142     function test_remove_by_item_middle() {
0143         const { view, one: target } = createViewWith3Items();
0144         const item = view.removeItem(target);
0145         compare(item, target);
0146         compare(view.count, 2);
0147     }
0148 
0149     function test_remove_by_item_last() {
0150         const { view, zero, one, two } = createViewWith3Items();
0151         let item;
0152 
0153         item = view.removeItem(zero);
0154         compare(item, zero);
0155         compare(view.count, 2);
0156 
0157         item = view.removeItem(one);
0158         compare(item, one);
0159         compare(view.count, 1);
0160 
0161         item = view.removeItem(two);
0162         compare(item, two);
0163         compare(view.count, 0);
0164     }
0165 
0166     function test_remove_by_item_null() {
0167         const { view } = createViewWith3Items();
0168         const item = view.removeItem(null);
0169         compare(item, null);
0170         compare(view.count, 3);
0171     }
0172 
0173     function test_remove_by_item_from_empty() {
0174         const view = createTemporaryObject(columnViewComponent, this);
0175         verify(view);
0176         let item;
0177 
0178         item = view.removeItem(null);
0179         compare(item, null);
0180 
0181         item = view.removeItem(this);
0182         compare(item, null);
0183     }
0184 
0185     function test_pop_item_arg() {
0186         const { view, zero, one, two } = createViewWith3Items();
0187 
0188         compare(view.pop(one), two);
0189         compare(view.count, 2);
0190         compare(view.pop(zero), one);
0191         compare(view.count, 1);
0192     }
0193 
0194     function test_pop_index_arg() {
0195         const { view, zero, one, two } = createViewWith3Items();
0196 
0197         compare(view.pop(1), two);
0198         compare(view.count, 2);
0199         compare(view.pop(-1), zero);
0200         compare(view.count, 0);
0201     }
0202 
0203     function test_pop_no_args() {
0204         const { view, zero, one, two } = createViewWith3Items();
0205 
0206         compare(view.pop(), two);
0207         compare(view.pop(), one);
0208         compare(view.pop(), zero);
0209         compare(view.pop(), null);
0210     }
0211 
0212     function test_move() {
0213         const { view, zero, one, two } = createViewWith3Items();
0214 
0215         compare(view.contentChildren.length, 3);
0216         compare(view.contentChildren[0], zero);
0217         compare(view.contentChildren[2], two);
0218 
0219         view.moveItem(0, 2);
0220 
0221         compare(view.contentChildren[0], one);
0222         compare(view.contentChildren[1], two);
0223         compare(view.contentChildren[2], zero);
0224 
0225         // TODO: test currentIndex adjustments
0226     }
0227 
0228     function test_insert_null() {
0229         const { view } = createViewWith3Items();
0230         view.insertItem(0, null);
0231         compare(view.count, 3);
0232     }
0233 
0234     function test_insert_duplicate() {
0235         const { view, one: target } = createViewWith3Items();
0236         view.insertItem(0, target);
0237         compare(view.count, 3);
0238     }
0239 
0240     function test_insert_leading() {
0241         const { view, zero, one, two } = createViewWith3Items();
0242         const item = createTemporaryObject(emptyItemPageComponent, this, { objectName: "item" });
0243         view.insertItem(0, item);
0244         compare(view.count, 4)
0245         compare(view.contentChildren, [item, zero, one, two]);
0246     }
0247 
0248     function test_insert_trailing() {
0249         const { view, zero, one, two } = createViewWith3Items();
0250         const item = createTemporaryObject(emptyItemPageComponent, this, { objectName: "item" });
0251         view.insertItem(view.count, item);
0252         compare(view.count, 4)
0253         compare(view.contentChildren, [zero, one, two, item]);
0254     }
0255 
0256     function test_insert_middle() {
0257         const { view, zero, one, two } = createViewWith3Items();
0258         const item = createTemporaryObject(emptyItemPageComponent, this, { objectName: "item" });
0259         view.insertItem(2, item);
0260         compare(view.count, 4)
0261         compare(view.contentChildren, [zero, one, item, two]);
0262     }
0263 
0264     function test_replace_middle() {
0265         const { view, zero, one, two } = createViewWith3Items();
0266         const item = createTemporaryObject(emptyItemPageComponent, this, { objectName: "item" });
0267         view.replaceItem(1, item);
0268         compare(view.count, 3)
0269         compare(view.contentChildren, [zero, item, two]);
0270     }
0271 
0272     function test_attached_index() {
0273         const { view, zero, one, two } = createViewWith3Items();
0274 
0275         compare(zero.Kirigami.ColumnView.index, 0);
0276         compare(one.Kirigami.ColumnView.index, 1);
0277         compare(two.Kirigami.ColumnView.index, 2);
0278     }
0279 
0280     component Filler : Rectangle {
0281         z: 1
0282         opacity: 0.2
0283         color: "#1EA8F7"
0284         border.color: "black"
0285         border.width: 1
0286         radius: 11
0287         implicitWidth: 100
0288         height: parent.height
0289     }
0290 
0291     component Page : Rectangle {
0292         id: page
0293 
0294         z: 0
0295         opacity: 0.2
0296         color: "#CF271C"
0297         border.color: "black"
0298         border.width: 1
0299         radius: 11
0300         height: parent.height
0301 
0302         MouseArea {
0303             anchors.fill: parent
0304             onClicked: mouse => {
0305                 page.Kirigami.ColumnView.view.currentIndex = page.Kirigami.ColumnView.index;
0306             }
0307         }
0308     }
0309 
0310     Component {
0311         id: clippingColumnViewComponent
0312         Row {
0313             readonly property Kirigami.ColumnView columnView: columnView
0314 
0315             width: 300
0316             height: 100
0317 
0318             Filler {}
0319             Kirigami.ColumnView {
0320                 id: columnView
0321 
0322                 height: 100
0323                 width: 100
0324 
0325                 columnWidth: 80
0326                 scrollDuration: 0
0327 
0328                 Page {}
0329                 Page {}
0330                 Page {}
0331             }
0332             Filler {}
0333         }
0334     }
0335 
0336     function test_clicks_outside() {
0337         const layout = createTemporaryObject(clippingColumnViewComponent, this);
0338         const { columnView } = layout;
0339         compare(columnView.count, 3);
0340         waitForPolish(columnView);
0341 
0342         mouseClick(layout);
0343         compare(columnView.currentIndex, 0);
0344 
0345         mouseClick(layout, 250); // center of trailing filler
0346         compare(columnView.currentIndex, 0);
0347 
0348         mouseClick(layout, 50); // center of leading filler
0349         compare(columnView.currentIndex, 0);
0350 
0351         mouseClick(layout, 190); // where the next page begins
0352         compare(columnView.currentIndex, 1);
0353 
0354         mouseClick(layout, 190); // where the next page begins
0355         compare(columnView.currentIndex, 2);
0356 
0357         mouseClick(layout, 50);
0358         compare(columnView.currentIndex, 2); // does not move
0359         columnView.clip = false;
0360         mouseClick(layout, 50);
0361         compare(columnView.currentIndex, 1); // moves
0362     }
0363 }