File indexing completed on 2024-11-10 05:11:07

0001 /*
0002  * Copyright 2018 by Marco Martin <mart@kde.org>
0003  *
0004  * Licensed under the Apache License, Version 2.0 (the "License");
0005  * you may not use this file except in compliance with the License.
0006  * You may obtain a copy of the License at
0007  *
0008  *    http://www.apache.org/licenses/LICENSE-2.0
0009  *
0010  * Unless required by applicable law or agreed to in writing, software
0011  * distributed under the License is distributed on an "AS IS" BASIS,
0012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0013  * See the License for the specific language governing permissions and
0014  * limitations under the License.
0015  *
0016  */
0017 
0018 #include <QtTest>
0019 #include <QWebSocket>
0020 #include <QWebSocketServer>
0021 #include <QAbstractItemModel>
0022 #include <QQuickView>
0023 #include <QQmlEngine>
0024 #include <QAbstractItemModelTester>
0025 #include "../import/mycroftcontroller.h"
0026 #include "../import/abstractdelegate.h"
0027 #include "../import/filereader.h"
0028 #include "../import/globalsettings.h"
0029 #include "../import/activeskillsmodel.h"
0030 #include "../import/delegatesmodel.h"
0031 #include "../import/abstractskillview.h"
0032 #include "../import/sessiondatamap.h"
0033 #include "../import/sessiondatamodel.h"
0034 
0035 class ModelTest : public QObject
0036 {
0037     Q_OBJECT
0038 
0039 public Q_SLOTS:
0040     void initTestCase();
0041 
0042 private Q_SLOTS:
0043     void testActiveSkillsModel();
0044     void testDelegatesModel();
0045     void testSessionDataModel();
0046 
0047 private:
0048     AbstractSkillView *m_view;
0049     ActiveSkillsModel *m_skillsModel;
0050     DelegatesModel *m_delegatesModel;
0051     SessionDataModel *m_sessionDataModel;
0052 };
0053 
0054 
0055 void ModelTest::initTestCase()
0056 {
0057     m_view = new AbstractSkillView();
0058     m_skillsModel = new ActiveSkillsModel(this);
0059     m_delegatesModel = new DelegatesModel(this);
0060     m_sessionDataModel = new SessionDataModel(this);
0061 
0062     new QAbstractItemModelTester(m_skillsModel, QAbstractItemModelTester::FailureReportingMode::QtTest, this);
0063     new QAbstractItemModelTester(m_delegatesModel, QAbstractItemModelTester::FailureReportingMode::QtTest, this);
0064     new QAbstractItemModelTester(m_sessionDataModel, QAbstractItemModelTester::FailureReportingMode::QtTest, this);
0065 }
0066 
0067 void ModelTest::testActiveSkillsModel()
0068 {
0069     m_skillsModel->insertSkills(0, QStringList({QStringLiteral("skill0"), QStringLiteral("skill1"), QStringLiteral("skill2"), QStringLiteral("skill3")}));
0070     m_skillsModel->moveRows(QModelIndex(), 2, 1, QModelIndex(), 1);
0071     m_skillsModel->moveRows(QModelIndex(), 2, 2, QModelIndex(), 1);
0072     m_skillsModel->moveRows(QModelIndex(), 0, 2, QModelIndex(), 3);
0073     m_skillsModel->moveRows(QModelIndex(), 0, 2, QModelIndex(), 4);
0074     m_skillsModel->removeRows(1, 2);
0075     m_skillsModel->insertSkills(2, QStringList({QStringLiteral("newSkill")}));
0076 }
0077 
0078 void ModelTest::testDelegatesModel()
0079 {
0080     new QAbstractItemModelTester(m_delegatesModel, QAbstractItemModelTester::FailureReportingMode::QtTest, this);
0081     m_delegatesModel->insertDelegateLoaders(0, {new DelegateLoader(m_view), new DelegateLoader(m_view), new DelegateLoader(m_view), new DelegateLoader(m_view)});
0082     m_delegatesModel->moveRows(QModelIndex(), 2, 1, QModelIndex(), 1);
0083     m_delegatesModel->moveRows(QModelIndex(), 2, 2, QModelIndex(), 1);
0084     m_delegatesModel->moveRows(QModelIndex(), 0, 2, QModelIndex(), 3);
0085     m_delegatesModel->moveRows(QModelIndex(), 0, 1, QModelIndex(), 4);
0086     m_delegatesModel->removeRows(1, 2);
0087     m_delegatesModel->insertDelegateLoaders(0, {new DelegateLoader(m_view)});
0088 }
0089 
0090 void ModelTest::testSessionDataModel()
0091 {
0092     m_sessionDataModel->insertData(0, QList<QVariantMap> ({{{QStringLiteral("prop"), QStringLiteral("value1")}}, {{QStringLiteral("prop"), QStringLiteral("value2")}},  {{QStringLiteral("prop"), QStringLiteral("value3")}}, {{QStringLiteral("prop"), QStringLiteral("value4")}}}));
0093     m_sessionDataModel->moveRows(QModelIndex(), 2, 1, QModelIndex(), 1);
0094     m_sessionDataModel->moveRows(QModelIndex(), 2, 2, QModelIndex(), 1);
0095     m_sessionDataModel->moveRows(QModelIndex(), 0, 2, QModelIndex(), 3);
0096     m_sessionDataModel->moveRows(QModelIndex(), 0, 2, QModelIndex(), 4);
0097     m_sessionDataModel->removeRows(1, 2);
0098     m_sessionDataModel->insertData(2, QList<QVariantMap> ({{{QStringLiteral("prop"), QStringLiteral("newValue")}}}));
0099     QCOMPARE(m_sessionDataModel->data(m_sessionDataModel->index(2, 0), m_sessionDataModel->roleNames().key("prop")).toString(), QStringLiteral("newValue"));
0100 }
0101 
0102 QTEST_MAIN(ModelTest);
0103 
0104 #include "modeltest.moc"