void tst_applicationwindow::defaultFocus() { QQmlEngine engine; QQmlComponent component(&engine); component.loadUrl(testFileUrl("defaultFocus.qml")); QObject* created = component.create(); QScopedPointer<QObject> cleanup(created); Q_UNUSED(cleanup); QVERIFY(created); QQuickWindow* window = qobject_cast<QQuickWindow*>(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<QQuickItem>(window->contentItem(), "item"); QVERIFY(item); QVERIFY(item->hasFocus()); QVERIFY(item->hasActiveFocus()); }
void tst_qquickfocusscope::canvasFocus() { QQuickView *view = new QQuickView; view->setSource(testFileUrl("canvasFocus.qml")); QQuickView alternateView; QQuickItem *rootObject = view->rootObject(); QVERIFY(rootObject); QQuickItem *rootItem = view->contentItem(); QQuickItem *scope1 = findItem<QQuickItem>(rootObject, QLatin1String("scope1")); QQuickItem *item1 = findItem<QQuickItem>(rootObject, QLatin1String("item1")); QQuickItem *scope2 = findItem<QQuickItem>(rootObject, QLatin1String("scope2")); QQuickItem *item2 = findItem<QQuickItem>(rootObject, QLatin1String("item2")); QVERIFY(scope1); QVERIFY(item1); QVERIFY(scope2); QVERIFY(item2); QSignalSpy rootFocusSpy(rootItem, SIGNAL(focusChanged(bool))); QSignalSpy scope1FocusSpy(scope1, SIGNAL(focusChanged(bool))); QSignalSpy item1FocusSpy(item1, SIGNAL(focusChanged(bool))); QSignalSpy scope2FocusSpy(scope2, SIGNAL(focusChanged(bool))); QSignalSpy item2FocusSpy(item2, SIGNAL(focusChanged(bool))); QSignalSpy rootActiveFocusSpy(rootItem, SIGNAL(activeFocusChanged(bool))); QSignalSpy scope1ActiveFocusSpy(scope1, SIGNAL(activeFocusChanged(bool))); QSignalSpy item1ActiveFocusSpy(item1, SIGNAL(activeFocusChanged(bool))); QSignalSpy scope2ActiveFocusSpy(scope2, SIGNAL(activeFocusChanged(bool))); QSignalSpy item2ActiveFocusSpy(item2, SIGNAL(activeFocusChanged(bool))); QEXPECT_FAIL("", "QTBUG-22415", Abort); QCOMPARE(rootItem->hasFocus(), false); QCOMPARE(rootItem->hasActiveFocus(), false); QCOMPARE(scope1->hasFocus(), true); QCOMPARE(scope1->hasActiveFocus(), false); QCOMPARE(item1->hasFocus(), true); QCOMPARE(item1->hasActiveFocus(), false); QCOMPARE(scope2->hasFocus(), false); QCOMPARE(scope2->hasActiveFocus(), false); QCOMPARE(item2->hasFocus(), false); QCOMPARE(item2->hasActiveFocus(), false); view->show(); view->requestActivate(); QVERIFY(QTest::qWaitForWindowActive(view)); QVERIFY(view == qGuiApp->focusWindow()); // Now the window has focus, active focus given to item1 QCOMPARE(rootItem->hasFocus(), true); QCOMPARE(rootItem->hasActiveFocus(), true); QCOMPARE(scope1->hasFocus(), true); QCOMPARE(scope1->hasActiveFocus(), true); QCOMPARE(item1->hasFocus(), true); QCOMPARE(item1->hasActiveFocus(), true); QCOMPARE(scope2->hasFocus(), false); QCOMPARE(scope2->hasActiveFocus(), false); QCOMPARE(item2->hasFocus(), false); QCOMPARE(item2->hasActiveFocus(), false); QCOMPARE(rootFocusSpy.count(), 1); QCOMPARE(rootActiveFocusSpy.count(), 1); QCOMPARE(scope1FocusSpy.count(), 0); QCOMPARE(scope1ActiveFocusSpy.count(), 1); QCOMPARE(item1FocusSpy.count(), 0); QCOMPARE(item1ActiveFocusSpy.count(), 1); // view->hide(); // seemingly doesn't remove focus, so have an another view steal it. alternateView.show(); alternateView.requestActivate(); QVERIFY(QTest::qWaitForWindowActive(&alternateView)); QVERIFY(QGuiApplication::focusWindow() == &alternateView); QCOMPARE(rootItem->hasFocus(), false); QCOMPARE(rootItem->hasActiveFocus(), false); QCOMPARE(scope1->hasFocus(), true); QCOMPARE(scope1->hasActiveFocus(), false); QCOMPARE(item1->hasFocus(), true); QCOMPARE(item1->hasActiveFocus(), false); QCOMPARE(rootFocusSpy.count(), 2); QCOMPARE(rootActiveFocusSpy.count(), 2); QCOMPARE(scope1FocusSpy.count(), 0); QCOMPARE(scope1ActiveFocusSpy.count(), 2); QCOMPARE(item1FocusSpy.count(), 0); QCOMPARE(item1ActiveFocusSpy.count(), 2); // window does not have focus, so item2 will not get active focus item2->forceActiveFocus(); QCOMPARE(rootItem->hasFocus(), false); QCOMPARE(rootItem->hasActiveFocus(), false); QCOMPARE(scope1->hasFocus(), false); QCOMPARE(scope1->hasActiveFocus(), false); QCOMPARE(item1->hasFocus(), true); QCOMPARE(item1->hasActiveFocus(), false); QCOMPARE(scope2->hasFocus(), true); QCOMPARE(scope2->hasActiveFocus(), false); QCOMPARE(item2->hasFocus(), true); QCOMPARE(item2->hasActiveFocus(), false); QCOMPARE(rootFocusSpy.count(), 2); QCOMPARE(rootActiveFocusSpy.count(), 2); QCOMPARE(scope1FocusSpy.count(), 1); QCOMPARE(scope1ActiveFocusSpy.count(), 2); QCOMPARE(item1FocusSpy.count(), 0); QCOMPARE(item1ActiveFocusSpy.count(), 2); QCOMPARE(scope2FocusSpy.count(), 1); QCOMPARE(scope2ActiveFocusSpy.count(), 0); QCOMPARE(item2FocusSpy.count(), 1); QCOMPARE(item2ActiveFocusSpy.count(), 0); // give the window focus, and item2 will get active focus view->show(); view->requestActivate(); QVERIFY(QTest::qWaitForWindowActive(view)); QVERIFY(QGuiApplication::focusWindow() == view); QCOMPARE(rootItem->hasFocus(), true); QCOMPARE(rootItem->hasActiveFocus(), true); QCOMPARE(scope2->hasFocus(), true); QCOMPARE(scope2->hasActiveFocus(), true); QCOMPARE(item2->hasFocus(), true); QCOMPARE(item2->hasActiveFocus(), true); QCOMPARE(rootFocusSpy.count(), 3); QCOMPARE(rootActiveFocusSpy.count(), 3); QCOMPARE(scope2FocusSpy.count(), 1); QCOMPARE(scope2ActiveFocusSpy.count(), 1); QCOMPARE(item2FocusSpy.count(), 1); QCOMPARE(item2ActiveFocusSpy.count(), 1); delete view; }