File indexing completed on 2023-10-03 06:52:06

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