File indexing completed on 2024-05-12 05:40:48
0001 /*************************************************************************** 0002 * Copyright (C) 2011 by Renaud Guezennec * 0003 * http://renaudguezennec.homelinux.org/accueil,3.html * 0004 * * 0005 * Rolisteam is free software; you can redistribute it and/or modify * 0006 * it under the terms of the GNU General Public License as published by * 0007 * the Free Software Foundation; either version 2 of the License, or * 0008 * (at your option) any later version. * 0009 * * 0010 * This program is distributed in the hope that it will be useful, * 0011 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 0012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 0013 * GNU General Public License for more details. * 0014 * * 0015 * You should have received a copy of the GNU General Public License * 0016 * along with this program; if not, write to the * 0017 * Free Software Foundation, Inc., * 0018 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * 0019 ***************************************************************************/ 0020 #include <QtTest/QtTest> 0021 0022 #include <QSignalSpy> 0023 #include <QUrl> 0024 0025 #include "controller/view_controller/mindmapcontroller.h" 0026 #include "controller/view_controller/sidemenucontroller.h" 0027 #include <QAbstractItemModelTester> 0028 #include <memory> 0029 0030 using namespace mindmap; 0031 0032 class SideMenuControllerTest : public QObject 0033 { 0034 Q_OBJECT 0035 0036 public: 0037 SideMenuControllerTest(); 0038 0039 private slots: 0040 void init(); 0041 void criteria(); 0042 void criteria_data(); 0043 0044 private: 0045 std::unique_ptr<mindmap::SideMenuController> m_ctrl; 0046 std::unique_ptr<MindMapController> m_mindmap; 0047 }; 0048 0049 SideMenuControllerTest::SideMenuControllerTest() {} 0050 0051 void SideMenuControllerTest::init() 0052 { 0053 m_mindmap.reset(new MindMapController("mindmaptest")); 0054 m_ctrl.reset(new mindmap::SideMenuController()); 0055 } 0056 0057 void initModel(mindmap::MindItemModel* model) 0058 { 0059 new QAbstractItemModelTester(model); 0060 auto l= model->addItem({}, MindItem::NodeType); 0061 0062 l.first->setText("foo"); 0063 l.first->setId("toto"); 0064 auto node1= dynamic_cast<MindNode*>(l.first); 0065 node1->setTags({"tag1", "tag2", "blue", "yellow"}); 0066 node1->setDescription("Description of node1"); 0067 0068 auto l2= model->addItem(l.first->id(), MindItem::NodeType); 0069 l2.first->setText("bar"); 0070 auto node2= dynamic_cast<MindNode*>(l2.first); 0071 node2->setTags({"tag1", "yellow", "mouse"}); 0072 node2->setDescription("bla.. bla.."); 0073 0074 auto l3= model->addItem(l2.first->id(), MindItem::NodeType); 0075 l3.first->setText("characters"); 0076 auto node3= dynamic_cast<MindNode*>(l3.first); 0077 node3->setTags({"pc", "failure", "coffe"}); 0078 node3->setDescription("The characters"); 0079 0080 auto l4= model->addItem(l.first->id(), MindItem::NodeType); 0081 l4.first->setText("sentiment"); 0082 auto node4= dynamic_cast<MindNode*>(l4.first); 0083 node4->setTags({"samourai", "sword", "killer"}); 0084 node4->setDescription("hello world!"); 0085 } 0086 0087 void SideMenuControllerTest::criteria() 0088 { 0089 QFETCH(FilteredModel::Criteria, criteria); 0090 QFETCH(QString, pattern); 0091 QFETCH(int, count); 0092 0093 initModel(m_mindmap->itemModel()); 0094 0095 QSignalSpy spyCtrl(m_ctrl.get(), &SideMenuController::controllerChanged); 0096 QSignalSpy spyPat(m_ctrl.get(), &SideMenuController::patternChanged); 0097 QSignalSpy spyCrit(m_ctrl.get(), &SideMenuController::criteriaChanged); 0098 0099 m_ctrl->setController(m_mindmap.get()); 0100 spyCtrl.wait(10); 0101 QCOMPARE(spyCtrl.count(), 1); 0102 m_ctrl->setController(m_mindmap.get()); 0103 spyCtrl.wait(10); 0104 QCOMPARE(spyCtrl.count(), 1); 0105 QCOMPARE(m_ctrl->controller(), m_mindmap.get()); 0106 0107 if(m_ctrl->pattern() != pattern) 0108 { 0109 m_ctrl->setPattern(pattern); 0110 spyPat.wait(10); 0111 QCOMPARE(spyPat.count(), 1); 0112 m_ctrl->setPattern(pattern); 0113 spyPat.wait(10); 0114 QCOMPARE(spyPat.count(), 1); 0115 QCOMPARE(m_ctrl->pattern(), pattern); 0116 } 0117 0118 if(m_ctrl->criteria() != criteria) 0119 { 0120 m_ctrl->setCriteria(static_cast<FilteredModel::Criteria>(criteria)); 0121 spyCrit.wait(10); 0122 QCOMPARE(spyCrit.count(), 1); 0123 m_ctrl->setCriteria(static_cast<FilteredModel::Criteria>(criteria)); 0124 spyCrit.wait(10); 0125 QCOMPARE(spyCrit.count(), 1); 0126 QCOMPARE(m_ctrl->criteria(), criteria); 0127 } 0128 0129 if(m_ctrl->model()->rowCount() != count) 0130 { 0131 for(int i= 0; i < m_ctrl->model()->rowCount(); ++i) 0132 { 0133 qDebug() << m_ctrl->model()->data(m_ctrl->model()->index(i, 0)); 0134 } 0135 } 0136 QCOMPARE(m_ctrl->model()->rowCount(), count); 0137 } 0138 void SideMenuControllerTest::criteria_data() 0139 { 0140 QTest::addColumn<FilteredModel::Criteria>("criteria"); 0141 QTest::addColumn<QString>("pattern"); 0142 QTest::addColumn<int>("count"); 0143 0144 QTest::addRow("empty") << FilteredModel::NoCrit << QString() << 4; 0145 QTest::addRow("empty name") << FilteredModel::NameCrit << QString() << 4; 0146 QTest::addRow("empty tag") << FilteredModel::TagCrit << QString() << 4; 0147 QTest::addRow("empty desc") << FilteredModel::DescriptionCrit << QString() << 4; 0148 QTest::addRow("empty parent") << FilteredModel::ParentOfCrit << QString() << 4; 0149 QTest::addRow("empty all") << FilteredModel::AllCrit << QString() << 4; 0150 0151 QTest::addRow("one letter") << FilteredModel::NoCrit << QString("a") << 4; 0152 QTest::addRow("one letter name") << FilteredModel::NameCrit << QString("a") << 2; 0153 QTest::addRow("one letter tag") << FilteredModel::TagCrit << QString("a") << 4; 0154 QTest::addRow("one letter desc") << FilteredModel::DescriptionCrit << QString("a") << 2; 0155 QTest::addRow("one letter all") << FilteredModel::AllCrit << QString("a") << 4; 0156 0157 QTest::addRow("two letters") << FilteredModel::NoCrit << QString("ta") << 4; 0158 QTest::addRow("two letters name") << FilteredModel::NameCrit << QString("ta") << 0; 0159 QTest::addRow("two letters tag") << FilteredModel::TagCrit << QString("ta") << 2; 0160 QTest::addRow("two letters desc") << FilteredModel::DescriptionCrit << QString("ta") << 0; 0161 QTest::addRow("two letters all") << FilteredModel::AllCrit << QString("ta") << 2; 0162 0163 QTest::addRow("three letters") << FilteredModel::NoCrit << QString("har") << 4; 0164 QTest::addRow("three letters name") << FilteredModel::NameCrit << QString("har") << 1; 0165 QTest::addRow("three letters tag") << FilteredModel::TagCrit << QString("har") << 0; 0166 QTest::addRow("three letters desc") << FilteredModel::DescriptionCrit << QString("har") << 1; 0167 QTest::addRow("three letters all") << FilteredModel::AllCrit << QString("har") << 1; 0168 0169 QTest::addRow("ParentOfCrit 1") << FilteredModel::ParentOfCrit << QString("tatz!") << 0; 0170 QTest::addRow("ParentOfCrit 2") << FilteredModel::ParentOfCrit << QString("toto") << 2; 0171 } 0172 0173 QTEST_MAIN(SideMenuControllerTest); 0174 0175 #include "tst_mindmapsidemenucontroller.moc"