/**************************************************************************** ** ** Copyright (C) 2015 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 #include #include #include #include #include #include #include #include "../shared/util.h" #include "../shared/visualtestutil.h" using namespace QQuickVisualTestUtil; class tst_applicationwindow : public QQmlDataTest { Q_OBJECT public: private slots: void qmlCreation(); void activeFocusOnTab1(); void activeFocusOnTab2(); void defaultFocus(); void implicitFill(); void attachedProperties(); }; void tst_applicationwindow::qmlCreation() { QQmlEngine engine; QQmlComponent component(&engine); component.loadUrl(testFileUrl("basicapplicationwindow.qml")); QObject* created = component.create(); QScopedPointer cleanup(created); QVERIFY(created); QQuickWindow* window = qobject_cast(created); QVERIFY(window); QVERIFY(!window->isVisible()); QCOMPARE(created->property("title"), QVariant("Test Application Window")); QQuickItem* statusBar = qvariant_cast(created->property("statusBar")); QVERIFY(!statusBar); QQuickItem* header = qvariant_cast(created->property("header")); QVERIFY(!header); QQuickItem* footer = qvariant_cast(created->property("footer")); QVERIFY(!footer); } void tst_applicationwindow::activeFocusOnTab1() { QQmlEngine engine; QQmlComponent component(&engine); component.loadUrl(testFileUrl("activefocusontab.qml")); QObject* created = component.create(); QScopedPointer cleanup(created); QVERIFY(created); QQuickWindow* window = qobject_cast(created); QVERIFY(window); window->show(); window->requestActivate(); QVERIFY(QTest::qWaitForWindowActive(window)); QVERIFY(QGuiApplication::focusWindow() == window); QQuickItem* contentItem = window->contentItem(); QVERIFY(contentItem); QVERIFY(contentItem->hasActiveFocus()); QQuickItem* item = findItem(window->contentItem(), "sub1"); QVERIFY(item); QVERIFY(!item->hasActiveFocus()); // Tab: contentItem->sub1 QKeyEvent key(QEvent::KeyPress, Qt::Key_Tab, Qt::NoModifier, "", false, 1); QGuiApplication::sendEvent(window, &key); QVERIFY(key.isAccepted()); item = findItem(window->contentItem(), "sub1"); QVERIFY(item); QVERIFY(item->hasActiveFocus()); // Tab: sub1->sub2 key = QKeyEvent(QEvent::KeyPress, Qt::Key_Tab, Qt::NoModifier, "", false, 1); QGuiApplication::sendEvent(window, &key); QVERIFY(key.isAccepted()); item = findItem(window->contentItem(), "sub2"); QVERIFY(item); QVERIFY(item->hasActiveFocus()); // Tab: sub2->sub1 key = QKeyEvent(QEvent::KeyPress, Qt::Key_Tab, Qt::NoModifier, "", false, 1); QGuiApplication::sendEvent(window, &key); QVERIFY(key.isAccepted()); item = findItem(window->contentItem(), "sub1"); QVERIFY(item); QVERIFY(item->hasActiveFocus()); } void tst_applicationwindow::activeFocusOnTab2() { QQmlEngine engine; QQmlComponent component(&engine); component.loadUrl(testFileUrl("activefocusontab.qml")); QObject* created = component.create(); QScopedPointer cleanup(created); QVERIFY(created); QQuickWindow* window = qobject_cast(created); QVERIFY(window); window->show(); window->requestActivate(); QVERIFY(QTest::qWaitForWindowActive(window)); QVERIFY(QGuiApplication::focusWindow() == window); QQuickItem* contentItem = window->contentItem(); QVERIFY(contentItem); QVERIFY(contentItem->hasActiveFocus()); QQuickItem* item = findItem(window->contentItem(), "sub2"); QVERIFY(item); QVERIFY(!item->hasActiveFocus()); // BackTab: contentItem->sub2 QKeyEvent key(QEvent::KeyPress, Qt::Key_Tab, Qt::ShiftModifier, "", false, 1); QGuiApplication::sendEvent(window, &key); QVERIFY(key.isAccepted()); item = findItem(window->contentItem(), "sub2"); QVERIFY(item); QVERIFY(item->hasActiveFocus()); // BackTab: sub2->sub1 key = QKeyEvent(QEvent::KeyPress, Qt::Key_Tab, Qt::ShiftModifier, "", false, 1); QGuiApplication::sendEvent(window, &key); QVERIFY(key.isAccepted()); item = findItem(window->contentItem(), "sub1"); QVERIFY(item); QVERIFY(item->hasActiveFocus()); // BackTab: sub1->sub2 key = QKeyEvent(QEvent::KeyPress, Qt::Key_Tab, Qt::ShiftModifier, "", false, 1); QGuiApplication::sendEvent(window, &key); QVERIFY(key.isAccepted()); item = findItem(window->contentItem(), "sub2"); QVERIFY(item); QVERIFY(item->hasActiveFocus()); } void tst_applicationwindow::defaultFocus() { QQmlEngine engine; QQmlComponent component(&engine); component.loadUrl(testFileUrl("defaultFocus.qml")); QObject* created = component.create(); QScopedPointer cleanup(created); Q_UNUSED(cleanup); QVERIFY(created); QQuickWindow* window = qobject_cast(created); QVERIFY(window); window->show(); window->requestActivate(); QVERIFY(QTest::qWaitForWindowActive(window)); QVERIFY(QGuiApplication::focusWindow() == window); QQuickItem* contentItem = window->contentItem(); QVERIFY(contentItem); QVERIFY(contentItem->hasActiveFocus()); // A single item in an ApplicationWindow with focus: true should receive focus. QQuickItem* item = findItem(window->contentItem(), "item"); QVERIFY(item); QVERIFY(item->hasFocus()); QVERIFY(item->hasActiveFocus()); } void tst_applicationwindow::implicitFill() { QQmlEngine engine; QQmlComponent component(&engine); component.loadUrl(testFileUrl("fill.qml")); QObject* created = component.create(); QScopedPointer cleanup(created); QVERIFY(created); QQuickWindow* window = qobject_cast(created); QVERIFY(window); QVERIFY(!window->isVisible()); QCOMPARE(window->width(), 400); QCOMPARE(window->height(), 400); window->show(); QVERIFY(QTest::qWaitForWindowActive(window)); QQuickItem *stackView = window->property("stackView").value(); QVERIFY(stackView); QCOMPARE(stackView->width(), 400.0); QCOMPARE(stackView->height(), 400.0); QQuickItem *nextItem = window->property("nextItem").value(); QVERIFY(nextItem); QVERIFY(QMetaObject::invokeMethod(window, "pushNextItem")); QCOMPARE(nextItem->width(), 400.0); QCOMPARE(nextItem->height(), 400.0); } void tst_applicationwindow::attachedProperties() { QQmlEngine engine; QQmlComponent component(&engine); component.loadUrl(testFileUrl("attachedProperties.qml")); QScopedPointer object(component.create()); QVERIFY2(!object.isNull(), qPrintable(component.errorString())); QQuickApplicationWindow *window = qobject_cast(object.data()); QVERIFY(window); QQuickItem *childItem = object->property("childItem").value(); QVERIFY(childItem); QCOMPARE(childItem->property("attached_window").value(), window); QCOMPARE(childItem->property("attached_contentItem").value(), window->contentItem()); QCOMPARE(childItem->property("attached_activeFocusItem").value(), window->activeFocusItem()); QCOMPARE(childItem->property("attached_header").value(), window->header()); QCOMPARE(childItem->property("attached_footer").value(), window->footer()); QCOMPARE(childItem->property("attached_overlay").value(), window->overlay()); QObject *childObject = object->property("childObject").value(); QVERIFY(childObject); QVERIFY(!childObject->property("attached_window").value()); QVERIFY(!childObject->property("attached_contentItem").value()); QVERIFY(!childObject->property("attached_activeFocusItem").value()); QVERIFY(!childObject->property("attached_header").value()); QVERIFY(!childObject->property("attached_footer").value()); QVERIFY(!childObject->property("attached_overlay").value()); QQuickWindow *childWindow = object->property("childWindow").value(); QVERIFY(childWindow); QVERIFY(!childWindow->property("attached_window").value()); QVERIFY(!childWindow->property("attached_contentItem").value()); QVERIFY(!childWindow->property("attached_activeFocusItem").value()); QVERIFY(!childWindow->property("attached_header").value()); QVERIFY(!childWindow->property("attached_footer").value()); QVERIFY(!childWindow->property("attached_overlay").value()); QQuickItem *childWindowItem = object->property("childWindowItem").value(); QVERIFY(childWindowItem); QVERIFY(!childWindowItem->property("attached_window").value()); QVERIFY(!childWindowItem->property("attached_contentItem").value()); QVERIFY(!childWindowItem->property("attached_activeFocusItem").value()); QVERIFY(!childWindowItem->property("attached_header").value()); QVERIFY(!childWindowItem->property("attached_footer").value()); QVERIFY(!childWindowItem->property("attached_overlay").value()); QObject *childWindowObject = object->property("childWindowObject").value(); QVERIFY(childWindowObject); QVERIFY(!childWindowObject->property("attached_window").value()); QVERIFY(!childWindowObject->property("attached_contentItem").value()); QVERIFY(!childWindowObject->property("attached_activeFocusItem").value()); QVERIFY(!childWindowObject->property("attached_header").value()); QVERIFY(!childWindowObject->property("attached_footer").value()); QVERIFY(!childWindowObject->property("attached_overlay").value()); QQuickApplicationWindow *childAppWindow = object->property("childAppWindow").value(); QVERIFY(childAppWindow); QVERIFY(!childAppWindow->property("attached_window").value()); QVERIFY(!childAppWindow->property("attached_contentItem").value()); QVERIFY(!childAppWindow->property("attached_activeFocusItem").value()); QVERIFY(!childAppWindow->property("attached_header").value()); QVERIFY(!childAppWindow->property("attached_footer").value()); QVERIFY(!childAppWindow->property("attached_overlay").value()); QQuickItem *childAppWindowItem = object->property("childAppWindowItem").value(); QVERIFY(childAppWindowItem); QCOMPARE(childAppWindowItem->property("attached_window").value(), childAppWindow); QCOMPARE(childAppWindowItem->property("attached_contentItem").value(), childAppWindow->contentItem()); QCOMPARE(childAppWindowItem->property("attached_activeFocusItem").value(), childAppWindow->activeFocusItem()); QCOMPARE(childAppWindowItem->property("attached_header").value(), childAppWindow->header()); QCOMPARE(childAppWindowItem->property("attached_footer").value(), childAppWindow->footer()); QCOMPARE(childAppWindowItem->property("attached_overlay").value(), childAppWindow->overlay()); QObject *childAppWindowObject = object->property("childAppWindowObject").value(); QVERIFY(childAppWindowObject); QVERIFY(!childAppWindowObject->property("attached_window").value()); QVERIFY(!childAppWindowObject->property("attached_contentItem").value()); QVERIFY(!childAppWindowObject->property("attached_activeFocusItem").value()); QVERIFY(!childAppWindowObject->property("attached_header").value()); QVERIFY(!childAppWindowObject->property("attached_footer").value()); QVERIFY(!childAppWindowObject->property("attached_overlay").value()); window->show(); window->requestActivate(); QVERIFY(QTest::qWaitForWindowActive(window)); QVERIFY(!childItem->hasActiveFocus()); childItem->forceActiveFocus(); QTRY_VERIFY(childItem->hasActiveFocus()); QCOMPARE(window->activeFocusItem(), childItem); QCOMPARE(childItem->property("attached_activeFocusItem").value(), childItem); QQuickItem *header = new QQuickItem; window->setHeader(header); QCOMPARE(window->header(), header); QCOMPARE(childItem->property("attached_header").value(), header); QQuickItem *footer = new QQuickItem; window->setFooter(footer); QCOMPARE(window->footer(), footer); QCOMPARE(childItem->property("attached_footer").value(), footer); childAppWindow->show(); childAppWindow->requestActivate(); QVERIFY(QTest::qWaitForWindowActive(childAppWindow)); QVERIFY(!childAppWindowItem->hasActiveFocus()); childAppWindowItem->forceActiveFocus(); QTRY_VERIFY(childAppWindowItem->hasActiveFocus()); QCOMPARE(childAppWindow->activeFocusItem(), childAppWindowItem); QCOMPARE(childAppWindowItem->property("attached_activeFocusItem").value(), childAppWindowItem); childItem->setParentItem(childAppWindow->contentItem()); QCOMPARE(childItem->window(), childAppWindow); QCOMPARE(childItem->property("attached_window").value(), childAppWindow); QCOMPARE(childItem->property("attached_contentItem").value(), childAppWindow->contentItem()); QCOMPARE(childItem->property("attached_activeFocusItem").value(), childAppWindow->activeFocusItem()); QCOMPARE(childItem->property("attached_header").value(), childAppWindow->header()); QCOMPARE(childItem->property("attached_footer").value(), childAppWindow->footer()); QCOMPARE(childItem->property("attached_overlay").value(), childAppWindow->overlay()); childItem->setParentItem(Q_NULLPTR); QVERIFY(!childItem->window()); QVERIFY(!childItem->property("attached_window").value()); QVERIFY(!childItem->property("attached_contentItem").value()); QVERIFY(!childItem->property("attached_activeFocusItem").value()); QVERIFY(!childItem->property("attached_header").value()); QVERIFY(!childItem->property("attached_footer").value()); QVERIFY(!childItem->property("attached_overlay").value()); } QTEST_MAIN(tst_applicationwindow) #include "tst_applicationwindow.moc"