File indexing completed on 2024-05-12 15:53:54

0001 /**
0002  * Copyright (C) 2001-2015 Klaralvdalens Datakonsult AB.  All rights reserved.
0003  *
0004  * This file is part of the KD Chart library.
0005  *
0006  * This program is free software; you can redistribute it and/or
0007  * modify it under the terms of the GNU General Public License as
0008  * published by the Free Software Foundation; either version 2 of
0009  * the License, or (at your option) any later version.
0010  *
0011  * This program is distributed in the hope that it will be useful,
0012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0014  * GNU General Public License for more details.
0015  *
0016  * You should have received a copy of the GNU General Public License
0017  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
0018  */
0019 
0020 #include <QtTest/QtTest>
0021 #include <QStandardItemModel>
0022 
0023 #include <KChartChart>
0024 #include <KChartGlobal>
0025 #include <KChartBarDiagram>
0026 #include <KChartLineDiagram>
0027 #include <KChartCartesianCoordinatePlane>
0028 #include <KChartLegend>
0029 
0030 #include <TableModel.h>
0031 
0032 using namespace KChart;
0033 
0034 class TestLegends: public QObject {
0035     Q_OBJECT
0036 private Q_SLOTS:
0037 
0038     void initTestCase()
0039     {
0040         m_chart = new Chart(nullptr);
0041         m_lines = new LineDiagram();
0042         m_bars = new BarDiagram();
0043         m_tableModel = new TableModel( this );
0044         m_tableModel->loadFromCSV( "../../examples/tools/modeldata/KChart-Test-Datatables.csv" );
0045         m_lines->setModel( m_tableModel );
0046         m_bars->setModel( m_tableModel );
0047         m_chart->coordinatePlane()->replaceDiagram( m_lines );
0048     }
0049 
0050     void testIntialOwnership()
0051     {
0052         Legend* l = new Legend( m_chart );
0053         m_chart->addLegend( l );
0054         QCOMPARE( m_chart->legend(), l );
0055         QCOMPARE( l->diagram(), (AbstractDiagram*)nullptr);
0056         l->setDiagram( m_lines );
0057         QCOMPARE( dynamic_cast< LineDiagram * >(l->diagram()), m_lines );
0058     }
0059 
0060     void testIntialOwnershipFromCtor()
0061     {
0062         Legend* l = new Legend( m_lines, m_chart );
0063         m_chart->replaceLegend( l );
0064         QCOMPARE( m_chart->legend(), l );
0065         QCOMPARE( dynamic_cast< LineDiagram * >(l->diagram()), m_lines );
0066     }
0067 
0068     void testReplacing()
0069     {
0070         Legend* l = new Legend( m_chart );
0071         QPointer<Legend> oldLegend = m_chart->legend();
0072         QCOMPARE( dynamic_cast< LineDiagram * >(oldLegend->diagram()), m_lines );
0073         m_chart->replaceLegend( l, oldLegend );
0074         QVERIFY( oldLegend.isNull() );
0075         QCOMPARE( l->diagram(), (AbstractDiagram*)nullptr );
0076         l->setDiagram( m_lines );
0077         QCOMPARE( dynamic_cast< LineDiagram * >(l->diagram()), m_lines );
0078     }
0079 
0080     void testReferenceArea()
0081     {
0082          Legend* l = new Legend( );
0083          QCOMPARE( l->referenceArea(), ( const QWidget* )nullptr );
0084          l->setReferenceArea( m_chart );
0085          QCOMPARE( dynamic_cast< const Chart * >(l->referenceArea()), const_cast< const Chart * >(m_chart) );
0086          Legend* l1 = new Legend( m_chart );
0087          QCOMPARE( dynamic_cast< const Chart * >(l1->referenceArea()), const_cast< const Chart * >(m_chart) );
0088          Legend* l2 = new Legend( m_lines,  m_chart );
0089          QCOMPARE( dynamic_cast< const Chart * >(l2->referenceArea()), const_cast< const Chart * >(m_chart) );
0090     }
0091 
0092     void testDiagramOwnership()
0093     {
0094         Legend* l = new Legend( m_chart );
0095         QVERIFY( l->diagrams().size() == 0 );
0096         l->addDiagram( m_lines );
0097         QVERIFY( l->diagrams().size() == 1 );
0098         l->addDiagram( m_bars );
0099         QVERIFY( l->diagrams().size() == 2 );
0100         QCOMPARE( dynamic_cast< LineDiagram * >(l->diagram()),  m_lines );
0101         l->removeDiagram( m_lines );
0102         QVERIFY( l->diagrams().size() == 1 );
0103         QCOMPARE( dynamic_cast< BarDiagram * >(l->diagram()),  m_bars );
0104         l->replaceDiagram( m_lines, m_bars );
0105         QVERIFY( l->diagrams().size() == 1 );
0106         QCOMPARE( dynamic_cast< LineDiagram * >(l->diagram()),  m_lines );
0107     }
0108 
0109     void testLegendSettings()
0110     {
0111        Legend* l = new Legend( m_lines,  m_chart );
0112        QVERIFY( l->position() == Position::NorthEast );
0113        QVERIFY( l->alignment() == Qt::AlignCenter );
0114        QVERIFY( l->orientation() == Qt::Vertical );
0115        QVERIFY( l->showLines() == false );
0116        QVERIFY( !l->titleText().isEmpty() );
0117        QVERIFY( l->spacing() == 1 );
0118        QVERIFY( l->legendStyle() == Legend::MarkersOnly );
0119        l->setPosition( Position::North );
0120        l->setAlignment( Qt::AlignLeft );
0121        l->setOrientation( Qt::Horizontal );
0122        l->setShowLines( true );
0123        l->setTitleText( QStringLiteral( "Lines" ) );
0124        l->setSpacing( 2 );
0125        l->setLegendStyle( Legend::LinesOnly );
0126        QVERIFY( l->position() == Position::North );
0127        QVERIFY( l->alignment() == Qt::AlignLeft );
0128        QVERIFY( l->orientation() == Qt::Horizontal );
0129        QVERIFY( l->showLines() == true );
0130        QVERIFY( l->titleText() == QStringLiteral( "Lines" ) );
0131        QVERIFY( l->spacing() == 2 );
0132        QVERIFY( l->legendStyle() == Legend::LinesOnly );
0133     }
0134 
0135 
0136     void cleanupTestCase()
0137     {
0138     }
0139 
0140 private:
0141     Chart *m_chart;
0142     BarDiagram *m_bars;
0143     LineDiagram *m_lines;
0144     TableModel *m_tableModel;
0145 
0146 };
0147 
0148 QTEST_MAIN(TestLegends)
0149 
0150 #include "main.moc"