Debian Codegraph @

lib/tests/modeltest.py

raw symbols SCIP
1 #############################################################################
2 ##
3 ## Copyright (C) 2007 Trolltech ASA. All rights reserved.
4 ##
5 ## This file is part of the Qt Concurrent project on Trolltech Labs.
6 ##
7 ## This file may be used under the terms of the GNU General Public
8 ## License version 2.0 as published by the Free Software Foundation
9 ## and appearing in the file LICENSE.GPL included in the packaging of
10 ## this file. Please review the following information to ensure GNU
11 ## General Public Licensing requirements will be met:
12 ## http://www.trolltech.com/products/qt/opensource.html
13 ##
14 ## If you are unsure which license is appropriate for your use, please
15 ## review the following information:
16 ## http://www.trolltech.com/products/qt/licensing.html or contact the
17 ## sales department at [email protected].
18 ##
19 ## This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
20 ## WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
21 ##
22 #############################################################################
23
24
25 from PyQt5 import sip
26 from PyQt5 import QtCore
27
28
29 class ModelTest(QtCore.QObject):
30 def __init__(self, _model, parent):
31 """
32 Connect to all of the models signals, Whenever anything happens recheck everything.
33 """
34 QtCore.QObject.__init__(self,parent)
35 self._model = _model
36 # QAbstractItemModel gives us a simple table of rows and columns. Each item in it (cell)
37 # has a unique index via QtCore.QModelIndex with row, colum
38 # for example:
39 # 0 1 2
40 # 0 . , ,
41 # 1 . . X <-- this X is at row:1, column:2
42 # 2 . . .
43 #
44 # Any data associated with X can be retrieved via the data() function
45 # passing it the 'role' that the data plays. The data is set via setData().
46 #
47 self.model = sip.cast(_model, QtCore.QAbstractItemModel)
48 self.insert = []
49 self.remove = []
50 self.fetchingMore = False
51 assert self.model
52
53 self.model.columnsAboutToBeInserted[QtCore.QModelIndex, int, int].connect(self.runAllTests)
54 self.model.columnsAboutToBeRemoved[QtCore.QModelIndex, int, int].connect(self.runAllTests)
55 self.model.columnsInserted[QtCore.QModelIndex, int, int].connect(self.runAllTests)
56 self.model.columnsRemoved[QtCore.QModelIndex, int, int].connect(self.runAllTests)
57
58 # self.model.dataChanged[QtCore.QModelIndex, QtCore.QModelIndex].connect(self.runAllTests)
59
60 self.model.headerDataChanged[QtCore.Qt.Orientation, int, int].connect(self.runAllTests)
61 self.model.layoutAboutToBeChanged.connect(self.runAllTests)
62 self.model.layoutChanged.connect(self.runAllTests)
63 self.model.modelReset.connect(self.runAllTests)
64 self.model.rowsAboutToBeInserted[QtCore.QModelIndex, int, int].connect(self.runAllTests)
65 self.model.rowsAboutToBeRemoved[QtCore.QModelIndex, int, int].connect(self.runAllTests)
66 self.model.rowsInserted[QtCore.QModelIndex, int, int].connect(self.runAllTests)
67 self.model.rowsRemoved[QtCore.QModelIndex, int, int].connect(self.runAllTests)
68
69 # Special checks for inserting/removing
70 self.model.rowsAboutToBeInserted[QtCore.QModelIndex, int, int].connect(self.rowsAboutToBeInserted)
71 self.model.rowsAboutToBeRemoved[QtCore.QModelIndex, int, int].connect(self.rowsAboutToBeRemoved)
72 self.model.rowsInserted[QtCore.QModelIndex, int, int].connect(self.rowsInserted)
73 self.model.rowsRemoved[QtCore.QModelIndex, int, int].connect(self.rowsRemoved)
74 self.runAllTests()
75
76 def nonDestructiveBasicTest(self):
77 """
78 nonDestructiveBasicTest tries to call a number of the basic functions (not all)
79 to make sure the model doesn't outright segfault, testing the functions that makes sense.
80 """
81 assert(self.model.buddy(QtCore.QModelIndex()) == QtCore.QModelIndex())
82 self.model.canFetchMore(QtCore.QModelIndex())
83 assert(self.model.columnCount(QtCore.QModelIndex()) >= 0)
84 # Try to fetch something from nothing - this:
85 #
86 # self.model.data(QtCore.QModelIndex(), QtCore.Qt.DisplayRole)
87 #
88 # reads as:
89 #
90 # from the (.data) table at (an invalid QtCore.QModelIndex) try to get a DisplayRole
91 #
92 # A bare QtCore.QModelIndex() creates a new empty index, and will be invalid: asking for the
93 # displayRole will bring back (or used to bring back) an invalid QVariant rather than
94 # raising an exception. With QVariant(2) set via sip, we don't get QVariants at all
95 # but a NoneType.
96 # Was: assert(self.model.data(QtCore.QModelIndex(), QtCore.Qt.DisplayRole) == QtCore.QVariant())
97 assert(self.model.data(QtCore.QModelIndex(), QtCore.Qt.DisplayRole) is None)
98 self.fetchingMore = True
99 self.model.fetchMore(QtCore.QModelIndex())
100 self.fetchingMore = False
101 flags = self.model.flags(QtCore.QModelIndex())
102 assert(int(flags & QtCore.Qt.ItemIsEnabled) == QtCore.Qt.ItemIsEnabled or int(flags & QtCore.Qt.ItemIsEnabled) == 0)
103 self.model.hasChildren(QtCore.QModelIndex())
104 self.model.hasIndex(0,0)
105 self.model.headerData(0,QtCore.Qt.Horizontal, QtCore.Qt.DisplayRole)
106 self.model.index(0,0, QtCore.QModelIndex())
107 self.model.itemData(QtCore.QModelIndex())
108 # Match returns indices for items were the data role (e.g. DisplayRole) matches the value (QVariant)
109 #
110 # so, this:
111 # cache = QtCore.QVariant()
112 # self.model.match(QtCore.QModelIndex(), -1, cache)
113 # reads as:
114 # Try to get things from the table that match the (non-existent) -1 'role'
115 # where the data stored there matches Qvariant
116 # We haven't got QVariants any more, so just pass anything
117 self.model.match(QtCore.QModelIndex(), -1, 'a string')
118 self.model.mimeTypes()
119 assert(self.model.parent(QtCore.QModelIndex()) == QtCore.QModelIndex())
120 assert(self.model.rowCount(QtCore.QModelIndex()) >= 0)
121 # setData(index, value, role) sets the (role) data at the index to value.
122 # so, this:
123 # variant = QtCore.QVariant()
124 # self.model.setData(QtCore.QModelIndex(), variant, -1)
125 # reads as:
126 # Put an empty QVariant() at an invalid index as an invalid role
127 self.model.setData(QtCore.QModelIndex(), 'a string', -1)
128 # Sets the data a dummy horizontal section (-1) to an empty QVariant...
129 # self.model.setHeaderData(-1, QtCore.Qt.Horizontal, QtCore.QVariant())
130 # ...and for section zero
131 # self.model.setHeaderData(0, QtCore.Qt.Horizontal, QtCore.QVariant())
132 # and for section 999999
133 # self.model.setHeaderData(999999, QtCore.Qt.Horizontal, QtCore.QVariant())
134 self.model.setHeaderData(-1, QtCore.Qt.Horizontal, 'a string')
135 self.model.setHeaderData(0, QtCore.Qt.Horizontal, 'a string')
136 self.model.setHeaderData(999999, QtCore.Qt.Horizontal, 'a string')
137 self.model.sibling(0,0,QtCore.QModelIndex())
138 self.model.span(QtCore.QModelIndex())
139 self.model.supportedDropActions()
140
141 def rowCount(self):
142 """
143 Tests self.model's implementation of QtCore.QAbstractItemModel::rowCount() and hasChildren()
144
145 self.models that are dynamically populated are not as fully tested here.
146 """
147 # check top row
148 topindex = self.model.index(0,0,QtCore.QModelIndex())
149 rows = self.model.rowCount(topindex)
150 assert(rows >= 0)
151 if rows > 0:
152 assert(self.model.hasChildren(topindex) is True)
153
154 secondlvl = self.model.index(0,0,topindex)
155 if secondlvl.isValid():
156 # check a row count where parent is valid
157 rows = self.model.rowCount(secondlvl)
158 assert(rows >= 0)
159 if rows > 0:
160 assert(self.model.hasChildren(secondlvl) is True)
161
162 # The self.models rowCount() is tested more extensively in checkChildren,
163 # but this catches the big mistakes
164
165 def columnCount(self):
166 """
167 Tests self.model's implementation of QtCore.QAbstractItemModel::columnCount() and hasChildren()
168 """
169 # check top row
170 topidx = self.model.index(0,0,QtCore.QModelIndex())
171 assert(self.model.columnCount(topidx) >= 0)
172
173 # check a column count where parent is valid
174 childidx = self.model.index(0,0,topidx)
175 if childidx.isValid() :
176 assert(self.model.columnCount(childidx) >= 0)
177
178 # columnCount() is tested more extensively in checkChildren,
179 # but this catches the big mistakes
180
181 def hasIndex(self):
182 """
183 Tests self.model's implementation of QtCore.QAbstractItemModel::hasIndex()
184 """
185 # Make sure that invalid values returns an invalid index
186 assert(self.model.hasIndex(-2,-2) == False)
187 assert(self.model.hasIndex(-2,0) == False)
188 assert(self.model.hasIndex(0,-2) == False)
189
190 rows = self.model.rowCount(QtCore.QModelIndex())
191 cols = self.model.columnCount(QtCore.QModelIndex())
192
193 # check out of bounds
194 assert(self.model.hasIndex(rows,cols) == False)
195 assert(self.model.hasIndex(rows+1,cols+1) == False)
196
197 if rows > 0:
198 assert(self.model.hasIndex(0,0) == True)
199
200 # hasIndex() is tested more extensively in checkChildren()
201 # but this catches the big mistakes
202
203 def index(self):
204 """
205 Tests self.model's implementation of QtCore.QAbstractItemModel::index()
206 """
207 # Make sure that invalid values returns an invalid index
208 assert(self.model.index(-2,-2, QtCore.QModelIndex()) == QtCore.QModelIndex())
209 assert(self.model.index(-2,0, QtCore.QModelIndex()) == QtCore.QModelIndex())
210 assert(self.model.index(0,-2, QtCore.QModelIndex()) == QtCore.QModelIndex())
211
212 rows = self.model.rowCount(QtCore.QModelIndex())
213 cols = self.model.columnCount(QtCore.QModelIndex())
214
215 if rows == 0:
216 return
217
218 # Catch off by one errors
219 assert(self.model.index(rows,cols, QtCore.QModelIndex()) == QtCore.QModelIndex())
220 assert(self.model.index(0,0, QtCore.QModelIndex()).isValid() == True)
221
222 # Make sure that the same index is *always* returned
223 a = self.model.index(0,0, QtCore.QModelIndex())
224 b = self.model.index(0,0, QtCore.QModelIndex())
225 assert(a==b)
226
227 # index() is tested more extensively in checkChildren()
228 # but this catches the big mistakes
229
230 def parent(self):
231 """
232 Tests self.model's implementation of QtCore.QAbstractItemModel::parent()
233 """
234 # Make sure the self.model wont crash and will return an invalid QtCore.QModelIndex
235 # when asked for the parent of an invalid index
236 assert(self.model.parent(QtCore.QModelIndex()) == QtCore.QModelIndex())
237
238 if self.model.rowCount(QtCore.QModelIndex()) == 0:
239 return;
240
241 # Column 0 | Column 1 |
242 # QtCore.Qself.modelIndex() | |
243 # \- topidx | topidx1 |
244 # \- childix | childidx1 |
245
246 # Common error test #1, make sure that a top level index has a parent
247 # that is an invalid QtCore.Qself.modelIndex
248 topidx = self.model.index(0,0,QtCore.QModelIndex())
249 assert(self.model.parent(topidx) == QtCore.QModelIndex())
250
251 # Common error test #2, make sure that a second level index has a parent
252 # that is the first level index
253 if self.model.rowCount(topidx) > 0 :
254 childidx = self.model.index(0,0,topidx)
255 assert(self.model.parent(childidx) == topidx)
256
257 # Common error test #3, the second column should NOT have the same children
258 # as the first column in a row
259 # Usually the second column shouldn't have children
260 topidx1 = self.model.index(0,1,QtCore.QModelIndex())
261 if self.model.rowCount(topidx1) > 0:
262 childidx = self.model.index(0,0,topidx)
263 childidx1 = self.model.index(0,0,topidx1)
264 assert(childidx != childidx1)
265
266 # Full test, walk n levels deep through the self.model making sure that all
267 # parent's children correctly specify their parent
268 self.checkChildren(QtCore.QModelIndex())
269
270 def data(self):
271 """
272 Tests self.model's implementation of QtCore.QAbstractItemModel::data()
273 """
274 # Invalid index should return an invalid qvariant
275 # assert( not self.model.data(QtCore.QModelIndex(), QtCore.Qt.DisplayRole).isValid())
276 #
277 # The above is no longer true: it should return None...
278 assert(self.model.data(QtCore.QModelIndex(), QtCore.Qt.DisplayRole) is None)
279
280 if self.model.rowCount(QtCore.QModelIndex()) == 0:
281 return
282
283 # A valid index should have a valid QtCore.QVariant data
284 assert(self.model.index(0,0, QtCore.QModelIndex()) is not None)
285
286 # shouldn't be able to set data on an invalid index
287 assert(self.model.setData(QtCore.QModelIndex(), "foo", QtCore.Qt.DisplayRole) is False)
288
289 # General Purpose roles that should return a QString
290 variant = self.model.data(self.model.index(0,0,QtCore.QModelIndex()), QtCore.Qt.ToolTipRole)
291 # We no longer use isValid(), so just test it exists
292 if variant:
293 assert( variant.canConvert( QtCore.QVariant.String ) )
294 variant = self.model.data(self.model.index(0,0,QtCore.QModelIndex()), QtCore.Qt.StatusTipRole)
295 if variant:
296 assert( variant.canConvert( QtCore.QVariant.String ) )
297 variant = self.model.data(self.model.index(0,0,QtCore.QModelIndex()), QtCore.Qt.WhatsThisRole)
298 if variant:
299 assert( variant.canConvert( QtCore.QVariant.String ) )
300
301 # General Purpose roles that should return a QSize
302 variant = self.model.data(self.model.index(0,0,QtCore.QModelIndex()), QtCore.Qt.SizeHintRole)
303 if variant:
304 assert( variant.canConvert( QtCore.QVariant.Size ) )
305
306 # General Purpose roles that should return a QFont
307 variant = self.model.data(self.model.index(0,0,QtCore.QModelIndex()), QtCore.Qt.FontRole)
308 if variant:
309 assert( variant.canConvert( QtCore.QVariant.Font ) )
310
311 # Check that the alignment is one we know about
312 variant = self.model.data(self.model.index(0,0,QtCore.QModelIndex()), QtCore.Qt.TextAlignmentRole)
313 if variant:
314 # alignment = variant.toInt()[0]
315 alignment = int(variant)
316 assert( alignment == QtCore.Qt.AlignLeft or
317 alignment == QtCore.Qt.AlignRight or
318 alignment == QtCore.Qt.AlignHCenter or
319 alignment == QtCore.Qt.AlignJustify)
320
321 # General Purpose roles that should return a QColor
322 variant = self.model.data(self.model.index(0,0,QtCore.QModelIndex()), QtCore.Qt.BackgroundColorRole)
323 if variant:
324 assert(variant.canConvert( QtCore.QVariant.Color ))
325 variant = self.model.data(self.model.index(0,0,QtCore.QModelIndex()), QtCore.Qt.TextColorRole)
326 if variant:
327 assert(variant.canConvert( QtCore.QVariant.Color ))
328
329 # Check that the "check state" is one we know about.
330 variant = self.model.data(self.model.index(0,0,QtCore.QModelIndex()), QtCore.Qt.CheckStateRole)
331 if variant:
332 # state = variant.toInt()[0]
333 state = int(variant)
334 assert( state == QtCore.Qt.Unchecked or
335 state == QtCore.Qt.PartiallyChecked or
336 state == QtCore.Qt.Checked )
337
338 def runAllTests(self):
339 if self.fetchingMore:
340 return
341 self.nonDestructiveBasicTest()
342 self.rowCount()
343 self.columnCount()
344 self.hasIndex()
345 self.index()
346 self.parent()
347 self.data()
348
349 def rowsAboutToBeInserted(self, parent, start, end):
350 """
351 Store what is about to be inserted to make sure it actually happens
352 """
353 c = {}
354 c['parent'] = parent
355 c['oldSize'] = self.model.rowCount(parent)
356 c['last'] = self.model.data(model.index(start-1, 0, parent))
357 c['next'] = self.model.data(model.index(start, 0, parent))
358 insert.append(c)
359
360 def rowsInserted(self, parent, start, end):
361 """
362 Confirm that what was said was going to happen actually did
363 """
364 c = insert.pop()
365 assert(c['parent'] == parent)
366 assert(c['oldSize'] + (end - start + 1) == self.model.rowCount(parent))
367 assert(c['last'] == self.model.data(model.index(start-1, 0, c['parent'])))
368
369 # if c['next'] != self.model.data(model.index(end+1, 0, c['parent'])):
370 # qDebug << start << end
371 # for i in range(0, self.model.rowCount(QtCore.QModelIndex())):
372 # qDebug << self.model.index(i, 0).data().toString()
373 # qDebug() << c['next'] << self.model.data(model.index(end+1, 0, c['parent']))
374
375 assert(c['next'] == self.model.data(model.index(end+1, 0, c['parent'])))
376
377 def rowsAboutToBeRemoved(self, parent, start, end):
378 """
379 Store what is about to be inserted to make sure it actually happens
380 """
381 c = {}
382 c['parent'] = parent
383 c['oldSize'] = self.model.rowCount(parent)
384 c['last'] = self.model.data(model.index(start-1, 0, parent))
385 c['next'] = self.model.data(model.index(end+1, 0, parent))
386 remove.append(c)
387
388 def rowsRemoved(self, parent, start, end):
389 """
390 Confirm that what was said was going to happen actually did
391 """
392 c = remove.pop()
393 assert(c['parent'] == parent)
394 assert(c['oldSize'] - (end - start + 1) == self.model.rowCount(parent))
395 assert(c['last'] == self.model.data(model.index(start-1, 0, c['parent'])))
396 assert(c['next'] == self.model.data(model.index(start, 0, c['parent'])))
397
398 def checkChildren(self, parent, depth = 0):
399 """
400 Called from parent() test.
401
402 A self.model that returns an index of parent X should also return X when asking
403 for the parent of the index
404
405 This recursive function does pretty extensive testing on the whole self.model in an
406 effort to catch edge cases.
407
408 This function assumes that rowCount(QtCore.QModelIndex()), columnCount(QtCore.QModelIndex()) and index() already work.
409 If they have a bug it will point it out, but the above tests should have already
410 found the basic bugs because it is easier to figure out the problem in
411 those tests then this one
412 """
413 # First just try walking back up the tree.
414 p = parent;
415 while p.isValid():
416 p = p.parent()
417
418 #For self.models that are dynamically populated
419 if self.model.canFetchMore( parent ):
420 self.fetchingMore = True
421 self.model.fetchMore(parent)
422 self.fetchingMore = False
423
424 rows = self.model.rowCount(parent)
425 cols = self.model.columnCount(parent)
426
427 if rows > 0:
428 assert(self.model.hasChildren(parent))
429
430 # Some further testing against rows(), columns, and hasChildren()
431 assert( rows >= 0 )
432 assert( cols >= 0 )
433
434 if rows > 0:
435 assert(self.model.hasChildren(parent) == True)
436
437 # qDebug() << "parent:" << self.model.data(parent).toString() << "rows:" << rows
438 # << "columns:" << cols << "parent column:" << parent.column()
439
440 assert( self.model.hasIndex( rows+1, 0, parent) == False)
441 for r in range(0,rows):
442 if self.model.canFetchMore(parent):
443 self.fetchingMore = True
444 self.model.fetchMore(parent)
445 self.fetchingMore = False
446 assert(self.model.hasIndex(r,cols+1,parent) == False)
447 for c in range(0,cols):
448 assert(self.model.hasIndex(r,c,parent))
449 index = self.model.index(r,c,parent)
450 # rowCount(QtCore.QModelIndex()) and columnCount(QtCore.QModelIndex()) said that it existed...
451 assert(index.isValid() == True)
452
453 # index() should always return the same index when called twice in a row
454 modIdx = self.model.index(r,c,parent)
455 assert(index == modIdx)
456
457 # Make sure we get the same index if we request it twice in a row
458 a = self.model.index(r,c,parent)
459 b = self.model.index(r,c,parent)
460 assert( a == b )
461
462 # Some basic checking on the index that is returned
463 # assert( index.model() == self.model )
464 # This raises an error that is not part of the qbrz code.
465 # see http://www.opensubscriber.com/message/[email protected]/10335500.html
466 assert( index.row() == r )
467 assert( index.column() == c )
468 # While you can technically return a QtCore.QVariant usually this is a sign
469 # if an bug in data() Disable if this really is ok in your self.model
470 # assert( self.model.data(index, QtCore.Qt.DisplayRole).isValid() == True )
471 #
472 # We no longer check isValid(), just make sure it's not None
473 assert( self.model.data(index, QtCore.Qt.DisplayRole) is not None)
474
475 #if the next test fails here is some somehwat useful debug you play with
476 # if self.model.parent(index) != parent:
477 # qDebug() << r << c << depth << self.model.data(index).toString()
478 # << self.model.data(parent).toString()
479 # qDebug() << index << parent << self.model.parent(index)
480 # # And a view that you can even use to show the self.model
481 # # view = QtGui.QTreeView()
482 # # view.setself.model(model)
483 # # view.show()
484 #
485
486 # Check that we can get back our real parent
487 p = self.model.parent( index )
488 assert( p.internalId() == parent.internalId() )
489 assert( p.row() == parent.row() )
490
491 # recursively go down the children
492 if self.model.hasChildren(index) and depth < 10:
493 # qDebug() << r << c << "hasChildren" << self.model.rowCount(index)
494 self.checkChildren(index, ++depth)
495 #else:
496 # if depth >= 10:
497 # qDebug() << "checked 10 deep"
498
499 # Make sure that after testing the children that the index doesn't change
500 newIdx = self.model.index(r,c,parent)
501 assert(index == newIdx)