1 /****************************************************************************
2 **
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
6 **
7 ** This file is part of the test suite of the Qt Toolkit.
8 **
9 ** This file 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, either version 3 of the License, or
12 ** (at your option) any later version.
13 **
14 ** $QT_BEGIN_LICENSE:LGPL$
15 ** GNU Lesser General Public License Usage
16 ** This file may be used under the terms of the GNU Lesser General Public
17 ** License version 2.1 as published by the Free Software Foundation and
18 ** appearing in the file LICENSE.LGPL included in the packaging of this
19 ** file. Please review the following information to ensure the GNU Lesser
20 ** General Public License version 2.1 requirements will be met:
21 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22 **
23 ** In addition, as a special exception, Nokia gives you certain additional
24 ** rights. These rights are described in the Nokia Qt LGPL Exception
25 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
26 **
27 ** GNU General Public License Usage
28 ** Alternatively, this file may be used under the terms of the GNU General
29 ** Public License version 3.0 as published by the Free Software Foundation
30 ** and appearing in the file LICENSE.GPL included in the packaging of this
31 ** file. Please review the following information to ensure the GNU General
32 ** Public License version 3.0 requirements will be met:
33 ** http://www.gnu.org/copyleft/gpl.html.
34 **
35 ** Other Usage
36 ** Alternatively, this file may be used in accordance with the terms and
37 ** conditions contained in a signed written agreement between you and Nokia.
38 **
39 **
40 **
41 **
42 **
43 ** $QT_END_LICENSE$
44 **
45 ****************************************************************************/
46
47
48 #include <QtTest/QTest>
49 #include <QObject>
50 #include <QStringListModel>
51 #include <QSortFilterProxyModel>
52 #include <QTreeWidget>
53 #include <QTreeWidgetItem>
54 #include <QStandardItemModel>
55 #include <QRegularExpression>
56
57 #include "modeltest.h"
58 #include "dynamictreemodel.h"
59
60
61 class tst_ModelTest : public QObject
62 {
63 Q_OBJECT
64
65 public:
tst_ModelTest()66 tst_ModelTest() {}
~tst_ModelTest()67 virtual ~tst_ModelTest() {}
68
69 public slots:
70 void initTestCase();
71 void cleanupTestCase();
72 void init();
73 void cleanup();
74
75 private slots:
76 void stringListModel();
77 void treeWidgetModel();
78 void standardItemModel();
79 void testInsertThroughProxy();
80 void moveSourceItems();
81 void testResetThroughProxy();
82 };
83
84
85
initTestCase()86 void tst_ModelTest::initTestCase()
87 {
88 }
89
cleanupTestCase()90 void tst_ModelTest::cleanupTestCase()
91 {
92 }
93
init()94 void tst_ModelTest::init()
95 {
96
97 }
98
cleanup()99 void tst_ModelTest::cleanup()
100 {
101 }
102 /*
103 tests
104 */
105
stringListModel()106 void tst_ModelTest::stringListModel()
107 {
108 QStringListModel model;
109 QSortFilterProxyModel proxy;
110
111 ModelTest t1( &model );
112 ModelTest t2( &proxy );
113
114 proxy.setSourceModel( &model );
115
116 model.setStringList( QStringList() << "2" << "3" << "1" );
117 model.setStringList( QStringList() << "a" << "e" << "plop" << "b" << "c" );
118
119 proxy.setDynamicSortFilter( true );
120 proxy.setFilterRegularExpression( QRegularExpression( "[^b]" ) );
121 }
122
treeWidgetModel()123 void tst_ModelTest::treeWidgetModel()
124 {
125 QTreeWidget widget;
126
127 ModelTest t1( widget.model() );
128
129 QTreeWidgetItem *root = new QTreeWidgetItem( &widget, QStringList( "root" ) );
130 for ( int i = 0; i < 20; ++i )
131 {
132 new QTreeWidgetItem( root, QStringList( QString::number( i ) ) );
133 }
134 QTreeWidgetItem *remove = root->child( 2 );
135 root->removeChild( remove );
136 QTreeWidgetItem *parent = new QTreeWidgetItem( &widget, QStringList( "parent" ) );
137 new QTreeWidgetItem( parent, QStringList( "child" ) );
138 widget.setItemHidden( parent, true );
139
140 widget.sortByColumn( 0 );
141 }
142
standardItemModel()143 void tst_ModelTest::standardItemModel()
144 {
145 QStandardItemModel model( 10, 10 );
146 QSortFilterProxyModel proxy;
147
148
149 ModelTest t1( &model );
150 ModelTest t2( &proxy );
151
152 proxy.setSourceModel( &model );
153
154 model.insertRows( 2, 5 );
155 model.removeRows( 4, 5 );
156
157 model.insertColumns( 2, 5 );
158 model.removeColumns( 4, 5 );
159
160 model.insertRows( 0, 5, model.index( 1, 1 ) );
161 model.insertColumns( 0, 5, model.index( 1, 3 ) );
162 }
163
testInsertThroughProxy()164 void tst_ModelTest::testInsertThroughProxy()
165 {
166 DynamicTreeModel *model = new DynamicTreeModel( this );
167
168 QSortFilterProxyModel *proxy = new QSortFilterProxyModel( this );
169 proxy->setSourceModel( model );
170
171 new ModelTest( proxy, this );
172
173 ModelInsertCommand *insertCommand = new ModelInsertCommand( model, this );
174 insertCommand->setNumCols( 4 );
175 insertCommand->setStartRow( 0 );
176 insertCommand->setEndRow( 9 );
177 // Parent is QModelIndex()
178 insertCommand->doCommand();
179
180 insertCommand = new ModelInsertCommand( model, this );
181 insertCommand->setNumCols( 4 );
182 insertCommand->setAncestorRowNumbers( QList<int>() << 5 );
183 insertCommand->setStartRow( 0 );
184 insertCommand->setEndRow( 9 );
185 insertCommand->doCommand();
186
187 ModelMoveCommand *moveCommand = new ModelMoveCommand( model, this );
188 moveCommand->setNumCols( 4 );
189 moveCommand->setStartRow( 0 );
190 moveCommand->setEndRow( 0 );
191 moveCommand->setDestRow( 9 );
192 moveCommand->setDestAncestors( QList<int>() << 5 );
193 moveCommand->doCommand();
194 }
195
196 /**
197 * Makes the persistent index list publicly accessible
198 */
199 class AccessibleProxyModel : public QSortFilterProxyModel
200 {
201 Q_OBJECT
202 public:
AccessibleProxyModel(QObject * parent=0)203 explicit AccessibleProxyModel( QObject *parent = 0 ) : QSortFilterProxyModel( parent ) {}
204
persistent()205 QModelIndexList persistent()
206 {
207 return persistentIndexList();
208 }
209 };
210
211 class ObservingObject : public QObject
212 {
213 Q_OBJECT
214 public:
ObservingObject(AccessibleProxyModel * proxy,QObject * parent=0)215 ObservingObject( AccessibleProxyModel *proxy, QObject *parent = 0 )
216 : QObject( parent ),
217 m_proxy( proxy )
218 {
219 connect( m_proxy, SIGNAL( layoutAboutToBeChanged() ), SLOT( storePersistent() ) );
220 connect( m_proxy, SIGNAL( layoutChanged() ), SLOT( checkPersistent() ) );
221 }
222
223 public slots:
224
storePersistent(const QModelIndex & parent)225 void storePersistent( const QModelIndex &parent )
226 {
227 for ( int row = 0; row < m_proxy->rowCount( parent ); ++row )
228 {
229 QModelIndex proxyIndex = m_proxy->index( row, 0, parent );
230 QModelIndex sourceIndex = m_proxy->mapToSource( proxyIndex );
231 Q_ASSERT( proxyIndex.isValid() );
232 Q_ASSERT( sourceIndex.isValid() );
233 m_persistentSourceIndexes.append( sourceIndex );
234 m_persistentProxyIndexes.append( proxyIndex );
235 if ( m_proxy->hasChildren( proxyIndex ) )
236 storePersistent( proxyIndex );
237 }
238 }
239
storePersistent()240 void storePersistent()
241 {
242 foreach ( const QModelIndex &idx, m_persistentProxyIndexes )
243 Q_ASSERT( idx.isValid() ); // This is called from layoutAboutToBeChanged. Persistent indexes should be valid
244
245 Q_ASSERT( m_proxy->persistent().isEmpty() );
246 storePersistent( QModelIndex() );
247 Q_ASSERT( !m_proxy->persistent().isEmpty() );
248 }
249
checkPersistent()250 void checkPersistent()
251 {
252 for ( int row = 0; row < m_persistentProxyIndexes.size(); ++row )
253 {
254 QModelIndex updatedProxy = m_persistentProxyIndexes.at( row );
255 QModelIndex updatedSource = m_persistentSourceIndexes.at( row );
256 }
257 for ( int row = 0; row < m_persistentProxyIndexes.size(); ++row )
258 {
259 QModelIndex updatedProxy = m_persistentProxyIndexes.at( row );
260 QModelIndex updatedSource = m_persistentSourceIndexes.at( row );
261 QCOMPARE( m_proxy->mapToSource( updatedProxy ), updatedSource );
262 }
263 m_persistentSourceIndexes.clear();
264 m_persistentProxyIndexes.clear();
265 }
266
267 private:
268 AccessibleProxyModel *m_proxy = nullptr;
269 QList<QPersistentModelIndex> m_persistentSourceIndexes;
270 QList<QPersistentModelIndex> m_persistentProxyIndexes;
271 };
272
moveSourceItems()273 void tst_ModelTest::moveSourceItems()
274 {
275 DynamicTreeModel *model = new DynamicTreeModel( this );
276 AccessibleProxyModel *proxy = new AccessibleProxyModel( this );
277 proxy->setSourceModel( model );
278
279 ModelInsertCommand *insertCommand = new ModelInsertCommand( model, this );
280 insertCommand->setStartRow( 0 );
281 insertCommand->setEndRow( 2 );
282 insertCommand->doCommand();
283
284 insertCommand = new ModelInsertCommand( model, this );
285 insertCommand->setAncestorRowNumbers( QList<int>() << 1 );
286 insertCommand->setStartRow( 0 );
287 insertCommand->setEndRow( 2 );
288 insertCommand->doCommand();
289
290 ObservingObject observer( proxy );
291
292 ModelMoveCommand *moveCommand = new ModelMoveCommand( model, this );
293 moveCommand->setStartRow( 0 );
294 moveCommand->setEndRow( 0 );
295 moveCommand->setDestAncestors( QList<int>() << 1 );
296 moveCommand->setDestRow( 0 );
297 moveCommand->doCommand();
298 }
299
testResetThroughProxy()300 void tst_ModelTest::testResetThroughProxy()
301 {
302 DynamicTreeModel *model = new DynamicTreeModel( this );
303
304 ModelInsertCommand *insertCommand = new ModelInsertCommand( model, this );
305 insertCommand->setStartRow( 0 );
306 insertCommand->setEndRow( 2 );
307 insertCommand->doCommand();
308
309 QPersistentModelIndex persistent = model->index( 0, 0 );
310
311 AccessibleProxyModel *proxy = new AccessibleProxyModel( this );
312 proxy->setSourceModel( model );
313
314 ObservingObject observer( proxy );
315 observer.storePersistent();
316
317 ModelResetCommand *resetCommand = new ModelResetCommand( model, this );
318 resetCommand->setNumCols( 0 );
319 resetCommand->doCommand();
320 }
321
322
323 QGSTEST_MAIN( tst_ModelTest )
324 #include "tst_modeltest.moc"
325