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 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863
|
// Copyright (C) 2017 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
import QtQuick
import QtTest
import QtQuick.Controls
import Qt.test.controls
TestCase {
id: testCase
width: 200
height: 200
visible: true
when: windowShown
name: "SwipeDelegate"
readonly property int dragDistance: Math.max(20, Application.styleHints.startDragDistance + 5)
Component {
id: backgroundFillComponent
SwipeDelegate {
background: Item { anchors.fill: parent }
}
}
Component {
id: backgroundCenterInComponent
SwipeDelegate {
background: Item { anchors.centerIn: parent }
}
}
Component {
id: backgroundLeftComponent
SwipeDelegate {
background: Item { anchors.left: parent.left }
}
}
Component {
id: backgroundRightComponent
SwipeDelegate {
background: Item { anchors.right: parent.right }
}
}
Component {
id: contentItemFillComponent
SwipeDelegate {
contentItem: Item { anchors.fill: parent }
}
}
Component {
id: contentItemCenterInComponent
SwipeDelegate {
contentItem: Item { anchors.centerIn: parent }
}
}
Component {
id: contentItemLeftComponent
SwipeDelegate {
contentItem: Item { anchors.left: parent.left }
}
}
Component {
id: contentItemRightComponent
SwipeDelegate {
contentItem: Item { anchors.right: parent.right }
}
}
function test_horizontalAnchors_data() {
return [
{ tag: "background, fill", component: backgroundFillComponent, itemName: "background", warningLocation: ":22:25" },
{ tag: "background, centerIn", component: backgroundCenterInComponent, itemName: "background", warningLocation: ":29:25" },
{ tag: "background, left", component: backgroundLeftComponent, itemName: "background", warningLocation: ":36:25" },
{ tag: "background, right", component: backgroundRightComponent, itemName: "background", warningLocation: ":43:25" },
{ tag: "contentItem, fill", component: contentItemFillComponent, itemName: "contentItem", warningLocation: ":50:26" },
{ tag: "contentItem, centerIn", component: contentItemCenterInComponent, itemName: "contentItem", warningLocation: ":57:26" },
{ tag: "contentItem, left", component: contentItemLeftComponent, itemName: "contentItem", warningLocation: ":64:26" },
{ tag: "contentItem, right", component: contentItemRightComponent, itemName: "contentItem", warningLocation: ":71:26" }
];
}
function test_horizontalAnchors(data) {
let warningMessage = Qt.resolvedUrl("tst_swipedelegate.qml") + data.warningLocation
+ ": QML QQuickItem: SwipeDelegate: cannot use horizontal anchors with " + data.itemName + "; unable to layout the item."
ignoreWarning(warningMessage);
let control = createTemporaryObject(data.component, testCase);
verify(control.contentItem);
}
Component {
id: greenLeftComponent
Rectangle {
objectName: "leftItem"
anchors.fill: parent
color: "green"
}
}
Component {
id: redRightComponent
Rectangle {
objectName: "rightItem"
anchors.fill: parent
color: "red"
}
}
Component {
id: swipeDelegateComponent
SwipeDelegate {
id: swipeDelegate
text: "SwipeDelegate"
width: 150
swipe.left: greenLeftComponent
swipe.right: redRightComponent
}
}
Component {
id: signalSpyComponent
SignalSpy {}
}
Component {
id: itemComponent
Item {}
}
// Assumes that the delegate is smaller than the width of the control.
function swipe(control, from, to) {
// Sanity check.
compare(control.swipe.position, from);
let distance = (to - from) * control.width;
mousePress(control, control.width / 2, control.height / 2, Qt.LeftButton);
mouseMove(control, control.width / 2 + distance, control.height / 2);
mouseRelease(control, control.width / 2 + distance, control.height / 2, Qt.LeftButton);
compare(control.swipe.position, to, "Expected swipe.position to be " + to
+ " after swiping from " + from + ", but it's " + control.swipe.position);
if (control.swipe.position === -1.0) {
if (control.swipe.right)
verify(control.swipe.rightItem);
else if (control.swipe.behind)
verify(control.swipe.behindItem);
} else if (control.swipe.position === 1.0) {
if (control.swipe.left)
verify(control.swipe.leftItem);
else if (control.swipe.behind)
verify(control.swipe.behindItem);
}
}
function test_settingDelegates() {
let control = createTemporaryObject(swipeDelegateComponent, testCase);
verify(control);
ignoreWarning(/QML SwipeDelegate: cannot set both behind and left\/right properties/)
control.swipe.behind = itemComponent;
// Shouldn't be any warnings when unsetting delegates.
control.swipe.left = null;
compare(control.swipe.leftItem, null);
// right is still set.
ignoreWarning(/QML SwipeDelegate: cannot set both behind and left\/right properties/)
control.swipe.behind = itemComponent;
control.swipe.right = null;
compare(control.swipe.rightItem, null);
control.swipe.behind = itemComponent;
ignoreWarning(/QML SwipeDelegate: cannot set both behind and left\/right properties/)
control.swipe.left = itemComponent;
ignoreWarning(/QML SwipeDelegate: cannot set both behind and left\/right properties/)
control.swipe.right = itemComponent;
control.swipe.behind = null;
control.swipe.left = greenLeftComponent;
control.swipe.right = redRightComponent;
// Test that the user is warned when attempting to set or unset left or
// right item while they're exposed.
// First, try the left item.
swipe(control, 0.0, 1.0);
let oldLeft = control.swipe.left;
let oldLeftItem = control.swipe.leftItem;
ignoreWarning(/QML SwipeDelegate: left\/right\/behind properties may only be set when swipe.position is 0/)
control.swipe.left = null;
compare(control.swipe.left, oldLeft);
compare(control.swipe.leftItem, oldLeftItem);
// Try the same thing with the right item.
swipe(control, 1.0, -1.0);
let oldRight = control.swipe.right;
let oldRightItem = control.swipe.rightItem;
ignoreWarning(/QML SwipeDelegate: left\/right\/behind properties may only be set when swipe.position is 0/)
control.swipe.right = null;
compare(control.swipe.right, oldRight);
compare(control.swipe.rightItem, oldRightItem);
// Return to the default position.
swipe(control, -1.0, 0.0);
tryCompare(control.background, "x", 0, 1000);
// Try the same thing with the behind item.
control.swipe.left = null;
verify(!control.swipe.left);
verify(!control.swipe.leftItem);
control.swipe.right = null;
verify(!control.swipe.right);
verify(!control.swipe.rightItem);
control.swipe.behind = greenLeftComponent;
verify(control.swipe.behind);
verify(!control.swipe.behindItem);
swipe(control, 0.0, 1.0);
let oldBehind = control.swipe.behind;
let oldBehindItem = control.swipe.behindItem;
ignoreWarning(/QML SwipeDelegate: left\/right\/behind properties may only be set when swipe.position is 0/)
control.swipe.behind = null;
compare(control.swipe.behind, oldBehind);
compare(control.swipe.behindItem, oldBehindItem);
}
function init() {
failOnWarning(/.?/)
}
function test_defaults() {
let control = createTemporaryObject(swipeDelegateComponent, testCase);
verify(control);
compare(control.baselineOffset, control.contentItem.y + control.contentItem.baselineOffset);
compare(control.swipe.position, 0);
verify(!control.pressed);
verify(!control.swipe.complete);
}
SignalSequenceSpy {
id: mouseSignalSequenceSpy
signals: ["pressed", "released", "canceled", "clicked", "doubleClicked", "pressedChanged", "pressAndHold"]
}
function test_swipe() {
let control = createTemporaryObject(swipeDelegateComponent, testCase);
verify(control);
let overDragDistance = Math.round(dragDistance * 1.1);
let completedSpy = signalSpyComponent.createObject(control, { target: control.swipe, signalName: "completed" });
verify(completedSpy);
verify(completedSpy.valid);
let openedSpy = signalSpyComponent.createObject(control, { target: control.swipe, signalName: "opened" });
verify(openedSpy);
verify(openedSpy.valid);
let closedSpy = signalSpyComponent.createObject(control, { target: control.swipe, signalName: "closed" });
verify(closedSpy);
verify(closedSpy.valid);
mouseSignalSequenceSpy.target = control;
mouseSignalSequenceSpy.expectedSequence = [["pressedChanged", { "pressed": true }], "pressed"];
mousePress(control, control.width / 2, control.height / 2);
verify(control.pressed);
compare(control.swipe.position, 0.0);
verify(!control.swipe.complete);
compare(completedSpy.count, 0);
compare(openedSpy.count, 0);
compare(closedSpy.count, 0);
verify(mouseSignalSequenceSpy.success);
verify(!control.swipe.leftItem);
verify(!control.swipe.rightItem);
// Drag to the right so that leftItem is created and visible.
mouseMove(control, control.width / 2 + overDragDistance, control.height / 2);
verify(control.pressed);
compare(control.swipe.position, overDragDistance / control.width);
verify(!control.swipe.complete);
compare(completedSpy.count, 0);
compare(openedSpy.count, 0);
compare(closedSpy.count, 0);
verify(control.swipe.leftItem);
verify(control.swipe.leftItem.visible);
compare(control.swipe.leftItem.parent, control);
compare(control.swipe.leftItem.objectName, "leftItem");
verify(!control.swipe.rightItem);
// Go back to 0.
mouseMove(control, control.width / 2, control.height / 2);
verify(control.pressed);
compare(control.swipe.position, 0.0);
verify(!control.swipe.complete);
compare(completedSpy.count, 0);
compare(openedSpy.count, 0);
compare(closedSpy.count, 0);
verify(control.swipe.leftItem);
verify(control.swipe.leftItem.visible);
compare(control.swipe.leftItem.parent, control);
compare(control.swipe.leftItem.objectName, "leftItem");
verify(!control.swipe.rightItem);
// Try the other direction. The right item should be created and visible,
// and the left item should be hidden.
mouseMove(control, control.width / 2 - overDragDistance, control.height / 2);
verify(control.pressed);
compare(control.swipe.position, -overDragDistance / control.width);
verify(!control.swipe.complete);
compare(completedSpy.count, 0);
compare(openedSpy.count, 0);
compare(closedSpy.count, 0);
verify(control.swipe.leftItem);
verify(!control.swipe.leftItem.visible);
verify(control.swipe.rightItem);
verify(control.swipe.rightItem.visible);
compare(control.swipe.rightItem.parent, control);
compare(control.swipe.rightItem.objectName, "rightItem");
// Now release outside the right edge of the control.
mouseMove(control, control.width * 1.1, control.height / 2);
verify(control.pressed);
compare(control.swipe.position, 0.6);
verify(!control.swipe.complete);
compare(completedSpy.count, 0);
compare(openedSpy.count, 0);
compare(closedSpy.count, 0);
verify(control.swipe.leftItem);
verify(control.swipe.leftItem.visible);
verify(control.swipe.rightItem);
verify(!control.swipe.rightItem.visible);
mouseSignalSequenceSpy.expectedSequence = [["pressedChanged", { "pressed": false }], "canceled"];
mouseRelease(control, control.width / 2, control.height / 2);
verify(!control.pressed);
tryCompare(control.swipe, "position", 1.0);
tryCompare(control.swipe, "complete", true);
compare(completedSpy.count, 1);
compare(openedSpy.count, 1);
compare(closedSpy.count, 0);
verify(mouseSignalSequenceSpy.success);
verify(control.swipe.leftItem);
verify(control.swipe.leftItem.visible);
verify(control.swipe.rightItem);
verify(!control.swipe.rightItem.visible);
tryCompare(control.contentItem, "x", control.width + control.leftPadding);
// Swiping from the right and releasing early should return position to 1.0.
mouseSignalSequenceSpy.expectedSequence = [["pressedChanged", { "pressed": true }], "pressed"];
mousePress(control, control.width / 2, control.height / 2);
verify(control.pressed);
compare(control.swipe.position, 1.0);
// complete should still be true, because we haven't moved yet, and hence
// haven't started grabbing behind's mouse events.
verify(control.swipe.complete);
compare(completedSpy.count, 1);
compare(openedSpy.count, 1);
compare(closedSpy.count, 0);
verify(mouseSignalSequenceSpy.success);
mouseMove(control, control.width / 2 - overDragDistance, control.height / 2);
verify(control.pressed);
verify(!control.swipe.complete);
compare(completedSpy.count, 1);
compare(openedSpy.count, 1);
compare(closedSpy.count, 0);
compare(control.swipe.position, 1.0 - overDragDistance / control.width);
// Since we went over the drag distance, we should expect canceled() to be emitted.
mouseSignalSequenceSpy.expectedSequence = [["pressedChanged", { "pressed": false }], "canceled"];
mouseRelease(control, control.width * 0.4, control.height / 2);
verify(!control.pressed);
tryCompare(control.swipe, "position", 1.0);
tryCompare(control.swipe, "complete", true);
compare(completedSpy.count, 2);
compare(openedSpy.count, 2);
compare(closedSpy.count, 0);
verify(mouseSignalSequenceSpy.success);
tryCompare(control.contentItem, "x", control.width + control.leftPadding);
// Swiping from the right and releasing should return contents to default position.
mouseSignalSequenceSpy.expectedSequence = [["pressedChanged", { "pressed": true }], "pressed"];
mousePress(control, control.width / 2, control.height / 2);
verify(control.pressed);
compare(control.swipe.position, 1.0);
verify(control.swipe.complete);
compare(completedSpy.count, 2);
compare(openedSpy.count, 2);
compare(closedSpy.count, 0);
verify(mouseSignalSequenceSpy.success);
mouseMove(control, control.width * -0.1, control.height / 2);
verify(control.pressed);
verify(!control.swipe.complete);
compare(completedSpy.count, 2);
compare(openedSpy.count, 2);
compare(closedSpy.count, 0);
compare(control.swipe.position, 0.4);
mouseSignalSequenceSpy.expectedSequence = [["pressedChanged", { "pressed": false }], "canceled"];
mouseRelease(control, control.width * -0.1, control.height / 2);
verify(!control.pressed);
tryCompare(control.swipe, "position", 0.0);
verify(!control.swipe.complete);
compare(completedSpy.count, 2);
compare(openedSpy.count, 2);
tryCompare(closedSpy, "count", 1);
verify(mouseSignalSequenceSpy.success);
tryCompare(control.contentItem, "x", control.leftPadding);
}
function test_swipeVelocity_data() {
return [
{ tag: "positive velocity", direction: 1 },
{ tag: "negative velocity", direction: -1 }
];
}
function test_swipeVelocity(data) {
skip("QTBUG-52003");
let control = createTemporaryObject(swipeDelegateComponent, testCase);
verify(control);
let distance = Math.round(dragDistance * 1.1);
if (distance >= control.width / 2)
skip("This test requires a startDragDistance that is less than half the width of the control");
distance *= data.direction;
mouseSignalSequenceSpy.target = control;
mouseSignalSequenceSpy.expectedSequence = [["pressedChanged", { "pressed": true }], "pressed"];
mousePress(control, control.width / 2, control.height / 2);
verify(control.pressed);
compare(control.swipe.position, 0.0);
verify(!control.swipe.complete);
verify(mouseSignalSequenceSpy.success);
verify(!control.swipe.leftItem);
verify(!control.swipe.rightItem);
// Swipe quickly to the side over a distance that is longer than the drag threshold,
// quicker than the expose velocity threshold, but shorter than the halfway mark.
mouseMove(control, control.width / 2 + distance, control.height / 2);
verify(control.pressed);
compare(control.swipe.position, distance / control.width);
verify(control.swipe.position < 0.5);
verify(!control.swipe.complete);
let expectedVisibleItem;
let expectedVisibleObjectName;
let expectedHiddenItem;
let expectedContentItemX;
if (distance > 0) {
expectedVisibleObjectName = "leftItem";
expectedVisibleItem = control.swipe.leftItem;
expectedHiddenItem = control.swipe.rightItem;
expectedContentItemX = control.width + control.leftPadding;
} else {
expectedVisibleObjectName = "rightItem";
expectedVisibleItem = control.swipe.rightItem;
expectedHiddenItem = control.swipe.leftItem;
expectedContentItemX = -control.width + control.leftPadding;
}
verify(expectedVisibleItem);
verify(expectedVisibleItem.visible);
compare(expectedVisibleItem.parent, control);
compare(expectedVisibleItem.objectName, expectedVisibleObjectName);
verify(!expectedHiddenItem);
mouseSignalSequenceSpy.expectedSequence = [["pressedChanged", { "pressed": false }], "released", "clicked"];
// Add a delay to ensure that the release event doesn't happen too quickly,
// and hence that the second timestamp isn't zero (can happen with e.g. release builds).
mouseRelease(control, control.width / 2 + distance, control.height / 2, Qt.LeftButton, Qt.NoModifier, 30);
verify(!control.pressed);
compare(control.swipe.position, data.direction);
verify(control.swipe.complete);
verify(mouseSignalSequenceSpy.success);
verify(expectedVisibleItem);
verify(expectedVisibleItem.visible);
verify(!expectedHiddenItem);
tryCompare(control.contentItem, "x", expectedContentItemX);
}
Component {
id: swipeDelegateWithButtonComponent
SwipeDelegate {
text: "SwipeDelegate"
width: 150
swipe.right: Button {
// make the button a bit shorter than the delegate, so
// that we're able to release the mouse outside of it
width: parent.width - 4
height: parent.height
text: "Boo!"
}
}
}
function test_eventsToLeftAndRight() {
let control = createTemporaryObject(swipeDelegateWithButtonComponent, testCase);
verify(control);
let closedSpy = signalSpyComponent.createObject(control, { target: control.swipe, signalName: "closed" });
verify(closedSpy);
verify(closedSpy.valid);
// The button should be pressed instead of the SwipeDelegate.
mouseDrag(control, control.width / 2, 0, -control.width, 0);
// Mouse has been released by this stage.
verify(!control.pressed);
compare(control.swipe.position, -1.0);
verify(control.swipe.rightItem);
verify(control.swipe.rightItem.visible);
compare(control.swipe.rightItem.parent, control);
let buttonPressedSpy = signalSpyComponent.createObject(control, { target: control.swipe.rightItem, signalName: "pressed" });
verify(buttonPressedSpy);
verify(buttonPressedSpy.valid);
let buttonReleasedSpy = signalSpyComponent.createObject(control, { target: control.swipe.rightItem, signalName: "released" });
verify(buttonReleasedSpy);
verify(buttonReleasedSpy.valid);
let buttonClickedSpy = signalSpyComponent.createObject(control, { target: control.swipe.rightItem, signalName: "clicked" });
verify(buttonClickedSpy);
verify(buttonClickedSpy.valid);
// Now press the button.
mousePress(control, control.width / 2, control.height / 2);
verify(!control.pressed);
let button = control.swipe.rightItem;
verify(button.pressed);
compare(buttonPressedSpy.count, 1);
compare(buttonReleasedSpy.count, 0);
compare(buttonClickedSpy.count, 0);
mouseRelease(control, control.width / 2, control.height / 2);
verify(!button.pressed);
compare(buttonPressedSpy.count, 1);
compare(buttonReleasedSpy.count, 1);
compare(buttonClickedSpy.count, 1);
// Returning back to a position of 0 and pressing on the control should
// result in the control being pressed.
mouseDrag(control, control.width / 2, 0, control.width * 0.6, 0);
tryCompare(closedSpy, "count", 1);
compare(control.swipe.position, 0);
mousePress(control, control.width / 2, control.height / 2);
verify(control.pressed);
verify(!button.pressed);
mouseRelease(control, control.width / 2, control.height / 2);
verify(!control.pressed);
// Try to press the button again, but drag and release outside of it.
// This should not click the button.
buttonClickedSpy.clear();
// Open the control, and press the button
mouseDrag(control, control.width / 2, control.height / 2, -control.width, 0);
mousePress(control);
verify(button.pressed);
// Drag the mouse outside the button, and release
mouseMove(control, control.width - 2, control.height / 2, -1, Qt.LeftButton);
mouseRelease(control);
verify(!button.pressed);
verify(!button.hovered);
// This should not be a click
compare(buttonClickedSpy.count, 0);
}
function test_mouseButtons() {
let control = createTemporaryObject(swipeDelegateComponent, testCase);
verify(control);
// click
mouseSignalSequenceSpy.target = control;
mouseSignalSequenceSpy.expectedSequence = [["pressedChanged", { "pressed": true }], "pressed"];
mousePress(control, control.width / 2, control.height / 2, Qt.LeftButton);
compare(control.pressed, true);
verify(mouseSignalSequenceSpy.success);
mouseSignalSequenceSpy.expectedSequence = [["pressedChanged", { "pressed": false }], "released", "clicked"];
mouseRelease(control, control.width / 2, control.height / 2, Qt.LeftButton);
compare(control.pressed, false);
verify(mouseSignalSequenceSpy.success);
// right button
mouseSignalSequenceSpy.expectedSequence = [];
mousePress(control, control.width / 2, control.height / 2, Qt.RightButton);
compare(control.pressed, false);
mouseRelease(control, control.width / 2, control.height / 2, Qt.RightButton);
compare(control.pressed, false);
verify(mouseSignalSequenceSpy.success);
// double click
mouseSignalSequenceSpy.expectedSequence = [
["pressedChanged", { "pressed": true }],
"pressed",
["pressedChanged", { "pressed": false }],
"released",
"clicked",
["pressedChanged", { "pressed": true }],
"pressed",
"doubleClicked",
["pressedChanged", { "pressed": false }],
"released"
];
mouseDoubleClickSequence(control, control.width / 2, control.height / 2, Qt.LeftButton);
verify(mouseSignalSequenceSpy.success);
// press and hold
let pressAndHoldSpy = signalSpyComponent.createObject(control, { target: control, signalName: "pressAndHold" });
verify(pressAndHoldSpy);
verify(pressAndHoldSpy.valid);
mouseSignalSequenceSpy.expectedSequence = [
["pressedChanged", { "pressed": true }],
"pressed",
"pressAndHold",
["pressedChanged", { "pressed": false }],
"released"
];
mousePress(control);
compare(control.pressed, true);
tryCompare(pressAndHoldSpy, "count", 1);
mouseRelease(control);
compare(control.pressed, false);
verify(mouseSignalSequenceSpy.success);
}
Component {
id: removableDelegatesComponent
ListView {
id: listView
width: 100
height: 120
property int rotation: 0
transform: [
Rotation {
angle: rotation
origin.x: 0
origin.y: 0
},
Translate {
x: (rotation === 90) ? parent.width : 0
y: 0
}
]
model: ListModel {
ListElement { name: "Apple" }
ListElement { name: "Orange" }
ListElement { name: "Pear" }
}
delegate: SwipeDelegate {
id: rootDelegate
text: modelData
width: listView.width
property alias removeAnimation: onRemoveAnimation
ListView.onRemove: onRemoveAnimation.start()
SequentialAnimation {
id: onRemoveAnimation
PropertyAction {
target: rootDelegate
property: "ListView.delayRemove"
value: true
}
NumberAnimation {
target: rootDelegate
property: "height"
to: 0
easing.type: Easing.InOutQuad
}
PropertyAction {
target: rootDelegate;
property: "ListView.delayRemove";
value: false
}
}
swipe.left: Rectangle {
objectName: "rectangle"
color: SwipeDelegate.pressed ? "#333" : "#444"
anchors.fill: parent
SwipeDelegate.onClicked: listView.model.remove(index)
Label {
objectName: "label"
text: "Remove"
color: "white"
anchors.centerIn: parent
}
}
}
}
}
function test_removableDelegates_data() {
return [
{ tag: "mouse", touch: false },
{ tag: "touch", touch: true },
{ tag: "mouse_rotation_90", touch: false, rotation: 90 },
{ tag: "touch_rotation_90", touch: true, rotation: 90 },
]
}
function test_removableDelegates(data) {
let listView = createTemporaryObject(removableDelegatesComponent, testCase);
verify(listView);
compare(listView.count, 3);
if (data.rotation)
listView.rotation = data.rotation;
let touch = data.touch ? touchEvent(listView) : null
// Expose the remove button.
let firstItem = listView.itemAt(0, 0);
if (data.touch)
touch.press(0, listView, firstItem.width / 2, firstItem.height / 2).commit()
else
mousePress(listView, firstItem.width / 2, firstItem.height / 2);
verify(firstItem.pressed);
compare(firstItem.swipe.position, 0.0);
verify(!firstItem.swipe.complete);
verify(!firstItem.swipe.leftItem);
if (data.touch)
touch.move(0, listView, firstItem.width * 1.1, firstItem.height / 2).commit()
else
mouseMove(listView, firstItem.width * 1.1, firstItem.height / 2);
verify(firstItem.pressed);
compare(firstItem.swipe.position, 0.6);
verify(!firstItem.swipe.complete);
verify(firstItem.swipe.leftItem);
verify(!firstItem.swipe.leftItem.SwipeDelegate.pressed);
if (data.touch)
touch.release(0, listView, firstItem.width / 2, firstItem.height / 2).commit()
else
mouseRelease(listView, firstItem.width / 2, firstItem.height / 2);
verify(!firstItem.pressed);
tryCompare(firstItem.swipe, "position", 1.0);
tryCompare(firstItem.swipe, "complete", true);
compare(listView.count, 3);
// Wait for it to settle down.
tryCompare(firstItem.contentItem, "x", firstItem.leftPadding + firstItem.width);
let leftClickedSpy = signalSpyComponent.createObject(firstItem.swipe.leftItem,
{ target: firstItem.swipe.leftItem.SwipeDelegate, signalName: "clicked" });
verify(leftClickedSpy);
verify(leftClickedSpy.valid);
// Click the left item to remove the delegate from the list.
let contentItemX = firstItem.contentItem.x;
// press
if (data.touch)
touch.press(0, listView, firstItem.width / 2, firstItem.height / 2).commit()
else
mousePress(listView, firstItem.width / 2, firstItem.height / 2);
verify(firstItem.swipe.leftItem.SwipeDelegate.pressed);
compare(leftClickedSpy.count, 0);
verify(firstItem.pressed);
// simulate inadvertent movement which can easily happen
if (data.touch)
touch.move(0, listView, firstItem.width / 2 + 1, firstItem.height / 2).commit()
else
mouseMove(listView, firstItem.width / 2 + 1, firstItem.height / 2);
// release
if (data.touch)
touch.release(0, listView, firstItem.width / 2, firstItem.height / 2).commit()
else
mouseRelease(listView, firstItem.width / 2, firstItem.height / 2);
verify(!firstItem.swipe.leftItem.SwipeDelegate.pressed);
compare(leftClickedSpy.count, 1);
verify(!firstItem.pressed);
leftClickedSpy = null;
tryCompare(firstItem.removeAnimation, "running", true);
// There was a bug where the resizeContent() would be called because the height
// of the control was changing due to the animation. contentItem would then
// change x position and hence be visible when it shouldn't be.
verify(firstItem.removeAnimation.running);
while (1) {
wait(10)
if (firstItem && firstItem.removeAnimation && firstItem.removeAnimation.running)
compare(firstItem.contentItem.x, contentItemX);
else
break;
}
compare(listView.count, 2);
}
Component {
id: leadingTrailingXComponent
SwipeDelegate {
id: delegate
width: 150
text: "SwipeDelegate"
swipe.left: Rectangle {
x: delegate.background.x - width
width: delegate.width
height: delegate.height
color: "green"
}
swipe.right: Rectangle {
x: delegate.background.x + delegate.background.width
width: delegate.width
height: delegate.height
color: "red"
}
}
}
Component {
id: leadingTrailingAnchorsComponent
SwipeDelegate {
id: delegate
width: 150
text: "SwipeDelegate"
swipe.left: Rectangle {
anchors.right: delegate.background.left
width: delegate.width
height: delegate.height
color: "green"
}
swipe.right: Rectangle {
anchors.left: delegate.background.right
width: delegate.width
height: delegate.height
color: "red"
}
}
}
function test_leadingTrailing_data() {
return [
{ tag: "x", component: leadingTrailingXComponent },
{ tag: "anchors", component: leadingTrailingAnchorsComponent },
];
}
function test_leadingTrailing(data) {
let control = createTemporaryObject(data.component, testCase);
verify(control);
mousePress(control, control.width / 2, control.height / 2, Qt.LeftButton);
mouseMove(control, control.width, control.height / 2);
verify(control.swipe.leftItem);
compare(control.swipe.leftItem.x, -control.width / 2);
mouseRelease(control, control.width / 2, control.height / 2, Qt.LeftButton);
}
function test_minMaxPosition() {
let control = createTemporaryObject(leadingTrailingXComponent, testCase);
verify(control);
// Should be limited within the range -1.0 to 1.0.
mousePress(control, control.width / 2, control.height / 2, Qt.LeftButton);
mouseMove(control, control.width * 1.5, control.height / 2);
compare(control.swipe.position, 1.0);
mouseMove(control, control.width * 1.6, control.height / 2);
compare(control.swipe.position, 1.0);
mouseMove(control, control.width * -1.6, control.height / 2);
compare(control.swipe.position, -1.0);
mouseRelease(control, control.width / 2, control.height / 2, Qt.LeftButton);
}
Component {
id: emptySwipeDelegateComponent
SwipeDelegate {
text: "SwipeDelegate"
width: 150
}
}
Component {
id: smallLeftComponent
Rectangle {
width: 80
height: 40
color: "green"
}
}
// swipe.position should be scaled to the width of the relevant delegate,
// and it shouldn't be possible to drag past the delegate (so that content behind the control is visible).
function test_delegateWidth() {
let control = createTemporaryObject(emptySwipeDelegateComponent, testCase);
verify(control);
control.swipe.left = smallLeftComponent;
// Ensure that the position is scaled to the width of the currently visible delegate.
let overDragDistance = Math.round(dragDistance * 1.1);
mousePress(control, control.width / 2, control.height / 2, Qt.LeftButton);
mouseMove(control, control.width / 2 + overDragDistance, control.height / 2);
verify(control.swipe.leftItem);
compare(control.swipe.position, overDragDistance / control.swipe.leftItem.width);
mouseMove(control, control.width / 2 + control.swipe.leftItem.width, control.height / 2);
compare(control.swipe.position, 1.0);
// Ensure that it's not possible to drag past the (left) delegate.
mouseMove(control, control.width / 2 + control.swipe.leftItem.width + 1, control.height / 2);
compare(control.swipe.position, 1.0);
// Now release over the right side; the position should be 1.0 and the background
// should be "anchored" to the right side of the left delegate item.
mouseMove(control, control.width / 2 + control.swipe.leftItem.width, control.height / 2);
mouseRelease(control, control.width / 2 + control.swipe.leftItem.width, control.height / 2, Qt.LeftButton);
compare(control.swipe.position, 1.0);
tryCompare(control.background, "x", control.swipe.leftItem.width, 1000);
}
SignalSpy {
id: leftVisibleSpy
signalName: "visibleChanged"
}
SignalSpy {
id: rightVisibleSpy
signalName: "visibleChanged"
}
function test_positionAfterSwipeCompleted() {
let control = createTemporaryObject(swipeDelegateComponent, testCase);
verify(control);
// Ensure that both delegates are constructed.
mousePress(control, 0, control.height / 2, Qt.LeftButton);
mouseMove(control, control.width * 1.1, control.height / 2);
verify(control.swipe.leftItem);
mouseMove(control, control.width * -0.1, control.height / 2);
verify(control.swipe.rightItem);
// Expose the left delegate.
mouseMove(control, control.swipe.leftItem.width, control.height / 2);
mouseRelease(control, control.swipe.leftItem.width, control.height / 2);
verify(control.swipe.complete);
compare(control.swipe.position, 1.0);
leftVisibleSpy.target = control.swipe.leftItem;
rightVisibleSpy.target = control.swipe.rightItem;
// Swipe from right to left without exposing the right item,
// and make sure that the right item never becomes visible
// (and hence that the left item never loses visibility).
mousePress(control, control.swipe.leftItem.width - 1, control.height / 2, Qt.LeftButton);
compare(leftVisibleSpy.count, 0);
compare(rightVisibleSpy.count, 0);
let newX = control.swipe.leftItem.width - Math.round(dragDistance * 1.1) -1;
mouseMove(control, newX, control.height / 2);
compare(leftVisibleSpy.count, 0);
compare(rightVisibleSpy.count, 0);
compare(control.swipe.position, (newX + 1) / control.swipe.leftItem.width);
mouseMove(control, 0, control.height / 2);
compare(control.swipe.position, 1 / control.swipe.leftItem.width);
// Because we move from (width - 1) to 0, so one pixel remains
// Test swiping over a distance that is greater than the width of the left item.
mouseMove(control, -1, control.height / 2);
verify(control.swipe.rightItem);
compare(control.swipe.position, 0);
// Now go back to 1.0.
mouseMove(control, control.swipe.leftItem.width - 1, control.height / 2);
compare(control.swipe.position, 1.0);
tryCompare(control.background, "x", control.swipe.leftItem.width, 1000);
mouseRelease(control, control.swipe.leftItem.width, control.height / 2, Qt.LeftButton);
}
// TODO: this somehow results in the behind item having a negative width
// Component {
// id: behindSwipeDelegateComponent
// SwipeDelegate {
// anchors.centerIn: parent
// swipe.behind: Rectangle {
// onXChanged: print("x changed", x)
// anchors.left: {
// print("anchors.left expression", swipe.position)
// swipe.position < 0 ? parent.background.right : undefined
// }
// anchors.right: {
// print("anchors.right expression", swipe.position)
// swipe.position > 0 ? parent.background.left : undefined
// }
// width: parent.width
// height: parent.height
// color: "green"
// }
// swipe.left: null
// swipe.right: null
// Rectangle {
// anchors.fill: parent
// color: "transparent"
// border.color: "darkorange"
// }
// }
// }
Component {
id: behindSwipeDelegateComponent
SwipeDelegate {
text: "SwipeDelegate"
width: 150
anchors.centerIn: parent
swipe.behind: Rectangle {
x: swipe.position < 0 ? parent.background.x + parent.background.width
: (swipe.position > 0 ? parent.background.x - width : 0)
width: parent.width
height: parent.height
color: "green"
}
swipe.left: null
swipe.right: null
}
}
function test_leadingTrailingBehindItem() {
let control = createTemporaryObject(behindSwipeDelegateComponent, testCase);
verify(control);
swipe(control, 0.0, 1.0);
verify(control.swipe.behindItem.visible);
compare(control.swipe.behindItem.x, control.background.x - control.background.width);
swipe(control, 1.0, -1.0);
verify(control.swipe.behindItem.visible);
compare(control.swipe.behindItem.x, control.background.x + control.background.width);
swipe(control, -1.0, 1.0);
verify(control.swipe.behindItem.visible);
compare(control.swipe.behindItem.x, control.background.x - control.background.width);
// Should be possible to "wrap" with a behind delegate specified.
mousePress(control, control.width / 2, control.height / 2, Qt.LeftButton);
mouseMove(control, control.width / 2 + control.swipe.behindItem.width * 0.8, control.height / 2);
compare(control.swipe.position, -0.2);
mouseRelease(control, control.width / 2 + control.swipe.behindItem.width * 0.8, control.height / 2, Qt.LeftButton);
tryCompare(control.swipe, "position", 0.0);
// Try wrapping the other way.
swipe(control, 0.0, -1.0);
verify(control.swipe.behindItem.visible);
compare(control.swipe.behindItem.x, control.background.x + control.background.width);
mousePress(control, control.width / 2, control.height / 2, Qt.LeftButton);
mouseMove(control, control.width / 2 - control.swipe.behindItem.width * 0.8, control.height / 2);
compare(control.swipe.position, 0.2);
mouseRelease(control, control.width / 2 - control.swipe.behindItem.width * 0.8, control.height / 2, Qt.LeftButton);
tryCompare(control.swipe, "position", 0.0);
}
Component {
id: closeSwipeDelegateComponent
SwipeDelegate {
text: "SwipeDelegate"
width: 150
swipe.right: Rectangle {
color: "green"
width: parent.width
height: parent.height
SwipeDelegate.onClicked: swipe.close()
}
}
}
function test_close() {
let control = createTemporaryObject(closeSwipeDelegateComponent, testCase);
verify(control);
let closedSpy = signalSpyComponent.createObject(control, { target: control.swipe, signalName: "closed" });
verify(closedSpy);
verify(closedSpy.valid);
swipe(control, 0.0, -1.0);
compare(control.swipe.rightItem.visible, true);
// Should animate, so it shouldn't change right away.
compare(control.swipe.rightItem.x, 0);
tryCompare(control.swipe.rightItem, "x", control.background.x + control.background.width);
mousePress(control);
verify(control.swipe.rightItem.SwipeDelegate.pressed);
mouseRelease(control);
verify(!control.swipe.rightItem.SwipeDelegate.pressed);
tryCompare(closedSpy, "count", 1);
compare(control.swipe.position, 0);
// Swiping after closing should work as normal.
swipe(control, 0.0, -1.0);
}
function test_callCloseWhenAlreadyClosed() {
let control = createTemporaryObject(swipeDelegateComponent, testCase)
verify(control)
let closedSpy = signalSpyComponent.createObject(control, { target: control.swipe, signalName: "closed" })
verify(closedSpy)
verify(closedSpy.valid)
// Calling close() when it's already closed should have no effect.
control.swipe.close()
compare(closedSpy.count, 0)
// The game goes for calling close() in response to a click.
control.clicked.connect(function() { control.swipe.close() })
mouseClick(control)
compare(closedSpy.count, 0)
}
// Can't just connect to pressed in QML, because there is a pressed property
// that conflicts with the signal.
Component {
id: swipeDelegateCloseOnPressedComponent
SwipeDelegate {
text: "SwipeDelegate"
width: 150
swipe.right: Rectangle {
objectName: "rightItem"
width: parent.width / 2
height: parent.height
color: "tomato"
}
onPressed: swipe.close()
}
}
/*
We don't want to support closing on pressed(); released() or clicked()
should be used instead. However, calling swipe.close() in response to
a press should still not cause closed() to be emitted.
*/
function test_closeOnPressed() {
let control = createTemporaryObject(swipeDelegateCloseOnPressedComponent, testCase)
verify(control)
swipe(control, 0.0, -1.0)
let closedSpy = signalSpyComponent.createObject(control, { target: control.swipe, signalName: "closed" })
verify(closedSpy)
verify(closedSpy.valid)
mousePress(control, control.width * 0.1)
compare(closedSpy.count, 0)
compare(control.swipe.position, -1.0)
// Simulate a somewhat realistic delay between press and release
// to ensure that the bug is triggered.
wait(100)
mouseRelease(control, control.width * 0.1)
compare(closedSpy.count, 0)
compare(control.swipe.position, -1.0)
}
Component {
id: multiActionSwipeDelegateComponent
SwipeDelegate {
text: "SwipeDelegate"
width: 150
swipe.right: Item {
objectName: "rightItemRoot"
width: parent.width
height: parent.height
property alias firstAction: firstAction
property alias secondAction: secondAction
property int firstClickCount: 0
property int secondClickCount: 0
Row {
anchors.fill: parent
anchors.margins: 5
Rectangle {
id: firstAction
width: parent.width / 2
height: parent.height
color: "tomato"
SwipeDelegate.onClicked: ++firstClickCount
}
Rectangle {
id: secondAction
width: parent.width / 2
height: parent.height
color: "navajowhite"
SwipeDelegate.onClicked: ++secondClickCount
}
}
}
}
}
// Tests that it's possible to have multiple non-interactive items in one delegate
// (e.g. left/right/behind) that can each receive clicks.
function test_multipleClickableActions() {
let control = createTemporaryObject(multiActionSwipeDelegateComponent, testCase);
verify(control);
swipe(control, 0.0, -1.0);
verify(control.swipe.rightItem);
tryCompare(control.swipe, "complete", true);
let firstClickedSpy = signalSpyComponent.createObject(control,
{ target: control.swipe.rightItem.firstAction.SwipeDelegate, signalName: "clicked" });
verify(firstClickedSpy);
verify(firstClickedSpy.valid);
// Clicked within rightItem, but not within an item using the attached properties.
mousePress(control, 2, 2);
compare(control.swipe.rightItem.firstAction.SwipeDelegate.pressed, false);
compare(firstClickedSpy.count, 0);
mouseRelease(control, 2, 2);
compare(control.swipe.rightItem.firstAction.SwipeDelegate.pressed, false);
compare(firstClickedSpy.count, 0);
// Click within the first item.
mousePress(control.swipe.rightItem.firstAction, 0, 0);
compare(control.swipe.rightItem.firstAction.SwipeDelegate.pressed, true);
compare(firstClickedSpy.count, 0);
mouseRelease(control.swipe.rightItem.firstAction, 0, 0);
compare(control.swipe.rightItem.firstAction.SwipeDelegate.pressed, false);
compare(firstClickedSpy.count, 1);
compare(control.swipe.rightItem.firstClickCount, 1);
let secondClickedSpy = signalSpyComponent.createObject(control,
{ target: control.swipe.rightItem.secondAction.SwipeDelegate, signalName: "clicked" });
verify(secondClickedSpy);
verify(secondClickedSpy.valid);
// Click within the second item.
mousePress(control.swipe.rightItem.secondAction, 0, 0);
compare(control.swipe.rightItem.secondAction.SwipeDelegate.pressed, true);
compare(secondClickedSpy.count, 0);
mouseRelease(control.swipe.rightItem.secondAction, 0, 0);
compare(control.swipe.rightItem.secondAction.SwipeDelegate.pressed, false);
compare(secondClickedSpy.count, 1);
compare(control.swipe.rightItem.secondClickCount, 1);
}
// Pressing on a "side action" and then dragging should eventually
// cause the ListView to grab the mouse and start changing its contentY.
// When this happens, it will grab the mouse and hence we must clear
// that action's pressed state so that it doesn't stay pressed after releasing.
function test_dragSideAction() {
let listView = createTemporaryObject(removableDelegatesComponent, testCase);
verify(listView);
let control = listView.itemAt(0, 0);
verify(control);
// Expose the side action.
swipe(control, 0.0, 1.0);
verify(control.swipe.leftItem);
tryCompare(control.swipe, "complete", true);
let pressedSpy = signalSpyComponent.createObject(control,
{ target: control.swipe.leftItem.SwipeDelegate, signalName: "pressedChanged" });
verify(pressedSpy);
verify(pressedSpy.valid);
let movingHorizontallySpy = createTemporaryObject(signalSpyComponent, testCase,
{ target: listView, signalName: "movingHorizontallyChanged" })
verify(movingHorizontallySpy)
verify(movingHorizontallySpy.valid)
let movingVerticallySpy = createTemporaryObject(signalSpyComponent, testCase,
{ target: listView, signalName: "movingVerticallyChanged" })
verify(movingVerticallySpy)
verify(movingVerticallySpy.valid)
let flickingHorizontallySpy = createTemporaryObject(signalSpyComponent, testCase,
{ target: listView, signalName: "flickingHorizontallyChanged" })
verify(flickingHorizontallySpy)
verify(flickingHorizontallySpy.valid)
let flickingVerticallySpy = createTemporaryObject(signalSpyComponent, testCase,
{ target: listView, signalName: "flickingVerticallyChanged" })
verify(flickingVerticallySpy)
verify(flickingVerticallySpy.valid)
// Drag the ListView vertically; its contentY should change.
mouseDrag(listView, 20, 20, 0, listView.height);
compare(pressedSpy.count, 2);
// Wait for it to stop moving.
tryCompare(listView, "flickingVertically", false)
// 2 because it should change to true then false.
compare(movingHorizontallySpy.count, 0)
compare(movingVerticallySpy.count, 2)
compare(flickingHorizontallySpy.count, 0)
compare(flickingVerticallySpy.count, 2)
compare(control.swipe.leftItem.SwipeDelegate.pressed, false);
}
// When the width of a SwipeDelegate changes (as it does upon portrait => landscape
// rotation, for example), the positions of the contentItem and background items
// should be updated accordingly.
function test_contentItemPosOnWidthChanged() {
let control = createTemporaryObject(swipeDelegateComponent, testCase);
verify(control);
swipe(control, 0.0, 1.0);
let oldContentItemX = control.contentItem.x;
let oldBackgroundX = control.background.x;
control.width += 100;
compare(control.contentItem.x, oldContentItemX + 100);
compare(control.background.x, oldBackgroundX + 100);
}
function test_contentItemHeightOnHeightChanged() {
let control = createTemporaryObject(swipeDelegateComponent, testCase);
verify(control);
// Try when swipe.complete is false.
let originalHeight = control.height;
let originalContentItemHeight = control.contentItem.height;
verify(control.height !== 10);
control.height = 10;
compare(control.contentItem.height, control.availableHeight);
verify(control.contentItem.height < originalContentItemHeight);
compare(control.contentItem.y, control.topPadding);
// Try when swipe.complete is true.
control.height = originalHeight;
swipe(control, 0.0, 1.0);
control.height = 10;
compare(control.contentItem.height, control.availableHeight);
verify(control.contentItem.height < originalContentItemHeight);
compare(control.contentItem.y, control.topPadding);
}
function test_releaseOutside_data() {
return [
{ tag: "no delegates", component: emptySwipeDelegateComponent },
{ tag: "delegates", component: swipeDelegateComponent },
];
}
function test_releaseOutside(data) {
let control = createTemporaryObject(data.component, testCase);
verify(control);
// Press and then release below the control.
mouseSignalSequenceSpy.target = control;
mouseSignalSequenceSpy.expectedSequence = [["pressedChanged", { "pressed": true }], "pressed", ["pressedChanged", { "pressed": false }]];
mousePress(control, control.width / 2, control.height / 2, Qt.LeftButton);
mouseMove(control, control.width / 2, control.height + 10);
verify(mouseSignalSequenceSpy.success);
mouseSignalSequenceSpy.expectedSequence = ["canceled"];
mouseRelease(control, control.width / 2, control.height + 10, Qt.LeftButton);
verify(mouseSignalSequenceSpy.success);
// Press and then release to the right of the control.
let hasDelegates = control.swipe.left || control.swipe.right || control.swipe.behind;
mouseSignalSequenceSpy.target = control;
mouseSignalSequenceSpy.expectedSequence = hasDelegates
? [["pressedChanged", { "pressed": true }], "pressed"]
: [["pressedChanged", { "pressed": true }], "pressed", ["pressedChanged", { "pressed": false }]];
mousePress(control, control.width / 2, control.height / 2, Qt.LeftButton);
mouseMove(control, control.width + 10, control.height / 2);
if (hasDelegates)
verify(control.swipe.position > 0);
verify(mouseSignalSequenceSpy.success);
mouseSignalSequenceSpy.expectedSequence = hasDelegates ? [["pressedChanged", { "pressed": false }], "canceled"] : ["canceled"];
mouseRelease(control, control.width + 10, control.height / 2, Qt.LeftButton);
verify(mouseSignalSequenceSpy.success);
}
Component {
id: leftRightWithLabelsComponent
SwipeDelegate {
id: delegate
text: "SwipeDelegate"
width: 150
background.opacity: 0.5
swipe.left: Rectangle {
width: parent.width
height: parent.height
color: SwipeDelegate.pressed ? Qt.darker("green") : "green"
property alias label: label
Label {
id: label
text: "Left"
color: "white"
anchors.margins: 10
anchors.left: parent.left
anchors.verticalCenter: parent.verticalCenter
}
SwipeDelegate.onClicked: delegate.swipe.close()
}
swipe.right: Rectangle {
width: parent.width
height: parent.height
anchors.right: parent.right
color: SwipeDelegate.pressed ? Qt.darker("green") : "red"
property alias label: label
Label {
id: label
text: "Right"
color: "white"
anchors.margins: 10
anchors.right: parent.right
anchors.verticalCenter: parent.verticalCenter
}
SwipeDelegate.onClicked: delegate.swipe.close()
}
}
}
function test_beginSwipeOverRightItem() {
let control = createTemporaryObject(leftRightWithLabelsComponent, testCase);
verify(control);
// Swipe to the left, exposing the right item.
swipe(control, 0.0, -1.0);
// Click to close it and go back to a position of 0.
mouseClick(control);
// TODO: Swipe to the left, with the mouse over the Label in the right item.
// The left item should not become visible at any point.
let rightLabel = control.swipe.rightItem.label;
let overDragDistance = Math.round(dragDistance * 1.1);
mousePress(rightLabel, rightLabel.width / 2, rightLabel.height / 2, Qt.rightButton);
mouseMove(rightLabel, rightLabel.width / 2 - overDragDistance, rightLabel.height / 2);
verify(!control.swipe.leftItem);
mouseRelease(rightLabel, rightLabel.width / 2 - overDragDistance, control.height / 2, Qt.LeftButton);
verify(!control.swipe.leftItem);
}
Component {
id: swipeDelegateDisabledComponent
SwipeDelegate {
id: swipeDelegate
text: "SwipeDelegate"
width: parent.width
height: checked ? implicitHeight * 2 : implicitHeight
checkable: true
swipe.enabled: false
swipe.right: Label {
text: swipeDelegate.checked ? qsTr("Expanded") : qsTr("Collapsed")
width: parent.width
height: parent.height
padding: 12
color: "white"
verticalAlignment: Label.AlignVCenter
horizontalAlignment: Label.AlignRight
}
}
}
function test_swipeEnabled() {
let control = createTemporaryObject(swipeDelegateDisabledComponent, testCase);
mousePress(control, control.width / 2, control.height / 2);
verify(control.pressed);
compare(control.swipe.position, 0.0);
verify(!control.swipe.complete);
verify(!control.swipe.leftItem);
verify(!control.swipe.rightItem);
// It shouldn't be possible to swipe.
let overDragDistance = Math.round(dragDistance * 1.1);
mouseMove(control, control.width / 2 - overDragDistance, control.height / 2);
verify(control.pressed);
compare(control.swipe.position, 0.0);
verify(!control.swipe.complete);
verify(!control.swipe.leftItem);
verify(!control.swipe.rightItem);
// Now move outside the right edge of the control and release.
mouseMove(control, control.width * 1.1, control.height / 2);
verify(control.pressed);
compare(control.swipe.position, 0.0);
verify(!control.swipe.complete);
verify(!control.swipe.leftItem);
verify(!control.swipe.rightItem);
mouseRelease(control, control.width / 2, control.height / 2);
verify(!control.pressed);
compare(control.swipe.position, 0.0);
verify(!control.swipe.complete);
verify(!control.swipe.leftItem);
verify(!control.swipe.rightItem);
// Now enabled swiping so that we can swipe to the left.
control.swipe.enabled = true;
swipe(control, 0, -1);
verify(control.swipe.complete);
// Now that the swipe is complete, disable swiping and then try to swipe again.
// It should stay at its position of -1.
control.swipe.enabled = false;
mousePress(control, control.width / 2, control.height / 2);
verify(control.pressed);
compare(control.swipe.position, -1.0);
mouseMove(control, control.width / 2 + overDragDistance, control.height / 2);
verify(control.pressed);
compare(control.swipe.position, -1.0);
verify(control.swipe.complete);
mouseRelease(control, control.width / 2 + overDragDistance, control.height / 2);
verify(!control.pressed);
compare(control.swipe.position, -1.0);
verify(control.swipe.complete);
}
function test_side() {
compare(SwipeDelegate.Left, 1.0);
compare(SwipeDelegate.Right, -1.0);
}
function test_open_side_data() {
return [
{ tag: "left", side: SwipeDelegate.Left, position: 1, complete: true, left: greenLeftComponent, right: null, behind: null },
{ tag: "right", side: SwipeDelegate.Right, position: -1, complete: true, left: null, right: redRightComponent, behind: null },
{ tag: "behind,left", side: SwipeDelegate.Left, position: 1, complete: true, left: null, right: null, behind: greenLeftComponent },
{ tag: "behind,right", side: SwipeDelegate.Right, position: -1, complete: true, left: null, right: null, behind: redRightComponent },
{ tag: "left,behind", side: SwipeDelegate.Left, position: 1, complete: true, left: null, right: null, behind: greenLeftComponent },
{ tag: "right,behind", side: SwipeDelegate.Right, position: -1, complete: true, left: null, right: null, behind: redRightComponent },
{ tag: "left,null", side: SwipeDelegate.Left, position: 0, complete: false, left: null, right: null, behind: null },
{ tag: "right,null", side: SwipeDelegate.Right, position: 0, complete: false, left: null, right: null, behind: null },
{ tag: "invalid", side: 0, position: 0, complete: false, left: greenLeftComponent, right: null, behind: null }
]
}
function test_open_side(data) {
let control = createTemporaryObject(emptySwipeDelegateComponent, testCase,
{"swipe.left": data.left, "swipe.right": data.right, "swipe.behind": data.behind});
verify(control);
control.swipe.open(data.side);
tryCompare(control.swipe, "position", data.position);
tryCompare(control.swipe, "complete", data.complete);
}
Component {
id: openSwipeDelegateComponent
SwipeDelegate {
text: "SwipeDelegate"
width: 150
onClicked: swipe.open(SwipeDelegate.Right)
swipe.right: Item {
width: parent.width
height: parent.height
}
}
}
function test_open() {
let control = createTemporaryObject(openSwipeDelegateComponent, testCase);
verify(control);
mouseClick(control);
tryCompare(control.swipe, "position", SwipeDelegate.Right);
tryCompare(control.background, "x", -control.background.width);
// Swiping after opening should work as normal.
swipe(control, SwipeDelegate.Right, 0.0);
tryCompare(control.swipe, "position", 0.0);
tryCompare(control.background, "x", 0);
}
Component {
id: animationSwipeDelegateComponent
SwipeDelegate {
id: control
text: "SwipeDelegate"
width: 150
swipe.left: greenLeftComponent
swipe.right: redRightComponent
swipe.transition: null
property alias behavior: xBehavior
property alias animation: numberAnimation
background: Rectangle {
color: control.down ? "#ccc" : "#fff"
Behavior on x {
id: xBehavior
enabled: !control.down
NumberAnimation {
id: numberAnimation
easing.type: Easing.InOutCubic
duration: 400
}
}
}
}
}
function test_animations() {
// Test that animations are run when releasing from a drag.
let control = createTemporaryObject(animationSwipeDelegateComponent, testCase);
verify(control);
mousePress(control, control.width / 2, control.height / 2, Qt.LeftButton);
mouseMove(control, control.width - 1, control.height / 2);
verify(control.down);
verify(!control.behavior.enabled);
verify(!control.animation.running);
mouseRelease(control, control.width - 1, control.height / 2, Qt.LeftButton);
verify(!control.down);
verify(control.behavior.enabled);
verify(control.animation.running);
}
function test_spacing() {
let control = createTemporaryObject(swipeDelegateComponent, testCase, { text: "Some long, long, long text" })
verify(control)
verify(control.contentItem.implicitWidth + control.leftPadding + control.rightPadding > control.background.implicitWidth)
let textLabel = findChild(control.contentItem, "label")
verify(textLabel)
// The implicitWidth of the IconLabel that all buttons use as their contentItem
// should be equal to the implicitWidth of the Text while no icon is set.
compare(control.contentItem.implicitWidth, textLabel.implicitWidth)
// That means that spacing shouldn't affect it.
control.spacing += 100
compare(control.contentItem.implicitWidth, textLabel.implicitWidth)
// The implicitWidth of the SwipeDelegate itself should, therefore, also never include spacing while no icon is set.
compare(control.implicitWidth, textLabel.implicitWidth + control.leftPadding + control.rightPadding)
}
function test_display_data() {
return [
{ "tag": "IconOnly", display: SwipeDelegate.IconOnly },
{ "tag": "TextOnly", display: SwipeDelegate.TextOnly },
{ "tag": "TextUnderIcon", display: SwipeDelegate.TextUnderIcon },
{ "tag": "TextBesideIcon", display: SwipeDelegate.TextBesideIcon },
{ "tag": "IconOnly, mirrored", display: SwipeDelegate.IconOnly, mirrored: true },
{ "tag": "TextOnly, mirrored", display: SwipeDelegate.TextOnly, mirrored: true },
{ "tag": "TextUnderIcon, mirrored", display: SwipeDelegate.TextUnderIcon, mirrored: true },
{ "tag": "TextBesideIcon, mirrored", display: SwipeDelegate.TextBesideIcon, mirrored: true }
]
}
function test_display(data) {
let control = createTemporaryObject(swipeDelegateComponent, testCase, {
text: "SwipeDelegate",
display: data.display,
width: 400,
"icon.source": "qrc:/qt-project.org/imports/QtQuick/Controls/Basic/images/check.png",
"LayoutMirroring.enabled": !!data.mirrored
})
verify(control)
compare(control.icon.source, "qrc:/qt-project.org/imports/QtQuick/Controls/Basic/images/check.png")
let iconImage = findChild(control.contentItem, "image")
let textLabel = findChild(control.contentItem, "label")
switch (control.display) {
case SwipeDelegate.IconOnly:
verify(iconImage)
verify(!textLabel)
compare(iconImage.x, Math.round((control.availableWidth - iconImage.width) / 2))
compare(iconImage.y, Math.round((control.availableHeight - iconImage.height) / 2))
break;
case SwipeDelegate.TextOnly:
verify(!iconImage)
verify(textLabel)
compare(textLabel.x, control.mirrored ? control.availableWidth - textLabel.width : 0)
compare(textLabel.y, (control.availableHeight - textLabel.height) / 2)
break;
case SwipeDelegate.TextUnderIcon:
verify(iconImage)
verify(textLabel)
compare(iconImage.x, Math.round((control.availableWidth - iconImage.width) / 2))
compare(textLabel.x, (control.availableWidth - textLabel.width) / 2)
verify(iconImage.y < textLabel.y)
break;
case SwipeDelegate.TextBesideIcon:
verify(iconImage)
verify(textLabel)
if (control.mirrored)
verify(textLabel.x < iconImage.x)
else
verify(iconImage.x < textLabel.x)
compare(iconImage.y, Math.round((control.availableHeight - iconImage.height) / 2))
compare(textLabel.y, (control.availableHeight - textLabel.height) / 2)
break;
}
}
function test_resizeParent() {
let container = createTemporaryObject(itemComponent, testCase, { objectName: "container", width: 100, height: 200 })
verify(container)
let control = swipeDelegateComponent.createObject(container, { width: Qt.binding(function() { return container.width }) })
verify(control)
// Resize while closed.
container.width = 200
compare(container.width, 200)
compare(control.width, 200)
compare(control.background.width, 200)
compare(control.contentItem.width, 200 - control.leftPadding - control.rightPadding)
// Return to original size.
container.width = 100
compare(control.width, 100)
compare(control.background.width, 100)
compare(control.contentItem.width, 100 - control.leftPadding - control.rightPadding)
// Swipe to the left to open.
swipe(control, 0, -1.0)
// Nothing should have changed except positions.
compare(control.width, 100)
compare(control.background.width, 100)
compare(control.contentItem.width, 100 - control.leftPadding - control.rightPadding)
// Resize while open.
container.width = 200
// The items should fill the width as usual.
compare(control.width, 200)
compare(control.background.width, 200)
compare(control.contentItem.width, 200 - control.leftPadding - control.rightPadding)
}
Component {
id: cppDelegateComponent
SwipeDelegate {
text: "SwipeDelegate"
width: 150
swipe.right: ComponentCreator.createComponent(
"import QtQuick; Rectangle { width: 100; height: parent.height; color: \"tomato\" }")
}
}
function test_delegateComponentCreatedInCpp() {
let control = createTemporaryObject(cppDelegateComponent, testCase)
verify(control)
swipe(control, 0, -1.0)
compare(control.swipe.rightItem.color, Qt.color("tomato"))
}
Component {
id: swipeDelegate
SwipeDelegate {
anchors.centerIn: parent
width: 100
height: 50
contentItem: Rectangle {
color: "red"
}
swipe.right: Row {
height: parent.height
anchors.right: parent.right
property alias buttonItem: button
Button {
id: button
width: 50
height: parent.height
text: "Button"
}
}
}
}
function test_mouseEventOnNonVisualItem() {
let control = createTemporaryObject(swipeDelegate, testCase)
verify(control)
swipe(control, 0, -1.0)
verify(control.swipe.rightItem.visible)
let rightItem = control.swipe.rightItem
let rightClickSpy = signalSpyComponent.createObject(control,
{ target: rightItem.buttonItem, signalName: "clicked" })
verify(rightClickSpy)
verify(rightClickSpy.valid)
mouseClick(rightItem)
compare(rightClickSpy.count, 1)
}
}
|