00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
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
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" );
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 );
00061 assert( first == second );
00062
00063 const KoGenStyle* s = coll.style( firstName );
00064 assert( s );
00065 assert( *s == first );
00066 s = coll.style( "foobarblah" );
00067 assert( !s );
00068
00069 KoGenStyle third( KoGenStyle::STYLE_AUTO, "paragraph", secondName );
00070 third.addProperty( "style:margin-left", "1.249cm" );
00071 third.addProperty( "style:page-number", "0" );
00072 third.addProperty( "style:foobar", "3", KoGenStyle::TextType );
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" );
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 );
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
00099
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
00141
00142
00143
00144
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() );
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
00170
00171
00172
00173 KoGenStyle dataStyle( KoGenStyle::STYLE_AUTO, "paragraph", defaultStyleName );
00174 assert( dataStyle.isEmpty() );
00175
00176
00177 return 0;
00178 }
00179
00180 int testUserStyles()
00181 {
00182 kDebug() << k_funcinfo << endl;
00183
00184
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
00205
00206
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" );
00213
00214
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
00241
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
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" );
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 }