Commit 8c75d120 authored by Matthieu Gallien's avatar Matthieu Gallien

fix update of metadata model when a track change

parent 5d1b080c
......@@ -16,13 +16,15 @@
*/
#include "models/trackmetadatamodel.h"
#include "qabstractitemmodeltester.h"
#include "databasetestdata.h"
#include <QDebug>
#include <QtTest>
class TrackMetadataModelTests: public QObject
class TrackMetadataModelTests: public QObject, public DatabaseTestData
{
Q_OBJECT
......@@ -67,6 +69,77 @@ private Q_SLOTS:
QCOMPARE(endRemovedRowsSpy.count(), 0);
QCOMPARE(myModel.rowCount(), 1);
}
void modifyTrackInDatabase()
{
QTemporaryFile databaseFile;
databaseFile.open();
qDebug() << "addOneTrackWithoutAlbumArtist" << databaseFile.fileName();
DatabaseInterface musicDb;
musicDb.init(QStringLiteral("testDb"), databaseFile.fileName());
musicDb.insertTracksList(mNewTracks, mNewCovers);
TrackMetadataModel myModel;
QAbstractItemModelTester testModel(&myModel);
QSignalSpy beginResetSpy(&myModel, &TrackMetadataModel::modelAboutToBeReset);
QSignalSpy endResetSpy(&myModel, &TrackMetadataModel::modelReset);
QSignalSpy beginInsertRowsSpy(&myModel, &TrackMetadataModel::rowsAboutToBeInserted);
QSignalSpy endInsertRowsSpy(&myModel, &TrackMetadataModel::rowsInserted);
QSignalSpy dataChangedSpy(&myModel, &TrackMetadataModel::dataChanged);
QSignalSpy beginRemovedRowsSpy(&myModel, &TrackMetadataModel::rowsAboutToBeRemoved);
QSignalSpy endRemovedRowsSpy(&myModel, &TrackMetadataModel::rowsRemoved);
QCOMPARE(beginResetSpy.count(), 0);
QCOMPARE(endResetSpy.count(), 0);
QCOMPARE(beginInsertRowsSpy.count(), 0);
QCOMPARE(endInsertRowsSpy.count(), 0);
QCOMPARE(dataChangedSpy.count(), 0);
QCOMPARE(beginRemovedRowsSpy.count(), 0);
QCOMPARE(endRemovedRowsSpy.count(), 0);
QCOMPARE(myModel.rowCount(), 0);
myModel.setDatabase(&musicDb);
auto trackId = musicDb.trackIdFromFileName(QUrl::fromLocalFile(QStringLiteral("/$1")));
myModel.initializeByTrackId(trackId);
QCOMPARE(beginResetSpy.count(), 1);
QCOMPARE(endResetSpy.count(), 1);
QCOMPARE(beginInsertRowsSpy.count(), 0);
QCOMPARE(endInsertRowsSpy.count(), 0);
QCOMPARE(dataChangedSpy.count(), 0);
QCOMPARE(beginRemovedRowsSpy.count(), 0);
QCOMPARE(endRemovedRowsSpy.count(), 0);
QCOMPARE(myModel.rowCount(), 11);
musicDb.trackHasStartedPlaying(QUrl::fromLocalFile(QStringLiteral("/$2")), QDateTime::currentDateTime());
QCOMPARE(beginResetSpy.count(), 1);
QCOMPARE(endResetSpy.count(), 1);
QCOMPARE(beginInsertRowsSpy.count(), 0);
QCOMPARE(endInsertRowsSpy.count(), 0);
QCOMPARE(dataChangedSpy.count(), 0);
QCOMPARE(beginRemovedRowsSpy.count(), 0);
QCOMPARE(endRemovedRowsSpy.count(), 0);
QCOMPARE(myModel.rowCount(), 11);
musicDb.trackHasStartedPlaying(QUrl::fromLocalFile(QStringLiteral("/$1")), QDateTime::currentDateTime());
QCOMPARE(beginResetSpy.count(), 2);
QCOMPARE(endResetSpy.count(), 2);
QCOMPARE(beginInsertRowsSpy.count(), 0);
QCOMPARE(endInsertRowsSpy.count(), 0);
QCOMPARE(dataChangedSpy.count(), 0);
QCOMPARE(beginRemovedRowsSpy.count(), 0);
QCOMPARE(endRemovedRowsSpy.count(), 0);
QCOMPARE(myModel.rowCount(), 12);
}
};
QTEST_GUILESS_MAIN(TrackMetadataModelTests)
......
......@@ -240,6 +240,10 @@ MusicListenersManager *TrackMetadataModel::manager() const
void TrackMetadataModel::trackData(const TrackMetadataModel::TrackDataType &trackData)
{
if (!mFullData.isEmpty() && trackData.databaseId() != mFullData.databaseId()) {
return;
}
fillDataFromTrackData(trackData);
}
......@@ -302,6 +306,31 @@ TrackMetadataModel::TrackDataType::mapped_type TrackMetadataModel::dataFromType(
return mFullData[metaData];
}
void TrackMetadataModel::initialize(MusicListenersManager *newManager, DatabaseInterface *trackDatabase)
{
mManager = newManager;
Q_EMIT managerChanged();
if (mManager) {
mDataLoader.setDatabase(mManager->viewDatabase());
} else if (trackDatabase) {
mDataLoader.setDatabase(trackDatabase);
}
if (mManager) {
mManager->connectModel(&mDataLoader);
}
connect(this, &TrackMetadataModel::needDataByDatabaseId,
&mDataLoader, &ModelDataLoader::loadDataByDatabaseId);
connect(this, &TrackMetadataModel::needDataByFileName,
&mDataLoader, &ModelDataLoader::loadDataByFileName);
connect(&mDataLoader, &ModelDataLoader::allTrackData,
this, &TrackMetadataModel::trackData);
connect(&mDataLoader, &ModelDataLoader::trackModified,
this, &TrackMetadataModel::trackData);
}
void TrackMetadataModel::initializeByTrackId(qulonglong databaseId)
{
Q_EMIT needDataByDatabaseId(ElisaUtils::Track, databaseId);
......@@ -314,20 +343,12 @@ void TrackMetadataModel::initializeByTrackFileName(const QUrl &fileName)
void TrackMetadataModel::setManager(MusicListenersManager *newManager)
{
mManager = newManager;
Q_EMIT managerChanged();
if (mManager) {
mDataLoader.setDatabase(mManager->viewDatabase());
mManager->connectModel(&mDataLoader);
initialize(newManager, nullptr);
}
connect(this, &TrackMetadataModel::needDataByDatabaseId,
&mDataLoader, &ModelDataLoader::loadDataByDatabaseId);
connect(this, &TrackMetadataModel::needDataByFileName,
&mDataLoader, &ModelDataLoader::loadDataByFileName);
connect(&mDataLoader, &ModelDataLoader::allTrackData,
this, &TrackMetadataModel::trackData);
}
void TrackMetadataModel::setDatabase(DatabaseInterface *trackDatabase)
{
initialize(nullptr, trackDatabase);
}
......
......@@ -105,6 +105,8 @@ public Q_SLOTS:
void setManager(MusicListenersManager *newManager);
void setDatabase(DatabaseInterface *trackDatabase);
protected:
void fillDataFromTrackData(const TrackMetadataModel::TrackDataType &trackData);
......@@ -117,6 +119,9 @@ protected:
private:
void initialize(MusicListenersManager *newManager,
DatabaseInterface *trackDatabase);
TrackDataType mFullData;
TrackDataType mTrackData;
......
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