~fboucault/unity-2d/windowimageprovider_remove_timestamp_hack

« back to all changes in this revision

Viewing changes to libunity-2d-private/tests/focuspathtest.cpp

  • Committer: Tarmac
  • Author(s): Renato Araujo Oliveira Filho, Florian Boucault
  • Date: 2011-11-22 18:20:16 UTC
  • mfrom: (768.5.42 places-rewrite)
  • Revision ID: tarmac-20111122182016-zyuclvankjf11dfo
[places] Rewrite RenderGrid to use only one Flickable element

* Created FocusPath element to help in the keyboard navigation;
* The code used to manually scroll the grid was removed, now everything is done by the QML Flickable element;
* This new implementation of RenderGrid allows every component be inside of this scroller, which makes the grid more flexible for moving elements around, helping in the implementation of Preview screen;

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * This file is part of unity-2d
 
3
 *
 
4
 * Copyright 2010 Canonical Ltd.
 
5
 *
 
6
 * Authors:
 
7
 * - Renato Araujo Oliveira Filho <renato.filho@canonical.com>
 
8
 *
 
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.
 
12
 *
 
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.
 
17
 *
 
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/>
 
20
 */
 
21
 
 
22
#include <unitytestmacro.h>
 
23
#include <config.h>
 
24
#include <focuspath.h>
 
25
 
 
26
#include <QGraphicsObject>
 
27
#include <QDeclarativeEngine>
 
28
#include <QObject>
 
29
#include <QDeclarativeView>
 
30
#include <QtTestGui>
 
31
 
 
32
class FocusPathTest : public QObject
 
33
{
 
34
    Q_OBJECT
 
35
private Q_SLOTS:
 
36
    void init();
 
37
    void cleanup();
 
38
    void testInitialization();
 
39
    void testChangeCurrentIndex();
 
40
    void testNavigation();
 
41
    void testFlow();
 
42
    void testNavigationDirection();
 
43
    void testRTLNavigation();
 
44
    void testOrderChange();
 
45
 
 
46
private:
 
47
    QDeclarativeView *m_view;
 
48
    FocusPath *m_focusPath;
 
49
 
 
50
    void createView();
 
51
};
 
52
 
 
53
 
 
54
/*
 
55
 * Loads a QML file with a grix 3x3 elements
 
56
 */
 
57
 
 
58
void FocusPathTest::init()
 
59
{
 
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())));
 
63
 
 
64
    QGraphicsObject *root = m_view->rootObject();
 
65
    Q_ASSERT(root);
 
66
    m_focusPath = root->findChild<FocusPath*>("focusPath");
 
67
    Q_ASSERT(m_focusPath);
 
68
 
 
69
}
 
70
 
 
71
void FocusPathTest::cleanup()
 
72
{
 
73
    m_focusPath = 0;
 
74
    delete m_view;
 
75
    m_view = 0;
 
76
}
 
77
 
 
78
/*
 
79
 * Check if all items was inserted in FocusPath
 
80
 */
 
81
void FocusPathTest::testInitialization()
 
82
{
 
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);
 
89
    }
 
90
 
 
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);
 
94
}
 
95
 
 
96
/*
 
97
 * Test change currentIndex manually
 
98
 */
 
99
void FocusPathTest::testChangeCurrentIndex()
 
100
{
 
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"));
 
105
 
 
106
    /* Set a invalid index */
 
107
    m_focusPath->setCurrentIndex(10);
 
108
 
 
109
    /* It must keep the previous index */
 
110
    QCOMPARE(m_focusPath->currentIndex(), 5);
 
111
    QCOMPARE(m_focusPath->currentItem()->property("itemData").toString(), QString("Item5"));
 
112
}
 
113
 
 
114
/*
 
115
 * Test if keyboard navigation is working correct
 
116
 */
 
117
void FocusPathTest::testNavigation()
 
118
{
 
119
    QCOMPARE(m_focusPath->currentItem()->property("itemData").toString(), QString("Item0"));
 
120
    /* Horizontal navigation */
 
121
    int index = 0;
 
122
    do {
 
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);
 
127
 
 
128
    do {
 
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);
 
133
 
 
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"));
 
145
}
 
146
 
 
147
/*
 
148
 * Test Flow property with value TopToBottom 
 
149
 */
 
150
void FocusPathTest::testFlow()
 
151
{
 
152
    QGraphicsObject *root = m_view->rootObject();
 
153
    Q_ASSERT(root);
 
154
    QObject *grid = root->findChild<QObject*>("gridLayout");
 
155
    grid->setProperty("flow", 1);
 
156
    QCOMPARE(m_focusPath->flow(), FocusPath::TopToBottom);
 
157
 
 
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);
 
175
}
 
176
 
 
177
/*
 
178
 * Test NavigationDirection limitations
 
179
 */
 
180
void FocusPathTest::testNavigationDirection()
 
181
{
 
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);
 
189
 
 
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);
 
197
}
 
198
 
 
199
/*
 
200
 * Test RightToLeft navigation
 
201
 */
 
202
void FocusPathTest::testRTLNavigation()
 
203
{
 
204
    QGraphicsObject *root = m_view->rootObject();
 
205
    Q_ASSERT(root);
 
206
    QObject *grid = root->findChild<QObject*>("gridLayout");
 
207
    grid->setProperty("layoutDirection", 1);
 
208
    m_focusPath->setFlow(FocusPath::RightToLeft);
 
209
 
 
210
 
 
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);
 
216
 
 
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);
 
221
 
 
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);
 
226
 
 
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);
 
231
}
 
232
 
 
233
/*
 
234
 * Test insert a new item in the midle of path
 
235
 */
 
236
 
 
237
void FocusPathTest::testOrderChange()
 
238
{
 
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"));
 
243
 
 
244
 
 
245
    QGraphicsObject *root = m_view->rootObject();
 
246
    Q_ASSERT(root);
 
247
    QObject *grid = root->findChild<QObject*>("gridLayout");
 
248
 
 
249
    /* Insert a new item with index 3 */
 
250
    QMetaObject::invokeMethod(grid, "insertItem", Q_ARG(QVariant, 3), Q_ARG(QVariant, "newItem3"));
 
251
 
 
252
    QCOMPARE(m_focusPath->path().size(), 10);
 
253
 
 
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"));
 
257
}
 
258
 
 
259
 
 
260
UAPP_TEST_MAIN(FocusPathTest)
 
261
 
 
262
#include "focuspathtest.moc"