TestGroup.cpp 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322
  1. /*
  2. * Copyright (C) 2023 KeePassXC Team <team@keepassxc.org>
  3. * Copyright (C) 2010 Felix Geyer <debfx@fobos.de>
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 2 or (at your option)
  8. * version 3 of the License.
  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 <http://www.gnu.org/licenses/>.
  17. */
  18. #include "TestGroup.h"
  19. #include "mock/MockClock.h"
  20. #include <QSet>
  21. #include <QSignalSpy>
  22. #include <QtTestGui>
  23. #include "core/Group.h"
  24. #include "core/Metadata.h"
  25. #include "crypto/Crypto.h"
  26. QTEST_GUILESS_MAIN(TestGroup)
  27. namespace
  28. {
  29. MockClock* m_clock = nullptr;
  30. }
  31. void TestGroup::initTestCase()
  32. {
  33. qRegisterMetaType<Entry*>("Entry*");
  34. qRegisterMetaType<Group*>("Group*");
  35. QVERIFY(Crypto::init());
  36. }
  37. void TestGroup::init()
  38. {
  39. Q_ASSERT(m_clock == nullptr);
  40. m_clock = new MockClock(2010, 5, 5, 10, 30, 10);
  41. MockClock::setup(m_clock);
  42. }
  43. void TestGroup::cleanup()
  44. {
  45. MockClock::teardown();
  46. m_clock = nullptr;
  47. }
  48. void TestGroup::testParenting()
  49. {
  50. auto db = new Database();
  51. QPointer<Group> rootGroup = db->rootGroup();
  52. auto tmpRoot = new Group();
  53. QPointer<Group> g1 = new Group();
  54. QPointer<Group> g2 = new Group();
  55. QPointer<Group> g3 = new Group();
  56. QPointer<Group> g4 = new Group();
  57. g1->setParent(tmpRoot);
  58. g2->setParent(tmpRoot);
  59. g3->setParent(tmpRoot);
  60. g4->setParent(tmpRoot);
  61. g2->setParent(g1);
  62. g4->setParent(g3);
  63. g3->setParent(g1);
  64. g1->setParent(db->rootGroup());
  65. QVERIFY(g1->parent() == rootGroup);
  66. QVERIFY(g2->parent() == g1);
  67. QVERIFY(g3->parent() == g1);
  68. QVERIFY(g4->parent() == g3);
  69. QVERIFY(g1->database() == db);
  70. QVERIFY(g2->database() == db);
  71. QVERIFY(g3->database() == db);
  72. QVERIFY(g4->database() == db);
  73. QCOMPARE(tmpRoot->children().size(), 0);
  74. QCOMPARE(rootGroup->children().size(), 1);
  75. QCOMPARE(g1->children().size(), 2);
  76. QCOMPARE(g2->children().size(), 0);
  77. QCOMPARE(g3->children().size(), 1);
  78. QCOMPARE(g4->children().size(), 0);
  79. QVERIFY(rootGroup->children().at(0) == g1);
  80. QVERIFY(rootGroup->children().at(0) == g1);
  81. QVERIFY(g1->children().at(0) == g2);
  82. QVERIFY(g1->children().at(1) == g3);
  83. QVERIFY(g3->children().contains(g4));
  84. auto g5 = new Group();
  85. auto g6 = new Group();
  86. g5->setParent(db->rootGroup());
  87. g6->setParent(db->rootGroup());
  88. QVERIFY(db->rootGroup()->children().at(1) == g5);
  89. QVERIFY(db->rootGroup()->children().at(2) == g6);
  90. g5->setParent(db->rootGroup());
  91. QVERIFY(db->rootGroup()->children().at(1) == g6);
  92. QVERIFY(db->rootGroup()->children().at(2) == g5);
  93. QSignalSpy spy(db, SIGNAL(groupDataChanged(Group*)));
  94. g2->setName("test");
  95. g4->setName("test");
  96. g3->setName("test");
  97. g1->setName("test");
  98. g3->setIcon(QUuid::createUuid());
  99. g1->setIcon(2);
  100. QCOMPARE(spy.count(), 6);
  101. delete db;
  102. QVERIFY(rootGroup.isNull());
  103. QVERIFY(g1.isNull());
  104. QVERIFY(g2.isNull());
  105. QVERIFY(g3.isNull());
  106. QVERIFY(g4.isNull());
  107. delete tmpRoot;
  108. }
  109. void TestGroup::testSignals()
  110. {
  111. auto db = new Database();
  112. auto db2 = new Database();
  113. QPointer<Group> root = db->rootGroup();
  114. QSignalSpy spyAboutToAdd(db, SIGNAL(groupAboutToAdd(Group*, int)));
  115. QSignalSpy spyAdded(db, SIGNAL(groupAdded()));
  116. QSignalSpy spyAboutToRemove(db, SIGNAL(groupAboutToRemove(Group*)));
  117. QSignalSpy spyRemoved(db, SIGNAL(groupRemoved()));
  118. QSignalSpy spyAboutToMove(db, SIGNAL(groupAboutToMove(Group*, Group*, int)));
  119. QSignalSpy spyMoved(db, SIGNAL(groupMoved()));
  120. QSignalSpy spyAboutToAdd2(db2, SIGNAL(groupAboutToAdd(Group*, int)));
  121. QSignalSpy spyAdded2(db2, SIGNAL(groupAdded()));
  122. QSignalSpy spyAboutToRemove2(db2, SIGNAL(groupAboutToRemove(Group*)));
  123. QSignalSpy spyRemoved2(db2, SIGNAL(groupRemoved()));
  124. QSignalSpy spyAboutToMove2(db2, SIGNAL(groupAboutToMove(Group*, Group*, int)));
  125. QSignalSpy spyMoved2(db2, SIGNAL(groupMoved()));
  126. auto g1 = new Group();
  127. auto g2 = new Group();
  128. g1->setParent(root);
  129. QCOMPARE(spyAboutToAdd.count(), 1);
  130. QCOMPARE(spyAdded.count(), 1);
  131. QCOMPARE(spyAboutToRemove.count(), 0);
  132. QCOMPARE(spyRemoved.count(), 0);
  133. QCOMPARE(spyAboutToMove.count(), 0);
  134. QCOMPARE(spyMoved.count(), 0);
  135. g2->setParent(root);
  136. QCOMPARE(spyAboutToAdd.count(), 2);
  137. QCOMPARE(spyAdded.count(), 2);
  138. QCOMPARE(spyAboutToRemove.count(), 0);
  139. QCOMPARE(spyRemoved.count(), 0);
  140. QCOMPARE(spyAboutToMove.count(), 0);
  141. QCOMPARE(spyMoved.count(), 0);
  142. g2->setParent(root);
  143. QCOMPARE(spyAboutToAdd.count(), 2);
  144. QCOMPARE(spyAdded.count(), 2);
  145. QCOMPARE(spyAboutToRemove.count(), 0);
  146. QCOMPARE(spyRemoved.count(), 0);
  147. QCOMPARE(spyAboutToMove.count(), 0);
  148. QCOMPARE(spyMoved.count(), 0);
  149. g2->setParent(root, 0);
  150. QCOMPARE(spyAboutToAdd.count(), 2);
  151. QCOMPARE(spyAdded.count(), 2);
  152. QCOMPARE(spyAboutToRemove.count(), 0);
  153. QCOMPARE(spyRemoved.count(), 0);
  154. QCOMPARE(spyAboutToMove.count(), 1);
  155. QCOMPARE(spyMoved.count(), 1);
  156. g1->setParent(g2);
  157. QCOMPARE(spyAboutToAdd.count(), 2);
  158. QCOMPARE(spyAdded.count(), 2);
  159. QCOMPARE(spyAboutToRemove.count(), 0);
  160. QCOMPARE(spyRemoved.count(), 0);
  161. QCOMPARE(spyAboutToMove.count(), 2);
  162. QCOMPARE(spyMoved.count(), 2);
  163. delete g1;
  164. QCOMPARE(spyAboutToAdd.count(), 2);
  165. QCOMPARE(spyAdded.count(), 2);
  166. QCOMPARE(spyAboutToRemove.count(), 1);
  167. QCOMPARE(spyRemoved.count(), 1);
  168. QCOMPARE(spyAboutToMove.count(), 2);
  169. QCOMPARE(spyMoved.count(), 2);
  170. g2->setParent(db2->rootGroup());
  171. QCOMPARE(spyAboutToAdd.count(), 2);
  172. QCOMPARE(spyAdded.count(), 2);
  173. QCOMPARE(spyAboutToRemove.count(), 2);
  174. QCOMPARE(spyRemoved.count(), 2);
  175. QCOMPARE(spyAboutToMove.count(), 2);
  176. QCOMPARE(spyMoved.count(), 2);
  177. QCOMPARE(spyAboutToAdd2.count(), 1);
  178. QCOMPARE(spyAdded2.count(), 1);
  179. QCOMPARE(spyAboutToRemove2.count(), 0);
  180. QCOMPARE(spyRemoved2.count(), 0);
  181. QCOMPARE(spyAboutToMove2.count(), 0);
  182. QCOMPARE(spyMoved2.count(), 0);
  183. auto g3 = new Group();
  184. auto g4 = new Group();
  185. g3->setParent(root);
  186. QCOMPARE(spyAboutToAdd.count(), 3);
  187. QCOMPARE(spyAdded.count(), 3);
  188. QCOMPARE(spyAboutToRemove.count(), 2);
  189. QCOMPARE(spyRemoved.count(), 2);
  190. QCOMPARE(spyAboutToMove.count(), 2);
  191. QCOMPARE(spyMoved.count(), 2);
  192. g4->setParent(root);
  193. QCOMPARE(spyAboutToAdd.count(), 4);
  194. QCOMPARE(spyAdded.count(), 4);
  195. QCOMPARE(spyAboutToRemove.count(), 2);
  196. QCOMPARE(spyRemoved.count(), 2);
  197. QCOMPARE(spyAboutToMove.count(), 2);
  198. QCOMPARE(spyMoved.count(), 2);
  199. g3->setParent(root);
  200. QCOMPARE(spyAboutToAdd.count(), 4);
  201. QCOMPARE(spyAdded.count(), 4);
  202. QCOMPARE(spyAboutToRemove.count(), 2);
  203. QCOMPARE(spyRemoved.count(), 2);
  204. QCOMPARE(spyAboutToMove.count(), 3);
  205. QCOMPARE(spyMoved.count(), 3);
  206. delete db;
  207. delete db2;
  208. QVERIFY(root.isNull());
  209. }
  210. void TestGroup::testEntries()
  211. {
  212. auto group = new Group();
  213. QPointer<Entry> entry1 = new Entry();
  214. entry1->setGroup(group);
  215. QPointer<Entry> entry2 = new Entry();
  216. entry2->setGroup(group);
  217. QCOMPARE(group->entries().size(), 2);
  218. QVERIFY(group->entries().at(0) == entry1);
  219. QVERIFY(group->entries().at(1) == entry2);
  220. delete group;
  221. QVERIFY(entry1.isNull());
  222. QVERIFY(entry2.isNull());
  223. }
  224. void TestGroup::testDeleteSignals()
  225. {
  226. QScopedPointer<Database> db(new Database());
  227. Group* groupRoot = db->rootGroup();
  228. auto groupChild = new Group();
  229. auto groupChildChild = new Group();
  230. groupRoot->setObjectName("groupRoot");
  231. groupChild->setObjectName("groupChild");
  232. groupChildChild->setObjectName("groupChildChild");
  233. groupChild->setParent(groupRoot);
  234. groupChildChild->setParent(groupChild);
  235. QSignalSpy spyAboutToRemove(db.data(), SIGNAL(groupAboutToRemove(Group*)));
  236. QSignalSpy spyRemoved(db.data(), SIGNAL(groupRemoved()));
  237. delete groupChild;
  238. QVERIFY(groupRoot->children().isEmpty());
  239. QCOMPARE(spyAboutToRemove.count(), 2);
  240. QCOMPARE(spyRemoved.count(), 2);
  241. auto group = new Group();
  242. auto entry = new Entry();
  243. entry->setGroup(group);
  244. QSignalSpy spyEntryAboutToRemove(group, SIGNAL(entryAboutToRemove(Entry*)));
  245. QSignalSpy spyEntryRemoved(group, SIGNAL(entryRemoved(Entry*)));
  246. delete entry;
  247. QVERIFY(group->entries().isEmpty());
  248. QCOMPARE(spyEntryAboutToRemove.count(), 1);
  249. QCOMPARE(spyEntryRemoved.count(), 1);
  250. delete group;
  251. QScopedPointer<Database> db2(new Database());
  252. Group* groupRoot2 = db2->rootGroup();
  253. auto group2 = new Group();
  254. group2->setParent(groupRoot2);
  255. auto entry2 = new Entry();
  256. entry2->setGroup(group2);
  257. QSignalSpy spyEntryAboutToRemove2(group2, SIGNAL(entryAboutToRemove(Entry*)));
  258. QSignalSpy spyEntryRemoved2(group2, SIGNAL(entryRemoved(Entry*)));
  259. delete group2;
  260. QCOMPARE(spyEntryAboutToRemove2.count(), 1);
  261. QCOMPARE(spyEntryRemoved2.count(), 1);
  262. }
  263. void TestGroup::testCopyCustomIcon()
  264. {
  265. QScopedPointer<Database> dbSource(new Database());
  266. QUuid groupIconUuid = QUuid::createUuid();
  267. QByteArray groupIcon("group icon");
  268. QString groupIconName("group icon");
  269. dbSource->metadata()->addCustomIcon(groupIconUuid, groupIcon, groupIconName);
  270. QUuid entryIconUuid = QUuid::createUuid();
  271. QByteArray entryIcon("entry icon");
  272. QString entryIconName("entry icon");
  273. dbSource->metadata()->addCustomIcon(entryIconUuid, entryIcon, entryIconName);
  274. auto* group = new Group();
  275. group->setParent(dbSource->rootGroup());
  276. group->setIcon(groupIconUuid);
  277. QCOMPARE(group->database()->metadata()->customIcon(groupIconUuid).data, groupIcon);
  278. QCOMPARE(group->database()->metadata()->customIcon(groupIconUuid).name, groupIconName);
  279. auto* entry = new Entry();
  280. entry->setGroup(dbSource->rootGroup());
  281. entry->setIcon(entryIconUuid);
  282. QCOMPARE(entry->database()->metadata()->customIcon(entryIconUuid).data, entryIcon);
  283. QCOMPARE(entry->database()->metadata()->customIcon(entryIconUuid).name, entryIconName);
  284. QScopedPointer<Database> dbTarget(new Database());
  285. group->setParent(dbTarget->rootGroup());
  286. QVERIFY(dbTarget->metadata()->hasCustomIcon(groupIconUuid));
  287. QCOMPARE(dbTarget->metadata()->customIcon(groupIconUuid).data, groupIcon);
  288. QCOMPARE(dbTarget->metadata()->customIcon(groupIconUuid).name, groupIconName);
  289. entry->setGroup(dbTarget->rootGroup());
  290. QVERIFY(dbTarget->metadata()->hasCustomIcon(entryIconUuid));
  291. QCOMPARE(dbTarget->metadata()->customIcon(entryIconUuid).data, entryIcon);
  292. QCOMPARE(dbTarget->metadata()->customIcon(entryIconUuid).name, entryIconName);
  293. }
  294. void TestGroup::testClone()
  295. {
  296. QScopedPointer<Database> db(new Database());
  297. QScopedPointer<Group> originalGroup(new Group());
  298. originalGroup->setParent(db->rootGroup());
  299. originalGroup->setName("Group");
  300. originalGroup->setIcon(42);
  301. QScopedPointer<Entry> originalGroupEntry(new Entry());
  302. originalGroupEntry->setGroup(originalGroup.data());
  303. originalGroupEntry->setTitle("GroupEntryOld");
  304. originalGroupEntry->setIcon(43);
  305. originalGroupEntry->beginUpdate();
  306. originalGroupEntry->setTitle("GroupEntry");
  307. originalGroupEntry->endUpdate();
  308. QScopedPointer<Group> subGroup(new Group());
  309. subGroup->setParent(originalGroup.data());
  310. subGroup->setName("SubGroup");
  311. QScopedPointer<Entry> subGroupEntry(new Entry());
  312. subGroupEntry->setGroup(subGroup.data());
  313. subGroupEntry->setTitle("SubGroupEntry");
  314. QScopedPointer<Group> clonedGroup(originalGroup->clone());
  315. QVERIFY(!clonedGroup->parentGroup());
  316. QVERIFY(!clonedGroup->database());
  317. QVERIFY(clonedGroup->uuid() != originalGroup->uuid());
  318. QCOMPARE(clonedGroup->name(), QString("Group"));
  319. QCOMPARE(clonedGroup->iconNumber(), 42);
  320. QCOMPARE(clonedGroup->children().size(), 1);
  321. QCOMPARE(clonedGroup->entries().size(), 1);
  322. Entry* clonedGroupEntry = clonedGroup->entries().at(0);
  323. QVERIFY(clonedGroupEntry->uuid() != originalGroupEntry->uuid());
  324. QCOMPARE(clonedGroupEntry->title(), QString("GroupEntry"));
  325. QCOMPARE(clonedGroupEntry->iconNumber(), 43);
  326. QCOMPARE(clonedGroupEntry->historyItems().size(), 0);
  327. Group* clonedSubGroup = clonedGroup->children().at(0);
  328. QVERIFY(clonedSubGroup->uuid() != subGroup->uuid());
  329. QCOMPARE(clonedSubGroup->name(), QString("SubGroup"));
  330. QCOMPARE(clonedSubGroup->children().size(), 0);
  331. QCOMPARE(clonedSubGroup->entries().size(), 1);
  332. Entry* clonedSubGroupEntry = clonedSubGroup->entries().at(0);
  333. QVERIFY(clonedSubGroupEntry->uuid() != subGroupEntry->uuid());
  334. QCOMPARE(clonedSubGroupEntry->title(), QString("SubGroupEntry"));
  335. QScopedPointer<Group> clonedGroupKeepUuid(originalGroup->clone(Entry::CloneNoFlags));
  336. QCOMPARE(clonedGroupKeepUuid->entries().at(0)->uuid(), originalGroupEntry->uuid());
  337. QCOMPARE(clonedGroupKeepUuid->children().at(0)->entries().at(0)->uuid(), subGroupEntry->uuid());
  338. QScopedPointer<Group> clonedGroupNoFlags(originalGroup->clone(Entry::CloneNoFlags, Group::CloneNoFlags));
  339. QCOMPARE(clonedGroupNoFlags->entries().size(), 0);
  340. QVERIFY(clonedGroupNoFlags->uuid() == originalGroup->uuid());
  341. QScopedPointer<Group> clonedGroupNewUuid(originalGroup->clone(Entry::CloneNoFlags, Group::CloneNewUuid));
  342. QCOMPARE(clonedGroupNewUuid->entries().size(), 0);
  343. QVERIFY(clonedGroupNewUuid->uuid() != originalGroup->uuid());
  344. // Making sure the new modification date is not the same.
  345. m_clock->advanceSecond(1);
  346. QScopedPointer<Group> clonedGroupResetTimeInfo(
  347. originalGroup->clone(Entry::CloneNoFlags, Group::CloneNewUuid | Group::CloneResetTimeInfo));
  348. QCOMPARE(clonedGroupResetTimeInfo->entries().size(), 0);
  349. QVERIFY(clonedGroupResetTimeInfo->uuid() != originalGroup->uuid());
  350. QVERIFY(clonedGroupResetTimeInfo->timeInfo().lastModificationTime()
  351. != originalGroup->timeInfo().lastModificationTime());
  352. }
  353. void TestGroup::testCopyCustomIcons()
  354. {
  355. QScopedPointer<Database> dbSource(new Database());
  356. QScopedPointer<Database> dbTarget(new Database());
  357. Metadata::CustomIconData icon1 = {QByteArray("icon 1"), "icon 1", Clock::currentDateTimeUtc()};
  358. Metadata::CustomIconData icon2 = {QByteArray("icon 2"), "icon 2", Clock::currentDateTimeUtc()};
  359. QScopedPointer<Group> group1(new Group());
  360. group1->setParent(dbSource->rootGroup());
  361. QUuid group1Icon = QUuid::createUuid();
  362. dbSource->metadata()->addCustomIcon(group1Icon, icon1);
  363. group1->setIcon(group1Icon);
  364. QScopedPointer<Group> group2(new Group());
  365. group2->setParent(group1.data());
  366. QUuid group2Icon = QUuid::createUuid();
  367. dbSource->metadata()->addCustomIcon(group2Icon, icon1);
  368. group2->setIcon(group2Icon);
  369. QScopedPointer<Entry> entry1(new Entry());
  370. entry1->setGroup(group2.data());
  371. QUuid entry1IconOld = QUuid::createUuid();
  372. dbSource->metadata()->addCustomIcon(entry1IconOld, icon1);
  373. entry1->setIcon(entry1IconOld);
  374. // add history item
  375. entry1->beginUpdate();
  376. QUuid entry1IconNew = QUuid::createUuid();
  377. dbSource->metadata()->addCustomIcon(entry1IconNew, icon1);
  378. entry1->setIcon(entry1IconNew);
  379. entry1->endUpdate();
  380. // test that we don't overwrite icons
  381. dbTarget->metadata()->addCustomIcon(group2Icon, icon1);
  382. dbTarget->metadata()->copyCustomIcons(group1->customIconsRecursive(), dbSource->metadata());
  383. Metadata* metaTarget = dbTarget->metadata();
  384. QCOMPARE(metaTarget->customIconsOrder().size(), 4);
  385. QVERIFY(metaTarget->hasCustomIcon(group1Icon));
  386. QVERIFY(metaTarget->hasCustomIcon(group2Icon));
  387. QVERIFY(metaTarget->hasCustomIcon(entry1IconOld));
  388. QVERIFY(metaTarget->hasCustomIcon(entry1IconNew));
  389. QCOMPARE(metaTarget->customIcon(group1Icon), icon1);
  390. QCOMPARE(metaTarget->customIcon(group2Icon), icon1);
  391. }
  392. void TestGroup::testFindEntry()
  393. {
  394. QScopedPointer<Database> db(new Database());
  395. auto entry1 = new Entry();
  396. entry1->setTitle(QString("entry1"));
  397. entry1->setGroup(db->rootGroup());
  398. entry1->setUuid(QUuid::createUuid());
  399. auto group1 = new Group();
  400. group1->setName("group1");
  401. auto entry2 = new Entry();
  402. entry2->setTitle(QString("entry2"));
  403. entry2->setGroup(group1);
  404. entry2->setUuid(QUuid::createUuid());
  405. group1->setParent(db->rootGroup());
  406. Entry* entry;
  407. entry = db->rootGroup()->findEntryByUuid(entry1->uuid());
  408. QVERIFY(entry);
  409. QCOMPARE(entry->title(), QString("entry1"));
  410. entry = db->rootGroup()->findEntryByPath(QString("entry1"));
  411. QVERIFY(entry);
  412. QCOMPARE(entry->title(), QString("entry1"));
  413. // We also can find the entry with the leading slash.
  414. entry = db->rootGroup()->findEntryByPath(QString("/entry1"));
  415. QVERIFY(entry);
  416. QCOMPARE(entry->title(), QString("entry1"));
  417. // But two slashes should not be accepted.
  418. entry = db->rootGroup()->findEntryByPath(QString("//entry1"));
  419. QVERIFY(!entry);
  420. entry = db->rootGroup()->findEntryByUuid(entry2->uuid());
  421. QVERIFY(entry);
  422. QCOMPARE(entry->title(), QString("entry2"));
  423. entry = db->rootGroup()->findEntryByPath(QString("group1/entry2"));
  424. QVERIFY(entry);
  425. QCOMPARE(entry->title(), QString("entry2"));
  426. entry = db->rootGroup()->findEntryByPath(QString("/entry2"));
  427. QVERIFY(!entry);
  428. // We also can find the entry with the leading slash.
  429. entry = db->rootGroup()->findEntryByPath(QString("/group1/entry2"));
  430. QVERIFY(entry);
  431. QCOMPARE(entry->title(), QString("entry2"));
  432. // Should also find the entry only by title.
  433. entry = db->rootGroup()->findEntryByPath(QString("entry2"));
  434. QVERIFY(entry);
  435. QCOMPARE(entry->title(), QString("entry2"));
  436. entry = db->rootGroup()->findEntryByPath(QString("invalid/path/to/entry2"));
  437. QVERIFY(!entry);
  438. entry = db->rootGroup()->findEntryByPath(QString("entry27"));
  439. QVERIFY(!entry);
  440. // A valid UUID that does not exist in this database.
  441. entry = db->rootGroup()->findEntryByUuid(QUuid("febfb01ebcdf9dbd90a3f1579dc75281"));
  442. QVERIFY(!entry);
  443. // An invalid UUID.
  444. entry = db->rootGroup()->findEntryByUuid(QUuid("febfb01ebcdf9dbd90a3f1579dc"));
  445. QVERIFY(!entry);
  446. // Empty strings
  447. entry = db->rootGroup()->findEntryByUuid({});
  448. QVERIFY(!entry);
  449. entry = db->rootGroup()->findEntryByPath({});
  450. QVERIFY(!entry);
  451. }
  452. void TestGroup::testFindGroupByPath()
  453. {
  454. QScopedPointer<Database> db(new Database());
  455. auto group1 = new Group();
  456. group1->setName("group1");
  457. group1->setParent(db->rootGroup());
  458. auto group2 = new Group();
  459. group2->setName("group2");
  460. group2->setParent(group1);
  461. Group* group;
  462. group = db->rootGroup()->findGroupByPath("/");
  463. QVERIFY(group);
  464. QCOMPARE(group->uuid(), db->rootGroup()->uuid());
  465. // We also accept it if the leading slash is missing.
  466. group = db->rootGroup()->findGroupByPath("");
  467. QVERIFY(group);
  468. QCOMPARE(group->uuid(), db->rootGroup()->uuid());
  469. group = db->rootGroup()->findGroupByPath("/group1/");
  470. QVERIFY(group);
  471. QCOMPARE(group->uuid(), group1->uuid());
  472. // We also accept it if the leading slash is missing.
  473. group = db->rootGroup()->findGroupByPath("group1/");
  474. QVERIFY(group);
  475. QCOMPARE(group->uuid(), group1->uuid());
  476. // Too many slashes at the end
  477. group = db->rootGroup()->findGroupByPath("group1//");
  478. QVERIFY(!group);
  479. // Missing a slash at the end.
  480. group = db->rootGroup()->findGroupByPath("/group1");
  481. QVERIFY(group);
  482. QCOMPARE(group->uuid(), group1->uuid());
  483. // Too many slashes at the start
  484. group = db->rootGroup()->findGroupByPath("//group1");
  485. QVERIFY(!group);
  486. group = db->rootGroup()->findGroupByPath("/group1/group2/");
  487. QVERIFY(group);
  488. QCOMPARE(group->uuid(), group2->uuid());
  489. // We also accept it if the leading slash is missing.
  490. group = db->rootGroup()->findGroupByPath("group1/group2/");
  491. QVERIFY(group);
  492. QCOMPARE(group->uuid(), group2->uuid());
  493. group = db->rootGroup()->findGroupByPath("group1/group2");
  494. QVERIFY(group);
  495. QCOMPARE(group->uuid(), group2->uuid());
  496. group = db->rootGroup()->findGroupByPath("invalid");
  497. QVERIFY(!group);
  498. }
  499. void TestGroup::testPrint()
  500. {
  501. QScopedPointer<Database> db(new Database());
  502. QString output = db->rootGroup()->print();
  503. QCOMPARE(output, QString("[empty]\n"));
  504. output = db->rootGroup()->print(true);
  505. QCOMPARE(output, QString("[empty]\n"));
  506. auto entry1 = new Entry();
  507. entry1->setTitle(QString("entry1"));
  508. entry1->setGroup(db->rootGroup());
  509. entry1->setUuid(QUuid::createUuid());
  510. output = db->rootGroup()->print();
  511. QCOMPARE(output, QString("entry1\n"));
  512. auto group1 = new Group();
  513. group1->setName("group1");
  514. group1->setParent(db->rootGroup());
  515. auto entry2 = new Entry();
  516. entry2->setTitle(QString("entry2"));
  517. entry2->setGroup(group1);
  518. entry2->setUuid(QUuid::createUuid());
  519. auto group2 = new Group();
  520. group2->setName("group2");
  521. group2->setParent(db->rootGroup());
  522. auto subGroup = new Group();
  523. subGroup->setName("subgroup");
  524. subGroup->setParent(group2);
  525. auto entry3 = new Entry();
  526. entry3->setTitle(QString("entry3"));
  527. entry3->setGroup(subGroup);
  528. entry3->setUuid(QUuid::createUuid());
  529. output = db->rootGroup()->print();
  530. QVERIFY(output.contains(QString("entry1\n")));
  531. QVERIFY(output.contains(QString("group1/\n")));
  532. QVERIFY(!output.contains(QString(" entry2\n")));
  533. QVERIFY(output.contains(QString("group2/\n")));
  534. QVERIFY(!output.contains(QString(" subgroup\n")));
  535. output = db->rootGroup()->print(true);
  536. QVERIFY(output.contains(QString("entry1\n")));
  537. QVERIFY(output.contains(QString("group1/\n")));
  538. QVERIFY(output.contains(QString(" entry2\n")));
  539. QVERIFY(output.contains(QString("group2/\n")));
  540. QVERIFY(output.contains(QString(" subgroup/\n")));
  541. QVERIFY(output.contains(QString(" entry3\n")));
  542. output = db->rootGroup()->print(true, true);
  543. QVERIFY(output.contains(QString("entry1\n")));
  544. QVERIFY(output.contains(QString("group1/\n")));
  545. QVERIFY(output.contains(QString("group1/entry2\n")));
  546. QVERIFY(output.contains(QString("group2/\n")));
  547. QVERIFY(output.contains(QString("group2/subgroup/\n")));
  548. QVERIFY(output.contains(QString("group2/subgroup/entry3\n")));
  549. output = group1->print();
  550. QVERIFY(!output.contains(QString("group1/\n")));
  551. QVERIFY(output.contains(QString("entry2\n")));
  552. output = group2->print(true, true);
  553. QVERIFY(!output.contains(QString("group2/\n")));
  554. QVERIFY(output.contains(QString("subgroup/\n")));
  555. QVERIFY(output.contains(QString("subgroup/entry3\n")));
  556. }
  557. void TestGroup::testAddEntryWithPath()
  558. {
  559. auto db = new Database();
  560. auto group1 = new Group();
  561. group1->setName("group1");
  562. group1->setParent(db->rootGroup());
  563. auto group2 = new Group();
  564. group2->setName("group2");
  565. group2->setParent(group1);
  566. Entry* entry = db->rootGroup()->addEntryWithPath("entry1");
  567. QVERIFY(entry);
  568. QVERIFY(!entry->uuid().isNull());
  569. entry = db->rootGroup()->addEntryWithPath("entry1");
  570. QVERIFY(!entry);
  571. entry = db->rootGroup()->addEntryWithPath("/entry1");
  572. QVERIFY(!entry);
  573. entry = db->rootGroup()->addEntryWithPath("entry2");
  574. QVERIFY(entry);
  575. QVERIFY(entry->title() == "entry2");
  576. QVERIFY(!entry->uuid().isNull());
  577. entry = db->rootGroup()->addEntryWithPath("/entry3");
  578. QVERIFY(entry);
  579. QVERIFY(entry->title() == "entry3");
  580. QVERIFY(!entry->uuid().isNull());
  581. entry = db->rootGroup()->addEntryWithPath("/group1/entry4");
  582. QVERIFY(entry);
  583. QVERIFY(entry->title() == "entry4");
  584. QVERIFY(!entry->uuid().isNull());
  585. entry = db->rootGroup()->addEntryWithPath("/group1/group2/entry5");
  586. QVERIFY(entry);
  587. QVERIFY(entry->title() == "entry5");
  588. QVERIFY(!entry->uuid().isNull());
  589. entry = db->rootGroup()->addEntryWithPath("/group1/invalid_group/entry6");
  590. QVERIFY(!entry);
  591. delete db;
  592. }
  593. void TestGroup::testIsRecycled()
  594. {
  595. Database db;
  596. db.metadata()->setRecycleBinEnabled(true);
  597. auto group1 = new Group();
  598. group1->setName("group1");
  599. group1->setParent(db.rootGroup());
  600. auto group2 = new Group();
  601. group2->setName("group2");
  602. group2->setParent(db.rootGroup());
  603. auto group3 = new Group();
  604. group3->setName("group3");
  605. group3->setParent(group2);
  606. auto group4 = new Group();
  607. group4->setName("group4");
  608. group4->setParent(db.rootGroup());
  609. db.recycleGroup(group2);
  610. QVERIFY(!group1->isRecycled());
  611. QVERIFY(group2->isRecycled());
  612. QVERIFY(group3->isRecycled());
  613. QVERIFY(!group4->isRecycled());
  614. db.recycleGroup(group4);
  615. QVERIFY(group4->isRecycled());
  616. }
  617. void TestGroup::testCopyDataFrom()
  618. {
  619. QScopedPointer<Group> group(new Group());
  620. group->setName("TestGroup");
  621. QScopedPointer<Group> group2(new Group());
  622. group2->setName("TestGroup2");
  623. QScopedPointer<Group> group3(new Group());
  624. group3->setName("TestGroup3");
  625. group3->customData()->set("testKey", "value");
  626. QSignalSpy spyGroupModified(group.data(), SIGNAL(modified()));
  627. QSignalSpy spyGroupDataChanged(group.data(), SIGNAL(groupDataChanged(Group*)));
  628. group->copyDataFrom(group2.data());
  629. QCOMPARE(spyGroupModified.count(), 1);
  630. QCOMPARE(spyGroupDataChanged.count(), 1);
  631. // if no change, no signals
  632. spyGroupModified.clear();
  633. spyGroupDataChanged.clear();
  634. group->copyDataFrom(group2.data());
  635. QCOMPARE(spyGroupModified.count(), 0);
  636. QCOMPARE(spyGroupDataChanged.count(), 0);
  637. // custom data change triggers a separate modified signal
  638. spyGroupModified.clear();
  639. spyGroupDataChanged.clear();
  640. group->copyDataFrom(group3.data());
  641. QCOMPARE(spyGroupDataChanged.count(), 1);
  642. QCOMPARE(spyGroupModified.count(), 2);
  643. }
  644. void TestGroup::testEquals()
  645. {
  646. QScopedPointer<Group> group(new Group());
  647. group->setName("TestGroup");
  648. QVERIFY(group->equals(group.data(), CompareItemDefault));
  649. }
  650. void TestGroup::testChildrenSort()
  651. {
  652. auto createTestGroupWithUnorderedChildren = []() -> Group* {
  653. auto parent = new Group();
  654. auto group1 = new Group();
  655. group1->setName("B");
  656. group1->setParent(parent);
  657. auto group2 = new Group();
  658. group2->setName("e");
  659. group2->setParent(parent);
  660. auto group3 = new Group();
  661. group3->setName("Test999");
  662. group3->setParent(parent);
  663. auto group4 = new Group();
  664. group4->setName("A");
  665. group4->setParent(parent);
  666. auto group5 = new Group();
  667. group5->setName("z");
  668. group5->setParent(parent);
  669. auto group6 = new Group();
  670. group6->setName("045");
  671. group6->setParent(parent);
  672. auto group7 = new Group();
  673. group7->setName("60");
  674. group7->setParent(parent);
  675. auto group8 = new Group();
  676. group8->setName("04test");
  677. group8->setParent(parent);
  678. auto group9 = new Group();
  679. group9->setName("Test12");
  680. group9->setParent(parent);
  681. auto group10 = new Group();
  682. group10->setName("i");
  683. group10->setParent(parent);
  684. auto subGroup1 = new Group();
  685. subGroup1->setName("sub_xte");
  686. subGroup1->setParent(group10);
  687. auto subGroup2 = new Group();
  688. subGroup2->setName("sub_010");
  689. subGroup2->setParent(group10);
  690. auto subGroup3 = new Group();
  691. subGroup3->setName("sub_000");
  692. subGroup3->setParent(group10);
  693. auto subGroup4 = new Group();
  694. subGroup4->setName("sub_M");
  695. subGroup4->setParent(group10);
  696. auto subGroup5 = new Group();
  697. subGroup5->setName("sub_p");
  698. subGroup5->setParent(group10);
  699. auto subGroup6 = new Group();
  700. subGroup6->setName("sub_45p");
  701. subGroup6->setParent(group10);
  702. auto subGroup7 = new Group();
  703. subGroup7->setName("sub_6p");
  704. subGroup7->setParent(group10);
  705. auto subGroup8 = new Group();
  706. subGroup8->setName("sub_tt");
  707. subGroup8->setParent(group10);
  708. auto subGroup9 = new Group();
  709. subGroup9->setName("sub_t0");
  710. subGroup9->setParent(group10);
  711. return parent;
  712. };
  713. Group* parent = createTestGroupWithUnorderedChildren();
  714. Group* subParent = parent->children().last();
  715. parent->sortChildrenRecursively();
  716. QList<Group*> children = parent->children();
  717. QCOMPARE(children.size(), 10);
  718. QCOMPARE(children[0]->name(), QString("045"));
  719. QCOMPARE(children[1]->name(), QString("04test"));
  720. QCOMPARE(children[2]->name(), QString("60"));
  721. QCOMPARE(children[3]->name(), QString("A"));
  722. QCOMPARE(children[4]->name(), QString("B"));
  723. QCOMPARE(children[5]->name(), QString("e"));
  724. QCOMPARE(children[6]->name(), QString("i"));
  725. QCOMPARE(children[7]->name(), QString("Test12"));
  726. QCOMPARE(children[8]->name(), QString("Test999"));
  727. QCOMPARE(children[9]->name(), QString("z"));
  728. children = subParent->children();
  729. QCOMPARE(children.size(), 9);
  730. QCOMPARE(children[0]->name(), QString("sub_000"));
  731. QCOMPARE(children[1]->name(), QString("sub_010"));
  732. QCOMPARE(children[2]->name(), QString("sub_45p"));
  733. QCOMPARE(children[3]->name(), QString("sub_6p"));
  734. QCOMPARE(children[4]->name(), QString("sub_M"));
  735. QCOMPARE(children[5]->name(), QString("sub_p"));
  736. QCOMPARE(children[6]->name(), QString("sub_t0"));
  737. QCOMPARE(children[7]->name(), QString("sub_tt"));
  738. QCOMPARE(children[8]->name(), QString("sub_xte"));
  739. delete parent;
  740. parent = createTestGroupWithUnorderedChildren();
  741. subParent = parent->children().last();
  742. parent->sortChildrenRecursively(true);
  743. children = parent->children();
  744. QCOMPARE(children.size(), 10);
  745. QCOMPARE(children[0]->name(), QString("z"));
  746. QCOMPARE(children[1]->name(), QString("Test999"));
  747. QCOMPARE(children[2]->name(), QString("Test12"));
  748. QCOMPARE(children[3]->name(), QString("i"));
  749. QCOMPARE(children[4]->name(), QString("e"));
  750. QCOMPARE(children[5]->name(), QString("B"));
  751. QCOMPARE(children[6]->name(), QString("A"));
  752. QCOMPARE(children[7]->name(), QString("60"));
  753. QCOMPARE(children[8]->name(), QString("04test"));
  754. QCOMPARE(children[9]->name(), QString("045"));
  755. children = subParent->children();
  756. QCOMPARE(children.size(), 9);
  757. QCOMPARE(children[0]->name(), QString("sub_xte"));
  758. QCOMPARE(children[1]->name(), QString("sub_tt"));
  759. QCOMPARE(children[2]->name(), QString("sub_t0"));
  760. QCOMPARE(children[3]->name(), QString("sub_p"));
  761. QCOMPARE(children[4]->name(), QString("sub_M"));
  762. QCOMPARE(children[5]->name(), QString("sub_6p"));
  763. QCOMPARE(children[6]->name(), QString("sub_45p"));
  764. QCOMPARE(children[7]->name(), QString("sub_010"));
  765. QCOMPARE(children[8]->name(), QString("sub_000"));
  766. delete parent;
  767. parent = createTestGroupWithUnorderedChildren();
  768. subParent = parent->children().last();
  769. subParent->sortChildrenRecursively();
  770. children = parent->children();
  771. QCOMPARE(children.size(), 10);
  772. QCOMPARE(children[0]->name(), QString("B"));
  773. QCOMPARE(children[1]->name(), QString("e"));
  774. QCOMPARE(children[2]->name(), QString("Test999"));
  775. QCOMPARE(children[3]->name(), QString("A"));
  776. QCOMPARE(children[4]->name(), QString("z"));
  777. QCOMPARE(children[5]->name(), QString("045"));
  778. QCOMPARE(children[6]->name(), QString("60"));
  779. QCOMPARE(children[7]->name(), QString("04test"));
  780. QCOMPARE(children[8]->name(), QString("Test12"));
  781. QCOMPARE(children[9]->name(), QString("i"));
  782. children = subParent->children();
  783. QCOMPARE(children.size(), 9);
  784. QCOMPARE(children[0]->name(), QString("sub_000"));
  785. QCOMPARE(children[1]->name(), QString("sub_010"));
  786. QCOMPARE(children[2]->name(), QString("sub_45p"));
  787. QCOMPARE(children[3]->name(), QString("sub_6p"));
  788. QCOMPARE(children[4]->name(), QString("sub_M"));
  789. QCOMPARE(children[5]->name(), QString("sub_p"));
  790. QCOMPARE(children[6]->name(), QString("sub_t0"));
  791. QCOMPARE(children[7]->name(), QString("sub_tt"));
  792. QCOMPARE(children[8]->name(), QString("sub_xte"));
  793. delete parent;
  794. parent = createTestGroupWithUnorderedChildren();
  795. subParent = parent->children().last();
  796. subParent->sortChildrenRecursively(true);
  797. children = parent->children();
  798. QCOMPARE(children.size(), 10);
  799. QCOMPARE(children[0]->name(), QString("B"));
  800. QCOMPARE(children[1]->name(), QString("e"));
  801. QCOMPARE(children[2]->name(), QString("Test999"));
  802. QCOMPARE(children[3]->name(), QString("A"));
  803. QCOMPARE(children[4]->name(), QString("z"));
  804. QCOMPARE(children[5]->name(), QString("045"));
  805. QCOMPARE(children[6]->name(), QString("60"));
  806. QCOMPARE(children[7]->name(), QString("04test"));
  807. QCOMPARE(children[8]->name(), QString("Test12"));
  808. QCOMPARE(children[9]->name(), QString("i"));
  809. children = subParent->children();
  810. QCOMPARE(children.size(), 9);
  811. QCOMPARE(children[0]->name(), QString("sub_xte"));
  812. QCOMPARE(children[1]->name(), QString("sub_tt"));
  813. QCOMPARE(children[2]->name(), QString("sub_t0"));
  814. QCOMPARE(children[3]->name(), QString("sub_p"));
  815. QCOMPARE(children[4]->name(), QString("sub_M"));
  816. QCOMPARE(children[5]->name(), QString("sub_6p"));
  817. QCOMPARE(children[6]->name(), QString("sub_45p"));
  818. QCOMPARE(children[7]->name(), QString("sub_010"));
  819. QCOMPARE(children[8]->name(), QString("sub_000"));
  820. delete parent;
  821. }
  822. void TestGroup::testHierarchy()
  823. {
  824. Group group1;
  825. group1.setName("group1");
  826. auto group2 = new Group();
  827. group2->setName("group2");
  828. group2->setParent(&group1);
  829. auto group3 = new Group();
  830. group3->setName("group3");
  831. group3->setParent(group2);
  832. QStringList hierarchy = group3->hierarchy();
  833. QVERIFY(hierarchy.size() == 3);
  834. QVERIFY(hierarchy.contains("group1"));
  835. QVERIFY(hierarchy.contains("group2"));
  836. QVERIFY(hierarchy.contains("group3"));
  837. hierarchy = group3->hierarchy(0);
  838. QVERIFY(hierarchy.isEmpty());
  839. hierarchy = group3->hierarchy(1);
  840. QVERIFY(hierarchy.size() == 1);
  841. QVERIFY(hierarchy.contains("group3"));
  842. hierarchy = group3->hierarchy(2);
  843. QVERIFY(hierarchy.size() == 2);
  844. QVERIFY(hierarchy.contains("group2"));
  845. QVERIFY(hierarchy.contains("group3"));
  846. }
  847. void TestGroup::testApplyGroupIconRecursively()
  848. {
  849. // Create a database with two nested groups with one entry each
  850. Database database;
  851. auto subgroup = new Group();
  852. subgroup->setName("Subgroup");
  853. subgroup->setParent(database.rootGroup());
  854. QVERIFY(subgroup);
  855. auto subsubgroup = new Group();
  856. subsubgroup->setName("Subsubgroup");
  857. subsubgroup->setParent(subgroup);
  858. QVERIFY(subsubgroup);
  859. Entry* subgroupEntry = subgroup->addEntryWithPath("Subgroup entry");
  860. QVERIFY(subgroupEntry);
  861. subgroup->setIcon(1);
  862. Entry* subsubgroupEntry = subsubgroup->addEntryWithPath("Subsubgroup entry");
  863. QVERIFY(subsubgroupEntry);
  864. subsubgroup->setIcon(2);
  865. // Set an icon per number to the root group and apply recursively
  866. // -> all groups and entries have the same icon
  867. const int rootIconNumber = 42;
  868. database.rootGroup()->setIcon(rootIconNumber);
  869. QVERIFY(database.rootGroup()->iconNumber() == rootIconNumber);
  870. database.rootGroup()->applyGroupIconToChildGroups();
  871. database.rootGroup()->applyGroupIconToChildEntries();
  872. QVERIFY(subgroup->iconNumber() == rootIconNumber);
  873. QVERIFY(subgroupEntry->iconNumber() == rootIconNumber);
  874. QVERIFY(subsubgroup->iconNumber() == rootIconNumber);
  875. QVERIFY(subsubgroupEntry->iconNumber() == rootIconNumber);
  876. // Set an icon per number to the subsubgroup and apply recursively
  877. // -> only the subsubgroup related groups and entries have updated icons
  878. const int subsubgroupIconNumber = 24;
  879. subsubgroup->setIcon(subsubgroupIconNumber);
  880. QVERIFY(subsubgroup->iconNumber() == subsubgroupIconNumber);
  881. subsubgroup->applyGroupIconToChildGroups();
  882. subsubgroup->applyGroupIconToChildEntries();
  883. QVERIFY(database.rootGroup()->iconNumber() == rootIconNumber);
  884. QVERIFY(subgroup->iconNumber() == rootIconNumber);
  885. QVERIFY(subgroupEntry->iconNumber() == rootIconNumber);
  886. QVERIFY(subsubgroup->iconNumber() == subsubgroupIconNumber);
  887. QVERIFY(subsubgroupEntry->iconNumber() == subsubgroupIconNumber);
  888. // Set an icon per UUID to the subgroup and apply recursively
  889. // -> all groups and entries except the root group have the same icon
  890. const QUuid subgroupIconUuid = QUuid::createUuid();
  891. QByteArray subgroupIcon("subgroup icon");
  892. database.metadata()->addCustomIcon(subgroupIconUuid, subgroupIcon);
  893. subgroup->setIcon(subgroupIconUuid);
  894. subgroup->applyGroupIconToChildGroups();
  895. subgroup->applyGroupIconToChildEntries();
  896. QVERIFY(database.rootGroup()->iconNumber() == rootIconNumber);
  897. QCOMPARE(subgroup->iconUuid(), subgroupIconUuid);
  898. QCOMPARE(subgroupEntry->iconUuid(), subgroupIconUuid);
  899. QCOMPARE(subsubgroup->iconUuid(), subgroupIconUuid);
  900. QCOMPARE(subsubgroupEntry->iconUuid(), subgroupIconUuid);
  901. QCOMPARE(subgroup->database()->metadata()->customIcon(subgroupIconUuid).data, subgroupIcon);
  902. // Reset all icons to root icon
  903. database.rootGroup()->setIcon(rootIconNumber);
  904. QVERIFY(database.rootGroup()->iconNumber() == rootIconNumber);
  905. database.rootGroup()->applyGroupIconToChildGroups();
  906. database.rootGroup()->applyGroupIconToChildEntries();
  907. QVERIFY(subgroup->iconNumber() == rootIconNumber);
  908. QVERIFY(subgroupEntry->iconNumber() == rootIconNumber);
  909. QVERIFY(subsubgroup->iconNumber() == rootIconNumber);
  910. QVERIFY(subsubgroupEntry->iconNumber() == rootIconNumber);
  911. // Apply only for child groups
  912. const int iconForGroups = 10;
  913. database.rootGroup()->setIcon(iconForGroups);
  914. QVERIFY(database.rootGroup()->iconNumber() == iconForGroups);
  915. database.rootGroup()->applyGroupIconToChildGroups();
  916. QVERIFY(database.rootGroup()->iconNumber() == iconForGroups);
  917. QVERIFY(subgroup->iconNumber() == iconForGroups);
  918. QVERIFY(subgroupEntry->iconNumber() == rootIconNumber);
  919. QVERIFY(subsubgroup->iconNumber() == iconForGroups);
  920. QVERIFY(subsubgroupEntry->iconNumber() == rootIconNumber);
  921. // Apply only for child entries
  922. const int iconForEntries = 20;
  923. database.rootGroup()->setIcon(iconForEntries);
  924. QVERIFY(database.rootGroup()->iconNumber() == iconForEntries);
  925. database.rootGroup()->applyGroupIconToChildEntries();
  926. QVERIFY(database.rootGroup()->iconNumber() == iconForEntries);
  927. QVERIFY(subgroup->iconNumber() == iconForGroups);
  928. QVERIFY(subgroupEntry->iconNumber() == iconForEntries);
  929. QVERIFY(subsubgroup->iconNumber() == iconForGroups);
  930. QVERIFY(subsubgroupEntry->iconNumber() == iconForEntries);
  931. }
  932. void TestGroup::testUsernamesRecursive()
  933. {
  934. Database database;
  935. // Create a subgroup
  936. auto subgroup = new Group();
  937. subgroup->setName("Subgroup");
  938. subgroup->setParent(database.rootGroup());
  939. // Generate entries in the root group and the subgroup
  940. Entry* rootGroupEntry = database.rootGroup()->addEntryWithPath("Root group entry");
  941. rootGroupEntry->setUsername("Name1");
  942. Entry* subgroupEntry = subgroup->addEntryWithPath("Subgroup entry");
  943. subgroupEntry->setUsername("Name2");
  944. Entry* subgroupEntryReusingUsername = subgroup->addEntryWithPath("Another subgroup entry");
  945. subgroupEntryReusingUsername->setUsername("Name2");
  946. QList<QString> usernames = database.rootGroup()->usernamesRecursive();
  947. QCOMPARE(usernames.size(), 2);
  948. QVERIFY(usernames.contains("Name1"));
  949. QVERIFY(usernames.contains("Name2"));
  950. QVERIFY(usernames.indexOf("Name2") < usernames.indexOf("Name1"));
  951. }
  952. void TestGroup::testMoveUpDown()
  953. {
  954. Database database;
  955. Group* root = database.rootGroup();
  956. QVERIFY(root);
  957. auto entry0 = new Entry();
  958. QVERIFY(entry0);
  959. entry0->setGroup(root);
  960. auto entry1 = new Entry();
  961. QVERIFY(entry1);
  962. entry1->setGroup(root);
  963. auto entry2 = new Entry();
  964. QVERIFY(entry2);
  965. entry2->setGroup(root);
  966. auto entry3 = new Entry();
  967. QVERIFY(entry3);
  968. entry3->setGroup(root);
  969. // default order, straight
  970. QCOMPARE(root->entries().at(0), entry0);
  971. QCOMPARE(root->entries().at(1), entry1);
  972. QCOMPARE(root->entries().at(2), entry2);
  973. QCOMPARE(root->entries().at(3), entry3);
  974. root->moveEntryDown(entry0);
  975. QCOMPARE(root->entries().at(0), entry1);
  976. QCOMPARE(root->entries().at(1), entry0);
  977. QCOMPARE(root->entries().at(2), entry2);
  978. QCOMPARE(root->entries().at(3), entry3);
  979. root->moveEntryDown(entry0);
  980. QCOMPARE(root->entries().at(0), entry1);
  981. QCOMPARE(root->entries().at(1), entry2);
  982. QCOMPARE(root->entries().at(2), entry0);
  983. QCOMPARE(root->entries().at(3), entry3);
  984. root->moveEntryDown(entry0);
  985. QCOMPARE(root->entries().at(0), entry1);
  986. QCOMPARE(root->entries().at(1), entry2);
  987. QCOMPARE(root->entries().at(2), entry3);
  988. QCOMPARE(root->entries().at(3), entry0);
  989. // no effect
  990. root->moveEntryDown(entry0);
  991. QCOMPARE(root->entries().at(0), entry1);
  992. QCOMPARE(root->entries().at(1), entry2);
  993. QCOMPARE(root->entries().at(2), entry3);
  994. QCOMPARE(root->entries().at(3), entry0);
  995. root->moveEntryUp(entry0);
  996. QCOMPARE(root->entries().at(0), entry1);
  997. QCOMPARE(root->entries().at(1), entry2);
  998. QCOMPARE(root->entries().at(2), entry0);
  999. QCOMPARE(root->entries().at(3), entry3);
  1000. root->moveEntryUp(entry0);
  1001. QCOMPARE(root->entries().at(0), entry1);
  1002. QCOMPARE(root->entries().at(1), entry0);
  1003. QCOMPARE(root->entries().at(2), entry2);
  1004. QCOMPARE(root->entries().at(3), entry3);
  1005. root->moveEntryUp(entry0);
  1006. QCOMPARE(root->entries().at(0), entry0);
  1007. QCOMPARE(root->entries().at(1), entry1);
  1008. QCOMPARE(root->entries().at(2), entry2);
  1009. QCOMPARE(root->entries().at(3), entry3);
  1010. // no effect
  1011. root->moveEntryUp(entry0);
  1012. QCOMPARE(root->entries().at(0), entry0);
  1013. QCOMPARE(root->entries().at(1), entry1);
  1014. QCOMPARE(root->entries().at(2), entry2);
  1015. QCOMPARE(root->entries().at(3), entry3);
  1016. root->moveEntryUp(entry2);
  1017. QCOMPARE(root->entries().at(0), entry0);
  1018. QCOMPARE(root->entries().at(1), entry2);
  1019. QCOMPARE(root->entries().at(2), entry1);
  1020. QCOMPARE(root->entries().at(3), entry3);
  1021. root->moveEntryDown(entry0);
  1022. QCOMPARE(root->entries().at(0), entry2);
  1023. QCOMPARE(root->entries().at(1), entry0);
  1024. QCOMPARE(root->entries().at(2), entry1);
  1025. QCOMPARE(root->entries().at(3), entry3);
  1026. root->moveEntryUp(entry3);
  1027. QCOMPARE(root->entries().at(0), entry2);
  1028. QCOMPARE(root->entries().at(1), entry0);
  1029. QCOMPARE(root->entries().at(2), entry3);
  1030. QCOMPARE(root->entries().at(3), entry1);
  1031. root->moveEntryUp(entry3);
  1032. QCOMPARE(root->entries().at(0), entry2);
  1033. QCOMPARE(root->entries().at(1), entry3);
  1034. QCOMPARE(root->entries().at(2), entry0);
  1035. QCOMPARE(root->entries().at(3), entry1);
  1036. root->moveEntryDown(entry2);
  1037. QCOMPARE(root->entries().at(0), entry3);
  1038. QCOMPARE(root->entries().at(1), entry2);
  1039. QCOMPARE(root->entries().at(2), entry0);
  1040. QCOMPARE(root->entries().at(3), entry1);
  1041. root->moveEntryUp(entry1);
  1042. QCOMPARE(root->entries().at(0), entry3);
  1043. QCOMPARE(root->entries().at(1), entry2);
  1044. QCOMPARE(root->entries().at(2), entry1);
  1045. QCOMPARE(root->entries().at(3), entry0);
  1046. }
  1047. void TestGroup::testPreviousParentGroup()
  1048. {
  1049. Database db;
  1050. auto* root = db.rootGroup();
  1051. root->setUuid(QUuid::createUuid());
  1052. QVERIFY(!root->uuid().isNull());
  1053. QVERIFY(!root->previousParentGroup());
  1054. QVERIFY(root->previousParentGroupUuid().isNull());
  1055. auto* group1 = new Group();
  1056. group1->setUuid(QUuid::createUuid());
  1057. group1->setParent(root);
  1058. QVERIFY(!group1->uuid().isNull());
  1059. QVERIFY(!group1->previousParentGroup());
  1060. QVERIFY(group1->previousParentGroupUuid().isNull());
  1061. auto* group2 = new Group();
  1062. group2->setParent(root);
  1063. group2->setUuid(QUuid::createUuid());
  1064. QVERIFY(!group2->uuid().isNull());
  1065. QVERIFY(!group2->previousParentGroup());
  1066. QVERIFY(group2->previousParentGroupUuid().isNull());
  1067. group1->setParent(group2);
  1068. QVERIFY(group1->previousParentGroupUuid() == root->uuid());
  1069. QVERIFY(group1->previousParentGroup() == root);
  1070. // Previous parent shouldn't be recorded if new and old parent are the same
  1071. group1->setParent(group2);
  1072. QVERIFY(group1->previousParentGroupUuid() == root->uuid());
  1073. QVERIFY(group1->previousParentGroup() == root);
  1074. group1->setParent(root);
  1075. QVERIFY(group1->previousParentGroupUuid() == group2->uuid());
  1076. QVERIFY(group1->previousParentGroup() == group2);
  1077. }
  1078. void TestGroup::testAutoTypeState()
  1079. {
  1080. Database db;
  1081. auto* root = db.rootGroup();
  1082. auto* entry1 = new Entry();
  1083. entry1->setGroup(root);
  1084. auto subGroup = new Group();
  1085. subGroup->setParent(root);
  1086. auto* entry2 = new Entry();
  1087. entry2->setGroup(subGroup);
  1088. // Disable Auto-Type from root group
  1089. root->setAutoTypeEnabled(Group::TriState::Disable);
  1090. QVERIFY(!entry1->groupAutoTypeEnabled());
  1091. QVERIFY(!entry2->groupAutoTypeEnabled());
  1092. // Enable Auto-Type for sub group
  1093. subGroup->setAutoTypeEnabled(Group::TriState::Enable);
  1094. QVERIFY(root->autoTypeEnabled() == Group::TriState::Disable);
  1095. QVERIFY(subGroup->autoTypeEnabled() == Group::TriState::Enable);
  1096. QVERIFY(!entry1->groupAutoTypeEnabled());
  1097. QVERIFY(entry2->groupAutoTypeEnabled());
  1098. }