#include <qtest.h>
+#include <qpa/qwindowsysteminterface.h>
#include <QtQuick/qquickitem.h>
#include <QtQuick/qquickcanvas.h>
#include <QtQuick/qquickview.h>
void addedToCanvas();
void changeParent();
void multipleFocusClears();
+ void focusSubItemInNonFocusScope();
+ void parentItemWithFocus();
+ void reparentFocusedItem();
void constructor();
void setParentItem();
void paintOrder_data();
void paintOrder();
+ void acceptedMouseButtons();
+
private:
enum PaintOrderOp {
void ensureFocus(QWindow *w) {
w->show();
w->requestActivateWindow();
- qApp->processEvents();
+ QTest::qWaitForWindowActive(w);
}
};
root->setFocus(true);
scope->setFocus(true);
scopedChild2->setFocus(true);
- QCOMPARE(root->hasFocus(), true);
+ QCOMPARE(root->hasFocus(), false);
QCOMPARE(child->hasFocus(), false);
QCOMPARE(scope->hasFocus(), false);
QCOMPARE(scopedChild->hasFocus(), false);
focusState[item].set(true, true);
focusState.active(item);
FVERIFY();
+ delete child2;
}
}
QTRY_VERIFY(QGuiApplication::focusWindow() == view);
}
+void tst_qquickitem::focusSubItemInNonFocusScope()
+{
+ QQuickView *view = new QQuickView;
+ view->setSource(testFileUrl("focusSubItemInNonFocusScope.qml"));
+ view->show();
+ qApp->processEvents();
+
+ QQuickItem *dummyItem = view->rootObject()->findChild<QQuickItem *>("dummyItem");
+ QVERIFY(dummyItem);
+
+ QQuickItem *textInput = view->rootObject()->findChild<QQuickItem *>("textInput");
+ QVERIFY(textInput);
+
+ QVERIFY(dummyItem->hasFocus());
+ QVERIFY(!textInput->hasFocus());
+ QVERIFY(dummyItem->hasActiveFocus());
+
+ QVERIFY(QMetaObject::invokeMethod(textInput, "forceActiveFocus"));
+
+ QVERIFY(!dummyItem->hasFocus());
+ QVERIFY(textInput->hasFocus());
+ QVERIFY(textInput->hasActiveFocus());
+
+ delete view;
+}
+
+void tst_qquickitem::parentItemWithFocus()
+{
+ QQuickCanvas canvas;
+ ensureFocus(&canvas);
+ QTRY_VERIFY(QGuiApplication::focusWindow() == &canvas);
+ {
+ QQuickItem parent;
+ QQuickItem child;
+
+ FocusState focusState;
+ focusState << &parent << &child;
+ FVERIFY();
+
+ parent.setFocus(true);
+ child.setFocus(true);
+ focusState[&parent].set(true, false);
+ focusState[&child].set(true, false);
+ FVERIFY();
+
+ child.setParentItem(&parent);
+ focusState[&parent].set(true, false);
+ focusState[&child].set(false, false);
+ FVERIFY();
+
+ parent.setParentItem(canvas.rootItem());
+ focusState[&parent].set(true, true);
+ focusState[&child].set(false, false);
+ focusState.active(&parent);
+ FVERIFY();
+
+ child.forceActiveFocus();
+ focusState[&parent].set(false, false);
+ focusState[&child].set(true, true);
+ focusState.active(&child);
+ FVERIFY();
+ } {
+ QQuickItem parent;
+ QQuickItem child;
+ QQuickItem grandchild(&child);
+
+ FocusState focusState;
+ focusState << &parent << &child << &grandchild;
+ FVERIFY();
+
+ parent.setFocus(true);
+ grandchild.setFocus(true);
+ focusState[&parent].set(true, false);
+ focusState[&child].set(false, false);
+ focusState[&grandchild].set(true, false);
+ FVERIFY();
+
+ child.setParentItem(&parent);
+ focusState[&parent].set(true, false);
+ focusState[&child].set(false, false);
+ focusState[&grandchild].set(false, false);
+ FVERIFY();
+
+ parent.setParentItem(canvas.rootItem());
+ focusState[&parent].set(true, true);
+ focusState[&child].set(false, false);
+ focusState[&grandchild].set(false, false);
+ focusState.active(&parent);
+ FVERIFY();
+
+ grandchild.forceActiveFocus();
+ focusState[&parent].set(false, false);
+ focusState[&child].set(false, false);
+ focusState[&grandchild].set(true, true);
+ focusState.active(&grandchild);
+ FVERIFY();
+ }
+
+ {
+ QQuickItem parent;
+ QQuickItem child1;
+ QQuickItem child2;
+
+ FocusState focusState;
+ focusState << &parent << &child1 << &child2;
+ parent.setFocus(true);
+ child1.setParentItem(&parent);
+ child2.setParentItem(&parent);
+ focusState[&parent].set(true, false);
+ focusState[&child1].set(false, false);
+ focusState[&child2].set(false, false);
+ FVERIFY();
+
+ child1.setFocus(true);
+ focusState[&parent].set(false, false);
+ focusState[&child1].set(true, false);
+ FVERIFY();
+
+ parent.setFocus(true);
+ focusState[&parent].set(true, false);
+ focusState[&child1].set(false, false);
+ FVERIFY();
+ }
+}
+
+void tst_qquickitem::reparentFocusedItem()
+{
+ QQuickCanvas canvas;
+ ensureFocus(&canvas);
+ QTRY_VERIFY(QGuiApplication::focusWindow() == &canvas);
+
+ QQuickItem parent(canvas.rootItem());
+ QQuickItem child(&parent);
+ QQuickItem sibling(&parent);
+ QQuickItem grandchild(&child);
+
+ FocusState focusState;
+ focusState << &parent << &child << &sibling << &grandchild;
+ FVERIFY();
+
+ grandchild.setFocus(true);
+ focusState[&parent].set(false, false);
+ focusState[&child].set(false, false);
+ focusState[&sibling].set(false, false);
+ focusState[&grandchild].set(true, true);
+ focusState.active(&grandchild);
+ FVERIFY();
+
+ // Parenting the item to another item within the same focus scope shouldn't change it's focus.
+ child.setParentItem(&sibling);
+ FVERIFY();
+}
+
void tst_qquickitem::constructor()
{
QQuickItem *root = new QQuickItem;
TestPolishItem *item = qobject_cast<TestPolishItem*>(view->rootObject());
QVERIFY(item);
-#ifdef Q_OS_MAC
- QSKIP("QTBUG-21590 view does not reliably receive polish without a running animation");
-#endif
-
QTRY_VERIFY(item->wasPolished);
delete view;
QCOMPARE(items, expected);
}
+void tst_qquickitem::acceptedMouseButtons()
+{
+ TestItem item;
+ QCOMPARE(item.acceptedMouseButtons(), Qt::MouseButtons(Qt::NoButton));
+
+ QQuickCanvas canvas;
+ item.setSize(QSizeF(200,100));
+ item.setParentItem(canvas.rootItem());
+
+ QTest::mousePress(&canvas, Qt::LeftButton, 0, QPoint(50, 50));
+ QTest::mouseRelease(&canvas, Qt::LeftButton, 0, QPoint(50, 50));
+ QCOMPARE(item.pressCount, 0);
+ QCOMPARE(item.releaseCount, 0);
+
+ QTest::mousePress(&canvas, Qt::RightButton, 0, QPoint(50, 50));
+ QTest::mouseRelease(&canvas, Qt::RightButton, 0, QPoint(50, 50));
+ QCOMPARE(item.pressCount, 0);
+ QCOMPARE(item.releaseCount, 0);
+
+ QTest::mousePress(&canvas, Qt::MiddleButton, 0, QPoint(50, 50));
+ QTest::mouseRelease(&canvas, Qt::MiddleButton, 0, QPoint(50, 50));
+ QCOMPARE(item.pressCount, 0);
+ QCOMPARE(item.releaseCount, 0);
+
+ item.setAcceptedMouseButtons(Qt::LeftButton);
+ QCOMPARE(item.acceptedMouseButtons(), Qt::MouseButtons(Qt::LeftButton));
+
+ QTest::mousePress(&canvas, Qt::LeftButton, 0, QPoint(50, 50));
+ QTest::mouseRelease(&canvas, Qt::LeftButton, 0, QPoint(50, 50));
+ QCOMPARE(item.pressCount, 1);
+ QCOMPARE(item.releaseCount, 1);
+
+ QTest::mousePress(&canvas, Qt::RightButton, 0, QPoint(50, 50));
+ QTest::mouseRelease(&canvas, Qt::RightButton, 0, QPoint(50, 50));
+ QCOMPARE(item.pressCount, 1);
+ QCOMPARE(item.releaseCount, 1);
+
+ QTest::mousePress(&canvas, Qt::MiddleButton, 0, QPoint(50, 50));
+ QTest::mouseRelease(&canvas, Qt::MiddleButton, 0, QPoint(50, 50));
+ QCOMPARE(item.pressCount, 1);
+ QCOMPARE(item.releaseCount, 1);
+
+ item.setAcceptedMouseButtons(Qt::RightButton | Qt::MiddleButton);
+ QCOMPARE(item.acceptedMouseButtons(), Qt::MouseButtons(Qt::RightButton | Qt::MiddleButton));
+
+ QTest::mousePress(&canvas, Qt::LeftButton, 0, QPoint(50, 50));
+ QTest::mouseRelease(&canvas, Qt::LeftButton, 0, QPoint(50, 50));
+ QCOMPARE(item.pressCount, 1);
+ QCOMPARE(item.releaseCount, 1);
+
+ QTest::mousePress(&canvas, Qt::RightButton, 0, QPoint(50, 50));
+ QTest::mouseRelease(&canvas, Qt::RightButton, 0, QPoint(50, 50));
+ QCOMPARE(item.pressCount, 2);
+ QCOMPARE(item.releaseCount, 2);
+
+ QTest::mousePress(&canvas, Qt::MiddleButton, 0, QPoint(50, 50));
+ QTest::mouseRelease(&canvas, Qt::MiddleButton, 0, QPoint(50, 50));
+ QCOMPARE(item.pressCount, 3);
+ QCOMPARE(item.releaseCount, 3);
+}
+
QTEST_MAIN(tst_qquickitem)