Commit 621b524f authored by Boudewijn Rempt's avatar Boudewijn Rempt

Add failing test for KisTagLoader

parent f61aecb2
......@@ -34,7 +34,7 @@ public:
, m_resourceType(resourceType)
{
m_dirIterator.reset(new QDirIterator(location + '/' + resourceType,
QStringList() << "*.tag",
QStringList() << "*.desktop",
QDir::Files | QDir::Readable,
QDirIterator::Subdirectories));
}
......
......@@ -20,8 +20,11 @@
#include "KisTagLoader.h"
#include <QIODevice>
#include <QLocale>
#include <kconfigini_p.h>
#include "kconfigini_p.h"
#include "kconfigbackend_p.h"
#include "kconfigdata.h"
class KisTagLoader::Private {
};
......@@ -30,6 +33,11 @@ KisTagLoader::KisTagLoader()
{
}
KisTagLoader::~KisTagLoader()
{
}
QString KisTagLoader::name() const
{
return QString();
......@@ -62,6 +70,16 @@ void KisTagLoader::setComment(const QString &comment) const
bool KisTagLoader::load(QIODevice &io)
{
KEntryMap map;
KConfigIniBackend ini;
KConfigBackend::ParseInfo r = ini.parseConfigIO(io, QLocale().name().toUtf8(), map, KConfigBackend::ParseOption::ParseGlobal, false);
if (!r == KConfigBackend::ParseInfo::ParseOk) {
if (!io.isOpen()) {
io.open(QIODevice::ReadOnly);
}
io.reset();
qWarning() << "Could not load this tag file" << QString::fromUtf8(io.readAll());
}
return false;
}
......
......@@ -34,11 +34,13 @@
*/
class QIODevice;
class KisTagLoader
#include "kritaresources_export.h"
class KRITARESOURCES_EXPORT KisTagLoader
{
public:
KisTagLoader();
virtual ~KisTagLoader() {}
virtual ~KisTagLoader();
QString name() const;
void setName(QString &name) const;
......
......@@ -79,3 +79,4 @@ void KConfigBackend::setLocalFilePath(const QString &file)
{
d->localFileName = file;
}
......@@ -33,6 +33,7 @@ class KConfigBackendPrivate;
class KEntryMap;
class QFile;
class QByteArray;
class QIODevice;
/**
* \class KConfigBackend kconfigbackend_p.h <KConfigBackend>
......@@ -184,6 +185,7 @@ public:
protected:
KConfigBackend();
void setLocalFilePath(const QString &file);
void setLocalIODevice(QIODevice &io);
private:
KConfigBackendPrivate *const d;
......
......@@ -54,10 +54,15 @@ static QByteArray lookup(const KConfigIniBackend::BufferFragment &fragment, QHas
return cache->insert(fragment, fragment.toByteArray()).value();
}
QString KConfigIniBackend::warningProlog(const QFile &file, int line)
QString KConfigIniBackend::warningProlog(const QIODevice &file, int line)
{
return QStringLiteral("KConfigIni: In file %2, line %1: ")
.arg(line).arg(file.fileName());
const QFile *f = dynamic_cast<const QFile*>(&file);
if (f) {
return QStringLiteral("KConfigIni: In file %2, line %1: ").arg(line).arg(f->fileName());
}
else {
return QStringLiteral("KConfigIni: line %1: ").arg(line);
}
}
KConfigIniBackend::KConfigIniBackend()
......@@ -69,8 +74,7 @@ KConfigIniBackend::~KConfigIniBackend()
{
}
KConfigBackend::ParseInfo
KConfigIniBackend::parseConfig(const QByteArray &currentLocale, KEntryMap &entryMap,
KConfigBackend::ParseInfo KConfigIniBackend::parseConfig(const QByteArray &currentLocale, KEntryMap &entryMap,
ParseOptions options)
{
return parseConfig(currentLocale, entryMap, options, false);
......@@ -78,14 +82,25 @@ KConfigIniBackend::parseConfig(const QByteArray &currentLocale, KEntryMap &entry
// merging==true is the merging that happens at the beginning of writeConfig:
// merge changes in the on-disk file with the changes in the KConfig object.
KConfigBackend::ParseInfo
KConfigIniBackend::parseConfig(const QByteArray &currentLocale, KEntryMap &entryMap,
ParseOptions options, bool merging)
KConfigBackend::ParseInfo KConfigIniBackend::parseConfig(const QByteArray &currentLocale, KEntryMap &entryMap, ParseOptions options, bool merging)
{
if (filePath().isEmpty() || !QFile::exists(filePath())) {
return ParseOk;
}
QFile file(filePath());
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
return ParseOpenError;
}
return parseConfigIO(file, currentLocale, entryMap, options, merging);
}
// merging==true is the merging that happens at the beginning of writeConfig:
// merge changes in the on-disk file with the changes in the KConfig object.
KConfigBackend::ParseInfo KConfigIniBackend::parseConfigIO(QIODevice &file, const QByteArray &currentLocale, KEntryMap &entryMap, ParseOptions options, bool merging)
{
const QByteArray currentLanguage = currentLocale.split('_').first();
bool bDefault = options & ParseDefaults;
......@@ -93,11 +108,6 @@ KConfigIniBackend::parseConfig(const QByteArray &currentLocale, KEntryMap &entry
QByteArray currentGroup("<default>");
QFile file(filePath());
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
return ParseOpenError;
}
QList<QByteArray> immutableGroups;
bool fileOptionImmutable = false;
......@@ -726,7 +736,7 @@ QByteArray KConfigIniBackend::stringToPrintable(const QByteArray &aString, Strin
return result;
}
char KConfigIniBackend::charFromHex(const char *str, const QFile &file, int line)
char KConfigIniBackend::charFromHex(const char *str, const QIODevice &file, int line)
{
unsigned char ret = 0;
for (int i = 0; i < 2; i++) {
......@@ -750,7 +760,7 @@ char KConfigIniBackend::charFromHex(const char *str, const QFile &file, int line
return char(ret);
}
void KConfigIniBackend::printableToString(BufferFragment *aString, const QFile &file, int line)
void KConfigIniBackend::printableToString(BufferFragment *aString, const QIODevice &file, int line)
{
if (aString->isEmpty() || aString->indexOf('\\') == -1) {
return;
......
......@@ -30,7 +30,7 @@ class QIODevice;
class KConfigIniBackend : public KConfigBackend
{
Q_OBJECT
Q_OBJECT
private:
QLockFile *lockFile;
......@@ -43,10 +43,16 @@ public:
ParseInfo parseConfig(const QByteArray &locale,
KEntryMap &entryMap,
ParseOptions options) override;
ParseInfo parseConfig(const QByteArray &locale,
KEntryMap &entryMap,
ParseOptions options,
bool merging);
ParseInfo parseConfigIO(QIODevice &file,
const QByteArray &locale,
KEntryMap &entryMap,
ParseOptions options,
bool merging);
bool writeConfig(const QByteArray &locale, KEntryMap &entryMap,
WriteOptions options) override;
......@@ -59,6 +65,11 @@ public:
void unlock() override;
bool isLocked() const override;
void writeEntries(const QByteArray &locale, QIODevice &file, const KEntryMap &map);
void writeEntries(const QByteArray &locale, QIODevice &file, const KEntryMap &map,
bool defaultGroup, bool &firstEntry);
protected:
enum StringType {
......@@ -68,14 +79,11 @@ protected:
};
// Warning: this modifies data in-place. Other BufferFragment objects referencing the same buffer
// fragment will get their data modified too.
static void printableToString(BufferFragment *aString, const QFile &file, int line);
static void printableToString(BufferFragment *aString, const QIODevice &file, int line);
static QByteArray stringToPrintable(const QByteArray &aString, StringType type);
static char charFromHex(const char *str, const QFile &file, int line);
static QString warningProlog(const QFile &file, int line);
static char charFromHex(const char *str, const QIODevice &file, int line);
static QString warningProlog(const QIODevice &file, int line);
void writeEntries(const QByteArray &locale, QIODevice &file, const KEntryMap &map);
void writeEntries(const QByteArray &locale, QIODevice &file, const KEntryMap &map,
bool defaultGroup, bool &firstEntry);
};
#endif // KCONFIGINI_P_H
......@@ -17,10 +17,10 @@
* Boston, MA 02110-1301, USA.
*/
#include "TestFolderStorage.h"
#include "TestTagLoader.h"
#include <QTest>
#include <KisFolderStorage.h>
#include <KisTagLoader.h>
#include <KisResourceLoader.h>
#include <KoResource.h>
#include <KisResourceLoaderRegistry.h>
......@@ -28,32 +28,34 @@
#ifndef FILES_DATA_DIR
#error "FILES_DATA_DIR not set. A directory with the data used for testing installing resources"
#endif
class Dummy : public KoResource {
public:
Dummy(const QString &f) : KoResource(f) {}
bool load() override { return true; }
bool loadFromDevice(QIODevice *) override { return true; }
bool save() override { return true; }
};
void TestFolderStorage ::testStorage()
void TestTagLoader ::testLoadTag()
{
KisFolderStorage folderStorage(QString(FILES_DATA_DIR));
KisResourceLoaderRegistry::instance()->add("brushes", new KisResourceLoader<Dummy>("dummy", "brushes", QStringList() << "image/x-gimp-brush"));
QSharedPointer<KisResourceStorage::ResourceIterator> iter = folderStorage.resources("brushes");
QVERIFY(iter->hasNext());
int count = 0;
while (iter->hasNext()) {
qDebug() << iter->url() << iter->type() << iter->lastModified();
iter->next();
count++;
KisTagLoader tagLoader;
QFile f(QString(FILES_DATA_DIR) + "/paintoppresets/test.deskop");
f.open(QFile::ReadOnly);
{
bool r = tagLoader.load(f);
f.close();
QVERIFY(r);
QVERIFY(tagLoader.name() == "* Favorites");
QVERIFY(tagLoader.comment() == "Your favorite brush presets");
QVERIFY(tagLoader.url() == "* Favorites");
}
QVERIFY(count == 1);
QLocale nl(QLocale::Dutch, QLocale::Netherlands);
QLocale::setDefault(nl);
{
bool r = tagLoader.load(f);
f.close();
QVERIFY(r);
QVERIFY(tagLoader.name() == "* Favorieten");
QVERIFY(tagLoader.comment() == "Jouw favoriete penseel presets");
QVERIFY(tagLoader.url() == "* Favorites");
}
}
QTEST_MAIN(TestFolderStorage)
QTEST_MAIN(TestTagLoader)
......@@ -17,16 +17,16 @@
* Boston, MA 02110-1301, USA.
*/
#ifndef TESTFOLDERSTORAGE_H
#define TESTFOLDERSTORAGE_H
#ifndef TESTTAGLOADER_H
#define TESTTAGLOADER_H
#include <QObject>
class TestFolderStorage : public QObject
class TestTagLoader : public QObject
{
Q_OBJECT
private Q_SLOTS:
void testStorage();
void testLoadTag();
private:
};
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment