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