1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the test suite of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:GPL-EXCEPT$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU
19** General Public License version 3 as published by the Free Software
20** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
21** included in the packaging of this file. Please review the following
22** information to ensure the GNU General Public License requirements will
23** be met: https://www.gnu.org/licenses/gpl-3.0.html.
24**
25** $QT_END_LICENSE$
26**
27****************************************************************************/
28
29#include <qtest.h>
30#include <QtTest/QSignalSpy>
31#include <QtQml/qqmlengine.h>
32#include <QtQml/qqmlcomponent.h>
33#include <QtQml/qqmlcontext.h>
34#include <QtQuick/qquickview.h>
35#include <QtQuick/private/qquickitem_p.h>
36#include "../shared/util.h"
37#include "../shared/visualtestutil.h"
38
39using namespace QQuickVisualTestUtil;
40
41class tst_applicationwindow : public QQmlDataTest
42{
43 Q_OBJECT
44public:
45
46private slots:
47 void qmlCreation();
48 void activeFocusOnTab1();
49 void activeFocusOnTab2();
50 void defaultFocus();
51};
52
53void tst_applicationwindow::qmlCreation()
54{
55 QQmlEngine engine;
56 QQmlComponent component(&engine);
57 component.loadUrl(url: testFileUrl(fileName: "basicapplicationwindow.qml"));
58 QObject* created = component.create();
59 QScopedPointer<QObject> cleanup(created);
60 QVERIFY(created);
61
62 QQuickWindow* window = qobject_cast<QQuickWindow*>(object: created);
63 QVERIFY(window);
64 QVERIFY(!window->isVisible());
65
66 QCOMPARE(created->property("title"), QVariant("Test Application Window"));
67
68 QQuickItem* statusBar = qvariant_cast<QQuickItem*>(v: created->property(name: "statusBar"));
69 QVERIFY(!statusBar);
70
71 QQuickItem* menuBar = qvariant_cast<QQuickItem*>(v: created->property(name: "menuBar"));
72 QVERIFY(!menuBar);
73
74 QQuickItem* toolBar = qvariant_cast<QQuickItem*>(v: created->property(name: "toolBar"));
75 QVERIFY(!toolBar);
76}
77
78void tst_applicationwindow::activeFocusOnTab1()
79{
80 QQmlEngine engine;
81 QQmlComponent component(&engine);
82 component.loadUrl(url: testFileUrl(fileName: "activefocusontab.qml"));
83 QObject* created = component.create();
84 QScopedPointer<QObject> cleanup(created);
85 QVERIFY(created);
86
87 QQuickWindow* window = qobject_cast<QQuickWindow*>(object: created);
88 QVERIFY(window);
89 window->show();
90 window->requestActivate();
91 QVERIFY(QTest::qWaitForWindowActive(window));
92 QVERIFY(QGuiApplication::focusWindow() == window);
93
94 QQuickItem* contentItem = window->contentItem();
95 QVERIFY(contentItem);
96 QVERIFY(contentItem->hasActiveFocus());
97
98 QQuickItem* item = findItem<QQuickItem>(parent: window->contentItem(), objectName: "sub1");
99 QVERIFY(item);
100 QVERIFY(!item->hasActiveFocus());
101
102 // Tab: contentItem->sub1
103 QKeyEvent key(QEvent::KeyPress, Qt::Key_Tab, Qt::NoModifier, "", false, 1);
104 QGuiApplication::sendEvent(receiver: window, event: &key);
105 QVERIFY(key.isAccepted());
106
107 item = findItem<QQuickItem>(parent: window->contentItem(), objectName: "sub1");
108 QVERIFY(item);
109 QVERIFY(item->hasActiveFocus());
110
111 // Tab: sub1->sub2
112 key = QKeyEvent(QEvent::KeyPress, Qt::Key_Tab, Qt::NoModifier, "", false, 1);
113 QGuiApplication::sendEvent(receiver: window, event: &key);
114 QVERIFY(key.isAccepted());
115
116 item = findItem<QQuickItem>(parent: window->contentItem(), objectName: "sub2");
117 QVERIFY(item);
118 QVERIFY(item->hasActiveFocus());
119
120 // Tab: sub2->sub1
121 key = QKeyEvent(QEvent::KeyPress, Qt::Key_Tab, Qt::NoModifier, "", false, 1);
122 QGuiApplication::sendEvent(receiver: window, event: &key);
123 QVERIFY(key.isAccepted());
124
125 item = findItem<QQuickItem>(parent: window->contentItem(), objectName: "sub1");
126 QVERIFY(item);
127 QVERIFY(item->hasActiveFocus());
128}
129
130void tst_applicationwindow::activeFocusOnTab2()
131{
132 QQmlEngine engine;
133 QQmlComponent component(&engine);
134 component.loadUrl(url: testFileUrl(fileName: "activefocusontab.qml"));
135 QObject* created = component.create();
136 QScopedPointer<QObject> cleanup(created);
137 QVERIFY(created);
138
139 QQuickWindow* window = qobject_cast<QQuickWindow*>(object: created);
140 QVERIFY(window);
141 window->show();
142 window->requestActivate();
143 QVERIFY(QTest::qWaitForWindowActive(window));
144 QVERIFY(QGuiApplication::focusWindow() == window);
145
146 QQuickItem* contentItem = window->contentItem();
147 QVERIFY(contentItem);
148 QVERIFY(contentItem->hasActiveFocus());
149
150 QQuickItem* item = findItem<QQuickItem>(parent: window->contentItem(), objectName: "sub2");
151 QVERIFY(item);
152 QVERIFY(!item->hasActiveFocus());
153
154 // BackTab: contentItem->sub2
155 QKeyEvent key(QEvent::KeyPress, Qt::Key_Tab, Qt::ShiftModifier, "", false, 1);
156 QGuiApplication::sendEvent(receiver: window, event: &key);
157 QVERIFY(key.isAccepted());
158
159 item = findItem<QQuickItem>(parent: window->contentItem(), objectName: "sub2");
160 QVERIFY(item);
161 QVERIFY(item->hasActiveFocus());
162
163 // BackTab: sub2->sub1
164 key = QKeyEvent(QEvent::KeyPress, Qt::Key_Tab, Qt::ShiftModifier, "", false, 1);
165 QGuiApplication::sendEvent(receiver: window, event: &key);
166 QVERIFY(key.isAccepted());
167
168 item = findItem<QQuickItem>(parent: window->contentItem(), objectName: "sub1");
169 QVERIFY(item);
170 QVERIFY(item->hasActiveFocus());
171
172 // BackTab: sub1->sub2
173 key = QKeyEvent(QEvent::KeyPress, Qt::Key_Tab, Qt::ShiftModifier, "", false, 1);
174 QGuiApplication::sendEvent(receiver: window, event: &key);
175 QVERIFY(key.isAccepted());
176
177 item = findItem<QQuickItem>(parent: window->contentItem(), objectName: "sub2");
178 QVERIFY(item);
179 QVERIFY(item->hasActiveFocus());
180}
181
182void tst_applicationwindow::defaultFocus()
183{
184 QQmlEngine engine;
185 QQmlComponent component(&engine);
186 component.loadUrl(url: testFileUrl(fileName: "defaultFocus.qml"));
187 QObject* created = component.create();
188 QScopedPointer<QObject> cleanup(created);
189 Q_UNUSED(cleanup);
190 QVERIFY(created);
191
192 QQuickWindow* window = qobject_cast<QQuickWindow*>(object: created);
193 QVERIFY(window);
194 window->show();
195 window->requestActivate();
196 QVERIFY(QTest::qWaitForWindowActive(window));
197 QVERIFY(QGuiApplication::focusWindow() == window);
198
199 QQuickItem* contentItem = window->contentItem();
200 QVERIFY(contentItem);
201 QVERIFY(contentItem->hasActiveFocus());
202
203 // A single item in an ApplicationWindow with focus: true should receive focus.
204 QQuickItem* item = findItem<QQuickItem>(parent: window->contentItem(), objectName: "item");
205 QVERIFY(item);
206 QVERIFY(item->hasFocus());
207 QVERIFY(item->hasActiveFocus());
208}
209
210QTEST_MAIN(tst_applicationwindow)
211
212#include "tst_applicationwindow.moc"
213

source code of qtquickcontrols/tests/auto/applicationwindow/tst_applicationwindow.cpp