testsortedsharemodel.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. const auto shareDef = *it;
  29. if(it->shareType == Share::TypeInternalLink || it->shareType == Share::TypePlaceholderLink) {
  30. continue; // Don't add the shares that are only internal in the client
  31. }
  32. helper.appendShareReplyData(*it);
  33. }
  34. }
  35. private:
  36. ShareTestHelper helper;
  37. FakeShareDefinition _userADefinition;
  38. FakeShareDefinition _userBDefinition;
  39. FakeShareDefinition _groupADefinition;
  40. FakeShareDefinition _groupBDefinition;
  41. FakeShareDefinition _linkADefinition;
  42. FakeShareDefinition _linkBDefinition;
  43. FakeShareDefinition _emailADefinition;
  44. FakeShareDefinition _emailBDefinition;
  45. FakeShareDefinition _remoteADefinition;
  46. FakeShareDefinition _remoteBDefinition;
  47. FakeShareDefinition _roomADefinition;
  48. FakeShareDefinition _roomBDefinition;
  49. FakeShareDefinition _internalLinkDefinition;
  50. QVector<FakeShareDefinition> _expectedOrder;
  51. static constexpr auto _expectedRemoteShareCount = 12;
  52. private slots:
  53. void initTestCase()
  54. {
  55. QSignalSpy helperSetupSucceeded(&helper, &ShareTestHelper::setupSucceeded);
  56. helper.setup();
  57. QCOMPARE(helperSetupSucceeded.count(), 1);
  58. const auto userAShareWith = QStringLiteral("user_a");
  59. const auto userAShareWithDisplayName = QStringLiteral("User A");
  60. _userADefinition = FakeShareDefinition(&helper, Share::TypeUser, userAShareWith, userAShareWithDisplayName);
  61. const auto userBShareWith = QStringLiteral("user_b");
  62. const auto userBShareWithDisplayName = QStringLiteral("User B");
  63. _userBDefinition = FakeShareDefinition(&helper, Share::TypeUser, userBShareWith, userBShareWithDisplayName);
  64. const auto groupAShareWith = QStringLiteral("group_a");
  65. const auto groupAShareWithDisplayName = QStringLiteral("Group A");
  66. _groupADefinition = FakeShareDefinition(&helper, Share::TypeGroup, groupAShareWith, groupAShareWithDisplayName);
  67. const auto groupBShareWith = QStringLiteral("group_b");
  68. const auto groupBShareWithDisplayName = QStringLiteral("Group B");
  69. _groupBDefinition = FakeShareDefinition(&helper, Share::TypeGroup, groupBShareWith, groupBShareWithDisplayName);
  70. const auto linkALabel = QStringLiteral("Link share label A");
  71. _linkADefinition = FakeShareDefinition(&helper, Share::TypeLink, {}, linkALabel);
  72. const auto linkBLabel = QStringLiteral("Link share label B");
  73. _linkBDefinition = FakeShareDefinition(&helper, Share::TypeLink, {}, linkBLabel);
  74. const auto emailAShareWith = QStringLiteral("email_a@nextcloud.com");
  75. const auto emailAShareWithDisplayName = QStringLiteral("email_a@nextcloud.com");
  76. _emailADefinition = FakeShareDefinition(&helper, Share::TypeEmail, emailAShareWith, emailAShareWithDisplayName);
  77. const auto emailBShareWith = QStringLiteral("email_b@nextcloud.com");
  78. const auto emailBShareWithDisplayName = QStringLiteral("email_b@nextcloud.com");
  79. _emailBDefinition = FakeShareDefinition(&helper, Share::TypeEmail, emailBShareWith, emailBShareWithDisplayName);
  80. const auto remoteAShareWith = QStringLiteral("remote_a");
  81. const auto remoteAShareWithDisplayName = QStringLiteral("Remote share A");
  82. _remoteADefinition = FakeShareDefinition(&helper, Share::TypeRemote, remoteAShareWith, remoteAShareWithDisplayName);
  83. const auto remoteBShareWith = QStringLiteral("remote_b");
  84. const auto remoteBShareWithDisplayName = QStringLiteral("Remote share B");
  85. _remoteBDefinition = FakeShareDefinition(&helper, Share::TypeRemote, remoteBShareWith, remoteBShareWithDisplayName);
  86. const auto roomAShareWith = QStringLiteral("room_a");
  87. const auto roomAShareWithDisplayName = QStringLiteral("Room A");
  88. _roomADefinition = FakeShareDefinition(&helper, Share::TypeRoom, roomAShareWith, roomAShareWithDisplayName);
  89. const auto roomBShareWith = QStringLiteral("room_b");
  90. const auto roomBShareWithDisplayName = QStringLiteral("Room B");
  91. _roomBDefinition = FakeShareDefinition(&helper, Share::TypeRoom, roomBShareWith, roomBShareWithDisplayName);
  92. // Dummy internal link share, just use it to check position
  93. _internalLinkDefinition.shareId = QStringLiteral("__internalLinkShareId__");
  94. _internalLinkDefinition.shareType = Share::TypeInternalLink;
  95. _expectedOrder = {// Placeholder link shares always go first, followed by normal link shares.
  96. _linkADefinition,
  97. _linkBDefinition,
  98. // For all other share types, we follow the Share::ShareType enum.
  99. _userADefinition,
  100. _userBDefinition,
  101. _groupADefinition,
  102. _groupBDefinition,
  103. _emailADefinition,
  104. _emailBDefinition,
  105. _remoteADefinition,
  106. _remoteBDefinition,
  107. _roomADefinition,
  108. _roomBDefinition,
  109. _internalLinkDefinition};
  110. }
  111. void testSetModel()
  112. {
  113. helper.resetTestData();
  114. addAllTestShares();
  115. QCOMPARE(helper.shareCount(), _expectedRemoteShareCount);
  116. ShareModel model;
  117. QSignalSpy sharesChanged(&model, &ShareModel::sharesChanged);
  118. model.setAccountState(helper.accountState.data());
  119. model.setLocalPath(helper.fakeFolder.localPath() + helper.testFileName);
  120. QVERIFY(sharesChanged.wait(5000));
  121. QCOMPARE(model.rowCount(), helper.shareCount() + 1); // Remember the internal link share!
  122. SortedShareModel sortedModel;
  123. QAbstractItemModelTester sortedModelTester(&sortedModel);
  124. QSignalSpy sortedModelReset(&sortedModel, &SortedShareModel::modelReset);
  125. QSignalSpy shareModelChanged(&sortedModel, &SortedShareModel::shareModelChanged);
  126. sortedModel.setShareModel(&model);
  127. QCOMPARE(shareModelChanged.count(), 1);
  128. QCOMPARE(sortedModelReset.count(), 1);
  129. QCOMPARE(sortedModel.rowCount(), model.rowCount());
  130. QCOMPARE(sortedModel.shareModel(), &model);
  131. }
  132. void testCorrectSort()
  133. {
  134. helper.resetTestData();
  135. addAllTestShares();
  136. QCOMPARE(helper.shareCount(), _expectedRemoteShareCount);
  137. ShareModel model;
  138. QSignalSpy sharesChanged(&model, &ShareModel::sharesChanged);
  139. model.setAccountState(helper.accountState.data());
  140. model.setLocalPath(helper.fakeFolder.localPath() + helper.testFileName);
  141. QVERIFY(sharesChanged.wait(5000));
  142. QCOMPARE(model.rowCount(), helper.shareCount() + 1); // Remember the internal link share!
  143. SortedShareModel sortedModel;
  144. QAbstractItemModelTester sortedModelTester(&sortedModel);
  145. QSignalSpy sortedModelReset(&sortedModel, &SortedShareModel::modelReset);
  146. sortedModel.setShareModel(&model);
  147. QCOMPARE(sortedModelReset.count(), 1);
  148. QCOMPARE(sortedModel.rowCount(), model.rowCount());
  149. for(auto i = 0; i < sortedModel.rowCount(); ++i) {
  150. const auto shareIndex = sortedModel.index(i, 0);
  151. const auto expectedShareDefinition = _expectedOrder.at(i);
  152. QCOMPARE(shareIndex.data(ShareModel::ShareTypeRole).toInt(), expectedShareDefinition.shareType);
  153. QCOMPARE(shareIndex.data(ShareModel::ShareIdRole).toString(), expectedShareDefinition.shareId);
  154. }
  155. }
  156. };
  157. QTEST_MAIN(TestSortedShareModel)
  158. #include "testsortedsharemodel.moc"