2
* This file is part of unity-2d
4
* Copyright 2010 Canonical Ltd.
7
* - Renato Araujo Oliveira Filho <renato.filho@canonical.com>
9
* This program is free software; you can redistribute it and/or modify
10
* it under the terms of the GNU General Public License as published by
11
* the Free Software Foundation; version 3.
13
* This program is distributed in the hope that it will be useful,
14
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
* GNU General Public License for more details.
18
* You should have received a copy of the GNU General Public License
19
* along with this program. If not, see <http://www.gnu.org/licenses/>
22
#include <unitytestmacro.h>
24
#include <focuspath.h>
26
#include <QGraphicsObject>
27
#include <QDeclarativeEngine>
29
#include <QDeclarativeView>
32
class FocusPathTest : public QObject
38
void testInitialization();
39
void testChangeCurrentIndex();
40
void testNavigation();
42
void testNavigationDirection();
43
void testRTLNavigation();
44
void testOrderChange();
47
QDeclarativeView *m_view;
48
FocusPath *m_focusPath;
55
* Loads a QML file with a grix 3x3 elements
58
void FocusPathTest::init()
60
m_view = new QDeclarativeView;
61
m_view->engine()->addImportPath(unity2dImportPath());
62
m_view->setSource(QUrl(QString("%1/libunity-2d-private/tests/focuspathtest.qml").arg(unity2dDirectory())));
64
QGraphicsObject *root = m_view->rootObject();
66
m_focusPath = root->findChild<FocusPath*>("focusPath");
67
Q_ASSERT(m_focusPath);
71
void FocusPathTest::cleanup()
79
* Check if all items was inserted in FocusPath
81
void FocusPathTest::testInitialization()
83
QList<PathItem > path = m_focusPath->path();
84
int focusPathSize = path.size();
85
QCOMPARE(focusPathSize, 9);
86
for(int i=0; i < focusPathSize; i++) {
87
QString itemName = QString("Item%1").arg(i);
88
QCOMPARE(path[i].second->property("itemData").toString(), itemName);
91
/* Tun a item ivisible, it must be removed from the path*/
92
path[5].second->setVisible(false);
93
QCOMPARE(m_focusPath->path().size(), 8);
97
* Test change currentIndex manually
99
void FocusPathTest::testChangeCurrentIndex()
101
QCOMPARE(m_focusPath->currentIndex(), 0);
102
m_focusPath->setCurrentIndex(5);
103
QCOMPARE(m_focusPath->currentIndex(), 5);
104
QCOMPARE(m_focusPath->currentItem()->property("itemData").toString(), QString("Item5"));
106
/* Set a invalid index */
107
m_focusPath->setCurrentIndex(10);
109
/* It must keep the previous index */
110
QCOMPARE(m_focusPath->currentIndex(), 5);
111
QCOMPARE(m_focusPath->currentItem()->property("itemData").toString(), QString("Item5"));
115
* Test if keyboard navigation is working correct
117
void FocusPathTest::testNavigation()
119
QCOMPARE(m_focusPath->currentItem()->property("itemData").toString(), QString("Item0"));
120
/* Horizontal navigation */
123
QTest::keyPress(m_view, Qt::Key_Right);
124
QCOMPARE(m_focusPath->currentItem()->property("itemData").toString(), QString("Item%1").arg(++index));
125
QCOMPARE(m_focusPath->currentIndex(), index);
126
} while(m_focusPath->currentIndex() < 8);
129
QTest::keyPress(m_view, Qt::Key_Left);
130
QCOMPARE(m_focusPath->currentItem()->property("itemData").toString(), QString("Item%1").arg(--index));
131
QCOMPARE(m_focusPath->currentIndex(), index);
132
} while (m_focusPath->currentIndex() > 0);
134
/* Vertical navigation */
135
QTest::keyPress(m_view, Qt::Key_Down);
136
QCOMPARE(m_focusPath->currentItem()->property("itemData").toString(), QString("Item3"));
137
QTest::keyPress(m_view, Qt::Key_Down);
138
QCOMPARE(m_focusPath->currentItem()->property("itemData").toString(), QString("Item6"));
139
QTest::keyPress(m_view, Qt::Key_Right);
140
QCOMPARE(m_focusPath->currentItem()->property("itemData").toString(), QString("Item7"));
141
QTest::keyPress(m_view, Qt::Key_Up);
142
QCOMPARE(m_focusPath->currentItem()->property("itemData").toString(), QString("Item4"));
143
QTest::keyPress(m_view, Qt::Key_Up);
144
QCOMPARE(m_focusPath->currentItem()->property("itemData").toString(), QString("Item1"));
148
* Test Flow property with value TopToBottom
150
void FocusPathTest::testFlow()
152
QGraphicsObject *root = m_view->rootObject();
154
QObject *grid = root->findChild<QObject*>("gridLayout");
155
grid->setProperty("flow", 1);
156
QCOMPARE(m_focusPath->flow(), FocusPath::TopToBottom);
158
QCOMPARE(m_focusPath->currentIndex(), 0);
159
QTest::keyPress(m_view, Qt::Key_Down);
160
QCOMPARE(m_focusPath->currentIndex(), 1);
161
QTest::keyPress(m_view, Qt::Key_Down);
162
QCOMPARE(m_focusPath->currentIndex(), 2);
163
QTest::keyPress(m_view, Qt::Key_Right);
164
QCOMPARE(m_focusPath->currentIndex(), 5);
165
QTest::keyPress(m_view, Qt::Key_Right);
166
QCOMPARE(m_focusPath->currentIndex(), 8);
167
QTest::keyPress(m_view, Qt::Key_Up);
168
QCOMPARE(m_focusPath->currentIndex(), 7);
169
QTest::keyPress(m_view, Qt::Key_Up);
170
QCOMPARE(m_focusPath->currentIndex(), 6);
171
QTest::keyPress(m_view, Qt::Key_Left);
172
QCOMPARE(m_focusPath->currentIndex(), 3);
173
QTest::keyPress(m_view, Qt::Key_Left);
174
QCOMPARE(m_focusPath->currentIndex(), 0);
178
* Test NavigationDirection limitations
180
void FocusPathTest::testNavigationDirection()
182
m_focusPath->setDirection(FocusPath::Horizontal);
183
QTest::keyPress(m_view, Qt::Key_Down);
184
QCOMPARE(m_focusPath->currentIndex(), 0);
185
QTest::keyPress(m_view, Qt::Key_Right);
186
QCOMPARE(m_focusPath->currentIndex(), 1);
187
QTest::keyPress(m_view, Qt::Key_Left);
188
QCOMPARE(m_focusPath->currentIndex(), 0);
190
m_focusPath->setDirection(FocusPath::Vertical);
191
QTest::keyPress(m_view, Qt::Key_Right);
192
QCOMPARE(m_focusPath->currentIndex(), 0);
193
QTest::keyPress(m_view, Qt::Key_Down);
194
QCOMPARE(m_focusPath->currentIndex(), 3);
195
QTest::keyPress(m_view, Qt::Key_Up);
196
QCOMPARE(m_focusPath->currentIndex(), 0);
200
* Test RightToLeft navigation
202
void FocusPathTest::testRTLNavigation()
204
QGraphicsObject *root = m_view->rootObject();
206
QObject *grid = root->findChild<QObject*>("gridLayout");
207
grid->setProperty("layoutDirection", 1);
208
m_focusPath->setFlow(FocusPath::RightToLeft);
211
QCOMPARE(m_focusPath->currentIndex(), 0);
212
QTest::keyPress(m_view, Qt::Key_Down);
213
QCOMPARE(m_focusPath->currentIndex(), 3);
214
QTest::keyPress(m_view, Qt::Key_Down);
215
QCOMPARE(m_focusPath->currentIndex(), 6);
217
QTest::keyPress(m_view, Qt::Key_Left);
218
QCOMPARE(m_focusPath->currentIndex(), 7);
219
QTest::keyPress(m_view, Qt::Key_Left);
220
QCOMPARE(m_focusPath->currentIndex(), 8);
222
QTest::keyPress(m_view, Qt::Key_Up);
223
QCOMPARE(m_focusPath->currentIndex(), 5);
224
QTest::keyPress(m_view, Qt::Key_Up);
225
QCOMPARE(m_focusPath->currentIndex(), 2);
227
QTest::keyPress(m_view, Qt::Key_Right);
228
QCOMPARE(m_focusPath->currentIndex(), 1);
229
QTest::keyPress(m_view, Qt::Key_Right);
230
QCOMPARE(m_focusPath->currentIndex(), 0);
234
* Test insert a new item in the midle of path
237
void FocusPathTest::testOrderChange()
239
/* Check if current index 3 is the item3 */
240
QCOMPARE(m_focusPath->currentIndex(), 0);
241
m_focusPath->setCurrentIndex(3);
242
QCOMPARE(m_focusPath->currentItem()->property("itemData").toString(), QString("Item3"));
245
QGraphicsObject *root = m_view->rootObject();
247
QObject *grid = root->findChild<QObject*>("gridLayout");
249
/* Insert a new item with index 3 */
250
QMetaObject::invokeMethod(grid, "insertItem", Q_ARG(QVariant, 3), Q_ARG(QVariant, "newItem3"));
252
QCOMPARE(m_focusPath->path().size(), 10);
254
/* Cehck if the item with index 3 is the new item */
255
m_focusPath->setCurrentIndex(3);
256
QCOMPARE(m_focusPath->currentItem()->property("itemData").toString(), QString("newItem3"));
260
UAPP_TEST_MAIN(FocusPathTest)
262
#include "focuspathtest.moc"