2020-08-13 21:08:54 +02:00
|
|
|
/*
|
|
|
|
SPDX-FileCopyrightText: 2007 Bertjan Broeksema <b.broeksema@kdemail.net>
|
|
|
|
|
|
|
|
SPDX-License-Identifier: LGPL-2.0-or-later
|
|
|
|
*/
|
2008-11-04 00:08:39 +01:00
|
|
|
|
|
|
|
#include "plasmoidpackagetest.h"
|
2009-09-21 23:37:44 +02:00
|
|
|
#include "../config-plasma.h"
|
2008-11-04 00:08:39 +01:00
|
|
|
|
2021-03-05 19:15:32 +01:00
|
|
|
#include <KJob>
|
2008-11-04 00:08:39 +01:00
|
|
|
#include <QDir>
|
|
|
|
#include <QFile>
|
2012-12-18 12:18:08 +01:00
|
|
|
#include <QStandardPaths>
|
2021-03-05 19:15:32 +01:00
|
|
|
#include <kzip.h>
|
2008-11-04 00:08:39 +01:00
|
|
|
|
2013-07-29 19:05:59 +02:00
|
|
|
#include <QDebug>
|
2012-12-09 14:33:24 +01:00
|
|
|
|
2011-07-21 14:48:12 +02:00
|
|
|
#include "applet.h"
|
|
|
|
#include "pluginloader.h"
|
2008-11-04 00:08:39 +01:00
|
|
|
|
2012-12-18 12:18:08 +01:00
|
|
|
void PlasmoidPackageTest::initTestCase()
|
|
|
|
{
|
2019-02-28 13:55:40 +01:00
|
|
|
QStandardPaths::setTestModeEnabled(true);
|
2012-12-18 12:18:08 +01:00
|
|
|
}
|
|
|
|
|
2008-11-04 00:08:39 +01:00
|
|
|
void PlasmoidPackageTest::init()
|
|
|
|
{
|
2013-07-29 19:05:59 +02:00
|
|
|
qDebug() << "PlasmoidPackage::init()";
|
2011-07-15 12:58:56 +02:00
|
|
|
m_package = QString("Package");
|
2012-12-18 12:18:08 +01:00
|
|
|
m_packageRoot = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + "/packageRoot";
|
2011-07-21 14:48:12 +02:00
|
|
|
m_defaultPackage = Plasma::PluginLoader::self()->loadPackage("Plasma/Applet");
|
2011-07-15 14:09:19 +02:00
|
|
|
cleanup(); // to prevent previous runs from interfering with this one
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void PlasmoidPackageTest::cleanup()
|
|
|
|
{
|
2013-07-29 19:05:59 +02:00
|
|
|
qDebug() << "cleaning up";
|
2008-11-04 00:08:39 +01:00
|
|
|
// Clean things up.
|
2012-12-18 12:18:08 +01:00
|
|
|
QDir(m_packageRoot).removeRecursively();
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void PlasmoidPackageTest::createTestPackage(const QString &packageName)
|
|
|
|
{
|
2013-07-29 19:05:59 +02:00
|
|
|
qDebug() << "Create test package" << m_packageRoot;
|
2011-07-15 12:58:56 +02:00
|
|
|
QDir pRoot(m_packageRoot);
|
2008-11-04 00:08:39 +01:00
|
|
|
// Create the root and package dir.
|
2009-09-21 23:37:44 +02:00
|
|
|
if (!pRoot.exists()) {
|
2011-07-15 12:58:56 +02:00
|
|
|
QVERIFY(QDir().mkpath(m_packageRoot));
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create the package dir
|
2011-07-15 12:58:56 +02:00
|
|
|
QVERIFY(QDir().mkpath(m_packageRoot + "/" + packageName));
|
2013-07-29 19:05:59 +02:00
|
|
|
qDebug() << "Created" << (m_packageRoot + "/" + packageName);
|
2008-11-04 00:08:39 +01:00
|
|
|
|
|
|
|
// Create the metadata.desktop file
|
2011-07-15 12:58:56 +02:00
|
|
|
QFile file(m_packageRoot + "/" + packageName + "/metadata.desktop");
|
2008-11-04 00:08:39 +01:00
|
|
|
|
|
|
|
QVERIFY(file.open(QIODevice::WriteOnly | QIODevice::Text));
|
|
|
|
|
|
|
|
QTextStream out(&file);
|
|
|
|
out << "[Desktop Entry]\n";
|
|
|
|
out << "Name=" << packageName << "\n";
|
|
|
|
out << "X-KDE-PluginInfo-Name=" << packageName << "\n";
|
|
|
|
file.flush();
|
|
|
|
file.close();
|
|
|
|
|
2013-07-29 19:05:59 +02:00
|
|
|
qDebug() << "OUT: " << packageName;
|
2012-12-09 14:33:24 +01:00
|
|
|
|
2013-02-15 16:31:26 +01:00
|
|
|
// Create the ui dir.
|
|
|
|
QVERIFY(QDir().mkpath(m_packageRoot + "/" + packageName + "/contents/ui"));
|
2008-11-04 00:08:39 +01:00
|
|
|
|
|
|
|
// Create the main file.
|
2013-02-15 16:31:26 +01:00
|
|
|
file.setFileName(m_packageRoot + "/" + packageName + "/contents/ui/main.qml");
|
2008-11-04 00:08:39 +01:00
|
|
|
QVERIFY(file.open(QIODevice::WriteOnly | QIODevice::Text));
|
|
|
|
|
|
|
|
out << "THIS IS A PLASMOID SCRIPT.....";
|
|
|
|
file.flush();
|
|
|
|
file.close();
|
|
|
|
|
2013-07-29 19:05:59 +02:00
|
|
|
qDebug() << "THIS IS A PLASMOID SCRIPT THING";
|
2011-05-12 05:46:35 +02:00
|
|
|
// Now we have a minimal plasmoid package which is valid. Let's add some
|
2008-11-04 00:08:39 +01:00
|
|
|
// files to it for test purposes.
|
|
|
|
|
|
|
|
// Create the images dir.
|
2011-07-15 12:58:56 +02:00
|
|
|
QVERIFY(QDir().mkpath(m_packageRoot + "/" + packageName + "/contents/images"));
|
|
|
|
file.setFileName(m_packageRoot + "/" + packageName + "/contents/images/image-1.svg");
|
2008-11-04 00:08:39 +01:00
|
|
|
|
|
|
|
QVERIFY(file.open(QIODevice::WriteOnly | QIODevice::Text));
|
|
|
|
|
|
|
|
out << "<svg>This is a test image</svg>";
|
|
|
|
file.flush();
|
|
|
|
file.close();
|
|
|
|
|
2011-07-15 12:58:56 +02:00
|
|
|
file.setFileName(m_packageRoot + "/" + packageName + "/contents/images/image-2.svg");
|
2008-11-04 00:08:39 +01:00
|
|
|
|
|
|
|
QVERIFY(file.open(QIODevice::WriteOnly | QIODevice::Text));
|
|
|
|
|
|
|
|
out.setDevice(&file);
|
|
|
|
out << "<svg>This is another test image</svg>";
|
|
|
|
file.flush();
|
|
|
|
file.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
void PlasmoidPackageTest::isValid()
|
|
|
|
{
|
2011-07-15 12:58:56 +02:00
|
|
|
Plasma::Package *p = new Plasma::Package(m_defaultPackage);
|
|
|
|
p->setPath(m_packageRoot + '/' + m_package);
|
2011-07-29 15:46:52 +02:00
|
|
|
#ifndef NDEBUG
|
2012-02-21 00:11:36 +01:00
|
|
|
qDebug() << "package path is" << p->path();
|
2011-07-29 15:46:52 +02:00
|
|
|
#endif
|
2008-11-04 00:08:39 +01:00
|
|
|
|
|
|
|
// A PlasmoidPackage is valid when:
|
|
|
|
// - The package root exists.
|
2013-02-15 16:31:26 +01:00
|
|
|
// - The package root consists an file named "ui/main.qml"
|
2008-11-04 00:08:39 +01:00
|
|
|
QVERIFY(!p->isValid());
|
|
|
|
|
|
|
|
// Create the root and package dir.
|
2011-07-15 12:58:56 +02:00
|
|
|
QVERIFY(QDir().mkpath(m_packageRoot));
|
|
|
|
QVERIFY(QDir().mkpath(m_packageRoot + "/" + m_package));
|
2008-11-04 00:08:39 +01:00
|
|
|
|
|
|
|
// Should still be invalid.
|
|
|
|
delete p;
|
2011-07-15 12:58:56 +02:00
|
|
|
p = new Plasma::Package(m_defaultPackage);
|
|
|
|
p->setPath(m_packageRoot + '/' + m_package);
|
2008-11-04 00:08:39 +01:00
|
|
|
QVERIFY(!p->isValid());
|
|
|
|
|
|
|
|
// Create the metadata.desktop file.
|
2011-07-15 12:58:56 +02:00
|
|
|
QFile file(m_packageRoot + "/" + m_package + "/metadata.desktop");
|
2008-11-04 00:08:39 +01:00
|
|
|
QVERIFY(file.open(QIODevice::WriteOnly | QIODevice::Text));
|
|
|
|
|
|
|
|
QTextStream out(&file);
|
|
|
|
out << "[Desktop Entry]\n";
|
|
|
|
out << "Name=test\n";
|
|
|
|
out << "Description=Just a test desktop file";
|
|
|
|
file.flush();
|
|
|
|
file.close();
|
|
|
|
|
2013-02-15 16:31:26 +01:00
|
|
|
// Create the ui dir.
|
|
|
|
QVERIFY(QDir().mkpath(m_packageRoot + "/" + m_package + "/contents/ui"));
|
2008-11-04 00:08:39 +01:00
|
|
|
|
|
|
|
// No main file yet so should still be invalid.
|
|
|
|
delete p;
|
2011-07-15 12:58:56 +02:00
|
|
|
p = new Plasma::Package(m_defaultPackage);
|
|
|
|
p->setPath(m_packageRoot + '/' + m_package);
|
2008-11-04 00:08:39 +01:00
|
|
|
QVERIFY(!p->isValid());
|
|
|
|
|
|
|
|
// Create the main file.
|
2013-02-15 16:31:26 +01:00
|
|
|
file.setFileName(m_packageRoot + "/" + m_package + "/contents/ui/main.qml");
|
2008-11-04 00:08:39 +01:00
|
|
|
QVERIFY(file.open(QIODevice::WriteOnly | QIODevice::Text));
|
|
|
|
|
|
|
|
out.setDevice(&file);
|
2009-09-21 23:37:44 +02:00
|
|
|
out << "THIS IS A PLASMOID SCRIPT.....\n";
|
2008-11-04 00:08:39 +01:00
|
|
|
file.flush();
|
|
|
|
file.close();
|
|
|
|
|
2009-09-21 23:37:44 +02:00
|
|
|
file.setPermissions(QFile::ReadUser | QFile::WriteUser);
|
2008-11-04 00:08:39 +01:00
|
|
|
// Main file exists so should be valid now.
|
|
|
|
delete p;
|
2011-07-15 12:58:56 +02:00
|
|
|
p = new Plasma::Package(m_defaultPackage);
|
|
|
|
p->setPath(m_packageRoot + '/' + m_package);
|
2008-11-04 00:08:39 +01:00
|
|
|
QVERIFY(p->isValid());
|
2009-09-21 23:48:19 +02:00
|
|
|
QCOMPARE(p->contentsHash(), QString("db0b38c2b4fe21a9f37923cc25152340de055f6d"));
|
2011-07-15 12:58:56 +02:00
|
|
|
delete p;
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void PlasmoidPackageTest::filePath()
|
|
|
|
{
|
2012-12-09 14:33:24 +01:00
|
|
|
return;
|
2008-11-04 00:08:39 +01:00
|
|
|
// Package::filePath() returns
|
|
|
|
// - {package_root}/{package_name}/path/to/file if the file exists
|
|
|
|
// - QString() otherwise.
|
2011-07-15 12:58:56 +02:00
|
|
|
Plasma::Package *p = new Plasma::Package(m_defaultPackage);
|
|
|
|
p->setPath(m_packageRoot + '/' + m_package);
|
2008-11-04 00:08:39 +01:00
|
|
|
|
|
|
|
QCOMPARE(p->filePath("scripts", "main"), QString());
|
|
|
|
|
2013-02-15 16:31:26 +01:00
|
|
|
QVERIFY(QDir().mkpath(m_packageRoot + "/" + m_package + "/contents/ui/main.qml"));
|
|
|
|
QFile file(m_packageRoot + "/" + m_package + "/contents/ui/main.qml");
|
2008-11-04 00:08:39 +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;
|
2011-07-15 12:58:56 +02:00
|
|
|
p = new Plasma::Package(m_defaultPackage);
|
|
|
|
p->setPath(m_packageRoot + '/' + m_package);
|
2008-11-04 00:08:39 +01:00
|
|
|
|
2013-02-15 16:31:26 +01:00
|
|
|
const QString path = QFileInfo(m_packageRoot + "/" + m_package + "/contents/ui/main.qml").canonicalFilePath();
|
2008-11-04 00:08:39 +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 structure 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 PlasmoidPackage.
|
|
|
|
QCOMPARE(p->filePath("scripts", "main"), path);
|
|
|
|
QCOMPARE(p->filePath("mainscript"), path);
|
2011-07-15 12:58:56 +02:00
|
|
|
delete p;
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void PlasmoidPackageTest::entryList()
|
|
|
|
{
|
|
|
|
// Create a package named @p packageName which is valid and has some images.
|
2011-07-15 14:18:17 +02:00
|
|
|
createTestPackage(m_package);
|
2008-11-04 00:08:39 +01:00
|
|
|
|
|
|
|
// Create a package object and verify that it is valid.
|
2011-07-15 12:58:56 +02:00
|
|
|
Plasma::Package *p = new Plasma::Package(m_defaultPackage);
|
|
|
|
p->setPath(m_packageRoot + '/' + m_package);
|
2008-11-04 00:08:39 +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"));
|
|
|
|
delete p;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PlasmoidPackageTest::createAndInstallPackage()
|
|
|
|
{
|
2013-07-29 19:05:59 +02:00
|
|
|
qDebug() << " ";
|
|
|
|
qDebug() << " CreateAndInstall ";
|
2013-02-15 16:31:26 +01:00
|
|
|
createTestPackage("plasmoid_to_package");
|
|
|
|
const QString packagePath = m_packageRoot + '/' + "testpackage.plasmoid";
|
|
|
|
|
|
|
|
KZip creator(packagePath);
|
|
|
|
QVERIFY(creator.open(QIODevice::WriteOnly));
|
|
|
|
creator.addLocalDirectory(m_packageRoot + '/' + "plasmoid_to_package", ".");
|
|
|
|
creator.close();
|
2013-06-27 16:39:03 +02:00
|
|
|
KIO::NetAccess::del(QUrl::fromLocalFile(m_packageRoot + "/plasmoid_to_package"), 0);
|
2013-02-15 16:31:26 +01:00
|
|
|
|
|
|
|
QVERIFY(QFile::exists(packagePath));
|
|
|
|
|
|
|
|
KZip package(packagePath);
|
|
|
|
QVERIFY(package.open(QIODevice::ReadOnly));
|
|
|
|
const KArchiveDirectory *dir = package.directory();
|
2021-03-05 19:15:32 +01:00
|
|
|
QVERIFY(dir); //
|
2013-02-15 16:31:26 +01:00
|
|
|
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("ui"));
|
|
|
|
QVERIFY(contents->entry("images"));
|
2012-12-09 14:33:24 +01:00
|
|
|
|
|
|
|
m_defaultPackageStructure = new Plasma::PackageStructure(this);
|
|
|
|
Plasma::Package *p = new Plasma::Package(m_defaultPackageStructure);
|
2013-07-29 19:05:59 +02:00
|
|
|
qDebug() << "Installing " << archivePath;
|
2021-03-05 19:15:32 +01:00
|
|
|
// const QString packageRoot = "plasma/plasmoids/";
|
|
|
|
// const QString servicePrefix = "plasma-applet-";
|
2014-04-26 01:45:47 +02:00
|
|
|
KJob *job = p->install(archivePath, m_packageRoot);
|
2021-03-05 19:15:32 +01:00
|
|
|
connect(job, SIGNAL(finished(KJob *)), SLOT(packageInstalled(KJob *)));
|
2012-12-09 14:33:24 +01:00
|
|
|
|
2021-03-05 19:15:32 +01:00
|
|
|
// QVERIFY(p->isValid());
|
2012-12-09 14:33:24 +01:00
|
|
|
delete p;
|
|
|
|
}
|
2008-11-04 00:08:39 +01:00
|
|
|
|
2014-04-26 01:45:47 +02:00
|
|
|
void PlasmoidPackageTest::packageInstalled(KJob *j)
|
2012-12-09 14:33:24 +01:00
|
|
|
{
|
2013-07-29 19:05:59 +02:00
|
|
|
qDebug() << "!!!!!!!!!!!!!!!!!!!! package installed" << (j->error() == KJob::NoError);
|
2012-12-09 14:33:24 +01:00
|
|
|
QVERIFY(j->error() == KJob::NoError);
|
2021-03-05 19:15:32 +01:00
|
|
|
// QVERIFY(p->path());
|
2012-12-09 14:33:24 +01:00
|
|
|
|
|
|
|
Plasma::Package *p = new Plasma::Package(m_defaultPackageStructure);
|
2014-04-26 01:45:47 +02:00
|
|
|
KJob *jj = p->uninstall("org.kde.microblog-qml", m_packageRoot);
|
2021-03-05 19:15:32 +01:00
|
|
|
// QObject::disconnect(j, SIGNAL(finished(KJob*)), this, SLOT(packageInstalled(KJob*)));
|
|
|
|
connect(jj, SIGNAL(finished(KJob *)), SLOT(packageInstalled(KJob *)));
|
2012-12-09 14:33:24 +01:00
|
|
|
}
|
2008-11-04 00:08:39 +01:00
|
|
|
|
2014-04-26 01:45:47 +02:00
|
|
|
void PlasmoidPackageTest::packageUninstalled(KJob *j)
|
2012-12-09 14:33:24 +01:00
|
|
|
{
|
2013-07-29 19:05:59 +02:00
|
|
|
qDebug() << "!!!!!!!!!!!!!!!!!!!!! package uninstalled";
|
2012-12-09 14:33:24 +01:00
|
|
|
QVERIFY(j->error() == KJob::NoError);
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
|
2012-12-18 12:18:08 +01:00
|
|
|
QTEST_MAIN(PlasmoidPackageTest)
|