File indexing completed on 2023-11-26 10:33:52

0001 /* GCompris - FileTest.cpp
0002  *
0003  * SPDX-FileCopyrightText: 2018 Himanshu Vishwakarma <himvish997@gmail.com>
0004  * GCompris  (C) 2018 GCompris Developers  <gcompris-devel@kde.org>
0005  *
0006  * Authors:
0007  *   Himanshu Vishwakarma <himvish997@gmail.com>
0008  *
0009  *   SPDX-License-Identifier: GPL-3.0-or-later
0010  */
0011 
0012 /* This file is used for the unit tests */
0013 
0014 #include <QTest>
0015 #include <QObject>
0016 #include <QFile>
0017 #include <QSignalSpy>
0018 
0019 #include "src/core/File.h"
0020 
0021 class CoreFileTest : public QObject
0022 {
0023     Q_OBJECT
0024 private Q_SLOTS:
0025     void cleanup();
0026     void FileExistsTest();
0027     void ReadWriteErrorsTest();
0028     void ReadWriteErrorsTest_data();
0029     void ReadWriteTest();
0030     void NameTest();
0031 };
0032 
0033 static const char *tempFilename = "./dummy_test_files.txt";
0034 static const char *fakeFilename = "-_/fezagvvx&V/d;-├╣lc";
0035 
0036 void CoreFileTest::FileExistsTest()
0037 {
0038     QFile tempFile(tempFilename);
0039     // open in write mode to create the file if does not exist
0040     tempFile.open(QIODevice::ReadWrite);
0041     tempFile.close();
0042 
0043     QVERIFY(File::exists(tempFilename));
0044 }
0045 
0046 void CoreFileTest::ReadWriteErrorsTest_data()
0047 {
0048     QTest::addColumn<QString>("filename");
0049     QTest::addColumn<QString>("readError");
0050     QTest::addColumn<QString>("writeError");
0051     QTest::newRow("empty file") << ""
0052                                 << QStringLiteral("source is empty")
0053                                 << QStringLiteral("source is empty");
0054 
0055     QTest::newRow("non existing file") << fakeFilename
0056                                        << QStringLiteral("Unable to open the file")
0057                                        << QStringLiteral("could not open file ") + fakeFilename;
0058 }
0059 
0060 void CoreFileTest::ReadWriteErrorsTest()
0061 {
0062     QFETCH(QString, filename);
0063     QFETCH(QString, readError);
0064     QFETCH(QString, writeError);
0065 
0066     const QString fileContent = QLatin1String("this is going to test the class File in the core");
0067 
0068     File file;
0069     QSignalSpy spyError(&file, &File::error);
0070     QVERIFY(spyError.isValid());
0071     QVERIFY(spyError.count() == 0);
0072     // we can't read
0073     QVERIFY(file.read(filename).isEmpty());
0074     QVERIFY(spyError.count() == 1);
0075     QString error = qvariant_cast<QString>(spyError.at(0).at(0));
0076     QCOMPARE(error, readError);
0077     // we can't write
0078     QVERIFY(!file.write(fileContent, filename));
0079     QVERIFY(spyError.count() == 2);
0080     error = qvariant_cast<QString>(spyError.at(1).at(0));
0081     QCOMPARE(error, writeError);
0082     // we can't append
0083     QVERIFY(!file.append(fileContent, filename));
0084     QVERIFY(spyError.count() == 3);
0085     error = qvariant_cast<QString>(spyError.at(2).at(0));
0086     QCOMPARE(error, writeError);
0087 }
0088 
0089 void CoreFileTest::ReadWriteTest()
0090 {
0091     QFile tempFile(tempFilename);
0092     // open in write mode to create the file if does not exist
0093     tempFile.open(QIODevice::ReadWrite);
0094     tempFile.close();
0095 
0096     File file;
0097     const QString fileContent = QLatin1String("this is going to test the class File in the core");
0098     // normal use case, file exists
0099     QVERIFY(file.write(fileContent, tempFilename));
0100     QCOMPARE(file.read(), fileContent);
0101 
0102     // append to the file
0103     const QString appendedText = QLatin1String("appended text.");
0104     QVERIFY(file.append(appendedText, tempFilename));
0105     QCOMPARE(file.read(), fileContent+appendedText);
0106 }
0107 
0108 void CoreFileTest::NameTest()
0109 {
0110     File file;
0111     QSignalSpy spyName(&file, &File::nameChanged);
0112     QVERIFY(spyName.isValid());
0113     QVERIFY(spyName.count() == 0);
0114     file.setName(tempFilename);
0115     QVERIFY(spyName.count() == 1);
0116     QCOMPARE(file.name(), QString(tempFilename));
0117     // test sanitizeUrl
0118     const QString sameNameUnsanitized = QStringLiteral("file://")+tempFilename;
0119     file.setName(sameNameUnsanitized);
0120     // no update triggered as same name after sanitization
0121     QVERIFY(spyName.count() == 1);
0122     QCOMPARE(file.name(), QString(tempFilename));
0123 
0124     const QString filenameUnsanitized = QStringLiteral("qrc:/")+tempFilename;
0125     file.setName(filenameUnsanitized);
0126     // no update triggered as same name after sanitization
0127     QVERIFY(spyName.count() == 2);
0128     QCOMPARE(file.name(), QStringLiteral(":/")+tempFilename);
0129 }
0130 
0131 void CoreFileTest::cleanup()
0132 {
0133     QFile::remove("./dummy_test_files.txt");
0134 }
0135 
0136 QTEST_MAIN(CoreFileTest)
0137 #include "FileTest.moc"