F:/KPlato/koffice/libs/kofficecore/tests/kogenstylestest.cpp

Aller à la documentation de ce fichier.
00001 /* This file is part of the KDE project
00002    Copyright (C) 2004-2006 David Faure <faure@kde.org>
00003 
00004    This library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Library General Public
00006    License version 2 as published by the Free Software Foundation.
00007 
00008    This library is distributed in the hope that it will be useful,
00009    but WITHOUT ANY WARRANTY; without even the implied warranty of
00010    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011    Library General Public License for more details.
00012 
00013    You should have received a copy of the GNU Library General Public License
00014    along with this library; see the file COPYING.LIB.  If not, write to
00015    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00016  * Boston, MA 02110-1301, USA.
00017 */
00018 
00019 #include <KoGenStyles.h>
00020 #include <KoXmlWriter.h>
00021 #include "../../store/tests/xmlwritertest.h"
00022 #include <kdebug.h>
00023 #include <assert.h>
00024 //Added by qt3to4:
00025 #include <Q3ValueList>
00026 
00027 int testLookup()
00028 {
00029     kDebug() << k_funcinfo << endl;
00030     KoGenStyles coll;
00031 
00032     QMap<QString, QString> map1;
00033     map1.insert( "map1key", "map1value" );
00034     QMap<QString, QString> map2;
00035     map2.insert( "map2key1", "map2value1" );
00036     map2.insert( "map2key2", "map2value2" );
00037 
00038     KoGenStyle first( KoGenStyle::STYLE_AUTO, "paragraph" );
00039     first.addAttribute( "style:master-page-name", "Standard" );
00040     first.addProperty( "style:page-number", "0" );
00041     first.addProperty( "style:foobar", "2", KoGenStyle::TextType );
00042     first.addStyleMap( map1 );
00043     first.addStyleMap( map2 );
00044 
00045     QString firstName = coll.lookup( first );
00046     kDebug() << "The first style got assigned the name " << firstName << endl;
00047     assert( firstName == "A1" ); // it's fine if it's something else, but the koxmlwriter tests require a known name
00048     assert( first.type() == KoGenStyle::STYLE_AUTO );
00049 
00050     KoGenStyle second( KoGenStyle::STYLE_AUTO, "paragraph" );
00051     second.addAttribute( "style:master-page-name", "Standard" );
00052     second.addProperty( "style:page-number", "0" );
00053     second.addProperty( "style:foobar", "2", KoGenStyle::TextType );
00054     second.addStyleMap( map1 );
00055     second.addStyleMap( map2 );
00056 
00057     QString secondName = coll.lookup( second );
00058     kDebug() << "The second style got assigned the name " << secondName << endl;
00059 
00060     assert( firstName == secondName ); // check that sharing works
00061     assert( first == second ); // check that operator== works :)
00062 
00063     const KoGenStyle* s = coll.style( firstName ); // check lookup of existing style
00064     assert( s );
00065     assert( *s == first );
00066     s = coll.style( "foobarblah" ); // check lookup of non-existing style
00067     assert( !s );
00068 
00069     KoGenStyle third( KoGenStyle::STYLE_AUTO, "paragraph", secondName ); // inherited style
00070     third.addProperty( "style:margin-left", "1.249cm" );
00071     third.addProperty( "style:page-number", "0" ); // same as parent
00072     third.addProperty( "style:foobar", "3", KoGenStyle::TextType ); // different from parent
00073     assert( third.parentName() == secondName );
00074 
00075     QString thirdName = coll.lookup( third, "P" );
00076     kDebug() << "The third style got assigned the name " << thirdName << endl;
00077     assert( thirdName == "P1" );
00078 
00079     KoGenStyle user( KoGenStyle::STYLE_USER, "paragraph" ); // differs from third since it doesn't inherit second, and has a different type
00080     user.addProperty( "style:margin-left", "1.249cm" );
00081 
00082     QString userStyleName = coll.lookup( user, "User", KoGenStyles::DontForceNumbering );
00083     kDebug() << "The user style got assigned the name " << userStyleName << endl;
00084     assert( userStyleName == "User" );
00085 
00086     KoGenStyle sameAsParent( KoGenStyle::STYLE_AUTO, "paragraph", secondName ); // inherited style
00087     sameAsParent.addAttribute( "style:master-page-name", "Standard" );
00088     sameAsParent.addProperty( "style:page-number", "0" );
00089     sameAsParent.addProperty( "style:foobar", "2", KoGenStyle::TextType );
00090     sameAsParent.addStyleMap( map1 );
00091     sameAsParent.addStyleMap( map2 );
00092     QString sapName = coll.lookup( sameAsParent, "foobar" );
00093     kDebug() << "The 'same as parent' style got assigned the name " << sapName << endl;
00094 
00095     assert( sapName == secondName );
00096     assert( coll.styles().count() == 3 );
00097 
00098     // OK, now add a style marked as for styles.xml; it looks like the above style, but
00099     // since it's marked for styles.xml it shouldn't be shared with it.
00100     KoGenStyle headerStyle( KoGenStyle::STYLE_AUTO, "paragraph" );
00101     headerStyle.addAttribute( "style:master-page-name", "Standard" );
00102     headerStyle.addProperty( "style:page-number", "0" );
00103     headerStyle.addProperty( "style:foobar", "2", KoGenStyle::TextType );
00104     headerStyle.addStyleMap( map1 );
00105     headerStyle.addStyleMap( map2 );
00106     headerStyle.setAutoStyleInStylesDotXml( true );
00107     QString headerStyleName = coll.lookup( headerStyle, "foobar" );
00108 
00109     assert( coll.styles().count() == 4 );
00110     assert( coll.styles( KoGenStyle::STYLE_AUTO ).count() == 2 );
00111     assert( coll.styles( KoGenStyle::STYLE_USER ).count() == 1 );
00112 
00113     Q3ValueList<KoGenStyles::NamedStyle> stylesXmlStyles = coll.styles( KoGenStyle::STYLE_AUTO, true );
00114     assert( stylesXmlStyles.count() == 1 );
00115     KoGenStyles::NamedStyle firstStyle = stylesXmlStyles.first();
00116     assert( firstStyle.name == headerStyleName );
00117 
00118     TEST_BEGIN( 0, 0 );
00119     first.writeStyle( &writer, coll, "style:style", firstName, "style:paragraph-properties" );
00120     TEST_END( "XML for first/second style", "<r>\n <style:style style:name=\"A1\" style:family=\"paragraph\" style:master-page-name=\"Standard\">\n  <style:paragraph-properties style:page-number=\"0\"/>\n  <style:text-properties style:foobar=\"2\"/>\n  <style:map map1key=\"map1value\"/>\n  <style:map map2key1=\"map2value1\" map2key2=\"map2value2\"/>\n </style:style>\n</r>\n" );
00121 
00122     TEST_BEGIN( 0, 0 );
00123     third.writeStyle( &writer, coll, "style:style", thirdName, "style:paragraph-properties" );
00124     TEST_END( "XML for third style", "<r>\n <style:style style:name=\"P1\" style:parent-style-name=\"A1\" style:family=\"paragraph\">\n  <style:paragraph-properties style:margin-left=\"1.249cm\"/>\n  <style:text-properties style:foobar=\"3\"/>\n </style:style>\n</r>\n" );
00125 
00126     coll.markStyleForStylesXml( firstName );
00127     {
00128         Q3ValueList<KoGenStyles::NamedStyle> stylesXmlStyles = coll.styles( KoGenStyle::STYLE_AUTO, true );
00129         assert( stylesXmlStyles.count() == 2 );
00130         Q3ValueList<KoGenStyles::NamedStyle> contentXmlStyles = coll.styles( KoGenStyle::STYLE_AUTO, false );
00131         assert( contentXmlStyles.count() == 1 );
00132     }
00133 
00134     return 0;
00135 }
00136 
00137 int testDefaultStyle()
00138 {
00139     kDebug() << k_funcinfo << endl;
00140     /* Create a default style,
00141      * and then an auto style with exactly the same attributes
00142      * -> the lookup gives the default style.
00143      *
00144      * Also checks how the default style gets written out to XML.
00145      */
00146     KoGenStyles coll;
00147 
00148     KoGenStyle defaultStyle( KoGenStyle::STYLE_AUTO, "paragraph" );
00149     defaultStyle.addAttribute( "style:master-page-name", "Standard" );
00150     defaultStyle.addProperty( "myfont", "isBold" );
00151     defaultStyle.setDefaultStyle( true );
00152     QString defaultStyleName = coll.lookup( defaultStyle );
00153     assert( !defaultStyleName.isEmpty() ); // whatever, but not empty
00154     assert( defaultStyle.type() == KoGenStyle::STYLE_AUTO );
00155     assert( defaultStyle.isDefaultStyle() );
00156 
00157     KoGenStyle anotherStyle( KoGenStyle::STYLE_AUTO, "paragraph" );
00158     anotherStyle.addAttribute( "style:master-page-name", "Standard" );
00159     anotherStyle.addProperty( "myfont", "isBold" );
00160     QString anotherStyleName = coll.lookup( anotherStyle );
00161     assert( anotherStyleName == defaultStyleName );
00162 
00163     assert( coll.styles().count() == 1 );
00164 
00165     TEST_BEGIN( 0, 0 );
00166     defaultStyle.writeStyle( &writer, coll, "style:default-style", defaultStyleName, "style:paragraph-properties" );
00167     TEST_END( "XML for default style", "<r>\n <style:default-style style:family=\"paragraph\" style:master-page-name=\"Standard\">\n  <style:paragraph-properties myfont=\"isBold\"/>\n </style:default-style>\n</r>\n" );
00168 
00169     // The kspread case: not writing out all properties, only if they differ
00170     // from the default style.
00171     // KoGenStyles doesn't fetch info from the parent style when testing
00172     // for equality, so KSpread uses isEmpty() to check for equality-to-parent.
00173     KoGenStyle dataStyle( KoGenStyle::STYLE_AUTO, "paragraph", defaultStyleName );
00174     assert( dataStyle.isEmpty() );
00175     // and then it doesn't look up the auto style, but rather uses the parent style directly.
00176 
00177     return 0;
00178 }
00179 
00180 int testUserStyles()
00181 {
00182     kDebug() << k_funcinfo << endl;
00183     /* Two user styles with exactly the same attributes+properties will not get merged, since
00184      * they don't have exactly the same attributes after all: the display-name obviously differs :)
00185      */
00186     KoGenStyles coll;
00187 
00188     KoGenStyle user1( KoGenStyle::STYLE_USER, "paragraph" );
00189     user1.addAttribute( "style:display-name", "User 1" );
00190     user1.addProperty( "myfont", "isBold" );
00191 
00192     QString user1StyleName = coll.lookup( user1, "User1", KoGenStyles::DontForceNumbering );
00193     kDebug() << "The user style got assigned the name " << user1StyleName << endl;
00194     assert( user1StyleName == "User1" );
00195 
00196     KoGenStyle user2( KoGenStyle::STYLE_USER, "paragraph" );
00197     user2.addAttribute( "style:display-name", "User 2" );
00198     user2.addProperty( "myfont", "isBold" );
00199 
00200     QString user2StyleName = coll.lookup( user2, "User2", KoGenStyles::DontForceNumbering );
00201     kDebug() << "The user style got assigned the name " << user2StyleName << endl;
00202     assert( user2StyleName == "User2" );
00203 
00204     // And now, what if the data uses that style?
00205     // This is like sameAsParent in the other test, but this time the
00206     // parent is a STYLE_USER...
00207     KoGenStyle dataStyle( KoGenStyle::STYLE_AUTO, "paragraph", user2StyleName );
00208     dataStyle.addProperty( "myfont", "isBold" );
00209 
00210     QString dataStyleName = coll.lookup( dataStyle, "DataStyle" );
00211     kDebug() << "The auto style got assigned the name " << dataStyleName << endl;
00212     assert( dataStyleName == "User2" ); // it found the parent as equal
00213 
00214     // Let's do the opposite test, just to make sure
00215     KoGenStyle dataStyle2( KoGenStyle::STYLE_AUTO, "paragraph", user2StyleName );
00216     dataStyle2.addProperty( "myfont", "isNotBold" );
00217 
00218     QString dataStyle2Name = coll.lookup( dataStyle2, "DataStyle" );
00219     kDebug() << "The different auto style got assigned the name " << dataStyle2Name << endl;
00220     assert( dataStyle2Name == "DataStyle1" );
00221 
00222     assert( coll.styles().count() == 3 );
00223 
00224     TEST_BEGIN( 0, 0 );
00225     user1.writeStyle( &writer, coll, "style:style", user1StyleName, "style:paragraph-properties" );
00226     TEST_END( "XML for user style 1", "<r>\n <style:style style:name=\"User1\" style:display-name=\"User 1\" style:family=\"paragraph\">\n  <style:paragraph-properties myfont=\"isBold\"/>\n </style:style>\n</r>\n" );
00227 
00228     TEST_BEGIN( 0, 0 );
00229     user2.writeStyle( &writer, coll, "style:style", user2StyleName, "style:paragraph-properties" );
00230     TEST_END( "XML for user style 2", "<r>\n <style:style style:name=\"User2\" style:display-name=\"User 2\" style:family=\"paragraph\">\n  <style:paragraph-properties myfont=\"isBold\"/>\n </style:style>\n</r>\n" );
00231 
00232     return 0;
00233 }
00234 
00235 int testStylesDotXml()
00236 {
00237     kDebug() << k_funcinfo << endl;
00238     KoGenStyles coll;
00239 
00240     // Check that an autostyle-in-style.xml and an autostyle-in-content.xml
00241     // don't get the same name. It confuses KoGenStyle's named-based maps.
00242     KoGenStyle headerStyle( KoGenStyle::STYLE_AUTO, "paragraph" );
00243     headerStyle.addAttribute( "style:master-page-name", "Standard" );
00244     headerStyle.addProperty( "style:page-number", "0" );
00245     headerStyle.setAutoStyleInStylesDotXml( true );
00246     QString headerStyleName = coll.lookup( headerStyle, "P" );
00247     assert( headerStyleName == "P1" );
00248 
00249     //coll.dump();
00250 
00251     KoGenStyle first( KoGenStyle::STYLE_AUTO, "paragraph" );
00252     first.addAttribute( "style:master-page-name", "Standard" );
00253     QString firstName = coll.lookup( first, "P" );
00254     kDebug() << "The auto style got assigned the name " << firstName << endl;
00255     assert( firstName == "P2" ); // anything but not P1.
00256     return 0;
00257 }
00258 
00259 int main( int, char** ) {
00260 
00261     if ( testLookup() )
00262         return 1;
00263     if ( testDefaultStyle() )
00264         return 1;
00265     if ( testUserStyles() )
00266         return 1;
00267     if ( testStylesDotXml() )
00268         return 1;
00269 
00270     return 0;
00271 }

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