File indexing completed on 2024-05-12 04:20:14

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 
0011 #include <KChartChart>
0012 #include <KChartGlobal>
0013 #include <KChartBarDiagram>
0014 #include <KChartLineDiagram>
0015 #include <KChartCartesianCoordinatePlane>
0016 #include <KChartPolarCoordinatePlane>
0017 #include <KChartLegend>
0018 #include <KChartHeaderFooter>
0019 
0020 using namespace KChart;
0021 
0022 class TestChartElementOwnership: public QObject {
0023     Q_OBJECT
0024 private Q_SLOTS:
0025 
0026     void initTestCase()
0027     {
0028         m_chart = new Chart(nullptr);
0029     }
0030 
0031     void testPlaneOwnership()
0032     {
0033         // check number plane
0034         AbstractCoordinatePlane*orig = m_chart->coordinatePlane();
0035         QCOMPARE( m_chart->coordinatePlanes().size(), 1 );
0036 
0037         // add and take
0038         CartesianCoordinatePlane *p = new CartesianCoordinatePlane();
0039         m_chart->addCoordinatePlane( p );
0040         QCOMPARE( m_chart->coordinatePlanes().size(), 2 );
0041         m_chart->takeCoordinatePlane( orig );
0042         QCOMPARE( m_chart->coordinatePlanes().size(), 1 );
0043         QCOMPARE( dynamic_cast< CartesianCoordinatePlane * >(m_chart->coordinatePlane()), p );
0044         m_chart->addCoordinatePlane( orig );
0045         QCOMPARE( m_chart->coordinatePlanes().size(), 2 );
0046 
0047         // replace abstract by polar
0048         PolarCoordinatePlane *po = new PolarCoordinatePlane();
0049         m_chart->replaceCoordinatePlane( po, orig );
0050         QCOMPARE( m_chart->coordinatePlanes().size(), 2 );
0051         m_chart->takeCoordinatePlane( p );
0052         QCOMPARE( m_chart->coordinatePlanes().size(), 1 );
0053         QCOMPARE( dynamic_cast< PolarCoordinatePlane * >(m_chart->coordinatePlane()), po );
0054         m_chart->addCoordinatePlane( p );
0055         QCOMPARE( m_chart->coordinatePlanes().size(), 2 );
0056 
0057         // delete
0058         delete po;
0059         QCOMPARE( m_chart->coordinatePlanes().size(), 1 );
0060         QCOMPARE( dynamic_cast< CartesianCoordinatePlane * >(m_chart->coordinatePlane()), p );
0061 
0062         // replace cartesian by polar
0063         PolarCoordinatePlane*polast = new PolarCoordinatePlane();
0064         m_chart->replaceCoordinatePlane( polast );
0065         QCOMPARE( m_chart->coordinatePlanes().size(), 1 );
0066         QCOMPARE( dynamic_cast< PolarCoordinatePlane * >(m_chart->coordinatePlane()), polast );
0067 
0068         // replace polar by cartesian
0069         CartesianCoordinatePlane* plast = new CartesianCoordinatePlane();
0070         m_chart->replaceCoordinatePlane( plast,  polast );
0071         QCOMPARE( m_chart->coordinatePlanes().size(), 1 );
0072         QCOMPARE( dynamic_cast< CartesianCoordinatePlane * >(m_chart->coordinatePlane()), plast );
0073 
0074     }
0075 
0076     void testHeaderFooterOwnership()
0077     {
0078         QCOMPARE( m_chart->headerFooters().size(), 0 );
0079         HeaderFooter * h = new HeaderFooter();
0080         m_chart->addHeaderFooter( h );
0081         QCOMPARE( m_chart->headerFooters().size(), 1 );
0082         m_chart->takeHeaderFooter( h );
0083         QCOMPARE( m_chart->headerFooters().size(), 0 );
0084         m_chart->addHeaderFooter( h );
0085         QCOMPARE( m_chart->headerFooters().size(), 1 );
0086         delete h;
0087         QCOMPARE( m_chart->headerFooters().size(), 0 );
0088     }
0089 
0090     void testHeaderFooterReplace()
0091     {
0092         QCOMPARE( m_chart->headerFooters().size(), 0 );
0093         HeaderFooter * h = new HeaderFooter();
0094         HeaderFooter * h1 = new HeaderFooter();
0095         m_chart->addHeaderFooter( h );
0096         QCOMPARE( m_chart->headerFooters().size(), 1 );
0097         m_chart->addHeaderFooter( h1 );
0098         QCOMPARE( m_chart->headerFooters().size(), 2 );
0099         m_chart->takeHeaderFooter( h );
0100         QCOMPARE( m_chart->headerFooters().size(), 1 );
0101         QCOMPARE( m_chart->headerFooter(), h1 );
0102         m_chart->replaceHeaderFooter( h,  h1 );
0103         QCOMPARE( m_chart->headerFooters().size(), 1 );
0104         delete h;
0105         QCOMPARE( m_chart->headerFooters().size(), 0 );
0106     }
0107 
0108 
0109     void testLegendOwnership()
0110     {
0111         // check no legend
0112         QCOMPARE( m_chart->legends().size(), 0 );
0113 
0114         // check add legend - take legend - delete legend
0115         Legend * legend = new Legend( m_chart->coordinatePlane()->diagram() );
0116         m_chart->addLegend( legend );
0117         QCOMPARE( m_chart->legends().size(), 1 );
0118         m_chart->takeLegend( legend );
0119         QCOMPARE( m_chart->legends().size(), 0 );
0120         m_chart->addLegend( legend );
0121         QCOMPARE( m_chart->legends().size(), 1 );
0122         delete legend;
0123         QCOMPARE( m_chart->legends().size(), 0 );
0124     }
0125 
0126     void testLegendReplace()
0127     {
0128 
0129         // check no legend
0130         QCOMPARE( m_chart->legends().size(), 0 );
0131         // check add several legends - take legend
0132         // replace legend - delete legend
0133         Legend * legend = new Legend( m_chart->coordinatePlane()->diagram() );
0134         Legend * legend2 = new Legend( m_chart->coordinatePlane()->diagram() );
0135         m_chart->addLegend( legend );
0136         QCOMPARE( m_chart->legends().size(), 1 );
0137         m_chart->addLegend( legend2 );
0138         QCOMPARE( m_chart->legends().size(), 2 );
0139         m_chart->takeLegend( legend );
0140         QCOMPARE( m_chart->legends().size(), 1 );
0141         QCOMPARE( m_chart->legend(), legend2 );
0142         m_chart->replaceLegend( legend, legend2  );
0143         QCOMPARE( m_chart->legends().size(), 1 );
0144         delete legend;
0145         QCOMPARE( m_chart->legends().size(), 0 );
0146     }
0147 
0148     void testPadding()
0149     {
0150       QVERIFY( m_chart->globalLeadingLeft() == false );
0151       m_chart->setGlobalLeading( 2, 2, 2, 2 );
0152       QCOMPARE( m_chart->globalLeadingLeft(), 2 );
0153       QCOMPARE( m_chart->globalLeadingTop(), 2 );
0154       QCOMPARE( m_chart->globalLeadingRight(), 2 );
0155       QCOMPARE( m_chart->globalLeadingBottom(), 2 );
0156       m_chart->setGlobalLeadingLeft( 5 );
0157       QCOMPARE( m_chart->globalLeadingLeft(), 5 );
0158       QCOMPARE( m_chart->globalLeadingTop(), 2 );
0159       QCOMPARE( m_chart->globalLeadingRight(), 2 );
0160       QCOMPARE( m_chart->globalLeadingBottom(), 2 );
0161       m_chart->setGlobalLeadingTop( 5 );
0162       QCOMPARE( m_chart->globalLeadingLeft(), 5 );
0163       QCOMPARE( m_chart->globalLeadingTop(), 5 );
0164       QCOMPARE( m_chart->globalLeadingRight(), 2 );
0165       QCOMPARE( m_chart->globalLeadingBottom(), 2 );
0166       m_chart->setGlobalLeadingRight( 5 );
0167       QCOMPARE( m_chart->globalLeadingLeft(), 5 );
0168       QCOMPARE( m_chart->globalLeadingTop(), 5 );
0169       QCOMPARE( m_chart->globalLeadingRight(), 5 );
0170       QCOMPARE( m_chart->globalLeadingBottom(), 2 );
0171       m_chart->setGlobalLeadingBottom( 5 );
0172       QCOMPARE( m_chart->globalLeadingLeft(), 5 );
0173       QCOMPARE( m_chart->globalLeadingTop(), 5 );
0174       QCOMPARE( m_chart->globalLeadingRight(), 5 );
0175       QCOMPARE( m_chart->globalLeadingBottom(), 5 );
0176       m_chart->setGlobalLeading( 2, 2, 2, 2 );
0177       QCOMPARE( m_chart->globalLeadingLeft(), 2 );
0178       QCOMPARE( m_chart->globalLeadingTop(), 2 );
0179       QCOMPARE( m_chart->globalLeadingRight(), 2 );
0180       QCOMPARE( m_chart->globalLeadingBottom(), 2 );
0181     }
0182 
0183 
0184     void testChartDeletion()
0185     {
0186         delete m_chart;
0187     }
0188 
0189     void cleanupTestCase()
0190     {
0191     }
0192 
0193 private:
0194     Chart *m_chart;
0195 
0196 };
0197 
0198 QTEST_MAIN(TestChartElementOwnership)
0199 
0200 #include "main.moc"