F:/KPlato/koffice/libs/kotext/tests/kotextformattertest.cpp

Aller à la documentation de ce fichier.
00001 //KoTextFormatter test (also for profiling purposes), GPL v2, David Faure <faure@kde.org>
00002 
00003 #include <QApplication>
00004 #include <kdebug.h>
00005 #include <kglobal.h>
00006 #include <klocale.h>
00007 
00008 #include "KoTextFormatter.h"
00009 #include "KoTextFormat.h"
00010 #include "KoTextDocument.h"
00011 #include "KoTextParag.h"
00012 #include "KoTextZoomHandler.h"
00013 #include "KoParagCounter.h"
00014 
00015 #include <assert.h>
00016 
00017 class KoTextFormatterTest
00018 {
00019 public:
00020     KoTextFormatterTest();
00021     ~KoTextFormatterTest() {
00022         delete zh;
00023         delete doc;
00024     }
00025 
00026     void speedTest();
00027     void counterAndBigChar();
00028     void oneLineTest();
00029     void noHeightTest();
00030     void noWidthEverTest();
00031     void tooWideChar();
00032 
00033 private:
00034     KoTextZoomHandler* zh;
00035     KoTextDocument* doc;
00036 };
00037 
00038 // To test various cases with variable document widths: a custom KoTextFlow
00039 class TextFlow : public KoTextFlow
00040 {
00041 public:
00042     TextFlow( int availableWidth, int availableHeight )
00043         : m_availableWidth( availableWidth ), m_availableHeight( availableHeight ),
00044           m_leftMargin( 0 )
00045     {
00046         setWidth( m_availableWidth );
00047     }
00048     virtual int availableHeight() const {
00049         return m_availableHeight;
00050     }
00051     void setLeftMargin( int lm ) {
00052         m_leftMargin = lm;
00053     }
00054     virtual void adjustMargins( int yp, int h, int reqMinWidth, int& leftMargin, int& rightMargin, int& pageWidth, KoTextParag* ) {
00055         Q_UNUSED(yp);
00056         Q_UNUSED(h);
00057         Q_UNUSED(reqMinWidth);
00058         Q_UNUSED(rightMargin);
00059         if ( m_leftMargin ) {
00060             leftMargin = m_leftMargin;
00061             pageWidth = m_availableWidth - m_leftMargin;
00062             kDebug() << "adjustMargins: returning leftMargin=" << leftMargin << " pageWidth=" << pageWidth << endl;
00063         } else
00064             pageWidth = m_availableWidth;
00065     }
00066 private:
00067     int m_availableWidth;
00068     int m_availableHeight;
00069     int m_leftMargin;
00070 };
00071 
00072 KoTextFormatterTest::KoTextFormatterTest()
00073 {
00074     zh = new KoTextZoomHandler;
00075     QFont defaultFont( "helvetica", 12 );
00076     KoTextFormatCollection* fc = new KoTextFormatCollection( defaultFont, Qt::black, "en_US", false /*no hyphenation*/ );
00077     KoTextFormatter* formatter = new KoTextFormatter;
00078     // fc and formatter are owned by the doc
00079     doc = new KoTextDocument( zh, fc, formatter );
00080 }
00081 
00082 void KoTextFormatterTest::speedTest()
00083 {
00084     kDebug() << k_funcinfo << endl;
00085     doc->clear(true);
00086     KoTextParag* parag = doc->firstParag();
00087     parag->append( "They burst into flames when it is time for them to die, and then they are reborn from the ashes" );
00088 
00089     // Format it 50 times
00090     for ( uint i = 0 ; i < 50 ; ++i )
00091     {
00092       parag->invalidate(0);
00093       parag->format();
00094     }
00095     doc->clear(false);
00096 }
00097 
00098 void KoTextFormatterTest::noHeightTest()
00099 {
00100     kDebug() << k_funcinfo << endl;
00101     // We test the case of going past maxY - by setting the available height to 0
00102     // Expected result: the formatter 'aborts', i.e. no line-breaking, but still
00103     // goes over each character to set them all correctly; and usually KWord
00104     // would create a new page and reformat the paragraph
00105     doc->setFlow( new TextFlow( 250, 0 ) ); // 250 is just enough for one char
00106     doc->clear(true);
00107     KoTextParag* parag = doc->firstParag();
00108     parag->append( "abcdefghi" );
00109     parag->format();
00110     assert( parag->lines() == 2 ); // one break, then we keep going
00111     doc->clear(false);
00112     doc->setFlow( new KoTextFlow ); // default
00113 }
00114 
00115 void KoTextFormatterTest::noWidthEverTest()
00116 {
00117     kDebug() << k_funcinfo << endl;
00118     // We test the case of formatting where there is no width (e.g. narrow
00119     // passage, or after last page).
00120     // Expected result: the formatter goes down until it finds more width
00121     // (TODO a test case where this happens)
00122     // If it doesn't find any (and hits maxY), then it 'aborts', i.e. no line-breaking,
00123     // but still goes over each character to set them all correctly; and usually KWord
00124     // would create a new page and reformat the paragraph
00125     TextFlow* flow = new TextFlow( 1000, 2000 );
00126     flow->setLeftMargin( 1000 );
00127     doc->setFlow( flow );
00128 
00129     doc->clear(true);
00130     KoTextParag* parag = doc->firstParag();
00131     parag->append( "abcdefghi" );
00132     parag->format();
00133     // The resulting paragraph is NOT marked as formatted. See kotextformatter.cc -r1.79
00134     assert( !parag->isValid() );
00135     doc->clear(false);
00136     doc->setFlow( new KoTextFlow ); // default
00137 }
00138 
00139 void KoTextFormatterTest::tooWideChar()
00140 {
00141     kDebug() << k_funcinfo << endl;
00142     // We test the case of formatting where there is not enough width
00143     // for the character (e.g. a very large inline table, larger than the document).
00144     // Expected result: the formatter goes down until it finds more width,
00145     // gives up, and come back up.
00146     doc->setFlow( new TextFlow( 10, 2000 ) );
00147     doc->clear(true);
00148     KoTextParag* parag = doc->firstParag();
00149     parag->append( "a" );
00150     parag->format();
00151     assert( parag->isValid() );
00152     assert( parag->lines() == 1 );
00153 
00154     doc->clear(false);
00155     doc->setFlow( new KoTextFlow ); // default
00156 }
00157 
00158 void KoTextFormatterTest::oneLineTest()
00159 {
00160     kDebug() << k_funcinfo << endl;
00161     // Normal case, only one line
00162     // Expected: the parag is as wide as the doc
00163     doc->setFlow( new TextFlow( 2000, 200 ) );
00164     doc->clear(true);
00165     KoTextParag* parag = doc->firstParag();
00166     parag->append( "abcdefghi" );
00167     parag->format();
00168     assert( parag->lines() == 1 );
00169     assert( parag->isValid() );
00170     assert( parag->rect().width() == 2000 );
00171     assert( parag->widthUsed() < 2000 );
00172     doc->clear(false);
00173     doc->setFlow( new KoTextFlow ); // default
00174 }
00175 
00176 void KoTextFormatterTest::counterAndBigChar()
00177 {
00178     kDebug() << k_funcinfo << endl;
00179     // Only one line, with a counter and a big char.
00180     // Bug #82609: the new height led to "formatting again" which restarted without taking the counter into account
00181     // Expected: the char starts after the counter
00182     doc->setFlow( new TextFlow( 2000, 200 ) );
00183     doc->clear(true);
00184     KoTextParag* parag = doc->firstParag();
00185     parag->append( "aB" );
00186     KoTextFormat f( *parag->at( 0 )->format() );
00187     f.setPointSize( 48 );
00188     parag->setFormat( 1, 1, doc->formatCollection()->format( &f ), true );
00189     KoParagCounter counter;
00190     counter.setNumbering( KoParagCounter::NUM_LIST );
00191     counter.setStyle( KoParagCounter::STYLE_NUM );
00192     parag->setCounter( &counter );
00193     parag->format();
00194     assert( parag->lines() == 1 );
00195     assert( parag->isValid() );
00196     assert( parag->rect().width() == 2000 );
00197     assert( parag->widthUsed() < 2000 );
00198     assert( parag->at(0)->x > 0 );
00199     doc->clear(false);
00200     doc->setFlow( new KoTextFlow ); // default
00201 }
00202 
00203 int main (int argc, char ** argv)
00204 {
00205     QApplication app(argc, argv);
00206 
00207     // Don't let locale settings lead to different hyphenation output
00208     KGlobal::locale()->setLanguage( QString::fromLatin1( "en_US" ) );
00209     KGlobal::locale()->setCountry( QString::fromLatin1( "C" ) );
00210 
00211     KoTextFormatterTest test;
00212     //test.speedTest();
00213     test.oneLineTest();
00214     test.counterAndBigChar();
00215     test.noHeightTest();
00216     test.noWidthEverTest();
00217     test.tooWideChar();
00218 
00219     return 0;
00220 }

Généré le Wed Nov 22 23:41:11 2006 pour KPlato par  doxygen 1.5.1-p1