tests/auto/qtextformat/tst_qtextformat.cpp
changeset 0 1918ee327afb
child 4 3b1da2848fc7
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the test suite of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 
       
    43 #include <QtTest/QtTest>
       
    44 
       
    45 #include <qcoreapplication.h>
       
    46 #include <qdebug.h>
       
    47 #include <qsettings.h>
       
    48 #include <qtextformat.h>
       
    49 #include <qtextdocument.h>
       
    50 #include <qtextcursor.h>
       
    51 #include <qtextobject.h>
       
    52 #include <qtextlayout.h>
       
    53 #include <qabstracttextdocumentlayout.h>
       
    54 
       
    55 //TESTED_CLASS=
       
    56 //TESTED_FILES=
       
    57 
       
    58 class tst_QTextFormat : public QObject
       
    59 {
       
    60 Q_OBJECT
       
    61 
       
    62 private slots:
       
    63     void getSetCheck();
       
    64     void defaultAlignment();
       
    65     void testQTextCharFormat() const;
       
    66     void testUnderlinePropertyPrecedence();
       
    67     void toFormat();
       
    68     void resolveFont();
       
    69     void getSetTabs();
       
    70     void testTabsUsed();
       
    71     void testFontStyleSetters();
       
    72 };
       
    73 
       
    74 /*! \internal
       
    75   This (used to) trigger a crash in:
       
    76  
       
    77     QDataStream &operator>>(QDataStream &stream, QTextFormat &fmt)
       
    78 
       
    79   which is most easily produced through QSettings.
       
    80  */
       
    81 void tst_QTextFormat::testQTextCharFormat() const
       
    82 {
       
    83     QSettings settings("test", "test");
       
    84     QTextCharFormat test;
       
    85 
       
    86     settings.value("test", test);
       
    87 }
       
    88 
       
    89 // Testing get/set functions
       
    90 void tst_QTextFormat::getSetCheck()
       
    91 {
       
    92     QTextFormat obj1;
       
    93     // int QTextFormat::objectIndex()
       
    94     // void QTextFormat::setObjectIndex(int)
       
    95     obj1.setObjectIndex(0);
       
    96     QCOMPARE(0, obj1.objectIndex());
       
    97     obj1.setObjectIndex(INT_MIN);
       
    98     QCOMPARE(INT_MIN, obj1.objectIndex());
       
    99     obj1.setObjectIndex(INT_MAX);
       
   100     QCOMPARE(INT_MAX, obj1.objectIndex());
       
   101 }
       
   102 
       
   103 void tst_QTextFormat::defaultAlignment()
       
   104 {
       
   105     QTextBlockFormat fmt;
       
   106     QVERIFY(!fmt.hasProperty(QTextFormat::BlockAlignment));
       
   107     QCOMPARE(fmt.intProperty(QTextFormat::BlockAlignment), 0);
       
   108     QVERIFY(fmt.alignment() == Qt::AlignLeft);
       
   109 }
       
   110 
       
   111 void tst_QTextFormat::testUnderlinePropertyPrecedence()
       
   112 {
       
   113     QTextCharFormat format;
       
   114     // use normal accessors and check internal state
       
   115     format.setUnderlineStyle(QTextCharFormat::NoUnderline);
       
   116     QCOMPARE(format.property(QTextFormat::FontUnderline).isNull(), false);
       
   117     QCOMPARE(format.property(QTextFormat::TextUnderlineStyle).isNull(), false);
       
   118     QCOMPARE(format.property(QTextFormat::FontUnderline).toBool(), false);
       
   119     QCOMPARE(format.property(QTextFormat::TextUnderlineStyle).toInt(), 0);
       
   120 
       
   121     format = QTextCharFormat();
       
   122     format.setUnderlineStyle(QTextCharFormat::SingleUnderline);
       
   123     QCOMPARE(format.property(QTextFormat::FontUnderline).isNull(), false);
       
   124     QCOMPARE(format.property(QTextFormat::TextUnderlineStyle).isNull(), false);
       
   125     QCOMPARE(format.property(QTextFormat::FontUnderline).toBool(), true);
       
   126     QCOMPARE(format.property(QTextFormat::TextUnderlineStyle).toInt(), 1);
       
   127 
       
   128     format = QTextCharFormat();
       
   129     format.setUnderlineStyle(QTextCharFormat::DotLine);
       
   130     QCOMPARE(format.property(QTextFormat::FontUnderline).isNull(), false);
       
   131     QCOMPARE(format.property(QTextFormat::TextUnderlineStyle).isNull(), false);
       
   132     QCOMPARE(format.property(QTextFormat::FontUnderline).toBool(), false);
       
   133     QVERIFY(format.property(QTextFormat::TextUnderlineStyle).toInt() > 0);
       
   134 
       
   135     // override accessors and use setProperty to create a false state.
       
   136     // then check font()
       
   137     format = QTextCharFormat();
       
   138     format.setProperty(QTextCharFormat::FontUnderline, true);
       
   139     QCOMPARE(format.property(QTextFormat::FontUnderline).isNull(), false);
       
   140     QCOMPARE(format.property(QTextFormat::TextUnderlineStyle).isNull(), true);
       
   141     QCOMPARE(format.fontUnderline(), true);
       
   142     QCOMPARE(format.font().underline(), true);
       
   143 
       
   144     format = QTextCharFormat();
       
   145     // create conflict. Should use the new property
       
   146     format.setProperty(QTextCharFormat::TextUnderlineStyle, QTextCharFormat::SingleUnderline);
       
   147     format.setProperty(QTextCharFormat::FontUnderline, false);
       
   148     QCOMPARE(format.fontUnderline(), true);
       
   149     QCOMPARE(format.font().underline(), true);
       
   150 
       
   151     format = QTextCharFormat();
       
   152     // create conflict. Should use the new property
       
   153     format.setProperty(QTextCharFormat::TextUnderlineStyle, QTextCharFormat::NoUnderline);
       
   154     format.setProperty(QTextCharFormat::FontUnderline, true);
       
   155     QCOMPARE(format.fontUnderline(), false);
       
   156     QCOMPARE(format.font().underline(), false);
       
   157 
       
   158     // do it again, but reverse the ordering (we use a QVector internally, so test a LOT ;)
       
   159     // create conflict. Should use the new property
       
   160     format.setProperty(QTextCharFormat::FontUnderline, false);
       
   161     format.setProperty(QTextCharFormat::TextUnderlineStyle, QTextCharFormat::SingleUnderline);
       
   162     QCOMPARE(format.fontUnderline(), true);
       
   163     QCOMPARE(format.font().underline(), true);
       
   164 
       
   165     format = QTextCharFormat();
       
   166     // create conflict. Should use the new property
       
   167     format.setProperty(QTextCharFormat::FontUnderline, true);
       
   168     format.setProperty(QTextCharFormat::TextUnderlineStyle, QTextCharFormat::NoUnderline);
       
   169     QCOMPARE(format.fontUnderline(), false);
       
   170     QCOMPARE(format.font().underline(), false);
       
   171 }
       
   172 
       
   173 void tst_QTextFormat::toFormat()
       
   174 {
       
   175     {
       
   176         QTextFormat fmt = QTextFrameFormat();
       
   177         QCOMPARE(fmt.toFrameFormat().type(), int(QTextFormat::FrameFormat));
       
   178     }
       
   179 
       
   180     {
       
   181         QTextFormat fmt = QTextTableFormat();
       
   182         QCOMPARE(fmt.toTableFormat().type(), int(QTextFormat::FrameFormat));
       
   183         QCOMPARE(fmt.toTableFormat().objectType(), int(QTextFormat::TableObject));
       
   184     }
       
   185 
       
   186     {
       
   187         QTextFormat fmt = QTextBlockFormat();
       
   188         QCOMPARE(fmt.toBlockFormat().type(), int(QTextFormat::BlockFormat));
       
   189     }
       
   190 
       
   191     {
       
   192         QTextFormat fmt = QTextCharFormat();
       
   193         QCOMPARE(fmt.toCharFormat().type(), int(QTextFormat::CharFormat));
       
   194     }
       
   195 
       
   196     {
       
   197         QTextFormat fmt = QTextListFormat();
       
   198         QCOMPARE(fmt.toListFormat().type(), int(QTextFormat::ListFormat));
       
   199     }
       
   200 }
       
   201 
       
   202 void tst_QTextFormat::resolveFont()
       
   203 {
       
   204     QTextDocument doc;
       
   205 
       
   206     QTextCharFormat fmt;
       
   207     fmt.setProperty(QTextFormat::ForegroundBrush, Qt::blue);
       
   208     fmt.setProperty(QTextFormat::FontItalic, true);
       
   209     QTextCursor(&doc).insertText("Test", fmt);
       
   210 
       
   211     QVector<QTextFormat> formats = doc.allFormats();
       
   212     QCOMPARE(formats.count(), 3);
       
   213 
       
   214     QVERIFY(formats.at(2).type() == QTextFormat::CharFormat);
       
   215     fmt = formats.at(2).toCharFormat();
       
   216 
       
   217     QVERIFY(!fmt.font().underline());
       
   218     QVERIFY(fmt.hasProperty(QTextFormat::ForegroundBrush));
       
   219 
       
   220     QFont f;
       
   221     f.setUnderline(true);
       
   222     doc.setDefaultFont(f);
       
   223     formats = doc.allFormats();
       
   224     fmt = formats.at(2).toCharFormat();
       
   225 
       
   226     QVERIFY(fmt.font().underline());
       
   227     QVERIFY(!fmt.hasProperty(QTextFormat::FontUnderline));
       
   228 
       
   229     // verify that deleting a non-existant property does not affect the font resolving
       
   230 
       
   231     QVERIFY(!fmt.hasProperty(QTextFormat::BackgroundBrush));
       
   232     fmt.clearProperty(QTextFormat::BackgroundBrush);
       
   233     QVERIFY(!fmt.hasProperty(QTextFormat::BackgroundBrush));
       
   234 
       
   235     QVERIFY(!fmt.hasProperty(QTextFormat::FontUnderline));
       
   236     QVERIFY(fmt.font().underline());
       
   237 
       
   238     // verify that deleting an existant but font _unrelated_ property does not affect the font resolving
       
   239 
       
   240     QVERIFY(fmt.hasProperty(QTextFormat::ForegroundBrush));
       
   241     fmt.clearProperty(QTextFormat::ForegroundBrush);
       
   242     QVERIFY(!fmt.hasProperty(QTextFormat::ForegroundBrush));
       
   243 
       
   244     QVERIFY(!fmt.hasProperty(QTextFormat::FontUnderline));
       
   245     QVERIFY(fmt.font().underline());
       
   246 
       
   247     // verify that removing a font property _does_ clear the resolving
       
   248 
       
   249     QVERIFY(fmt.hasProperty(QTextFormat::FontItalic));
       
   250     fmt.clearProperty(QTextFormat::FontItalic);
       
   251     QVERIFY(!fmt.hasProperty(QTextFormat::FontItalic));
       
   252 
       
   253     QVERIFY(!fmt.hasProperty(QTextFormat::FontUnderline));
       
   254     QVERIFY(!fmt.font().underline());
       
   255     QVERIFY(!fmt.font().italic());
       
   256 
       
   257     // reset
       
   258     fmt = formats.at(2).toCharFormat();
       
   259 
       
   260     QVERIFY(fmt.font().underline());
       
   261     QVERIFY(!fmt.hasProperty(QTextFormat::FontUnderline));
       
   262 
       
   263     // verify that _setting_ an unrelated property does _not_ affect the resolving
       
   264 
       
   265     QVERIFY(!fmt.hasProperty(QTextFormat::IsAnchor));
       
   266     fmt.setProperty(QTextFormat::IsAnchor, true);
       
   267     QVERIFY(fmt.hasProperty(QTextFormat::IsAnchor));
       
   268 
       
   269     QVERIFY(fmt.font().underline());
       
   270     QVERIFY(!fmt.hasProperty(QTextFormat::FontUnderline));
       
   271 
       
   272     // verify that setting a _related_ font property does affect the resolving
       
   273     //
       
   274     QVERIFY(!fmt.hasProperty(QTextFormat::FontStrikeOut));
       
   275     fmt.setProperty(QTextFormat::FontStrikeOut, true);
       
   276     QVERIFY(fmt.hasProperty(QTextFormat::FontStrikeOut));
       
   277 
       
   278     QVERIFY(!fmt.font().underline());
       
   279     QVERIFY(!fmt.hasProperty(QTextFormat::FontUnderline));
       
   280     QVERIFY(fmt.font().strikeOut());
       
   281 }
       
   282 
       
   283 void tst_QTextFormat::getSetTabs()
       
   284 {
       
   285     class Comparator {
       
   286       public:
       
   287         Comparator(const QList<QTextOption::Tab> &tabs, const QList<QTextOption::Tab> &tabs2)
       
   288         {
       
   289             QCOMPARE(tabs.count(), tabs2.count());
       
   290             for(int i=0; i < tabs.count(); i++) {
       
   291                 QTextOption::Tab t1 = tabs[i];
       
   292                 QTextOption::Tab t2 = tabs2[i];
       
   293                 QCOMPARE(t1.position, t2.position);
       
   294                 QCOMPARE(t1.type, t2.type);
       
   295                 QCOMPARE(t1.delimiter, t2.delimiter);
       
   296             }
       
   297         }
       
   298     };
       
   299 
       
   300     QList<QTextOption::Tab> tabs;
       
   301     QTextBlockFormat format;
       
   302     format.setTabPositions(tabs);
       
   303     Comparator c1(tabs, format.tabPositions());
       
   304 
       
   305     QTextOption::Tab tab1;
       
   306     tab1.position = 1234;
       
   307     tabs.append(tab1);
       
   308     format.setTabPositions(tabs);
       
   309     Comparator c2(tabs, format.tabPositions());
       
   310 
       
   311     QTextOption::Tab tab2;
       
   312     tab2.position = 3456;
       
   313     tab2.type = QTextOption::RightTab;
       
   314     tab2.delimiter = QChar('x');
       
   315     tabs.append(tab2);
       
   316     format.setTabPositions(tabs);
       
   317     Comparator c3(tabs, format.tabPositions());
       
   318 }
       
   319 
       
   320 void tst_QTextFormat::testTabsUsed()
       
   321 {
       
   322     QTextDocument doc;
       
   323     QTextCursor cursor(&doc);
       
   324 
       
   325     QList<QTextOption::Tab> tabs;
       
   326     QTextBlockFormat format;
       
   327     QTextOption::Tab tab;
       
   328     tab.position = 100;
       
   329     tabs.append(tab);
       
   330     format.setTabPositions(tabs);
       
   331     cursor.mergeBlockFormat(format);
       
   332     cursor.insertText("foo\tbar");
       
   333     //doc.setPageSize(QSizeF(200, 200));
       
   334     doc.documentLayout()->pageCount(); // force layout;
       
   335 
       
   336     QTextBlock block = doc.begin();
       
   337     QTextLayout *layout = block.layout();
       
   338     QVERIFY(layout);
       
   339     QCOMPARE(layout->lineCount(), 1);
       
   340     QTextLine line = layout->lineAt(0);
       
   341     QCOMPARE(line.cursorToX(4), 100.);
       
   342 
       
   343     QTextOption option = layout->textOption();
       
   344     QCOMPARE(option.tabs().count(), tabs.count());
       
   345 
       
   346 }
       
   347 
       
   348 void tst_QTextFormat::testFontStyleSetters()
       
   349 {
       
   350     QTextCharFormat format;
       
   351 
       
   352     // test the setters
       
   353     format.setFontStyleHint(QFont::Serif);
       
   354     QCOMPARE(format.font().styleHint(), QFont::Serif);
       
   355     QCOMPARE(format.font().styleStrategy(), QFont::PreferDefault);
       
   356     format.setFontStyleStrategy(QFont::PreferOutline);
       
   357     QCOMPARE(format.font().styleStrategy(), QFont::PreferOutline);
       
   358 
       
   359     // test setting properties through setFont()
       
   360     QFont font;
       
   361     font.setStyleHint(QFont::SansSerif, QFont::PreferAntialias);
       
   362     format.setFont(font);
       
   363     QCOMPARE(format.font().styleHint(), QFont::SansSerif);
       
   364     QCOMPARE(format.font().styleStrategy(), QFont::PreferAntialias);
       
   365 
       
   366     // test kerning
       
   367     format.setFontKerning(false);
       
   368     QCOMPARE(format.font().kerning(), false);
       
   369     format.setFontKerning(true);
       
   370     QCOMPARE(format.font().kerning(), true);
       
   371     font.setKerning(false);
       
   372     format.setFont(font);
       
   373     QCOMPARE(format.font().kerning(), false);
       
   374 }
       
   375 
       
   376 QTEST_MAIN(tst_QTextFormat)
       
   377 #include "tst_qtextformat.moc"