File: FileTest.cpp

package info (click to toggle)
gcompris-qt 26.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 144,692 kB
  • sloc: javascript: 64,854; cpp: 7,375; xml: 1,349; python: 1,310; sh: 584; sql: 236; php: 183; java: 121; perl: 40; ansic: 14; makefile: 7; awk: 6
file content (153 lines) | stat: -rw-r--r-- 4,613 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/* GCompris - FileTest.cpp
 *
 * SPDX-FileCopyrightText: 2018 Himanshu Vishwakarma <himvish997@gmail.com>
 * GCompris  (C) 2018 GCompris Developers  <gcompris-devel@kde.org>
 *
 * Authors:
 *   Himanshu Vishwakarma <himvish997@gmail.com>
 *
 *   SPDX-License-Identifier: GPL-3.0-or-later
 */

/* This file is used for the unit tests */

#include <QTest>
#include <QObject>
#include <QFile>
#include <QSignalSpy>

#include "src/core/File.h"

class CoreFileTest : public QObject
{
    Q_OBJECT
private Q_SLOTS:
    void cleanup();
    void FileExistsTest();
    void ReadWriteErrorsTest();
    void ReadWriteErrorsTest_data();
    void ReadWriteTest();
    void NameTest();
    void CopyTest();
};

namespace {
    const char *tempFilename = "./dummy_test_files.txt";
    const char *fakeFilename = "-_/fezagvvx&V/d;-ùlc";
};

void CoreFileTest::FileExistsTest()
{
    QFile tempFile(tempFilename);
    // open in write mode to create the file if does not exist
    tempFile.open(QIODevice::ReadWrite);
    tempFile.close();

    QVERIFY(File::exists(tempFilename));
}

void CoreFileTest::ReadWriteErrorsTest_data()
{
    QTest::addColumn<QString>("filename");
    QTest::addColumn<QString>("readError");
    QTest::addColumn<QString>("writeError");
    QTest::newRow("empty file") << ""
                                << QStringLiteral("source is empty")
                                << QStringLiteral("source is empty");

    QTest::newRow("non existing file") << fakeFilename
                                       << QStringLiteral("Unable to open the file")
                                       << QStringLiteral("could not open file ") + fakeFilename;
}

void CoreFileTest::ReadWriteErrorsTest()
{
    QFETCH(QString, filename);
    QFETCH(QString, readError);
    QFETCH(QString, writeError);

    const QString fileContent = QLatin1String("this is going to test the class File in the core");

    File file;
    QSignalSpy spyError(&file, &File::error);
    QVERIFY(spyError.isValid());
    QVERIFY(spyError.count() == 0);
    // we can't read
    QVERIFY(file.read(filename).isEmpty());
    QVERIFY(spyError.count() == 1);
    QString error = qvariant_cast<QString>(spyError.at(0).at(0));
    QCOMPARE(error, readError);
    // we can't write
    QVERIFY(!file.write(fileContent, filename));
    QVERIFY(spyError.count() == 2);
    error = qvariant_cast<QString>(spyError.at(1).at(0));
    QCOMPARE(error, writeError);
    // we can't append
    QVERIFY(!file.append(fileContent, filename));
    QVERIFY(spyError.count() == 3);
    error = qvariant_cast<QString>(spyError.at(2).at(0));
    QCOMPARE(error, writeError);
}

void CoreFileTest::ReadWriteTest()
{
    QFile tempFile(tempFilename);
    // open in write mode to create the file if does not exist
    tempFile.open(QIODevice::ReadWrite);
    tempFile.close();

    File file;
    const QString fileContent = QLatin1String("this is going to test the class File in the core");
    // normal use case, file exists
    QVERIFY(file.write(fileContent, tempFilename));
    QCOMPARE(file.read(), fileContent);

    // append to the file
    const QString appendedText = QLatin1String("appended text.");
    QVERIFY(file.append(appendedText, tempFilename));
    QCOMPARE(file.read(), fileContent+appendedText);
}

void CoreFileTest::NameTest()
{
    File file;
    QSignalSpy spyName(&file, &File::nameChanged);
    QVERIFY(spyName.isValid());
    QVERIFY(spyName.count() == 0);
    file.setName(tempFilename);
    QVERIFY(spyName.count() == 1);
    QCOMPARE(file.name(), QString(tempFilename));
    // test sanitizeUrl
    const QString sameNameUnsanitized = QStringLiteral("file://")+tempFilename;
    file.setName(sameNameUnsanitized);

    // no update triggered as same name after sanitization
    QVERIFY(spyName.count() == 1);
    QCOMPARE(file.name(), QString(tempFilename));

    const QString filenameUnsanitized = QStringLiteral("qrc:/")+tempFilename;
    file.setName(filenameUnsanitized);
    // no update triggered as same name after sanitization
    QVERIFY(spyName.count() == 2);
    QCOMPARE(file.name(), QStringLiteral(":/")+tempFilename);

    // test sanitizeUrl for windows case
    const QString windowsNameUnsanitized = QStringLiteral("file:///c:/windows.png");
    file.setName(windowsNameUnsanitized);
    QCOMPARE(file.name(), QString("c:/windows.png"));
}

void CoreFileTest:: CopyTest()
{
    File file;
    QVERIFY(file.copy(tempFilename, "./copy.txt"));
    QFile::remove("./copy.txt");
}

void CoreFileTest::cleanup()
{
    QFile::remove("./dummy_test_files.txt");
}

QTEST_MAIN(CoreFileTest)
#include "FileTest.moc"