[vlc-commits] [Git][videolan/vlc][master] 2 commits: qt: add tests for base model

Steve Lhomme (@robUx4) gitlab at videolan.org
Fri Jan 19 09:15:30 UTC 2024



Steve Lhomme pushed to branch master at VideoLAN / VLC


Commits:
4ddc948f by Prince Gupta at 2024-01-19T08:50:06+00:00
qt: add tests for base model

- - - - -
4ea2f636 by Prince Gupta at 2024-01-19T08:50:06+00:00
qt: add test for reset cache

- - - - -


2 changed files:

- modules/gui/qt/Makefile.am
- + modules/gui/qt/tests/test_base_model.cpp


Changes:

=====================================
modules/gui/qt/Makefile.am
=====================================
@@ -1192,6 +1192,19 @@ $(builddir)/../../vlc-qt-check: vlc-qt-check
 endif
 endif
 
+base_model_test_SOURCES = tests/test_base_model.cpp \
+        util/base_model.cpp util/base_model.hpp util/base_model_p.hpp
+
+nodist_base_model_test_SOURCES = util/base_model.moc.cpp \
+        util/listcache.moc.cpp \
+        util/locallistcacheloader.moc.cpp
+
+base_model_test_CXXFLAGS = $(AM_CXXFLAGS) $(QT_CFLAGS) -fPIC $(CXXFLAGS_qt)
+base_model_test_LDADD = $(QT5_PLUGINS_LIBS) $(QT_LIBS) $(LIBS_qt)
+check_PROGRAMS = base_model_test
+TESTS = base_model_test
+
+
 QML_LOG_COMPILER = $(builddir)/qml_test -input
 
 if HAVE_QT5_QUICK_TEST
@@ -1202,8 +1215,8 @@ nodist_qml_test_SOURCES += qmlcache_loader.cpp $(libqt_plugin_la_QML)
 endif
 qml_test_CXXFLAGS = $(AM_CXXFLAGS) $(QT_CFLAGS) -fPIC $(CXXFLAGS_qt) ${QT5_QUICK_TEST_CFLAGS} -DQUICK_TEST_SOURCE_DIR="\"${srcdir}/tests\""
 qml_test_LDADD = $(QT5_PLUGINS_LIBS) ${QT5_QUICK_TEST_LIBS} $(QT_LIBS) $(LIBS_qt)
-check_PROGRAMS = qml_test
+check_PROGRAMS += qml_test
 EXTRA_DIST += tests/tst_FSM.qml
-TESTS = tests/tst_FSM.qml
+TESTS += tests/tst_FSM.qml
 endif
 endif


