File indexing completed on 2024-04-14 07:16:26

0001 /* GCompris - ActivityInfoTest.cpp
0002  *
0003  * SPDX-FileCopyrightText: 2018 Himanshu Vishwakarma <himvish997@gmail.com>
0004  *
0005  * Authors:
0006  *   Himanshu Vishwakarma <himvish997@gmail.com>
0007  *
0008  *   SPDX-License-Identifier: GPL-3.0-or-later
0009  */
0010 
0011 #include <QTest>
0012 #include <QObject>
0013 #include <QSignalSpy>
0014 
0015 #include "ApplicationSettingsMock.h"
0016 #include "src/core/ActivityInfo.h"
0017 
0018 #define ACTIVITY_INFO_TEST_ATTRIBUTE(attributeName, accessorName, attributeType) \
0019 { \
0020   QFETCH(attributeType, attributeName); \
0021   QSignalSpy spy(&activityinfo, &ActivityInfo::attributeName ## Changed); \
0022   QVERIFY(spy.isValid()); \
0023   QVERIFY(spy.count() == 0); \
0024   activityinfo.set ## accessorName(attributeName); \
0025   QVERIFY(spy.count() == 1); \
0026   QCOMPARE(activityinfo.attributeName(), attributeName); \
0027 }
0028 
0029 class CoreActivityInfoTest : public QObject
0030 {
0031     Q_OBJECT
0032 private Q_SLOTS:
0033     void ActivityInfoTest();
0034     void ActivityInfoTest_data();
0035     void levelsTest();
0036 };
0037 
0038 void CoreActivityInfoTest::ActivityInfoTest_data()
0039 {
0040     QTest::addColumn<QString>("name");
0041     QTest::addColumn<QString>("section");
0042     QTest::addColumn<quint32>("difficulty");
0043     QTest::addColumn<quint32>("minimalDifficulty");
0044     QTest::addColumn<quint32>("maximalDifficulty");
0045     QTest::addColumn<QString>("icon");
0046     QTest::addColumn<QString>("author");
0047     QTest::addColumn<QString>("title");
0048     QTest::addColumn<QString>("description");
0049     QTest::addColumn<QString>("goal");
0050     QTest::addColumn<QString>("prerequisite");
0051     QTest::addColumn<QString>("manual");
0052     QTest::addColumn<QString>("credit");
0053     QTest::addColumn<bool>("favorite");
0054     QTest::addColumn<bool>("enabled");
0055     QTest::addColumn<int>("createdInVersion");
0056 
0057     QTest::newRow("ActivityInfo") << "Name" << "section" << (quint32)3 << (quint32)2 << (quint32)4 << "icon" << "author" << "title" << "description" << "goal" << "prerequisite" << "manual" << "credit" << false << false << 2;
0058     QTest::newRow("UnknownInfo") << "Unknown" << "Unknown" << (quint32)5 << (quint32)1 << (quint32)6 << "Unknown" << "Unknown" << "Unknown" << "Unknown" << "Unknown" << "Unknown" << "Unknown" << "Unknown" << true << true << 10;
0059     QTest::newRow("Empty") << "" << "" << (quint32)0 << (quint32)1 << (quint32)2 << "" << "" << "" << "" << "" << "" << "" << "" << true << true << 0;
0060 }
0061 
0062 void CoreActivityInfoTest::ActivityInfoTest()
0063 {
0064     ActivityInfo activityinfo;
0065 
0066     // called here to set the static instance object to the mock one
0067     ApplicationSettingsMock::getInstance();
0068 
0069     QVERIFY(activityinfo.name().isEmpty());
0070     QVERIFY(activityinfo.section().isEmpty());
0071     QVERIFY(activityinfo.icon().isEmpty());
0072     QVERIFY(activityinfo.author().isEmpty());
0073     QVERIFY(activityinfo.title().isEmpty());
0074     QVERIFY(activityinfo.description().isEmpty());
0075     QVERIFY(activityinfo.goal().isEmpty());
0076     QVERIFY(activityinfo.prerequisite().isEmpty());
0077     QVERIFY(activityinfo.manual().isEmpty());
0078     QVERIFY(activityinfo.credit().isEmpty());
0079     QVERIFY(!activityinfo.favorite());
0080     QVERIFY(activityinfo.enabled());
0081     QCOMPARE(activityinfo.createdInVersion(), 0);
0082 
0083     ACTIVITY_INFO_TEST_ATTRIBUTE(name, Name, QString);
0084     ACTIVITY_INFO_TEST_ATTRIBUTE(section, Section, QString);
0085     ACTIVITY_INFO_TEST_ATTRIBUTE(difficulty, Difficulty, quint32);
0086     ACTIVITY_INFO_TEST_ATTRIBUTE(minimalDifficulty, MinimalDifficulty, quint32);
0087     ACTIVITY_INFO_TEST_ATTRIBUTE(maximalDifficulty, MaximalDifficulty, quint32);
0088     ACTIVITY_INFO_TEST_ATTRIBUTE(icon, Icon, QString);
0089     ACTIVITY_INFO_TEST_ATTRIBUTE(author, Author, QString);
0090     ACTIVITY_INFO_TEST_ATTRIBUTE(title, Title, QString);
0091     ACTIVITY_INFO_TEST_ATTRIBUTE(description, Description, QString);
0092     ACTIVITY_INFO_TEST_ATTRIBUTE(goal, Goal, QString);
0093     ACTIVITY_INFO_TEST_ATTRIBUTE(prerequisite, Prerequisite, QString);
0094     ACTIVITY_INFO_TEST_ATTRIBUTE(manual, Manual, QString);
0095     ACTIVITY_INFO_TEST_ATTRIBUTE(credit, Credit, QString);
0096     ACTIVITY_INFO_TEST_ATTRIBUTE(favorite, Favorite, bool);
0097     ACTIVITY_INFO_TEST_ATTRIBUTE(enabled, Enabled, bool);
0098     ACTIVITY_INFO_TEST_ATTRIBUTE(createdInVersion, CreatedInVersion, int);
0099 
0100     delete ApplicationSettingsMock::getInstance();
0101 }
0102 
0103 void CoreActivityInfoTest::levelsTest()
0104 {
0105     // called here to set the static instance object to the mock one
0106     ApplicationSettingsMock::getInstance();
0107 
0108     ActivityInfo activityinfo;
0109     activityinfo.setName(QStringLiteral("activityTest"));
0110     QVERIFY(!activityinfo.hasDataset());
0111     activityinfo.setLevels({QStringLiteral("1,2,3,4,5,6")});
0112     QStringList expected;
0113     expected << "1" << "2" << "3" << "4" << "5" << "6";
0114     QCOMPARE(activityinfo.levels(), expected);
0115     QCOMPARE(activityinfo.currentLevels(), expected);
0116 
0117     for(int i = 1 ; i < 7 ; ++ i) {
0118         Dataset *d = new Dataset();
0119         d->setDifficulty(i);
0120         activityinfo.addDataset(QString::number(i), d);
0121     }
0122 
0123     activityinfo.enableDatasetsBetweenDifficulties(3, 4);
0124     QCOMPARE(activityinfo.minimalDifficulty(), 3);
0125     QCOMPARE(activityinfo.maximalDifficulty(), 4);
0126 
0127     QVERIFY(activityinfo.hasDataset());
0128 
0129     for(int i = 1 ; i < 7 ; ++ i) {
0130         QVERIFY(activityinfo.getDataset(QString::number(i))->enabled() == (i == 3 || i == 4));
0131         delete activityinfo.getDataset(QString::number(i));
0132     }
0133 }
0134 
0135 QTEST_MAIN(CoreActivityInfoTest)
0136 #include "ActivityInfoTest.moc"