1 /** 2 * Copyright (C) 2001-2015 Klaralvdalens Datakonsult AB. All rights reserved. 3 * 4 * This file is part of the KD Chart library. 5 * 6 * This program is free software; you can redistribute it and/or 7 * modify it under the terms of the GNU General Public License as 8 * published by the Free Software Foundation; either version 2 of 9 * the License, or (at your option) any later version. 10 * 11 * This program is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with this program. If not, see <https://www.gnu.org/licenses/>. 18 */ 19 20 #include <QtTest/QtTest> 21 #include <KChartChart> 22 #include <KChartGlobal> 23 #include <KChartBarDiagram> 24 #include <KChartThreeDBarAttributes> 25 #include <KChartCartesianCoordinatePlane> 26 27 #include <TableModel.h> 28 29 using namespace KChart; 30 31 class TestBarDiagrams: public QObject { 32 Q_OBJECT 33 private Q_SLOTS: 34 initTestCase()35 void initTestCase() 36 { 37 m_chart = new Chart(nullptr); 38 m_model = new TableModel( nullptr ); 39 m_model->loadFromCSV( ":/data" ); 40 m_bars = new BarDiagram(); 41 m_bars->setModel( m_model ); 42 m_chart->coordinatePlane()->replaceDiagram( m_bars ); 43 } 44 testBarDiagramType()45 void testBarDiagramType() 46 { 47 QCOMPARE( m_bars->type(), BarDiagram::Normal ); 48 m_bars->setType( BarDiagram::Stacked ); 49 QCOMPARE( m_bars->type(), BarDiagram::Stacked ); 50 m_bars->setType( BarDiagram::Percent ); 51 QCOMPARE( m_bars->type(), BarDiagram::Percent ); 52 #ifdef TEMPORARILY_REMOVED 53 m_bars->setType( BarDiagram::Rows ); 54 QCOMPARE( m_bars->type(), BarDiagram::Rows ); 55 #endif 56 //reset to normal 57 m_bars->setType( BarDiagram::Normal ); 58 } 59 testBarAttributesLevelSettings()60 void testBarAttributesLevelSettings() 61 { 62 //check segments 63 const int rows = m_bars->model()->rowCount(); 64 QCOMPARE( m_bars->numberOfAbscissaSegments(), rows ); 65 const int cols = m_bars->model()->columnCount(); 66 QCOMPARE( m_bars->numberOfOrdinateSegments(), cols ); 67 QModelIndex idx = m_model->index(rows-3, cols-3, QModelIndex()); 68 // create attribut 69 BarAttributes ba( m_bars->barAttributes() ); 70 BarAttributes baCol( m_bars->barAttributes() ); 71 BarAttributes baIndex( m_bars->barAttributes() ); 72 // modify at different level and compare 73 baCol.setBarGapFactor(2 ); 74 baIndex.setBarGapFactor( 3 ); 75 m_bars->setBarAttributes( ba ); 76 m_bars->setBarAttributes( cols-2, baCol ); 77 m_bars->setBarAttributes( idx, baIndex ); 78 QVERIFY( m_bars->barAttributes() != m_bars->barAttributes(cols-2) ); 79 QVERIFY( m_bars->barAttributes() != m_bars->barAttributes(idx) ); 80 QVERIFY( m_bars->barAttributes(cols-2) != m_bars->barAttributes(idx) ); 81 QCOMPARE( m_bars->barAttributes(), ba ); 82 QCOMPARE( m_bars->barAttributes( cols - 2 ), baCol ); 83 QCOMPARE( m_bars->barAttributes( idx ), baIndex ); 84 // try and override the cols and index level - should not work 85 m_bars->setBarAttributes( ba ); 86 QVERIFY( m_bars->barAttributes().barGapFactor() == 0.4 ); 87 QVERIFY( m_bars->barAttributes( cols-2 ).barGapFactor() == 2 ); 88 QVERIFY( m_bars->barAttributes( idx ).barGapFactor() == 3 ); 89 } 90 testBarAttributesValueSettings()91 void testBarAttributesValueSettings() 92 { 93 BarAttributes ba( m_bars->barAttributes() ); 94 95 // check default values 96 QVERIFY( ba.fixedDataValueGap() == 6 ); 97 QVERIFY( ba.useFixedDataValueGap() == false ); 98 QVERIFY( ba.fixedValueBlockGap() == 24 ); 99 QVERIFY( ba.useFixedValueBlockGap() == false ); 100 QVERIFY( ba.fixedBarWidth() == -1 ); 101 QVERIFY( ba.useFixedBarWidth() == false ); 102 QVERIFY( ba.drawSolidExcessArrows() == false ); 103 QVERIFY( ba.groupGapFactor() == 2.0 ); 104 QVERIFY( ba.barGapFactor() == 0.4 ); 105 //change settings 106 ba.setFixedDataValueGap( 7 ); 107 ba.setUseFixedDataValueGap( true ); 108 ba.setFixedValueBlockGap( 25 ); 109 ba.setUseFixedValueBlockGap( true ); 110 ba.setFixedBarWidth( 1 ); 111 ba.setUseFixedBarWidth( true ); 112 ba.setDrawSolidExcessArrows( true ); //not implemented yet 113 ba.setGroupGapFactor( 2 ); 114 ba.setBarGapFactor( 1 ); 115 m_bars->setBarAttributes( ba ); 116 // get new values 117 QVERIFY( m_bars->barAttributes().fixedDataValueGap() == 7 ); 118 QVERIFY( m_bars->barAttributes().useFixedDataValueGap() == true ); 119 QVERIFY( m_bars->barAttributes().fixedValueBlockGap() == 25 ); 120 QVERIFY( m_bars->barAttributes().useFixedValueBlockGap() == true ); 121 QVERIFY( m_bars->barAttributes().fixedBarWidth() == 1 ); 122 QVERIFY( m_bars->barAttributes().useFixedBarWidth() == true ); 123 QVERIFY( m_bars->barAttributes().drawSolidExcessArrows() == true ); 124 QVERIFY( m_bars->barAttributes().groupGapFactor() == 2 ); 125 QVERIFY( m_bars->barAttributes().barGapFactor() == 1 ); 126 } 127 testThreeDBarAttributesLevelSettings()128 void testThreeDBarAttributesLevelSettings() 129 { 130 //check segments 131 const int rows = m_bars->model()->rowCount(); 132 QCOMPARE( m_bars->numberOfAbscissaSegments(), rows ); 133 const int cols = m_bars->model()->columnCount(); 134 QCOMPARE( m_bars->numberOfOrdinateSegments(), cols ); 135 QModelIndex idx = m_model->index(rows-3, cols-3, QModelIndex()); 136 // create attribut 137 ThreeDBarAttributes td( m_bars->threeDBarAttributes() ); 138 ThreeDBarAttributes tdCol( m_bars->threeDBarAttributes() ); 139 ThreeDBarAttributes tdIndex( m_bars->threeDBarAttributes() ); 140 // modify at different level and compare 141 tdCol.setDepth(25 ); 142 tdIndex.setDepth( 30 ); 143 m_bars->setThreeDBarAttributes( td ); 144 m_bars->setThreeDBarAttributes( cols-2, tdCol ); 145 m_bars->setThreeDBarAttributes( idx, tdIndex ); 146 QVERIFY( m_bars->threeDBarAttributes() != 147 m_bars->threeDBarAttributes(cols-2) ); 148 QVERIFY( m_bars->threeDBarAttributes() != 149 m_bars->threeDBarAttributes(idx) ); 150 QVERIFY( m_bars->threeDBarAttributes(cols-2) != 151 m_bars->threeDBarAttributes(idx) ); 152 QCOMPARE( m_bars->threeDBarAttributes(), td ); 153 QCOMPARE( m_bars->threeDBarAttributes( cols - 2 ), tdCol ); 154 QCOMPARE( m_bars->threeDBarAttributes( idx ), tdIndex ); 155 // try and override the cols and index level - should not work 156 m_bars->setThreeDBarAttributes( td ); 157 QVERIFY( m_bars->threeDBarAttributes().depth() == 20 ); 158 QVERIFY( m_bars->threeDBarAttributes( cols-2 ).depth() == 25 ); 159 QVERIFY( m_bars->threeDBarAttributes( idx ).depth() == 30 ); 160 } 161 testThreeDBarAttributesValueSettings()162 void testThreeDBarAttributesValueSettings() 163 { 164 ThreeDBarAttributes td( m_bars->threeDBarAttributes() ); 165 166 //check default values 167 //generics == AbstractThreeD 168 QVERIFY( td.isEnabled() == false ); 169 QVERIFY( td.depth() == 20 ); 170 QVERIFY( td.validDepth() == 0.0 ); 171 //bars specifics 172 QVERIFY( td.useShadowColors() == true ); // Not implemented 173 QVERIFY( td.angle() == 45 ); // Not implemented 174 175 //set new values 176 td.setEnabled( true ); 177 td.setDepth( 40 ); 178 td.setUseShadowColors( false ); 179 td.setAngle( 75 ); 180 m_bars->setThreeDBarAttributes( td ); 181 182 //get new values 183 QVERIFY( m_bars->threeDBarAttributes().isEnabled() == true ); 184 QVERIFY( m_bars->threeDBarAttributes().depth() == 40 ); 185 QVERIFY( m_bars->threeDBarAttributes().validDepth() == 40 ); 186 QVERIFY( m_bars->threeDBarAttributes().useShadowColors() == false ); 187 QVERIFY( m_bars->threeDBarAttributes().angle() == 75 ); 188 } 189 cleanupTestCase()190 void cleanupTestCase() 191 { 192 } 193 194 private: 195 Chart *m_chart; 196 BarDiagram *m_bars; 197 TableModel *m_model; 198 199 }; 200 201 QTEST_MAIN(TestBarDiagrams) 202 203 #include "main.moc" 204