|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2009 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 ** $QT_BEGIN_LICENSE:LGPL$ |
|
10 ** No Commercial Usage |
|
11 ** This file contains pre-release code and may not be distributed. |
|
12 ** You may use this file in accordance with the terms and conditions |
|
13 ** contained in the Technology Preview License Agreement accompanying |
|
14 ** this package. |
|
15 ** |
|
16 ** GNU Lesser General Public License Usage |
|
17 ** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 ** General Public License version 2.1 as published by the Free Software |
|
19 ** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 ** packaging of this file. Please review the following information to |
|
21 ** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 ** |
|
24 ** In addition, as a special exception, Nokia gives you certain additional |
|
25 ** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 ** |
|
28 ** If you have questions regarding the use of this file, please contact |
|
29 ** Nokia at qt-info@nokia.com. |
|
30 ** |
|
31 ** |
|
32 ** |
|
33 ** |
|
34 ** |
|
35 ** |
|
36 ** |
|
37 ** |
|
38 ** $QT_END_LICENSE$ |
|
39 ** |
|
40 ****************************************************************************/ |
|
41 |
|
42 |
|
43 #include <QtTest/QtTest> |
|
44 #include <qabstractproxymodel.h> |
|
45 #include <QItemSelection> |
|
46 #include <qstandarditemmodel.h> |
|
47 |
|
48 class tst_QAbstractProxyModel : public QObject |
|
49 { |
|
50 Q_OBJECT |
|
51 |
|
52 public slots: |
|
53 void initTestCase(); |
|
54 void cleanupTestCase(); |
|
55 void init(); |
|
56 void cleanup(); |
|
57 |
|
58 private slots: |
|
59 void qabstractproxymodel_data(); |
|
60 void qabstractproxymodel(); |
|
61 void data_data(); |
|
62 void data(); |
|
63 void flags_data(); |
|
64 void flags(); |
|
65 void headerData_data(); |
|
66 void headerData(); |
|
67 void itemData_data(); |
|
68 void itemData(); |
|
69 void mapFromSource_data(); |
|
70 void mapFromSource(); |
|
71 void mapSelectionFromSource_data(); |
|
72 void mapSelectionFromSource(); |
|
73 void mapSelectionToSource_data(); |
|
74 void mapSelectionToSource(); |
|
75 void mapToSource_data(); |
|
76 void mapToSource(); |
|
77 void revert_data(); |
|
78 void revert(); |
|
79 void setSourceModel_data(); |
|
80 void setSourceModel(); |
|
81 void submit_data(); |
|
82 void submit(); |
|
83 }; |
|
84 |
|
85 // Subclass that exposes the protected functions. |
|
86 class SubQAbstractProxyModel : public QAbstractProxyModel |
|
87 { |
|
88 public: |
|
89 // QAbstractProxyModel::mapFromSource is a pure virtual function. |
|
90 QModelIndex mapFromSource(QModelIndex const& sourceIndex) const |
|
91 { Q_UNUSED(sourceIndex); return QModelIndex(); } |
|
92 |
|
93 // QAbstractProxyModel::mapToSource is a pure virtual function. |
|
94 QModelIndex mapToSource(QModelIndex const& proxyIndex) const |
|
95 { Q_UNUSED(proxyIndex); return QModelIndex(); } |
|
96 |
|
97 QModelIndex index(int, int, const QModelIndex&) const |
|
98 { |
|
99 return QModelIndex(); |
|
100 } |
|
101 |
|
102 QModelIndex parent(const QModelIndex&) const |
|
103 { |
|
104 return QModelIndex(); |
|
105 } |
|
106 |
|
107 int rowCount(const QModelIndex&) const |
|
108 { |
|
109 return 0; |
|
110 } |
|
111 |
|
112 int columnCount(const QModelIndex&) const |
|
113 { |
|
114 return 0; |
|
115 } |
|
116 }; |
|
117 |
|
118 // This will be called before the first test function is executed. |
|
119 // It is only called once. |
|
120 void tst_QAbstractProxyModel::initTestCase() |
|
121 { |
|
122 } |
|
123 |
|
124 // This will be called after the last test function is executed. |
|
125 // It is only called once. |
|
126 void tst_QAbstractProxyModel::cleanupTestCase() |
|
127 { |
|
128 } |
|
129 |
|
130 // This will be called before each test function is executed. |
|
131 void tst_QAbstractProxyModel::init() |
|
132 { |
|
133 } |
|
134 |
|
135 // This will be called after every test function. |
|
136 void tst_QAbstractProxyModel::cleanup() |
|
137 { |
|
138 } |
|
139 |
|
140 void tst_QAbstractProxyModel::qabstractproxymodel_data() |
|
141 { |
|
142 } |
|
143 |
|
144 void tst_QAbstractProxyModel::qabstractproxymodel() |
|
145 { |
|
146 SubQAbstractProxyModel model; |
|
147 model.data(QModelIndex()); |
|
148 model.flags(QModelIndex()); |
|
149 model.headerData(0, Qt::Vertical, 0); |
|
150 model.itemData(QModelIndex()); |
|
151 model.mapFromSource(QModelIndex()); |
|
152 model.mapSelectionFromSource(QItemSelection()); |
|
153 model.mapSelectionToSource(QItemSelection()); |
|
154 model.mapToSource(QModelIndex()); |
|
155 model.revert(); |
|
156 model.setSourceModel(0); |
|
157 QCOMPARE(model.sourceModel(), (QAbstractItemModel*)0); |
|
158 model.submit(); |
|
159 } |
|
160 |
|
161 Q_DECLARE_METATYPE(QVariant) |
|
162 Q_DECLARE_METATYPE(QModelIndex) |
|
163 void tst_QAbstractProxyModel::data_data() |
|
164 { |
|
165 QTest::addColumn<QModelIndex>("proxyIndex"); |
|
166 QTest::addColumn<int>("role"); |
|
167 QTest::addColumn<QVariant>("data"); |
|
168 QTest::newRow("null") << QModelIndex() << 0 << QVariant(); |
|
169 } |
|
170 |
|
171 // public QVariant data(QModelIndex const& proxyIndex, int role = Qt::DisplayRole) const |
|
172 void tst_QAbstractProxyModel::data() |
|
173 { |
|
174 QFETCH(QModelIndex, proxyIndex); |
|
175 QFETCH(int, role); |
|
176 QFETCH(QVariant, data); |
|
177 |
|
178 SubQAbstractProxyModel model; |
|
179 QCOMPARE(model.data(proxyIndex, role), data); |
|
180 } |
|
181 |
|
182 Q_DECLARE_METATYPE(Qt::ItemFlags) |
|
183 void tst_QAbstractProxyModel::flags_data() |
|
184 { |
|
185 QTest::addColumn<QModelIndex>("index"); |
|
186 QTest::addColumn<Qt::ItemFlags>("flags"); |
|
187 QTest::newRow("null") << QModelIndex() << (Qt::ItemFlags)0; |
|
188 } |
|
189 |
|
190 // public Qt::ItemFlags flags(QModelIndex const& index) const |
|
191 void tst_QAbstractProxyModel::flags() |
|
192 { |
|
193 QFETCH(QModelIndex, index); |
|
194 QFETCH(Qt::ItemFlags, flags); |
|
195 |
|
196 SubQAbstractProxyModel model; |
|
197 QCOMPARE(model.flags(index), flags); |
|
198 } |
|
199 |
|
200 Q_DECLARE_METATYPE(Qt::Orientation) |
|
201 Q_DECLARE_METATYPE(Qt::ItemDataRole) |
|
202 void tst_QAbstractProxyModel::headerData_data() |
|
203 { |
|
204 QTest::addColumn<int>("section"); |
|
205 QTest::addColumn<Qt::Orientation>("orientation"); |
|
206 QTest::addColumn<Qt::ItemDataRole>("role"); |
|
207 QTest::addColumn<QVariant>("headerData"); |
|
208 QTest::newRow("null") << 0 << Qt::Vertical << Qt::UserRole << QVariant(); |
|
209 } |
|
210 |
|
211 // public QVariant headerData(int section, Qt::Orientation orientation, int role) const |
|
212 void tst_QAbstractProxyModel::headerData() |
|
213 { |
|
214 QFETCH(int, section); |
|
215 QFETCH(Qt::Orientation, orientation); |
|
216 QFETCH(Qt::ItemDataRole, role); |
|
217 QFETCH(QVariant, headerData); |
|
218 |
|
219 SubQAbstractProxyModel model; |
|
220 QCOMPARE(model.headerData(section, orientation, role), headerData); |
|
221 } |
|
222 |
|
223 void tst_QAbstractProxyModel::itemData_data() |
|
224 { |
|
225 QTest::addColumn<QModelIndex>("index"); |
|
226 QTest::addColumn<int>("count"); |
|
227 |
|
228 QTest::newRow("null") << QModelIndex() << 0; |
|
229 } |
|
230 |
|
231 // public QMap<int,QVariant> itemData(QModelIndex const& index) const |
|
232 void tst_QAbstractProxyModel::itemData() |
|
233 { |
|
234 QFETCH(QModelIndex, index); |
|
235 QFETCH(int, count); |
|
236 SubQAbstractProxyModel model; |
|
237 QCOMPARE(model.itemData(index).count(), count); |
|
238 } |
|
239 |
|
240 void tst_QAbstractProxyModel::mapFromSource_data() |
|
241 { |
|
242 QTest::addColumn<QModelIndex>("sourceIndex"); |
|
243 QTest::addColumn<QModelIndex>("mapFromSource"); |
|
244 QTest::newRow("null") << QModelIndex() << QModelIndex(); |
|
245 } |
|
246 |
|
247 // public QModelIndex mapFromSource(QModelIndex const& sourceIndex) const |
|
248 void tst_QAbstractProxyModel::mapFromSource() |
|
249 { |
|
250 QFETCH(QModelIndex, sourceIndex); |
|
251 QFETCH(QModelIndex, mapFromSource); |
|
252 |
|
253 SubQAbstractProxyModel model; |
|
254 QCOMPARE(model.mapFromSource(sourceIndex), mapFromSource); |
|
255 } |
|
256 |
|
257 Q_DECLARE_METATYPE(QItemSelection) |
|
258 void tst_QAbstractProxyModel::mapSelectionFromSource_data() |
|
259 { |
|
260 QTest::addColumn<QItemSelection>("selection"); |
|
261 QTest::addColumn<QItemSelection>("mapSelectionFromSource"); |
|
262 QTest::newRow("null") << QItemSelection() << QItemSelection(); |
|
263 QTest::newRow("empty") << QItemSelection(QModelIndex(), QModelIndex()) << QItemSelection(QModelIndex(), QModelIndex()); |
|
264 } |
|
265 |
|
266 // public QItemSelection mapSelectionFromSource(QItemSelection const& selection) const |
|
267 void tst_QAbstractProxyModel::mapSelectionFromSource() |
|
268 { |
|
269 QFETCH(QItemSelection, selection); |
|
270 QFETCH(QItemSelection, mapSelectionFromSource); |
|
271 |
|
272 SubQAbstractProxyModel model; |
|
273 QCOMPARE(model.mapSelectionFromSource(selection), mapSelectionFromSource); |
|
274 } |
|
275 |
|
276 void tst_QAbstractProxyModel::mapSelectionToSource_data() |
|
277 { |
|
278 QTest::addColumn<QItemSelection>("selection"); |
|
279 QTest::addColumn<QItemSelection>("mapSelectionToSource"); |
|
280 QTest::newRow("null") << QItemSelection() << QItemSelection(); |
|
281 QTest::newRow("empty") << QItemSelection(QModelIndex(), QModelIndex()) << QItemSelection(QModelIndex(), QModelIndex()); |
|
282 } |
|
283 |
|
284 // public QItemSelection mapSelectionToSource(QItemSelection const& selection) const |
|
285 void tst_QAbstractProxyModel::mapSelectionToSource() |
|
286 { |
|
287 QFETCH(QItemSelection, selection); |
|
288 QFETCH(QItemSelection, mapSelectionToSource); |
|
289 |
|
290 SubQAbstractProxyModel model; |
|
291 QCOMPARE(model.mapSelectionToSource(selection), mapSelectionToSource); |
|
292 } |
|
293 |
|
294 void tst_QAbstractProxyModel::mapToSource_data() |
|
295 { |
|
296 QTest::addColumn<QModelIndex>("proxyIndex"); |
|
297 QTest::addColumn<QModelIndex>("mapToSource"); |
|
298 QTest::newRow("null") << QModelIndex() << QModelIndex(); |
|
299 } |
|
300 |
|
301 // public QModelIndex mapToSource(QModelIndex const& proxyIndex) const |
|
302 void tst_QAbstractProxyModel::mapToSource() |
|
303 { |
|
304 QFETCH(QModelIndex, proxyIndex); |
|
305 QFETCH(QModelIndex, mapToSource); |
|
306 |
|
307 SubQAbstractProxyModel model; |
|
308 QCOMPARE(model.mapToSource(proxyIndex), mapToSource); |
|
309 } |
|
310 |
|
311 void tst_QAbstractProxyModel::revert_data() |
|
312 { |
|
313 //QTest::addColumn<int>("foo"); |
|
314 //QTest::newRow("null") << 0; |
|
315 } |
|
316 |
|
317 // public void revert() |
|
318 void tst_QAbstractProxyModel::revert() |
|
319 { |
|
320 //QFETCH(int, foo); |
|
321 |
|
322 SubQAbstractProxyModel model; |
|
323 model.revert(); |
|
324 } |
|
325 |
|
326 void tst_QAbstractProxyModel::setSourceModel_data() |
|
327 { |
|
328 //QTest::addColumn<int>("sourceModelCount"); |
|
329 //QTest::newRow("null") << 0; |
|
330 } |
|
331 |
|
332 // public void setSourceModel(QAbstractItemModel* sourceModel) |
|
333 void tst_QAbstractProxyModel::setSourceModel() |
|
334 { |
|
335 //QFETCH(int, sourceModelCount); |
|
336 |
|
337 SubQAbstractProxyModel model; |
|
338 QStandardItemModel *sourceModel = new QStandardItemModel(&model); |
|
339 model.setSourceModel(sourceModel); |
|
340 QCOMPARE(model.sourceModel(), static_cast<QAbstractItemModel*>(sourceModel)); |
|
341 |
|
342 QStandardItemModel *sourceModel2 = new QStandardItemModel(&model); |
|
343 model.setSourceModel(sourceModel2); |
|
344 QCOMPARE(model.sourceModel(), static_cast<QAbstractItemModel*>(sourceModel2)); |
|
345 |
|
346 delete sourceModel2; |
|
347 QCOMPARE(model.sourceModel(), static_cast<QAbstractItemModel*>(0)); |
|
348 } |
|
349 |
|
350 void tst_QAbstractProxyModel::submit_data() |
|
351 { |
|
352 QTest::addColumn<bool>("submit"); |
|
353 QTest::newRow("null") << true; |
|
354 } |
|
355 |
|
356 // public bool submit() |
|
357 void tst_QAbstractProxyModel::submit() |
|
358 { |
|
359 QFETCH(bool, submit); |
|
360 |
|
361 SubQAbstractProxyModel model; |
|
362 QCOMPARE(model.submit(), submit); |
|
363 } |
|
364 |
|
365 QTEST_MAIN(tst_QAbstractProxyModel) |
|
366 #include "tst_qabstractproxymodel.moc" |
|
367 |