Commit 9973f0a3 authored by Michael Heidelbach's avatar Michael Heidelbach

baloo-widgets: Add autotest for asychronously extracted data

Reviewers: #dolphin, elvisangelaccio

Reviewed By: #dolphin, elvisangelaccio

Subscribers: ngraham

Tags: #baloo

Differential Revision: https://phabricator.kde.org/D10149
parent 03d9abe7
......@@ -6,11 +6,13 @@ ecm_add_test(extractortest.cpp
TEST_NAME "extractortest"
LINK_LIBRARIES Qt5::Test
)
ecm_add_test(filemetadatawidgettest.cpp
TEST_NAME "filemetadatawidgettest"
ecm_add_tests(
filemetadatawidgettest.cpp
filemetadataitemcounttest.cpp
LINK_LIBRARIES KF5::KIOCore
KF5::KIOWidgets
KF5::KIOFileWidgets
KF5::ConfigCore
KF5::BalooWidgets
KF5::FileMetaData
Qt5::Test
......
/*
* This file is part of the KDE Baloo Project
* Copyright 2018 Michael Heidelbach <ottwolt@gmail.com>
*
* 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 of
* the License or (at your option) version 3 or any later version
* accepted by the membership of KDE e.V. (or its successor approved
* by the membership of KDE e.V.), which shall act as a proxy
* defined in Section 14 of version 3 of the license.
*
* 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 General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "filemetadataitemcounttest.h"
#include "config.h"
#include <QObject>
#include <QTest>
#include <QMap>
#include <QLabel>
#include <QSignalSpy>
#include <QMetaType>
#include <QProcess>
#include <QStandardPaths>
#include <QDebug>
#include <KFileItem>
#include <KRatingWidget>
#include <KConfig>
#include <KConfigGroup>
QTEST_MAIN(FileMetadataItemCountTest)
void FileMetadataItemCountTest::initTestCase()
{
qRegisterMetaType<KFileItemList>("KFileItemList");
QStandardPaths::setTestModeEnabled(true);
KConfig balooConfig("baloofilerc", KConfig::NoGlobals);
KConfigGroup balooSettings = balooConfig.group("General");
// If we use .writePathEntry here, the test will fail.
balooSettings.writeEntry(QStringLiteral("folders"), QString());
// Ensure show configuration
KConfig config("baloofileinformationrc", KConfig::NoGlobals);
KConfigGroup settings = config.group("Show");
const auto keys = settings.keyList();
for (const auto &key: keys) {
settings.writeEntry(key, true);
}
}
void FileMetadataItemCountTest::init()
{
m_widget = new Baloo::FileMetaDataWidget;
}
void FileMetadataItemCountTest::cleanup()
{
delete m_widget;
}
void FileMetadataItemCountTest::testItemCount()
{
const int expectedItems = 19;
const int widgetsPerItem = 2;
QSignalSpy spy(m_widget, &Baloo::FileMetaDataWidget::metaDataRequestFinished);
m_widget->setItems(KFileItemList()
<< QUrl::fromLocalFile(QFINDTESTDATA("testtagged.mp3"))
);
QVERIFY(spy.wait());
QCOMPARE(spy.count(), 1);
QList<QWidget*> items = m_widget->findChildren<QWidget*>(QString(), Qt::FindDirectChildrenOnly);
QCOMPARE(items.count(), expectedItems * widgetsPerItem);
}
/*
* This file is part of the KDE Baloo Project
* Copyright 2018 Michael Heidelbach <ottwolt@gmail.com>
*
* 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 of
* the License or (at your option) version 3 or any later version
* accepted by the membership of KDE e.V. (or its successor approved
* by the membership of KDE e.V.), which shall act as a proxy
* defined in Section 14 of version 3 of the license.
*
* 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 General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef FILEMETADATAITEMCOUNTTEST_H
#define FILEMETADATAITEMCOUNTTEST_H
#include <filemetadatawidget.h>
class FileMetadataItemCountTest : public QObject
{
Q_OBJECT
private Q_SLOTS:
void initTestCase();
void init();
void cleanup();
void testItemCount();
private:
Baloo::FileMetaDataWidget* m_widget;
};
#endif // FILEMETADATAITEMCOUNTTEST_H
......@@ -23,16 +23,66 @@
#include "filemetadatawidgettest.h"
#include <QObject>
#include <KFileItem>
#include <QTest>
#include <QMap>
#include <QLabel>
#include <QSignalSpy>
#include <QMetaType>
#include <QProcess>
#include <QStandardPaths>
#include <QDebug>
#include <KFileItem>
#include <KRatingWidget>
#include <KConfig>
#include <KConfigGroup>
QTEST_MAIN(FileMetadataWidgetTest)
void FileMetadataWidgetTest::initTestCase()
{
qRegisterMetaType<KFileItemList>("KFileItemList");
qputenv("LC_ALL", "en_US.UTF-8");
QStandardPaths::setTestModeEnabled(true);
KConfig balooConfig("baloofilerc", KConfig::NoGlobals);
KConfigGroup balooSettings = balooConfig.group("General");
// If we use .writePathEntry here, the test will fail.
balooSettings.writeEntry(QStringLiteral("folders"), QString());
// Ensure show configuration
KConfig config("baloofileinformationrc", KConfig::NoGlobals);
KConfigGroup settings = config.group("Show");
const auto keys = settings.keyList();
for (const auto &key: keys) {
settings.writeEntry(key, true);
}
const QString exe = QStandardPaths::findExecutable(QStringLiteral("setfattr"));
if (exe.isEmpty()) {
return;
}
const QStringList args = {QStringLiteral("--name=user.baloo.rating"),
QStringLiteral("--value=5") ,
QFINDTESTDATA("testtagged.mp3"),
QFINDTESTDATA("testtagged.m4a")};
QProcess process;
process.start(exe, args);
if (!process.waitForFinished(10000)) {
qDebug() << "setfattr timed out";
return;
}
if (process.exitStatus() == QProcess::NormalExit) {
m_mayTestRating = true;
} else {
qDebug() << "setfattr err:" << process.readAllStandardError();
}
}
void FileMetadataWidgetTest::init()
......@@ -65,7 +115,7 @@ void FileMetadataWidgetTest::shouldSignalOnceFile()
{
QSignalSpy spy(m_widget, &Baloo::FileMetaDataWidget::metaDataRequestFinished);
m_widget->setItems(KFileItemList()
<< QUrl::fromLocalFile(QStringLiteral("%1/testtagged.m4a").arg(TESTS_SAMPLE_FILES_PATH))
<< QUrl::fromLocalFile(QFINDTESTDATA("testtagged.m4a"))
);
QVERIFY(spy.wait());
QCOMPARE(spy.count(), 1);
......@@ -77,12 +127,72 @@ void FileMetadataWidgetTest::shouldSignalOnceFiles()
{
QSignalSpy spy(m_widget, &Baloo::FileMetaDataWidget::metaDataRequestFinished);
m_widget->setItems(KFileItemList()
<< QUrl::fromLocalFile(QStringLiteral("%1/test.mp3").arg(TESTS_SAMPLE_FILES_PATH))
<< QUrl::fromLocalFile(QStringLiteral("%1/testtagged.mp3").arg(TESTS_SAMPLE_FILES_PATH))
<< QUrl::fromLocalFile(QStringLiteral("%1/testtagged.m4a").arg(TESTS_SAMPLE_FILES_PATH))
<< QUrl::fromLocalFile(QFINDTESTDATA("test.mp3"))
<< QUrl::fromLocalFile(QFINDTESTDATA("testtagged.mp3"))
<< QUrl::fromLocalFile(QFINDTESTDATA("testtagged.m4a"))
);
QVERIFY(spy.wait());
QCOMPARE(spy.count(), 1);
QCOMPARE(m_widget->items().count(), 3);
}
void FileMetadataWidgetTest::shouldShowProperties()
{
QSignalSpy spy(m_widget, &Baloo::FileMetaDataWidget::metaDataRequestFinished);
m_widget->setItems(KFileItemList()
<< QUrl::fromLocalFile(QFINDTESTDATA("testtagged.mp3"))
);
QVERIFY(spy.wait());
QCOMPARE(spy.count(), 1);
// simple property
QLabel* valueWidget = m_widget->findChild<QLabel*>("kfileitem#type");
QVERIFY2(valueWidget, "Type data missing");
QCOMPARE(valueWidget->text(), QLatin1String("MP3 audio"));
if (m_mayTestRating) {
// editable property
KRatingWidget* ratingWidget = m_widget->findChild<KRatingWidget*>("rating");
QVERIFY2(ratingWidget, "Rating data missing");
QCOMPARE(ratingWidget->rating(), 5);
} else {
qDebug() << "Skipped 'Rating' test";
}
// async property
valueWidget = m_widget->findChild<QLabel*>("albumArtist");
QVERIFY2(valueWidget, "albumArtist data was not found");
QCOMPARE(valueWidget->text(), QLatin1String("Bill Laswell"));
}
void FileMetadataWidgetTest::shouldShowCommonProperties()
{
QSignalSpy spy(m_widget, &Baloo::FileMetaDataWidget::metaDataRequestFinished);
m_widget->setItems(KFileItemList()
<< QUrl::fromLocalFile(QFINDTESTDATA("testtagged.mp3"))
<< QUrl::fromLocalFile(QFINDTESTDATA("testtagged.m4a"))
);
QVERIFY(spy.wait());
QCOMPARE(spy.count(), 1);
// simple property
QLabel* valueWidget = m_widget->findChild<QLabel*>("kfileitem#type");
QVERIFY(!valueWidget);
valueWidget = m_widget->findChild<QLabel*>("kfileitem#totalSize");
// circumvent i18n formatting
QCOMPARE(valueWidget->text().left(3), QLatin1String("153"));
// editable property
if (m_mayTestRating) {
KRatingWidget* ratingWidget = m_widget->findChild<KRatingWidget*>("rating");
QCOMPARE(ratingWidget->rating(), 5);
} else {
qDebug() << "Skipped 'Rating' test";
}
// async property
// FIXME: Make this pass
// QCOMPARE( map->value("Album Artist:"), QLatin1String("Bill Laswell"));
}
......@@ -36,9 +36,12 @@ private Q_SLOTS:
void shouldSignalOnceWithEmptyFile();
void shouldSignalOnceFile();
void shouldSignalOnceFiles();
void shouldShowProperties();
void shouldShowCommonProperties();
private:
Baloo::FileMetaDataWidget* m_widget;
bool m_mayTestRating = false;
};
#endif // FILEMETADATAWIDGETTEST_H
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