TestGroupModel.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 "TestGlobal.h"
  19. #include <QSignalSpy>
  20. #include "crypto/Crypto.h"
  21. #include "gui/group/GroupModel.h"
  22. #include "modeltest.h"
  23. QTEST_GUILESS_MAIN(TestGroupModel)
  24. void TestGroupModel::initTestCase()
  25. {
  26. qRegisterMetaType<QModelIndex>("QModelIndex");
  27. QVERIFY(Crypto::init());
  28. }
  29. void TestGroupModel::test()
  30. {
  31. Database* db = new Database();
  32. Group* groupRoot = db->rootGroup();
  33. groupRoot->setObjectName("groupRoot");
  34. groupRoot->setName("groupRoot");
  35. Group* group1 = new Group();
  36. group1->setObjectName("group1");
  37. group1->setName("group1");
  38. group1->setParent(groupRoot);
  39. Group* group11 = new Group();
  40. group1->setObjectName("group11");
  41. group11->setName("group11");
  42. group11->setParent(group1);
  43. Group* group12 = new Group();
  44. group1->setObjectName("group12");
  45. group12->setName("group12");
  46. group12->setParent(group1);
  47. Group* group121 = new Group();
  48. group1->setObjectName("group121");
  49. group121->setName("group121");
  50. group121->setParent(group12);
  51. GroupModel* model = new GroupModel(db, this);
  52. ModelTest* modelTest = new ModelTest(model, this);
  53. QModelIndex indexRoot = model->index(0, 0);
  54. QModelIndex index1 = model->index(0, 0, indexRoot);
  55. QModelIndex index11 = model->index(0, 0, index1);
  56. QPersistentModelIndex index12 = model->index(1, 0, index1);
  57. QModelIndex index121 = model->index(0, 0, index12);
  58. QCOMPARE(model->data(indexRoot).toString(), QString("groupRoot"));
  59. QCOMPARE(model->data(index1).toString(), QString("group1"));
  60. QCOMPARE(model->data(index11).toString(), QString("group11"));
  61. QCOMPARE(model->data(index12).toString(), QString("group12"));
  62. QCOMPARE(model->data(index121).toString(), QString("group121"));
  63. QSignalSpy spy1(model, SIGNAL(dataChanged(QModelIndex, QModelIndex)));
  64. group11->setName("test");
  65. group121->setIcon(4);
  66. QCOMPARE(spy1.count(), 2);
  67. QCOMPARE(model->data(index11).toString(), QString("test"));
  68. QSignalSpy spyAboutToAdd(model, SIGNAL(rowsAboutToBeInserted(QModelIndex, int, int)));
  69. QSignalSpy spyAdded(model, SIGNAL(rowsInserted(QModelIndex, int, int)));
  70. QSignalSpy spyAboutToRemove(model, SIGNAL(rowsAboutToBeRemoved(QModelIndex, int, int)));
  71. QSignalSpy spyRemoved(model, SIGNAL(rowsRemoved(QModelIndex, int, int)));
  72. QSignalSpy spyAboutToMove(model, SIGNAL(rowsAboutToBeMoved(QModelIndex, int, int, QModelIndex, int)));
  73. QSignalSpy spyMoved(model, SIGNAL(rowsMoved(QModelIndex, int, int, QModelIndex, int)));
  74. Group* group2 = new Group();
  75. group2->setObjectName("group2");
  76. group2->setName("group2");
  77. group2->setParent(groupRoot);
  78. QModelIndex index2 = model->index(1, 0, indexRoot);
  79. QCOMPARE(spyAboutToAdd.count(), 1);
  80. QCOMPARE(spyAdded.count(), 1);
  81. QCOMPARE(spyAboutToRemove.count(), 0);
  82. QCOMPARE(spyRemoved.count(), 0);
  83. QCOMPARE(spyAboutToMove.count(), 0);
  84. QCOMPARE(spyMoved.count(), 0);
  85. QCOMPARE(model->data(index2).toString(), QString("group2"));
  86. group12->setParent(group1, 0);
  87. QCOMPARE(spyAboutToAdd.count(), 1);
  88. QCOMPARE(spyAdded.count(), 1);
  89. QCOMPARE(spyAboutToRemove.count(), 0);
  90. QCOMPARE(spyRemoved.count(), 0);
  91. QCOMPARE(spyAboutToMove.count(), 1);
  92. QCOMPARE(spyMoved.count(), 1);
  93. QCOMPARE(model->data(index12).toString(), QString("group12"));
  94. group12->setParent(group1, 1);
  95. QCOMPARE(spyAboutToAdd.count(), 1);
  96. QCOMPARE(spyAdded.count(), 1);
  97. QCOMPARE(spyAboutToRemove.count(), 0);
  98. QCOMPARE(spyRemoved.count(), 0);
  99. QCOMPARE(spyAboutToMove.count(), 2);
  100. QCOMPARE(spyMoved.count(), 2);
  101. QCOMPARE(model->data(index12).toString(), QString("group12"));
  102. group12->setParent(group2);
  103. QCOMPARE(spyAboutToAdd.count(), 1);
  104. QCOMPARE(spyAdded.count(), 1);
  105. QCOMPARE(spyAboutToRemove.count(), 0);
  106. QCOMPARE(spyRemoved.count(), 0);
  107. QCOMPARE(spyAboutToMove.count(), 3);
  108. QCOMPARE(spyMoved.count(), 3);
  109. QVERIFY(index12.isValid());
  110. QCOMPARE(model->data(index12).toString(), QString("group12"));
  111. QCOMPARE(model->data(index12.model()->index(0, 0, index12)).toString(), QString("group121"));
  112. delete group12;
  113. QCOMPARE(spyAboutToAdd.count(), 1);
  114. QCOMPARE(spyAdded.count(), 1);
  115. QCOMPARE(spyAboutToRemove.count(), 2);
  116. QCOMPARE(spyRemoved.count(), 2);
  117. QCOMPARE(spyAboutToMove.count(), 3);
  118. QCOMPARE(spyMoved.count(), 3);
  119. QVERIFY(!index12.isValid());
  120. // test removing a group that has children
  121. delete group1;
  122. delete db;
  123. delete modelTest;
  124. delete model;
  125. }