ResourceFolderModel_test.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. // SPDX-License-Identifier: GPL-3.0-only
  2. /*
  3. * PolyMC - Minecraft Launcher
  4. * Copyright (C) 2022 Sefa Eyeoglu <contact@scrumplex.net>
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, version 3.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. *
  18. * This file incorporates work covered by the following copyright and
  19. * permission notice:
  20. *
  21. * Copyright 2013-2021 MultiMC Contributors
  22. *
  23. * Licensed under the Apache License, Version 2.0 (the "License");
  24. * you may not use this file except in compliance with the License.
  25. * You may obtain a copy of the License at
  26. *
  27. * http://www.apache.org/licenses/LICENSE-2.0
  28. *
  29. * Unless required by applicable law or agreed to in writing, software
  30. * distributed under the License is distributed on an "AS IS" BASIS,
  31. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  32. * See the License for the specific language governing permissions and
  33. * limitations under the License.
  34. */
  35. #include <QTest>
  36. #include <QTemporaryDir>
  37. #include <QTimer>
  38. #include <FileSystem.h>
  39. #include <minecraft/mod/ModFolderModel.h>
  40. #include <minecraft/mod/ResourceFolderModel.h>
  41. #define EXEC_UPDATE_TASK(EXEC, VERIFY) \
  42. QEventLoop loop; \
  43. \
  44. connect(&model, &ResourceFolderModel::updateFinished, &loop, &QEventLoop::quit); \
  45. \
  46. QTimer expire_timer; \
  47. expire_timer.callOnTimeout(&loop, &QEventLoop::quit); \
  48. expire_timer.setSingleShot(true); \
  49. expire_timer.start(4000); \
  50. \
  51. VERIFY(EXEC); \
  52. loop.exec(); \
  53. \
  54. QVERIFY2(expire_timer.isActive(), "Timer has expired. The update never finished."); \
  55. expire_timer.stop(); \
  56. \
  57. disconnect(&model, nullptr, &loop, nullptr);
  58. class ResourceFolderModelTest : public QObject
  59. {
  60. Q_OBJECT
  61. private
  62. slots:
  63. // test for GH-1178 - install a folder with files to a mod list
  64. void test_1178()
  65. {
  66. // source
  67. QString source = QFINDTESTDATA("testdata/ResourceFolderModel/test_folder");
  68. // sanity check
  69. QVERIFY(!source.endsWith('/'));
  70. auto verify = [](QString path)
  71. {
  72. QDir target_dir(FS::PathCombine(path, "test_folder"));
  73. QVERIFY(target_dir.entryList().contains("pack.mcmeta"));
  74. QVERIFY(target_dir.entryList().contains("assets"));
  75. };
  76. // 1. test with no trailing /
  77. {
  78. QString folder = source;
  79. QTemporaryDir tempDir;
  80. QEventLoop loop;
  81. ModFolderModel m(tempDir.path(), true);
  82. connect(&m, &ModFolderModel::updateFinished, &loop, &QEventLoop::quit);
  83. QTimer expire_timer;
  84. expire_timer.callOnTimeout(&loop, &QEventLoop::quit);
  85. expire_timer.setSingleShot(true);
  86. expire_timer.start(4000);
  87. m.installMod(folder);
  88. loop.exec();
  89. QVERIFY2(expire_timer.isActive(), "Timer has expired. The update never finished.");
  90. expire_timer.stop();
  91. verify(tempDir.path());
  92. }
  93. // 2. test with trailing /
  94. {
  95. QString folder = source + '/';
  96. QTemporaryDir tempDir;
  97. QEventLoop loop;
  98. ModFolderModel m(tempDir.path(), true);
  99. connect(&m, &ModFolderModel::updateFinished, &loop, &QEventLoop::quit);
  100. QTimer expire_timer;
  101. expire_timer.callOnTimeout(&loop, &QEventLoop::quit);
  102. expire_timer.setSingleShot(true);
  103. expire_timer.start(4000);
  104. m.installMod(folder);
  105. loop.exec();
  106. QVERIFY2(expire_timer.isActive(), "Timer has expired. The update never finished.");
  107. expire_timer.stop();
  108. verify(tempDir.path());
  109. }
  110. }
  111. void test_addFromWatch()
  112. {
  113. QString source = QFINDTESTDATA("testdata/ResourceFolderModel");
  114. ModFolderModel model(source);
  115. QCOMPARE(model.size(), 0);
  116. EXEC_UPDATE_TASK(model.startWatching(), )
  117. for (auto mod : model.allMods())
  118. qDebug() << mod->name();
  119. QCOMPARE(model.size(), 4);
  120. model.stopWatching();
  121. }
  122. void test_removeResource()
  123. {
  124. QString folder_resource = QFINDTESTDATA("testdata/ResourceFolderModel/test_folder");
  125. QString file_mod = QFINDTESTDATA("testdata/ResourceFolderModel/supercoolmod.jar");
  126. QTemporaryDir tmp;
  127. ResourceFolderModel model(QDir(tmp.path()));
  128. QCOMPARE(model.size(), 0);
  129. {
  130. EXEC_UPDATE_TASK(model.installResource(file_mod), QVERIFY)
  131. }
  132. QCOMPARE(model.size(), 1);
  133. qDebug() << "Added first mod.";
  134. {
  135. EXEC_UPDATE_TASK(model.startWatching(), )
  136. }
  137. QCOMPARE(model.size(), 1);
  138. qDebug() << "Started watching the temp folder.";
  139. {
  140. EXEC_UPDATE_TASK(model.installResource(folder_resource), QVERIFY)
  141. }
  142. QCOMPARE(model.size(), 2);
  143. qDebug() << "Added second mod.";
  144. {
  145. EXEC_UPDATE_TASK(model.uninstallResource("supercoolmod.jar"), QVERIFY);
  146. }
  147. QCOMPARE(model.size(), 1);
  148. qDebug() << "Removed first mod.";
  149. QString mod_file_name {model.at(0).fileinfo().fileName()};
  150. QVERIFY(!mod_file_name.isEmpty());
  151. {
  152. EXEC_UPDATE_TASK(model.uninstallResource(mod_file_name), QVERIFY);
  153. }
  154. QCOMPARE(model.size(), 0);
  155. qDebug() << "Removed second mod.";
  156. model.stopWatching();
  157. }
  158. void test_enable_disable()
  159. {
  160. QString folder_resource = QFINDTESTDATA("testdata/ResourceFolderModel/test_folder");
  161. QString file_mod = QFINDTESTDATA("testdata/ResourceFolderModel/supercoolmod.jar");
  162. QTemporaryDir tmp;
  163. ResourceFolderModel model(tmp.path());
  164. QCOMPARE(model.size(), 0);
  165. {
  166. EXEC_UPDATE_TASK(model.installResource(folder_resource), QVERIFY)
  167. }
  168. {
  169. EXEC_UPDATE_TASK(model.installResource(file_mod), QVERIFY)
  170. }
  171. for (auto res : model.all())
  172. qDebug() << res->name();
  173. QCOMPARE(model.size(), 2);
  174. auto& res_1 = model.at(0).type() != ResourceType::FOLDER ? model.at(0) : model.at(1);
  175. auto& res_2 = model.at(0).type() == ResourceType::FOLDER ? model.at(0) : model.at(1);
  176. auto id_1 = res_1.internal_id();
  177. auto id_2 = res_2.internal_id();
  178. bool initial_enabled_res_2 = res_2.enabled();
  179. bool initial_enabled_res_1 = res_1.enabled();
  180. QVERIFY(res_1.type() != ResourceType::FOLDER && res_1.type() != ResourceType::UNKNOWN);
  181. qDebug() << "res_1 is of the correct type.";
  182. QVERIFY(res_1.enabled());
  183. qDebug() << "res_1 is initially enabled.";
  184. QVERIFY(res_1.enable(EnableAction::TOGGLE));
  185. QVERIFY(res_1.enabled() == !initial_enabled_res_1);
  186. qDebug() << "res_1 got successfully toggled.";
  187. QVERIFY(res_1.enable(EnableAction::TOGGLE));
  188. qDebug() << "res_1 got successfully toggled again.";
  189. QVERIFY(res_1.enabled() == initial_enabled_res_1);
  190. QVERIFY(res_1.internal_id() == id_1);
  191. qDebug() << "res_1 got back to its initial state.";
  192. QVERIFY(!res_2.enable(initial_enabled_res_2 ? EnableAction::ENABLE : EnableAction::DISABLE));
  193. QVERIFY(res_2.enabled() == initial_enabled_res_2);
  194. QVERIFY(res_2.internal_id() == id_2);
  195. }
  196. };
  197. QTEST_GUILESS_MAIN(ResourceFolderModelTest)
  198. #include "ResourceFolderModel_test.moc"