diff --git a/applets/systemtray/CMakeLists.txt b/applets/systemtray/CMakeLists.txt --- a/applets/systemtray/CMakeLists.txt +++ b/applets/systemtray/CMakeLists.txt @@ -27,3 +27,4 @@ install(TARGETS org.kde.plasma.private.systemtray DESTINATION ${KDE_INSTALL_PLUGINDIR}/plasma/applets) add_subdirectory(container) +add_subdirectory(tests) diff --git a/applets/systemtray/tests/CMakeLists.txt b/applets/systemtray/tests/CMakeLists.txt new file mode 100644 --- /dev/null +++ b/applets/systemtray/tests/CMakeLists.txt @@ -0,0 +1 @@ +add_subdirectory(statusnotifier) diff --git a/applets/systemtray/tests/statusnotifier/CMakeLists.txt b/applets/systemtray/tests/statusnotifier/CMakeLists.txt new file mode 100644 --- /dev/null +++ b/applets/systemtray/tests/statusnotifier/CMakeLists.txt @@ -0,0 +1,22 @@ +set(statusnotifiertest_SRCS + main.cpp + statusnotifiertest.cpp + pumpjob.cpp +) + +ki18n_wrap_ui(statusnotifiertest_SRCS statusnotifiertest.ui) + +add_executable(statusnotifiertest ${statusnotifiertest_SRCS}) + +target_link_libraries(statusnotifiertest + Qt5::Widgets + Qt5::Core + KF5::CoreAddons + KF5::KIOCore + KF5::Service + KF5::Notifications + KF5::I18n +) + +include(ECMMarkAsTest) +ecm_mark_as_test(statusnotifiertest) diff --git a/applets/systemtray/tests/statusnotifier/main.cpp b/applets/systemtray/tests/statusnotifier/main.cpp new file mode 100644 --- /dev/null +++ b/applets/systemtray/tests/statusnotifier/main.cpp @@ -0,0 +1,46 @@ +/* + * Copyright 2013 Sebastian Kügler + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2, + * or (at your option) any later version. + * + * This program 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 General Public License for more details + * + * You should have received a copy of the GNU Library General Public + * License along with this program; if not, write to the + * Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#include +#include + +#include +#include + +#include "statusnotifiertest.h" + +int main(int argc, char **argv) +{ + QApplication app(argc, argv); + QCommandLineParser parser; + + const QString description = QStringLiteral("Statusnotifier test app"); + const char version[] = "1.0"; + + app.setApplicationVersion(version); + parser.addVersionOption(); + parser.addHelpOption(); + parser.setApplicationDescription(description); + + StatusNotifierTest test; + int ex = test.runMain(); + app.exec(); + return ex; +} + diff --git a/applets/systemtray/tests/statusnotifier/pumpjob.h b/applets/systemtray/tests/statusnotifier/pumpjob.h new file mode 100644 --- /dev/null +++ b/applets/systemtray/tests/statusnotifier/pumpjob.h @@ -0,0 +1,56 @@ +/****************************************************************************** +* Copyright 2013 Sebastian Kügler * +* * +* 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. * +*******************************************************************************/ + +#ifndef PUMPJOB_H +#define PUMPJOB_H + +#include + +#include + +class PumpJobPrivate; + +class PumpJob : public KIO::Job +{ + Q_OBJECT + + public: + PumpJob(int interval = 0); + virtual ~PumpJob(); + + virtual void start(); + virtual bool doKill(); + virtual bool doSuspend(); + virtual bool doResume(); + + virtual bool isSuspended() const; + + void init(); + Q_SIGNALS: + void suspended(KJob *job); + void resumed(KJob *job); + +public Q_SLOTS: + void timeout(); + + private: + PumpJobPrivate* d; +}; + +#endif diff --git a/applets/systemtray/tests/statusnotifier/pumpjob.cpp b/applets/systemtray/tests/statusnotifier/pumpjob.cpp new file mode 100644 --- /dev/null +++ b/applets/systemtray/tests/statusnotifier/pumpjob.cpp @@ -0,0 +1,138 @@ +/****************************************************************************** +* Copyright 2013 Sebastian Kügler * +* * +* 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 "pumpjob.h" + +#include + +#include +#include +#include +#include + +static QTextStream cout(stdout); + +class PumpJobPrivate { +public: + QString name; + QString error; + + QTimer* timer; + int interval = 200; + + int counter = 0; + + bool suspended = false; +}; + +PumpJob::PumpJob(int interval) : + KIO::Job() +{ + d = new PumpJobPrivate; + + if (interval) { + d->interval = d->interval * interval; + } + KIO::getJobTracker()->registerJob(this); + + d->timer = new QTimer(this); + d->timer->setInterval(d->interval); + qDebug() << "Starting job with interval: " << d->interval; + + connect(d->timer, &QTimer::timeout, this, &PumpJob::timeout); + + init(); +} + +void PumpJob::init() +{ + emit description(this, i18n("Pump Job"), + qMakePair(i18n("Source"), QStringLiteral("this is the source")), + qMakePair(i18n("Destination"), QStringLiteral("destination, baby"))); + d->timer->start(); + +} + +PumpJob::~PumpJob() +{ + KIO::getJobTracker()->unregisterJob(this); + qDebug() << "Bye bye"; + delete d; +} + +void PumpJob::start() +{ + qDebug() << "Starting job / timer"; + d->timer->start(); +} + +bool PumpJob::doKill() +{ + qDebug() << "kill"; + emitResult(); + d->timer->stop(); + setError(KIO::ERR_USER_CANCELED); + setErrorText(QStringLiteral("You killed the job.")); + return KJob::doKill(); +} + +bool PumpJob::doResume() +{ + d->suspended = false; + qDebug() << "resume"; + d->timer->start(); + emit resumed(this); + return KJob::doResume(); +} + +bool PumpJob::isSuspended() const +{ + return d->suspended; +} + +bool PumpJob::doSuspend() +{ + d->suspended = true; + qDebug() << "suspend"; + d->timer->stop(); + emit suspended(this); + return KJob::doSuspend(); +} + +void PumpJob::timeout() +{ + d->counter++; + setPercent(d->counter); + emitSpeed(1024 * 1024 * d->counter / 70); // just something randomly changing + int seconds = (int)((d->interval * 100) - (d->interval * percent())) / 1000; + emit infoMessage(this, i18n("Testing kuiserver (%1 seconds remaining)", seconds), i18n("Testing kuiserver (%1 seconds remaining)", seconds)); + + qDebug() << "percent: " << percent() << " Seconds: " << seconds; + if (d->counter % 20 == 0) { + //qDebug() << "percent: " << percent() << " Seconds: " << seconds; + } + + if (d->counter >= 100) { + qDebug() << "Job done"; + emitResult(); + } +} + +#include "moc_pumpjob.cpp" + diff --git a/applets/systemtray/tests/statusnotifier/statusnotifiertest.h b/applets/systemtray/tests/statusnotifier/statusnotifiertest.h new file mode 100644 --- /dev/null +++ b/applets/systemtray/tests/statusnotifier/statusnotifiertest.h @@ -0,0 +1,63 @@ +/****************************************************************************** +* Copyright 2013 Sebastian Kügler * +* * +* 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. * +*******************************************************************************/ + +#ifndef STATUSNOTIFIERTEST_H +#define STATUSNOTIFIERTEST_H + +#include +#include +#include + +#include + +#include "ui_statusnotifiertest.h" + +class StatusNotifierTestPrivate; + +class StatusNotifierTest : public QDialog, public Ui_StatusNotifierTest +{ + Q_OBJECT + + public: + StatusNotifierTest(QWidget* parent = 0); + virtual ~StatusNotifierTest(); + + void init(); + void log(const QString &msg); + + public Q_SLOTS: + int runMain(); + void timeout(); + void updateUi(); + void updateNotifier(); + + void activateRequested (bool active, const QPoint &pos); + void scrollRequested (int delta, Qt::Orientation orientation); + void secondaryActivateRequested (const QPoint &pos); + + void enableJob(bool enable = true); + void setJobProgress(KJob *j, unsigned long v); + void result(KJob *job); + + + private: + StatusNotifierTestPrivate* d; +}; + +#endif diff --git a/applets/systemtray/tests/statusnotifier/statusnotifiertest.cpp b/applets/systemtray/tests/statusnotifier/statusnotifiertest.cpp new file mode 100644 --- /dev/null +++ b/applets/systemtray/tests/statusnotifier/statusnotifiertest.cpp @@ -0,0 +1,266 @@ +/****************************************************************************** +* Copyright 2013 Sebastian Kügler * +* * +* 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 "statusnotifiertest.h" +#include "pumpjob.h" + +#include +#include +#include +#include + +#include + +#include +#include + +#include +#include + +#include +#include + + +static QTextStream cout(stdout); + +class StatusNotifierTestPrivate { +public: + QString pluginName; + QTimer* timer; + int interval = 1500; + QStringList loglines; + + KStatusNotifierItem* systemNotifier; + PumpJob *job; + +}; + +StatusNotifierTest::StatusNotifierTest(QWidget* parent) : + QDialog(parent) +{ + d = new StatusNotifierTestPrivate; + d->job = 0; + + init(); + + setupUi(this); + connect(updateButton, &QPushButton::clicked, this, &StatusNotifierTest::updateNotifier); + connect(jobEnabledCheck, &QCheckBox::toggled, this, &StatusNotifierTest::enableJob); + updateUi(); + show(); + raise(); + log(QStringLiteral("started")); +} + +void StatusNotifierTest::init() +{ + d->systemNotifier = new KStatusNotifierItem(this); + //d->systemNotifier->setCategory(KStatusNotifierItem::SystemServices); + //d->systemNotifier->setCategory(KStatusNotifierItem::Hardware); + d->systemNotifier->setCategory(KStatusNotifierItem::Communications); + d->systemNotifier->setIconByName(QStringLiteral("plasma")); + d->systemNotifier->setStatus(KStatusNotifierItem::Active); + d->systemNotifier->setToolTipTitle(i18nc("tooltip title", "System Service Item")); + d->systemNotifier->setTitle(i18nc("title", "StatusNotifierTest")); + d->systemNotifier->setToolTipSubTitle(i18nc("tooltip subtitle", "Some explanation from the beach.")); + + connect(d->systemNotifier, &KStatusNotifierItem::activateRequested, + this, &StatusNotifierTest::activateRequested); + connect(d->systemNotifier, &KStatusNotifierItem::secondaryActivateRequested, + this, &StatusNotifierTest::secondaryActivateRequested); + connect(d->systemNotifier, &KStatusNotifierItem::scrollRequested, + this, &StatusNotifierTest::scrollRequested); + + auto menu = new QMenu(this); + menu->addAction(QIcon::fromTheme(QStringLiteral("document-edit")), QStringLiteral("action 1")); + menu->addAction(QIcon::fromTheme(QStringLiteral("mail-send")), QStringLiteral("action 2")); + auto subMenu = new QMenu(this); + subMenu->setTitle(QStringLiteral("Sub Menu")); + subMenu->addAction(QStringLiteral("subaction1")); + subMenu->addAction(QStringLiteral("subaction2")); + menu->addMenu(subMenu); + + d->systemNotifier->setContextMenu(menu); +} + +StatusNotifierTest::~StatusNotifierTest() +{ + delete d; +} + +void StatusNotifierTest::log(const QString& msg) +{ + qDebug() << "msg: " << msg; + d->loglines.prepend(msg); + + + logEdit->setText(d->loglines.join('\n')); +} + +void StatusNotifierTest::updateUi() +{ + if (!d->systemNotifier) { + return; + } + statusActive->setChecked(d->systemNotifier->status() == KStatusNotifierItem::Active); + statusPassive->setChecked(d->systemNotifier->status() == KStatusNotifierItem::Passive); + statusNeedsAttention->setChecked(d->systemNotifier->status() == KStatusNotifierItem::NeedsAttention); + + statusActive->setEnabled(!statusAuto->isChecked()); + statusPassive->setEnabled(!statusAuto->isChecked()); + statusNeedsAttention->setEnabled(!statusAuto->isChecked()); + + iconName->setText(d->systemNotifier->iconName()); + tooltipText->setText(d->systemNotifier->toolTipTitle()); + tooltipSubtext->setText(d->systemNotifier->toolTipSubTitle()); + +} + +void StatusNotifierTest::updateNotifier() +{ + //log("update"); + if (!enabledCheck->isChecked()) { + delete d->systemNotifier; + d->systemNotifier = 0; + return; + } else { + if (!d->systemNotifier) { + init(); + } + } + + + if (!d->systemNotifier) { + return; + } + if (statusAuto->isChecked()) { + d->timer->start(); + } else { + d->timer->stop(); + } + + KStatusNotifierItem::ItemStatus s = KStatusNotifierItem::Passive; + if (statusActive->isChecked()) { + s = KStatusNotifierItem::Active; + } else if (statusNeedsAttention->isChecked()) { + s = KStatusNotifierItem::NeedsAttention; + } + d->systemNotifier->setStatus(s); + + d->systemNotifier->setIconByName(iconName->text()); + + d->systemNotifier->setToolTip(iconName->text(), tooltipText->text(), tooltipSubtext->text()); + + updateUi(); +} + + + +int StatusNotifierTest::runMain() +{ + d->timer = new QTimer(this); + connect(d->timer, &QTimer::timeout, this, &StatusNotifierTest::timeout); + d->timer->setInterval(d->interval); + //d->timer->start(); + return 0; +} + +void StatusNotifierTest::timeout() +{ + if (!d->systemNotifier) { + return; + } + + if (d->systemNotifier->status() == KStatusNotifierItem::Passive) { + d->systemNotifier->setStatus(KStatusNotifierItem::Active); + qDebug() << " Now Active"; + } else if (d->systemNotifier->status() == KStatusNotifierItem::Active) { + d->systemNotifier->setStatus(KStatusNotifierItem::NeedsAttention); + qDebug() << " Now NeedsAttention"; + } else if (d->systemNotifier->status() == KStatusNotifierItem::NeedsAttention) { + d->systemNotifier->setStatus(KStatusNotifierItem::Passive); + qDebug() << " Now passive"; + } + updateUi(); +} + +void StatusNotifierTest::activateRequested(bool active, const QPoint& pos) +{ + Q_UNUSED(active); + Q_UNUSED(pos); + log(QStringLiteral("Activated")); +} + +void StatusNotifierTest::secondaryActivateRequested(const QPoint& pos) +{ + Q_UNUSED(pos); + log(QStringLiteral("secondaryActivateRequested")); +} + + +void StatusNotifierTest::scrollRequested(int delta, Qt::Orientation orientation) +{ + ///QString msg = QString("Scrolled " << " by: "); + //+ //QString msg = QString("Scrolled " + ((orientation == Qt::Horizontal) ? "Horizontally" : "Vertically") + " by: " //+ QString::number(delta)); + QString msg(QStringLiteral("Scrolled by ")); + msg.append(QString::number(delta)); + msg.append((orientation == Qt::Horizontal) ? " Horizontally" : " Vertically"); + log(msg); +} + + +// Jobs + +void StatusNotifierTest::enableJob(bool enable) +{ + qDebug() << "Job enabled." << enable; + if (enable) { + d->job = new PumpJob(speedSlider->value()); + //QObject::connect(d->job, &KJob::percent, this, &StatusNotifierTest::setJobProgress); + QObject::connect(d->job, SIGNAL(percent(KJob*, unsigned long)), this, SLOT(setJobProgress(KJob*, unsigned long))); + QObject::connect(d->job, &KJob::result, this, &StatusNotifierTest::result); + //d->job->start(); // KIO autostarts jobs + } else { + if (d->job) { + d->timer->stop(); + jobEnabledCheck->setChecked(Qt::Unchecked); + d->job->kill(); + } + } +} + +void StatusNotifierTest::setJobProgress(KJob *j, unsigned long v) +{ + Q_UNUSED(j) + jobProgressBar->setValue(v); +} + +void StatusNotifierTest::result(KJob* job) +{ + if (job->error()) { + qDebug() << "Job Error:" << job->errorText() << job->errorString(); + } else { + qDebug() << "Job finished successfully."; + } + jobEnabledCheck->setCheckState(Qt::Unchecked); +} + + +#include "moc_statusnotifiertest.cpp" + diff --git a/applets/systemtray/tests/statusnotifier/statusnotifiertest.ui b/applets/systemtray/tests/statusnotifier/statusnotifiertest.ui new file mode 100644 --- /dev/null +++ b/applets/systemtray/tests/statusnotifier/statusnotifiertest.ui @@ -0,0 +1,223 @@ + + + StatusNotifierTest + + + + 0 + 0 + 434 + 572 + + + + + 0 + 100 + + + + Stat&usNotifier Testap + + + + + + + + + + + 1 + + + + Stat&us Notifier + + + + 0 + + + + + <html><head/><body><p><span style=" font-weight:600;">Log</span></p></body></html> + + + + + + + + 0 + 120 + + + + true + + + + + + + <b>Status</b> + + + + + + + Enabled + + + true + + + + + + + Automatic + + + + + + + Passive + + + + + + + Ac&tive + + + + + + + &NeedsAttention + + + + + + + <b>Icon</b> + + + + + + + + + + <b>ToolTip</b> + + + + + + + + + + + + + + + + Update + + + + + + + + &Jobs + + + + 0 + + + + + <b>Job Control</b> + + + + + + + <b>Progress</b> + + + + + + + 0 + + + + + + + <b>Naming</b> + + + + + + + + + + + + + 1 + + + 10 + + + 10 + + + 5 + + + Qt::Horizontal + + + QSlider::TicksBelow + + + 1 + + + + + + + Job Started + + + + + + + + + + + +