=====================================
modules/gui/qt/tests/test_base_model.cpp
=====================================
@@ -0,0 +1,206 @@
+/*****************************************************************************
+ * Copyright (C) 2024 VLC authors and VideoLAN
+ *
+ * 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 ) 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 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.
+ *****************************************************************************/
+
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include "../util/base_model.hpp"
+#include "../util/base_model_p.hpp"
+#include "../util/locallistcacheloader.hpp"
+
+#include <memory>
+#include <vector>
+
+#include <QCoreApplication>
+#include <QElapsedTimer>
+#include <QDebug>
+
+
+struct Item
+{
+    int id;
+    Item(int i) : id {i} {}
+};
+
+using ItemPtr = std::shared_ptr<Item>;
+using ItemLoader = LocalListCacheLoader<ItemPtr>;
+
+template<>
+bool ListCache<ItemPtr>::compareItems(const ItemPtr& a, const ItemPtr& b)
+{
+    //just compare the pointers here
+    return a == b;
+}
+
+class ModelPrivate;
+
+class Model : public BaseModel
+{
+    Q_DECLARE_PRIVATE(Model);
+
+public:
+    Model(QObject *parent = nullptr);
+
+    QVariant data(const QModelIndex & index, int role) const override;
+
+    void append(int id);
+
+private:
+};
+
+class ModelPrivate
+        : public BaseModelPrivateT<ItemPtr>
+        , public LocalListCacheLoader<ItemPtr>::ModelSource
+{
+    Q_DECLARE_PUBLIC(Model)
+public:
+    ModelPrivate(Model* pub)
+        : BaseModelPrivateT<ItemPtr>(pub)
+    {}
+
+    ItemLoader::ItemCompare getSortFunction() const
+    {
+        return [](const ItemPtr& l, const ItemPtr& r) -> bool
+        {
+            return l->id < r->id;
+        };
+    }
+
+    std::unique_ptr<ListCacheLoader<ItemPtr>> createLoader() const override
+    {
+        return std::make_unique<ItemLoader>(this, m_searchPattern, getSortFunction());
+    }
+
+    bool initializeModel() override
+    {
+        return true;
+    }
+
+public: //LocalListCacheLoader::ModelSource implementation
+    size_t getModelRevision() const override
+    {
+        return rev;
+    }
+
+    std::vector<ItemPtr> getModelData(const QString& pattern) const override
+    {
+        Q_UNUSED(pattern);
+        return m_items;
+    }
+
+private:
+    size_t rev = 0;
+    std::vector<ItemPtr> m_items;
+};
+
+Model::Model(QObject *parent)
+    : BaseModel(new ModelPrivate(this), parent)
+{
+}
+
+QVariant Model::data(const QModelIndex &index, int role) const
+{
+    Q_D(const Model);
+    Q_UNUSED(role);
+
+    const ItemPtr *item = d->item(index.row());
+    if (!item)
+        return {};
+
+    return (*item)->id;
+}
+
+void Model::append(int id)
+{
+    Q_D(Model);
+
+    d->m_items.push_back(std::make_shared<Item>(id));
+    d->rev++;
+    d->invalidateCache();
+}
+
+void await(int msec)
+{
+    QElapsedTimer timer;
+    timer.start();
+    while (!timer.hasExpired(msec))
+    {
+        QCoreApplication::processEvents(QEventLoop::AllEvents, msec);
+    }
+}
+
+void test_invalidate_on_invalidate()
+{
+    Model m;
+    m.invalidateCache(); // need to force initialize model (FIXME??)
+
+    // bug of MR!4485 MR!4684
+    m.connect(&m, &Model::loadingChanged, [&m]() { m.setLimit(1); });
+    await(1);
+    assert(!m.loading());
+}
+
+
+void test_null_reset()
+{
+    Model m;
+    // should not crash or give out warning
+    // bug of MR!4786
+    m.resetCache();
+}
+
+void test_sanity()
+{
+    Model m;
+    m.invalidateCache(); // need to force initialize model (FIXME??)
+
+    m.setLimit(2);
+    m.append(1);
+    m.append(2);
+    await(1);
+    assert(m.getCount() == 2);
+
+    m.setLimit(1);
+    await(1);
+    assert(m.getCount() == 1);
+    assert(m.getMaximumCount() == 2);
+
+    m.setLimit(2);
+    await(1);
+    assert(m.getCount() == 2);
+    for (int i = 0; i < 2; ++i)
+    {
+        assert(m.data(m.index(i), 0) == i + 1);
+    }
+}
+
+int main(int argc, char **argv)
+{
+    QCoreApplication a(argc, argv);
+    QMetaObject::invokeMethod(&a, [&a]()
+    {
+        test_null_reset();
+        test_invalidate_on_invalidate();
+        test_sanity();
+
+        a.quit();
+    }, Qt::QueuedConnection);
+    return a.exec();
+}



View it on GitLab: https://code.videolan.org/videolan/vlc/-/compare/d8f9556dae59464ef74abd895c4c0bc25af1bf3f...4ea2f636707f67ccb6be74fb51da86b0cb120519

-- 
View it on GitLab: https://code.videolan.org/videolan/vlc/-/compare/d8f9556dae59464ef74abd895c4c0bc25af1bf3f...4ea2f636707f67ccb6be74fb51da86b0cb120519
You're receiving this email because of your account on code.videolan.org.


VideoLAN code repository instance


More information about the vlc-commits mailing list