From 5107b4c5e670b7357b9eb0f8f81f9810eaa9647b Mon Sep 17 00:00:00 2001 From: Yann Bodson Date: Tue, 30 Mar 2010 17:34:10 +1000 Subject: Move basic focusscope examples into auto tests. --- examples/declarative/declarative.pro | 1 - examples/declarative/focusscope/test.qml | 76 ------ examples/declarative/focusscope/test2.qml | 40 --- examples/declarative/focusscope/test3.qml | 52 ---- examples/declarative/focusscope/test4.qml | 75 ------ examples/declarative/focusscope/test5.qml | 83 ------ tests/auto/declarative/declarative.pro | 1 + .../qdeclarativefocusscope/data/test.qml | 77 ++++++ .../qdeclarativefocusscope/data/test2.qml | 39 +++ .../qdeclarativefocusscope/data/test3.qml | 52 ++++ .../qdeclarativefocusscope/data/test4.qml | 76 ++++++ .../qdeclarativefocusscope/data/test5.qml | 84 +++++++ .../qdeclarativefocusscope.pro | 6 + .../tst_qdeclarativefocusscope.cpp | 278 +++++++++++++++++++++ 14 files changed, 613 insertions(+), 327 deletions(-) delete mode 100644 examples/declarative/focusscope/test.qml delete mode 100644 examples/declarative/focusscope/test2.qml delete mode 100644 examples/declarative/focusscope/test3.qml delete mode 100644 examples/declarative/focusscope/test4.qml delete mode 100644 examples/declarative/focusscope/test5.qml create mode 100644 tests/auto/declarative/qdeclarativefocusscope/data/test.qml create mode 100644 tests/auto/declarative/qdeclarativefocusscope/data/test2.qml create mode 100644 tests/auto/declarative/qdeclarativefocusscope/data/test3.qml create mode 100644 tests/auto/declarative/qdeclarativefocusscope/data/test4.qml create mode 100644 tests/auto/declarative/qdeclarativefocusscope/data/test5.qml create mode 100644 tests/auto/declarative/qdeclarativefocusscope/qdeclarativefocusscope.pro create mode 100644 tests/auto/declarative/qdeclarativefocusscope/tst_qdeclarativefocusscope.cpp diff --git a/examples/declarative/declarative.pro b/examples/declarative/declarative.pro index bddfbee..0f58738 100644 --- a/examples/declarative/declarative.pro +++ b/examples/declarative/declarative.pro @@ -21,7 +21,6 @@ sources.files = \ effects \ fillmode \ focus \ - focusscope \ fonts \ gridview \ layouts \ diff --git a/examples/declarative/focusscope/test.qml b/examples/declarative/focusscope/test.qml deleted file mode 100644 index e4332e7..0000000 --- a/examples/declarative/focusscope/test.qml +++ /dev/null @@ -1,76 +0,0 @@ -import Qt 4.6 - -Rectangle { - color: "white" - width: 800 - height: 600 - - Keys.onDigit9Pressed: console.log("Error - Root") - - FocusScope { - id: myScope - focus: true - - Keys.onDigit9Pressed: console.log("Error - FocusScope") - - Rectangle { - height: 120 - width: 420 - - color: "transparent" - border.width: 5 - border.color: myScope.wantsFocus?"blue":"black" - - Rectangle { - id: item1 - x: 10; y: 10 - width: 100; height: 100; color: "green" - border.width: 5 - border.color: wantsFocus?"blue":"black" - Keys.onDigit9Pressed: console.log("Top Left"); - KeyNavigation.right: item2 - focus: true - - Rectangle { - width: 50; height: 50; anchors.centerIn: parent - color: parent.focus?"red":"transparent" - } - } - - Rectangle { - id: item2 - x: 310; y: 10 - width: 100; height: 100; color: "green" - border.width: 5 - border.color: wantsFocus?"blue":"black" - KeyNavigation.left: item1 - Keys.onDigit9Pressed: console.log("Top Right"); - - Rectangle { - width: 50; height: 50; anchors.centerIn: parent - color: parent.focus?"red":"transparent" - } - } - } - KeyNavigation.down: item3 - } - - Text { x:100; y:170; text: "Blue border indicates scoped focus\nBlack border indicates NOT scoped focus\nRed box indicates active focus\nUse arrow keys to navigate\nPress \"9\" to print currently focused item" } - - Rectangle { - id: item3 - x: 10; y: 300 - width: 100; height: 100; color: "green" - border.width: 5 - border.color: wantsFocus?"blue":"black" - - Keys.onDigit9Pressed: console.log("Bottom Left"); - KeyNavigation.up: myScope - - Rectangle { - width: 50; height: 50; anchors.centerIn: parent - color: parent.focus?"red":"transparent" - } - } - -} diff --git a/examples/declarative/focusscope/test2.qml b/examples/declarative/focusscope/test2.qml deleted file mode 100644 index 5b6971a..0000000 --- a/examples/declarative/focusscope/test2.qml +++ /dev/null @@ -1,40 +0,0 @@ -import Qt 4.6 - -Rectangle { - color: "white" - width: 800 - height: 600 - - Text { text: "All five rectangles should be red" } - - FocusScope { - y: 100 - focus: true - Rectangle { width: 50; height: 50; color: parent.wantsFocus?"red":"blue" } - - FocusScope { - y: 100 - focus: true - Rectangle { width: 50; height: 50; color: parent.wantsFocus?"red":"blue" } - - FocusScope { - y: 100 - focus: true - Rectangle { width: 50; height: 50; color: parent.wantsFocus?"red":"blue" } - - FocusScope { - y: 100 - focus: true - Rectangle { width: 50; height: 50; color: parent.wantsFocus?"red":"blue" } - - FocusScope { - y: 100 - focus: true - Rectangle { width: 50; height: 50; color: parent.wantsFocus?"red":"blue" } - } - } - } - } - } - -} diff --git a/examples/declarative/focusscope/test3.qml b/examples/declarative/focusscope/test3.qml deleted file mode 100644 index 9344d07..0000000 --- a/examples/declarative/focusscope/test3.qml +++ /dev/null @@ -1,52 +0,0 @@ -import Qt 4.6 - -Rectangle { - color: "white" - width: 800 - height: 600 - - ListModel { - id: model - ListElement { name: "1" } - ListElement { name: "2" } - ListElement { name: "3" } - ListElement { name: "4" } - ListElement { name: "5" } - ListElement { name: "6" } - ListElement { name: "6" } - ListElement { name: "8" } - ListElement { name: "9" } - } - - Component { - id: verticalDelegate - FocusScope { - id: root - width: 50; height: 50; - Keys.onDigit9Pressed: console.log("Error - " + name) - Rectangle { - focus: true - Keys.onDigit9Pressed: console.log(name) - width: 50; height: 50; - color: root.ListView.isCurrentItem?"red":"green" - Text { text: name; anchors.centerIn: parent } - } - } - } - - ListView { - width: 800; height: 50; orientation: "Horizontal" - focus: true - model: model - delegate: verticalDelegate - preferredHighlightBegin: 100 - preferredHighlightEnd: 100 - highlightRangeMode: "StrictlyEnforceRange" - } - - - Text { - y: 100; x: 50 - text: "Currently selected element should be red\nPressing \"9\" should print the number of the currently selected item\nBe sure to scroll all the way to the right, pause, and then all the way to the left." - } -} diff --git a/examples/declarative/focusscope/test4.qml b/examples/declarative/focusscope/test4.qml deleted file mode 100644 index cc96df9..0000000 --- a/examples/declarative/focusscope/test4.qml +++ /dev/null @@ -1,75 +0,0 @@ -import Qt 4.6 - -Rectangle { - color: "white" - width: 800 - height: 600 - - Keys.onDigit9Pressed: console.log("Error - Root") - - FocusScope { - id: myScope - - Keys.onDigit9Pressed: console.log("Error - FocusScope") - - Rectangle { - height: 120 - width: 420 - - color: "transparent" - border.width: 5 - border.color: myScope.wantsFocus?"blue":"black" - - Rectangle { - id: item1 - x: 10; y: 10 - width: 100; height: 100; color: "green" - border.width: 5 - border.color: wantsFocus?"blue":"black" - Keys.onDigit9Pressed: console.log("Error - Top Left"); - KeyNavigation.right: item2 - focus: true - - Rectangle { - width: 50; height: 50; anchors.centerIn: parent - color: parent.focus?"red":"transparent" - } - } - - Rectangle { - id: item2 - x: 310; y: 10 - width: 100; height: 100; color: "green" - border.width: 5 - border.color: wantsFocus?"blue":"black" - KeyNavigation.left: item1 - Keys.onDigit9Pressed: console.log("Error - Top Right"); - - Rectangle { - width: 50; height: 50; anchors.centerIn: parent - color: parent.focus?"red":"transparent" - } - } - } - KeyNavigation.down: item3 - } - - Text { x:100; y:170; text: "There should be no blue borders, or red squares.\nPressing \"9\" should do nothing.\nArrow keys should have no effect." } - - Rectangle { - id: item3 - x: 10; y: 300 - width: 100; height: 100; color: "green" - border.width: 5 - border.color: wantsFocus?"blue":"black" - - Keys.onDigit9Pressed: console.log("Error - Bottom Left"); - KeyNavigation.up: myScope - - Rectangle { - width: 50; height: 50; anchors.centerIn: parent - color: parent.focus?"red":"transparent" - } - } - -} diff --git a/examples/declarative/focusscope/test5.qml b/examples/declarative/focusscope/test5.qml deleted file mode 100644 index da98350..0000000 --- a/examples/declarative/focusscope/test5.qml +++ /dev/null @@ -1,83 +0,0 @@ -import Qt 4.6 - -Rectangle { - color: "white" - width: 800 - height: 600 - - Keys.onReturnPressed: console.log("Error - Root") - - FocusScope { - id: myScope - focus: true - - Keys.onReturnPressed: console.log("Error - FocusScope") - - Rectangle { - height: 120 - width: 420 - - color: "transparent" - border.width: 5 - border.color: myScope.wantsFocus?"blue":"black" - - Rectangle { - x: 10; y: 10 - width: 100; height: 100; color: "green" - border.width: 5 - border.color: item1.wantsFocus?"blue":"black" - } - - TextEdit { - id: item1 - x: 20; y: 20 - width: 90; height: 90 - color: "white" - font.pixelSize: 20 - Keys.onReturnPressed: console.log("Top Left"); - KeyNavigation.right: item2 - focus: true - wrap: true - text: "Box 1" - } - - Rectangle { - id: item2 - x: 310; y: 10 - width: 100; height: 100; color: "green" - border.width: 5 - border.color: wantsFocus?"blue":"black" - KeyNavigation.left: item1 - Keys.onReturnPressed: console.log("Top Right"); - - Rectangle { - width: 50; height: 50; anchors.centerIn: parent - color: parent.focus?"red":"transparent" - } - } - } - KeyNavigation.down: item3 - } - - Text { x:100; y:170; text: "Blue border indicates scoped focus\nBlack border indicates NOT scoped focus\nRed box or flashing cursor indicates active focus\nUse arrow keys to navigate\nPress Ctrl-Return to print currently focused item" } - - Rectangle { - x: 10; y: 300 - width: 100; height: 100; color: "green" - border.width: 5 - border.color: item3.wantsFocus?"blue":"black" - } - - TextEdit { - id: item3 - x: 20; y: 310 - width: 90; height: 90 - color: "white" - font.pixelSize: 20 - text: "Box 3" - - Keys.onReturnPressed: console.log("Bottom Left"); - KeyNavigation.up: myScope - wrap: true - } -} diff --git a/tests/auto/declarative/declarative.pro b/tests/auto/declarative/declarative.pro index bebc54e..2d88058 100644 --- a/tests/auto/declarative/declarative.pro +++ b/tests/auto/declarative/declarative.pro @@ -23,6 +23,7 @@ SUBDIRS += \ qdeclarativeborderimage \ # Cover qdeclarativeflickable \ # Cover qdeclarativeflipable \ # Cover + qdeclarativefocusscope \ # Cover qdeclarativegridview \ # Cover qdeclarativeitem \ # Cover qdeclarativelistview \ # Cover diff --git a/tests/auto/declarative/qdeclarativefocusscope/data/test.qml b/tests/auto/declarative/qdeclarativefocusscope/data/test.qml new file mode 100644 index 0000000..647e5bf --- /dev/null +++ b/tests/auto/declarative/qdeclarativefocusscope/data/test.qml @@ -0,0 +1,77 @@ +import Qt 4.6 + +Rectangle { + color: "white" + width: 800 + height: 600 + + Keys.onDigit9Pressed: console.log("Error - Root") + + FocusScope { + id: myScope + focus: true + + Keys.onDigit9Pressed: console.log("Error - FocusScope") + + Rectangle { + objectName: "item0" + height: 120 + width: 420 + + color: "transparent" + border.width: 5 + //border.color: myScope.wantsFocus?"blue":"black" + + Rectangle { + id: item1; objectName: "item1" + x: 10; y: 10 + width: 100; height: 100; color: "green" + border.width: 5 + border.color: wantsFocus?"blue":"black" + Keys.onDigit9Pressed: console.debug("Top Left"); + KeyNavigation.right: item2 + focus: true + + Rectangle { + width: 50; height: 50; anchors.centerIn: parent + color: parent.focus?"red":"transparent" + } + } + + Rectangle { + id: item2; objectName: "item2" + x: 310; y: 10 + width: 100; height: 100; color: "green" + border.width: 5 + border.color: wantsFocus?"blue":"black" + KeyNavigation.left: item1 + Keys.onDigit9Pressed: console.log("Top Right"); + + Rectangle { + width: 50; height: 50; anchors.centerIn: parent + color: parent.focus?"red":"transparent" + } + } + } + KeyNavigation.down: item3 + } + + Text { x:100; y:170; text: "Blue border indicates scoped focus\nBlack border indicates NOT scoped focus\nRed box indicates active focus\nUse arrow keys to navigate\nPress \"9\" to print currently focused item" } + + Rectangle { + id: item3; objectName: "item3" + x: 10; y: 300 + width: 100; height: 100; color: "green" + border.width: 5 + border.color: wantsFocus?"blue":"black" + + Keys.onDigit9Pressed: console.log("Bottom Left"); + KeyNavigation.up: myScope + + Rectangle { + width: 50; height: 50; anchors.centerIn: parent + color: parent.focus?"red":"transparent" + } + } + +} diff --git a/tests/auto/declarative/qdeclarativefocusscope/data/test2.qml b/tests/auto/declarative/qdeclarativefocusscope/data/test2.qml new file mode 100644 index 0000000..277fda4 --- /dev/null +++ b/tests/auto/declarative/qdeclarativefocusscope/data/test2.qml @@ -0,0 +1,39 @@ +import Qt 4.6 + +Rectangle { + color: "white" + width: 800 + height: 600 + + Text { text: "All five rectangles should be red" } + + FocusScope { + y: 100 + focus: true; objectName: "item1" + Rectangle { width: 50; height: 50; color: parent.wantsFocus?"red":"blue" } + + FocusScope { + y: 100 + focus: true; objectName: "item2" + Rectangle { width: 50; height: 50; color: parent.wantsFocus?"red":"blue" } + + FocusScope { + y: 100 + focus: true; objectName: "item3" + Rectangle { width: 50; height: 50; color: parent.wantsFocus?"red":"blue" } + + FocusScope { + y: 100 + focus: true; objectName: "item4" + Rectangle { width: 50; height: 50; color: parent.wantsFocus?"red":"blue" } + + FocusScope { + y: 100 + focus: true; objectName: "item5" + Rectangle { width: 50; height: 50; color: parent.wantsFocus?"red":"blue" } + } + } + } + } + } +} diff --git a/tests/auto/declarative/qdeclarativefocusscope/data/test3.qml b/tests/auto/declarative/qdeclarativefocusscope/data/test3.qml new file mode 100644 index 0000000..9344d07 --- /dev/null +++ b/tests/auto/declarative/qdeclarativefocusscope/data/test3.qml @@ -0,0 +1,52 @@ +import Qt 4.6 + +Rectangle { + color: "white" + width: 800 + height: 600 + + ListModel { + id: model + ListElement { name: "1" } + ListElement { name: "2" } + ListElement { name: "3" } + ListElement { name: "4" } + ListElement { name: "5" } + ListElement { name: "6" } + ListElement { name: "6" } + ListElement { name: "8" } + ListElement { name: "9" } + } + + Component { + id: verticalDelegate + FocusScope { + id: root + width: 50; height: 50; + Keys.onDigit9Pressed: console.log("Error - " + name) + Rectangle { + focus: true + Keys.onDigit9Pressed: console.log(name) + width: 50; height: 50; + color: root.ListView.isCurrentItem?"red":"green" + Text { text: name; anchors.centerIn: parent } + } + } + } + + ListView { + width: 800; height: 50; orientation: "Horizontal" + focus: true + model: model + delegate: verticalDelegate + preferredHighlightBegin: 100 + preferredHighlightEnd: 100 + highlightRangeMode: "StrictlyEnforceRange" + } + + + Text { + y: 100; x: 50 + text: "Currently selected element should be red\nPressing \"9\" should print the number of the currently selected item\nBe sure to scroll all the way to the right, pause, and then all the way to the left." + } +} diff --git a/tests/auto/declarative/qdeclarativefocusscope/data/test4.qml b/tests/auto/declarative/qdeclarativefocusscope/data/test4.qml new file mode 100644 index 0000000..d8bd390 --- /dev/null +++ b/tests/auto/declarative/qdeclarativefocusscope/data/test4.qml @@ -0,0 +1,76 @@ +import Qt 4.6 + +Rectangle { + color: "white" + width: 800 + height: 600 + + Keys.onDigit9Pressed: console.log("Error - Root") + + FocusScope { + id: myScope + + Keys.onDigit9Pressed: console.log("Error - FocusScope") + + Rectangle { + objectName: "item0" + height: 120 + width: 420 + + color: "transparent" + border.width: 5 + //border.color: myScope.wantsFocus?"blue":"black" + + Rectangle { + id: item1; objectName: "item1" + x: 10; y: 10 + width: 100; height: 100; color: "green" + border.width: 5 + border.color: wantsFocus?"blue":"black" + Keys.onDigit9Pressed: console.log("Error - Top Left"); + KeyNavigation.right: item2 + focus: true + + Rectangle { + width: 50; height: 50; anchors.centerIn: parent + color: parent.focus?"red":"transparent" + } + } + + Rectangle { + id: item2; objectName: "item2" + x: 310; y: 10 + width: 100; height: 100; color: "green" + border.width: 5 + border.color: wantsFocus?"blue":"black" + KeyNavigation.left: item1 + Keys.onDigit9Pressed: console.log("Error - Top Right"); + + Rectangle { + width: 50; height: 50; anchors.centerIn: parent + color: parent.focus?"red":"transparent" + } + } + } + KeyNavigation.down: item3 + } + + Text { x:100; y:170; text: "There should be no blue borders, or red squares.\nPressing \"9\" should do nothing.\nArrow keys should have no effect." } + + Rectangle { + id: item3; objectName: "item3" + x: 10; y: 300 + width: 100; height: 100; color: "green" + border.width: 5 + border.color: wantsFocus?"blue":"black" + + Keys.onDigit9Pressed: console.log("Error - Bottom Left"); + KeyNavigation.up: myScope + + Rectangle { + width: 50; height: 50; anchors.centerIn: parent + color: parent.focus?"red":"transparent" + } + } + +} diff --git a/tests/auto/declarative/qdeclarativefocusscope/data/test5.qml b/tests/auto/declarative/qdeclarativefocusscope/data/test5.qml new file mode 100644 index 0000000..0b75ec4 --- /dev/null +++ b/tests/auto/declarative/qdeclarativefocusscope/data/test5.qml @@ -0,0 +1,84 @@ +import Qt 4.6 + +Rectangle { + color: "white" + width: 800 + height: 600 + + Keys.onReturnPressed: console.log("Error - Root") + + FocusScope { + id: myScope + focus: true + + Keys.onReturnPressed: console.log("Error - FocusScope") + + Rectangle { + objectName: "item0" + height: 120 + width: 420 + + color: "transparent" + border.width: 5 + //border.color: myScope.wantsFocus?"blue":"black" + + Rectangle { + x: 10; y: 10 + width: 100; height: 100; color: "green" + border.width: 5 + border.color: item1.wantsFocus?"blue":"black" + } + + TextEdit { + id: item1; objectName: "item1" + x: 20; y: 20 + width: 90; height: 90 + color: "white" + font.pixelSize: 20 + Keys.onReturnPressed: console.log("Top Left"); + KeyNavigation.right: item2 + focus: true + wrap: true + text: "Box 1" + } + + Rectangle { + id: item2; objectName: "item2" + x: 310; y: 10 + width: 100; height: 100; color: "green" + border.width: 5 + border.color: wantsFocus?"blue":"black" + KeyNavigation.left: item1 + Keys.onReturnPressed: console.log("Top Right"); + + Rectangle { + width: 50; height: 50; anchors.centerIn: parent + color: parent.focus?"red":"transparent" + } + } + } + KeyNavigation.down: item3 + } + + Text { x:100; y:170; text: "Blue border indicates scoped focus\nBlack border indicates NOT scoped focus\nRed box or flashing cursor indicates active focus\nUse arrow keys to navigate\nPress Ctrl-Return to print currently focused item" } + + Rectangle { + x: 10; y: 300 + width: 100; height: 100; color: "green" + border.width: 5 + border.color: item3.wantsFocus?"blue":"black" + } + + TextEdit { + id: item3; objectName: "item3" + x: 20; y: 310 + width: 90; height: 90 + color: "white" + font.pixelSize: 20 + text: "Box 3" + + Keys.onReturnPressed: console.log("Bottom Left"); + KeyNavigation.up: myScope + wrap: true + } +} diff --git a/tests/auto/declarative/qdeclarativefocusscope/qdeclarativefocusscope.pro b/tests/auto/declarative/qdeclarativefocusscope/qdeclarativefocusscope.pro new file mode 100644 index 0000000..4d7a9b3 --- /dev/null +++ b/tests/auto/declarative/qdeclarativefocusscope/qdeclarativefocusscope.pro @@ -0,0 +1,6 @@ +load(qttest_p4) +contains(QT_CONFIG,declarative): QT += declarative +SOURCES += tst_qdeclarativefocusscope.cpp +macx:CONFIG -= app_bundle + +DEFINES += SRCDIR=\\\"$$PWD\\\" diff --git a/tests/auto/declarative/qdeclarativefocusscope/tst_qdeclarativefocusscope.cpp b/tests/auto/declarative/qdeclarativefocusscope/tst_qdeclarativefocusscope.cpp new file mode 100644 index 0000000..1bd8331 --- /dev/null +++ b/tests/auto/declarative/qdeclarativefocusscope/tst_qdeclarativefocusscope.cpp @@ -0,0 +1,278 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the Technology Preview License Agreement accompanying +** this package. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include + + +class tst_qdeclarativefocusscope : public QObject +{ + Q_OBJECT +public: + tst_qdeclarativefocusscope() {} + + template + T *findItem(QGraphicsObject *parent, const QString &id); + +private slots: + void basic(); + void nested(); + void noFocus(); + void textEdit(); +}; + +/* + Find an item with the specified id. +*/ +template +T *tst_qdeclarativefocusscope::findItem(QGraphicsObject *parent, const QString &objectName) +{ + const QMetaObject &mo = T::staticMetaObject; + QList children = parent->childItems(); + for (int i = 0; i < children.count(); ++i) { + QDeclarativeItem *item = qobject_cast(children.at(i)->toGraphicsObject()); + if (item) { + if (mo.cast(item) && (objectName.isEmpty() || item->objectName() == objectName)) { + return static_cast(item); + } + item = findItem(item, objectName); + if (item) + return static_cast(item); + } + } + return 0; +} + +void tst_qdeclarativefocusscope::basic() +{ + QDeclarativeView *view = new QDeclarativeView; + view->setSource(QUrl::fromLocalFile(SRCDIR "/data/test.qml")); + + QDeclarativeRectangle *item0 = findItem(view->rootObject(), QLatin1String("item0")); + QDeclarativeRectangle *item1 = findItem(view->rootObject(), QLatin1String("item1")); + QDeclarativeRectangle *item2 = findItem(view->rootObject(), QLatin1String("item2")); + QDeclarativeRectangle *item3 = findItem(view->rootObject(), QLatin1String("item3")); + QVERIFY(item0 != 0); + QVERIFY(item1 != 0); + QVERIFY(item2 != 0); + QVERIFY(item3 != 0); + + view->show(); + qApp->setActiveWindow(view); + qApp->processEvents(); + +#ifdef Q_WS_X11 + // to be safe and avoid failing setFocus with window managers + qt_x11_wait_for_window_manager(view); +#endif + + QVERIFY(view->hasFocus()); + QVERIFY(view->scene()->hasFocus()); + QVERIFY(item0->wantsFocus() == true); + QVERIFY(item1->hasFocus() == true); + QVERIFY(item2->hasFocus() == false); + QVERIFY(item3->hasFocus() == false); + + QTest::keyClick(view, Qt::Key_Right); + QVERIFY(item0->wantsFocus() == true); + QVERIFY(item1->hasFocus() == false); + QVERIFY(item2->hasFocus() == true); + QVERIFY(item3->hasFocus() == false); + + QTest::keyClick(view, Qt::Key_Down); + QVERIFY(item0->wantsFocus() == false); + QVERIFY(item1->hasFocus() == false); + QVERIFY(item2->hasFocus() == false); + QVERIFY(item3->hasFocus() == true); + + delete view; +} + +void tst_qdeclarativefocusscope::nested() +{ + QDeclarativeView *view = new QDeclarativeView; + view->setSource(QUrl::fromLocalFile(SRCDIR "/data/test2.qml")); + + QDeclarativeFocusScope *item1 = findItem(view->rootObject(), QLatin1String("item1")); + QDeclarativeFocusScope *item2 = findItem(view->rootObject(), QLatin1String("item2")); + QDeclarativeFocusScope *item3 = findItem(view->rootObject(), QLatin1String("item3")); + QDeclarativeFocusScope *item4 = findItem(view->rootObject(), QLatin1String("item4")); + QDeclarativeFocusScope *item5 = findItem(view->rootObject(), QLatin1String("item5")); + QVERIFY(item1 != 0); + QVERIFY(item2 != 0); + QVERIFY(item3 != 0); + QVERIFY(item4 != 0); + QVERIFY(item5 != 0); + + view->show(); + qApp->setActiveWindow(view); + qApp->processEvents(); + +#ifdef Q_WS_X11 + // to be safe and avoid failing setFocus with window managers + qt_x11_wait_for_window_manager(view); +#endif + + QVERIFY(view->hasFocus()); + QVERIFY(view->scene()->hasFocus()); + + QVERIFY(item1->wantsFocus() == true); + QVERIFY(item1->hasFocus() == false); + QVERIFY(item2->wantsFocus() == true); + QVERIFY(item2->hasFocus() == false); + QVERIFY(item3->wantsFocus() == true); + QVERIFY(item3->hasFocus() == false); + QVERIFY(item4->wantsFocus() == true); + QVERIFY(item4->hasFocus() == false); + QVERIFY(item5->wantsFocus() == true); + QVERIFY(item5->hasFocus() == true); + delete view; +} + +void tst_qdeclarativefocusscope::noFocus() +{ + QDeclarativeView *view = new QDeclarativeView; + view->setSource(QUrl::fromLocalFile(SRCDIR "/data/test4.qml")); + + QDeclarativeRectangle *item0 = findItem(view->rootObject(), QLatin1String("item0")); + QDeclarativeRectangle *item1 = findItem(view->rootObject(), QLatin1String("item1")); + QDeclarativeRectangle *item2 = findItem(view->rootObject(), QLatin1String("item2")); + QDeclarativeRectangle *item3 = findItem(view->rootObject(), QLatin1String("item3")); + QVERIFY(item0 != 0); + QVERIFY(item1 != 0); + QVERIFY(item2 != 0); + QVERIFY(item3 != 0); + + view->show(); + qApp->setActiveWindow(view); + qApp->processEvents(); + +#ifdef Q_WS_X11 + // to be safe and avoid failing setFocus with window managers + qt_x11_wait_for_window_manager(view); +#endif + + QVERIFY(view->hasFocus()); + QVERIFY(view->scene()->hasFocus()); + QVERIFY(item0->wantsFocus() == false); + QVERIFY(item1->hasFocus() == false); + QVERIFY(item2->hasFocus() == false); + QVERIFY(item3->hasFocus() == false); + + QTest::keyClick(view, Qt::Key_Right); + QVERIFY(item0->wantsFocus() == false); + QVERIFY(item1->hasFocus() == false); + QVERIFY(item2->hasFocus() == false); + QVERIFY(item3->hasFocus() == false); + + QTest::keyClick(view, Qt::Key_Down); + QVERIFY(item0->wantsFocus() == false); + QVERIFY(item1->hasFocus() == false); + QVERIFY(item2->hasFocus() == false); + QVERIFY(item3->hasFocus() == false); + + delete view; +} + +void tst_qdeclarativefocusscope::textEdit() +{ + QDeclarativeView *view = new QDeclarativeView; + view->setSource(QUrl::fromLocalFile(SRCDIR "/data/test5.qml")); + + QDeclarativeRectangle *item0 = findItem(view->rootObject(), QLatin1String("item0")); + QDeclarativeTextEdit *item1 = findItem(view->rootObject(), QLatin1String("item1")); + QDeclarativeRectangle *item2 = findItem(view->rootObject(), QLatin1String("item2")); + QDeclarativeTextEdit *item3 = findItem(view->rootObject(), QLatin1String("item3")); + QVERIFY(item0 != 0); + QVERIFY(item1 != 0); + QVERIFY(item2 != 0); + QVERIFY(item3 != 0); + + view->show(); + qApp->setActiveWindow(view); + qApp->processEvents(); + +#ifdef Q_WS_X11 + // to be safe and avoid failing setFocus with window managers + qt_x11_wait_for_window_manager(view); +#endif + + QVERIFY(view->hasFocus()); + QVERIFY(view->scene()->hasFocus()); + QVERIFY(item0->wantsFocus() == true); + QVERIFY(item1->hasFocus() == true); + QVERIFY(item2->hasFocus() == false); + QVERIFY(item3->hasFocus() == false); + + QTest::keyClick(view, Qt::Key_Right); + QVERIFY(item0->wantsFocus() == true); + QVERIFY(item1->hasFocus() == true); + QVERIFY(item2->hasFocus() == false); + QVERIFY(item3->hasFocus() == false); + + QTest::keyClick(view, Qt::Key_Right); + QTest::keyClick(view, Qt::Key_Right); + QTest::keyClick(view, Qt::Key_Right); + QTest::keyClick(view, Qt::Key_Right); + QTest::keyClick(view, Qt::Key_Right); + QVERIFY(item0->wantsFocus() == true); + QVERIFY(item1->hasFocus() == false); + QVERIFY(item2->hasFocus() == true); + QVERIFY(item3->hasFocus() == false); + + QTest::keyClick(view, Qt::Key_Down); + QVERIFY(item0->wantsFocus() == false); + QVERIFY(item1->hasFocus() == false); + QVERIFY(item2->hasFocus() == false); + QVERIFY(item3->hasFocus() == true); + + delete view; +} + +QTEST_MAIN(tst_qdeclarativefocusscope) + +#include "tst_qdeclarativefocusscope.moc" -- cgit v0.12