TestGroupModel.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  2. * Copyright (C) 2010 Felix Geyer <debfx@fobos.de>
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 2 or (at your option)
  7. * version 3 of the License.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include "TestGroupModel.h"
  18. #include <QSignalSpy>
  19. #include <QTest>
  20. #include "core/Group.h"
  21. #include "crypto/Crypto.h"
  22. #include "gui/group/GroupModel.h"
  23. #include "modeltest.h"
  24. QTEST_GUILESS_MAIN(TestGroupModel)
  25. void TestGroupModel::initTestCase()
  26. {
  27. qRegisterMetaType<QModelIndex>("QModelIndex");
  28. QVERIFY(Crypto::init());
  29. }
  30. void TestGroupModel::test()
  31. {
  32. auto db = new Database();
  33. Group* groupRoot = db->rootGroup();
  34. groupRoot->setObjectName("groupRoot");
  35. groupRoot->setName("groupRoot");
  36. auto group1 = new Group();
  37. group1->setObjectName("group1");
  38. group1->setName("group1");
  39. group1->setParent(groupRoot);
  40. auto group11 = new Group();
  41. group1->setObjectName("group11");
  42. group11->setName("group11");
  43. group11->setParent(group1);
  44. auto group12 = new Group();
  45. group1->setObjectName("group12");
  46. group12->setName("group12");
  47. group12->setParent(group1);
  48. auto group121 = new Group();
  49. group1->setObjectName("group121");
  50. group121->setName("group121");
  51. group121->setParent(group12);
  52. auto model = new GroupModel(db, this);
  53. auto modelTest = new ModelTest(model, this);
  54. QModelIndex indexRoot = model->index(0, 0);
  55. QModelIndex index1 = model->index(0, 0, indexRoot);
  56. QModelIndex index11 = model->index(0, 0, index1);
  57. QPersistentModelIndex index12 = model->index(1, 0, index1);
  58. QModelIndex index121 = model->index(0, 0, index12);
  59. QCOMPARE(model->data(indexRoot).toString(), QString("groupRoot"));
  60. QCOMPARE(model->data(index1).toString(), QString("group1"));
  61. QCOMPARE(model->data(index11).toString(), QString("group11"));
  62. QCOMPARE(model->data(index12).toString(), QString("group12"));
  63. QCOMPARE(model->data(index121).toString(), QString("group121"));
  64. QSignalSpy spy1(model, SIGNAL(dataChanged(QModelIndex, QModelIndex)));
  65. group11->setName("test");
  66. group121->setIcon(4);
  67. QCOMPARE(spy1.count(), 2);
  68. QCOMPARE(model->data(index11).toString(), QString("test"));
  69. QSignalSpy spyAboutToAdd(model, SIGNAL(rowsAboutToBeInserted(QModelIndex, int, int)));
  70. QSignalSpy spyAdded(model, SIGNAL(rowsInserted(QModelIndex, int, int)));
  71. QSignalSpy spyAboutToRemove(model, SIGNAL(rowsAboutToBeRemoved(QModelIndex, int, int)));
  72. QSignalSpy spyRemoved(model, SIGNAL(rowsRemoved(QModelIndex, int, int)));
  73. QSignalSpy spyAboutToMove(model, SIGNAL(rowsAboutToBeMoved(QModelIndex, int, int, QModelIndex, int)));
  74. QSignalSpy spyMoved(model, SIGNAL(rowsMoved(QModelIndex, int, int, QModelIndex, int)));
  75. auto group2 = new Group();
  76. group2->setObjectName("group2");
  77. group2->setName("group2");
  78. group2->setParent(groupRoot);
  79. QModelIndex index2 = model->index(1, 0, indexRoot);
  80. QCOMPARE(spyAboutToAdd.count(), 1);
  81. QCOMPARE(spyAdded.count(), 1);
  82. QCOMPARE(spyAboutToRemove.count(), 0);
  83. QCOMPARE(spyRemoved.count(), 0);
  84. QCOMPARE(spyAboutToMove.count(), 0);
  85. QCOMPARE(spyMoved.count(), 0);
  86. QCOMPARE(model->data(index2).toString(), QString("group2"));
  87. group12->setParent(group1, 0);
  88. QCOMPARE(spyAboutToAdd.count(), 1);
  89. QCOMPARE(spyAdded.count(), 1);
  90. QCOMPARE(spyAboutToRemove.count(), 0);
  91. QCOMPARE(spyRemoved.count(), 0);
  92. QCOMPARE(spyAboutToMove.count(), 1);
  93. QCOMPARE(spyMoved.count(), 1);
  94. QCOMPARE(model->data(index12).toString(), QString("group12"));
  95. group12->setParent(group1, 1);
  96. QCOMPARE(spyAboutToAdd.count(), 1);
  97. QCOMPARE(spyAdded.count(), 1);
  98. QCOMPARE(spyAboutToRemove.count(), 0);
  99. QCOMPARE(spyRemoved.count(), 0);
  100. QCOMPARE(spyAboutToMove.count(), 2);
  101. QCOMPARE(spyMoved.count(), 2);
  102. QCOMPARE(model->data(index12).toString(), QString("group12"));
  103. group12->setParent(group2);
  104. QCOMPARE(spyAboutToAdd.count(), 1);
  105. QCOMPARE(spyAdded.count(), 1);
  106. QCOMPARE(spyAboutToRemove.count(), 0);
  107. QCOMPARE(spyRemoved.count(), 0);
  108. QCOMPARE(spyAboutToMove.count(), 3);
  109. QCOMPARE(spyMoved.count(), 3);
  110. QVERIFY(index12.isValid());
  111. QCOMPARE(model->data(index12).toString(), QString("group12"));
  112. QCOMPARE(model->data(index12.model()->index(0, 0, index12)).toString(), QString("group121"));
  113. delete group12;
  114. QCOMPARE(spyAboutToAdd.count(), 1);
  115. QCOMPARE(spyAdded.count(), 1);
  116. QCOMPARE(spyAboutToRemove.count(), 2);
  117. QCOMPARE(spyRemoved.count(), 2);
  118. QCOMPARE(spyAboutToMove.count(), 3);
  119. QCOMPARE(spyMoved.count(), 3);
  120. QVERIFY(!index12.isValid());
  121. // test removing a group that has children
  122. delete group1;
  123. delete db;
  124. delete modelTest;
  125. delete model;
  126. }