|
| 1 | +/*************************************************************************** |
| 2 | + testqgslayoutpage.cpp |
| 3 | + --------------------- |
| 4 | + begin : November 2014 |
| 5 | + copyright : (C) 2014 by Nyall Dawson |
| 6 | + email : nyall dot dawson at gmail dot com |
| 7 | + ***************************************************************************/ |
| 8 | + |
| 9 | +/*************************************************************************** |
| 10 | + * * |
| 11 | + * This program is free software; you can redistribute it and/or modify * |
| 12 | + * it under the terms of the GNU General Public License as published by * |
| 13 | + * the Free Software Foundation; either version 2 of the License, or * |
| 14 | + * (at your option) any later version. * |
| 15 | + * * |
| 16 | + ***************************************************************************/ |
| 17 | + |
| 18 | +#include "qgslayout.h" |
| 19 | +#include "qgslayoutitempage.h" |
| 20 | +#include "qgslayoutitemregistry.h" |
| 21 | +#include "qgis.h" |
| 22 | +#include "qgsproject.h" |
| 23 | +#include <QObject> |
| 24 | +#include "qgstest.h" |
| 25 | + |
| 26 | +class TestQgsLayoutPage : public QObject |
| 27 | +{ |
| 28 | + Q_OBJECT |
| 29 | + |
| 30 | + private slots: |
| 31 | + void initTestCase();// will be called before the first testfunction is executed. |
| 32 | + void cleanupTestCase();// will be called after the last testfunction was executed. |
| 33 | + void init();// will be called before each testfunction is executed. |
| 34 | + void cleanup();// will be called after every testfunction. |
| 35 | + void itemType(); |
| 36 | + void pageSize(); |
| 37 | + void decodePageOrientation(); |
| 38 | + |
| 39 | + private: |
| 40 | + QString mReport; |
| 41 | + |
| 42 | +}; |
| 43 | + |
| 44 | +void TestQgsLayoutPage::initTestCase() |
| 45 | +{ |
| 46 | + mReport = "<h1>Layout Page Tests</h1>\n"; |
| 47 | +} |
| 48 | + |
| 49 | +void TestQgsLayoutPage::cleanupTestCase() |
| 50 | +{ |
| 51 | + QString myReportFile = QDir::tempPath() + QDir::separator() + "qgistest.html"; |
| 52 | + QFile myFile( myReportFile ); |
| 53 | + if ( myFile.open( QIODevice::WriteOnly | QIODevice::Append ) ) |
| 54 | + { |
| 55 | + QTextStream myQTextStream( &myFile ); |
| 56 | + myQTextStream << mReport; |
| 57 | + myFile.close(); |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +void TestQgsLayoutPage::init() |
| 62 | +{ |
| 63 | + |
| 64 | +} |
| 65 | + |
| 66 | +void TestQgsLayoutPage::cleanup() |
| 67 | +{ |
| 68 | + |
| 69 | +} |
| 70 | + |
| 71 | +void TestQgsLayoutPage::itemType() |
| 72 | +{ |
| 73 | + QgsProject p; |
| 74 | + QgsLayout l( &p ); |
| 75 | + QgsLayoutItemPage *page = new QgsLayoutItemPage( &l ); |
| 76 | + QCOMPARE( page->type(), static_cast< int >( QgsLayoutItemRegistry::LayoutPage ) ); |
| 77 | +} |
| 78 | + |
| 79 | +void TestQgsLayoutPage::pageSize() |
| 80 | +{ |
| 81 | + QgsProject p; |
| 82 | + QgsLayout l( &p ); |
| 83 | + QgsLayoutItemPage *page = new QgsLayoutItemPage( &l ); |
| 84 | + page->setPageSize( QgsLayoutSize( 270, 297, QgsUnitTypes::LayoutMeters ) ); |
| 85 | + QCOMPARE( page->pageSize().width(), 270.0 ); |
| 86 | + QCOMPARE( page->pageSize().height(), 297.0 ); |
| 87 | + QCOMPARE( page->pageSize().units(), QgsUnitTypes::LayoutMeters ); |
| 88 | + QCOMPARE( page->orientation(), QgsLayoutItemPage::Portrait ); |
| 89 | + page->setPageSize( QgsLayoutSize( 297, 270, QgsUnitTypes::LayoutMeters ) ); |
| 90 | + QCOMPARE( page->orientation(), QgsLayoutItemPage::Landscape ); |
| 91 | + |
| 92 | + // from registry |
| 93 | + QVERIFY( !page->setPageSize( "hoooyeah" ) ); |
| 94 | + // should be unchanged |
| 95 | + QCOMPARE( page->pageSize().width(), 297.0 ); |
| 96 | + QCOMPARE( page->pageSize().height(), 270.0 ); |
| 97 | + QCOMPARE( page->pageSize().units(), QgsUnitTypes::LayoutMeters ); |
| 98 | + |
| 99 | + // good size |
| 100 | + QVERIFY( page->setPageSize( "A5" ) ); |
| 101 | + QCOMPARE( page->pageSize().width(), 148.0 ); |
| 102 | + QCOMPARE( page->pageSize().height(), 210.0 ); |
| 103 | + QCOMPARE( page->pageSize().units(), QgsUnitTypes::LayoutMillimeters ); |
| 104 | + QCOMPARE( page->orientation(), QgsLayoutItemPage::Portrait ); |
| 105 | + |
| 106 | + QVERIFY( page->setPageSize( "A5", QgsLayoutItemPage::Landscape ) ); |
| 107 | + QCOMPARE( page->pageSize().width(), 210.0 ); |
| 108 | + QCOMPARE( page->pageSize().height(), 148.0 ); |
| 109 | + QCOMPARE( page->pageSize().units(), QgsUnitTypes::LayoutMillimeters ); |
| 110 | + QCOMPARE( page->orientation(), QgsLayoutItemPage::Landscape ); |
| 111 | + |
| 112 | +} |
| 113 | + |
| 114 | +void TestQgsLayoutPage::decodePageOrientation() |
| 115 | +{ |
| 116 | + //test good string |
| 117 | + bool ok = false; |
| 118 | + QCOMPARE( QgsLayoutItemPage::decodePageOrientation( QString( " porTrait " ), &ok ), QgsLayoutItemPage::Portrait ); |
| 119 | + QVERIFY( ok ); |
| 120 | + QCOMPARE( QgsLayoutItemPage::decodePageOrientation( QString( "landscape" ), &ok ), QgsLayoutItemPage::Landscape ); |
| 121 | + QVERIFY( ok ); |
| 122 | + |
| 123 | + //test bad string |
| 124 | + QgsLayoutItemPage::decodePageOrientation( QString(), &ok ); |
| 125 | + QVERIFY( !ok ); |
| 126 | +} |
| 127 | + |
| 128 | +QGSTEST_MAIN( TestQgsLayoutPage ) |
| 129 | +#include "testqgslayoutpage.moc" |
0 commit comments