File indexing completed on 2024-12-22 04:14:57

0001 /*
0002  *  SPDX-FileCopyrightText: 2020 Saurabh Kumar <saurabhk660@gmail.com>
0003  *
0004  *  SPDX-License-Identifier: GPL-2.0-or-later
0005  */
0006 
0007 #include "StoryboardModelTest.h"
0008 
0009 #include <simpletest.h>
0010 #include <QWidget>
0011 #include <StoryboardModel.h>
0012 #include <CommentModel.h>
0013 
0014 void StoryboardModelTest::init()
0015 {
0016     /* TODO: Seems like it's wrong to be testing the storyboard model without image data.
0017      * We should reconsider this later down the line. The model should depend on having an image
0018      * to be valid! */
0019 
0020     m_commentModel = new StoryboardCommentModel(this);
0021     m_storyboardModel = new StoryboardModel(this);
0022     m_storyboardModel->setCommentModel(m_commentModel);
0023 
0024     m_commentModel->insertRows(m_commentModel->rowCount(),1);
0025     int pos = m_storyboardModel->rowCount();
0026     m_storyboardModel->insertRows(pos,1);
0027     m_storyboardModel->insertChildRows(pos);
0028     QCOMPARE(m_commentModel->rowCount(), 1);
0029 }
0030 
0031 
0032 void StoryboardModelTest::cleanup()
0033 {
0034     m_storyboardModel->removeRows(0, m_storyboardModel->rowCount());
0035     m_commentModel->removeRows(0, m_commentModel->rowCount());
0036 
0037     delete m_storyboardModel;
0038     delete m_commentModel;
0039 }
0040 
0041 void StoryboardModelTest::testAddComment()
0042 {
0043 
0044     auto sbTester = new QAbstractItemModelTester(m_storyboardModel, this);
0045     auto commentTester = new QAbstractItemModelTester(m_commentModel, this);
0046     Q_UNUSED(sbTester);
0047     Q_UNUSED(commentTester);
0048 
0049     QModelIndex storyboardItem = m_storyboardModel->index(0,0);
0050     int rowStoryboard = m_storyboardModel->rowCount(storyboardItem);
0051     int commentCount = m_commentModel->rowCount();
0052 
0053     QCOMPARE(rowStoryboard, commentCount + 4);
0054     m_commentModel->insertRows(m_commentModel->rowCount(),1);
0055 
0056     QCOMPARE(commentCount + 1, m_commentModel->rowCount());
0057     QCOMPARE(m_storyboardModel->rowCount(m_storyboardModel->index(0,0)), m_commentModel->rowCount() + 4);
0058 
0059 #if defined(QT_NO_DEBUG) && !defined(QT_FORCE_ASSERTS)
0060     // add at an invalid position
0061     // if assertions are enabled, this will make Qt crash in QAbstractItemModel::beginInsertRows
0062     m_commentModel->insertRows(-1, 1);
0063 
0064     QCOMPARE(commentCount + 1, m_commentModel->rowCount());
0065     QCOMPARE(m_storyboardModel->rowCount(m_storyboardModel->index(0,0)), m_commentModel->rowCount() + 4);
0066 #endif
0067 }
0068 
0069 void StoryboardModelTest::testRemoveComment()
0070 {
0071 
0072     auto sbTester = new QAbstractItemModelTester(m_storyboardModel, this);
0073     auto commentTester = new QAbstractItemModelTester(m_commentModel, this);
0074     Q_UNUSED(sbTester);
0075     Q_UNUSED(commentTester);
0076 
0077     int rowStoryboard = m_storyboardModel->rowCount(m_storyboardModel->index(0,0));
0078     int rowsComment = m_commentModel->rowCount();
0079 
0080     QCOMPARE(rowStoryboard, rowsComment + 4);
0081 
0082     m_commentModel->removeRows(m_commentModel->rowCount() - 1,1);
0083 
0084     QCOMPARE(rowsComment - 1, m_commentModel->rowCount());
0085     QCOMPARE(m_storyboardModel->rowCount(m_storyboardModel->index(0,0)), m_commentModel->rowCount() + 4);
0086 
0087 #if defined(QT_NO_DEBUG) && !defined(QT_FORCE_ASSERTS)
0088     // if assertions are enabled, this will make Qt crash in QAbstractItemModel::beginRemoveRows
0089     m_commentModel->removeRows(-1,1);
0090 
0091     QCOMPARE(rowsComment - 1, m_commentModel->rowCount());
0092     QCOMPARE(m_storyboardModel->rowCount(m_storyboardModel->index(0,0)), m_commentModel->rowCount() + 4);
0093 #endif
0094 }
0095 
0096 void StoryboardModelTest::testCommentNameChanged()
0097 {
0098     auto tester = new QAbstractItemModelTester(m_commentModel, this);
0099     Q_UNUSED(tester);
0100     QModelIndex index = m_commentModel->index(m_commentModel->rowCount() - 1, 0);
0101     QVariant value = QVariant(QString("newValue"));
0102     m_commentModel->setData(index, value);
0103 
0104     QCOMPARE(QString("newValue"), m_commentModel->data(index));
0105 }
0106 
0107 void StoryboardModelTest::testFrameAdded()
0108 {
0109     int rows = m_storyboardModel->rowCount();
0110     auto tester = new QAbstractItemModelTester(m_storyboardModel, 0);
0111     Q_UNUSED(tester);
0112     int pos = m_storyboardModel->rowCount();
0113     m_storyboardModel->insertRows(pos, 1);
0114     m_storyboardModel->insertChildRows(pos);
0115 
0116     QCOMPARE(rows + 1, m_storyboardModel->rowCount());
0117 }
0118 
0119 void StoryboardModelTest::testFrameRemoved()
0120 {
0121     int rows = m_storyboardModel->rowCount();
0122     auto tester = new QAbstractItemModelTester(m_storyboardModel, 0);
0123     Q_UNUSED(tester);
0124     m_storyboardModel->removeRows(m_storyboardModel->rowCount() - 1,1);
0125 
0126     QCOMPARE(rows-1, m_storyboardModel->rowCount());
0127 }
0128 
0129 
0130 void StoryboardModelTest::testFrameChanged()
0131 {
0132 
0133     auto tester = new QAbstractItemModelTester(m_storyboardModel, 0);
0134     Q_UNUSED(tester);
0135     QModelIndex parentIndex = m_storyboardModel->index(m_storyboardModel->rowCount() - 1, 0);
0136     QModelIndex frameIndex = m_storyboardModel->index(0, 0, parentIndex);
0137     QVariant value = QVariant(100);
0138     m_storyboardModel->setData(frameIndex, value, Qt::EditRole);
0139 
0140     QCOMPARE(m_storyboardModel->data(frameIndex).toInt(), 100);
0141 
0142     //invalid value shouldn't change anything
0143     QVariant invalidValue = QVariant(-100);
0144     m_storyboardModel->setData(frameIndex, invalidValue, Qt::EditRole);
0145 
0146     QCOMPARE(m_storyboardModel->data(frameIndex).toInt(), 100);
0147 }
0148 
0149 void StoryboardModelTest::testDurationChanged()
0150 {
0151     auto tester = new QAbstractItemModelTester(m_storyboardModel, 0);
0152     Q_UNUSED(tester);
0153     QModelIndex parentIndex = m_storyboardModel->index(m_storyboardModel->rowCount() - 1, 0);
0154     QModelIndex secIndex = m_storyboardModel->index(2, 0, parentIndex);
0155     QVariant value = QVariant(100);
0156     m_storyboardModel->setData(secIndex, value, Qt::EditRole);
0157 
0158     QCOMPARE(m_storyboardModel->data(secIndex).toInt(), 100);
0159 
0160     //invalid value shouldn't change anything
0161     QVariant invalidValue = QVariant(-100);
0162     m_storyboardModel->setData(secIndex, invalidValue, Qt::EditRole);
0163 
0164     QCOMPARE(m_storyboardModel->data(secIndex).toInt(), 0);
0165 }
0166 
0167 void StoryboardModelTest::testCommentChanged()
0168 {
0169     auto tester = new QAbstractItemModelTester(m_storyboardModel, 0);
0170     Q_UNUSED(tester);
0171     QModelIndex parentIndex = m_storyboardModel->index(m_storyboardModel->rowCount() - 1, 0);
0172     QModelIndex commentIndex = m_storyboardModel->index(4, 0, parentIndex);
0173     QVariant value = QVariant(QString("newComment"));
0174     m_storyboardModel->setData(commentIndex, value, Qt::EditRole);
0175 
0176     QCOMPARE(m_storyboardModel->data(commentIndex).toString(), "newComment");
0177 }
0178 
0179 SIMPLE_TEST_MAIN(StoryboardModelTest)