File indexing completed on 2024-05-19 04:39:50

0001 /*
0002     SPDX-FileCopyrightText: 2008 Andreas Pakulat <apaku@gmx.de>
0003     SPDX-FileCopyrightText: 2008 Manuel Breugelmans <mbr.nxi@gmail.com>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #include "test_toolviewtoolbar.h"
0009 
0010 #include <QTest>
0011 
0012 #include <QTextEdit>
0013 #include <QToolBar>
0014 #include <QStandardPaths>
0015 
0016 #include <sublime/view.h>
0017 #include <sublime/area.h>
0018 #include <sublime/controller.h>
0019 #include <sublime/mainwindow.h>
0020 #include <sublime/tooldocument.h>
0021 
0022 using namespace Sublime;
0023 
0024 class ToolViewToolBarFactory : public SimpleToolWidgetFactory<QTextEdit> {
0025 public:
0026     explicit ToolViewToolBarFactory(const QString &id): SimpleToolWidgetFactory<QTextEdit>(id) {}
0027     QList<QAction*> toolBarActions( QWidget* ) const override
0028     {
0029         return actionList;
0030     }
0031 
0032     ~ToolViewToolBarFactory() override
0033     {
0034         qDeleteAll(actionList);
0035     }
0036 
0037     void addAction(const QString &text)
0038     {
0039         auto* action = new QAction(text, nullptr);
0040         actionList.append(action);
0041     }
0042 
0043 private:
0044     QList<QAction*> actionList;
0045 };
0046 
0047 void TestToolViewToolBar::initTestCase()
0048 {
0049     QStandardPaths::setTestModeEnabled(true);
0050 }
0051 
0052 void TestToolViewToolBar::init()
0053 {
0054     // this is starting to become a GeneralFixture
0055     controller = new Controller(this);
0056     area = new Area( controller, QStringLiteral("Area") );
0057     auto* mw = new MainWindow(controller);
0058 
0059     // a horizontal tool with toolbar
0060     auto* factoryT1 = new ToolViewToolBarFactory(QStringLiteral("tool1factory"));
0061     actionTextT1 = QStringLiteral("Tool1Action");
0062     factoryT1->addAction(actionTextT1);
0063     tool1 = new ToolDocument( QStringLiteral("tool1"), controller, factoryT1 );
0064     viewT11 = tool1->createView();
0065     area->addToolView( viewT11, Sublime::Bottom );
0066 
0067     // a vertical tool with toolbar
0068     auto* factoryT2 = new ToolViewToolBarFactory(QStringLiteral("tool2factory"));
0069     actionTextT2 = QStringLiteral("Tool2Action");
0070     factoryT2->addAction(actionTextT2);
0071     tool2 = new ToolDocument( QStringLiteral("tool2"), controller, factoryT2 );
0072     viewT21 = tool2->createView();
0073     area->addToolView( viewT21, Sublime::Left );
0074 
0075     controller->showArea(area, mw);
0076 }
0077 
0078 void TestToolViewToolBar::cleanup()
0079 {
0080     delete controller;
0081 }
0082 
0083 QToolBar* TestToolViewToolBar::fetchToolBarFor(Sublime::View* view)
0084 {
0085     QWidget* toolWidget = view->widget();
0086     const char* loc = "fetchToolBarFor";
0087     Q_UNUSED(loc);
0088     Q_ASSERT_X(toolWidget, loc, "Tool refuses to create widget (null).");
0089     Q_ASSERT(toolWidget->parent());
0090     auto* toolWin = qobject_cast<QMainWindow*>(toolWidget->parent());
0091     Q_ASSERT_X(toolWin, loc, "Tool widget's parent is not a QMainWindow.");
0092     QList<QToolBar*> toolBars = toolWin->findChildren<QToolBar*>();
0093     int barCount = toolBars.count();
0094     char* failMsg = qstrdup(QStringLiteral("Expected to find a toolbar but found %1").arg(barCount).toLatin1().data());
0095     Q_UNUSED(failMsg);
0096     Q_ASSERT_X(barCount == 1, loc, failMsg);
0097     delete [] failMsg;
0098     return toolBars.at(0);
0099 }
0100 
0101 void TestToolViewToolBar::assertGoodBar(QToolBar* toolbar, const QString& actionText)
0102 {
0103     QVERIFY( toolbar );
0104     QVERIFY( !toolbar->isFloatable() );
0105     QCOMPARE( toolbar->iconSize(), QSize( 16, 16 ) );
0106     QList<QAction*> actions = toolbar->actions();
0107     QCOMPARE( actions.count(), 1 );
0108     QCOMPARE( actions.at(0)->text(), actionText);
0109     QCOMPARE( toolbar->orientation(), Qt::Horizontal );
0110 }
0111 
0112 void TestToolViewToolBar::horizontalTool()
0113 {
0114     // viewT11 was added with Sublime::Bottom, so it should have a horizontal bar
0115     QToolBar* bar = fetchToolBarFor(viewT11);
0116     assertGoodBar(bar, actionTextT1);
0117 }
0118 
0119 void TestToolViewToolBar::verticalTool()
0120 {
0121     // viewT21 was added with Sublime::Left, so it should have a vertical bar
0122     QToolBar* bar = fetchToolBarFor(viewT21);
0123     assertGoodBar(bar, actionTextT2);
0124 }
0125 
0126 void TestToolViewToolBar::toolViewMove()
0127 {
0128     area->moveToolView( viewT11, Sublime::Right );
0129     area->moveToolView( viewT21, Sublime::Bottom );
0130     QToolBar* barT1 = fetchToolBarFor(viewT11);
0131     QToolBar* barT2 = fetchToolBarFor(viewT21);
0132     assertGoodBar(barT1, actionTextT1);
0133     assertGoodBar(barT2, actionTextT2);
0134 }
0135 
0136 QTEST_MAIN(TestToolViewToolBar)
0137 
0138 #include "moc_test_toolviewtoolbar.cpp"