Page MenuHomePhorge

D114.1732293241.diff
No OneTemporary

Size
25 KB
Referenced Files
None
Subscribers
None

D114.1732293241.diff

diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -32,6 +32,7 @@
matrix-sticker-pack-list.cpp
matrix-sticker-pack-source.cpp
matrix-user-given-attrs-map.cpp
+ kazv-abstract-list-model.cpp
qt-promise-handler.cpp
device-mgmt/matrix-device.cpp
diff --git a/src/kazv-abstract-list-model.hpp b/src/kazv-abstract-list-model.hpp
new file mode 100644
--- /dev/null
+++ b/src/kazv-abstract-list-model.hpp
@@ -0,0 +1,40 @@
+/*
+ * This file is part of kazv.
+ * SPDX-FileCopyrightText: 2020-2024 tusooa <tusooa@kazv.moe>
+ * SPDX-License-Identifier: AGPL-3.0-or-later
+ */
+
+#pragma once
+#include <kazv-defs.hpp>
+#include <QObject>
+#include <QQmlEngine>
+#include <QAbstractListModel>
+#include <lager/extra/qt.hpp>
+
+class KazvAbstractListModel : public QAbstractListModel
+{
+ Q_OBJECT
+
+ int m_internalCount;
+
+public:
+ enum InitMethod {
+ InitNow,
+ InitLater,
+ };
+
+ explicit KazvAbstractListModel(lager::reader<int> count, InitMethod initMethod = InitNow, QObject *parent = 0);
+ explicit KazvAbstractListModel(QObject *parent = 0);
+ ~KazvAbstractListModel() override;
+
+ LAGER_QT_READER(int, count);
+
+ int rowCount(const QModelIndex &index) const override;
+ QVariant data(const QModelIndex &index, int role) const override;
+
+protected:
+ void initCountCursor(lager::reader<int> count, InitMethod initMethod = InitNow);
+
+private Q_SLOTS:
+ void updateInternalCount();
+};
diff --git a/src/kazv-abstract-list-model.cpp b/src/kazv-abstract-list-model.cpp
new file mode 100644
--- /dev/null
+++ b/src/kazv-abstract-list-model.cpp
@@ -0,0 +1,76 @@
+/*
+ * This file is part of kazv.
+ * SPDX-FileCopyrightText: 2020-2024 tusooa <tusooa@kazv.moe>
+ * SPDX-License-Identifier: AGPL-3.0-or-later
+ */
+
+#include <kazv-defs.hpp>
+#include <QTimer>
+#include <lager/constant.hpp>
+#include "kazv-abstract-list-model.hpp"
+
+KazvAbstractListModel::KazvAbstractListModel(lager::reader<int> countArg, InitMethod initMethod, QObject *parent)
+ : QAbstractListModel(parent)
+ , m_internalCount(0)
+{
+ connect(this, &KazvAbstractListModel::countChanged, this, &KazvAbstractListModel::updateInternalCount);
+
+ initCountCursor(countArg, initMethod);
+}
+
+KazvAbstractListModel::KazvAbstractListModel(QObject *parent)
+ : QAbstractListModel(parent)
+ , m_internalCount(0)
+ , LAGER_QT(count)(lager::make_constant(0))
+{
+ connect(this, &KazvAbstractListModel::countChanged, this, &KazvAbstractListModel::updateInternalCount);
+}
+
+KazvAbstractListModel::~KazvAbstractListModel() = default;
+
+void KazvAbstractListModel::initCountCursor(lager::reader<int> countArg, InitMethod initMethod)
+{
+ LAGER_QT(count) = countArg;
+
+ if (initMethod == InitNow) {
+ m_internalCount = count();
+ } else {
+ // The following is taken from MatrixRoomTimeline.
+ // HACK: If internally the count is set to its full count at the beginning,
+ // ListView will instantiate everything in the timeline.
+ // I don't know why it is the case, so I keep the internal count as 0
+ // when it is constructed, and update it in the next tick.
+ QTimer::singleShot(0, this, [this] { Q_EMIT countChanged(count()); });
+ }
+}
+
+QVariant KazvAbstractListModel::data(const QModelIndex &/* index */, int /* role */) const
+{
+ return QVariant();
+}
+
+int KazvAbstractListModel::rowCount(const QModelIndex &parent) const
+{
+ if (parent.isValid()) {
+ return 0;
+ } else {
+ return m_internalCount;
+ }
+}
+
+void KazvAbstractListModel::updateInternalCount()
+{
+ auto curCount = count();
+ auto oldCount = m_internalCount;
+ auto diff = std::abs(curCount - oldCount);
+
+ if (curCount > oldCount) {
+ beginInsertRows(QModelIndex(), 0, diff - 1);
+ m_internalCount = curCount;
+ endInsertRows();
+ } else if (curCount < oldCount) {
+ beginRemoveRows(QModelIndex(), 0, diff - 1);
+ m_internalCount = curCount;
+ endRemoveRows();
+ }
+}
diff --git a/src/matrix-room-list.hpp b/src/matrix-room-list.hpp
--- a/src/matrix-room-list.hpp
+++ b/src/matrix-room-list.hpp
@@ -9,19 +9,18 @@
#include <QObject>
#include <QQmlEngine>
-#include <QAbstractListModel>
-
#include <lager/sensor.hpp>
#include <lager/state.hpp>
#include <lager/extra/qt.hpp>
#include <client/client.hpp>
+#include "kazv-abstract-list-model.hpp"
Q_MOC_INCLUDE("matrix-user-given-attrs-map.hpp")
class MatrixRoom;
class MatrixUserGivenAttrsMap;
-class MatrixRoomList : public QAbstractListModel
+class MatrixRoomList : public KazvAbstractListModel
{
Q_OBJECT
QML_ELEMENT
@@ -30,7 +29,6 @@
Kazv::Client m_client;
QString m_tagId;
lager::sensor<std::string> m_tagIdCursor;
- int m_internalCount;
lager::state<std::string, lager::automatic_tag> m_filter;
lager::reader<Kazv::JsonWrap> m_userGivenNicknameMap;
lager::reader<immer::flex_vector<std::string>> m_roomIds;
@@ -42,16 +40,9 @@
Q_INVOKABLE void setTagId(QString tagId);
LAGER_QT_CURSOR(QString, filter);
- LAGER_QT_READER(int, count);
LAGER_QT_READER(QStringList, roomIds);
Q_INVOKABLE MatrixRoom *at(int index) const;
Q_INVOKABLE QString roomIdAt(int index) const;
Q_INVOKABLE MatrixRoom *room(QString roomId) const;
-
- QVariant data(const QModelIndex &index, int role) const override;
- int rowCount(const QModelIndex &parent) const override;
-
-private Q_SLOTS:
- void updateInternalCount();
};
diff --git a/src/matrix-room-list.cpp b/src/matrix-room-list.cpp
--- a/src/matrix-room-list.cpp
+++ b/src/matrix-room-list.cpp
@@ -31,11 +31,10 @@
}
MatrixRoomList::MatrixRoomList(Kazv::Client client, QString tagId, QString filter, QObject *parent)
- : QAbstractListModel(parent)
+ : KazvAbstractListModel(parent)
, m_client(client)
, m_tagId(tagId)
, m_tagIdCursor(lager::make_sensor([this] { return m_tagId.toStdString(); }))
- , m_internalCount(0)
, m_filter(lager::make_state(filter.toStdString(), lager::automatic_tag{}))
, m_userGivenNicknameMap(userGivenNicknameMapFor(m_client).map(&Event::content))
, m_roomIds(lager::with(m_tagIdCursor, m_client.rooms(), m_filter, m_client.roomIdsByTagId(), m_userGivenNicknameMap)
@@ -143,15 +142,12 @@
}
}))
, LAGER_QT(filter)(m_filter.xform(strToQt, qStringToStd))
- , LAGER_QT(count)(m_roomIds.xform(containerSize))
, LAGER_QT(roomIds)(m_roomIds.xform(zug::map(
[](auto container) {
return zug::into(QStringList{}, strToQt, std::move(container));
})))
{
- m_internalCount = count();
-
- connect(this, &MatrixRoomList::countChanged, this, &MatrixRoomList::updateInternalCount);
+ initCountCursor(m_roomIds.xform(containerSize));
}
MatrixRoomList::~MatrixRoomList() = default;
@@ -192,34 +188,3 @@
userGivenNicknameMapFor(m_client)
);
}
-
-QVariant MatrixRoomList::data(const QModelIndex &/* index */, int /* role */) const
-{
- return QVariant();
-}
-
-int MatrixRoomList::rowCount(const QModelIndex &parent = QModelIndex()) const
-{
- if (parent.isValid()) {
- return 0;
- } else {
- return count();
- }
-}
-
-void MatrixRoomList::updateInternalCount()
-{
- auto curCount = count();
- auto oldCount = m_internalCount;
- auto diff = std::abs(curCount - oldCount);
-
- if (curCount > oldCount) {
- beginInsertRows(QModelIndex(), 0, diff - 1);
- m_internalCount = curCount;
- endInsertRows();
- } else if (curCount < oldCount) {
- beginRemoveRows(QModelIndex(), 0, diff - 1);
- m_internalCount = curCount;
- endRemoveRows();
- }
-}
diff --git a/src/matrix-room-member-list-model.hpp b/src/matrix-room-member-list-model.hpp
--- a/src/matrix-room-member-list-model.hpp
+++ b/src/matrix-room-member-list-model.hpp
@@ -9,15 +9,14 @@
#include <QObject>
#include <QQmlEngine>
-#include <QAbstractListModel>
-
#include <lager/extra/qt.hpp>
#include <lager/state.hpp>
#include <client/room/room.hpp>
+#include "kazv-abstract-list-model.hpp"
class MatrixRoomMember;
-class MatrixRoomMemberListModel : public QAbstractListModel
+class MatrixRoomMemberListModel : public KazvAbstractListModel
{
Q_OBJECT
QML_ELEMENT
@@ -25,20 +24,12 @@
lager::state<std::string, lager::automatic_tag> m_filter;
lager::reader<Kazv::EventList> m_members;
- int m_internalCount;
public:
explicit MatrixRoomMemberListModel(lager::reader<Kazv::EventList> members, QString filter = QString(), lager::reader<Kazv::Event> userGivenNicknameEvent = lager::make_constant(Kazv::Event()), QObject *parent = 0);
~MatrixRoomMemberListModel() override;
LAGER_QT_CURSOR(QString, filter);
- LAGER_QT_READER(int, count);
-
- int rowCount(const QModelIndex &index) const override;
- QVariant data(const QModelIndex &index, int role) const override;
Q_INVOKABLE MatrixRoomMember *at(int index) const;
-
-private Q_SLOTS:
- void updateInternalCount();
};
diff --git a/src/matrix-room-member-list-model.cpp b/src/matrix-room-member-list-model.cpp
--- a/src/matrix-room-member-list-model.cpp
+++ b/src/matrix-room-member-list-model.cpp
@@ -18,7 +18,7 @@
using namespace Kazv;
MatrixRoomMemberListModel::MatrixRoomMemberListModel(lager::reader<Kazv::EventList> members, QString filter, lager::reader<Kazv::Event> userGivenNicknameEvent, QObject *parent)
- : QAbstractListModel(parent)
+ : KazvAbstractListModel(parent)
, m_filter(lager::make_state(filter.toStdString(), lager::automatic_tag{}))
, m_members(lager::with(
members,
@@ -50,14 +50,9 @@
return false;
}), members);
}))
- , m_internalCount(0)
, LAGER_QT(filter)(m_filter.xform(strToQt, qStringToStd))
- , LAGER_QT(count)(m_members.map([](const auto &m) -> int { return m.size(); }))
{
- m_internalCount = count();
-
- connect(this, &MatrixRoomMemberListModel::countChanged,
- this, &MatrixRoomMemberListModel::updateInternalCount);
+ initCountCursor(m_members.map([](const auto &m) -> int { return m.size(); }));
}
MatrixRoomMemberListModel::~MatrixRoomMemberListModel() = default;
@@ -66,34 +61,3 @@
{
return new MatrixRoomMember(m_members[index][lager::lenses::or_default]);
}
-
-int MatrixRoomMemberListModel::rowCount(const QModelIndex &index) const
-{
- if (index.isValid()) {
- return 0;
- } else {
- return count();
- }
-}
-
-QVariant MatrixRoomMemberListModel::data(const QModelIndex &, int) const
-{
- return QVariant();
-}
-
-void MatrixRoomMemberListModel::updateInternalCount()
-{
- auto curCount = count();
- auto oldCount = m_internalCount;
- auto diff = std::abs(curCount - oldCount);
-
- if (curCount > oldCount) {
- beginInsertRows(QModelIndex(), 0, diff - 1);
- m_internalCount = curCount;
- endInsertRows();
- } else if (curCount < oldCount) {
- beginRemoveRows(QModelIndex(), 0, diff - 1);
- m_internalCount = curCount;
- endRemoveRows();
- }
-}
diff --git a/src/matrix-room-timeline.hpp b/src/matrix-room-timeline.hpp
--- a/src/matrix-room-timeline.hpp
+++ b/src/matrix-room-timeline.hpp
@@ -9,15 +9,15 @@
#include <QObject>
#include <QQmlEngine>
-#include <QAbstractListModel>
#include <lager/extra/qt.hpp>
#include <client/room/room.hpp>
+#include "kazv-abstract-list-model.hpp"
class MatrixEvent;
-class MatrixRoomTimeline : public QAbstractListModel
+class MatrixRoomTimeline : public KazvAbstractListModel
{
Q_OBJECT
QML_ELEMENT
@@ -27,7 +27,6 @@
lager::reader<immer::flex_vector<std::string>> m_timelineEventIds;
lager::reader<immer::map<std::string, Kazv::Event>> m_messagesMap;
- int m_internalCount;
lager::reader<immer::flex_vector<Kazv::LocalEchoDesc>> m_localEchoes;
lager::reader<immer::map<std::string, std::string>> m_timelineGaps;
@@ -35,16 +34,9 @@
explicit MatrixRoomTimeline(Kazv::Room room, QObject *parent = 0);
~MatrixRoomTimeline() override;
- LAGER_QT_READER(int, count);
LAGER_QT_READER(QSet<QString>, gaps);
- int rowCount(const QModelIndex &index) const override;
- QVariant data(const QModelIndex &index, int role) const override;
-
Q_INVOKABLE MatrixEvent *at(int index) const;
Q_INVOKABLE int indexOfEvent(const QString &eventId) const;
-
-private Q_SLOTS:
- void updateInternalCount();
};
diff --git a/src/matrix-room-timeline.cpp b/src/matrix-room-timeline.cpp
--- a/src/matrix-room-timeline.cpp
+++ b/src/matrix-room-timeline.cpp
@@ -24,17 +24,12 @@
using namespace Kazv;
MatrixRoomTimeline::MatrixRoomTimeline(Kazv::Room room, QObject *parent)
- : QAbstractListModel(parent)
+ : KazvAbstractListModel(parent)
, m_room(room)
, m_timelineEventIds(m_room.timelineEventIds())
, m_messagesMap(m_room.messagesMap())
- , m_internalCount(0)
, m_localEchoes(m_room.localEchoes())
, m_timelineGaps(m_room.timelineGaps())
- , LAGER_QT(count)(lager::with(
- m_timelineEventIds.xform(containerSize),
- m_localEchoes.xform(containerSize)
- ).map([](const auto &tlSize, const auto &localSize) { return tlSize + localSize; }))
, LAGER_QT(gaps)(m_timelineGaps.map([](auto g) {
QSet<QString> res{};
for (const auto &[k, _v] : g) {
@@ -43,12 +38,10 @@
return res;
}))
{
- connect(this, &MatrixRoomTimeline::countChanged, this, &MatrixRoomTimeline::updateInternalCount);
- // HACK: If internally the count is set to its full count at the beginning,
- // ListView will instantiate everything in the timeline.
- // I don't know why it is the case, so I keep the internal count as 0
- // when it is constructed, and update it in the next tick.
- QTimer::singleShot(0, this, [this] { Q_EMIT countChanged(count()); });
+ initCountCursor(lager::with(
+ m_timelineEventIds.xform(containerSize),
+ m_localEchoes.xform(containerSize)
+ ).map([](const auto &tlSize, const auto &localSize) { return tlSize + localSize; }), InitLater);
}
MatrixRoomTimeline::~MatrixRoomTimeline() = default;
@@ -71,20 +64,6 @@
}), m_room);
}
-int MatrixRoomTimeline::rowCount(const QModelIndex &index) const
-{
- if (index.isValid()) {
- return 0;
- } else {
- return m_internalCount;
- }
-}
-
-QVariant MatrixRoomTimeline::data(const QModelIndex &, int) const
-{
- return QVariant();
-}
-
int MatrixRoomTimeline::indexOfEvent(const QString &eventId) const
{
auto eventMap = m_messagesMap.get();
@@ -113,20 +92,3 @@
return count() - it.index() - 1;
}
}
-
-void MatrixRoomTimeline::updateInternalCount()
-{
- auto curCount = count();
- auto oldCount = m_internalCount;
- auto diff = std::abs(curCount - oldCount);
-
- if (curCount > oldCount) {
- beginInsertRows(QModelIndex(), 0, diff - 1);
- m_internalCount = curCount;
- endInsertRows();
- } else if (curCount < oldCount) {
- beginRemoveRows(QModelIndex(), 0, diff - 1);
- m_internalCount = curCount;
- endRemoveRows();
- }
-}
diff --git a/src/matrix-sticker-pack-list.hpp b/src/matrix-sticker-pack-list.hpp
--- a/src/matrix-sticker-pack-list.hpp
+++ b/src/matrix-sticker-pack-list.hpp
@@ -9,7 +9,6 @@
#include <QObject>
#include <QQmlEngine>
-#include <QAbstractListModel>
#include <immer/flex_vector.hpp>
@@ -18,13 +17,13 @@
#include <client/client.hpp>
#include <base/event.hpp>
-
+#include "kazv-abstract-list-model.hpp"
#include "matrix-sticker-pack-source.hpp"
Q_MOC_INCLUDE("matrix-sticker-pack.hpp")
class MatrixStickerPack;
-class MatrixStickerPackList : public QAbstractListModel
+class MatrixStickerPackList : public KazvAbstractListModel
{
Q_OBJECT
QML_ELEMENT
@@ -32,19 +31,10 @@
Kazv::Client m_client;
lager::reader<immer::flex_vector<MatrixStickerPackSource>> m_events;
- int m_internalCount;
public:
explicit MatrixStickerPackList(Kazv::Client client, QObject *parent = 0);
~MatrixStickerPackList() override;
- LAGER_QT_READER(int, count);
-
Q_INVOKABLE MatrixStickerPack *at(int index) const;
-
- QVariant data(const QModelIndex &index, int role) const override;
- int rowCount(const QModelIndex &parent) const override;
-
-private Q_SLOTS:
- void updateInternalCount();
};
diff --git a/src/matrix-sticker-pack-list.cpp b/src/matrix-sticker-pack-list.cpp
--- a/src/matrix-sticker-pack-list.cpp
+++ b/src/matrix-sticker-pack-list.cpp
@@ -27,17 +27,13 @@
}
MatrixStickerPackList::MatrixStickerPackList(Client client, QObject *parent)
- : QAbstractListModel(parent)
+ : KazvAbstractListModel(parent)
, m_client(client)
, m_events(getEventsFromClient(m_client))
- , m_internalCount(0)
- , LAGER_QT(count)(m_events.map([](const auto &events) {
- return static_cast<int>(events.size());
- }))
{
- m_internalCount = count();
-
- connect(this, &MatrixStickerPackList::countChanged, this, &MatrixStickerPackList::updateInternalCount);
+ initCountCursor(m_events.map([](const auto &events) {
+ return static_cast<int>(events.size());
+ }));
}
MatrixStickerPackList::~MatrixStickerPackList() = default;
@@ -56,34 +52,3 @@
}
}));
}
-
-QVariant MatrixStickerPackList::data(const QModelIndex &/* index */, int /* role */) const
-{
- return QVariant();
-}
-
-int MatrixStickerPackList::rowCount(const QModelIndex &parent = QModelIndex()) const
-{
- if (parent.isValid()) {
- return 0;
- } else {
- return count();
- }
-}
-
-void MatrixStickerPackList::updateInternalCount()
-{
- auto curCount = count();
- auto oldCount = m_internalCount;
- auto diff = std::abs(curCount - oldCount);
-
- if (curCount > oldCount) {
- beginInsertRows(QModelIndex(), 0, diff - 1);
- m_internalCount = curCount;
- endInsertRows();
- } else if (curCount < oldCount) {
- beginRemoveRows(QModelIndex(), 0, diff - 1);
- m_internalCount = curCount;
- endRemoveRows();
- }
-}
diff --git a/src/matrix-sticker-pack.hpp b/src/matrix-sticker-pack.hpp
--- a/src/matrix-sticker-pack.hpp
+++ b/src/matrix-sticker-pack.hpp
@@ -9,13 +9,12 @@
#include <QObject>
#include <QQmlEngine>
-#include <QAbstractListModel>
#include <lager/reader.hpp>
#include <lager/extra/qt.hpp>
#include <base/event.hpp>
-
+#include "kazv-abstract-list-model.hpp"
#include "matrix-sticker-pack-source.hpp"
#include "meta-types.hpp"
Q_MOC_INCLUDE("matrix-sticker.hpp")
@@ -24,7 +23,7 @@
class MatrixSticker;
class MatrixEvent;
-class MatrixStickerPack : public QAbstractListModel
+class MatrixStickerPack : public KazvAbstractListModel
{
Q_OBJECT
QML_ELEMENT
@@ -33,21 +32,15 @@
lager::reader<MatrixStickerPackSource> m_source;
lager::reader<Kazv::Event> m_event;
lager::reader<Kazv::json> m_images;
- int m_internalCount;
public:
explicit MatrixStickerPack(lager::reader<MatrixStickerPackSource> source, QObject *parent = 0);
~MatrixStickerPack() override;
- LAGER_QT_READER(int, count);
-
Q_INVOKABLE MatrixSticker *at(int index) const;
Q_INVOKABLE bool hasShortCode(const QString &shortCode) const;
- QVariant data(const QModelIndex &index, int role) const override;
- int rowCount(const QModelIndex &parent) const override;
-
/**
* Add a sticker to the pack and return the source of the
* modified pack.
@@ -57,7 +50,4 @@
* @return The MatrixStickerPackSource of the modified pack.
*/
Q_INVOKABLE QVariant addSticker(const QString &shortCode, MatrixEvent *event) const;
-
-private Q_SLOTS:
- void updateInternalCount();
};
diff --git a/src/matrix-sticker-pack.cpp b/src/matrix-sticker-pack.cpp
--- a/src/matrix-sticker-pack.cpp
+++ b/src/matrix-sticker-pack.cpp
@@ -15,7 +15,7 @@
using namespace Kazv;
MatrixStickerPack::MatrixStickerPack(lager::reader<MatrixStickerPackSource> source, QObject *parent)
- : QAbstractListModel(parent)
+ : KazvAbstractListModel(parent)
, m_source(source)
, m_event(m_source[&MatrixStickerPackSource::event])
, m_images(m_event.xform(
@@ -37,14 +37,10 @@
return json::array();
}
})))
- , m_internalCount(0)
- , LAGER_QT(count)(m_images.map([](const JsonWrap &images) -> int {
- return images.get().size();
- }))
{
- m_internalCount = count();
-
- connect(this, &MatrixStickerPack::countChanged, this, &MatrixStickerPack::updateInternalCount);
+ initCountCursor(m_images.map([](const JsonWrap &images) -> int {
+ return images.get().size();
+ }));
}
MatrixStickerPack::~MatrixStickerPack() = default;
@@ -78,37 +74,6 @@
.make().get();
}
-QVariant MatrixStickerPack::data(const QModelIndex &/* index */, int /* role */) const
-{
- return QVariant();
-}
-
-int MatrixStickerPack::rowCount(const QModelIndex &parent = QModelIndex()) const
-{
- if (parent.isValid()) {
- return 0;
- } else {
- return count();
- }
-}
-
-void MatrixStickerPack::updateInternalCount()
-{
- auto curCount = count();
- auto oldCount = m_internalCount;
- auto diff = std::abs(curCount - oldCount);
-
- if (curCount > oldCount) {
- beginInsertRows(QModelIndex(), 0, diff - 1);
- m_internalCount = curCount;
- endInsertRows();
- } else if (curCount < oldCount) {
- beginRemoveRows(QModelIndex(), 0, diff - 1);
- m_internalCount = curCount;
- endRemoveRows();
- }
-}
-
QVariant MatrixStickerPack::addSticker(const QString &shortCode, MatrixEvent *event) const
{
if (!event) {
diff --git a/src/tests/CMakeLists.txt b/src/tests/CMakeLists.txt
--- a/src/tests/CMakeLists.txt
+++ b/src/tests/CMakeLists.txt
@@ -20,6 +20,7 @@
matrix-sticker-pack-test.cpp
matrix-link-test.cpp
matrix-user-given-attrs-map-test.cpp
+ kazv-abstract-list-model-test.cpp
LINK_LIBRARIES Qt${QT_MAJOR_VERSION}::Test kazvtestlib
)
diff --git a/src/tests/kazv-abstract-list-model-test.cpp b/src/tests/kazv-abstract-list-model-test.cpp
new file mode 100644
--- /dev/null
+++ b/src/tests/kazv-abstract-list-model-test.cpp
@@ -0,0 +1,104 @@
+/*
+ * This file is part of kazv.
+ * SPDX-FileCopyrightText: 2024 tusooa <tusooa@kazv.moe>
+ * SPDX-License-Identifier: AGPL-3.0-or-later
+ */
+
+#include <kazv-defs.hpp>
+#include <QtTest>
+#include <QSignalSpy>
+#include <lager/state.hpp>
+#include "kazv-abstract-list-model.hpp"
+
+class KazvAbstractListModelTest : public QObject
+{
+ Q_OBJECT
+
+private Q_SLOTS:
+ void testInit();
+ void testConstruct();
+ void testSignals();
+};
+
+class TestListModel : public KazvAbstractListModel
+{
+ Q_OBJECT
+
+public:
+ TestListModel(lager::reader<int> count, InitMethod initMethod, QObject *parent = 0)
+ : KazvAbstractListModel(parent)
+ {
+ initCountCursor(count, initMethod);
+ }
+};
+
+void KazvAbstractListModelTest::testInit()
+{
+ {
+ auto count = lager::make_state(2, lager::automatic_tag{});
+ TestListModel model(count, KazvAbstractListModel::InitNow);
+ QSignalSpy spy(&model, &KazvAbstractListModel::countChanged);
+ QCOMPARE(spy.count(), 0);
+ QCOMPARE(model.count(), 2);
+ QCOMPARE(model.rowCount(QModelIndex()), 2);
+
+ count.set(5);
+ QCOMPARE(model.rowCount(QModelIndex()), 5);
+ }
+
+ {
+ auto count = lager::make_state(2, lager::automatic_tag{});
+ TestListModel model(count, KazvAbstractListModel::InitLater);
+ QSignalSpy spy(&model, &KazvAbstractListModel::countChanged);
+ QCOMPARE(model.rowCount(QModelIndex()), 0);
+ spy.wait();
+ QCOMPARE(spy.count(), 1);
+ QCOMPARE(model.count(), 2);
+ QCOMPARE(model.rowCount(QModelIndex()), 2);
+
+ count.set(5);
+ QCOMPARE(model.rowCount(QModelIndex()), 5);
+ }
+}
+
+void KazvAbstractListModelTest::testConstruct()
+{
+ auto count = lager::make_state(2, lager::automatic_tag{});
+ KazvAbstractListModel model(count, KazvAbstractListModel::InitNow);
+ QSignalSpy spy(&model, &KazvAbstractListModel::countChanged);
+ QCOMPARE(spy.count(), 0);
+ QCOMPARE(model.count(), 2);
+ QCOMPARE(model.rowCount(QModelIndex()), 2);
+
+ count.set(5);
+ QCOMPARE(model.rowCount(QModelIndex()), 5);
+}
+
+void KazvAbstractListModelTest::testSignals()
+{
+ auto count = lager::make_state(2, lager::automatic_tag{});
+ KazvAbstractListModel model(count, KazvAbstractListModel::InitNow);
+ QSignalSpy countChangedSpy(&model, &KazvAbstractListModel::countChanged);
+ QSignalSpy rowsInsertedSpy(&model, &QAbstractItemModel::rowsInserted);
+ QSignalSpy rowsRemovedSpy(&model, &QAbstractItemModel::rowsRemoved);
+
+ // 2 + 3 = 5
+ count.set(5);
+ QCOMPARE(countChangedSpy.count(), 1);
+ QCOMPARE(rowsInsertedSpy.count(), 1);
+ QCOMPARE(rowsInsertedSpy[0][1], 0);
+ QCOMPARE(rowsInsertedSpy[0][2], 2);
+ QCOMPARE(rowsRemovedSpy.count(), 0);
+
+ // 5 - 4 = 1
+ count.set(1);
+ QCOMPARE(countChangedSpy.count(), 2);
+ QCOMPARE(rowsInsertedSpy.count(), 1);
+ QCOMPARE(rowsRemovedSpy.count(), 1);
+ QCOMPARE(rowsRemovedSpy[0][1], 0);
+ QCOMPARE(rowsRemovedSpy[0][2], 3);
+}
+
+QTEST_MAIN(KazvAbstractListModelTest)
+
+#include "kazv-abstract-list-model-test.moc"

File Metadata

Mime Type
text/plain
Expires
Fri, Nov 22, 8:34 AM (1 h, 3 s)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
38830
Default Alt Text
D114.1732293241.diff (25 KB)

Event Timeline