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

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