testsortedsharemodel.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /*
  2. * Copyright (C) by Claudio Cambra <claudio.cambra@nextcloud.com>
  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 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  11. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  12. * for more details.
  13. */
  14. #include "gui/filedetails/sortedsharemodel.h"
  15. #include <QTest>
  16. #include <QAbstractItemModelTester>
  17. #include <QSignalSpy>
  18. #include "sharetestutils.h"
  19. using namespace OCC;
  20. class TestSortedShareModel : public QObject
  21. {
  22. Q_OBJECT
  23. public slots:
  24. void addAllTestShares()
  25. {
  26. // Let's insert them in the opposite order we want from the model
  27. for (auto it = _expectedOrder.crbegin(); it != _expectedOrder.crend(); ++it) {
  28. helper.appendShareReplyData(*it);
  29. }
  30. }
  31. private:
  32. ShareTestHelper helper;
  33. FakeShareDefinition _userADefinition;
  34. FakeShareDefinition _userBDefinition;
  35. FakeShareDefinition _groupADefinition;
  36. FakeShareDefinition _groupBDefinition;
  37. FakeShareDefinition _linkADefinition;
  38. FakeShareDefinition _linkBDefinition;
  39. FakeShareDefinition _emailADefinition;
  40. FakeShareDefinition _emailBDefinition;
  41. FakeShareDefinition _remoteADefinition;
  42. FakeShareDefinition _remoteBDefinition;
  43. FakeShareDefinition _roomADefinition;
  44. FakeShareDefinition _roomBDefinition;
  45. QVector<FakeShareDefinition> _expectedOrder;
  46. static constexpr auto _expectedShareCount = 12;
  47. private slots:
  48. void initTestCase()
  49. {
  50. QSignalSpy helperSetupSucceeded(&helper, &ShareTestHelper::setupSucceeded);
  51. helper.setup();
  52. QCOMPARE(helperSetupSucceeded.count(), 1);
  53. const auto userAShareWith = QStringLiteral("user_a");
  54. const auto userAShareWithDisplayName = QStringLiteral("User A");
  55. _userADefinition = FakeShareDefinition(&helper, Share::TypeUser, userAShareWith, userAShareWithDisplayName);
  56. const auto userBShareWith = QStringLiteral("user_b");
  57. const auto userBShareWithDisplayName = QStringLiteral("User B");
  58. _userBDefinition = FakeShareDefinition(&helper, Share::TypeUser, userBShareWith, userBShareWithDisplayName);
  59. const auto groupAShareWith = QStringLiteral("group_a");
  60. const auto groupAShareWithDisplayName = QStringLiteral("Group A");
  61. _groupADefinition = FakeShareDefinition(&helper, Share::TypeGroup, groupAShareWith, groupAShareWithDisplayName);
  62. const auto groupBShareWith = QStringLiteral("group_b");
  63. const auto groupBShareWithDisplayName = QStringLiteral("Group B");
  64. _groupBDefinition = FakeShareDefinition(&helper, Share::TypeGroup, groupBShareWith, groupBShareWithDisplayName);
  65. const auto linkALabel = QStringLiteral("Link share label A");
  66. _linkADefinition = FakeShareDefinition(&helper, Share::TypeLink, {}, linkALabel);
  67. const auto linkBLabel = QStringLiteral("Link share label B");
  68. _linkBDefinition = FakeShareDefinition(&helper, Share::TypeLink, {}, linkBLabel);
  69. const auto emailAShareWith = QStringLiteral("email_a@nextcloud.com");
  70. const auto emailAShareWithDisplayName = QStringLiteral("email_a@nextcloud.com");
  71. _emailADefinition = FakeShareDefinition(&helper, Share::TypeEmail, emailAShareWith, emailAShareWithDisplayName);
  72. const auto emailBShareWith = QStringLiteral("email_b@nextcloud.com");
  73. const auto emailBShareWithDisplayName = QStringLiteral("email_b@nextcloud.com");
  74. _emailBDefinition = FakeShareDefinition(&helper, Share::TypeEmail, emailBShareWith, emailBShareWithDisplayName);
  75. const auto remoteAShareWith = QStringLiteral("remote_a");
  76. const auto remoteAShareWithDisplayName = QStringLiteral("Remote share A");
  77. _remoteADefinition = FakeShareDefinition(&helper, Share::TypeRemote, remoteAShareWith, remoteAShareWithDisplayName);
  78. const auto remoteBShareWith = QStringLiteral("remote_b");
  79. const auto remoteBShareWithDisplayName = QStringLiteral("Remote share B");
  80. _remoteBDefinition = FakeShareDefinition(&helper, Share::TypeRemote, remoteBShareWith, remoteBShareWithDisplayName);
  81. const auto roomAShareWith = QStringLiteral("room_a");
  82. const auto roomAShareWithDisplayName = QStringLiteral("Room A");
  83. _roomADefinition = FakeShareDefinition(&helper, Share::TypeRoom, roomAShareWith, roomAShareWithDisplayName);
  84. const auto roomBShareWith = QStringLiteral("room_b");
  85. const auto roomBShareWithDisplayName = QStringLiteral("Room B");
  86. _roomBDefinition = FakeShareDefinition(&helper, Share::TypeRoom, roomBShareWith, roomBShareWithDisplayName);
  87. _expectedOrder = {// Placeholder link shares always go first, followed by normal link shares.
  88. _linkADefinition,
  89. _linkBDefinition,
  90. // For all other share types, we follow the Share::ShareType enum.
  91. _userADefinition,
  92. _userBDefinition,
  93. _groupADefinition,
  94. _groupBDefinition,
  95. _emailADefinition,
  96. _emailBDefinition,
  97. _remoteADefinition,
  98. _remoteBDefinition,
  99. _roomADefinition,
  100. _roomBDefinition};
  101. }
  102. void testSetModel()
  103. {
  104. helper.resetTestData();
  105. addAllTestShares();
  106. QCOMPARE(helper.shareCount(), _expectedShareCount);
  107. ShareModel model;
  108. QSignalSpy sharesChanged(&model, &ShareModel::sharesChanged);
  109. model.setAccountState(helper.accountState.data());
  110. model.setLocalPath(helper.fakeFolder.localPath() + helper.testFileName);
  111. QVERIFY(sharesChanged.wait(5000));
  112. QCOMPARE(model.rowCount(), helper.shareCount());
  113. SortedShareModel sortedModel;
  114. QAbstractItemModelTester sortedModelTester(&sortedModel);
  115. QSignalSpy sortedModelReset(&sortedModel, &SortedShareModel::modelReset);
  116. QSignalSpy shareModelChanged(&sortedModel, &SortedShareModel::shareModelChanged);
  117. sortedModel.setShareModel(&model);
  118. QCOMPARE(shareModelChanged.count(), 1);
  119. QCOMPARE(sortedModelReset.count(), 1);
  120. QCOMPARE(sortedModel.rowCount(), model.rowCount());
  121. QCOMPARE(sortedModel.shareModel(), &model);
  122. }
  123. void testCorrectSort()
  124. {
  125. helper.resetTestData();
  126. addAllTestShares();
  127. QCOMPARE(helper.shareCount(), _expectedShareCount);
  128. ShareModel model;
  129. QSignalSpy sharesChanged(&model, &ShareModel::sharesChanged);
  130. model.setAccountState(helper.accountState.data());
  131. model.setLocalPath(helper.fakeFolder.localPath() + helper.testFileName);
  132. QVERIFY(sharesChanged.wait(5000));
  133. QCOMPARE(model.rowCount(), helper.shareCount());
  134. SortedShareModel sortedModel;
  135. QAbstractItemModelTester sortedModelTester(&sortedModel);
  136. QSignalSpy sortedModelReset(&sortedModel, &SortedShareModel::modelReset);
  137. sortedModel.setShareModel(&model);
  138. QCOMPARE(sortedModelReset.count(), 1);
  139. QCOMPARE(sortedModel.rowCount(), model.rowCount());
  140. for(auto i = 0; i < sortedModel.rowCount(); ++i) {
  141. const auto shareIndex = sortedModel.index(i, 0);
  142. const auto expectedShareDefinition = _expectedOrder.at(i);
  143. QCOMPARE(shareIndex.data(ShareModel::ShareTypeRole).toInt(), expectedShareDefinition.shareType);
  144. QCOMPARE(shareIndex.data(ShareModel::ShareIdRole).toString(), expectedShareDefinition.shareId);
  145. }
  146. }
  147. };
  148. QTEST_MAIN(TestSortedShareModel)
  149. #include "testsortedsharemodel.moc"