File indexing completed on 2022-09-27 12:38:59

0001 /*
0002     SPDX-FileCopyrightText: 2019 Hartmut Riesenbeck <hartmut.riesenbeck@gmx.de>
0003     SPDX-License-Identifier: GPL-2.0-or-later
0004 */
0005 #include "sessionmanagerfixedtest.h"
0006 
0007 #include "documentsettings.h"
0008 #include "prefs.h"
0009 #include "sessionmanagerfixed.h"
0010 #include <KEduVocDocument>
0011 #include <KEduVocWordtype>
0012 #include <QTest>
0013 
0014 namespace SessionManagerFixedTests
0015 {
0016 void SessionManagerFixedTest::initTestCase()
0017 {
0018     // Prevent writing by tast case modified pareleys config file to current users configuration
0019     QStandardPaths::setTestModeEnabled(true);
0020 }
0021 
0022 void SessionManagerFixedTest::cleanupTestCase()
0023 {
0024     if (QStandardPaths::isTestModeEnabled()) {
0025         QFileInfo prefsFile(QStandardPaths::writableLocation(QStandardPaths::ConfigLocation), Prefs::self()->config()->name());
0026         if (prefsFile.exists()) {
0027             QFile::remove(prefsFile.filePath());
0028         }
0029     }
0030 }
0031 
0032 void SessionManagerFixedTest::setupBasicPreferences() const
0033 {
0034     Prefs::setLearningLanguage(0);
0035     Prefs::setKnownLanguage(1);
0036     Prefs::setPracticeDirection(Prefs::EnumPracticeDirection::KnownToLearning);
0037     Prefs::setAllowImageInsteadOfWord(false);
0038     Prefs::setExpire(false);
0039     Prefs::setWordTypesInPracticeEnabled(false);
0040     Prefs::setBlock(true);
0041     Prefs::setPracticeMinimumWrongCount(0);
0042     Prefs::setPracticeMaximumWrongCount(1000);
0043     Prefs::setPracticeMinimumTimesAsked(0);
0044     Prefs::setPracticeMaximumTimesAsked(1000);
0045     Prefs::setPracticeMinimumGrade(0);
0046     Prefs::setPracticeMaximumGrade(7);
0047     Prefs::setSessionMaxSize(20);
0048 }
0049 
0050 void SessionManagerFixedTest::initDocumentPracticeModeDependent(KEduVocDocument &doc, int nEntries) const
0051 {
0052     KEduVocWordType *root = doc.wordTypeContainer();
0053     KEduVocWordType *noun = new KEduVocWordType(QStringLiteral("Noun"), root);
0054     noun->setWordType(KEduVocWordFlag::Noun);
0055     root->appendChildContainer(noun);
0056 
0057     KEduVocWordType *nounChild = new KEduVocWordType(QStringLiteral("Masculine"), noun);
0058     nounChild->setWordType(KEduVocWordFlag::Noun | KEduVocWordFlag::Masculine);
0059     noun->appendChildContainer(nounChild);
0060     nounChild = new KEduVocWordType(QStringLiteral("Feminine"), noun);
0061     nounChild->setWordType(KEduVocWordFlag::Noun | KEduVocWordFlag::Feminine);
0062     noun->appendChildContainer(nounChild);
0063     nounChild = new KEduVocWordType(QStringLiteral("Neuter"), noun);
0064     nounChild->setWordType(KEduVocWordFlag::Noun | KEduVocWordFlag::Neuter);
0065     noun->appendChildContainer(nounChild);
0066 
0067     KEduVocWordType *verb = new KEduVocWordType(QStringLiteral("Verb"), root);
0068     verb->setWordType(KEduVocWordFlag::Verb);
0069     root->appendChildContainer(verb);
0070 
0071     KEduVocWordType *adjective = new KEduVocWordType(QStringLiteral("Adjective"), root);
0072     adjective->setWordType(KEduVocWordFlag::Adjective);
0073     root->appendChildContainer(adjective);
0074 
0075     KEduVocLesson *lesson = new KEduVocLesson(QStringLiteral("Lesson 1"), doc.lesson());
0076     doc.lesson()->appendChildContainer(lesson);
0077 
0078     doc.appendIdentifier();
0079     doc.appendIdentifier();
0080     doc.identifier(0).setName(QStringLiteral("Languge 0"));
0081     doc.identifier(0).setLocale(QStringLiteral("l0"));
0082     doc.identifier(1).setName(QStringLiteral("Languge 1"));
0083     doc.identifier(1).setLocale(QStringLiteral("l1"));
0084 
0085     for (int i = 0; i < nEntries; i++) {
0086         KEduVocExpression *expression = new KEduVocExpression;
0087         expression->setTranslation(0, QStringLiteral("translation 0 word %1").arg(i));
0088         expression->setTranslation(1, QStringLiteral("translation 1 word %1").arg(i));
0089         lesson->appendEntry(expression);
0090 
0091         if (Prefs::practiceMode() == Prefs::EnumPracticeMode::GenderPractice) {
0092             auto container = doc.wordTypeContainer()->childOfType(KEduVocWordFlag::Noun | KEduVocWordFlag::Masculine);
0093             expression->translation(0)->setWordType(container);
0094             expression->translation(1)->setWordType(container);
0095 
0096             // Set top entry vocabel to already practiced and leave article as new
0097             expression->translation(0)->setGrade(KV_LEV1_GRADE);
0098             expression->translation(1)->setGrade(KV_LEV1_GRADE);
0099         }
0100 
0101         if (Prefs::practiceMode() == Prefs::EnumPracticeMode::ConjugationPractice) {
0102             KEduVocConjugation conjugation0;
0103             conjugation0.setConjugation(KEduVocText(QStringLiteral("conjugation %1 00").arg(i)), KEduVocWordFlag::Singular | KEduVocWordFlag::First);
0104             conjugation0.setConjugation(KEduVocText(QStringLiteral("conjugation %1 01").arg(i)), KEduVocWordFlag::Singular | KEduVocWordFlag::Second);
0105             conjugation0.setConjugation(KEduVocText(QStringLiteral("conjugation %1 03").arg(i)),
0106                                         KEduVocWordFlag::Singular | KEduVocWordFlag::Third | KEduVocWordFlag::Neuter);
0107             expression->translation(0)->setConjugation(QStringLiteral("tense"), conjugation0);
0108 
0109             KEduVocConjugation conjugation1;
0110             conjugation1.setConjugation(KEduVocText(QStringLiteral("conjugation %1 10").arg(i)), KEduVocWordFlag::Singular | KEduVocWordFlag::First);
0111             conjugation1.setConjugation(KEduVocText(QStringLiteral("conjugation %1 11").arg(i)), KEduVocWordFlag::Singular | KEduVocWordFlag::Second);
0112             conjugation1.setConjugation(KEduVocText(QStringLiteral("conjugation %1 13").arg(i)),
0113                                         KEduVocWordFlag::Singular | KEduVocWordFlag::Third | KEduVocWordFlag::Neuter);
0114             expression->translation(1)->setConjugation(QStringLiteral("tense"), conjugation1);
0115 
0116             auto container = doc.wordTypeContainer()->childOfType(KEduVocWordFlag::Verb);
0117             expression->translation(0)->setWordType(container);
0118             expression->translation(1)->setWordType(container);
0119 
0120             // Set top level entry to already practiced and leave conjugations as new
0121             expression->translation(0)->setGrade(KV_LEV1_GRADE);
0122             expression->translation(1)->setGrade(KV_LEV1_GRADE);
0123         }
0124 
0125         if (Prefs::practiceMode() == Prefs::EnumPracticeMode::ComparisonPractice) {
0126             expression->translation(0)->setComparativeForm(KEduVocText(QStringLiteral("comparative %1 0").arg(i)));
0127             expression->translation(0)->setSuperlativeForm(KEduVocText(QStringLiteral("superlative %1 0").arg(i)));
0128             expression->translation(1)->setComparativeForm(KEduVocText(QStringLiteral("comparative %1 1").arg(i)));
0129             expression->translation(1)->setSuperlativeForm(KEduVocText(QStringLiteral("superlative %1 1").arg(i)));
0130 
0131             auto container = doc.wordTypeContainer()->childOfType(KEduVocWordFlag::Adjective);
0132             expression->translation(0)->setWordType(container);
0133             expression->translation(1)->setWordType(container);
0134 
0135             // Set top level entry to already practiced and leave comparative forms as new
0136             expression->translation(0)->setGrade(KV_LEV1_GRADE);
0137             expression->translation(1)->setGrade(KV_LEV1_GRADE);
0138         }
0139     }
0140 
0141     doc.setUrl(QUrl::fromLocalFile(QStringLiteral("dummy_fquJ4CM7.kvtml")));
0142     doc.setModified(false);
0143 }
0144 
0145 void SessionManagerFixedTest::test_maximumNumberOfNewWordsPerSession_written()
0146 {
0147     constexpr int MAX_NUMBER_OF_NEW_WORDS = 5;
0148 
0149     setupBasicPreferences();
0150     Prefs::setSessionMaxNewWords(MAX_NUMBER_OF_NEW_WORDS);
0151     Prefs::setPracticeMode(Prefs::EnumPracticeMode::WrittenPractice);
0152 
0153     KEduVocDocument doc;
0154     // Fill document with 10 unpracticed entries appropriate to current practice mode
0155     initDocumentPracticeModeDependent(doc, 10);
0156 
0157     QWidget dummy;
0158     Practice::SessionManagerFixed sessionManager(&dummy);
0159     sessionManager.setDocument(&doc);
0160 
0161     QCOMPARE(sessionManager.allEntryCount(), MAX_NUMBER_OF_NEW_WORDS);
0162 }
0163 
0164 void SessionManagerFixedTest::test_maximumNumberOfNewWordsPerSession_gender()
0165 {
0166     constexpr int MAX_NUMBER_OF_NEW_WORDS = 5;
0167 
0168     setupBasicPreferences();
0169     Prefs::setSessionMaxNewWords(MAX_NUMBER_OF_NEW_WORDS);
0170     Prefs::setPracticeMode(Prefs::EnumPracticeMode::GenderPractice);
0171 
0172     KEduVocDocument doc;
0173     // Fill document with 10 unpracticed entries appropriate to current practice mode
0174     initDocumentPracticeModeDependent(doc, 10);
0175 
0176     QWidget dummy;
0177     Practice::SessionManagerFixed sessionManager(&dummy);
0178     sessionManager.setDocument(&doc);
0179 
0180     QCOMPARE(sessionManager.allEntryCount(), MAX_NUMBER_OF_NEW_WORDS);
0181 }
0182 
0183 void SessionManagerFixedTest::test_maximumNumberOfNewWordsPerSession_conjugation()
0184 {
0185     constexpr int MAX_NUMBER_OF_NEW_WORDS = 5;
0186 
0187     setupBasicPreferences();
0188     Prefs::setSessionMaxNewWords(MAX_NUMBER_OF_NEW_WORDS);
0189     Prefs::setPracticeMode(Prefs::EnumPracticeMode::ConjugationPractice);
0190 
0191     KEduVocDocument doc;
0192     // Fill document with 10 unpracticed entries appropriate to current practice mode
0193     initDocumentPracticeModeDependent(doc, 10);
0194 
0195     DocumentSettings documentSettings(doc.url().url() + QStringLiteral("0"));
0196     documentSettings.load();
0197     documentSettings.setConjugationTenses({QStringLiteral("tense")});
0198     documentSettings.save();
0199 
0200     QWidget dummy;
0201     Practice::SessionManagerFixed sessionManager(&dummy);
0202     sessionManager.setDocument(&doc);
0203 
0204     QCOMPARE(sessionManager.allEntryCount(), MAX_NUMBER_OF_NEW_WORDS);
0205 }
0206 
0207 void SessionManagerFixedTest::test_maximumNumberOfNewWordsPerSession_comparsion()
0208 {
0209     constexpr int MAX_NUMBER_OF_NEW_WORDS = 5;
0210 
0211     setupBasicPreferences();
0212     Prefs::setSessionMaxNewWords(MAX_NUMBER_OF_NEW_WORDS);
0213     Prefs::setPracticeMode(Prefs::EnumPracticeMode::ComparisonPractice);
0214 
0215     KEduVocDocument doc;
0216     // Fill document with 10 unpracticed entries appropriate to current practice mode
0217     initDocumentPracticeModeDependent(doc, 10);
0218 
0219     QWidget dummy;
0220     Practice::SessionManagerFixed sessionManager(&dummy);
0221     sessionManager.setDocument(&doc);
0222 
0223     QCOMPARE(sessionManager.allEntryCount(), MAX_NUMBER_OF_NEW_WORDS);
0224 }
0225 
0226 }
0227 
0228 QTEST_MAIN(SessionManagerFixedTests::SessionManagerFixedTest)