1 /*
2     SPDX-FileCopyrightText: 2013 Andreas Cord-Landwehr <cordlandwehr@kde.org>
3 
4     SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
5 */
6 
7 #include "test_unit.h"
8 #include "../mocks/languagestub.h"
9 #include "core/language.h"
10 #include "core/phonemegroup.h"
11 #include "core/phrase.h"
12 #include "core/unit.h"
13 #include "resourcerepositorystub.h"
14 #include <QDebug>
15 #include <QSignalSpy>
16 
17 TestUnit::TestUnit() = default;
18 
init()19 void TestUnit::init()
20 {
21     qRegisterMetaType<std::shared_ptr<IPhrase>>("std::shared_ptr<IPhrase>");
22 }
23 
cleanup()24 void TestUnit::cleanup()
25 {
26 }
27 
unitPropertyChanges()28 void TestUnit::unitPropertyChanges()
29 {
30     auto unit = Unit::create();
31     QVERIFY(unit);
32 
33     {
34         QSignalSpy spy(unit.get(), &Unit::idChanged);
35         QString id("testId");
36         unit->setId(id);
37         QCOMPARE(unit->id(), id);
38         QCOMPARE(spy.count(), 1);
39     }
40     {
41         QString foreignId("foreignId");
42         unit->setForeignId(foreignId);
43         QCOMPARE(unit->foreignId(), foreignId);
44     }
45     {
46         QSignalSpy spy(unit.get(), &Unit::titleChanged);
47         QString title("title");
48         unit->setTitle(title);
49         QCOMPARE(unit->title(), title);
50         QCOMPARE(spy.count(), 1);
51     }
52 }
53 
addAndRemovePhrases()54 void TestUnit::addAndRemovePhrases()
55 {
56     auto unit = Unit::create();
57     QString unitId {"unitId"};
58     unit->setId(unitId);
59     QVERIFY(unit);
60 
61     auto phraseA = Phrase::create();
62     QString phraseIdA {"phraseIdA"};
63     phraseA->setId(phraseIdA);
64 
65     auto phraseB = Phrase::create();
66     QString phraseIdB {"phraseIdB"};
67     phraseB->setId(phraseIdB);
68 
69     {
70         QSignalSpy aboutSpy(unit.get(), &Unit::phraseAboutToBeAdded);
71         QSignalSpy addedSpy(unit.get(), &Unit::phraseAdded);
72         QCOMPARE(unit->phrases().count(), 0);
73         unit->addPhrase(phraseA, 0);
74         QCOMPARE(unit->phrases().count(), 1);
75         QCOMPARE(aboutSpy.count(), 1);
76         auto parameters = aboutSpy.takeFirst();
77         // TODO check object parameter
78         QCOMPARE(parameters.at(1).toInt(), 0);
79         QCOMPARE(addedSpy.count(), 1);
80         QCOMPARE(phraseA->unit()->id(), unitId);
81     }
82 
83     { // prepend second phrase
84         QSignalSpy aboutSpy(unit.get(), &Unit::phraseAboutToBeAdded);
85         QSignalSpy addedSpy(unit.get(), &Unit::phraseAdded);
86         QCOMPARE(unit->phrases().count(), 1);
87         unit->addPhrase(phraseB, 0);
88         QCOMPARE(unit->phrases().count(), 2);
89         QCOMPARE(aboutSpy.count(), 1);
90         auto parameters = aboutSpy.takeFirst();
91         // TODO check object parameter
92         QCOMPARE(parameters.at(1).toInt(), 0);
93         QCOMPARE(addedSpy.count(), 1);
94         QCOMPARE(phraseB->unit()->id(), unitId);
95     }
96 
97     {
98         QSignalSpy aboutSpy(unit.get(), &Unit::phraseAboutToBeRemoved);
99         QSignalSpy removedSpy(unit.get(), &Unit::phraseRemoved);
100         QCOMPARE(unit->phrases().count(), 2);
101         unit->removePhrase(phraseA);
102         QCOMPARE(unit->phrases().count(), 1);
103         QCOMPARE(aboutSpy.count(), 1);
104         QCOMPARE(removedSpy.count(), 1);
105     }
106 }
107 
108 QTEST_GUILESS_MAIN(TestUnit)
109