From e98a572ab519243a688f8630594249ed7c1b279d Mon Sep 17 00:00:00 2001 From: Adam Higerd Date: Wed, 29 Jun 2022 20:53:43 -0500 Subject: [PATCH] more tests, more debugging --- src/platform/qt/library/LibraryModel.cpp | 29 +++-- src/platform/qt/test/library.cpp | 150 ++++++++++++++++++++--- 2 files changed, 150 insertions(+), 29 deletions(-) diff --git a/src/platform/qt/library/LibraryModel.cpp b/src/platform/qt/library/LibraryModel.cpp index 6804852ec54..20e1dbcbcab 100644 --- a/src/platform/qt/library/LibraryModel.cpp +++ b/src/platform/qt/library/LibraryModel.cpp @@ -101,9 +101,9 @@ void LibraryModel::addEntriesTree(const QList& items) { QHash> newPaths; for (const LibraryEntry& item : items) { if (m_pathIndex.contains(item.base)) { - newPaths[item.base] << &item; - } else { byPath[item.base] << &item; + } else { + newPaths[item.base] << &item; } } @@ -155,9 +155,13 @@ void LibraryModel::updateEntries(const QList& items) { void LibraryModel::removeEntries(const QList& items) { SpanSet removedRootSpans; QHash removedTreeSpans; + int firstModifiedIndex = m_games.size(); for (const QString& item : items) { int pos = m_gameIndex.value(item, -1); Q_ASSERT(pos >= 0); + if (pos < firstModifiedIndex) { + firstModifiedIndex = pos; + } LibraryEntry* entry = &m_games[pos]; QModelIndex parent = indexForPath(entry->base); Q_ASSERT(!m_treeMode || parent.isValid()); @@ -166,6 +170,7 @@ void LibraryModel::removeEntries(const QList& items) { if (!m_treeMode) { removedRootSpans.add(pos); } + m_gameIndex.remove(item); } for (const QString& base : removedTreeSpans.keys()) { SpanSet& spanSet = removedTreeSpans[base]; @@ -180,14 +185,17 @@ void LibraryModel::removeEntries(const QList& items) { m_pathIndex.remove(base); m_pathOrder.removeAll(base); } + continue; } - continue; } QModelIndex parent = indexForPath(base); spanSet.sort(true); for (const SpanSet::Span& span : spanSet.spans) { if (m_treeMode) { beginRemoveRows(parent, span.left, span.right); + for (int i = span.left; i <= span.right; i++) { + + } } pathIndex.erase(pathIndex.begin() + span.left, pathIndex.begin() + span.right + 1); if (m_treeMode) { @@ -209,6 +217,9 @@ void LibraryModel::removeEntries(const QList& items) { } endRemoveRows(); } + for (int i = m_games.count() - 1; i >= firstModifiedIndex; i--) { + m_gameIndex[m_games[i].fullpath] = i; + } } QModelIndex LibraryModel::index(const QString& game) const { @@ -225,19 +236,19 @@ QModelIndex LibraryModel::index(const QString& game) const { QModelIndex LibraryModel::index(int row, int column, const QModelIndex& parent) const { if (!parent.isValid()) { - return createIndex(row, column, quintptr(-1)); + return createIndex(row, column, quintptr(0)); } - if (!m_treeMode || parent.internalId() != quintptr(-1) || parent.column() != 0) { + if (!m_treeMode || parent.internalId() || parent.column() != 0) { return QModelIndex(); } - return createIndex(row, column, parent.row()); + return createIndex(row, column, parent.row() + 1); } QModelIndex LibraryModel::parent(const QModelIndex& child) const { - if (!child.isValid() || child.internalId() == quintptr(-1)) { + if (!child.isValid() || child.internalId() == 0) { return QModelIndex(); } - return createIndex(child.internalId(), 0, -1); + return createIndex(child.internalId() - 1, 0, quintptr(0)); } int LibraryModel::columnCount(const QModelIndex& parent) const { @@ -300,7 +311,7 @@ QVariant LibraryModel::data(const QModelIndex& index, int role) const { case COL_SIZE: return (role == Qt::DisplayRole) ? QVariant(niceSizeFormat(entry->filesize)) : QVariant(int(entry->filesize)); case COL_CRC32: - return QStringLiteral("%0").arg(entry->crc32, 8, 16, QChar('0')); + return (role == Qt::DisplayRole) ? QVariant(QStringLiteral("%0").arg(entry->crc32, 8, 16, QChar('0'))) : QVariant(entry->crc32); } } } else if (role == Qt::TextAlignmentRole) { diff --git a/src/platform/qt/test/library.cpp b/src/platform/qt/test/library.cpp index bca0ead6c3f..2bcccb79722 100644 --- a/src/platform/qt/test/library.cpp +++ b/src/platform/qt/test/library.cpp @@ -10,9 +10,13 @@ #include #endif -#include #include +#define FIND_GBA_ROW(gba, gb) \ + int gba = findGbaRow(); \ + if (gba < 0) QFAIL("Could not find gba row"); \ + int gb = 1 - gba; + using namespace QGBA; class LibraryModelTest : public QObject { @@ -21,11 +25,20 @@ Q_OBJECT private: LibraryModel* model = nullptr; - LibraryEntry makeGBA(const QString& base, const QString& name, uint32_t crc) { + int findGbaRow() { + for (int i = 0; i < model->rowCount(); i++) { + if (model->index(i, 0).data() == "gba") { + return i; + } + } + return -1; + } + + LibraryEntry makeGBA(const QString& name, uint32_t crc) { LibraryEntry entry; - entry.base = base; + entry.base = "/gba"; entry.filename = name + ".gba"; - entry.fullpath = base + "/" + entry.filename; + entry.fullpath = entry.base + "/" + entry.filename; entry.title = name; entry.internalTitle = name.toUpper().toUtf8(); entry.internalCode = entry.internalTitle.replace(" ", "").left(4); @@ -35,23 +48,70 @@ Q_OBJECT return entry; } - LibraryEntry makeGB(const QString& base, const QString& name, uint32_t crc) { - LibraryEntry entry = makeGBA(base, name, crc); - entry.filename = name + ".gb"; - entry.fullpath = base + "/" + entry.filename; + LibraryEntry makeGB(const QString& name, uint32_t crc) { + LibraryEntry entry = makeGBA(name, crc); + entry.base = "/gb"; + entry.filename = entry.filename.replace("gba", "gb"); + entry.fullpath = entry.fullpath.replace("gba", "gb"); entry.platform = mPLATFORM_GB; - entry.filesize /= 4;; + entry.filesize /= 4; return entry; } void addTestGames1() { model->addEntries({ - makeGBA("/gba", "Test Game", 0x12345678), - makeGBA("/gba", "Another", 0x23456789), - makeGB("/gb", "Old Game", 0x87654321), + makeGBA("Test Game", 0x12345678), + makeGBA("Another", 0x23456789), + makeGB("Old Game", 0x87654321), }); } + void addTestGames2() { + model->addEntries({ + makeGBA("Game 3", 0x12345679), + makeGBA("Game 4", 0x2345678A), + makeGBA("Game 5", 0x2345678B), + makeGB("Game 6", 0x87654322), + makeGB("Game 7", 0x87654323), + }); + } + + void updateGame() { + LibraryEntry game = makeGBA("Another", 0x88888888); + model->updateEntries({ game }); + QModelIndex idx = find("Another"); + QVERIFY2(idx.isValid(), "game not found"); + QCOMPARE(idx.siblingAtColumn(LibraryModel::COL_CRC32).data(Qt::EditRole).toInt(), 0x88888888); + } + + void removeGames1() { + model->removeEntries({ "/gba/Another.gba", "/gb/Game 6.gb" }); + QVERIFY2(!find("Another").isValid(), "game not removed"); + QVERIFY2(!find("Game 6").isValid(), "game not removed"); + } + + void removeGames2() { + model->removeEntries({ "/gb/Old Game.gb", "/gb/Game 7.gb" }); + QVERIFY2(!find("Old Game").isValid(), "game not removed"); + QVERIFY2(!find("Game 7").isValid(), "game not removed"); + } + + QModelIndex find(const QString& name) { + for (int i = 0; i < model->rowCount(); i++) { + QModelIndex idx = model->index(i, 0); + if (idx.data().toString() == name) { + return idx; + } + for (int j = 0; j < model->rowCount(idx); j++) { + QModelIndex child = model->index(j, 0, idx); + if (child.data().toString() == name) { + return child; + } + } + } + return QModelIndex(); + } + private slots: void init() { model = new LibraryModel(); @@ -65,24 +125,74 @@ private slots: model = nullptr; } - void initList() { + void testList() { addTestGames1(); QCOMPARE(model->rowCount(), 3); + addTestGames2(); + QCOMPARE(model->rowCount(), 8); + updateGame(); + model->removeEntries({ "/gba/Another.gba", "/gb/Game 6.gb" }); + QCOMPARE(model->rowCount(), 6); + model->removeEntries({ "/gb/Old Game.gb", "/gb/Game 7.gb" }); + QCOMPARE(model->rowCount(), 4); } - void initTree() { - QSignalSpy resetSpy(model, SIGNAL(modelReset())); + void testTree() { model->setTreeMode(true); - QVERIFY(resetSpy.count()); addTestGames1(); - int gbRow = 0, gbaRow = 1; - if (model->index(0, 0).data() == "gba") { - gbaRow = 0; - gbRow = 1; + FIND_GBA_ROW(gbaRow, gbRow); + QCOMPARE(model->rowCount(), 2); + QCOMPARE(model->rowCount(model->index(gbRow, 0)), 1); + QCOMPARE(model->rowCount(model->index(gbaRow, 0)), 2); + addTestGames2(); + QCOMPARE(model->rowCount(), 2); + QCOMPARE(model->rowCount(model->index(gbRow, 0)), 3); + QCOMPARE(model->rowCount(model->index(gbaRow, 0)), 5); + updateGame(); + removeGames1(); + QCOMPARE(model->rowCount(), 2); + QCOMPARE(model->rowCount(model->index(gbRow, 0)), 2); + QCOMPARE(model->rowCount(model->index(gbaRow, 0)), 4); + removeGames2(); + QVERIFY2(!find("gb").isValid(), "did not remove gb folder"); + QCOMPARE(model->rowCount(), 1); + QCOMPARE(model->rowCount(model->index(0, 0)), 4); + } + + void modeSwitchTest1() { + addTestGames1(); + { + QSignalSpy resetSpy(model, SIGNAL(modelReset())); + model->setTreeMode(true); + QVERIFY(resetSpy.count()); } + FIND_GBA_ROW(gbaRow, gbRow); QCOMPARE(model->rowCount(), 2); QCOMPARE(model->rowCount(model->index(gbRow, 0)), 1); QCOMPARE(model->rowCount(model->index(gbaRow, 0)), 2); + { + QSignalSpy resetSpy(model, SIGNAL(modelReset())); + model->setTreeMode(false); + QVERIFY(resetSpy.count()); + } + addTestGames2(); + QCOMPARE(model->rowCount(), 8); + } + + void modeSwitchTest2() { + model->setTreeMode(false); + addTestGames1(); + model->setTreeMode(true); + FIND_GBA_ROW(gbaRow, gbRow); + QCOMPARE(model->rowCount(), 2); + QCOMPARE(model->rowCount(model->index(gbRow, 0)), 1); + QCOMPARE(model->rowCount(model->index(gbaRow, 0)), 2); + addTestGames2(); + QCOMPARE(model->rowCount(), 2); + QCOMPARE(model->rowCount(model->index(gbRow, 0)), 3); + QCOMPARE(model->rowCount(model->index(gbaRow, 0)), 5); + model->setTreeMode(false); + QCOMPARE(model->rowCount(), 8); } };