| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 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 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 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 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 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 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
self.model.setData(QtCore.QModelIndex(), 'a string', -1) |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 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 |
|
| 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 |
|
| 157 |
rows = self.model.rowCount(secondlvl) |
| 158 |
assert(rows >= 0) |
| 159 |
if rows > 0: |
| 160 |
assert(self.model.hasChildren(secondlvl) is True) |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
def columnCount(self): |
| 166 |
""" |
| 167 |
Tests self.model's implementation of QtCore.QAbstractItemModel::columnCount() and hasChildren() |
| 168 |
""" |
| 169 |
|
| 170 |
topidx = self.model.index(0,0,QtCore.QModelIndex()) |
| 171 |
assert(self.model.columnCount(topidx) >= 0) |
| 172 |
|
| 173 |
|
| 174 |
childidx = self.model.index(0,0,topidx) |
| 175 |
if childidx.isValid() : |
| 176 |
assert(self.model.columnCount(childidx) >= 0) |
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
def hasIndex(self): |
| 182 |
""" |
| 183 |
Tests self.model's implementation of QtCore.QAbstractItemModel::hasIndex() |
| 184 |
""" |
| 185 |
|
| 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 |
|
| 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 |
|
| 201 |
|
| 202 |
|
| 203 |
def index(self): |
| 204 |
""" |
| 205 |
Tests self.model's implementation of QtCore.QAbstractItemModel::index() |
| 206 |
""" |
| 207 |
|
| 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 |
|
| 219 |
assert(self.model.index(rows,cols, QtCore.QModelIndex()) == QtCore.QModelIndex()) |
| 220 |
assert(self.model.index(0,0, QtCore.QModelIndex()).isValid() == True) |
| 221 |
|
| 222 |
|
| 223 |
a = self.model.index(0,0, QtCore.QModelIndex()) |
| 224 |
b = self.model.index(0,0, QtCore.QModelIndex()) |
| 225 |
assert(a==b) |
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
def parent(self): |
| 231 |
""" |
| 232 |
Tests self.model's implementation of QtCore.QAbstractItemModel::parent() |
| 233 |
""" |
| 234 |
|
| 235 |
|
| 236 |
assert(self.model.parent(QtCore.QModelIndex()) == QtCore.QModelIndex()) |
| 237 |
|
| 238 |
if self.model.rowCount(QtCore.QModelIndex()) == 0: |
| 239 |
return; |
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
topidx = self.model.index(0,0,QtCore.QModelIndex()) |
| 249 |
assert(self.model.parent(topidx) == QtCore.QModelIndex()) |
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
if self.model.rowCount(topidx) > 0 : |
| 254 |
childidx = self.model.index(0,0,topidx) |
| 255 |
assert(self.model.parent(childidx) == topidx) |
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 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 |
|
| 267 |
|
| 268 |
self.checkChildren(QtCore.QModelIndex()) |
| 269 |
|
| 270 |
def data(self): |
| 271 |
""" |
| 272 |
Tests self.model's implementation of QtCore.QAbstractItemModel::data() |
| 273 |
""" |
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 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 |
|
| 284 |
assert(self.model.index(0,0, QtCore.QModelIndex()) is not None) |
| 285 |
|
| 286 |
|
| 287 |
assert(self.model.setData(QtCore.QModelIndex(), "foo", QtCore.Qt.DisplayRole) is False) |
| 288 |
|
| 289 |
|
| 290 |
variant = self.model.data(self.model.index(0,0,QtCore.QModelIndex()), QtCore.Qt.ToolTipRole) |
| 291 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 312 |
variant = self.model.data(self.model.index(0,0,QtCore.QModelIndex()), QtCore.Qt.TextAlignmentRole) |
| 313 |
if variant: |
| 314 |
|
| 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 |
|
| 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 |
|
| 330 |
variant = self.model.data(self.model.index(0,0,QtCore.QModelIndex()), QtCore.Qt.CheckStateRole) |
| 331 |
if variant: |
| 332 |
|
| 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 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 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 |
|
| 414 |
p = parent; |
| 415 |
while p.isValid(): |
| 416 |
p = p.parent() |
| 417 |
|
| 418 |
|
| 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 |
|
| 431 |
assert( rows >= 0 ) |
| 432 |
assert( cols >= 0 ) |
| 433 |
|
| 434 |
if rows > 0: |
| 435 |
assert(self.model.hasChildren(parent) == True) |
| 436 |
|
| 437 |
|
| 438 |
|
| 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 |
|
| 451 |
assert(index.isValid() == True) |
| 452 |
|
| 453 |
|
| 454 |
modIdx = self.model.index(r,c,parent) |
| 455 |
assert(index == modIdx) |
| 456 |
|
| 457 |
|
| 458 |
a = self.model.index(r,c,parent) |
| 459 |
b = self.model.index(r,c,parent) |
| 460 |
assert( a == b ) |
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
assert( index.row() == r ) |
| 467 |
assert( index.column() == c ) |
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
assert( self.model.data(index, QtCore.Qt.DisplayRole) is not None) |
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
p = self.model.parent( index ) |
| 488 |
assert( p.internalId() == parent.internalId() ) |
| 489 |
assert( p.row() == parent.row() ) |
| 490 |
|
| 491 |
|
| 492 |
if self.model.hasChildren(index) and depth < 10: |
| 493 |
|
| 494 |
self.checkChildren(index, ++depth) |
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
newIdx = self.model.index(r,c,parent) |
| 501 |
assert(index == newIdx) |