on 0cfbca3 help sign in
unity8/tests/plugins/Dash/listviewwithpageheadersectionexternalmodeltest.cpp
248 lines 8.2 kB history raw
1/*
2 * Copyright (C) 2013 Canonical, Ltd.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 3.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
17#include "listviewwithpageheader.h"
19#include <QAbstractItemModel>
20#include <QQmlEngine>
21#include <QQuickView>
22#include <QSignalSpy>
23#include <QSortFilterProxyModel>
24#include <QtTestGui>
25#pragma GCC diagnostic push
26#pragma GCC diagnostic ignored "-pedantic"
27#include <private/qqmllistmodel_p.h>
28#include <private/qquickanimation_p.h>
29#include <private/qquickitem_p.h>
30#pragma GCC diagnostic pop
32#include <paths.h>
34class StringListModel : public QAbstractListModel
36public:
37 StringListModel()
38 {
39 list << "A" << "B" << "C" << "D" << "E" << "F" << "G" << "H";
40 }
42 int rowCount(const QModelIndex &parent) const override
43 {
44 if (parent.isValid())
45 return 0;
46 else
47 return list.count();
48 }
50 QVariant data(const QModelIndex &index, int role) const override
51 {
52 if (role == Qt::SizeHintRole) return 50;
53 else if (role == Qt::DisplayRole) return list[index.row()];
54 return QVariant();
55 }
57 QHash<int, QByteArray> roleNames() const override
58 {
59 QHash<int, QByteArray> hash;
60 hash.insert(Qt::DisplayRole, "type");
61 hash.insert(Qt::SizeHintRole, "size");
62 return hash;
63 }
65 void moveLastToFirst()
66 {
67 const int last = list.count() - 1;
68 beginMoveRows(QModelIndex(), last, last, QModelIndex(), 0);
69 list.move(last, 0);
70 endMoveRows();
71 }
73 QStringList list;
74};
76class ListViewWithPageHeaderTestSection : public QObject
78 Q_OBJECT
80private:
81 void verifyItem(int visibleIndex, qreal pos, qreal height, bool culled, const QString &sectionHeader, bool sectionHeaderCulled)
82 {
83 QTRY_VERIFY(visibleIndex < lvwph->m_visibleItems.count());
84 QTRY_COMPARE(lvwph->m_visibleItems[visibleIndex]->y(), pos);
85 QTRY_COMPARE(lvwph->m_visibleItems[visibleIndex]->height(), height);
86 QCOMPARE(QQuickItemPrivate::get(lvwph->m_visibleItems[visibleIndex]->m_item)->culled, culled);
87 QCOMPARE(section(lvwph->m_visibleItems[visibleIndex]->sectionItem()), sectionHeader);
88 if (!sectionHeader.isNull()) {
89 QCOMPARE(QQuickItemPrivate::get(lvwph->m_visibleItems[visibleIndex]->sectionItem())->culled, sectionHeaderCulled);
90 QCOMPARE(sectionDelegateIndex(lvwph->m_visibleItems[visibleIndex]->sectionItem()), lvwph->m_firstVisibleIndex + visibleIndex);
91 }
92 }
94 void changeContentY(qreal change)
95 {
96 const qreal dest = lvwph->contentY() + change;
97 if (dest > lvwph->contentY()) {
98 const qreal jump = 25;
99 while (lvwph->contentY() + jump < dest) {
100 lvwph->setContentY(lvwph->contentY() + jump);
101 QTest::qWait(1);
102 }
103 } else {
104 const qreal jump = -25;
105 while (lvwph->contentY() + jump > dest) {
106 lvwph->setContentY(lvwph->contentY() + jump);
107 QTest::qWait(1);
108 }
109 }
110 lvwph->setContentY(dest);
111 QTest::qWait(1);
112 }
114 void scrollToTop()
115 {
116 const qreal jump = -25;
117 while (!lvwph->isAtYBeginning()) {
118 if (lvwph->contentY() + jump > -lvwph->minYExtent()) {
119 lvwph->setContentY(lvwph->contentY() + jump);
120 } else {
121 lvwph->setContentY(lvwph->contentY() - 1);
122 }
123 QTest::qWait(1);
124 }
125 }
127 void scrollToBottom()
128 {
129 const qreal jump = 25;
130 while (!lvwph->isAtYEnd()) {
131 if (lvwph->contentY() + lvwph->height() + jump < lvwph->contentHeight()) {
132 lvwph->setContentY(lvwph->contentY() + jump);
133 } else {
134 lvwph->setContentY(lvwph->contentY() + 1);
135 }
136 QTest::qWait(1);
137 }
138 }
140 void verifyInitialTopPosition()
141 {
142 QTRY_COMPARE(lvwph->m_visibleItems.count(), 0);
143 QCOMPARE(lvwph->m_firstVisibleIndex, -1);
144 QCOMPARE(lvwph->m_minYExtent, 0.);
145 QCOMPARE(lvwph->m_clipItem->y(), 0.);
146 QCOMPARE(lvwph->m_clipItem->clip(), false);
147 QCOMPARE(lvwph->m_headerItem->y(), 0.);
148 QCOMPARE(lvwph->m_headerItem->height(), 50.);
149 QCOMPARE(lvwph->contentY(), 0.);
150 QCOMPARE(lvwph->m_headerItemShownHeight, 0.);
151 QVERIFY(QQuickItemPrivate::get(lvwph->m_topSectionItem)->culled);
152 }
154 QString section(QQuickItem *item)
155 {
156 return item ? QQmlEngine::contextForObject(item)->parentContext()->contextProperty(QLatin1String("section")).toString() : QString();
157 }
159 int sectionDelegateIndex(QQuickItem *item)
160 {
161 return item ? QQmlEngine::contextForObject(item)->parentContext()->contextProperty(QLatin1String("delegateIndex")).toInt() : -1;
162 }
164private Q_SLOTS:
166 void initTestCase()
167 {
168 }
170 void init()
171 {
172 view = new QQuickView();
173 view->setSource(QUrl::fromLocalFile(testDataDir() + "/" TEST_DIR "/listviewwithpageheadertestsectionexternalmodel.qml"));
174 lvwph = dynamic_cast<ListViewWithPageHeader*>(view->rootObject()->findChild<QQuickFlickable*>());
175 QVERIFY(lvwph);
176 view->show();
177 QTest::qWaitForWindowExposed(view);
179 verifyInitialTopPosition();
180 }
182 void cleanup()
183 {
184 delete view;
185 }
187 void testCreationDeletion()
188 {
189 // Nothing, init/cleanup already tests this
190 }
192 void testMoveWithProxy()
193 {
194 StringListModel m;
195 QSortFilterProxyModel proxy;
196 proxy.setSourceModel(&m);
198 lvwph->setModel(&proxy);
200 QTRY_COMPARE(lvwph->m_visibleItems.count(), 8);
201 QCOMPARE(lvwph->m_firstVisibleIndex, 0);
202 verifyItem(0, 50., 90., false, "A", false);
203 verifyItem(1, 140., 90., false, "B", false);
204 verifyItem(2, 230., 90., false, "C", false);
205 verifyItem(3, 320., 90., false, "D", false);
206 verifyItem(4, 410., 90., false, "E", false);
207 verifyItem(5, 500., 90., false, "F", false);
208 verifyItem(6, 590., 90., true, "G", true);
209 verifyItem(7, 680., 90., true, "H", true);
210 QCOMPARE(lvwph->m_minYExtent, 0.);
211 QCOMPARE(lvwph->m_clipItem->y(), 0.);
212 QCOMPARE(lvwph->m_clipItem->clip(), false);
213 QCOMPARE(lvwph->m_headerItem->y(), 0.);
214 QCOMPARE(lvwph->m_headerItem->height(), 50.);
215 QCOMPARE(lvwph->contentY(), 0.);
216 QCOMPARE(lvwph->m_headerItemShownHeight, 0.);
217 QVERIFY(QQuickItemPrivate::get(lvwph->m_topSectionItem)->culled);
219 m.moveLastToFirst();
221 QTRY_COMPARE(lvwph->m_visibleItems.count(), 8);
222 QCOMPARE(lvwph->m_firstVisibleIndex, 0);
223 verifyItem(0, 50., 90., false, "H", false);
224 verifyItem(1, 140., 90., false, "A", false);
225 verifyItem(2, 230., 90., false, "B", false);
226 verifyItem(3, 320., 90., false, "C", false);
227 verifyItem(4, 410., 90., false, "D", false);
228 verifyItem(5, 500., 90., false, "E", false);
229 verifyItem(6, 590., 90., true, "F", true);
230 verifyItem(7, 680., 90., true, "G", true);
231 QCOMPARE(lvwph->m_minYExtent, 0.);
232 QCOMPARE(lvwph->m_clipItem->y(), 0.);
233 QCOMPARE(lvwph->m_clipItem->clip(), false);
234 QCOMPARE(lvwph->m_headerItem->y(), 0.);
235 QCOMPARE(lvwph->m_headerItem->height(), 50.);
236 QCOMPARE(lvwph->contentY(), 0.);
237 QCOMPARE(lvwph->m_headerItemShownHeight, 0.);
238 QVERIFY(QQuickItemPrivate::get(lvwph->m_topSectionItem)->culled);
239 }
241private:
242 QQuickView *view;
243 ListViewWithPageHeader *lvwph;
244};
246QTEST_MAIN(ListViewWithPageHeaderTestSection)
248#include "listviewwithpageheadersectionexternalmodeltest.moc"