360 |
361 |
361 SubQAbstractProxyModel model; |
362 SubQAbstractProxyModel model; |
362 QCOMPARE(model.submit(), submit); |
363 QCOMPARE(model.submit(), submit); |
363 } |
364 } |
364 |
365 |
|
366 class StandardItemModelWithCustomRoleNames : public QStandardItemModel |
|
367 { |
|
368 public: |
|
369 enum CustomRole { |
|
370 CustomRole1 = Qt::UserRole, |
|
371 CustomRole2 |
|
372 }; |
|
373 |
|
374 StandardItemModelWithCustomRoleNames() { |
|
375 QHash<int, QByteArray> _roleNames = roleNames(); |
|
376 _roleNames.insert(CustomRole1, "custom1"); |
|
377 _roleNames.insert(CustomRole2, "custom2"); |
|
378 setRoleNames(_roleNames); |
|
379 } |
|
380 }; |
|
381 |
|
382 class AnotherStandardItemModelWithCustomRoleNames : public QStandardItemModel |
|
383 { |
|
384 public: |
|
385 enum CustomRole { |
|
386 AnotherCustomRole1 = Qt::UserRole + 10, // Different to StandardItemModelWithCustomRoleNames::CustomRole1 |
|
387 AnotherCustomRole2 |
|
388 }; |
|
389 |
|
390 AnotherStandardItemModelWithCustomRoleNames() { |
|
391 QHash<int, QByteArray> _roleNames = roleNames(); |
|
392 _roleNames.insert(AnotherCustomRole1, "another_custom1"); |
|
393 _roleNames.insert(AnotherCustomRole2, "another_custom2"); |
|
394 setRoleNames(_roleNames); |
|
395 } |
|
396 }; |
|
397 |
|
398 /** |
|
399 Verifies that @p subSet is a subset of @p superSet. That is, all keys in @p subSet exist in @p superSet and have the same values. |
|
400 */ |
|
401 static void verifySubSetOf(const QHash<int, QByteArray> &superSet, const QHash<int, QByteArray> &subSet) |
|
402 { |
|
403 QHash<int, QByteArray>::const_iterator it = subSet.constBegin(); |
|
404 const QHash<int, QByteArray>::const_iterator end = subSet.constEnd(); |
|
405 for ( ; it != end; ++it ) { |
|
406 QVERIFY(superSet.contains(it.key())); |
|
407 QVERIFY(it.value() == superSet.value(it.key())); |
|
408 } |
|
409 } |
|
410 |
|
411 void tst_QAbstractProxyModel::testRoleNames() |
|
412 { |
|
413 QStandardItemModel defaultModel; |
|
414 StandardItemModelWithCustomRoleNames model; |
|
415 QHash<int, QByteArray> rootModelRoleNames = model.roleNames(); |
|
416 QHash<int, QByteArray> defaultModelRoleNames = defaultModel.roleNames(); |
|
417 |
|
418 verifySubSetOf( rootModelRoleNames, defaultModelRoleNames); |
|
419 QVERIFY( rootModelRoleNames.size() == defaultModelRoleNames.size() + 2 ); |
|
420 QVERIFY( rootModelRoleNames.contains(StandardItemModelWithCustomRoleNames::CustomRole1)); |
|
421 QVERIFY( rootModelRoleNames.contains(StandardItemModelWithCustomRoleNames::CustomRole2)); |
|
422 QVERIFY( rootModelRoleNames.value(StandardItemModelWithCustomRoleNames::CustomRole1) == "custom1" ); |
|
423 QVERIFY( rootModelRoleNames.value(StandardItemModelWithCustomRoleNames::CustomRole2) == "custom2" ); |
|
424 |
|
425 SubQAbstractProxyModel proxy1; |
|
426 proxy1.setSourceModel(&model); |
|
427 QHash<int, QByteArray> proxy1RoleNames = proxy1.roleNames(); |
|
428 verifySubSetOf( proxy1RoleNames, defaultModelRoleNames ); |
|
429 QVERIFY( proxy1RoleNames.size() == defaultModelRoleNames.size() + 2 ); |
|
430 QVERIFY( proxy1RoleNames.contains(StandardItemModelWithCustomRoleNames::CustomRole1)); |
|
431 QVERIFY( proxy1RoleNames.contains(StandardItemModelWithCustomRoleNames::CustomRole2)); |
|
432 QVERIFY( proxy1RoleNames.value(StandardItemModelWithCustomRoleNames::CustomRole1) == "custom1" ); |
|
433 QVERIFY( proxy1RoleNames.value(StandardItemModelWithCustomRoleNames::CustomRole2) == "custom2" ); |
|
434 |
|
435 SubQAbstractProxyModel proxy2; |
|
436 proxy2.setSourceModel(&proxy1); |
|
437 QHash<int, QByteArray> proxy2RoleNames = proxy2.roleNames(); |
|
438 verifySubSetOf( proxy2RoleNames, defaultModelRoleNames ); |
|
439 QVERIFY( proxy2RoleNames.size() == defaultModelRoleNames.size() + 2 ); |
|
440 QVERIFY( proxy2RoleNames.contains(StandardItemModelWithCustomRoleNames::CustomRole1)); |
|
441 QVERIFY( proxy2RoleNames.contains(StandardItemModelWithCustomRoleNames::CustomRole2)); |
|
442 QVERIFY( proxy2RoleNames.value(StandardItemModelWithCustomRoleNames::CustomRole1) == "custom1" ); |
|
443 QVERIFY( proxy2RoleNames.value(StandardItemModelWithCustomRoleNames::CustomRole2) == "custom2" ); |
|
444 |
|
445 } |
|
446 |
365 QTEST_MAIN(tst_QAbstractProxyModel) |
447 QTEST_MAIN(tst_QAbstractProxyModel) |
366 #include "tst_qabstractproxymodel.moc" |
448 #include "tst_qabstractproxymodel.moc" |
367 |
449 |