File indexing completed on 2024-05-12 04:20:15
0001 /** 0002 * SPDX-FileCopyrightText: 2001-2015 Klaralvdalens Datakonsult AB. All rights reserved. 0003 * 0004 * This file is part of the KD Chart library. 0005 * 0006 * SPDX-License-Identifier: GPL-2.0-or-later 0007 */ 0008 0009 #include <QtTest/QtTest> 0010 #include <QStandardItemModel> 0011 0012 #include <KChartChart> 0013 #include <KChartGlobal> 0014 #include <KChartBarDiagram> 0015 #include <KChartLineDiagram> 0016 #include <KChartCartesianCoordinatePlane> 0017 #include <KChartLegend> 0018 0019 #include <TableModel.h> 0020 0021 using namespace KChart; 0022 0023 class TestLegends: public QObject { 0024 Q_OBJECT 0025 private Q_SLOTS: 0026 0027 void initTestCase() 0028 { 0029 m_chart = new Chart(nullptr); 0030 m_lines = new LineDiagram(); 0031 m_bars = new BarDiagram(); 0032 m_tableModel = new TableModel( this ); 0033 m_tableModel->loadFromCSV( "../../examples/tools/modeldata/KChart-Test-Datatables.csv" ); 0034 m_lines->setModel( m_tableModel ); 0035 m_bars->setModel( m_tableModel ); 0036 m_chart->coordinatePlane()->replaceDiagram( m_lines ); 0037 } 0038 0039 void testIntialOwnership() 0040 { 0041 Legend* l = new Legend( m_chart ); 0042 m_chart->addLegend( l ); 0043 QCOMPARE( m_chart->legend(), l ); 0044 QCOMPARE( l->diagram(), (AbstractDiagram*)nullptr); 0045 l->setDiagram( m_lines ); 0046 QCOMPARE( dynamic_cast< LineDiagram * >(l->diagram()), m_lines ); 0047 } 0048 0049 void testIntialOwnershipFromCtor() 0050 { 0051 Legend* l = new Legend( m_lines, m_chart ); 0052 m_chart->replaceLegend( l ); 0053 QCOMPARE( m_chart->legend(), l ); 0054 QCOMPARE( dynamic_cast< LineDiagram * >(l->diagram()), m_lines ); 0055 } 0056 0057 void testReplacing() 0058 { 0059 Legend* l = new Legend( m_chart ); 0060 QPointer<Legend> oldLegend = m_chart->legend(); 0061 QCOMPARE( dynamic_cast< LineDiagram * >(oldLegend->diagram()), m_lines ); 0062 m_chart->replaceLegend( l, oldLegend ); 0063 QVERIFY( oldLegend.isNull() ); 0064 QCOMPARE( l->diagram(), (AbstractDiagram*)nullptr ); 0065 l->setDiagram( m_lines ); 0066 QCOMPARE( dynamic_cast< LineDiagram * >(l->diagram()), m_lines ); 0067 } 0068 0069 void testReferenceArea() 0070 { 0071 Legend* l = new Legend( ); 0072 QCOMPARE( l->referenceArea(), ( const QWidget* )nullptr ); 0073 l->setReferenceArea( m_chart ); 0074 QCOMPARE( dynamic_cast< const Chart * >(l->referenceArea()), const_cast< const Chart * >(m_chart) ); 0075 Legend* l1 = new Legend( m_chart ); 0076 QCOMPARE( dynamic_cast< const Chart * >(l1->referenceArea()), const_cast< const Chart * >(m_chart) ); 0077 Legend* l2 = new Legend( m_lines, m_chart ); 0078 QCOMPARE( dynamic_cast< const Chart * >(l2->referenceArea()), const_cast< const Chart * >(m_chart) ); 0079 } 0080 0081 void testDiagramOwnership() 0082 { 0083 Legend* l = new Legend( m_chart ); 0084 QVERIFY( l->diagrams().size() == 0 ); 0085 l->addDiagram( m_lines ); 0086 QVERIFY( l->diagrams().size() == 1 ); 0087 l->addDiagram( m_bars ); 0088 QVERIFY( l->diagrams().size() == 2 ); 0089 QCOMPARE( dynamic_cast< LineDiagram * >(l->diagram()), m_lines ); 0090 l->removeDiagram( m_lines ); 0091 QVERIFY( l->diagrams().size() == 1 ); 0092 QCOMPARE( dynamic_cast< BarDiagram * >(l->diagram()), m_bars ); 0093 l->replaceDiagram( m_lines, m_bars ); 0094 QVERIFY( l->diagrams().size() == 1 ); 0095 QCOMPARE( dynamic_cast< LineDiagram * >(l->diagram()), m_lines ); 0096 } 0097 0098 void testLegendSettings() 0099 { 0100 Legend* l = new Legend( m_lines, m_chart ); 0101 QVERIFY( l->position() == Position::NorthEast ); 0102 QVERIFY( l->alignment() == Qt::AlignCenter ); 0103 QVERIFY( l->orientation() == Qt::Vertical ); 0104 QVERIFY( l->showLines() == false ); 0105 QVERIFY( !l->titleText().isEmpty() ); 0106 QVERIFY( l->spacing() == 1 ); 0107 QVERIFY( l->legendStyle() == Legend::MarkersOnly ); 0108 l->setPosition( Position::North ); 0109 l->setAlignment( Qt::AlignLeft ); 0110 l->setOrientation( Qt::Horizontal ); 0111 l->setShowLines( true ); 0112 l->setTitleText( QStringLiteral( "Lines" ) ); 0113 l->setSpacing( 2 ); 0114 l->setLegendStyle( Legend::LinesOnly ); 0115 QVERIFY( l->position() == Position::North ); 0116 QVERIFY( l->alignment() == Qt::AlignLeft ); 0117 QVERIFY( l->orientation() == Qt::Horizontal ); 0118 QVERIFY( l->showLines() == true ); 0119 QVERIFY( l->titleText() == QStringLiteral( "Lines" ) ); 0120 QVERIFY( l->spacing() == 2 ); 0121 QVERIFY( l->legendStyle() == Legend::LinesOnly ); 0122 } 0123 0124 0125 void cleanupTestCase() 0126 { 0127 } 0128 0129 private: 0130 Chart *m_chart; 0131 BarDiagram *m_bars; 0132 LineDiagram *m_lines; 0133 TableModel *m_tableModel; 0134 0135 }; 0136 0137 QTEST_MAIN(TestLegends) 0138 0139 #include "main.moc"