tests/auto/qfontcombobox/tst_qfontcombobox.cpp
changeset 0 1918ee327afb
child 3 41300fa6a67c
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 #include <qfontcombobox.h>
       
    45 
       
    46 class tst_QFontComboBox : public QObject
       
    47 {
       
    48     Q_OBJECT
       
    49 
       
    50 public slots:
       
    51     void initTestCase();
       
    52     void cleanupTestCase();
       
    53     void init();
       
    54     void cleanup();
       
    55 
       
    56 private slots:
       
    57     void qfontcombobox_data();
       
    58     void qfontcombobox();
       
    59     void currentFont_data();
       
    60     void currentFont();
       
    61     void fontFilters_data();
       
    62     void fontFilters();
       
    63     void sizeHint();
       
    64     void writingSystem_data();
       
    65     void writingSystem();
       
    66     void currentFontChanged();
       
    67 };
       
    68 
       
    69 // Subclass that exposes the protected functions.
       
    70 class SubQFontComboBox : public QFontComboBox
       
    71 {
       
    72 public:
       
    73     void call_currentFontChanged(QFont const& f)
       
    74         { return SubQFontComboBox::currentFontChanged(f); }
       
    75 
       
    76     bool call_event(QEvent* e)
       
    77         { return SubQFontComboBox::event(e); }
       
    78 };
       
    79 
       
    80 // This will be called before the first test function is executed.
       
    81 // It is only called once.
       
    82 void tst_QFontComboBox::initTestCase()
       
    83 {
       
    84 }
       
    85 
       
    86 // This will be called after the last test function is executed.
       
    87 // It is only called once.
       
    88 void tst_QFontComboBox::cleanupTestCase()
       
    89 {
       
    90 }
       
    91 
       
    92 // This will be called before each test function is executed.
       
    93 void tst_QFontComboBox::init()
       
    94 {
       
    95 }
       
    96 
       
    97 // This will be called after every test function.
       
    98 void tst_QFontComboBox::cleanup()
       
    99 {
       
   100 }
       
   101 
       
   102 void tst_QFontComboBox::qfontcombobox_data()
       
   103 {
       
   104 }
       
   105 
       
   106 void tst_QFontComboBox::qfontcombobox()
       
   107 {
       
   108     SubQFontComboBox box;
       
   109     QCOMPARE(box.currentFont(), QFont());
       
   110     QCOMPARE(box.fontFilters(), QFontComboBox::AllFonts);
       
   111     box.setCurrentFont(QFont());
       
   112     box.setFontFilters(QFontComboBox::AllFonts);
       
   113     box.setWritingSystem(QFontDatabase::Any);
       
   114     QVERIFY(box.sizeHint() != QSize());
       
   115     QCOMPARE(box.writingSystem(), QFontDatabase::Any);
       
   116     box.call_currentFontChanged(QFont());
       
   117     QEvent event(QEvent::None);
       
   118     QCOMPARE(box.call_event(&event), false);
       
   119 }
       
   120 
       
   121 void tst_QFontComboBox::currentFont_data()
       
   122 {
       
   123     QTest::addColumn<QFont>("currentFont");
       
   124     // Normalize the names
       
   125     QFont defaultFont;
       
   126     QTest::newRow("default") << defaultFont;
       
   127     defaultFont.setPointSize(defaultFont.pointSize() + 10);
       
   128     QTest::newRow("default") << defaultFont;
       
   129     QFontDatabase db;
       
   130     QStringList list = db.families();
       
   131     for (int i = 0; i < list.count(); ++i) {
       
   132         QFont f = QFont(QFontInfo(QFont(list.at(i))).family());
       
   133         QTest::newRow(qPrintable(list.at(i))) << f;
       
   134     }
       
   135 }
       
   136 
       
   137 // public QFont currentFont() const
       
   138 void tst_QFontComboBox::currentFont()
       
   139 {
       
   140     QFETCH(QFont, currentFont);
       
   141 
       
   142     SubQFontComboBox box;
       
   143     QSignalSpy spy0(&box, SIGNAL(currentFontChanged(QFont const&)));
       
   144     QFont oldCurrentFont = box.currentFont();
       
   145 
       
   146     box.setCurrentFont(currentFont);
       
   147     QCOMPARE(box.currentFont(), currentFont);
       
   148     QString boxFontFamily = QFontInfo(box.currentFont()).family();
       
   149     QRegExp foundry(" \\[.*\\]");
       
   150     if (!currentFont.family().contains(foundry))
       
   151         boxFontFamily.remove(foundry);
       
   152     QCOMPARE(boxFontFamily, currentFont.family());
       
   153 
       
   154     if (oldCurrentFont != box.currentFont()) {
       
   155         //the signal may be emit twice if there is a foundry into brackets
       
   156         QVERIFY(spy0.count() >= 1);
       
   157     }
       
   158 }
       
   159 
       
   160 Q_DECLARE_METATYPE(QFontComboBox::FontFilters)
       
   161 void tst_QFontComboBox::fontFilters_data()
       
   162 {
       
   163     QTest::addColumn<QFontComboBox::FontFilters>("fontFilters");
       
   164     QTest::newRow("AllFonts")
       
   165         << QFontComboBox::FontFilters(QFontComboBox::AllFonts);
       
   166     QTest::newRow("ScalableFonts")
       
   167         << QFontComboBox::FontFilters(QFontComboBox::ScalableFonts);
       
   168     QTest::newRow("NonScalableFonts")
       
   169         << QFontComboBox::FontFilters(QFontComboBox::NonScalableFonts);
       
   170     QTest::newRow("MonospacedFonts")
       
   171         << QFontComboBox::FontFilters(QFontComboBox::MonospacedFonts);
       
   172     QTest::newRow("ProportionalFonts")
       
   173         << QFontComboBox::FontFilters(QFontComboBox::ProportionalFonts);
       
   174 
       
   175     // combine two
       
   176     QTest::newRow("ProportionalFonts | NonScalableFonts")
       
   177         << QFontComboBox::FontFilters(QFontComboBox::ProportionalFonts | QFontComboBox::NonScalableFonts);
       
   178 
       
   179     // i.e. all
       
   180     QTest::newRow("ScalableFonts | NonScalableFonts")
       
   181         << QFontComboBox::FontFilters(QFontComboBox::ScalableFonts | QFontComboBox::NonScalableFonts);
       
   182 
       
   183 }
       
   184 
       
   185 // public QFontComboBox::FontFilters fontFilters() const
       
   186 void tst_QFontComboBox::fontFilters()
       
   187 {
       
   188     QFETCH(QFontComboBox::FontFilters, fontFilters);
       
   189 
       
   190     SubQFontComboBox box;
       
   191     QSignalSpy spy0(&box, SIGNAL(currentFontChanged(QFont const&)));
       
   192     QFont currentFont = box.currentFont();
       
   193 
       
   194     box.setFontFilters(fontFilters);
       
   195     QCOMPARE(box.fontFilters(), fontFilters);
       
   196 
       
   197     QFontDatabase db;
       
   198     QStringList list = db.families();
       
   199     int c = 0;
       
   200     const int scalableMask = (QFontComboBox::ScalableFonts | QFontComboBox::NonScalableFonts);
       
   201     const int spacingMask = (QFontComboBox::ProportionalFonts | QFontComboBox::MonospacedFonts);
       
   202     if((fontFilters & scalableMask) == scalableMask)
       
   203         fontFilters &= ~scalableMask;
       
   204     if((fontFilters & spacingMask) == spacingMask)
       
   205         fontFilters &= ~spacingMask;
       
   206 
       
   207     for (int i = 0; i < list.count(); ++i) {
       
   208         if (fontFilters & QFontComboBox::ScalableFonts) {
       
   209             if (!db.isSmoothlyScalable(list[i]))
       
   210                 continue;
       
   211         } else if (fontFilters & QFontComboBox::NonScalableFonts) {
       
   212             if (db.isSmoothlyScalable(list[i]))
       
   213                 continue;
       
   214         }
       
   215         if (fontFilters & QFontComboBox::MonospacedFonts) {
       
   216             if (!db.isFixedPitch(list[i]))
       
   217                 continue;
       
   218         } else if (fontFilters & QFontComboBox::ProportionalFonts) {
       
   219             if (db.isFixedPitch(list[i]))
       
   220                 continue;
       
   221         }
       
   222         c++;
       
   223     }
       
   224 
       
   225     QCOMPARE(box.model()->rowCount(), c);
       
   226 
       
   227     if (c == 0)
       
   228         QCOMPARE(box.currentFont(), QFont());
       
   229 
       
   230     QCOMPARE(spy0.count(), (currentFont != box.currentFont()) ? 1 : 0);
       
   231 }
       
   232 
       
   233 // public QSize sizeHint() const
       
   234 void tst_QFontComboBox::sizeHint()
       
   235 {
       
   236     SubQFontComboBox box;
       
   237     QSize sizeHint = box.QComboBox::sizeHint();
       
   238     QFontMetrics fm(box.font());
       
   239     sizeHint.setWidth(qMax(sizeHint.width(), fm.width(QLatin1Char('m'))*14));
       
   240     QCOMPARE(box.sizeHint(), sizeHint);
       
   241 }
       
   242 
       
   243 Q_DECLARE_METATYPE(QFontDatabase::WritingSystem)
       
   244 void tst_QFontComboBox::writingSystem_data()
       
   245 {
       
   246     QTest::addColumn<QFontDatabase::WritingSystem>("writingSystem");
       
   247     QTest::newRow("Any") << QFontDatabase::Any;
       
   248     QTest::newRow("Latin") << QFontDatabase::Latin;
       
   249     QTest::newRow("Lao") << QFontDatabase::Lao;
       
   250     QTest::newRow("TraditionalChinese") << QFontDatabase::TraditionalChinese;
       
   251     QTest::newRow("Ogham") << QFontDatabase::Ogham;
       
   252     QTest::newRow("Runic") << QFontDatabase::Runic;
       
   253 
       
   254     for (int i = 0; i < 31; ++i)
       
   255         QTest::newRow("enum") << (QFontDatabase::WritingSystem)i;
       
   256 }
       
   257 
       
   258 // public QFontDatabase::WritingSystem writingSystem() const
       
   259 void tst_QFontComboBox::writingSystem()
       
   260 {
       
   261     QFETCH(QFontDatabase::WritingSystem, writingSystem);
       
   262 
       
   263     SubQFontComboBox box;
       
   264     QSignalSpy spy0(&box, SIGNAL(currentFontChanged(QFont const&)));
       
   265     QFont currentFont = box.currentFont();
       
   266 
       
   267     box.setWritingSystem(writingSystem);
       
   268     QCOMPARE(box.writingSystem(), writingSystem);
       
   269 
       
   270     QFontDatabase db;
       
   271     QStringList list = db.families(writingSystem);
       
   272     QCOMPARE(box.model()->rowCount(), list.count());
       
   273 
       
   274     if (list.count() == 0)
       
   275         QCOMPARE(box.currentFont(), QFont());
       
   276 
       
   277     QCOMPARE(spy0.count(), (currentFont != box.currentFont()) ? 1 : 0);
       
   278 }
       
   279 
       
   280 // protected void currentFontChanged(QFont const& f)
       
   281 void tst_QFontComboBox::currentFontChanged()
       
   282 {
       
   283     SubQFontComboBox box;
       
   284     QSignalSpy spy0(&box, SIGNAL(currentFontChanged(QFont const&)));
       
   285 
       
   286     if (box.model()->rowCount() > 2) {
       
   287         QTest::keyPress(&box, Qt::Key_Down);
       
   288         QCOMPARE(spy0.count(), 1);
       
   289     } else
       
   290         qWarning("Not enough fonts installed on test system. Consider adding some");
       
   291 }
       
   292 
       
   293 QTEST_MAIN(tst_QFontComboBox)
       
   294 #include "tst_qfontcombobox.moc"
       
   295