Add PackageStructure::read, write and related tests.

CCMAIL:panel-devel@kde.org

svn path=/trunk/KDE/kdebase/workspace/libs/plasma/; revision=743970
This commit is contained in:
Paolo Capriotti 2007-12-02 13:16:39 +00:00
parent 7dbc6b9ef1
commit 49f0a32580
5 changed files with 178 additions and 0 deletions

View File

@ -20,6 +20,7 @@
#include "packagestructure.h"
#include <QMap>
#include <KConfigGroup>
namespace Plasma
{
@ -231,5 +232,58 @@ QStringList PackageStructure::mimetypes(const char* key) const
return it.value().mimetypes;
}
PackageStructure PackageStructure::read(const KConfigBase *config)
{
QString type = config->group("").readEntry("Type", "");
PackageStructure structure(type);
QStringList groups = config->groupList();
foreach (QString groupName, groups) {
QByteArray key = groupName.toAscii();
KConfigGroup entry = config->group(key);
QString path = entry.readEntry("Path", QString());
QString name = entry.readEntry("Name", QString());
QStringList mimetypes = entry.readEntry("Mimetypes", QStringList());
bool directory = entry.readEntry("Directory", false);
bool required = entry.readEntry("Required", false);
if (directory) {
structure.addDirectoryDefinition(key, path, name);
}
else {
structure.addFileDefinition(key, path, name);
}
structure.setMimetypes(key, mimetypes);
structure.setRequired(key, required);
}
return structure;
}
void PackageStructure::write(KConfigBase *config) const
{
config->group("").writeEntry("Type", type());
QMap<QByteArray, ContentStructure>::const_iterator it = d->contents.constBegin();
while (it != d->contents.constEnd()) {
KConfigGroup group = config->group(it.key());
group.writeEntry("Path", it.value().path);
group.writeEntry("Name", it.value().name);
if (!it.value().mimetypes.isEmpty()) {
group.writeEntry("Mimetypes", it.value().mimetypes);
}
if (it.value().directory) {
group.writeEntry("Directory", true);
}
if (it.value().required) {
group.writeEntry("Required", true);
}
++it;
}
}
} // Plasma namespace

View File

@ -24,6 +24,8 @@
#include <plasma/plasma_export.h>
class KConfigBase;
namespace Plasma
{
@ -176,6 +178,16 @@ public:
**/
PackageStructure& operator=(const PackageStructure& rhs);
/**
* Read a package structure from a config file.
*/
static PackageStructure read(const KConfigBase *config);
/**
* Write this package structure to a config file.
*/
void write(KConfigBase *config) const;
private:
class Private;
Private * const d;

View File

@ -19,8 +19,11 @@
#include "packagestructuretest.h"
#include <KConfig>
#include <KConfigGroup>
#include <KDebug>
#include "plasma/packagestructure.h"
#include "plasma/packages.cpp"
void PackageStructureTest::init()
@ -107,6 +110,75 @@ void PackageStructureTest::mimetypes()
QCOMPARE(ps->mimetypes("images"), mimetypes);
}
void PackageStructureTest::read()
{
QString structurePath = QString(KDESRCDIR) + "/plasmoidpackagerc";
KConfig config(structurePath);
Plasma::PackageStructure structure =
Plasma::PackageStructure::read(&config);
// check some names
QCOMPARE(structure.name("config"), i18n("Configuration Definitions"));
QCOMPARE(structure.name("mainscript"), i18n("Main Script File"));
// check some paths
QCOMPARE(structure.path("images"), QString("images"));
QCOMPARE(structure.path("mainscript"), QString("code/main"));
// compare files
QList<const char *> files;
files << "mainconfiggui" << "mainconfigxml" << "mainscript";
QList<const char *> psFiles = structure.files();
QCOMPARE(psFiles.count(), files.count());
for (int i = 0; i < files.count(); ++i) {
QCOMPARE(psFiles[i], files[i]);
}
// compare required files
QList<const char *> reqFiles = structure.requiredFiles();
QCOMPARE(reqFiles.count(), 1);
QCOMPARE(reqFiles[0], "mainscript");
// compare directories
QList <const char *> dirs;
dirs << "config" << "configui" << "images" << "scripts";
QList <const char *> psDirs = structure.directories();
QCOMPARE(psDirs.count(), dirs.count());
for (int i = 0; i < dirs.count(); i++) {
QCOMPARE(psDirs[i], dirs[i]);
}
QCOMPARE(structure.requiredDirectories().size(), 0);
}
void PackageStructureTest::write()
{
QString file1 = QDir::homePath() + "/.kde-unit-test/packagerc";
QString file2 = QString(KDESRCDIR) + "/plasmoidpackagerc";
KConfig config(file1);
ps->write(&config);
// check type
QCOMPARE(config.group("").readEntry("Type", QString()), QString("Plasmoid"));
// check groups
QStringList groups;
groups << "images" << "config" << "configui" << "scripts"
<< "mainconfiggui" << "mainconfigxml" << "mainscript";
groups.sort();
QCOMPARE(config.groupList(), groups);
// check scripts
KConfigGroup scripts = config.group("scripts");
QCOMPARE(scripts.readEntry("Path", QString()), QString("code"));
QCOMPARE(scripts.readEntry("Name", QString()), QString("Executable Scripts"));
QCOMPARE(scripts.readEntry("Mimetypes", QStringList()), QStringList() << "text/*");
QCOMPARE(scripts.readEntry("Directory", false), true);
QCOMPARE(scripts.readEntry("Required", false), false);
}
QTEST_KDEMAIN(PackageStructureTest, NoGUI)
//#include "packagestructuretest.moc"

View File

@ -41,6 +41,8 @@ private Q_SLOTS:
void name();
void required();
void mimetypes();
void read();
void write();
//TODO: add tests for copy construction

38
tests/plasmoidpackagerc Normal file
View File

@ -0,0 +1,38 @@
Type=Plasmoid
[images]
Path=images
Name=Images
Mimetypes=image/svg+xml,image/png,image/jpeg
Directory=true
[config]
Path=config/xml
Name=Configuration Definitions
Mimetypes=text/xml
Directory=true
[configui]
Path=config/ui
Name=Configuration UI
Mimetypes=text/xml
Directory=true
[scripts]
Path=code
Name=Executable Scripts
Mimetypes=text/*
Directory=true
[mainconfiggui]
Path=config/ui/main.ui
Name=Main Config UI File
[mainconfigxml]
Path=config/ui/main.xml
Name=Configuration XML File
[mainscript]
Path=code/main
Name=Main Script File
Required=true