on master help sign in
unity8/tests/plugins/Dash/listviewwithpageheadersectionexternalmodeltest.cpp
246 lines 8.0 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#include <private/qqmllistmodel_p.h>
26#include <private/qquickanimation_p.h>
27#include <private/qquickitem_p.h>
29#include <paths.h>
31class StringListModel : public QAbstractListModel
33 Q_OBJECT
34public:
35 StringListModel()
36 {
37 list << "A" << "B" << "C" << "D" << "E" << "F" << "G" << "H";
38 }
40 int rowCount(const QModelIndex &parent) const override
41 {
42 if (parent.isValid())
43 return 0;
44 else
45 return list.count();
46 }
48 QVariant data(const QModelIndex &index, int role) const override
49 {
50 if (role == Qt::SizeHintRole) return 50;
51 else if (role == Qt::DisplayRole) return list[index.row()];
52 return QVariant();
53 }
55 QHash<int, QByteArray> roleNames() const override
56 {
57 QHash<int, QByteArray> hash;
58 hash.insert(Qt::DisplayRole, "type");
59 hash.insert(Qt::SizeHintRole, "size");
60 return hash;
61 }
63 void moveLastToFirst()
64 {
65 const int last = list.count() - 1;
66 beginMoveRows(QModelIndex(), last, last, QModelIndex(), 0);
67 list.move(last, 0);
68 endMoveRows();
69 }
71 QStringList list;
72};
74class ListViewWithPageHeaderTestSection : public QObject
76 Q_OBJECT
78private:
79 void verifyItem(int visibleIndex, qreal pos, qreal height, bool culled, const QString &sectionHeader, bool sectionHeaderCulled)
80 {
81 QTRY_VERIFY(visibleIndex < lvwph->m_visibleItems.count());
82 QTRY_COMPARE(lvwph->m_visibleItems[visibleIndex]->y(), pos);
83 QTRY_COMPARE(lvwph->m_visibleItems[visibleIndex]->height(), height);
84 QCOMPARE(QQuickItemPrivate::get(lvwph->m_visibleItems[visibleIndex]->m_item)->culled, culled);
85 QCOMPARE(section(lvwph->m_visibleItems[visibleIndex]->sectionItem()), sectionHeader);
86 if (!sectionHeader.isNull()) {
87 QCOMPARE(QQuickItemPrivate::get(lvwph->m_visibleItems[visibleIndex]->sectionItem())->culled, sectionHeaderCulled);
88 QCOMPARE(sectionDelegate(lvwph->m_visibleItems[visibleIndex]->sectionItem()), lvwph->m_visibleItems[visibleIndex]->m_item);
89 }
90 }
92 void changeContentY(qreal change)
93 {
94 const qreal dest = lvwph->contentY() + change;
95 if (dest > lvwph->contentY()) {
96 const qreal jump = 25;
97 while (lvwph->contentY() + jump < dest) {
98 lvwph->setContentY(lvwph->contentY() + jump);
99 QTest::qWait(1);
100 }
101 } else {
102 const qreal jump = -25;
103 while (lvwph->contentY() + jump > dest) {
104 lvwph->setContentY(lvwph->contentY() + jump);
105 QTest::qWait(1);
106 }
107 }
108 lvwph->setContentY(dest);
109 QTest::qWait(1);
110 }
112 void scrollToTop()
113 {
114 const qreal jump = -25;
115 while (!lvwph->isAtYBeginning()) {
116 if (lvwph->contentY() + jump > -lvwph->minYExtent()) {
117 lvwph->setContentY(lvwph->contentY() + jump);
118 } else {
119 lvwph->setContentY(lvwph->contentY() - 1);
120 }
121 QTest::qWait(1);
122 }
123 }
125 void scrollToBottom()
126 {
127 const qreal jump = 25;
128 while (!lvwph->isAtYEnd()) {
129 if (lvwph->contentY() + lvwph->height() + jump < lvwph->contentHeight()) {
130 lvwph->setContentY(lvwph->contentY() + jump);
131 } else {
132 lvwph->setContentY(lvwph->contentY() + 1);
133 }
134 QTest::qWait(1);
135 }
136 }
138 void verifyInitialTopPosition()
139 {
140 QTRY_COMPARE(lvwph->m_visibleItems.count(), 0);
141 QCOMPARE(lvwph->m_firstVisibleIndex, -1);
142 QCOMPARE(lvwph->m_minYExtent, 0.);
143 QCOMPARE(lvwph->m_clipItem->y(), 0.);
144 QCOMPARE(lvwph->m_clipItem->clip(), false);
145 QCOMPARE(lvwph->m_headerItem->y(), 0.);
146 QCOMPARE(lvwph->m_headerItem->height(), 50.);
147 QCOMPARE(lvwph->contentY(), 0.);
148 QCOMPARE(lvwph->m_headerItemShownHeight, 0.);
149 QVERIFY(QQuickItemPrivate::get(lvwph->m_topSectionItem)->culled);
150 }
152 QString section(QQuickItem *item)
153 {
154 return item ? item->property("text").toString() : QString();
155 }
157 QQuickItem *sectionDelegate(QQuickItem *item)
158 {
159 return item ? item->property("delegate").value<QQuickItem *>() : nullptr;
160 }
162private Q_SLOTS:
164 void initTestCase()
165 {
166 }
168 void init()
169 {
170 view = new QQuickView();
171 view->setSource(QUrl::fromLocalFile(testDataDir() + "/" TEST_DIR "/listviewwithpageheadertestsectionexternalmodel.qml"));
172 lvwph = static_cast<ListViewWithPageHeader*>(view->rootObject()->findChild<QQuickFlickable*>());
173 QVERIFY(lvwph);
174 view->show();
175 QTest::qWaitForWindowExposed(view);
177 verifyInitialTopPosition();
178 }
180 void cleanup()
181 {
182 delete view;
183 }
185 void testCreationDeletion()
186 {
187 // Nothing, init/cleanup already tests this
188 }
190 void testMoveWithProxy()
191 {
192 StringListModel m;
193 QSortFilterProxyModel proxy;
194 proxy.setSourceModel(&m);
196 lvwph->setModel(&proxy);
198 QTRY_COMPARE(lvwph->m_visibleItems.count(), 8);
199 QCOMPARE(lvwph->m_firstVisibleIndex, 0);
200 verifyItem(0, 50., 90., false, "A", false);
201 verifyItem(1, 140., 90., false, "B", false);
202 verifyItem(2, 230., 90., false, "C", false);
203 verifyItem(3, 320., 90., false, "D", false);
204 verifyItem(4, 410., 90., false, "E", false);
205 verifyItem(5, 500., 90., false, "F", false);
206 verifyItem(6, 590., 90., true, "G", true);
207 verifyItem(7, 680., 90., true, "H", true);
208 QCOMPARE(lvwph->m_minYExtent, 0.);
209 QCOMPARE(lvwph->m_clipItem->y(), 0.);
210 QCOMPARE(lvwph->m_clipItem->clip(), false);
211 QCOMPARE(lvwph->m_headerItem->y(), 0.);
212 QCOMPARE(lvwph->m_headerItem->height(), 50.);
213 QCOMPARE(lvwph->contentY(), 0.);
214 QCOMPARE(lvwph->m_headerItemShownHeight, 0.);
215 QVERIFY(QQuickItemPrivate::get(lvwph->m_topSectionItem)->culled);
217 m.moveLastToFirst();
219 QTRY_COMPARE(lvwph->m_visibleItems.count(), 8);
220 QCOMPARE(lvwph->m_firstVisibleIndex, 0);
221 verifyItem(0, 50., 90., false, "H", false);
222 verifyItem(1, 140., 90., false, "A", false);
223 verifyItem(2, 230., 90., false, "B", false);
224 verifyItem(3, 320., 90., false, "C", false);
225 verifyItem(4, 410., 90., false, "D", false);
226 verifyItem(5, 500., 90., false, "E", false);
227 verifyItem(6, 590., 90., true, "F", true);
228 verifyItem(7, 680., 90., true, "G", true);
229 QCOMPARE(lvwph->m_minYExtent, 0.);
230 QCOMPARE(lvwph->m_clipItem->y(), 0.);
231 QCOMPARE(lvwph->m_clipItem->clip(), false);
232 QCOMPARE(lvwph->m_headerItem->y(), 0.);
233 QCOMPARE(lvwph->m_headerItem->height(), 50.);
234 QCOMPARE(lvwph->contentY(), 0.);
235 QCOMPARE(lvwph->m_headerItemShownHeight, 0.);
236 QVERIFY(QQuickItemPrivate::get(lvwph->m_topSectionItem)->culled);
237 }
239private:
240 QQuickView *view;
241 ListViewWithPageHeader *lvwph;
242};
244QTEST_MAIN(ListViewWithPageHeaderTestSection)
246#include "listviewwithpageheadersectionexternalmodeltest.moc"