tests/auto/qfontdatabase/tst_qfontdatabase.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 
       
    46 #include <qfontdatabase.h>
       
    47 
       
    48 #ifdef Q_OS_SYMBIAN
       
    49 #define SRCDIR "."
       
    50 #endif
       
    51 
       
    52 //TESTED_CLASS=
       
    53 //TESTED_FILES=
       
    54 
       
    55 class tst_QFontDatabase : public QObject
       
    56 {
       
    57 Q_OBJECT
       
    58 
       
    59 public:
       
    60     tst_QFontDatabase();
       
    61     virtual ~tst_QFontDatabase();
       
    62 
       
    63 public slots:
       
    64     void init();
       
    65     void cleanup();
       
    66 private slots:
       
    67     void styles_data();
       
    68     void styles();
       
    69 
       
    70     void fixedPitch_data();
       
    71     void fixedPitch();
       
    72 
       
    73     void widthTwoTimes_data();
       
    74     void widthTwoTimes();
       
    75 
       
    76     void addAppFont_data();
       
    77     void addAppFont();
       
    78 };
       
    79 
       
    80 tst_QFontDatabase::tst_QFontDatabase()
       
    81 {
       
    82 #ifndef Q_OS_IRIX
       
    83     QDir::setCurrent(SRCDIR);
       
    84 #endif
       
    85 }
       
    86 
       
    87 tst_QFontDatabase::~tst_QFontDatabase()
       
    88 {
       
    89 
       
    90 }
       
    91 
       
    92 void tst_QFontDatabase::init()
       
    93 {
       
    94 // TODO: Add initialization code here.
       
    95 // This will be executed immediately before each test is run.
       
    96 }
       
    97 
       
    98 void tst_QFontDatabase::cleanup()
       
    99 {
       
   100 // TODO: Add cleanup code here.
       
   101 // This will be executed immediately after each test is run.
       
   102 }
       
   103 
       
   104 void tst_QFontDatabase::styles_data()
       
   105 {
       
   106     QTest::addColumn<QString>("font");
       
   107 
       
   108     QTest::newRow( "data0" ) << QString( "Times New Roman" );
       
   109 }
       
   110 
       
   111 void tst_QFontDatabase::styles()
       
   112 {
       
   113     QFETCH( QString, font );
       
   114 
       
   115     QFontDatabase fdb;
       
   116     QStringList styles = fdb.styles( font );
       
   117     QStringList::Iterator it = styles.begin();
       
   118     while ( it != styles.end() ) {
       
   119 	QString style = *it;
       
   120 	QString trimmed = style.trimmed();
       
   121 	++it;
       
   122 
       
   123 	QCOMPARE( style, trimmed );
       
   124     }
       
   125 }
       
   126 
       
   127 void tst_QFontDatabase::fixedPitch_data()
       
   128 {
       
   129     QTest::addColumn<QString>("font");
       
   130     QTest::addColumn<bool>("fixedPitch");
       
   131 
       
   132     QTest::newRow( "Times New Roman" ) << QString( "Times New Roman" ) << false;
       
   133     QTest::newRow( "Arial" ) << QString( "Arial" ) << false;
       
   134     QTest::newRow( "Script" ) << QString( "Script" ) << false;
       
   135     QTest::newRow( "Courier" ) << QString( "Courier" ) << true;
       
   136     QTest::newRow( "Courier New" ) << QString( "Courier New" ) << true;
       
   137     QTest::newRow( "Lucida Console" ) << QString( "Lucida Console" ) << true;
       
   138 }
       
   139 
       
   140 void tst_QFontDatabase::fixedPitch()
       
   141 {
       
   142 #ifdef Q_WS_QWS
       
   143     QSKIP("fixedPitch not implemented for Qtopia Core", SkipAll);
       
   144 #endif
       
   145     QFETCH(QString, font);
       
   146     QFETCH(bool, fixedPitch);
       
   147 
       
   148     QFontDatabase fdb;
       
   149     if (!fdb.families().contains(font))
       
   150 	QSKIP( "Font not installed", SkipSingle);
       
   151 
       
   152     QCOMPARE(fdb.isFixedPitch(font), fixedPitch);
       
   153 
       
   154     QFont qfont(font);
       
   155     QFontInfo fi(qfont);
       
   156     QCOMPARE(fi.fixedPitch(), fixedPitch);
       
   157 }
       
   158 
       
   159 void tst_QFontDatabase::widthTwoTimes_data()
       
   160 {
       
   161     QTest::addColumn<QString>("font");
       
   162     QTest::addColumn<int>("pixelSize");
       
   163     QTest::addColumn<QString>("text");
       
   164 
       
   165     QTest::newRow("Arial") << QString("Arial") << 1000 << QString("Some text");
       
   166 }
       
   167 
       
   168 void tst_QFontDatabase::widthTwoTimes()
       
   169 {
       
   170     QFETCH(QString, font);
       
   171     QFETCH(int, pixelSize);
       
   172     QFETCH(QString, text);
       
   173 
       
   174     QFont f;
       
   175     f.setFamily(font);
       
   176     f.setPixelSize(pixelSize);
       
   177 
       
   178     QFontMetrics fm(f);
       
   179     int w1 = fm.charWidth(text, 0);
       
   180     int w2 = fm.charWidth(text, 0);
       
   181 
       
   182     QCOMPARE(w1, w2);
       
   183 }
       
   184 
       
   185 void tst_QFontDatabase::addAppFont_data()
       
   186 {
       
   187     QTest::addColumn<bool>("useMemoryFont");
       
   188     QTest::newRow("font file") << false;
       
   189     QTest::newRow("memory font") << true;
       
   190 }
       
   191 
       
   192 void tst_QFontDatabase::addAppFont()
       
   193 {
       
   194 #ifdef Q_OS_SYMBIAN
       
   195     QSKIP( "Symbian: Application fonts are not yet supported", SkipAll );
       
   196 #else
       
   197     QFETCH(bool, useMemoryFont);
       
   198     QSignalSpy fontDbChangedSpy(QApplication::instance(), SIGNAL(fontDatabaseChanged()));
       
   199 
       
   200     QFontDatabase db;
       
   201 
       
   202     const QStringList oldFamilies = db.families();
       
   203     QVERIFY(!oldFamilies.isEmpty());
       
   204 
       
   205     fontDbChangedSpy.clear();
       
   206 
       
   207     int id;
       
   208     if (useMemoryFont) {
       
   209         QFile fontfile("FreeMono.ttf");
       
   210         fontfile.open(QIODevice::ReadOnly);
       
   211         QByteArray fontdata = fontfile.readAll();
       
   212         QVERIFY(!fontdata.isEmpty());
       
   213         id = QFontDatabase::addApplicationFontFromData(fontdata);
       
   214     } else {
       
   215         id = QFontDatabase::addApplicationFont("FreeMono.ttf");
       
   216     }
       
   217 #if defined(Q_OS_HPUX) && defined(QT_NO_FONTCONFIG)
       
   218     // Documentation says that X11 systems that don't have fontconfig
       
   219     // don't support application fonts.
       
   220     QCOMPARE(id, -1);
       
   221     return;
       
   222 #endif
       
   223     QCOMPARE(fontDbChangedSpy.count(), 1);
       
   224 // addApplicationFont is supported on Mac, don't skip the test if it breaks.
       
   225 #ifndef Q_WS_MAC
       
   226     if (id == -1) {
       
   227         QSKIP("Skip the test since app fonts are not supported on this system", SkipSingle);
       
   228         return;
       
   229     }
       
   230 #endif
       
   231 
       
   232     const QStringList addedFamilies = QFontDatabase::applicationFontFamilies(id);
       
   233     QVERIFY(!addedFamilies.isEmpty());
       
   234 
       
   235     const QStringList newFamilies = db.families();
       
   236     QVERIFY(!newFamilies.isEmpty());
       
   237     QVERIFY(newFamilies.count() >= oldFamilies.count());
       
   238 
       
   239     for (int i = 0; i < addedFamilies.count(); ++i)
       
   240         QVERIFY(newFamilies.contains(addedFamilies.at(i)));
       
   241 
       
   242     QVERIFY(QFontDatabase::removeApplicationFont(id));
       
   243     QCOMPARE(fontDbChangedSpy.count(), 2);
       
   244 
       
   245     QVERIFY(db.families() == oldFamilies);
       
   246 #endif
       
   247 }
       
   248 
       
   249 QTEST_MAIN(tst_QFontDatabase)
       
   250 #include "tst_qfontdatabase.moc"