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
|
// Copyright (C) 2021 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
#include <QtCore/qloggingcategory.h>
#include <QtTest/qtest.h>
#include <QtTest/qsignalspy.h>
#include <QtQml/qqmlfile.h>
#include <QtQuick/private/qquicklistview_p.h>
#include <QtQuickTest/quicktest.h>
#include <QtQuickControlsTestUtils/private/controlstestutils_p.h>
#include <QtQuickControlsTestUtils/private/dialogstestutils_p.h>
#include <QtQuickDialogs2/private/qquickfiledialog_p.h>
#include <QtQuickDialogs2QuickImpl/private/qquickfiledialogdelegate_p.h>
#include <QtQuickDialogs2QuickImpl/private/qquickfiledialogimpl_p_p.h>
#include <QtQuickDialogs2QuickImpl/private/qquickfolderbreadcrumbbar_p.h>
#include <QtQuickDialogs2QuickImpl/private/qquickfolderbreadcrumbbar_p_p.h>
#include <QtQuickDialogs2QuickImpl/private/qquickplatformfiledialog_p.h>
#include <QtQuickDialogs2QuickImpl/private/qquicksidebar_p.h>
#include <QtQuickDialogs2Utils/private/qquickfilenamefilter_p.h>
#include <QtQuickTemplates2/private/qquickapplicationwindow_p.h>
#include <QtQuickTemplates2/private/qquickcombobox_p.h>
#include <QtQuickTemplates2/private/qquickdialogbuttonbox_p.h>
#include <QtQuickTemplates2/private/qquickdialogbuttonbox_p_p.h>
#include <QtQuickTemplates2/private/qquicklabel_p.h>
#include <QtQuickTemplates2/private/qquickoverlay_p.h>
#include <QtQuickControls2/qquickstyle.h>
using namespace QQuickVisualTestUtils;
using namespace QQuickDialogTestUtils;
using namespace QQuickControlsTestUtils;
Q_LOGGING_CATEGORY(lcTest, "qt.quick.dialogs.tests.quickfiledialogimpl")
class tst_QQuickFileDialogImpl : public QQmlDataTest
{
Q_OBJECT
public:
tst_QQuickFileDialogImpl();
static void initMain()
{
// We need to set this attribute.
QCoreApplication::setAttribute(Qt::AA_DontUseNativeDialogs);
// We also don't want to run this for every style, as each one will have
// different ways of implementing the dialogs.
// For now we only test one style.
QQuickStyle::setStyle("Basic");
}
private slots:
void initTestCase() override;
void init() override;
void cleanupTestCase();
void defaults();
void chooseFileViaStandardButtons();
void chooseFileViaDoubleClick();
void chooseFileViaTextEdit();
void chooseFileViaEnter();
void bindCurrentFolder_data();
void bindCurrentFolder();
void changeFolderViaStandardButtons();
void changeFolderViaDoubleClick_data();
void changeFolderViaDoubleClick();
void chooseFolderViaTextEdit();
void chooseFolderViaEnter();
void chooseFileAndThenFolderViaTextEdit();
void cancelDialogWhileTextEditHasFocus();
void closingDialogCancels();
void goUp_data();
void goUp();
void goUpWhileTextEditHasFocus();
void goIntoLargeFolder();
void goUpIntoLargeFolder();
void keyAndShortcutHandling();
void bindNameFilters();
void changeNameFilters();
void changeNameFiltersAfterChangingFolder();
void tabFocusNavigation();
void acceptRejectLabel();
void bindTitle();
void itemsDisabledWhenNecessary();
void fileMode_data();
void fileMode();
void defaultSuffix_data();
void defaultSuffix();
void done_data();
void done();
void setSelectedFile_data();
void setSelectedFile();
void selectNewFileViaTextField_data();
void selectNewFileViaTextField();
void selectExistingFileShouldWarnUserWhenFileModeEqualsSaveFile();
void fileNameTextFieldOnlyChangesWhenSelectingFiles();
void setSchemeForSelectedFile();
void reopenAfterHideEvent();
void sidebarStandardPaths();
private:
enum DelegateOrderPolicy
{
ShowDirectoriesFirst,
ShowFilesFirst
};
QStringList tempDirExpectedVisibleFiles(DelegateOrderPolicy order) const;
QStringList tempSubDirExpectedVisibleFiles(DelegateOrderPolicy order) const;
QTemporaryDir tempDir;
QScopedPointer<QFile> tempFile1;
QScopedPointer<QFile> tempFile2;
QDir tempSubDir;
QDir tempSubSubDir;
QScopedPointer<QFile> tempSubFile1;
QScopedPointer<QFile> tempSubFile2;
QTemporaryDir largeTempDir;
QStringList largeTempDirPaths;
QDir largeTempDirLargeSubDir;
const int largeTempDirLargeSubDirIndex = 80;
QDir oldCurrentDir;
#if QT_CONFIG(shortcut)
const QKeySequence goUpKeySequence = QKeySequence(Qt::ALT | Qt::Key_Up);
const QKeySequence editPathKeySequence = QKeySequence(Qt::CTRL | Qt::Key_L);
#endif
};
QStringList tst_QQuickFileDialogImpl::tempDirExpectedVisibleFiles(DelegateOrderPolicy order) const
{
return order == ShowDirectoriesFirst
? QStringList { tempSubDir.path(), tempFile1->fileName(), tempFile2->fileName() }
: QStringList { tempFile1->fileName(), tempFile2->fileName(), tempSubDir.path() };
}
QStringList tst_QQuickFileDialogImpl::tempSubDirExpectedVisibleFiles(DelegateOrderPolicy order) const
{
return order == ShowDirectoriesFirst
? QStringList { tempSubSubDir.path(), tempSubFile1->fileName(), tempSubFile2->fileName() }
: QStringList { tempSubFile1->fileName(), tempSubFile2->fileName(), tempSubSubDir.path() };
}
// We don't want to fail on warnings until QTBUG-98964 is fixed,
// as we deliberately prevent deferred execution in some of the tests here,
// which causes warnings.
tst_QQuickFileDialogImpl::tst_QQuickFileDialogImpl()
: QQmlDataTest(QT_QMLTEST_DATADIR, FailOnWarningsPolicy::DoNotFailOnWarnings)
{
}
void tst_QQuickFileDialogImpl::initTestCase()
{
QQmlDataTest::initTestCase();
qputenv("QT_QUICK_DIALOGS_PRESELECT_FIRST_FILE", "1");
// Ensure that each test starts off in the temporary directory.
oldCurrentDir = QDir::current();
QDir::setCurrent(tempDir.path());
QVERIFY2(tempDir.isValid(), qPrintable(tempDir.errorString()));
// QTEST_QUICKCONTROLS_MAIN constructs the test case object once,
// and then calls qRun() for each style, and qRun() calls initTestCase().
// So, we need to check if we've already made the temporary directory.
// Note that this is only necessary if the test is run with more than one style.
if (!QDir(tempDir.path()).isEmpty())
return;
/*
Create a couple of files within the temporary directory. The structure is:
[temp directory] (tempDir)
├── sub-dir (tempSubDir)
│ ├── sub-sub-dir (tempSubSubDir)
│ ├── sub-file1.txt (tempSubFile1)
│ └── sub-file2.txt (tempSubFile2)
├── file1.txt (tempFile1)
└── file2.txt (tempFile2)
*/
tempSubDir = QDir(tempDir.path());
QVERIFY2(tempSubDir.mkdir("sub-dir"), qPrintable(QString::fromLatin1(
"Failed to make sub-directory \"sub-dir\" in %1. Permissions are: %2")
.arg(tempDir.path()).arg(QDebug::toString(QFileInfo(tempDir.path()).permissions()))));
QVERIFY(tempSubDir.cd("sub-dir"));
tempSubSubDir = QDir(tempSubDir.path());
QVERIFY2(tempSubSubDir.mkdir("sub-sub-dir"), qPrintable(QString::fromLatin1(
"Failed to make sub-directory \"sub-sub-dir\" in %1. Permissions are: %2")
.arg(tempSubDir.path()).arg(QDebug::toString(QFileInfo(tempSubDir.path()).permissions()))));
QVERIFY(tempSubSubDir.cd("sub-sub-dir"));
tempSubFile1.reset(new QFile(tempSubDir.path() + "/sub-file1.txt"));
QVERIFY(tempSubFile1->open(QIODevice::ReadWrite));
tempSubFile2.reset(new QFile(tempSubDir.path() + "/sub-file2.txt"));
QVERIFY(tempSubFile2->open(QIODevice::ReadWrite));
tempFile1.reset(new QFile(tempDir.path() + "/file1.txt"));
QVERIFY(tempFile1->open(QIODevice::ReadWrite));
tempFile2.reset(new QFile(tempDir.path() + "/file2.txt"));
QVERIFY(tempFile2->open(QIODevice::ReadWrite));
/*
Create another temporary directory that contains a large amount of folders.
*/
QVERIFY2(largeTempDir.isValid(), qPrintable(largeTempDir.errorString()));
const static int largeFileCount = 100;
const QDir largeTempDirectory(largeTempDir.path());
for (int i = 0; i < largeFileCount; ++i) {
// Pad with zeroes so that the directories are ordered as we expect.
const QString dirName = QString::fromLatin1("dir%1").arg(i, 3, 10, QLatin1Char('0'));
QVERIFY(largeTempDirectory.mkdir(dirName));
largeTempDirPaths.append(largeTempDirectory.filePath(dirName));
}
// ... and within one of those folders, more folders.
largeTempDirLargeSubDir = QDir(largeTempDir.path() + "/dir"
+ QString::fromLatin1("%1").arg(largeTempDirLargeSubDirIndex, 3, 10, QLatin1Char('0')));
QVERIFY(largeTempDirLargeSubDir.exists());
const QDir largeTempSubDirectory = QDir(largeTempDirLargeSubDir.path());
for (int i = 0; i < largeFileCount; ++i)
QVERIFY(largeTempSubDirectory.mkdir(QString::fromLatin1("sub-dir%1").arg(i, 3, 10, QLatin1Char('0'))));
}
void tst_QQuickFileDialogImpl::init()
{
// Do this before each test function in case the test sets it.
qputenv("QT_QUICK_DIALOGS_SHOW_DIRS_FIRST", "1");
}
void tst_QQuickFileDialogImpl::cleanupTestCase()
{
// Just in case...
QDir::setCurrent(oldCurrentDir.path());
}
#define VERIFY_FILE_SELECTED(expectedCurrentFolderUrl, expectedCurrentFileUrl) \
{ \
COMPARE_URL(dialogHelper.dialog->selectedFile(), expectedCurrentFileUrl); \
COMPARE_URLS(dialogHelper.dialog->selectedFiles(), { expectedCurrentFileUrl }); \
/* We also test the deprecated currentFile* API until it's removed. */ \
COMPARE_URL(dialogHelper.dialog->currentFile(), expectedCurrentFileUrl); \
COMPARE_URLS(dialogHelper.dialog->currentFiles(), { expectedCurrentFileUrl }); \
COMPARE_URL(dialogHelper.quickDialog->selectedFile(), expectedCurrentFileUrl); \
COMPARE_URL(dialogHelper.dialog->currentFolder(), expectedCurrentFolderUrl); \
COMPARE_URL(dialogHelper.quickDialog->currentFolder(), expectedCurrentFolderUrl); \
}
#define VERIFY_DELEGATE_CURRENT(expectedCurrentFileUrl, expectedCurrentIndex) \
{ \
QTRY_COMPARE(dialogHelper.fileDialogListView->currentIndex(), expectedCurrentIndex); \
QQuickFileDialogDelegate *fileDelegate = nullptr; \
QTRY_VERIFY(findViewDelegateItem(dialogHelper.fileDialogListView, expectedCurrentIndex, fileDelegate)); \
COMPARE_URL(fileDelegate->file(), expectedCurrentFileUrl); \
}
#define VERIFY_DELEGATE_FOCUSED(expectedCurrentIndex) \
{ \
QTRY_COMPARE(dialogHelper.fileDialogListView->currentIndex(), expectedCurrentIndex); \
QQuickFileDialogDelegate *fileDelegate = nullptr; \
QTRY_VERIFY(findViewDelegateItem(dialogHelper.fileDialogListView, expectedCurrentIndex, fileDelegate)); \
QTRY_VERIFY2(fileDelegate->hasActiveFocus(), qPrintable(QString::fromLatin1( \
"Expected delegate at index %1 to have focus, but %2 has it") \
.arg(QString::number(expectedCurrentIndex), \
QDebug::toString(QGuiApplication::focusWindow()->focusObject())))); \
QVERIFY(fileDelegate->isHighlighted()); \
}
/*
Checks that currentFolder, selectedFile, and currentIndex are what we expect.
It also checks that the relevant delegate in the view is current, has focus, and is highlighted.
As the FolderListModel (the view's model) is asynchronous, we need to wait for the currentIndex to change.
Can only be called when the dialog is open.
*/
#define VERIFY_FILE_SELECTED_AND_FOCUSED(expectedCurrentFolderUrl, expectedCurrentFileUrl, expectedCurrentIndex) \
VERIFY_FILE_SELECTED(expectedCurrentFolderUrl, expectedCurrentFileUrl) \
VERIFY_DELEGATE_CURRENT(expectedCurrentFileUrl, expectedCurrentIndex) \
VERIFY_DELEGATE_FOCUSED(expectedCurrentIndex)
class FileDialogTestHelper : public DialogTestHelper<QQuickFileDialog, QQuickFileDialogImpl>
{
public:
FileDialogTestHelper(QQmlDataTest *testCase, const QString &testFilePath,
const QStringList &qmlImportPaths = {}, const QVariantMap &initialProperties = {});
bool openDialog() override;
QPointer<QQuickListView> fileDialogListView;
};
FileDialogTestHelper::FileDialogTestHelper(QQmlDataTest *testCase, const QString &testFilePath,
const QStringList &qmlImportPaths, const QVariantMap &initialProperties)
: DialogTestHelper(testCase, testFilePath, qmlImportPaths, initialProperties)
{
}
bool FileDialogTestHelper::openDialog()
{
if (!DialogTestHelper::openDialog())
return false;
fileDialogListView = quickDialog->findChild<QQuickListView*>("fileDialogListView");
return fileDialogListView != nullptr;
}
void tst_QQuickFileDialogImpl::defaults()
{
QTest::failOnWarning(QRegularExpression(".*"));
FileDialogTestHelper dialogHelper(this, "fileDialog.qml");
QVERIFY2(dialogHelper.isWindowInitialized(), dialogHelper.failureMessage());
QVERIFY(dialogHelper.waitForWindowActive());
COMPARE_URL(dialogHelper.dialog->selectedFile(), QUrl());
// It should default to the current directory.
COMPARE_URL(dialogHelper.dialog->currentFolder(), QUrl::fromLocalFile(QDir().absolutePath()));
// The first file in the directory should be selected, but not until the dialog is actually open,
// as QQuickFileDialogImpl hasn't been created yet.
COMPARE_URL(dialogHelper.dialog->currentFile(), QUrl());
COMPARE_URLS(dialogHelper.dialog->currentFiles(), {});
QCOMPARE(dialogHelper.dialog->title(), QString());
QVERIFY(dialogHelper.openDialog());
QTRY_VERIFY(dialogHelper.isQuickDialogOpen());
QVERIFY(dialogHelper.waitForPopupWindowActiveAndPolished());
QQuickFileDialogImpl *quickDialog = dialogHelper.window()->findChild<QQuickFileDialogImpl*>();
QVERIFY(quickDialog);
VERIFY_FILE_SELECTED_AND_FOCUSED(QUrl::fromLocalFile(QDir().absolutePath()), QUrl::fromLocalFile(tempSubDir.path()), 0);
QCOMPARE(quickDialog->title(), QString());
}
void tst_QQuickFileDialogImpl::chooseFileViaStandardButtons()
{
// Open the dialog.
FileDialogTestHelper dialogHelper(this, "fileDialog.qml");
OPEN_QUICK_DIALOG();
QVERIFY(dialogHelper.waitForPopupWindowActiveAndPolished());
// Select the delegate by clicking once.
QSignalSpy dialogSelectedFileChangedSpy(dialogHelper.dialog, SIGNAL(selectedFileChanged()));
QVERIFY(dialogSelectedFileChangedSpy.isValid());
QSignalSpy dialogCurrentFileChangedSpy(dialogHelper.dialog, SIGNAL(currentFileChanged()));
QVERIFY(dialogCurrentFileChangedSpy.isValid());
QQuickFileDialogDelegate *delegate = nullptr;
QTRY_VERIFY(findViewDelegateItem(dialogHelper.fileDialogListView, 2, delegate));
COMPARE_URL(delegate->file(), QUrl::fromLocalFile(tempFile2->fileName()));
QVERIFY(clickButton(delegate));
VERIFY_FILE_SELECTED_AND_FOCUSED(QUrl::fromLocalFile(tempDir.path()), QUrl::fromLocalFile(tempFile2->fileName()), 2);
QCOMPARE(dialogSelectedFileChangedSpy.size(), 1);
QCOMPARE(dialogCurrentFileChangedSpy.size(), 1);
// Click the "Open" button.
QVERIFY(dialogHelper.quickDialog->footer());
auto dialogButtonBox = dialogHelper.quickDialog->footer()->findChild<QQuickDialogButtonBox*>();
QVERIFY(dialogButtonBox);
QQuickAbstractButton* openButton = findDialogButton(dialogButtonBox, "Open");
QVERIFY(openButton);
QVERIFY(clickButton(openButton));
COMPARE_URL(dialogHelper.dialog->selectedFile(), QUrl::fromLocalFile(tempFile2->fileName()));
COMPARE_URLS(dialogHelper.dialog->selectedFiles(), { QUrl::fromLocalFile(tempFile2->fileName()) });
COMPARE_URL(dialogHelper.quickDialog->selectedFile(), QUrl::fromLocalFile(tempFile2->fileName()));
QCOMPARE(dialogSelectedFileChangedSpy.size(), 1);
QCOMPARE(dialogCurrentFileChangedSpy.size(), 1);
QTRY_VERIFY(!dialogHelper.quickDialog->isVisible());
QVERIFY(!dialogHelper.dialog->isVisible());
}
void tst_QQuickFileDialogImpl::chooseFileViaDoubleClick()
{
// Open the dialog.
FileDialogTestHelper dialogHelper(this, "fileDialog.qml");
OPEN_QUICK_DIALOG();
QVERIFY(dialogHelper.waitForPopupWindowActiveAndPolished());
// Select the delegate by double-clicking.
QQuickFileDialogDelegate *delegate = nullptr;
QTRY_VERIFY(findViewDelegateItem(dialogHelper.fileDialogListView, 2, delegate));
COMPARE_URL(delegate->file(), QUrl::fromLocalFile(tempFile2->fileName()))
QVERIFY(doubleClickButton(delegate));
COMPARE_URL(dialogHelper.dialog->currentFile(), QUrl::fromLocalFile(tempFile2->fileName()))
COMPARE_URLS(dialogHelper.dialog->currentFiles(), { QUrl::fromLocalFile(tempFile2->fileName()) })
COMPARE_URL(dialogHelper.dialog->selectedFile(), QUrl::fromLocalFile(tempFile2->fileName()))
COMPARE_URLS(dialogHelper.dialog->selectedFiles(), { QUrl::fromLocalFile(tempFile2->fileName()) })
QVERIFY(!dialogHelper.dialog->isVisible());
QTRY_VERIFY(!dialogHelper.quickDialog->isVisible());
}
void tst_QQuickFileDialogImpl::chooseFileViaTextEdit()
{
// Open the dialog.
FileDialogTestHelper dialogHelper(this, "fileDialog.qml");
OPEN_QUICK_DIALOG();
QVERIFY(dialogHelper.waitForPopupWindowActiveAndPolished());
// Ensure that fileDialogListView has loaded its items, as we force active focus
// on the current item when we set it in setFileDialogListViewCurrentIndex(),
// which can make the TextField's visibility check
// below fail due to it being hidden when it loses activeFocus.
VERIFY_FILE_SELECTED_AND_FOCUSED(QUrl::fromLocalFile(tempDir.path()), QUrl::fromLocalFile(tempSubDir.path()), 0);
#if QT_CONFIG(shortcut)
// Get the text edit visible with Ctrl+L.
QTest::keySequence(dialogHelper.popupWindow(), editPathKeySequence);
auto breadcrumbBar = dialogHelper.quickDialog->findChild<QQuickFolderBreadcrumbBar*>();
QVERIFY(breadcrumbBar);
QVERIFY(breadcrumbBar->textField()->isVisible());
QCOMPARE(breadcrumbBar->textField()->text(), dialogHelper.dialog->currentFolder().toLocalFile());
QCOMPARE(breadcrumbBar->textField()->selectedText(), breadcrumbBar->textField()->text());
// Enter the path to the file in the text edit.
enterText(dialogHelper.popupWindow(), tempFile2->fileName());
QCOMPARE(breadcrumbBar->textField()->text(), tempFile2->fileName());
// Hit enter to accept.
QTest::keyClick(dialogHelper.popupWindow(), Qt::Key_Return);
COMPARE_URL(dialogHelper.quickDialog->selectedFile(), QUrl::fromLocalFile(tempFile2->fileName()));
COMPARE_URL(dialogHelper.dialog->selectedFile(), QUrl::fromLocalFile(tempFile2->fileName()));
COMPARE_URLS(dialogHelper.dialog->selectedFiles(), { QUrl::fromLocalFile(tempFile2->fileName()) });
QVERIFY(!dialogHelper.dialog->isVisible());
QTRY_VERIFY(!dialogHelper.quickDialog->isVisible());
#endif
}
void tst_QQuickFileDialogImpl::chooseFileViaEnter()
{
// Open the dialog.
FileDialogTestHelper dialogHelper(this, "fileDialog.qml");
OPEN_QUICK_DIALOG();
QVERIFY(dialogHelper.waitForPopupWindowActiveAndPolished());
// Before moving down, the first delegate in the view should be selected and have focus.
VERIFY_FILE_SELECTED_AND_FOCUSED(QUrl::fromLocalFile(tempDir.path()), QUrl::fromLocalFile(tempSubDir.path()), 0);
// Select the first file in the view by navigating with the down key.
QTest::keyClick(dialogHelper.popupWindow(), Qt::Key_Down);
COMPARE_URL(dialogHelper.dialog->currentFile(), QUrl::fromLocalFile(tempFile1->fileName()));
// Select the delegate by pressing enter.
QTest::keyClick(dialogHelper.popupWindow(), Qt::Key_Return);
COMPARE_URL(dialogHelper.dialog->selectedFile(), QUrl::fromLocalFile(tempFile1->fileName()));
COMPARE_URLS(dialogHelper.dialog->selectedFiles(), { QUrl::fromLocalFile(tempFile1->fileName()) });
QVERIFY(!dialogHelper.dialog->isVisible());
QTRY_VERIFY(!dialogHelper.quickDialog->isVisible());
QCOMPARE(dialogHelper.dialog->result(), QQuickFileDialog::Accepted);
}
void tst_QQuickFileDialogImpl::bindCurrentFolder_data()
{
QTest::addColumn<QUrl>("initialFolder");
QTest::addColumn<QUrl>("expectedFolder");
QTest::addColumn<QStringList>("expectedVisibleFiles");
const auto currentDirUrl = QUrl::fromLocalFile(QDir::current().path());
const auto tempSubDirUrl = QUrl::fromLocalFile(tempSubDir.path());
const auto tempSubFile1Url = QUrl::fromLocalFile(tempSubFile1->fileName());
const QStringList currentDirFiles = tempDirExpectedVisibleFiles(ShowDirectoriesFirst);
const QStringList tempSubDirFiles = tempSubDirExpectedVisibleFiles(ShowDirectoriesFirst);
// Setting the folder to "sub-dir" should result in "sub-file1.txt" and "sub-file2.txt" being visible.
QTest::addRow("sub-dir") << tempSubDirUrl << tempSubDirUrl << tempSubDirFiles;
// Setting a file as the folder shouldn't work, and the dialog shouldn't change its folder.
QTest::addRow("sub-dir/sub-file1.txt") << tempSubFile1Url << currentDirUrl << currentDirFiles;
}
void tst_QQuickFileDialogImpl::bindCurrentFolder()
{
QFETCH(QUrl, initialFolder);
QFETCH(QUrl, expectedFolder);
QFETCH(QStringList, expectedVisibleFiles);
// Open the dialog.
FileDialogTestHelper dialogHelper(this, "bindCurrentFolder.qml", {},
{{ "initialFolder", initialFolder }});
OPEN_QUICK_DIALOG();
COMPARE_URL(dialogHelper.dialog->currentFolder(), expectedFolder);
QString failureMessage;
// Even waiting for ListView polish and that the FolderListModel's status is ready aren't enough
// on Windows, apparently, as sometimes there just aren't any delegates by the time we do the check.
// So, we use QTRY_VERIFY2 each time we call this function just to be safe.
QTRY_VERIFY2(verifyFileDialogDelegates(dialogHelper.fileDialogListView, expectedVisibleFiles, failureMessage), qPrintable(failureMessage));
// Check that the breadcrumb bar is correct by constructing the expected files from the expectedFolder.
auto breadcrumbBar = dialogHelper.quickDialog->findChild<QQuickFolderBreadcrumbBar*>();
QVERIFY(breadcrumbBar);
QVERIFY2(verifyBreadcrumbDelegates(breadcrumbBar, expectedFolder, failureMessage), qPrintable(failureMessage));
}
void tst_QQuickFileDialogImpl::changeFolderViaStandardButtons()
{
// Open the dialog.
FileDialogTestHelper dialogHelper(this, "fileDialog.qml");
OPEN_QUICK_DIALOG();
QVERIFY(dialogHelper.waitForPopupWindowActiveAndPolished());
// Select the delegate by clicking once.
QQuickFileDialogDelegate *delegate = nullptr;
QTRY_VERIFY(findViewDelegateItem(dialogHelper.fileDialogListView, 0, delegate));
COMPARE_URL(delegate->file(), QUrl::fromLocalFile(tempSubDir.path()));
QVERIFY(clickButton(delegate));
// The selectedFile should change, but not currentFolder.
VERIFY_FILE_SELECTED_AND_FOCUSED(QUrl::fromLocalFile(tempDir.path()), QUrl::fromLocalFile(tempSubDir.path()), 0);
// Click the "Open" button. The dialog should navigate to that directory, but still be open.
QVERIFY(dialogHelper.quickDialog->footer());
auto dialogButtonBox = dialogHelper.quickDialog->footer()->findChild<QQuickDialogButtonBox*>();
QVERIFY(dialogButtonBox);
QQuickAbstractButton* openButton = findDialogButton(dialogButtonBox, "Open");
QVERIFY(openButton);
QVERIFY(clickButton(openButton));
COMPARE_URL(dialogHelper.dialog->selectedFile(), QUrl::fromLocalFile(tempSubSubDir.path()));
COMPARE_URL(dialogHelper.dialog->currentFolder(), QUrl::fromLocalFile(tempSubDir.path()));
QVERIFY(dialogHelper.dialog->isVisible());
dialogHelper.dialog->close();
QVERIFY(!dialogHelper.dialog->isVisible());
QTRY_VERIFY(!dialogHelper.quickDialog->isVisible());
}
void tst_QQuickFileDialogImpl::changeFolderViaDoubleClick_data()
{
QTest::addColumn<bool>("showDirsFirst");
QTest::newRow("showDirsFirst=true") << true;
QTest::newRow("showDirsFirst=false") << false;
}
void tst_QQuickFileDialogImpl::changeFolderViaDoubleClick()
{
QFETCH(bool, showDirsFirst);
qputenv("QT_QUICK_DIALOGS_SHOW_DIRS_FIRST", showDirsFirst ? "1" : "0");
// Open the dialog.
FileDialogTestHelper dialogHelper(this, "fileDialog.qml");
OPEN_QUICK_DIALOG();
// Select the "sub-dir" delegate by double-clicking.
QQuickFileDialogDelegate *subDirDelegate = nullptr;
const int subDirIndex = showDirsFirst ? 0 : 2;
QTRY_VERIFY(findViewDelegateItem(dialogHelper.fileDialogListView, subDirIndex, subDirDelegate));
COMPARE_URL(subDirDelegate->file(), QUrl::fromLocalFile(tempSubDir.path()));
QVERIFY(doubleClickButton(subDirDelegate));
const QStringList expectedVisibleFiles = tempSubDirExpectedVisibleFiles(showDirsFirst ? ShowDirectoriesFirst : ShowFilesFirst);
QString failureMessage;
QTRY_VERIFY2(verifyFileDialogDelegates(dialogHelper.fileDialogListView, expectedVisibleFiles, failureMessage), qPrintable(failureMessage));
// The first file in the directory should now be selected.
const QUrl firstFileUrl = showDirsFirst ? QUrl::fromLocalFile(tempSubSubDir.path()) : QUrl::fromLocalFile(tempSubFile1->fileName());
VERIFY_FILE_SELECTED_AND_FOCUSED(QUrl::fromLocalFile(tempSubDir.path()), firstFileUrl, 0);
// Since we only chose a folder, the dialog should still be open.
QVERIFY(dialogHelper.dialog->isVisible());
dialogHelper.dialog->close();
QVERIFY(!dialogHelper.dialog->isVisible());
QTRY_VERIFY(!dialogHelper.quickDialog->isVisible());
}
void tst_QQuickFileDialogImpl::chooseFolderViaTextEdit()
{
// Open the dialog.
FileDialogTestHelper dialogHelper(this, "fileDialog.qml");
OPEN_QUICK_DIALOG();
QVERIFY(dialogHelper.waitForPopupWindowActiveAndPolished());
// See comment in chooseFileViaTextEdit for why we check for this.
VERIFY_FILE_SELECTED_AND_FOCUSED(QUrl::fromLocalFile(tempDir.path()), QUrl::fromLocalFile(tempSubDir.path()), 0);
#if QT_CONFIG(shortcut)
// Get the text edit visible with Ctrl+L.
const auto editPathKeySequence = QKeySequence(Qt::CTRL | Qt::Key_L);
QTest::keySequence(dialogHelper.popupWindow(), editPathKeySequence);
#endif
auto breadcrumbBar = dialogHelper.quickDialog->findChild<QQuickFolderBreadcrumbBar*>();
QVERIFY(breadcrumbBar);
QVERIFY(breadcrumbBar->textField()->isVisible());
QCOMPARE(breadcrumbBar->textField()->text(), dialogHelper.dialog->currentFolder().toLocalFile());
QCOMPARE(breadcrumbBar->textField()->selectedText(), breadcrumbBar->textField()->text());
// Enter the path to the folder in the text edit.
enterText(dialogHelper.popupWindow(), tempSubDir.path());
QCOMPARE(breadcrumbBar->textField()->text(), tempSubDir.path());
// Hit enter to accept.
QTest::keyClick(dialogHelper.popupWindow(), Qt::Key_Return);
QString failureMessage;
QTRY_VERIFY2(verifyFileDialogDelegates(dialogHelper.fileDialogListView,
tempSubDirExpectedVisibleFiles(ShowDirectoriesFirst), failureMessage), qPrintable(failureMessage));
// The first file in the directory should be selected, which is "sub-sub-dir".
// Note that the TextEdit will still have focus, so we can't use VERIFY_FILE_SELECTED_AND_FOCUSED.
VERIFY_FILE_SELECTED(QUrl::fromLocalFile(tempSubDir.path()), QUrl::fromLocalFile(tempSubSubDir.path()));
VERIFY_DELEGATE_CURRENT(QUrl::fromLocalFile(tempSubSubDir.path()), 0)
QVERIFY(dialogHelper.dialog->isVisible());
dialogHelper.dialog->close();
QVERIFY(!dialogHelper.dialog->isVisible());
QTRY_VERIFY(!dialogHelper.quickDialog->isVisible());
}
void tst_QQuickFileDialogImpl::chooseFolderViaEnter()
{
// Open the dialog.
FileDialogTestHelper dialogHelper(this, "fileDialog.qml");
OPEN_QUICK_DIALOG();
QVERIFY(dialogHelper.waitForPopupWindowActiveAndPolished());
// The first delegate in the view should be selected and have focus.
VERIFY_FILE_SELECTED_AND_FOCUSED(QUrl::fromLocalFile(tempDir.path()), QUrl::fromLocalFile(tempSubDir.path()), 0);
// Select the delegate by pressing enter.
QTest::keyClick(dialogHelper.popupWindow(), Qt::Key_Return);
QString failureMessage;
QTRY_VERIFY2(verifyFileDialogDelegates(dialogHelper.fileDialogListView,
tempSubDirExpectedVisibleFiles(ShowDirectoriesFirst), failureMessage), qPrintable(failureMessage));
// The first file in the new directory should be selected, which is "sub-sub-dir".
VERIFY_FILE_SELECTED_AND_FOCUSED(QUrl::fromLocalFile(tempSubDir.path()), QUrl::fromLocalFile(tempSubSubDir.path()), 0);
// Since we only chose a folder, the dialog should still be open.
QVERIFY(dialogHelper.dialog->isVisible());
dialogHelper.dialog->close();
QVERIFY(!dialogHelper.dialog->isVisible());
QTRY_VERIFY(!dialogHelper.quickDialog->isVisible());
}
void tst_QQuickFileDialogImpl::chooseFileAndThenFolderViaTextEdit()
{
// Open the dialog.
FileDialogTestHelper dialogHelper(this, "fileDialog.qml");
OPEN_QUICK_DIALOG();
QVERIFY(dialogHelper.waitForPopupWindowActiveAndPolished());
// See comment in chooseFileViaTextEdit for why we check for this.
VERIFY_FILE_SELECTED_AND_FOCUSED(QUrl::fromLocalFile(tempDir.path()), QUrl::fromLocalFile(tempSubDir.path()), 0);
#if QT_CONFIG(shortcut)
// Get the text edit visible with Ctrl+L.
QTest::keySequence(dialogHelper.popupWindow(), editPathKeySequence);
#endif
auto breadcrumbBar = dialogHelper.quickDialog->findChild<QQuickFolderBreadcrumbBar*>();
QVERIFY(breadcrumbBar);
QVERIFY(breadcrumbBar->textField()->isVisible());
QCOMPARE(breadcrumbBar->textField()->text(), dialogHelper.dialog->currentFolder().toLocalFile());
QCOMPARE(breadcrumbBar->textField()->selectedText(), breadcrumbBar->textField()->text());
// Enter the path to the file in the text edit.
enterText(dialogHelper.popupWindow(), tempFile2->fileName());
QCOMPARE(breadcrumbBar->textField()->text(), tempFile2->fileName());
// Hit enter to accept.
QTest::keyClick(dialogHelper.popupWindow(), Qt::Key_Return);
COMPARE_URL(dialogHelper.quickDialog->selectedFile(), QUrl::fromLocalFile(tempFile2->fileName()));
COMPARE_URL(dialogHelper.dialog->selectedFile(), QUrl::fromLocalFile(tempFile2->fileName()));
COMPARE_URLS(dialogHelper.dialog->selectedFiles(), { QUrl::fromLocalFile(tempFile2->fileName()) });
QVERIFY(!dialogHelper.dialog->isVisible());
QTRY_VERIFY(!dialogHelper.quickDialog->isVisible());
// Check that the text edit is hidden and breadcrumbs are shown instead.
QVERIFY(!breadcrumbBar->textField()->isVisible());
// Re-open the dialog.
QVERIFY(dialogHelper.openDialog());
QTRY_VERIFY(dialogHelper.isQuickDialogOpen());
QVERIFY(dialogHelper.waitForPopupWindowActiveAndPolished());
// The breadcrumbs should be visible after opening, not the text edit.
QVERIFY(!breadcrumbBar->textField()->isVisible());
#if QT_CONFIG(shortcut)
// Get the text edit visible with Ctrl+L.
QTest::keySequence(dialogHelper.popupWindow(), editPathKeySequence);
#endif
QVERIFY(breadcrumbBar->textField()->isVisible());
// The text edit should show the directory that contains the last file that was selected.
QCOMPARE(breadcrumbBar->textField()->text(), tempDir.path());
QCOMPARE(breadcrumbBar->textField()->selectedText(), breadcrumbBar->textField()->text());
// Enter the path to the folder in the text edit.
enterText(dialogHelper.popupWindow(), tempSubDir.path());
QCOMPARE(breadcrumbBar->textField()->text(), tempSubDir.path());
// Hit enter to accept.
QTest::keyClick(dialogHelper.popupWindow(), Qt::Key_Return);
QString failureMessage;
QTRY_VERIFY2(verifyFileDialogDelegates(dialogHelper.fileDialogListView,
tempSubDirExpectedVisibleFiles(ShowDirectoriesFirst), failureMessage), qPrintable(failureMessage));
// The first file in the directory should be selected, which is "sub-sub-dir".
// Note that the TextEdit will still have focus, so we can't use VERIFY_FILE_SELECTED_AND_FOCUSED.
VERIFY_FILE_SELECTED(QUrl::fromLocalFile(tempSubDir.path()), QUrl::fromLocalFile(tempSubSubDir.path()));
VERIFY_DELEGATE_CURRENT(QUrl::fromLocalFile(tempSubSubDir.path()), 0)
// Close the dialog.
dialogHelper.dialog->close();
QVERIFY(!dialogHelper.dialog->isVisible());
QTRY_VERIFY(!dialogHelper.quickDialog->isVisible());
}
void tst_QQuickFileDialogImpl::cancelDialogWhileTextEditHasFocus()
{
// Open the dialog.
FileDialogTestHelper dialogHelper(this, "fileDialog.qml");
OPEN_QUICK_DIALOG();
QVERIFY(dialogHelper.waitForPopupWindowActiveAndPolished());
// See comment in chooseFileViaTextEdit for why we check for this.
VERIFY_FILE_SELECTED_AND_FOCUSED(QUrl::fromLocalFile(tempDir.path()), QUrl::fromLocalFile(tempSubDir.path()), 0);
#if QT_CONFIG(shortcut)
// Get the text edit visible with Ctrl+L.
QTest::keySequence(dialogHelper.window(), editPathKeySequence);
#endif
auto breadcrumbBar = dialogHelper.quickDialog->findChild<QQuickFolderBreadcrumbBar*>();
QVERIFY(breadcrumbBar);
QVERIFY(breadcrumbBar->textField()->hasActiveFocus());
// Close it via the cancel button.
auto dialogButtonBox = dialogHelper.quickDialog->footer()->findChild<QQuickDialogButtonBox*>();
QVERIFY(dialogButtonBox);
const QString cancelText = QQuickDialogButtonBoxPrivate::buttonText(QPlatformDialogHelper::Cancel);
auto cancelButton = findDialogButton(dialogButtonBox, cancelText);
QVERIFY(cancelButton);
QVERIFY(clickButton(cancelButton));
// Open it again. The text field should not be visible, but the breadcrumb bar itself should be.
dialogHelper.dialog->open();
QVERIFY(dialogHelper.dialog->isVisible());
QTRY_VERIFY(dialogHelper.quickDialog->isOpened());
QVERIFY(breadcrumbBar->isVisible());
// The ListView that contains the breadcrumb delegates should be visible.
QVERIFY(breadcrumbBar->contentItem()->isVisible());
QVERIFY(!breadcrumbBar->textField()->isVisible());
}
void tst_QQuickFileDialogImpl::closingDialogCancels()
{
// Open the dialog.
DialogTestHelper<QQuickFileDialog, QQuickFileDialogImpl> dialogHelper(this, "fileDialog.qml");
OPEN_QUICK_DIALOG();
QSignalSpy accepted(dialogHelper.dialog, &QQuickAbstractDialog::accepted);
QSignalSpy rejected(dialogHelper.dialog, &QQuickAbstractDialog::rejected);
// Accept the dialog.
QVERIFY(QMetaObject::invokeMethod(dialogHelper.window(), "doneAccepted"));
QVERIFY(!dialogHelper.dialog->isVisible());
QTRY_VERIFY(!dialogHelper.quickDialog->isVisible());
QCOMPARE(accepted.size(), 1);
QCOMPARE(rejected.size(), 0);
// Re-open the dialog.
accepted.clear();
QVERIFY(dialogHelper.openDialog());
QTRY_VERIFY(dialogHelper.isQuickDialogOpen());
// Close the dialog.
CLOSE_QUICK_DIALOG();
QCOMPARE(accepted.size(), 0);
QCOMPARE(rejected.size(), 1);
}
void tst_QQuickFileDialogImpl::goUp_data()
{
QTest::addColumn<bool>("showDirsFirst");
QTest::newRow("showDirsFirst=true") << true;
QTest::newRow("showDirsFirst=false") << false;
}
void tst_QQuickFileDialogImpl::goUp()
{
QFETCH(bool, showDirsFirst);
qputenv("QT_QUICK_DIALOGS_SHOW_DIRS_FIRST", showDirsFirst ? "1" : "0");
// Open the dialog. Start off in "sub-dir".
FileDialogTestHelper dialogHelper(this, "bindCurrentFolder.qml", {},
{{ "initialFolder", QUrl::fromLocalFile(tempSubDir.path()) }});
OPEN_QUICK_DIALOG();
// Go up a directory via the button next to the breadcrumb bar.
qCDebug(lcTest) << "going up to" << tempDir.path() << "- files in that dir:\n"
<< QQuickFileDialogImplPrivate::get(dialogHelper.quickDialog)->fileList(QDir(tempDir.path()));
auto breadcrumbBar = dialogHelper.quickDialog->findChild<QQuickFolderBreadcrumbBar*>();
QVERIFY(breadcrumbBar);
auto barListView = qobject_cast<QQuickListView*>(breadcrumbBar->contentItem());
QVERIFY(barListView);
if (QQuickTest::qIsPolishScheduled(barListView))
QVERIFY(QQuickTest::qWaitForPolish(barListView));
QVERIFY(clickButton(breadcrumbBar->upButton()));
// The previous directory that we were in should now be selected (matches e.g. Windows and Ubuntu).
QString failureMessage;
QTRY_VERIFY2(verifyFileDialogDelegates(dialogHelper.fileDialogListView,
tempDirExpectedVisibleFiles(showDirsFirst ? ShowDirectoriesFirst : ShowFilesFirst), failureMessage), qPrintable(failureMessage));
int expectedCurrentIndex = showDirsFirst ? 0 : 2;
VERIFY_FILE_SELECTED_AND_FOCUSED(QUrl::fromLocalFile(tempDir.path()), QUrl::fromLocalFile(tempSubDir.path()), expectedCurrentIndex);
#if QT_CONFIG(shortcut)
// Go up a directory via the keyboard shortcut.
QDir tempParentDir(tempDir.path());
QVERIFY(tempParentDir.cdUp());
const auto filesInTempParentDir = QQuickFileDialogImplPrivate::get(dialogHelper.quickDialog)->fileList(tempParentDir);
qCDebug(lcTest) << "going up to" << tempParentDir.path() << "- files in that dir:\n" << filesInTempParentDir;
QTest::keySequence(dialogHelper.window(), goUpKeySequence);
// Ubuntu on QEMU arm shows no files in /tmp even if there are.
if (!filesInTempParentDir.isEmpty()) {
expectedCurrentIndex = filesInTempParentDir.indexOf(QFileInfo(tempDir.path()));
QVERIFY(expectedCurrentIndex != -1);
VERIFY_FILE_SELECTED_AND_FOCUSED(QUrl::fromLocalFile(tempParentDir.path()), QUrl::fromLocalFile(tempDir.path()), expectedCurrentIndex);
}
#endif
}
void tst_QQuickFileDialogImpl::goUpWhileTextEditHasFocus()
{
// Open the dialog.
FileDialogTestHelper dialogHelper(this, "bindCurrentFolder.qml", {},
{{ "initialFolder", QUrl::fromLocalFile(tempSubDir.path()) }});
OPEN_QUICK_DIALOG();
QVERIFY(dialogHelper.waitForPopupWindowActiveAndPolished());
// See comment in chooseFileViaTextEdit for why we check for this.
VERIFY_FILE_SELECTED_AND_FOCUSED(QUrl::fromLocalFile(tempSubDir.path()), QUrl::fromLocalFile(tempSubSubDir.path()), 0);
#if QT_CONFIG(shortcut)
// Get the text edit visible with Ctrl+L.
QTest::keySequence(dialogHelper.popupWindow(), editPathKeySequence);
#endif
auto breadcrumbBar = dialogHelper.quickDialog->findChild<QQuickFolderBreadcrumbBar*>();
QVERIFY(breadcrumbBar);
QVERIFY(breadcrumbBar->textField()->hasActiveFocus());
// Go up a directory via the button next to the breadcrumb bar.
auto barListView = qobject_cast<QQuickListView*>(breadcrumbBar->contentItem());
QVERIFY(barListView);
if (QQuickTest::qIsPolishScheduled(barListView))
QVERIFY(QQuickTest::qWaitForPolish(barListView));
QVERIFY(clickButton(breadcrumbBar->upButton()));
// The path should have changed to the parent directory.
COMPARE_URL(dialogHelper.dialog->currentFolder(), QUrl::fromLocalFile(tempDir.path()));
// The text edit should be hidden when it loses focus.
QVERIFY(!breadcrumbBar->textField()->hasActiveFocus());
QVERIFY(!breadcrumbBar->textField()->isVisible());
// The focus should be given to the first delegate.
QVERIFY(dialogHelper.popupWindow()->activeFocusItem());
QQuickFileDialogDelegate *firstDelegate = nullptr;
QTRY_VERIFY(findViewDelegateItem(dialogHelper.fileDialogListView, 0, firstDelegate));
QCOMPARE(dialogHelper.popupWindow()->activeFocusItem(), firstDelegate);
}
void tst_QQuickFileDialogImpl::goIntoLargeFolder()
{
// Open the dialog. Start off in the large directory.
FileDialogTestHelper dialogHelper(this, "bindCurrentFolder.qml", {},
{{ "initialFolder", QUrl::fromLocalFile(largeTempDir.path()) }});
OPEN_QUICK_DIALOG();
// If the screen is so tall that the contentItem is not vertically larger than the view,
// then the test makes no sense.
if (QQuickTest::qIsPolishScheduled(dialogHelper.fileDialogListView))
QVERIFY(QQuickTest::qWaitForPolish(dialogHelper.fileDialogListView));
// Just to be safe, make sure it's at least twice as big.
if (dialogHelper.fileDialogListView->contentItem()->height() < dialogHelper.fileDialogListView->height() * 2) {
QSKIP(qPrintable(QString::fromLatin1("Expected height of dialogHelper.fileDialogListView's contentItem (%1)" \
" to be at least twice as big as the ListView's height (%2)")
.arg(dialogHelper.fileDialogListView->contentItem()->height()).arg(dialogHelper.fileDialogListView->height())));
}
// Scroll down to largeTempDirLargeSubDirIndex. The view should be somewhere towards the end.
QVERIFY(QMetaObject::invokeMethod(dialogHelper.fileDialogListView, "positionViewAtIndex", Qt::DirectConnection,
Q_ARG(int, largeTempDirLargeSubDirIndex), Q_ARG(int, QQuickItemView::PositionMode::Center)));
QVERIFY(dialogHelper.fileDialogListView->contentY() > 0);
// Go into it. The view should start at the top of the directory, not at the same contentY
// that it had in the previous directory.
QQuickFileDialogDelegate *subDirDelegate = nullptr;
QTRY_VERIFY(findViewDelegateItem(dialogHelper.fileDialogListView, largeTempDirLargeSubDirIndex, subDirDelegate));
COMPARE_URL(subDirDelegate->file(), QUrl::fromLocalFile(largeTempDirLargeSubDir.path()));
QVERIFY(doubleClickButton(subDirDelegate));
COMPARE_URL(dialogHelper.dialog->currentFolder(), QUrl::fromLocalFile(largeTempDirLargeSubDir.path()));
COMPARE_URL(dialogHelper.quickDialog->currentFolder(), QUrl::fromLocalFile(largeTempDirLargeSubDir.path()));
QCOMPARE(dialogHelper.fileDialogListView->contentY(), 0);
}
void tst_QQuickFileDialogImpl::goUpIntoLargeFolder()
{
// Open the dialog. Start off in the large sub-directory.
FileDialogTestHelper dialogHelper(this, "bindCurrentFolder.qml", {},
{{ "initialFolder", QUrl::fromLocalFile(largeTempDirLargeSubDir.path()) }});
OPEN_QUICK_DIALOG();
// We didn't set an initial selectedFile, so the first file in the directory will be selected.
VERIFY_FILE_SELECTED_AND_FOCUSED(QUrl::fromLocalFile(largeTempDirLargeSubDir.path()),
QUrl::fromLocalFile(largeTempDirLargeSubDir.path() + "/sub-dir000"), 0);
#if QT_CONFIG(shortcut)
// Go up a directory via the keyboard shortcut.
QTest::keySequence(dialogHelper.window(), goUpKeySequence);
QString failureMessage;
QTRY_VERIFY2(verifyFileDialogDelegates(dialogHelper.fileDialogListView,
largeTempDirPaths, failureMessage), qPrintable(failureMessage));
VERIFY_FILE_SELECTED_AND_FOCUSED(QUrl::fromLocalFile(largeTempDir.path()),
QUrl::fromLocalFile(largeTempDirLargeSubDir.path()), largeTempDirLargeSubDirIndex);
#endif
}
void tst_QQuickFileDialogImpl::keyAndShortcutHandling()
{
// Open the dialog.
FileDialogTestHelper dialogHelper(this, "fileDialog.qml");
OPEN_QUICK_DIALOG();
QVERIFY(dialogHelper.waitForPopupWindowActiveAndPolished());
VERIFY_FILE_SELECTED_AND_FOCUSED(QUrl::fromLocalFile(tempDir.path()), QUrl::fromLocalFile(tempSubDir.path()), 0);
#if QT_CONFIG(shortcut)
// Get the text edit visible with Ctrl+L.
QTest::keySequence(dialogHelper.popupWindow(), editPathKeySequence);
#endif
auto breadcrumbBar = dialogHelper.quickDialog->findChild<QQuickFolderBreadcrumbBar*>();
QVERIFY(breadcrumbBar);
QVERIFY(breadcrumbBar->textField()->isVisible());
QCOMPARE(breadcrumbBar->textField()->text(), dialogHelper.dialog->currentFolder().toLocalFile());
QCOMPARE(breadcrumbBar->textField()->selectedText(), breadcrumbBar->textField()->text());
#if QT_CONFIG(shortcut)
// Ctrl+L shouldn't hide it.
QTest::keySequence(dialogHelper.popupWindow(), editPathKeySequence);
#endif
QVERIFY(breadcrumbBar->textField()->isVisible());
// Cancel it with the escape key.
QTest::keyClick(dialogHelper.popupWindow(), Qt::Key_Escape);
QVERIFY(!breadcrumbBar->textField()->isVisible());
QVERIFY(dialogHelper.dialog->isVisible());
#if QT_CONFIG(shortcut)
// Make it visible.
QTest::keySequence(dialogHelper.popupWindow(), editPathKeySequence);
#endif
QVERIFY(breadcrumbBar->textField()->isVisible());
// Cancel it with the escape key again.
QTest::keyClick(dialogHelper.popupWindow(), Qt::Key_Escape);
QVERIFY(!breadcrumbBar->textField()->isVisible());
QVERIFY(dialogHelper.dialog->isVisible());
// Pressing escape now should close the dialog.
QTest::keyClick(dialogHelper.popupWindow(), Qt::Key_Escape);
QVERIFY(!dialogHelper.dialog->isVisible());
QTRY_VERIFY(!dialogHelper.quickDialog->isVisible());
}
void tst_QQuickFileDialogImpl::bindNameFilters()
{
// Open the dialog.
FileDialogTestHelper dialogHelper(this, "bindTxtHtmlNameFilters.qml");
OPEN_QUICK_DIALOG();
// Only "sub-dir", "text1.txt" and "text2.txt" should be visible, since *.txt is the first filter.
QString failureMessage;
QTRY_VERIFY2(verifyFileDialogDelegates(dialogHelper.fileDialogListView,
tempDirExpectedVisibleFiles(ShowDirectoriesFirst), failureMessage), qPrintable(failureMessage));
}
void tst_QQuickFileDialogImpl::changeNameFilters()
{
FileDialogTestHelper dialogHelper(this, "fileDialog.qml");
QVERIFY2(dialogHelper.isWindowInitialized(), dialogHelper.failureMessage());
QVERIFY(dialogHelper.waitForWindowActive());
// Open the dialog and check that selectedNameFilter is correct.
// By default, QFileDialogOptions::defaultNameFilterString() is used.
QVERIFY(dialogHelper.openDialog());
QTRY_VERIFY(dialogHelper.isQuickDialogOpen());
QVERIFY(dialogHelper.waitForPopupWindowActiveAndPolished());
QCOMPARE(dialogHelper.dialog->selectedNameFilter()->name(), "All Files");
QCOMPARE(dialogHelper.quickDialog->selectedNameFilter()->name(), "All Files");
QCOMPARE(dialogHelper.dialog->selectedNameFilter()->index(), 0);
QCOMPARE(dialogHelper.quickDialog->selectedNameFilter()->index(), 0);
QCOMPARE(dialogHelper.dialog->selectedNameFilter()->extensions(), { "*" });
QCOMPARE(dialogHelper.quickDialog->selectedNameFilter()->extensions(), { "*" });
QCOMPARE(dialogHelper.dialog->selectedNameFilter()->globs(), { "*" });
QCOMPARE(dialogHelper.quickDialog->selectedNameFilter()->globs(), { "*" });
// Close the dialog.
dialogHelper.dialog->close();
QVERIFY(!dialogHelper.dialog->isVisible());
QTRY_VERIFY(!dialogHelper.quickDialog->isVisible());
QTRY_VERIFY(!dialogHelper.popupWindow());
// Set .txt and .html filters.
QSignalSpy nameFiltersChangedSpy(dialogHelper.dialog, SIGNAL(nameFiltersChanged()));
QVERIFY(nameFiltersChangedSpy.isValid());
const QStringList nameFilters = { "Text files (*.txt)", "HTML files (*.html)" };
dialogHelper.dialog->setNameFilters(nameFilters);
QCOMPARE(dialogHelper.dialog->nameFilters(), nameFilters);
QCOMPARE(nameFiltersChangedSpy.size(), 1);
QCOMPARE(dialogHelper.dialog->selectedNameFilter()->name(), "Text files");
QCOMPARE(dialogHelper.dialog->selectedNameFilter()->index(), 0);
QCOMPARE(dialogHelper.dialog->selectedNameFilter()->extensions(), { "txt" });
QCOMPARE(dialogHelper.dialog->selectedNameFilter()->globs(), { "*.txt" });
// Re-open the dialog.
QVERIFY(dialogHelper.openDialog());
QTRY_VERIFY(dialogHelper.isQuickDialogOpen());
QVERIFY(dialogHelper.waitForPopupWindowActiveAndPolished());
// QQuickFileDialogImpl's values only get set before opening.
QCOMPARE(dialogHelper.quickDialog->selectedNameFilter()->name(), "Text files");
QCOMPARE(dialogHelper.quickDialog->selectedNameFilter()->index(), 0);
QCOMPARE(dialogHelper.quickDialog->selectedNameFilter()->extensions(), { "txt" });
QCOMPARE(dialogHelper.quickDialog->selectedNameFilter()->globs(), { "*.txt" });
// Only "sub-dir", "text1.txt" and "text2.txt" should be visible, since *.txt is the first filter.
QString failureMessage;
QTRY_VERIFY2(verifyFileDialogDelegates(dialogHelper.fileDialogListView,
tempDirExpectedVisibleFiles(ShowDirectoriesFirst), failureMessage), qPrintable(failureMessage));
// Open the ComboBox's popup.
const QQuickComboBox *comboBox = dialogHelper.quickDialog->findChild<QQuickComboBox*>();
QVERIFY(comboBox);
QCOMPARE(comboBox->window(), dialogHelper.popupWindow());
QQuickPopup *comboBoxPopup = comboBox->popup();
QVERIFY(comboBoxPopup);
const QPoint comboBoxCenterGlobalPos = comboBox->mapToGlobal(comboBox->boundingRect().center()).toPoint();
const QPoint comboBoxCenterPos = dialogHelper.popupWindow()->mapFromGlobal(comboBoxCenterGlobalPos);
QTest::mouseClick(dialogHelper.popupWindow(), Qt::LeftButton, Qt::NoModifier, comboBoxCenterPos);
QTRY_VERIFY(comboBoxPopup->isOpened());
// Select the .html delegate and close the combobox popup. The only visible entry should be the sub-dir.
QQuickListView *comboBoxPopupListView = qobject_cast<QQuickListView*>(comboBoxPopup->contentItem());
QVERIFY(comboBoxPopupListView);
{
QQuickAbstractButton *htmlDelegate = nullptr;
QVERIFY(QQuickTest::qWaitForPolish(comboBoxPopupListView));
QTRY_VERIFY(findViewDelegateItem(comboBoxPopupListView, 1, htmlDelegate));
QVERIFY(clickButton(htmlDelegate));
}
QTRY_VERIFY(!comboBoxPopup->isOpened());
// Use QTRY_VERIFY2 here to fix a failure on QEMU armv7 (QT_QPA_PLATFORM=offscreen).
// Not sure why it's necessary.
QTRY_VERIFY2(verifyFileDialogDelegates(dialogHelper.fileDialogListView, { tempSubDir.path() }, failureMessage), qPrintable(failureMessage));
// Open the popup again.
QTest::mouseClick(dialogHelper.popupWindow(), Qt::LeftButton, Qt::NoModifier, comboBoxCenterPos);
QTRY_VERIFY(comboBoxPopup->isOpened());
// Select .txt and close the combobox popup. The original entries should be visible.
{
QQuickAbstractButton *txtDelegate = nullptr;
QVERIFY(QQuickTest::qWaitForPolish(comboBoxPopupListView));
QTRY_VERIFY(findViewDelegateItem(comboBoxPopupListView, 0, txtDelegate));
QCOMPARE(txtDelegate->text(), nameFilters.at(0));
QVERIFY(clickButton(txtDelegate));
}
QTRY_VERIFY(!comboBoxPopup->isOpened());
QTRY_VERIFY2(verifyFileDialogDelegates(dialogHelper.fileDialogListView,
tempDirExpectedVisibleFiles(ShowDirectoriesFirst), failureMessage), qPrintable(failureMessage));
}
void tst_QQuickFileDialogImpl::changeNameFiltersAfterChangingFolder()
{
// Open the dialog.
FileDialogTestHelper dialogHelper(this, "bindAllTxtHtmlNameFilters.qml");
OPEN_QUICK_DIALOG();
QVERIFY(dialogHelper.waitForPopupWindowActiveAndPolished());
// Go into the "sub-dir" folder.
QString failureMessage;
QTRY_VERIFY2(verifyFileDialogDelegates(dialogHelper.fileDialogListView,
tempDirExpectedVisibleFiles(ShowDirectoriesFirst), failureMessage), qPrintable(failureMessage));
QQuickFileDialogDelegate *subDirDelegate = nullptr;
QTRY_VERIFY(findViewDelegateItem(dialogHelper.fileDialogListView, 0, subDirDelegate));
QVERIFY(doubleClickButton(subDirDelegate));
COMPARE_URL(dialogHelper.dialog->currentFolder(), QUrl::fromLocalFile(tempSubDir.path()));
COMPARE_URL(dialogHelper.quickDialog->currentFolder(), QUrl::fromLocalFile(tempSubDir.path()));
// Open the ComboBox's popup.
const QQuickComboBox *comboBox = dialogHelper.quickDialog->findChild<QQuickComboBox*>();
QVERIFY(comboBox);
const QPoint comboBoxCenterPos = comboBox->mapToScene({ comboBox->width() / 2, comboBox->height() / 2 }).toPoint();
QTest::mouseClick(dialogHelper.popupWindow(), Qt::LeftButton, Qt::NoModifier, comboBoxCenterPos);
QTRY_VERIFY(comboBox->popup()->isOpened());
// Select the .html delegate, close the combobox popup, and ensure that the change had an effect.
QQuickListView *comboBoxPopupListView = qobject_cast<QQuickListView*>(comboBox->popup()->contentItem());
QVERIFY(comboBoxPopupListView);
{
QQuickAbstractButton *htmlDelegate = nullptr;
QTRY_VERIFY(findViewDelegateItem(comboBoxPopupListView, 2, htmlDelegate));
QVERIFY(clickButton(htmlDelegate));
}
QTRY_VERIFY(!comboBox->popup()->isVisible());
// There are no HTML files in "sub-dir", so there should only be the one "sub-sub-dir" delegate.
QTRY_VERIFY2(verifyFileDialogDelegates(dialogHelper.fileDialogListView, { tempSubSubDir.path() }, failureMessage), qPrintable(failureMessage));
}
void tst_QQuickFileDialogImpl::tabFocusNavigation()
{
// Open the dialog.
FileDialogTestHelper dialogHelper(this, "bindTxtHtmlNameFilters.qml");
OPEN_QUICK_DIALOG();
QVERIFY(dialogHelper.waitForPopupWindowActiveAndPolished());
QList<QQuickItem*> expectedFocusItems;
// The initial focus should be on the first delegate.
QQuickFileDialogDelegate *firstDelegate = nullptr;
QTRY_VERIFY(findViewDelegateItem(dialogHelper.fileDialogListView, 0, firstDelegate));
expectedFocusItems << firstDelegate;
// Tab should move to the name filters combobox.
QQuickComboBox *comboBox = dialogHelper.quickDialog->findChild<QQuickComboBox*>();
QVERIFY(comboBox);
expectedFocusItems << comboBox;
// Next, the left-most dialog button.
auto dialogButtonBox = dialogHelper.quickDialog->footer()->findChild<QQuickDialogButtonBox*>();
QVERIFY(dialogButtonBox);
QCOMPARE(dialogButtonBox->count(), 2);
auto leftMostButton = qobject_cast<QQuickAbstractButton*>(dialogButtonBox->itemAt(0));
QVERIFY(leftMostButton);
expectedFocusItems << leftMostButton;
// Then, the right-most dialog button.
auto rightMostButton = qobject_cast<QQuickAbstractButton*>(dialogButtonBox->itemAt(1));
QVERIFY(rightMostButton);
expectedFocusItems << rightMostButton;
// Then, the up button.
auto breadcrumbBar = dialogHelper.quickDialog->findChild<QQuickFolderBreadcrumbBar*>();
QVERIFY(breadcrumbBar);
expectedFocusItems << breadcrumbBar->upButton();
// Finally, add each bread crumb delegate.
for (int i = 0; i < dialogHelper.fileDialogListView->count(); ++i) {
QQuickFileDialogDelegate *delegate = nullptr;
QTRY_VERIFY(findViewDelegateItem(dialogHelper.fileDialogListView, i, delegate));
expectedFocusItems << delegate;
}
// Tab through each item, checking the focus after each.
for (auto expectedFocusItem : std::as_const(expectedFocusItems)) {
// Check the focus item first so that we account for the first item.
// Print detailed failure message as workaround for QTBUG-92102.
QVERIFY2(dialogHelper.popupWindow()->activeFocusItem() == expectedFocusItem, qPrintable(QString::fromLatin1(
"\n Actual: %1\n Expected: %2").arg(QDebug::toString(dialogHelper.popupWindow()->activeFocusItem()))
.arg(QDebug::toString(expectedFocusItem))));
if (expectedFocusItem != expectedFocusItems.last())
QTest::keyClick(dialogHelper.popupWindow(), Qt::Key_Tab);
}
// Ensure the order is reversed when shift-tabbing.
std::reverse(expectedFocusItems.begin(), expectedFocusItems.end());
// We know the first (last) item has focus already, so skip it.
expectedFocusItems.removeFirst();
for (auto expectedFocusItem : std::as_const(expectedFocusItems)) {
QTest::keyClick(dialogHelper.popupWindow(), Qt::Key_Tab, Qt::ShiftModifier);
QCOMPARE(dialogHelper.popupWindow()->activeFocusItem(), expectedFocusItem);
}
}
void tst_QQuickFileDialogImpl::acceptRejectLabel()
{
// Open the dialog.
FileDialogTestHelper dialogHelper(this, "acceptRejectLabel.qml");
OPEN_QUICK_DIALOG();
// Check that the accept and reject buttons' labels have changed.
auto dialogButtonBox = dialogHelper.quickDialog->footer()->findChild<QQuickDialogButtonBox*>();
QVERIFY(dialogButtonBox);
QVERIFY(findDialogButton(dialogButtonBox, "AcceptTest"));
QVERIFY(findDialogButton(dialogButtonBox, "RejectTest"));
// Close the dialog.
dialogHelper.dialog->close();
QVERIFY(!dialogHelper.dialog->isVisible());
QTRY_VERIFY(!dialogHelper.quickDialog->isVisible());
// Reset back to the default text.
dialogHelper.dialog->resetAcceptLabel();
dialogHelper.dialog->resetRejectLabel();
// Re-open the dialog.
dialogHelper.dialog->open();
QVERIFY(dialogHelper.dialog->isVisible());
QTRY_VERIFY(dialogHelper.quickDialog->isOpened());
// Check that the defaults are back.
const QString openText = QQuickDialogButtonBoxPrivate::buttonText(QPlatformDialogHelper::Open);
QVERIFY2(findDialogButton(dialogButtonBox, openText), qPrintable(QString::fromLatin1(
"Failed to find dialog button with text \"%1\"").arg(openText)));
const QString cancelText = QQuickDialogButtonBoxPrivate::buttonText(QPlatformDialogHelper::Cancel);
QVERIFY2(findDialogButton(dialogButtonBox, cancelText), qPrintable(QString::fromLatin1(
"Failed to find dialog button with text \"%1\"").arg(cancelText)));
}
void tst_QQuickFileDialogImpl::bindTitle()
{
// Open the dialog.
FileDialogTestHelper dialogHelper(this, "bindTitle.qml");
OPEN_QUICK_DIALOG();
// Open the dialog and check that the correct title is displayed.
QQuickFileDialog *dialog = dialogHelper.window()->property("dialog").value<QQuickFileDialog*>();
QVERIFY(dialog);
const QString expectedTitle = QLatin1String("Test Title");
QCOMPARE(dialogHelper.dialog->title(), expectedTitle);
QCOMPARE(dialogHelper.quickDialog->title(), expectedTitle);
auto header = dialogHelper.quickDialog->header();
QVERIFY(header);
auto dialogTitleBarLabel = dialogHelper.quickDialog->header()->findChild<QQuickLabel*>("dialogTitleBarLabel");
QVERIFY(dialogTitleBarLabel);
QCOMPARE(dialogTitleBarLabel->text(), expectedTitle);
}
void tst_QQuickFileDialogImpl::itemsDisabledWhenNecessary()
{
QTemporaryDir anotherTempDir;
QVERIFY(anotherTempDir.isValid());
QDir subDir(anotherTempDir.path());
QVERIFY(subDir.mkdir("emptyDir"));
QVERIFY(subDir.cd("emptyDir"));
// Open the dialog.
FileDialogTestHelper dialogHelper(this, "bindCurrentFolder.qml", {},
{{ "initialFolder", QUrl::fromLocalFile(subDir.path()) }});
OPEN_QUICK_DIALOG();
COMPARE_URL(dialogHelper.dialog->currentFolder(), QUrl::fromLocalFile(subDir.path()));
COMPARE_URL(dialogHelper.quickDialog->currentFolder(), QUrl::fromLocalFile(subDir.path()));
QVERIFY(dialogHelper.waitForPopupWindowActiveAndPolished());
// We opened it in a folder that has no files, so the Open button should be disabled.
QVERIFY(dialogHelper.quickDialog->footer());
auto dialogButtonBox = dialogHelper.quickDialog->footer()->findChild<QQuickDialogButtonBox*>();
QVERIFY(dialogButtonBox);
QQuickAbstractButton* openButton = findDialogButton(dialogButtonBox, "Open");
QVERIFY(openButton);
QCOMPARE(openButton->isEnabled(), false);
// Now go up. The Open button should now be enabled.
auto breadcrumbBar = dialogHelper.quickDialog->findChild<QQuickFolderBreadcrumbBar*>();
QVERIFY(breadcrumbBar);
QVERIFY(clickButton(breadcrumbBar->upButton()));
QCOMPARE(openButton->isEnabled(), true);
COMPARE_URL(dialogHelper.dialog->currentFolder(), QUrl::fromLocalFile(anotherTempDir.path()));
COMPARE_URL(dialogHelper.quickDialog->currentFolder(), QUrl::fromLocalFile(anotherTempDir.path()));
#if QT_CONFIG(shortcut)
// Get the text edit visible with Ctrl+L. The Open button should now be disabled.
QTest::keySequence(dialogHelper.popupWindow(), editPathKeySequence);
QVERIFY(breadcrumbBar->textField()->isVisible());
QCOMPARE(openButton->isEnabled(), false);
#endif
// Hide it with the escape key. The Open button should now be enabled.
QTest::keyClick(dialogHelper.popupWindow(), Qt::Key_Escape);
QVERIFY(!breadcrumbBar->textField()->isVisible());
QCOMPARE(openButton->isEnabled(), true);
}
void tst_QQuickFileDialogImpl::fileMode_data()
{
QTest::addColumn<QQuickFileDialog::FileMode>("fileMode");
QTest::addColumn<QString>("acceptButtonText");
QTest::newRow("OpenFile") << QQuickFileDialog::OpenFile << "Open";
QTest::newRow("OpenFiles") << QQuickFileDialog::OpenFiles << "Open";
QTest::newRow("SaveFile") << QQuickFileDialog::SaveFile << "Save";
}
void tst_QQuickFileDialogImpl::fileMode()
{
QFETCH(QQuickFileDialog::FileMode, fileMode);
QFETCH(QString, acceptButtonText);
// Open the dialog.
FileDialogTestHelper dialogHelper(this, "fileDialog.qml");
dialogHelper.dialog->setFileMode(fileMode);
OPEN_QUICK_DIALOG();
QVERIFY(dialogHelper.waitForPopupWindowActiveAndPolished());
// Select the first file (not a directory).
QQuickFileDialogDelegate *tempFile1Delegate = nullptr;
QTRY_VERIFY(findViewDelegateItem(dialogHelper.fileDialogListView, 1, tempFile1Delegate));
COMPARE_URL(tempFile1Delegate->file(), QUrl::fromLocalFile(tempFile1->fileName()));
QVERIFY(clickButton(tempFile1Delegate));
COMPARE_URL(dialogHelper.dialog->currentFile(), QUrl::fromLocalFile(tempFile1->fileName()));
COMPARE_URLS(dialogHelper.dialog->currentFiles(), { QUrl::fromLocalFile(tempFile1->fileName()) });
// All modes should support opening an existing file, so the accept button should be enabled.
QVERIFY(dialogHelper.quickDialog->footer());
auto dialogButtonBox = dialogHelper.quickDialog->footer()->findChild<QQuickDialogButtonBox*>();
QVERIFY(dialogButtonBox);
QQuickAbstractButton *acceptButton = findDialogButton(dialogButtonBox, acceptButtonText);
QVERIFY(acceptButton);
QCOMPARE(acceptButton->isEnabled(), true);
// Only the OpenFiles mode should allow multiple files to be selected, however.
QQuickFileDialogDelegate *tempFile2Delegate = nullptr;
QTRY_VERIFY(findViewDelegateItem(dialogHelper.fileDialogListView, 2, tempFile2Delegate));
COMPARE_URL(tempFile2Delegate->file(), QUrl::fromLocalFile(tempFile2->fileName()));
QTest::keyPress(dialogHelper.popupWindow(), Qt::Key_Shift);
QVERIFY(clickButton(tempFile2Delegate));
QTest::keyRelease(dialogHelper.popupWindow(), Qt::Key_Shift);
if (fileMode == QQuickFileDialog::OpenFiles) {
// currentFile() always points to the first file in the list of selected files.
COMPARE_URL(dialogHelper.dialog->currentFile(), QUrl::fromLocalFile(tempFile1->fileName()));
const QList<QUrl> expectedSelectedFiles = {
QUrl::fromLocalFile(tempFile1->fileName()), QUrl::fromLocalFile(tempFile2->fileName()) };
COMPARE_URLS(dialogHelper.dialog->currentFiles(), expectedSelectedFiles);
} else {
// OpenFile and SaveFile dialogs should have tempFile2 selected since it was clicked,
// but the shift should do nothing, so tempFile1 should no longer be selected.
COMPARE_URL(dialogHelper.dialog->currentFile(), QUrl::fromLocalFile(tempFile2->fileName()));
COMPARE_URLS(dialogHelper.dialog->currentFiles(), { QUrl::fromLocalFile(tempFile2->fileName()) });
}
#if QT_CONFIG(shortcut)
// Get the text edit visible with Ctrl+L.
QTest::keySequence(dialogHelper.popupWindow(), editPathKeySequence);
#endif
auto breadcrumbBar = dialogHelper.quickDialog->findChild<QQuickFolderBreadcrumbBar*>();
QVERIFY(breadcrumbBar);
QVERIFY(breadcrumbBar->textField()->isVisible());
// Typing in the name of an non-existent file should only work for SaveFile.
const QString nonExistentFilePath = "/foo/bar.txt";
enterText(dialogHelper.popupWindow(), nonExistentFilePath);
QCOMPARE(breadcrumbBar->textField()->text(), nonExistentFilePath);
QTest::keyClick(dialogHelper.popupWindow(), Qt::Key_Return);
if (fileMode == QQuickFileDialog::SaveFile) {
COMPARE_URL(dialogHelper.dialog->selectedFile(), QUrl::fromLocalFile(nonExistentFilePath));
COMPARE_URLS(dialogHelper.dialog->selectedFiles(), { QUrl::fromLocalFile(nonExistentFilePath) });
QVERIFY(!dialogHelper.dialog->isVisible());
QTRY_VERIFY(!dialogHelper.quickDialog->isVisible());
} else {
// For OpenFile(s), we do what Qt Quick Dialogs 1.x did, and restore the previous (valid) dir path.
// The currentFile(s) should remain unchanged too.
QVERIFY(dialogHelper.dialog->isVisible());
COMPARE_URL(dialogHelper.dialog->currentFolder(), QUrl::fromLocalFile(tempDir.path()));
QCOMPARE(breadcrumbBar->textField()->text(), tempDir.path());
// Should be unchanged from the last time.
if (fileMode == QQuickFileDialog::OpenFiles) {
COMPARE_URL(dialogHelper.dialog->currentFile(), QUrl::fromLocalFile(tempFile1->fileName()));
const QList<QUrl> expectedSelectedFiles = {
QUrl::fromLocalFile(tempFile1->fileName()), QUrl::fromLocalFile(tempFile2->fileName()) };
COMPARE_URLS(dialogHelper.dialog->currentFiles(), expectedSelectedFiles);
} else { // OpenFile
COMPARE_URL(dialogHelper.dialog->currentFile(), QUrl::fromLocalFile(tempFile2->fileName()));
COMPARE_URLS(dialogHelper.dialog->currentFiles(), { QUrl::fromLocalFile(tempFile2->fileName()) });
}
}
}
void tst_QQuickFileDialogImpl::defaultSuffix_data()
{
QTest::addColumn<QString>("defaultSuffix");
QTest::newRow("txt") << "txt";
QTest::newRow(".txt") << ".txt";
}
void tst_QQuickFileDialogImpl::defaultSuffix()
{
QFETCH(QString, defaultSuffix);
// Simplify the test by using a directory with no files, and add a single file there.
QFile tempFile1(tempSubSubDir.path() + "/file1");
QVERIFY(tempFile1.open(QIODevice::ReadWrite));
// Open the dialog.
FileDialogTestHelper dialogHelper(this, "bindCurrentFolder.qml", {},
{{ "initialFolder", QUrl::fromLocalFile(tempSubSubDir.path()) }});
dialogHelper.dialog->setDefaultSuffix(defaultSuffix);
OPEN_QUICK_DIALOG();
COMPARE_URL(dialogHelper.dialog->currentFolder(), QUrl::fromLocalFile(tempSubSubDir.path()));
// There should be one extension-less file: "file1".
QString failureMessage;
const QStringList expectedVisibleFiles = { tempFile1.fileName() };
QTRY_VERIFY2(verifyFileDialogDelegates(dialogHelper.fileDialogListView, expectedVisibleFiles, failureMessage), qPrintable(failureMessage));
// Choose the delegate. The suffix should be added to the delegates.
QQuickFileDialogDelegate *file1Delegate = nullptr;
QTRY_VERIFY(findViewDelegateItem(dialogHelper.fileDialogListView, 0, file1Delegate));
COMPARE_URL(file1Delegate->file(), QUrl::fromLocalFile(tempFile1.fileName()));
QVERIFY(doubleClickButton(file1Delegate));
QVERIFY(!dialogHelper.dialog->isVisible());
QTRY_VERIFY(!dialogHelper.quickDialog->isVisible());
const QUrl fileUrlWithSuffix = QUrl::fromLocalFile(tempFile1.fileName() + ".txt");
COMPARE_URL(dialogHelper.dialog->selectedFile(), fileUrlWithSuffix);
COMPARE_URLS(dialogHelper.dialog->selectedFiles(), { fileUrlWithSuffix });
}
void tst_QQuickFileDialogImpl::done_data()
{
QTest::addColumn<QQuickFileDialog::StandardCode>("result");
QTest::newRow("Accepted") << QQuickFileDialog::Accepted;
QTest::newRow("Rejected") << QQuickFileDialog::Rejected;
}
void tst_QQuickFileDialogImpl::done()
{
QFETCH(QQuickFileDialog::StandardCode, result);
// Open the dialog.
FileDialogTestHelper dialogHelper(this, "fileDialog.qml");
OPEN_QUICK_DIALOG();
switch (result) {
case QQuickFileDialog::Accepted:
QVERIFY(QMetaObject::invokeMethod(dialogHelper.window(), "doneAccepted"));
break;
case QQuickFileDialog::Rejected:
QVERIFY(QMetaObject::invokeMethod(dialogHelper.window(), "doneRejected"));
break;
}
QVERIFY(!dialogHelper.dialog->isVisible());
QTRY_VERIFY(!dialogHelper.quickDialog->isVisible());
QCOMPARE(dialogHelper.dialog->result(), result);
}
void tst_QQuickFileDialogImpl::setSelectedFile_data()
{
fileMode_data();
}
void tst_QQuickFileDialogImpl::setSelectedFile()
{
QFETCH(QQuickFileDialog::FileMode, fileMode);
// Open the dialog.
const auto tempFile1Url = QUrl::fromLocalFile(tempFile1->fileName());
const QVariantMap initialProperties = {
{ "tempFile1Url", QVariant::fromValue(tempFile1Url) },
{ "fileMode", QVariant::fromValue(fileMode) }
};
FileDialogTestHelper dialogHelper(
this, "setSelectedFile.qml", {}, initialProperties);
dialogHelper.dialog->setOptions(QFileDialogOptions::DontConfirmOverwrite);
OPEN_QUICK_DIALOG();
QVERIFY(dialogHelper.waitForPopupWindowActiveAndPolished());
// The selected file should be what we set.
VERIFY_FILE_SELECTED_AND_FOCUSED(QUrl::fromLocalFile(tempDir.path()), tempFile1Url, 1);
// Select the next file in the view by navigating with the down key.
// We know it already has focus, as VERIFY_FILE_SELECTED_AND_FOCUSED checks that.
QTest::keyClick(dialogHelper.popupWindow(), Qt::Key_Down);
const auto tempFile2Url = QUrl::fromLocalFile(tempFile2->fileName());
COMPARE_URL(dialogHelper.quickDialog->selectedFile(), tempFile2Url);
COMPARE_URL(dialogHelper.dialog->selectedFile(), tempFile2Url);
// Select the delegate by pressing enter.
QTest::keyClick(dialogHelper.popupWindow(), Qt::Key_Return);
COMPARE_URL(dialogHelper.dialog->selectedFile(), tempFile2Url);
COMPARE_URLS(dialogHelper.dialog->selectedFiles(), { tempFile2Url });
QVERIFY(!dialogHelper.dialog->isVisible());
QTRY_VERIFY(!dialogHelper.quickDialog->isVisible());
QCOMPARE(dialogHelper.dialog->result(), QQuickFileDialog::Accepted);
// Set a different initial selectedFile and re-open.
dialogHelper.dialog->setSelectedFile(QUrl::fromLocalFile(tempFile1->fileName()));
QVERIFY(dialogHelper.openDialog());
QTRY_VERIFY(dialogHelper.isQuickDialogOpen());
QVERIFY(dialogHelper.waitForPopupWindowActiveAndPolished());
VERIFY_FILE_SELECTED_AND_FOCUSED(QUrl::fromLocalFile(tempDir.path()), tempFile1Url, 1);
// Close it.
dialogHelper.dialog->close();
QVERIFY(!dialogHelper.dialog->isVisible());
QTRY_VERIFY(!dialogHelper.quickDialog->isVisible());
// Try to set an invalid selectedFile; it should be a no-op for modes other than SaveFile,
// and the previous selectedFile should still be selected.
const QString invalidPath = tempDir.path() + "/does-not-exist.txt";
if (fileMode != QQuickFileDialog::SaveFile) {
QTest::ignoreMessage(QtWarningMsg, QRegularExpression(QLatin1String(".*QML FileDialog: Cannot set ")
+ invalidPath + QLatin1String(" as a selected file because it doesn't exist")));
}
dialogHelper.dialog->setSelectedFile(QUrl::fromLocalFile(invalidPath));
QVERIFY(dialogHelper.openDialog());
QVERIFY(dialogHelper.waitForPopupWindowActiveAndPolished());
if (fileMode != QQuickFileDialog::SaveFile) {
VERIFY_FILE_SELECTED_AND_FOCUSED(QUrl::fromLocalFile(tempDir.path()), tempFile1Url, 1);
} else {
QTRY_COMPARE(dialogHelper.fileDialogListView->currentIndex(), -1);
}
}
void tst_QQuickFileDialogImpl::selectNewFileViaTextField_data()
{
fileMode_data();
}
void tst_QQuickFileDialogImpl::selectNewFileViaTextField()
{
QFETCH(QQuickFileDialog::FileMode, fileMode);
QFETCH(QString, acceptButtonText);
// Open the dialog.
FileDialogTestHelper dialogHelper(this, "fileDialog.qml");
dialogHelper.dialog->setFileMode(fileMode);
if (fileMode == QQuickFileDialog::SaveFile)
dialogHelper.dialog->setSelectedFile(QUrl());
OPEN_QUICK_DIALOG();
QVERIFY(dialogHelper.waitForPopupWindowActiveAndPolished());
const QQuickTextField *fileNameTextField =
dialogHelper.quickDialog->findChild<QQuickTextField *>("fileNameTextField");
QVERIFY(fileNameTextField);
QVERIFY2(fileNameTextField->isVisible() == (fileMode == QQuickFileDialog::SaveFile),
"The TextField for file name should only be visible when the FileMode is 'SaveFile'");
if (fileMode == QQuickFileDialog::SaveFile) {
QVERIFY(dialogHelper.quickDialog->footer());
auto dialogButtonBox = dialogHelper.quickDialog->footer()->findChild<QQuickDialogButtonBox*>();
QVERIFY(dialogButtonBox);
QQuickAbstractButton *acceptButton = findDialogButton(dialogButtonBox, acceptButtonText);
QVERIFY(acceptButton);
QCOMPARE(acceptButton->isEnabled(), false);
const QPoint textFieldCenterPos =
fileNameTextField->mapToScene({ fileNameTextField->width() / 2, fileNameTextField->height() / 2 }).toPoint();
QTest::mouseClick(dialogHelper.popupWindow(), Qt::LeftButton, Qt::NoModifier, textFieldCenterPos);
QTRY_VERIFY(fileNameTextField->hasActiveFocus());
QCOMPARE(acceptButton->isEnabled(), false);
const QByteArray newFileName("foo.txt");
for (const auto &c : newFileName)
QTest::keyClick(dialogHelper.popupWindow(), c);
QCOMPARE(acceptButton->isEnabled(), true);
QTest::keyClick(dialogHelper.popupWindow(), Qt::Key_Enter);
QCOMPARE(acceptButton->isEnabled(), true);
QTRY_COMPARE(fileNameTextField->text(), newFileName);
QCOMPARE(dialogHelper.dialog->selectedFile().fileName(), newFileName);
QVERIFY(fileNameTextField->hasActiveFocus());
for (int i = 0; i < newFileName.size(); i++)
QTest::keyClick(dialogHelper.popupWindow(), Qt::Key_Backspace);
QCOMPARE(acceptButton->isEnabled(), false);
}
}
void tst_QQuickFileDialogImpl::selectExistingFileShouldWarnUserWhenFileModeEqualsSaveFile()
{
FileDialogTestHelper dialogHelper(this, "fileDialog.qml");
dialogHelper.dialog->setFileMode(QQuickFileDialog::SaveFile);
dialogHelper.dialog->setSelectedFile(QUrl::fromLocalFile(tempFile1->fileName()));
OPEN_QUICK_DIALOG();
QVERIFY(dialogHelper.waitForPopupWindowActiveAndPolished());
QSignalSpy acceptedSpy(dialogHelper.dialog, SIGNAL(accepted()));
auto *dialogButtonBox = dialogHelper.quickDialog->footer()->findChild<QQuickDialogButtonBox *>();
QVERIFY(dialogButtonBox);
auto *confirmationDialog = dialogHelper.quickDialog->findChild<QQuickDialog *>("confirmationDialog");
QVERIFY(confirmationDialog);
auto *openButton = dialogButtonBox->standardButton(QPlatformDialogHelper::Open);
QVERIFY(openButton);
auto *confirmationButtonBox = qobject_cast<QQuickDialogButtonBox *>(confirmationDialog->footer());
QVERIFY(confirmationButtonBox);
QVERIFY(clickButton(openButton));
QTRY_VERIFY(confirmationDialog->isOpened());
QVERIFY(dialogHelper.dialog->isVisible());
// Yes button should have focus by default
QTest::keyClick(dialogHelper.popupWindow(), Qt::Key_Space, Qt::NoModifier);
QTRY_VERIFY(!confirmationDialog->isOpened());
QVERIFY(!dialogHelper.dialog->isVisible());
QCOMPARE(acceptedSpy.count(), 1);
// Try again, but click "No" this time.
QVERIFY(dialogHelper.openDialog());
QTRY_VERIFY(dialogHelper.isQuickDialogOpen());
QVERIFY(dialogHelper.waitForPopupWindowActiveAndPolished());
QVERIFY(clickButton(openButton));
QTRY_VERIFY(confirmationDialog->isOpened());
QVERIFY(dialogHelper.dialog->isVisible());
// Make No button trigger a clicked() event.
auto *confirmationNoButton = confirmationButtonBox->standardButton(QPlatformDialogHelper::No);
QVERIFY(confirmationNoButton);
QVERIFY(clickButton(confirmationNoButton));
// FileDialog is still opened
QTRY_VERIFY(!confirmationDialog->isOpened());
QVERIFY(dialogHelper.dialog->isVisible());
QCOMPARE(acceptedSpy.count(), 1);
// Try again
QVERIFY(clickButton(openButton));
QTRY_VERIFY(confirmationDialog->isOpened());
QVERIFY(dialogHelper.dialog->isVisible());
// QQuickFileDialogImplPrivate::handleClick() should give the Yes button focus via forceActiveFocus().
QTRY_COMPARE(dialogHelper.popupWindow()->focusObject(), static_cast<QQuickDialogButtonBox *>(confirmationDialog->footer())->standardButton(QPlatformDialogHelper::Yes));
QTest::keyClick(dialogHelper.popupWindow(), Qt::Key_Space, Qt::NoModifier);
QTRY_VERIFY(!confirmationDialog->isOpened());
QVERIFY(!dialogHelper.dialog->isVisible());
QCOMPARE(acceptedSpy.count(), 2);
// Make sure that DontConfirmOverwrite works
dialogHelper.dialog->setOptions(QFileDialogOptions::DontConfirmOverwrite);
QVERIFY(dialogHelper.openDialog());
QTRY_VERIFY(dialogHelper.isQuickDialogOpen());
QVERIFY(dialogHelper.waitForPopupWindowActiveAndPolished());
QVERIFY(clickButton(openButton));
QTRY_VERIFY(!confirmationDialog->isOpened());
QVERIFY(!dialogHelper.dialog->isVisible());
QCOMPARE(acceptedSpy.count(), 3);
}
void tst_QQuickFileDialogImpl::fileNameTextFieldOnlyChangesWhenSelectingFiles()
{
const auto tempSubFile1Url = QUrl::fromLocalFile(tempSubFile1->fileName());
const auto tempSubDirUrl = QUrl::fromLocalFile(tempSubDir.path());
const auto tempFile11Url = QUrl::fromLocalFile(tempFile1->fileName());
const QVariantMap initialProperties = {
{ "tempFile1Url", QVariant::fromValue(tempSubFile1Url) },
{ "fileMode", QVariant::fromValue(QQuickFileDialog::SaveFile) }
};
FileDialogTestHelper dialogHelper(this, "setSelectedFile.qml", {}, initialProperties);
OPEN_QUICK_DIALOG();
QVERIFY(dialogHelper.waitForPopupWindowActiveAndPolished());
QQuickTextField *fileNameTextField =
dialogHelper.quickDialog->findChild<QQuickTextField *>("fileNameTextField");
QVERIFY(fileNameTextField);
auto getSelectedFileInfo = [&dialogHelper]() {
return QFileInfo(dialogHelper.dialog->selectedFile().toLocalFile());
};
QVERIFY(getSelectedFileInfo().isFile());
QCOMPARE(fileNameTextField->text(), tempSubFile1Url.fileName());
QCOMPARE(dialogHelper.dialog->selectedFile(), tempSubFile1Url);
auto *breadcrumbBar = dialogHelper.quickDialog->findChild<QQuickFolderBreadcrumbBar *>();
QVERIFY(breadcrumbBar);
// Pressing the up button causes tempSubDir to be selected
QVERIFY(clickButton(breadcrumbBar->upButton()));
QVERIFY(getSelectedFileInfo().isDir());
QCOMPARE(fileNameTextField->text(), tempSubFile1Url.fileName());
QCOMPARE(dialogHelper.dialog->selectedFile(), tempSubDirUrl);
// Change the selected file from the outside
dialogHelper.dialog->close();
dialogHelper.dialog->setSelectedFile(tempFile11Url);
dialogHelper.openDialog();
QTRY_VERIFY(dialogHelper.isQuickDialogOpen());
QVERIFY(getSelectedFileInfo().isFile());
QCOMPARE(fileNameTextField->text(), tempFile11Url.fileName());
QCOMPARE(dialogHelper.dialog->selectedFile(), tempFile11Url);
}
void tst_QQuickFileDialogImpl::setSchemeForSelectedFile()
{
const auto tempSubFile1Url = QUrl::fromLocalFile(tempSubFile1->fileName());
const QVariantMap initialProperties = {
{ "tempFile1Url", QVariant::fromValue(tempSubFile1Url) },
{ "fileMode", QVariant::fromValue(QQuickFileDialog::SaveFile) }
};
FileDialogTestHelper dialogHelper(this, "setSelectedFile.qml", {}, initialProperties);
OPEN_QUICK_DIALOG();
QVERIFY(dialogHelper.waitForPopupWindowActiveAndPolished());
QQuickTextField *fileNameTextField =
dialogHelper.quickDialog->findChild<QQuickTextField *>("fileNameTextField");
QVERIFY(fileNameTextField);
QVERIFY(!tempSubFile1Url.scheme().isEmpty());
QVERIFY(!dialogHelper.dialog->selectedFile().scheme().isEmpty());
QCOMPARE(tempSubFile1Url, dialogHelper.dialog->selectedFile());
fileNameTextField->clear();
const QPoint textFieldCenterPos =
fileNameTextField->mapToScene({ fileNameTextField->width() / 2, fileNameTextField->height() / 2 }).toPoint();
QTest::mouseClick(dialogHelper.popupWindow(), Qt::LeftButton, Qt::NoModifier, textFieldCenterPos);
const QByteArray newFileName("helloworld.txt");
for (const auto &c : newFileName)
QTest::keyClick(dialogHelper.popupWindow(), c);
QTest::keyClick(dialogHelper.popupWindow(), Qt::Key_Enter, Qt::NoModifier);
QTRY_COMPARE(fileNameTextField->text(), QString::fromLatin1(newFileName));
const auto newFilePath =
QUrl::fromLocalFile(QFileInfo(tempSubFile1Url.toLocalFile()).dir().absolutePath() + u'/' + newFileName);
QVERIFY(!newFilePath.scheme().isEmpty());
QVERIFY(!dialogHelper.dialog->selectedFile().scheme().isEmpty());
QCOMPARE(dialogHelper.dialog->selectedFile(), newFilePath);
}
void tst_QQuickFileDialogImpl::reopenAfterHideEvent()
{
// Open the dialog.
FileDialogTestHelper dialogHelper(this, "fileDialog.qml");
OPEN_QUICK_DIALOG();
QVERIFY(dialogHelper.waitForPopupWindowActiveAndPolished());
QQuickWindow *popupWindow = dialogHelper.popupWindow();
QVERIFY(popupWindow);
QVERIFY(dialogHelper.dialog->isVisible());
QVERIFY(popupWindow->isVisible());
QHideEvent hideEvent;
QCoreApplication::sendEvent(popupWindow, &hideEvent);
QVERIFY(!dialogHelper.isQuickDialogOpen());
QVERIFY(!dialogHelper.dialog->isVisible());
QVERIFY(dialogHelper.openDialog());
QVERIFY(dialogHelper.isQuickDialogOpen());
QVERIFY(dialogHelper.dialog->isVisible());
dialogHelper.dialog->close();
}
void tst_QQuickFileDialogImpl::sidebarStandardPaths()
{
// Open the dialog.
FileDialogTestHelper dialogHelper(this, "fileDialog.qml");
OPEN_QUICK_DIALOG();
QVERIFY(dialogHelper.waitForPopupWindowActiveAndPolished());
QQuickWindow *popupWindow = dialogHelper.popupWindow();
QVERIFY(popupWindow);
QVERIFY(dialogHelper.dialog->isVisible());
QVERIFY(popupWindow->isVisible());
QQuickSideBar *sideBar = dialogHelper.quickDialog->findChild<QQuickSideBar *>();
auto paths = sideBar->effectiveFolderPaths();
int sideBarCount = paths.count();
for (int i = 0; i < sideBarCount; ++i) {
auto *button = qobject_cast<QQuickAbstractButton *>(sideBar->itemAt(i));
QVERIFY(button);
QVERIFY(clickButton(button));
QCOMPARE(dialogHelper.dialog->currentFolder().toLocalFile(), QStandardPaths::writableLocation(paths[i]));
QCOMPARE(dialogHelper.quickDialog->currentFolder().toLocalFile(), QStandardPaths::writableLocation(paths[i]));
}
}
QTEST_MAIN(tst_QQuickFileDialogImpl)
#include "tst_qquickfiledialogimpl.moc"
|