2007-11-03 02:50:57 +01:00
|
|
|
/******************************************************************************
|
|
|
|
* Copyright 2007 by Bertjan Broeksema <b.broeksema@kdemail.net> *
|
|
|
|
* *
|
|
|
|
* This library is free software; you can redistribute it and/or *
|
|
|
|
* modify it under the terms of the GNU Library General Public *
|
|
|
|
* License as published by the Free Software Foundation; either *
|
|
|
|
* version 2 of the License, or (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
* This library is distributed in the hope that it will be useful, *
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
|
|
|
|
* Library General Public License for more details. *
|
|
|
|
* *
|
|
|
|
* You should have received a copy of the GNU Library General Public License *
|
|
|
|
* along with this library; see the file COPYING.LIB. If not, write to *
|
|
|
|
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, *
|
|
|
|
* Boston, MA 02110-1301, USA. *
|
|
|
|
*******************************************************************************/
|
|
|
|
|
|
|
|
#include "plasmoidpackagetest.h"
|
|
|
|
|
|
|
|
#include <QDir>
|
|
|
|
#include <QFile>
|
2007-12-02 13:04:57 +01:00
|
|
|
#include <KZip>
|
|
|
|
|
2008-02-26 21:48:02 +01:00
|
|
|
#include "plasma/applet.h"
|
2007-12-02 13:04:57 +01:00
|
|
|
#include "plasma/packagemetadata.h"
|
2007-11-03 02:50:57 +01:00
|
|
|
|
|
|
|
void PlasmoidPackageTest::init()
|
|
|
|
{
|
|
|
|
mPackage = QString("Package");
|
2008-06-18 01:21:39 +02:00
|
|
|
mPackageRoot = QDir::homePath() + "/.kde-unit-test/packageRoot/";
|
2008-02-26 21:48:02 +01:00
|
|
|
ps = Plasma::Applet::packageStructure();
|
2007-11-03 02:50:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void PlasmoidPackageTest::cleanup()
|
|
|
|
{
|
2008-06-18 01:21:39 +02:00
|
|
|
if (p) {
|
2007-11-09 20:03:50 +01:00
|
|
|
delete p;
|
|
|
|
p = 0;
|
|
|
|
}
|
2007-11-08 21:27:43 +01:00
|
|
|
|
2007-11-03 02:50:57 +01:00
|
|
|
// Clean things up.
|
2007-11-08 21:27:43 +01:00
|
|
|
QDir local = QDir::homePath() + QLatin1String("/.kde-unit-test/packageRoot/");
|
2008-06-18 01:21:39 +02:00
|
|
|
foreach(const QString &dir, local.entryList(QDir::Dirs)) {
|
2007-12-02 13:04:57 +01:00
|
|
|
removeDir(QLatin1String("packageRoot/" + dir.toLatin1() + "/contents/code"));
|
|
|
|
removeDir(QLatin1String("packageRoot/" + dir.toLatin1() + "/contents/images"));
|
|
|
|
removeDir(QLatin1String("packageRoot/" + dir.toLatin1() + "/contents"));
|
2007-11-08 21:27:43 +01:00
|
|
|
removeDir(QLatin1String("packageRoot/" + dir.toLatin1()));
|
|
|
|
}
|
|
|
|
|
2007-11-03 02:50:57 +01:00
|
|
|
QDir().rmpath(QDir::homePath() + "/.kde-unit-test/packageRoot");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Copied from ktimezonetest.h
|
|
|
|
void PlasmoidPackageTest::removeDir(const QString &subdir)
|
|
|
|
{
|
|
|
|
QDir local = QDir::homePath() + QLatin1String("/.kde-unit-test/") + subdir;
|
|
|
|
foreach(const QString &file, local.entryList(QDir::Files))
|
|
|
|
if(!local.remove(file))
|
|
|
|
qWarning("%s: removing failed", qPrintable( file ));
|
|
|
|
QCOMPARE((int)local.entryList(QDir::Files).count(), 0);
|
|
|
|
local.cdUp();
|
|
|
|
QString subd = subdir;
|
|
|
|
subd.remove(QRegExp("^.*/"));
|
|
|
|
local.rmpath(subd);
|
|
|
|
}
|
|
|
|
|
2007-11-08 21:27:43 +01:00
|
|
|
void PlasmoidPackageTest::createTestPackage(const QString &packageName)
|
|
|
|
{
|
2007-11-09 20:03:50 +01:00
|
|
|
QDir pRoot(mPackageRoot);
|
2007-11-08 21:27:43 +01:00
|
|
|
// Create the root and package dir.
|
2007-11-09 20:03:50 +01:00
|
|
|
if(!pRoot.exists())
|
|
|
|
{
|
|
|
|
QVERIFY(QDir().mkpath(mPackageRoot));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create the package dir
|
2007-11-08 21:27:43 +01:00
|
|
|
QVERIFY(QDir().mkpath(mPackageRoot + "/" + packageName));
|
|
|
|
|
2007-11-08 23:13:13 +01:00
|
|
|
// Create the metadata.desktop file
|
|
|
|
QFile file(mPackageRoot + "/" + packageName + "/metadata.desktop");
|
|
|
|
|
|
|
|
QVERIFY(file.open(QIODevice::WriteOnly | QIODevice::Text));
|
|
|
|
|
|
|
|
QTextStream out(&file);
|
|
|
|
out << "[Desktop Entry]\n";
|
|
|
|
out << "Name=" << packageName << "\n";
|
2007-11-09 20:03:50 +01:00
|
|
|
out << "X-KDE-PluginInfo-Name=" << packageName << "\n";
|
2007-11-08 23:13:13 +01:00
|
|
|
file.flush();
|
|
|
|
file.close();
|
|
|
|
|
2007-11-08 21:27:43 +01:00
|
|
|
// Create the code dir.
|
2007-12-02 13:04:57 +01:00
|
|
|
QVERIFY(QDir().mkpath(mPackageRoot + "/" + packageName + "/contents/code"));
|
2007-11-08 21:27:43 +01:00
|
|
|
|
|
|
|
// Create the main file.
|
2007-12-02 13:04:57 +01:00
|
|
|
file.setFileName(mPackageRoot + "/" + packageName + "/contents/code/main");
|
2007-11-08 21:27:43 +01:00
|
|
|
QVERIFY(file.open(QIODevice::WriteOnly | QIODevice::Text));
|
|
|
|
|
|
|
|
out << "THIS IS A PLASMOID SCRIPT.....";
|
|
|
|
file.flush();
|
|
|
|
file.close();
|
|
|
|
|
|
|
|
// Now we have a minimal plasmoid package which is valid. Let's add some
|
|
|
|
// files to it for test purposes.
|
|
|
|
|
|
|
|
// Create the images dir.
|
2007-12-02 13:04:57 +01:00
|
|
|
QVERIFY(QDir().mkpath(mPackageRoot + "/" + packageName + "/contents/images"));
|
|
|
|
file.setFileName(mPackageRoot + "/" + packageName + "/contents/images/image-1.svg");
|
2007-11-08 21:27:43 +01:00
|
|
|
|
|
|
|
QVERIFY(file.open(QIODevice::WriteOnly | QIODevice::Text));
|
|
|
|
|
|
|
|
out << "<svg>This is a test image</svg>";
|
|
|
|
file.flush();
|
|
|
|
file.close();
|
|
|
|
|
2007-12-02 13:04:57 +01:00
|
|
|
file.setFileName(mPackageRoot + "/" + packageName + "/contents/images/image-2.svg");
|
2007-11-08 21:27:43 +01:00
|
|
|
|
|
|
|
QVERIFY(file.open(QIODevice::WriteOnly | QIODevice::Text));
|
|
|
|
|
|
|
|
out.setDevice(&file);
|
|
|
|
out << "<svg>This is another test image</svg>";
|
|
|
|
file.flush();
|
|
|
|
file.close();
|
|
|
|
}
|
|
|
|
|
2007-11-03 02:50:57 +01:00
|
|
|
void PlasmoidPackageTest::isValid()
|
|
|
|
{
|
2008-02-26 21:48:02 +01:00
|
|
|
p = new Plasma::Package(mPackageRoot, mPackage, ps);
|
2007-11-08 21:27:43 +01:00
|
|
|
|
2007-11-03 02:50:57 +01:00
|
|
|
// A PlasmoidPackage is valid when:
|
|
|
|
// - The package root exists.
|
|
|
|
// - The package root consists an file named "code/main"
|
|
|
|
QVERIFY(!p->isValid());
|
2007-11-08 23:13:13 +01:00
|
|
|
|
2007-11-03 02:50:57 +01:00
|
|
|
// Create the root and package dir.
|
|
|
|
QVERIFY(QDir().mkpath(mPackageRoot));
|
|
|
|
QVERIFY(QDir().mkpath(mPackageRoot + "/" + mPackage));
|
2007-11-08 23:13:13 +01:00
|
|
|
|
2007-11-03 02:50:57 +01:00
|
|
|
// Should still be invalid.
|
|
|
|
delete p;
|
2008-02-26 21:48:02 +01:00
|
|
|
p = new Plasma::Package(mPackageRoot, mPackage, ps);
|
2007-11-03 02:50:57 +01:00
|
|
|
QVERIFY(!p->isValid());
|
2007-11-08 23:13:13 +01:00
|
|
|
|
|
|
|
// Create the metadata.desktop file.
|
|
|
|
QFile file(mPackageRoot + "/" + mPackage + "/metadata.desktop");
|
|
|
|
QVERIFY(file.open(QIODevice::WriteOnly | QIODevice::Text));
|
|
|
|
|
|
|
|
QTextStream out(&file);
|
2007-12-02 13:04:57 +01:00
|
|
|
out << "[Desktop Entry]\n";
|
|
|
|
out << "Name=test\n";
|
|
|
|
out << "Description=Just a test desktop file";
|
2007-11-08 23:13:13 +01:00
|
|
|
file.flush();
|
|
|
|
file.close();
|
|
|
|
|
2007-11-03 02:50:57 +01:00
|
|
|
// Create the code dir.
|
2007-12-02 13:04:57 +01:00
|
|
|
QVERIFY(QDir().mkpath(mPackageRoot + "/" + mPackage + "/contents/code"));
|
2007-11-08 23:13:13 +01:00
|
|
|
|
2007-11-03 02:50:57 +01:00
|
|
|
// No main file yet so should still be invalid.
|
|
|
|
delete p;
|
2008-02-26 21:48:02 +01:00
|
|
|
p = new Plasma::Package(mPackageRoot, mPackage, ps);
|
2007-11-03 02:50:57 +01:00
|
|
|
QVERIFY(!p->isValid());
|
2007-11-08 23:13:13 +01:00
|
|
|
|
2007-11-03 02:50:57 +01:00
|
|
|
// Create the main file.
|
2007-12-02 13:04:57 +01:00
|
|
|
file.setFileName(mPackageRoot + "/" + mPackage + "/contents/code/main");
|
2007-11-03 02:50:57 +01:00
|
|
|
QVERIFY(file.open(QIODevice::WriteOnly | QIODevice::Text));
|
2007-11-08 23:13:13 +01:00
|
|
|
|
|
|
|
out.setDevice(&file);
|
2007-11-03 02:50:57 +01:00
|
|
|
out << "THIS IS A PLASMOID SCRIPT.....";
|
|
|
|
file.flush();
|
|
|
|
file.close();
|
2007-11-08 23:13:13 +01:00
|
|
|
|
2007-11-03 02:50:57 +01:00
|
|
|
// Main file exists so should be valid now.
|
|
|
|
delete p;
|
2008-02-26 21:48:02 +01:00
|
|
|
p = new Plasma::Package(mPackageRoot, mPackage, ps);
|
2007-11-03 02:50:57 +01:00
|
|
|
QVERIFY(p->isValid());
|
|
|
|
}
|
|
|
|
|
2007-11-03 13:22:21 +01:00
|
|
|
void PlasmoidPackageTest::filePath()
|
|
|
|
{
|
|
|
|
// Package::filePath() returns
|
|
|
|
// - {package_root}/{package_name}/path/to/file if the file exists
|
|
|
|
// - QString() otherwise.
|
2008-02-26 21:48:02 +01:00
|
|
|
p = new Plasma::Package(mPackageRoot, mPackage, ps);
|
2007-11-03 13:22:21 +01:00
|
|
|
|
|
|
|
QCOMPARE(p->filePath("scripts", "main"), QString());
|
|
|
|
|
2007-12-02 13:04:57 +01:00
|
|
|
QVERIFY(QDir().mkpath(mPackageRoot + "/" + mPackage + "/contents/code"));
|
|
|
|
QFile file(mPackageRoot + "/" + mPackage + "/contents/code/main");
|
2007-11-03 13:22:21 +01:00
|
|
|
QVERIFY(file.open(QIODevice::WriteOnly | QIODevice::Text));
|
|
|
|
|
|
|
|
QTextStream out(&file);
|
|
|
|
out << "THIS IS A PLASMOID SCRIPT.....";
|
|
|
|
file.flush();
|
|
|
|
file.close();
|
|
|
|
|
|
|
|
// The package is valid by now so a path for code/main should get returned.
|
|
|
|
delete p;
|
2008-02-26 21:48:02 +01:00
|
|
|
p = new Plasma::Package(mPackageRoot, mPackage, ps);
|
2007-11-03 13:22:21 +01:00
|
|
|
|
2007-12-02 13:04:57 +01:00
|
|
|
QString path = mPackageRoot + "/" + mPackage + "/contents/code/main";
|
2007-11-03 13:22:21 +01:00
|
|
|
|
|
|
|
// Two ways to get the same info.
|
|
|
|
// 1. Give the file type which refers to a class of files (a directory) in
|
|
|
|
// the package strucutre and the file name.
|
|
|
|
// 2. Give the file type which refers to a file in the package structure.
|
|
|
|
//
|
|
|
|
// NOTE: scripts, main and mainscript are defined in packages.cpp and are
|
|
|
|
// specific for a PlasmoidPackag.
|
|
|
|
QCOMPARE(p->filePath("scripts", "main"), path);
|
|
|
|
QCOMPARE(p->filePath("mainscript"), path);
|
|
|
|
}
|
|
|
|
|
2007-11-08 21:27:43 +01:00
|
|
|
void PlasmoidPackageTest::entryList()
|
|
|
|
{
|
|
|
|
QString packageName("SomePlasmoid");
|
|
|
|
|
|
|
|
// Create a package named @p packageName which is valid and has some images.
|
|
|
|
createTestPackage(packageName);
|
|
|
|
|
|
|
|
// Create a package object and verify that it is valid.
|
2008-02-26 21:48:02 +01:00
|
|
|
p = new Plasma::Package(mPackageRoot, packageName, ps);
|
2007-11-08 21:27:43 +01:00
|
|
|
QVERIFY(p->isValid());
|
|
|
|
|
|
|
|
// Now we have a valid package that should contain the following files in
|
|
|
|
// given filetypes:
|
|
|
|
// fileTye - Files
|
|
|
|
// scripts - {"main"}
|
|
|
|
// images - {"image-1.svg", "image-2.svg"}
|
|
|
|
QStringList files = p->entryList("scripts");
|
|
|
|
QCOMPARE(files.size(), 1);
|
|
|
|
QVERIFY(files.contains("main"));
|
|
|
|
|
|
|
|
files = p->entryList("images");
|
|
|
|
QCOMPARE(files.size(), 2);
|
|
|
|
QVERIFY(files.contains("image-1.svg"));
|
|
|
|
QVERIFY(files.contains("image-2.svg"));
|
|
|
|
}
|
|
|
|
|
2007-11-09 20:03:50 +01:00
|
|
|
void PlasmoidPackageTest::knownPackages()
|
|
|
|
{
|
|
|
|
// Don't do strange things when package root doesn't exists.
|
2007-11-26 20:16:40 +01:00
|
|
|
QDir pRoot = QDir(mPackageRoot + "blah");
|
2007-11-09 20:03:50 +01:00
|
|
|
QVERIFY(!pRoot.exists());
|
2008-02-26 21:48:02 +01:00
|
|
|
p = new Plasma::Package(mPackageRoot + "blah", mPackage, ps);
|
2008-06-18 01:21:39 +02:00
|
|
|
QCOMPARE(Plasma::Package::listInstalled(mPackageRoot), QStringList());
|
2007-11-09 20:03:50 +01:00
|
|
|
delete p;
|
|
|
|
|
|
|
|
// Don't do strange things when an empty package root exists
|
|
|
|
QVERIFY(QDir().mkpath(mPackageRoot));
|
|
|
|
//QVERIFY(pRoot.exists());
|
2008-02-26 21:48:02 +01:00
|
|
|
p = new Plasma::Package(mPackageRoot, mPackage, ps);
|
2008-06-18 01:21:39 +02:00
|
|
|
QCOMPARE(Plasma::Package::listInstalled(mPackageRoot), QStringList());
|
2007-11-09 20:03:50 +01:00
|
|
|
delete p;
|
|
|
|
|
|
|
|
// Do not return a directory as package if it has no metadata.desktop file
|
|
|
|
QVERIFY(QDir().mkpath(mPackageRoot + "/invalid_plasmoid"));
|
2008-02-26 21:48:02 +01:00
|
|
|
p = new Plasma::Package(mPackageRoot, mPackage, ps);
|
2008-06-18 01:21:39 +02:00
|
|
|
QCOMPARE(Plasma::Package::listInstalled(mPackageRoot), QStringList());
|
2007-11-09 20:03:50 +01:00
|
|
|
delete p;
|
|
|
|
|
|
|
|
// Let's add a valid package and see what happens.
|
|
|
|
QString plamoid1("a_valid_plasmoid");
|
|
|
|
createTestPackage(plamoid1);
|
2008-02-26 21:48:02 +01:00
|
|
|
p = new Plasma::Package(mPackageRoot, mPackage, ps);
|
2007-11-09 20:03:50 +01:00
|
|
|
|
2008-06-18 01:21:39 +02:00
|
|
|
QStringList packages = Plasma::Package::listInstalled(mPackageRoot);
|
2007-11-09 20:03:50 +01:00
|
|
|
QCOMPARE(packages.size(), 1);
|
|
|
|
QVERIFY(packages.contains(plamoid1));
|
|
|
|
|
|
|
|
// Ok.... one more valid package.
|
2008-06-18 01:21:39 +02:00
|
|
|
QString plamoid2("another_valid_plasmoid");
|
2007-11-09 20:03:50 +01:00
|
|
|
createTestPackage(plamoid2);
|
2008-02-26 21:48:02 +01:00
|
|
|
p = new Plasma::Package(mPackageRoot, mPackage, ps);
|
2007-11-09 20:03:50 +01:00
|
|
|
|
2008-06-18 01:21:39 +02:00
|
|
|
packages = Plasma::Package::listInstalled(mPackageRoot);
|
2007-11-09 20:03:50 +01:00
|
|
|
QCOMPARE(packages.size(), 2);
|
|
|
|
QVERIFY(packages.contains(plamoid1));
|
|
|
|
QVERIFY(packages.contains(plamoid2));
|
|
|
|
}
|
|
|
|
|
2007-11-26 20:16:40 +01:00
|
|
|
void PlasmoidPackageTest::metadata()
|
|
|
|
{
|
|
|
|
QString plasmoid("plasmoid_with_metadata");
|
|
|
|
createTestPackage(plasmoid);
|
|
|
|
|
|
|
|
QString path = mPackageRoot + '/' + plasmoid + "/metadata.desktop";
|
2008-02-26 21:48:02 +01:00
|
|
|
p = new Plasma::Package(mPackageRoot, plasmoid, ps);
|
2007-12-02 13:04:57 +01:00
|
|
|
const Plasma::PackageMetadata *metadata = p->metadata();
|
|
|
|
QVERIFY(metadata);
|
|
|
|
QCOMPARE(metadata->name(), plasmoid);
|
|
|
|
}
|
|
|
|
|
|
|
|
void PlasmoidPackageTest::createAndInstallPackage()
|
|
|
|
{
|
|
|
|
QString plasmoid("plasmoid_to_package");
|
|
|
|
createTestPackage(plasmoid);
|
|
|
|
|
|
|
|
QString packagePath = mPackageRoot + '/' + "package.zip";
|
|
|
|
Plasma::PackageMetadata metadata(
|
|
|
|
QString(KDESRCDIR) + "/packagemetadatatest.desktop");
|
2008-06-18 01:21:39 +02:00
|
|
|
QVERIFY(Plasma::Package::createPackage(metadata,
|
|
|
|
mPackageRoot + '/' + plasmoid + "/contents",
|
|
|
|
packagePath));
|
2007-12-02 13:04:57 +01:00
|
|
|
QVERIFY(QFile::exists(packagePath));
|
2008-06-18 01:21:39 +02:00
|
|
|
|
2007-12-02 13:04:57 +01:00
|
|
|
KZip package(packagePath);
|
|
|
|
QVERIFY(package.open(QIODevice::ReadOnly));
|
|
|
|
const KArchiveDirectory *dir = package.directory();
|
|
|
|
QVERIFY(dir);
|
|
|
|
QVERIFY(dir->entry("metadata.desktop"));
|
|
|
|
const KArchiveEntry *contentsEntry = dir->entry("contents");
|
|
|
|
QVERIFY(contentsEntry);
|
|
|
|
QVERIFY(contentsEntry->isDirectory());
|
|
|
|
const KArchiveDirectory *contents =
|
|
|
|
static_cast<const KArchiveDirectory *>(contentsEntry);
|
|
|
|
QVERIFY(contents->entry("code"));
|
|
|
|
QVERIFY(contents->entry("images"));
|
2008-06-18 01:21:39 +02:00
|
|
|
|
2008-09-27 02:17:07 +02:00
|
|
|
QVERIFY(Plasma::Package::installPackage(packagePath, mPackageRoot, "plasma-applet-"));
|
2007-12-02 13:04:57 +01:00
|
|
|
QString installedPackage = mPackageRoot + "/test";
|
2008-06-18 01:21:39 +02:00
|
|
|
|
2007-12-02 13:04:57 +01:00
|
|
|
QVERIFY(QFile::exists(installedPackage));
|
2008-06-18 01:21:39 +02:00
|
|
|
|
2008-02-26 21:48:02 +01:00
|
|
|
p = new Plasma::Package(installedPackage, ps);
|
2008-06-18 01:21:39 +02:00
|
|
|
QVERIFY(p->isValid());
|
2007-11-26 20:16:40 +01:00
|
|
|
}
|
|
|
|
|
2007-11-03 02:50:57 +01:00
|
|
|
QTEST_KDEMAIN(PlasmoidPackageTest, NoGUI)
|