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
|
/****************************************************************************
**
** Copyright (C) 2017 The Qt Company Ltd.
** Contact: http://www.qt.io/licensing/
**
** This file is part of the test suite of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL3$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see http://www.qt.io/terms-conditions. For further
** information use the contact form at http://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 3 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPLv3 included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 3 requirements
** will be met: https://www.gnu.org/licenses/lgpl.html.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 2.0 or later as published by the Free
** Software Foundation and appearing in the file LICENSE.GPL included in
** the packaging of this file. Please review the following information to
** ensure the GNU General Public License version 2.0 requirements will be
** met: http://www.gnu.org/licenses/gpl-2.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include <QtTest/qtest.h>
#include <QtTest/qsignalspy.h>
#include "../shared/menuutil.h"
#include "../shared/util.h"
#include "../shared/visualtestutil.h"
#include "../shared/qtest_quickcontrols.h"
#include <QtGui/qpa/qwindowsysteminterface.h>
#include <QtQuickTemplates2/private/qquickapplicationwindow_p.h>
#include <QtQuickTemplates2/private/qquickcombobox_p.h>
#include <QtQuickTemplates2/private/qquickoverlay_p.h>
#include <QtQuickTemplates2/private/qquickpopup_p.h>
#include <QtQuickTemplates2/private/qquickbutton_p.h>
#include <QtQuickTemplates2/private/qquickslider_p.h>
#include <QtQuickTemplates2/private/qquickstackview_p.h>
using namespace QQuickVisualTestUtil;
class tst_QQuickPopup : public QQmlDataTest
{
Q_OBJECT
private slots:
void initTestCase();
void visible_data();
void visible();
void state();
void overlay_data();
void overlay();
void zOrder_data();
void zOrder();
void windowChange();
void closePolicy_data();
void closePolicy();
void activeFocusOnClose1();
void activeFocusOnClose2();
void activeFocusOnClose3();
void hover_data();
void hover();
void wheel_data();
void wheel();
void parentDestroyed();
void nested();
void grabber();
void cursorShape();
void componentComplete();
void closeOnEscapeWithNestedPopups();
void enabled();
void orientation_data();
void orientation();
};
void tst_QQuickPopup::initTestCase()
{
QQmlDataTest::initTestCase();
qputenv("QML_NO_TOUCH_COMPRESSION", "1");
}
void tst_QQuickPopup::visible_data()
{
QTest::addColumn<QString>("source");
QTest::newRow("Window") << "window.qml";
QTest::newRow("ApplicationWindow") << "applicationwindow.qml";
}
void tst_QQuickPopup::visible()
{
QFETCH(QString, source);
QQuickApplicationHelper helper(this, source);
QQuickWindow *window = helper.window;
window->show();
window->requestActivate();
QVERIFY(QTest::qWaitForWindowActive(window));
QQuickPopup *popup = window->property("popup").value<QQuickPopup*>();
QVERIFY(popup);
QQuickItem *popupItem = popup->popupItem();
popup->open();
QVERIFY(popup->isVisible());
QQuickOverlay *overlay = QQuickOverlay::overlay(window);
QVERIFY(overlay);
QVERIFY(overlay->childItems().contains(popupItem));
popup->close();
QTRY_VERIFY(!popup->isVisible());
QVERIFY(!overlay->childItems().contains(popupItem));
popup->setVisible(true);
QVERIFY(popup->isVisible());
QVERIFY(overlay->childItems().contains(popupItem));
popup->setVisible(false);
QTRY_VERIFY(!popup->isVisible());
QVERIFY(!overlay->childItems().contains(popupItem));
}
void tst_QQuickPopup::state()
{
QQuickApplicationHelper helper(this, "applicationwindow.qml");
QQuickWindow *window = helper.window;
window->show();
QVERIFY(QTest::qWaitForWindowExposed(window));
QQuickPopup *popup = window->property("popup").value<QQuickPopup*>();
QVERIFY(popup);
QCOMPARE(popup->isVisible(), false);
QSignalSpy visibleChangedSpy(popup, SIGNAL(visibleChanged()));
QSignalSpy aboutToShowSpy(popup, SIGNAL(aboutToShow()));
QSignalSpy aboutToHideSpy(popup, SIGNAL(aboutToHide()));
QSignalSpy openedSpy(popup, SIGNAL(opened()));
QSignalSpy closedSpy(popup, SIGNAL(closed()));
QVERIFY(visibleChangedSpy.isValid());
QVERIFY(aboutToShowSpy.isValid());
QVERIFY(aboutToHideSpy.isValid());
QVERIFY(openedSpy.isValid());
QVERIFY(closedSpy.isValid());
popup->open();
QCOMPARE(visibleChangedSpy.count(), 1);
QCOMPARE(aboutToShowSpy.count(), 1);
QCOMPARE(aboutToHideSpy.count(), 0);
QTRY_COMPARE(openedSpy.count(), 1);
QCOMPARE(closedSpy.count(), 0);
popup->close();
QTRY_COMPARE(visibleChangedSpy.count(), 2);
QCOMPARE(aboutToShowSpy.count(), 1);
QCOMPARE(aboutToHideSpy.count(), 1);
QCOMPARE(openedSpy.count(), 1);
QTRY_COMPARE(closedSpy.count(), 1);
}
void tst_QQuickPopup::overlay_data()
{
QTest::addColumn<QString>("source");
QTest::addColumn<bool>("modal");
QTest::addColumn<bool>("dim");
QTest::newRow("Window") << "window.qml" << false << false;
QTest::newRow("Window,dim") << "window.qml" << false << true;
QTest::newRow("Window,modal") << "window.qml" << true << false;
QTest::newRow("Window,modal,dim") << "window.qml" << true << true;
QTest::newRow("ApplicationWindow") << "applicationwindow.qml" << false << false;
QTest::newRow("ApplicationWindow,dim") << "applicationwindow.qml" << false << true;
QTest::newRow("ApplicationWindow,modal") << "applicationwindow.qml" << true << false;
QTest::newRow("ApplicationWindow,modal,dim") << "applicationwindow.qml" << true << true;
}
void tst_QQuickPopup::overlay()
{
QFETCH(QString, source);
QFETCH(bool, modal);
QFETCH(bool, dim);
QQuickApplicationHelper helper(this, source);
QQuickWindow *window = helper.window;
window->show();
window->requestActivate();
QVERIFY(QTest::qWaitForWindowActive(window));
QQuickOverlay *overlay = QQuickOverlay::overlay(window);
QVERIFY(overlay);
QSignalSpy overlayPressedSignal(overlay, SIGNAL(pressed()));
QSignalSpy overlayReleasedSignal(overlay, SIGNAL(released()));
QVERIFY(overlayPressedSignal.isValid());
QVERIFY(overlayReleasedSignal.isValid());
QVERIFY(!overlay->isVisible()); // no popups open
QTest::mouseClick(window, Qt::LeftButton);
QCOMPARE(overlayPressedSignal.count(), 0);
QCOMPARE(overlayReleasedSignal.count(), 0);
QQuickPopup *popup = window->property("popup").value<QQuickPopup*>();
QVERIFY(popup);
QQuickOverlayAttached *overlayAttached = qobject_cast<QQuickOverlayAttached *>(qmlAttachedPropertiesObject<QQuickOverlay>(popup));
QVERIFY(overlayAttached);
QCOMPARE(overlayAttached->overlay(), overlay);
QSignalSpy overlayAttachedPressedSignal(overlayAttached, SIGNAL(pressed()));
QSignalSpy overlayAttachedReleasedSignal(overlayAttached, SIGNAL(released()));
QVERIFY(overlayAttachedPressedSignal.isValid());
QVERIFY(overlayAttachedReleasedSignal.isValid());
QQuickButton *button = window->property("button").value<QQuickButton*>();
QVERIFY(button);
int overlayPressCount = 0;
int overlayReleaseCount = 0;
popup->open();
QVERIFY(popup->isVisible());
QVERIFY(overlay->isVisible());
QTRY_VERIFY(popup->isOpened());
QTest::mousePress(window, Qt::LeftButton, Qt::NoModifier, QPoint(1, 1));
QCOMPARE(overlayPressedSignal.count(), ++overlayPressCount);
QCOMPARE(overlayReleasedSignal.count(), overlayReleaseCount);
QCOMPARE(overlayAttachedPressedSignal.count(), overlayPressCount);
QCOMPARE(overlayAttachedReleasedSignal.count(), overlayReleaseCount);
QTRY_VERIFY(!popup->isVisible());
QVERIFY(!overlay->isVisible());
QTest::mouseRelease(window, Qt::LeftButton, Qt::NoModifier, QPoint(1, 1));
QCOMPARE(overlayPressedSignal.count(), overlayPressCount);
QCOMPARE(overlayReleasedSignal.count(), overlayReleaseCount); // no modal-popups open
QCOMPARE(overlayAttachedPressedSignal.count(), overlayPressCount);
QCOMPARE(overlayAttachedReleasedSignal.count(), overlayReleaseCount);
popup->setDim(dim);
popup->setModal(modal);
popup->setClosePolicy(QQuickPopup::CloseOnReleaseOutside);
// mouse
popup->open();
QVERIFY(popup->isVisible());
QVERIFY(overlay->isVisible());
QTRY_VERIFY(popup->isOpened());
QTest::mousePress(window, Qt::LeftButton, Qt::NoModifier, QPoint(1, 1));
QCOMPARE(overlayPressedSignal.count(), ++overlayPressCount);
QCOMPARE(overlayReleasedSignal.count(), overlayReleaseCount);
QCOMPARE(overlayAttachedPressedSignal.count(), overlayPressCount);
QCOMPARE(overlayAttachedReleasedSignal.count(), overlayReleaseCount);
QTest::mouseRelease(window, Qt::LeftButton, Qt::NoModifier, QPoint(1, 1));
QCOMPARE(overlayPressedSignal.count(), overlayPressCount);
QCOMPARE(overlayReleasedSignal.count(), ++overlayReleaseCount);
QCOMPARE(overlayAttachedPressedSignal.count(), overlayPressCount);
QCOMPARE(overlayAttachedReleasedSignal.count(), overlayReleaseCount);
QTRY_VERIFY(!popup->isVisible());
QVERIFY(!overlay->isVisible());
// touch
popup->open();
QVERIFY(popup->isVisible());
QVERIFY(overlay->isVisible());
struct TouchDeviceDeleter
{
static inline void cleanup(QTouchDevice *device)
{
QWindowSystemInterface::unregisterTouchDevice(device);
delete device;
}
};
QScopedPointer<QTouchDevice, TouchDeviceDeleter> device(new QTouchDevice);
device->setType(QTouchDevice::TouchScreen);
QWindowSystemInterface::registerTouchDevice(device.data());
QTest::touchEvent(window, device.data()).press(0, QPoint(1, 1));
QCOMPARE(overlayPressedSignal.count(), ++overlayPressCount);
QCOMPARE(overlayReleasedSignal.count(), overlayReleaseCount);
QCOMPARE(overlayAttachedPressedSignal.count(), overlayPressCount);
QCOMPARE(overlayAttachedReleasedSignal.count(), overlayReleaseCount);
QTest::touchEvent(window, device.data()).release(0, QPoint(1, 1));
QCOMPARE(overlayPressedSignal.count(), overlayPressCount);
QCOMPARE(overlayReleasedSignal.count(), ++overlayReleaseCount);
QCOMPARE(overlayAttachedPressedSignal.count(), overlayPressCount);
QCOMPARE(overlayAttachedReleasedSignal.count(), overlayReleaseCount);
QTRY_VERIFY(!popup->isVisible());
QVERIFY(!overlay->isVisible());
// multi-touch
popup->open();
QVERIFY(popup->isVisible());
QVERIFY(overlay->isVisible());
QVERIFY(!button->isPressed());
QTest::touchEvent(window, device.data()).press(0, button->mapToScene(QPointF(1, 1)).toPoint());
QVERIFY(popup->isVisible());
QVERIFY(overlay->isVisible());
QCOMPARE(button->isPressed(), !modal);
QCOMPARE(overlayPressedSignal.count(), ++overlayPressCount);
QCOMPARE(overlayReleasedSignal.count(), overlayReleaseCount);
QTest::touchEvent(window, device.data()).stationary(0).press(1, button->mapToScene(QPointF(button->width() / 2, button->height() / 2)).toPoint());
QVERIFY(popup->isVisible());
QVERIFY(overlay->isVisible());
QCOMPARE(button->isPressed(), !modal);
QCOMPARE(overlayPressedSignal.count(), ++overlayPressCount);
QCOMPARE(overlayReleasedSignal.count(), overlayReleaseCount);
QTest::touchEvent(window, device.data()).release(0, button->mapToScene(QPointF(1, 1)).toPoint()).stationary(1);
QTRY_VERIFY(!popup->isVisible());
QVERIFY(!overlay->isVisible());
QVERIFY(!button->isPressed());
QCOMPARE(overlayPressedSignal.count(), overlayPressCount);
QCOMPARE(overlayReleasedSignal.count(), ++overlayReleaseCount);
QTest::touchEvent(window, device.data()).release(1, button->mapToScene(QPointF(button->width() / 2, button->height() / 2)).toPoint());
QVERIFY(!popup->isVisible());
QVERIFY(!overlay->isVisible());
QVERIFY(!button->isPressed());
QCOMPARE(overlayPressedSignal.count(), overlayPressCount);
QCOMPARE(overlayReleasedSignal.count(), overlayReleaseCount);
}
void tst_QQuickPopup::zOrder_data()
{
QTest::addColumn<QString>("source");
QTest::newRow("Window") << "window.qml";
QTest::newRow("ApplicationWindow") << "applicationwindow.qml";
}
void tst_QQuickPopup::zOrder()
{
QFETCH(QString, source);
QQuickApplicationHelper helper(this, source);
QQuickWindow *window = helper.window;
window->show();
window->requestActivate();
QVERIFY(QTest::qWaitForWindowActive(window));
QQuickPopup *popup = window->property("popup").value<QQuickPopup*>();
QVERIFY(popup);
popup->setModal(true);
QQuickPopup *popup2 = window->property("popup2").value<QQuickPopup*>();
QVERIFY(popup2);
popup2->setModal(true);
// show popups in reverse order. popup2 has higher z-order so it appears
// on top and must be closed first, even if the other popup was opened last
popup2->open();
popup->open();
QVERIFY(popup2->isVisible());
QVERIFY(popup->isVisible());
QTest::mouseClick(window, Qt::LeftButton, Qt::NoModifier, QPoint(1, 1));
QTRY_VERIFY(!popup2->isVisible());
QVERIFY(popup->isVisible());
QTest::mouseClick(window, Qt::LeftButton, Qt::NoModifier, QPoint(1, 1));
QVERIFY(!popup2->isVisible());
QTRY_VERIFY(!popup->isVisible());
}
void tst_QQuickPopup::windowChange()
{
QQuickPopup popup;
QSignalSpy spy(&popup, SIGNAL(windowChanged(QQuickWindow*)));
QVERIFY(spy.isValid());
QQuickItem item;
popup.setParentItem(&item);
QVERIFY(!popup.window());
QCOMPARE(spy.count(), 0);
QQuickWindow window;
item.setParentItem(window.contentItem());
QCOMPARE(popup.window(), &window);
QCOMPARE(spy.count(), 1);
item.setParentItem(nullptr);
QVERIFY(!popup.window());
QCOMPARE(spy.count(), 2);
popup.setParentItem(window.contentItem());
QCOMPARE(popup.window(), &window);
QCOMPARE(spy.count(), 3);
popup.resetParentItem();
QVERIFY(!popup.window());
QCOMPARE(spy.count(), 4);
popup.setParent(&window);
popup.resetParentItem();
QCOMPARE(popup.window(), &window);
QCOMPARE(spy.count(), 5);
popup.setParent(this);
popup.resetParentItem();
QVERIFY(!popup.window());
QCOMPARE(spy.count(), 6);
item.setParentItem(window.contentItem());
popup.setParent(&item);
popup.resetParentItem();
QCOMPARE(popup.window(), &window);
QCOMPARE(spy.count(), 7);
popup.setParent(nullptr);
}
Q_DECLARE_METATYPE(QQuickPopup::ClosePolicy)
void tst_QQuickPopup::closePolicy_data()
{
qRegisterMetaType<QQuickPopup::ClosePolicy>();
QTest::addColumn<QString>("source");
QTest::addColumn<QQuickPopup::ClosePolicy>("closePolicy");
QTest::newRow("Window:NoAutoClose") << "window.qml"<< static_cast<QQuickPopup::ClosePolicy>(QQuickPopup::NoAutoClose);
QTest::newRow("Window:CloseOnPressOutside") << "window.qml"<< static_cast<QQuickPopup::ClosePolicy>(QQuickPopup::CloseOnPressOutside);
QTest::newRow("Window:CloseOnPressOutsideParent") << "window.qml"<< static_cast<QQuickPopup::ClosePolicy>(QQuickPopup::CloseOnPressOutsideParent);
QTest::newRow("Window:CloseOnPressOutside|Parent") << "window.qml"<< static_cast<QQuickPopup::ClosePolicy>(QQuickPopup::CloseOnPressOutside | QQuickPopup::CloseOnPressOutsideParent);
QTest::newRow("Window:CloseOnReleaseOutside") << "window.qml"<< static_cast<QQuickPopup::ClosePolicy>(QQuickPopup::CloseOnReleaseOutside);
QTest::newRow("Window:CloseOnReleaseOutside|Parent") << "window.qml"<< static_cast<QQuickPopup::ClosePolicy>(QQuickPopup::CloseOnReleaseOutside | QQuickPopup::CloseOnReleaseOutsideParent);
QTest::newRow("Window:CloseOnEscape") << "window.qml"<< static_cast<QQuickPopup::ClosePolicy>(QQuickPopup::CloseOnEscape);
QTest::newRow("ApplicationWindow:NoAutoClose") << "applicationwindow.qml"<< static_cast<QQuickPopup::ClosePolicy>(QQuickPopup::NoAutoClose);
QTest::newRow("ApplicationWindow:CloseOnPressOutside") << "applicationwindow.qml"<< static_cast<QQuickPopup::ClosePolicy>(QQuickPopup::CloseOnPressOutside);
QTest::newRow("ApplicationWindow:CloseOnPressOutsideParent") << "applicationwindow.qml"<< static_cast<QQuickPopup::ClosePolicy>(QQuickPopup::CloseOnPressOutsideParent);
QTest::newRow("ApplicationWindow:CloseOnPressOutside|Parent") << "applicationwindow.qml"<< static_cast<QQuickPopup::ClosePolicy>(QQuickPopup::CloseOnPressOutside | QQuickPopup::CloseOnPressOutsideParent);
QTest::newRow("ApplicationWindow:CloseOnReleaseOutside") << "applicationwindow.qml"<< static_cast<QQuickPopup::ClosePolicy>(QQuickPopup::CloseOnReleaseOutside);
QTest::newRow("ApplicationWindow:CloseOnReleaseOutside|Parent") << "applicationwindow.qml"<< static_cast<QQuickPopup::ClosePolicy>(QQuickPopup::CloseOnReleaseOutside | QQuickPopup::CloseOnReleaseOutsideParent);
QTest::newRow("ApplicationWindow:CloseOnEscape") << "applicationwindow.qml"<< static_cast<QQuickPopup::ClosePolicy>(QQuickPopup::CloseOnEscape);
}
void tst_QQuickPopup::closePolicy()
{
QFETCH(QString, source);
QFETCH(QQuickPopup::ClosePolicy, closePolicy);
QQuickApplicationHelper helper(this, source);
QQuickWindow *window = helper.window;
window->show();
window->requestActivate();
QVERIFY(QTest::qWaitForWindowActive(window));
QQuickPopup *popup = window->property("popup").value<QQuickPopup*>();
QVERIFY(popup);
QQuickButton *button = window->property("button").value<QQuickButton*>();
QVERIFY(button);
popup->setModal(true);
popup->setFocus(true);
popup->setClosePolicy(closePolicy);
popup->open();
QVERIFY(popup->isVisible());
QTRY_VERIFY(popup->isOpened());
// press outside popup and its parent
QTest::mousePress(window, Qt::LeftButton, Qt::NoModifier, QPoint(1, 1));
if (closePolicy.testFlag(QQuickPopup::CloseOnPressOutside) || closePolicy.testFlag(QQuickPopup::CloseOnPressOutsideParent))
QTRY_VERIFY(!popup->isVisible());
else
QVERIFY(popup->isVisible());
popup->open();
QVERIFY(popup->isVisible());
QTRY_VERIFY(popup->isOpened());
// release outside popup and its parent
QTest::mouseRelease(window, Qt::LeftButton, Qt::NoModifier, QPoint(1, 1));
if (closePolicy.testFlag(QQuickPopup::CloseOnReleaseOutside))
QTRY_VERIFY(!popup->isVisible());
else
QVERIFY(popup->isVisible());
popup->open();
QVERIFY(popup->isVisible());
QTRY_VERIFY(popup->isOpened());
// press outside popup but inside its parent
QTest::mousePress(window, Qt::LeftButton, Qt::NoModifier, QPoint(button->x() + 1, button->y() + 1));
if (closePolicy.testFlag(QQuickPopup::CloseOnPressOutside) && !closePolicy.testFlag(QQuickPopup::CloseOnPressOutsideParent))
QTRY_VERIFY(!popup->isVisible());
else
QVERIFY(popup->isVisible());
popup->open();
QVERIFY(popup->isVisible());
QTRY_VERIFY(popup->isOpened());
// release outside popup but inside its parent
QTest::mouseRelease(window, Qt::LeftButton, Qt::NoModifier, QPoint(button->x() + 1, button->y() + 1));
if (closePolicy.testFlag(QQuickPopup::CloseOnReleaseOutside) && !closePolicy.testFlag(QQuickPopup::CloseOnReleaseOutsideParent))
QTRY_VERIFY(!popup->isVisible());
else
QVERIFY(popup->isVisible());
popup->open();
QVERIFY(popup->isVisible());
QTRY_VERIFY(popup->isOpened());
// press inside and release outside
QTest::mousePress(window, Qt::LeftButton, Qt::NoModifier, QPoint(button->x() + popup->x() + 1,
button->y() + popup->y() + 1));
QVERIFY(popup->isVisible());
QTest::mouseRelease(window, Qt::LeftButton, Qt::NoModifier, QPoint(1, 1));
QVERIFY(popup->isVisible());
// escape
QTest::keyClick(window, Qt::Key_Escape);
if (closePolicy.testFlag(QQuickPopup::CloseOnEscape))
QTRY_VERIFY(!popup->isVisible());
else
QVERIFY(popup->isVisible());
}
void tst_QQuickPopup::activeFocusOnClose1()
{
// Test that a popup that never sets focus: true (e.g. ToolTip) doesn't affect
// the active focus item when it closes.
QQuickApplicationHelper helper(this, QStringLiteral("activeFocusOnClose1.qml"));
QQuickApplicationWindow *window = helper.appWindow;
window->show();
window->requestActivate();
QVERIFY(QTest::qWaitForWindowActive(window));
QQuickPopup *focusedPopup = helper.appWindow->property("focusedPopup").value<QQuickPopup*>();
QVERIFY(focusedPopup);
QQuickPopup *nonFocusedPopup = helper.appWindow->property("nonFocusedPopup").value<QQuickPopup*>();
QVERIFY(nonFocusedPopup);
focusedPopup->open();
QVERIFY(focusedPopup->isVisible());
QTRY_VERIFY(focusedPopup->isOpened());
QVERIFY(focusedPopup->hasActiveFocus());
nonFocusedPopup->open();
QVERIFY(nonFocusedPopup->isVisible());
QTRY_VERIFY(nonFocusedPopup->isOpened());
QVERIFY(focusedPopup->hasActiveFocus());
nonFocusedPopup->close();
QTRY_VERIFY(!nonFocusedPopup->isVisible());
QVERIFY(focusedPopup->hasActiveFocus());
// QTBUG-66113: force active focus on a popup that did not request focus
nonFocusedPopup->open();
nonFocusedPopup->forceActiveFocus();
QVERIFY(nonFocusedPopup->isVisible());
QTRY_VERIFY(nonFocusedPopup->isOpened());
QVERIFY(nonFocusedPopup->hasActiveFocus());
nonFocusedPopup->close();
QTRY_VERIFY(!nonFocusedPopup->isVisible());
QVERIFY(focusedPopup->hasActiveFocus());
}
void tst_QQuickPopup::activeFocusOnClose2()
{
// Test that a popup that sets focus: true but relinquishes focus (e.g. by
// calling forceActiveFocus() on another item) before it closes doesn't
// affect the active focus item when it closes.
QQuickApplicationHelper helper(this, QStringLiteral("activeFocusOnClose2.qml"));
QQuickApplicationWindow *window = helper.appWindow;
window->show();
window->requestActivate();
QVERIFY(QTest::qWaitForWindowActive(window));
QQuickPopup *popup1 = helper.appWindow->property("popup1").value<QQuickPopup*>();
QVERIFY(popup1);
QQuickPopup *popup2 = helper.appWindow->property("popup2").value<QQuickPopup*>();
QVERIFY(popup2);
QQuickButton *closePopup2Button = helper.appWindow->property("closePopup2Button").value<QQuickButton*>();
QVERIFY(closePopup2Button);
popup1->open();
QVERIFY(popup1->isVisible());
QTRY_VERIFY(popup1->isOpened());
QVERIFY(popup1->hasActiveFocus());
popup2->open();
QVERIFY(popup2->isVisible());
QTRY_VERIFY(popup2->isOpened());
QVERIFY(popup2->hasActiveFocus());
// Causes popup1.contentItem.forceActiveFocus() to be called, then closes popup2.
QTest::mouseClick(window, Qt::LeftButton, Qt::NoModifier,
closePopup2Button->mapToScene(QPointF(closePopup2Button->width() / 2, closePopup2Button->height() / 2)).toPoint());
QTRY_VERIFY(!popup2->isVisible());
QVERIFY(popup1->hasActiveFocus());
}
void tst_QQuickPopup::activeFocusOnClose3()
{
// Test that a closing popup that had focus doesn't steal focus from
// another popup that the focus was transferred to.
QQuickApplicationHelper helper(this, QStringLiteral("activeFocusOnClose3.qml"));
QQuickApplicationWindow *window = helper.appWindow;
window->show();
window->requestActivate();
QVERIFY(QTest::qWaitForWindowActive(window));
QQuickPopup *popup1 = helper.appWindow->property("popup1").value<QQuickPopup*>();
QVERIFY(popup1);
QQuickPopup *popup2 = helper.appWindow->property("popup2").value<QQuickPopup*>();
QVERIFY(popup2);
popup1->open();
QVERIFY(popup1->isVisible());
QTRY_VERIFY(popup1->hasActiveFocus());
popup2->open();
popup1->close();
QSignalSpy closedSpy(popup1, SIGNAL(closed()));
QVERIFY(closedSpy.isValid());
QVERIFY(closedSpy.wait());
QVERIFY(!popup1->isVisible());
QVERIFY(popup2->isVisible());
QTRY_VERIFY(popup2->hasActiveFocus());
}
void tst_QQuickPopup::hover_data()
{
QTest::addColumn<QString>("source");
QTest::addColumn<bool>("modal");
QTest::newRow("Window:modal") << "window-hover.qml" << true;
QTest::newRow("Window:modeless") << "window-hover.qml" << false;
QTest::newRow("ApplicationWindow:modal") << "applicationwindow-hover.qml" << true;
QTest::newRow("ApplicationWindow:modeless") << "applicationwindow-hover.qml" << false;
}
void tst_QQuickPopup::hover()
{
QFETCH(QString, source);
QFETCH(bool, modal);
QQuickApplicationHelper helper(this, source);
QQuickWindow *window = helper.window;
window->show();
window->requestActivate();
QVERIFY(QTest::qWaitForWindowActive(window));
QQuickPopup *popup = window->property("popup").value<QQuickPopup*>();
QVERIFY(popup);
popup->setModal(modal);
QQuickButton *parentButton = window->property("parentButton").value<QQuickButton*>();
QVERIFY(parentButton);
parentButton->setHoverEnabled(true);
QQuickButton *childButton = window->property("childButton").value<QQuickButton*>();
QVERIFY(childButton);
childButton->setHoverEnabled(true);
QSignalSpy openedSpy(popup, SIGNAL(opened()));
QVERIFY(openedSpy.isValid());
popup->open();
QVERIFY(openedSpy.count() == 1 || openedSpy.wait());
// hover the parent button outside the popup
QTest::mouseMove(window, QPoint(window->width() - 1, window->height() - 1));
QCOMPARE(parentButton->isHovered(), !modal);
QVERIFY(!childButton->isHovered());
// hover the popup background
QTest::mouseMove(window, QPoint(1, 1));
QVERIFY(!parentButton->isHovered());
QVERIFY(!childButton->isHovered());
// hover the child button in a popup
QTest::mouseMove(window, QPoint(popup->x() + popup->width() / 2, popup->y() + popup->height() / 2));
QVERIFY(!parentButton->isHovered());
QVERIFY(childButton->isHovered());
QSignalSpy closedSpy(popup, SIGNAL(closed()));
QVERIFY(closedSpy.isValid());
popup->close();
QVERIFY(closedSpy.count() == 1 || closedSpy.wait());
// hover the parent button after closing the popup
QTest::mouseMove(window, QPoint(window->width() / 2, window->height() / 2));
QVERIFY(parentButton->isHovered());
}
void tst_QQuickPopup::wheel_data()
{
QTest::addColumn<QString>("source");
QTest::addColumn<bool>("modal");
QTest::newRow("Window:modal") << "window-wheel.qml" << true;
QTest::newRow("Window:modeless") << "window-wheel.qml" << false;
QTest::newRow("ApplicationWindow:modal") << "applicationwindow-wheel.qml" << true;
QTest::newRow("ApplicationWindow:modeless") << "applicationwindow-wheel.qml" << false;
}
static bool sendWheelEvent(QQuickItem *item, const QPoint &localPos, int degrees)
{
QQuickWindow *window = item->window();
QWheelEvent wheelEvent(localPos, item->window()->mapToGlobal(localPos), QPoint(0, 0), QPoint(0, 8 * degrees), 0, Qt::Vertical, Qt::NoButton, 0);
QSpontaneKeyEvent::setSpontaneous(&wheelEvent);
return qGuiApp->notify(window, &wheelEvent);
}
void tst_QQuickPopup::wheel()
{
QFETCH(QString, source);
QFETCH(bool, modal);
QQuickApplicationHelper helper(this, source);
QQuickWindow *window = helper.window;
window->show();
QVERIFY(QTest::qWaitForWindowExposed(window));
QQuickSlider *contentSlider = window->property("contentSlider").value<QQuickSlider*>();
QVERIFY(contentSlider);
QQuickPopup *popup = window->property("popup").value<QQuickPopup*>();
QVERIFY(popup && popup->contentItem());
popup->setModal(modal);
QQuickSlider *popupSlider = window->property("popupSlider").value<QQuickSlider*>();
QVERIFY(popupSlider);
{
// wheel over the content
qreal oldContentValue = contentSlider->value();
qreal oldPopupValue = popupSlider->value();
QVERIFY(sendWheelEvent(contentSlider, QPoint(contentSlider->width() / 2, contentSlider->height() / 2), 15));
QVERIFY(!qFuzzyCompare(contentSlider->value(), oldContentValue)); // must have moved
QVERIFY(qFuzzyCompare(popupSlider->value(), oldPopupValue)); // must not have moved
}
QSignalSpy openedSpy(popup, SIGNAL(opened()));
QVERIFY(openedSpy.isValid());
popup->open();
QVERIFY(openedSpy.count() == 1 || openedSpy.wait());
{
// wheel over the popup content
qreal oldContentValue = contentSlider->value();
qreal oldPopupValue = popupSlider->value();
QVERIFY(sendWheelEvent(popupSlider, QPoint(popupSlider->width() / 2, popupSlider->height() / 2), 15));
QVERIFY(qFuzzyCompare(contentSlider->value(), oldContentValue)); // must not have moved
QVERIFY(!qFuzzyCompare(popupSlider->value(), oldPopupValue)); // must have moved
}
{
// wheel over the overlay
qreal oldContentValue = contentSlider->value();
qreal oldPopupValue = popupSlider->value();
QVERIFY(sendWheelEvent(QQuickOverlay::overlay(window), QPoint(0, 0), 15));
if (modal) {
// the content below a modal overlay must not move
QVERIFY(qFuzzyCompare(contentSlider->value(), oldContentValue));
} else {
// the content below a modeless overlay must move
QVERIFY(!qFuzzyCompare(contentSlider->value(), oldContentValue));
}
QVERIFY(qFuzzyCompare(popupSlider->value(), oldPopupValue)); // must not have moved
}
}
void tst_QQuickPopup::parentDestroyed()
{
QQuickPopup popup;
popup.setParentItem(new QQuickItem);
delete popup.parentItem();
QVERIFY(!popup.parentItem());
}
void tst_QQuickPopup::nested()
{
QQuickApplicationHelper helper(this, QStringLiteral("nested.qml"));
QQuickWindow *window = helper.window;
window->show();
QVERIFY(QTest::qWaitForWindowExposed(window));
QQuickPopup *modalPopup = window->property("modalPopup").value<QQuickPopup *>();
QVERIFY(modalPopup);
QQuickPopup *modelessPopup = window->property("modelessPopup").value<QQuickPopup *>();
QVERIFY(modelessPopup);
modalPopup->open();
QCOMPARE(modalPopup->isVisible(), true);
modelessPopup->open();
QCOMPARE(modelessPopup->isVisible(), true);
// click outside the modeless popup on the top, but inside the modal popup below
QTest::mouseClick(window, Qt::LeftButton, Qt::NoModifier, QPoint(150, 150));
QTRY_COMPARE(modelessPopup->isVisible(), false);
QCOMPARE(modalPopup->isVisible(), true);
}
// QTBUG-56697
void tst_QQuickPopup::grabber()
{
QQuickApplicationHelper helper(this, QStringLiteral("grabber.qml"));
QQuickWindow *window = helper.window;
window->show();
QVERIFY(QTest::qWaitForWindowExposed(window));
QQuickPopup *menu = window->property("menu").value<QQuickPopup *>();
QVERIFY(menu);
QQuickPopup *popup = window->property("popup").value<QQuickPopup *>();
QVERIFY(popup);
QQuickPopup *combo = window->property("combo").value<QQuickPopup *>();
QVERIFY(combo);
menu->open();
QTRY_COMPARE(menu->isOpened(), true);
waitForMenuListViewPolish(menu);
QCOMPARE(popup->isVisible(), false);
QCOMPARE(combo->isVisible(), false);
// click a menu item to open the popup
QTest::mouseClick(window, Qt::LeftButton, Qt::NoModifier, QPoint(menu->width() / 2, menu->height() / 2));
QTRY_COMPARE(menu->isVisible(), false);
QTRY_COMPARE(popup->isOpened(), true);
QCOMPARE(combo->isVisible(), false);
combo->open();
QCOMPARE(menu->isVisible(), false);
QCOMPARE(popup->isVisible(), true);
QTRY_COMPARE(combo->isOpened(), true);
// click outside to close both the combo popup and the parent popup
QTest::mouseClick(window, Qt::LeftButton, Qt::NoModifier, QPoint(window->width() - 1, window->height() - 1));
QCOMPARE(menu->isVisible(), false);
QTRY_COMPARE(popup->isVisible(), false);
QTRY_COMPARE(combo->isVisible(), false);
menu->open();
QTRY_COMPARE(menu->isOpened(), true);
waitForMenuListViewPolish(menu);
QCOMPARE(popup->isVisible(), false);
QCOMPARE(combo->isVisible(), false);
// click outside the menu to close it (QTBUG-56697)
QTest::mouseClick(window, Qt::LeftButton, Qt::NoModifier, QPoint(window->width() - 1, window->height() - 1));
QTRY_COMPARE(menu->isVisible(), false);
QCOMPARE(popup->isVisible(), false);
QCOMPARE(combo->isVisible(), false);
}
void tst_QQuickPopup::cursorShape()
{
// Ensure that the mouse cursor has the correct shape when over a popup
// which is itself over an item with a different shape.
QQuickApplicationHelper helper(this, QStringLiteral("cursor.qml"));
QQuickApplicationWindow *window = helper.appWindow;
window->show();
QVERIFY(QTest::qWaitForWindowExposed(window));
QQuickPopup *popup = helper.appWindow->property("popup").value<QQuickPopup*>();
QVERIFY(popup);
popup->open();
QVERIFY(popup->isVisible());
QTRY_VERIFY(popup->isOpened());
QQuickItem *textField = helper.appWindow->property("textField").value<QQuickItem*>();
QVERIFY(textField);
// Move the mouse over the text field.
const QPoint textFieldPos(popup->x() - 10, textField->height() / 2);
QTest::mouseMove(window, textFieldPos);
QCOMPARE(window->cursor().shape(), textField->cursor().shape());
// Move the mouse over the popup where it overlaps with the text field.
const QPoint textFieldOverlapPos(popup->x() + 10, textField->height() / 2);
QTest::mouseMove(window, textFieldOverlapPos);
QCOMPARE(window->cursor().shape(), popup->popupItem()->cursor().shape());
popup->close();
QTRY_VERIFY(!popup->isVisible());
}
class FriendlyPopup : public QQuickPopup
{
friend class tst_QQuickPopup;
};
void tst_QQuickPopup::componentComplete()
{
FriendlyPopup cppPopup;
QVERIFY(cppPopup.isComponentComplete());
QQmlEngine engine;
QQmlComponent component(&engine);
component.setData("import QtQuick.Controls 2.2; Popup { }", QUrl());
FriendlyPopup *qmlPopup = static_cast<FriendlyPopup *>(component.beginCreate(engine.rootContext()));
QVERIFY(qmlPopup);
QVERIFY(!qmlPopup->isComponentComplete());
component.completeCreate();
QVERIFY(qmlPopup->isComponentComplete());
}
void tst_QQuickPopup::closeOnEscapeWithNestedPopups()
{
// Tests the scenario in the Gallery example, where there are nested popups that should
// close in the correct order when the Escape key is pressed.
QQuickApplicationHelper helper(this, QStringLiteral("closeOnEscapeWithNestedPopups.qml"));
QQuickApplicationWindow *window = helper.appWindow;
window->show();
QVERIFY(QTest::qWaitForWindowExposed(window));
// The stack view should have two items, and it should pop the second when escape is pressed
// and it has focus.
QQuickStackView *stackView = window->findChild<QQuickStackView*>("stackView");
QVERIFY(stackView);
QCOMPARE(stackView->depth(), 2);
QQuickItem *optionsToolButton = window->findChild<QQuickItem*>("optionsToolButton");
QVERIFY(optionsToolButton);
// Click on the options tool button. The settings menu should pop up.
const QPoint optionsToolButtonCenter = optionsToolButton->mapToScene(
QPointF(optionsToolButton->width() / 2, optionsToolButton->height() / 2)).toPoint();
QTest::mouseClick(window, Qt::LeftButton, Qt::NoModifier, optionsToolButtonCenter);
QQuickPopup *optionsMenu = window->findChild<QQuickPopup*>("optionsMenu");
QVERIFY(optionsMenu);
QTRY_VERIFY(optionsMenu->isVisible());
waitForMenuListViewPolish(optionsMenu);
QQuickItem *settingsMenuItem = window->findChild<QQuickItem*>("settingsMenuItem");
QVERIFY(settingsMenuItem);
// Click on the settings menu item. The settings dialog should pop up.
const QPoint settingsMenuItemCenter = settingsMenuItem->mapToScene(
QPointF(settingsMenuItem->width() / 2, settingsMenuItem->height() / 2)).toPoint();
QTest::mouseClick(window, Qt::LeftButton, Qt::NoModifier, settingsMenuItemCenter);
QQuickPopup *settingsDialog = window->contentItem()->findChild<QQuickPopup*>("settingsDialog");
QVERIFY(settingsDialog);
QTRY_VERIFY(settingsDialog->isVisible());
QQuickComboBox *comboBox = window->contentItem()->findChild<QQuickComboBox*>("comboBox");
QVERIFY(comboBox);
// Click on the combo box button. The combo box popup should pop up.
const QPoint comboBoxCenter = comboBox->mapToScene(
QPointF(comboBox->width() / 2, comboBox->height() / 2)).toPoint();
QTest::mouseClick(window, Qt::LeftButton, Qt::NoModifier, comboBoxCenter);
QTRY_VERIFY(comboBox->popup()->isVisible());
// Close the combo box popup with the escape key. The settings dialog should still be visible.
QTest::keyClick(window, Qt::Key_Escape);
QTRY_VERIFY(!comboBox->popup()->isVisible());
QVERIFY(settingsDialog->isVisible());
// Close the settings dialog with the escape key.
QTest::keyClick(window, Qt::Key_Escape);
QTRY_VERIFY(!settingsDialog->isVisible());
// The stack view should still have two items.
QCOMPARE(stackView->depth(), 2);
// Remove one by pressing the Escape key (the Shortcut should be activated).
QTest::keyClick(window, Qt::Key_Escape);
QCOMPARE(stackView->depth(), 1);
}
void tst_QQuickPopup::enabled()
{
QQuickPopup popup;
QVERIFY(popup.isEnabled());
QVERIFY(popup.popupItem()->isEnabled());
QSignalSpy enabledSpy(&popup, &QQuickPopup::enabledChanged);
QVERIFY(enabledSpy.isValid());
popup.setEnabled(false);
QVERIFY(!popup.isEnabled());
QVERIFY(!popup.popupItem()->isEnabled());
QCOMPARE(enabledSpy.count(), 1);
popup.popupItem()->setEnabled(true);
QVERIFY(popup.isEnabled());
QVERIFY(popup.popupItem()->isEnabled());
QCOMPARE(enabledSpy.count(), 2);
}
void tst_QQuickPopup::orientation_data()
{
QTest::addColumn<Qt::ScreenOrientation>("orientation");
QTest::addColumn<QPointF>("position");
QTest::newRow("Portrait") << Qt::PortraitOrientation << QPointF(330, 165);
QTest::newRow("Landscape") << Qt::LandscapeOrientation << QPointF(165, 270);
QTest::newRow("InvertedPortrait") << Qt::InvertedPortraitOrientation << QPointF(270, 135);
QTest::newRow("InvertedLandscape") << Qt::InvertedLandscapeOrientation << QPointF(135, 330);
}
void tst_QQuickPopup::orientation()
{
QFETCH(Qt::ScreenOrientation, orientation);
QFETCH(QPointF, position);
QQuickApplicationHelper helper(this, "orientation.qml");
QQuickWindow *window = helper.window;
window->reportContentOrientationChange(orientation);
window->show();
QVERIFY(QTest::qWaitForWindowActive(window));
QQuickPopup *popup = window->property("popup").value<QQuickPopup*>();
QVERIFY(popup);
popup->open();
QCOMPARE(popup->popupItem()->position(), position);
}
QTEST_QUICKCONTROLS_MAIN(tst_QQuickPopup)
#include "tst_qquickpopup.moc"
|