test_courseresource.cpp 6.75 KB
Newer Older
1
/*
Andreas Cord-Landwehr's avatar
Andreas Cord-Landwehr committed
2
 *  Copyright 2013  Andreas Cord-Landwehr <cordlandwehr@kde.org>
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU General Public License as
 *  published by the Free Software Foundation; either version 2 of
 *  the License or (at your option) version 3 or any later version
 *  accepted by the membership of KDE e.V. (or its successor approved
 *  by the membership of KDE e.V.), which shall act as a proxy
 *  defined in Section 14 of version 3 of the license.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "test_courseresource.h"
#include "resourcerepositorystub.h"
#include "core/language.h"
#include "core/unit.h"
#include "core/phrase.h"
26
#include "core/phonemegroup.h"
27
28
29
30
31
32
#include "core/resources/languageresource.h"
#include "core/resources/courseresource.h"

#include <QTest>
#include <QDebug>
#include <QTemporaryFile>
33
#include <QSignalSpy>
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <QIODevice>
#include <QFile>
#include <QXmlSchema>
#include <QXmlSchemaValidator>
#include <QDomDocument>

TestCourseResource::TestCourseResource()
{
}

void TestCourseResource::init()
{
}

void TestCourseResource::cleanup()
{
}

void TestCourseResource::loadCourseResource()
{
54
55
    std::unique_ptr<Language> language(new Language);
    language->setId("de");
56
57
58
    auto group = language->addPhonemeGroup("id", "title");
    group->addPhoneme("g", "G");
    group->addPhoneme("u", "U");
59
60
61
62
    std::vector<std::unique_ptr<Language>> languages;
    languages.push_back(std::move(language));
    ResourceRepositoryStub repository(std::move(languages));

63
64
65
66
67
68
    const QString courseDirectory = "data/courses/de/";
    const QString courseFile = courseDirectory + "de.xml";

    CourseResource course(QUrl::fromLocalFile(courseFile), &repository);
    QCOMPARE(course.file().toLocalFile(), courseFile);
    QCOMPARE(course.id(), "de");
69
    QCOMPARE(course.foreignId(), "artikulate-basic");
70
71
72
73
    QCOMPARE(course.title(), "Artikulate Deutsch");
    QCOMPARE(course.description(), "Ein Kurs in (hoch-)deutscher Aussprache.");
    QVERIFY(course.language() != nullptr);
    QCOMPARE(course.language()->id(), "de");
74
    QCOMPARE(course.units().count(), 1);
75
    QCOMPARE(course.units().first()->course(), &course);
76

77
    const auto unit = course.units().first();
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
    QVERIFY(unit != nullptr);
    QCOMPARE(unit->id(), "1");
    QCOMPARE(unit->title(), QStringLiteral("Auf der Straße"));
    QCOMPARE(unit->foreignId(), "{dd60f04a-eb37-44b7-9787-67aaf7d3578d}");

    QCOMPARE(unit->phraseList().count(), 3);
    // note: this test takes the silent assumption that phrases are added to the list in same
    //   order as they are defined in the file. This assumption should be made explicit or dropped
    const auto firstPhrase = unit->phraseList().first();
    QVERIFY(firstPhrase != nullptr);
    QCOMPARE(firstPhrase->id(), "1");
    QCOMPARE(firstPhrase->foreignId(), "{3a4c1926-60d7-44c6-80d1-03165a641c75}");
    QCOMPARE(firstPhrase->text(), "Guten Tag.");
    QCOMPARE(firstPhrase->soundFileUrl(), courseDirectory + "de_01.ogg");
    QCOMPARE(firstPhrase->type(), Phrase::Type::Sentence);
93
    QCOMPARE(firstPhrase->phonemes().count(), 2);
94
95
}

96
void TestCourseResource::unitAddAndRemoveHandling()
97
98
{
    // boilerplate
99
100
101
102
103
104
    std::unique_ptr<Language> language(new Language);
    language->setId("de");
    std::vector<std::unique_ptr<Language>> languages;
    languages.push_back(std::move(language));
    ResourceRepositoryStub repository(std::move(languages));

105
106
107
108
109
    const QString courseDirectory = "data/courses/de/";
    const QString courseFile = courseDirectory + "de.xml";
    CourseResource course(QUrl::fromLocalFile(courseFile), &repository);

    // begin of test
110
    std::unique_ptr<Unit> unit(new Unit);
111
    unit->setId("testunit");
112
    const int initialUnitNumber = course.units().count();
113
    QCOMPARE(initialUnitNumber, 1);
114
    QSignalSpy spyAboutToBeAdded(&course, SIGNAL(unitAboutToBeAdded(std::shared_ptr<Unit>, int)));
115
116
117
    QSignalSpy spyAdded(&course, SIGNAL(unitAdded()));
    QCOMPARE(spyAboutToBeAdded.count(), 0);
    QCOMPARE(spyAdded.count(), 0);
118
    auto sharedUnit = course.addUnit(std::move(unit));
119
    QCOMPARE(course.units().count(), initialUnitNumber + 1);
120
121
    QCOMPARE(spyAboutToBeAdded.count(), 1);
    QCOMPARE(spyAdded.count(), 1);
122
    QCOMPARE(sharedUnit->course(), &course);
123
124
}

125
126
127
void TestCourseResource::coursePropertyChanges()
{
    // boilerplate
128
129
130
131
132
133
    std::unique_ptr<Language> language(new Language);
    language->setId("de");
    std::vector<std::unique_ptr<Language>> languages;
    languages.push_back(std::move(language));
    ResourceRepositoryStub repository(std::move(languages));

134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
    const QString courseDirectory = "data/courses/de/";
    const QString courseFile = courseDirectory + "de.xml";
    CourseResource course(QUrl::fromLocalFile(courseFile), &repository);

    // id
    {
        const QString value = "newId";
        QSignalSpy spy(&course, SIGNAL(idChanged()));
        QCOMPARE(spy.count(), 0);
        course.setId(value);
        QCOMPARE(course.id(), value);
        QCOMPARE(spy.count(), 1);
    }

    // foreign id
    {
        const QString value = "newForeignId";
        QSignalSpy spy(&course, SIGNAL(foreignIdChanged()));
        QCOMPARE(spy.count(), 0);
        course.setForeignId(value);
        QCOMPARE(course.foreignId(), value);
        QCOMPARE(spy.count(), 1);
    }

    // title
    {
        const QString value = "newTitle";
        QSignalSpy spy(&course, SIGNAL(titleChanged()));
        QCOMPARE(spy.count(), 0);
        course.setTitle(value);
        QCOMPARE(course.title(), value);
        QCOMPARE(spy.count(), 1);
    }

    // title
    {
        const QString value = "newI18nTitle";
        QSignalSpy spy(&course, SIGNAL(i18nTitleChanged()));
        QCOMPARE(spy.count(), 0);
        course.setI18nTitle(value);
        QCOMPARE(course.i18nTitle(), value);
        QCOMPARE(spy.count(), 1);
    }

    // description
    {
        const QString value = "newDescription";
        QSignalSpy spy(&course, SIGNAL(descriptionChanged()));
        QCOMPARE(spy.count(), 0);
        course.setDescription(value);
        QCOMPARE(course.description(), value);
        QCOMPARE(spy.count(), 1);
    }

    // language
    {
190
        std::shared_ptr<Language> testLanguage;
191
192
        QSignalSpy spy(&course, SIGNAL(languageChanged()));
        QCOMPARE(spy.count(), 0);
193
194
        course.setLanguage(testLanguage);
        QCOMPARE(course.language(), testLanguage);
195
196
197
        QCOMPARE(spy.count(), 1);
    }
}
198
199

QTEST_GUILESS_MAIN(TestCourseResource)