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