2011-04-27 10:05:43 +00:00
/****************************************************************************
* *
2016-01-15 12:36:27 +00:00
* * Copyright ( C ) 2016 The Qt Company Ltd .
* * Contact : https : //www.qt.io/licensing/
2011-04-27 10:05:43 +00:00
* *
* * This file is part of the test suite of the Qt Toolkit .
* *
2016-01-15 12:36:27 +00:00
* * $ QT_BEGIN_LICENSE : GPL - EXCEPT $
2012-09-19 12:28:29 +00:00
* * Commercial License Usage
* * Licensees holding valid commercial Qt licenses may use this file in
* * accordance with the commercial license agreement provided with the
* * Software or , alternatively , in accordance with the terms contained in
2015-01-28 08:44:43 +00:00
* * a written agreement between you and The Qt Company . For licensing terms
2016-01-15 12:36:27 +00:00
* * and conditions see https : //www.qt.io/terms-conditions. For further
* * information use the contact form at https : //www.qt.io/contact-us.
2012-09-19 12:28:29 +00:00
* *
2016-01-15 12:36:27 +00:00
* * GNU General Public License Usage
* * Alternatively , this file may be used under the terms of the GNU
* * General Public License version 3 as published by the Free Software
* * Foundation with exceptions as appearing in the file LICENSE . GPL3 - EXCEPT
* * included in the packaging of this file . Please review the following
* * information to ensure the GNU General Public License requirements will
* * be met : https : //www.gnu.org/licenses/gpl-3.0.html.
2011-04-27 10:05:43 +00:00
* *
* * $ QT_END_LICENSE $
* *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
# include <QtTest/QtTest>
# include <qfont.h>
2012-10-17 11:31:33 +00:00
# include <private/qfont_p.h>
2011-04-27 10:05:43 +00:00
# include <qfontdatabase.h>
# include <qfontinfo.h>
# include <qstringlist.h>
2012-06-05 12:00:47 +00:00
# include <qguiapplication.h>
# ifndef QT_NO_WIDGETS
2011-04-27 10:05:43 +00:00
# include <qwidget.h>
2012-06-05 12:00:47 +00:00
# endif
2011-04-27 10:05:43 +00:00
# include <qlist.h>
class tst_QFont : public QObject
{
Q_OBJECT
private slots :
void getSetCheck ( ) ;
void exactMatch ( ) ;
void compare ( ) ;
void resolve ( ) ;
2012-06-05 12:00:47 +00:00
# ifndef QT_NO_WIDGETS
2011-04-27 10:05:43 +00:00
void resetFont ( ) ;
2012-06-05 12:00:47 +00:00
# endif
2011-04-27 10:05:43 +00:00
void isCopyOf ( ) ;
void italicOblique ( ) ;
void insertAndRemoveSubstitutions ( ) ;
2014-03-26 10:06:31 +00:00
void serialize_data ( ) ;
void serialize ( ) ;
2020-05-12 14:14:07 +00:00
2011-06-04 12:47:22 +00:00
void styleName ( ) ;
2011-10-05 15:10:28 +00:00
void defaultFamily_data ( ) ;
void defaultFamily ( ) ;
2016-07-29 06:24:58 +00:00
void toAndFromString ( ) ;
2017-01-08 23:10:42 +00:00
void fromStringWithoutStyleName ( ) ;
2019-09-16 14:55:17 +00:00
void fromDegenerateString_data ( ) ;
void fromDegenerateString ( ) ;
2012-10-17 11:31:33 +00:00
void sharing ( ) ;
2018-09-14 22:12:42 +00:00
void familyNameWithCommaQuote_data ( ) ;
void familyNameWithCommaQuote ( ) ;
void setFamilies_data ( ) ;
void setFamilies ( ) ;
void setFamiliesAndFamily_data ( ) ;
void setFamiliesAndFamily ( ) ;
2011-04-27 10:05:43 +00:00
} ;
// Testing get/set functions
void tst_QFont : : getSetCheck ( )
{
QFont obj1 ;
// Style QFont::style()
// void QFont::setStyle(Style)
obj1 . setStyle ( QFont : : Style ( QFont : : StyleNormal ) ) ;
QCOMPARE ( QFont : : Style ( QFont : : StyleNormal ) , obj1 . style ( ) ) ;
obj1 . setStyle ( QFont : : Style ( QFont : : StyleItalic ) ) ;
QCOMPARE ( QFont : : Style ( QFont : : StyleItalic ) , obj1 . style ( ) ) ;
obj1 . setStyle ( QFont : : Style ( QFont : : StyleOblique ) ) ;
QCOMPARE ( QFont : : Style ( QFont : : StyleOblique ) , obj1 . style ( ) ) ;
// StyleStrategy QFont::styleStrategy()
// void QFont::setStyleStrategy(StyleStrategy)
obj1 . setStyleStrategy ( QFont : : StyleStrategy ( QFont : : PreferDefault ) ) ;
QCOMPARE ( QFont : : StyleStrategy ( QFont : : PreferDefault ) , obj1 . styleStrategy ( ) ) ;
obj1 . setStyleStrategy ( QFont : : StyleStrategy ( QFont : : PreferBitmap ) ) ;
QCOMPARE ( QFont : : StyleStrategy ( QFont : : PreferBitmap ) , obj1 . styleStrategy ( ) ) ;
obj1 . setStyleStrategy ( QFont : : StyleStrategy ( QFont : : PreferDevice ) ) ;
QCOMPARE ( QFont : : StyleStrategy ( QFont : : PreferDevice ) , obj1 . styleStrategy ( ) ) ;
obj1 . setStyleStrategy ( QFont : : StyleStrategy ( QFont : : PreferOutline ) ) ;
QCOMPARE ( QFont : : StyleStrategy ( QFont : : PreferOutline ) , obj1 . styleStrategy ( ) ) ;
obj1 . setStyleStrategy ( QFont : : StyleStrategy ( QFont : : ForceOutline ) ) ;
QCOMPARE ( QFont : : StyleStrategy ( QFont : : ForceOutline ) , obj1 . styleStrategy ( ) ) ;
obj1 . setStyleStrategy ( QFont : : StyleStrategy ( QFont : : PreferMatch ) ) ;
QCOMPARE ( QFont : : StyleStrategy ( QFont : : PreferMatch ) , obj1 . styleStrategy ( ) ) ;
obj1 . setStyleStrategy ( QFont : : StyleStrategy ( QFont : : PreferQuality ) ) ;
QCOMPARE ( QFont : : StyleStrategy ( QFont : : PreferQuality ) , obj1 . styleStrategy ( ) ) ;
obj1 . setStyleStrategy ( QFont : : StyleStrategy ( QFont : : PreferAntialias ) ) ;
QCOMPARE ( QFont : : StyleStrategy ( QFont : : PreferAntialias ) , obj1 . styleStrategy ( ) ) ;
obj1 . setStyleStrategy ( QFont : : StyleStrategy ( QFont : : NoAntialias ) ) ;
QCOMPARE ( QFont : : StyleStrategy ( QFont : : NoAntialias ) , obj1 . styleStrategy ( ) ) ;
}
void tst_QFont : : exactMatch ( )
{
QFont font ;
// Check if a non-existing font hasn't an exact match
font = QFont ( " BogusFont " , 33 ) ;
QVERIFY ( ! font . exactMatch ( ) ) ;
2016-05-11 12:10:50 +00:00
QVERIFY ( ! QFont ( " sans " ) . exactMatch ( ) ) ;
QVERIFY ( ! QFont ( " sans-serif " ) . exactMatch ( ) ) ;
QVERIFY ( ! QFont ( " serif " ) . exactMatch ( ) ) ;
QVERIFY ( ! QFont ( " monospace " ) . exactMatch ( ) ) ;
2018-09-14 22:12:42 +00:00
font . setFamilies ( QStringList ( ) < < " BogusFont " ) ;
QVERIFY ( ! font . exactMatch ( ) ) ;
QVERIFY ( ! QFont ( " sans " ) . exactMatch ( ) ) ;
QVERIFY ( ! QFont ( " sans-serif " ) . exactMatch ( ) ) ;
QVERIFY ( ! QFont ( " serif " ) . exactMatch ( ) ) ;
QVERIFY ( ! QFont ( " monospace " ) . exactMatch ( ) ) ;
2020-10-12 11:08:06 +00:00
// Confirm that exactMatch is true for a valid font
2020-11-02 18:48:25 +00:00
const QString family = QFontDatabase : : families ( ) . first ( ) ;
const QString style = QFontDatabase : : styles ( family ) . first ( ) ;
const int pointSize = QFontDatabase : : pointSizes ( family , style ) . first ( ) ;
font = QFontDatabase : : font ( family , style , pointSize ) ;
2020-10-12 11:08:06 +00:00
QVERIFY ( font . exactMatch ( ) ) ;
2020-11-02 18:48:25 +00:00
if ( QFontDatabase : : families ( ) . contains ( " Arial " ) ) {
2020-10-12 11:08:06 +00:00
font = QFont ( " Arial " ) ;
QVERIFY ( font . exactMatch ( ) ) ;
font = QFont ( QString ( ) ) ;
font . setFamilies ( { " Arial " } ) ;
QVERIFY ( font . exactMatch ( ) ) ;
}
2011-04-27 10:05:43 +00:00
}
void tst_QFont : : italicOblique ( )
{
2020-11-02 18:48:25 +00:00
QStringList families = QFontDatabase : : families ( ) ;
2011-04-27 10:05:43 +00:00
if ( families . isEmpty ( ) )
return ;
QStringList : : ConstIterator f_it , f_end = families . end ( ) ;
for ( f_it = families . begin ( ) ; f_it ! = f_end ; + + f_it ) {
2014-01-13 14:48:44 +00:00
QString family = * f_it ;
2020-11-02 18:48:25 +00:00
QStringList styles = QFontDatabase : : styles ( family ) ;
2014-01-13 14:48:44 +00:00
QStringList : : ConstIterator s_it , s_end = styles . end ( ) ;
for ( s_it = styles . begin ( ) ; s_it ! = s_end ; + + s_it ) {
QString style = * s_it ;
2020-11-02 18:48:25 +00:00
if ( QFontDatabase : : isSmoothlyScalable ( family , style ) ) {
2014-01-13 14:48:44 +00:00
if ( style . contains ( " Oblique " ) ) {
style . replace ( " Oblique " , " Italic " ) ;
} else if ( style . contains ( " Italic " ) ) {
style . replace ( " Italic " , " Oblique " ) ;
} else {
continue ;
}
2020-11-02 18:48:25 +00:00
QFont f = QFontDatabase : : font ( family , style , 12 ) ;
2014-01-13 14:48:44 +00:00
QVERIFY ( f . italic ( ) ) ;
}
}
2011-04-27 10:05:43 +00:00
}
}
void tst_QFont : : compare ( )
{
QFont font ;
{
2014-01-13 14:48:44 +00:00
QFont font2 = font ;
font2 . setPointSize ( 24 ) ;
QVERIFY ( font ! = font2 ) ;
QCOMPARE ( font < font2 , ! ( font2 < font ) ) ;
2011-04-27 10:05:43 +00:00
}
{
2014-01-13 14:48:44 +00:00
QFont font2 = font ;
font2 . setPixelSize ( 24 ) ;
QVERIFY ( font ! = font2 ) ;
QCOMPARE ( font < font2 , ! ( font2 < font ) ) ;
2011-04-27 10:05:43 +00:00
}
font . setPointSize ( 12 ) ;
font . setItalic ( false ) ;
font . setWeight ( QFont : : Normal ) ;
font . setUnderline ( false ) ;
font . setStrikeOut ( false ) ;
font . setOverline ( false ) ;
{
2014-01-13 14:48:44 +00:00
QFont font2 = font ;
font2 . setPointSize ( 24 ) ;
QVERIFY ( font ! = font2 ) ;
QCOMPARE ( font < font2 , ! ( font2 < font ) ) ;
2011-04-27 10:05:43 +00:00
}
{
2014-01-13 14:48:44 +00:00
QFont font2 = font ;
font2 . setPixelSize ( 24 ) ;
QVERIFY ( font ! = font2 ) ;
QCOMPARE ( font < font2 , ! ( font2 < font ) ) ;
2011-04-27 10:05:43 +00:00
}
{
2014-01-13 14:48:44 +00:00
QFont font2 = font ;
font2 . setItalic ( true ) ;
QVERIFY ( font ! = font2 ) ;
QCOMPARE ( font < font2 , ! ( font2 < font ) ) ;
font2 . setItalic ( false ) ;
2015-07-30 13:15:12 +00:00
QCOMPARE ( font , font2 ) ;
2014-01-13 14:48:44 +00:00
QVERIFY ( ! ( font < font2 ) ) ;
font2 . setWeight ( QFont : : Bold ) ;
QVERIFY ( font ! = font2 ) ;
QCOMPARE ( font < font2 , ! ( font2 < font ) ) ;
font2 . setWeight ( QFont : : Normal ) ;
2015-07-30 13:15:12 +00:00
QCOMPARE ( font , font2 ) ;
2014-01-13 14:48:44 +00:00
QVERIFY ( ! ( font < font2 ) ) ;
font . setUnderline ( true ) ;
QVERIFY ( font ! = font2 ) ;
QCOMPARE ( font < font2 , ! ( font2 < font ) ) ;
font . setUnderline ( false ) ;
2015-07-30 13:15:12 +00:00
QCOMPARE ( font , font2 ) ;
2014-01-13 14:48:44 +00:00
QVERIFY ( ! ( font < font2 ) ) ;
font . setStrikeOut ( true ) ;
QVERIFY ( font ! = font2 ) ;
QCOMPARE ( font < font2 , ! ( font2 < font ) ) ;
font . setStrikeOut ( false ) ;
2015-07-30 13:15:12 +00:00
QCOMPARE ( font , font2 ) ;
2014-01-13 14:48:44 +00:00
QVERIFY ( ! ( font < font2 ) ) ;
font . setOverline ( true ) ;
QVERIFY ( font ! = font2 ) ;
QCOMPARE ( font < font2 , ! ( font2 < font ) ) ;
font . setOverline ( false ) ;
2015-07-30 13:15:12 +00:00
QCOMPARE ( font , font2 ) ;
2014-01-13 14:48:44 +00:00
QVERIFY ( ! ( font < font2 ) ) ;
2011-04-27 10:05:43 +00:00
font . setCapitalization ( QFont : : SmallCaps ) ;
2014-01-13 14:48:44 +00:00
QVERIFY ( font ! = font2 ) ;
QCOMPARE ( font < font2 , ! ( font2 < font ) ) ;
2011-04-27 10:05:43 +00:00
font . setCapitalization ( QFont : : MixedCase ) ;
2015-07-30 13:15:12 +00:00
QCOMPARE ( font , font2 ) ;
2014-01-13 14:48:44 +00:00
QVERIFY ( ! ( font < font2 ) ) ;
2011-04-27 10:05:43 +00:00
}
}
void tst_QFont : : resolve ( )
{
QFont font ;
font . setPointSize ( font . pointSize ( ) * 2 ) ;
font . setItalic ( false ) ;
font . setWeight ( QFont : : Normal ) ;
font . setUnderline ( false ) ;
font . setStrikeOut ( false ) ;
font . setOverline ( false ) ;
font . setStretch ( QFont : : Unstretched ) ;
QFont font1 ;
font1 . setWeight ( QFont : : Bold ) ;
QFont font2 = font1 . resolve ( font ) ;
2015-07-30 13:15:12 +00:00
QCOMPARE ( font2 . weight ( ) , font1 . weight ( ) ) ;
2011-04-27 10:05:43 +00:00
2015-07-30 13:15:12 +00:00
QCOMPARE ( font2 . pointSize ( ) , font . pointSize ( ) ) ;
QCOMPARE ( font2 . italic ( ) , font . italic ( ) ) ;
QCOMPARE ( font2 . underline ( ) , font . underline ( ) ) ;
QCOMPARE ( font2 . overline ( ) , font . overline ( ) ) ;
QCOMPARE ( font2 . strikeOut ( ) , font . strikeOut ( ) ) ;
QCOMPARE ( font2 . stretch ( ) , font . stretch ( ) ) ;
2011-04-27 10:05:43 +00:00
QFont font3 ;
font3 . setStretch ( QFont : : UltraCondensed ) ;
QFont font4 = font3 . resolve ( font1 ) . resolve ( font ) ;
2015-07-30 13:15:12 +00:00
QCOMPARE ( font4 . stretch ( ) , font3 . stretch ( ) ) ;
2011-04-27 10:05:43 +00:00
2015-07-30 13:15:12 +00:00
QCOMPARE ( font4 . weight ( ) , font . weight ( ) ) ;
QCOMPARE ( font4 . pointSize ( ) , font . pointSize ( ) ) ;
QCOMPARE ( font4 . italic ( ) , font . italic ( ) ) ;
QCOMPARE ( font4 . underline ( ) , font . underline ( ) ) ;
QCOMPARE ( font4 . overline ( ) , font . overline ( ) ) ;
QCOMPARE ( font4 . strikeOut ( ) , font . strikeOut ( ) ) ;
2011-04-27 10:05:43 +00:00
QFont f1 , f2 , f3 ;
f2 . setPointSize ( 45 ) ;
f3 . setPointSize ( 55 ) ;
QFont f4 = f1 . resolve ( f2 ) ;
QCOMPARE ( f4 . pointSize ( ) , 45 ) ;
f4 = f4 . resolve ( f3 ) ;
QCOMPARE ( f4 . pointSize ( ) , 55 ) ;
2018-09-14 22:12:42 +00:00
QFont font5 , font6 ;
const QStringList fontFamilies = { QStringLiteral ( " Arial " ) } ;
font5 . setFamilies ( fontFamilies ) ;
font6 = font6 . resolve ( font5 ) ;
QCOMPARE ( font6 . families ( ) , fontFamilies ) ;
2019-10-29 08:44:50 +00:00
QFont font7 , font8 ;
2020-10-19 09:39:24 +00:00
// This will call setFamilies() directly now
2019-10-29 08:44:50 +00:00
font7 . setFamily ( QLatin1String ( " Helvetica " ) ) ;
font8 . setFamilies ( fontFamilies ) ;
font7 = font7 . resolve ( font8 ) ;
2020-10-19 09:39:24 +00:00
QCOMPARE ( font7 . families ( ) , QStringList ( { " Helvetica " } ) ) ;
2019-10-29 08:44:50 +00:00
QCOMPARE ( font7 . family ( ) , " Helvetica " ) ;
2011-04-27 10:05:43 +00:00
}
2012-06-05 12:00:47 +00:00
# ifndef QT_NO_WIDGETS
2011-04-27 10:05:43 +00:00
void tst_QFont : : resetFont ( )
{
QWidget parent ;
Fix font and palette propagation for themed children created at runtime
Widgets have a default palette and font that is influenced by several
factors: theme, style, QApplication-wide overrides, and the parent's.
If an application sets a font or palette on a parent, then widgets
inherit those settings, no matter when they are added to the parent.
The bug is that this is not true for widgets that have an application-
wide override defined by the platform theme. For those, we need to merge
parent palette and font attributes with the theme, and this is currently
not done correctly, as the respective masks are not merged and inherited.
This change fixes this for fonts and palettes. Children are inheriting
their parent's inheritance masks, combined with the mask for the
attributes set explicitly on the parent. This makes the font and palette
resolving code correctly adopt those attributes that are set explicily,
while leaving everything else as per the theme override.
The test verifies that this works for children and grand children added
to a widget that has a palette or font set, both when themed and
unthemed. Children with own entries don't inherit from parent.
The QFont::resetFont test had to be changed, as it was testing the
wrong behavior. If the child would be added to the parent before the
font property was set, then the test would have failed. Since this
change makes sure that children inherit fonts in the same way, no
matter on when they are added to their parent, the test is now
modified to cover both cases, and ensures that they return identical
results.
[ChangeLog][QtWidgets][QWidget] Fonts and palette settings are inherited
by children from their parents even if the children have application-
wide platform theme overrides.
Change-Id: I179a652b735e85bba3fafc30098d08d61684f488
Fixes: QTBUG-82125
Reviewed-by: Allan Sandfeld Jensen <allan.jensen@qt.io>
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
Reviewed-by: Vitaly Fanaskov <vitaly.fanaskov@qt.io>
2020-02-19 13:11:12 +00:00
QWidget firstChild ( & parent ) ;
2011-04-27 10:05:43 +00:00
QFont parentFont = parent . font ( ) ;
parentFont . setPointSize ( parentFont . pointSize ( ) + 2 ) ;
parent . setFont ( parentFont ) ;
Fix font and palette propagation for themed children created at runtime
Widgets have a default palette and font that is influenced by several
factors: theme, style, QApplication-wide overrides, and the parent's.
If an application sets a font or palette on a parent, then widgets
inherit those settings, no matter when they are added to the parent.
The bug is that this is not true for widgets that have an application-
wide override defined by the platform theme. For those, we need to merge
parent palette and font attributes with the theme, and this is currently
not done correctly, as the respective masks are not merged and inherited.
This change fixes this for fonts and palettes. Children are inheriting
their parent's inheritance masks, combined with the mask for the
attributes set explicitly on the parent. This makes the font and palette
resolving code correctly adopt those attributes that are set explicily,
while leaving everything else as per the theme override.
The test verifies that this works for children and grand children added
to a widget that has a palette or font set, both when themed and
unthemed. Children with own entries don't inherit from parent.
The QFont::resetFont test had to be changed, as it was testing the
wrong behavior. If the child would be added to the parent before the
font property was set, then the test would have failed. Since this
change makes sure that children inherit fonts in the same way, no
matter on when they are added to their parent, the test is now
modified to cover both cases, and ensures that they return identical
results.
[ChangeLog][QtWidgets][QWidget] Fonts and palette settings are inherited
by children from their parents even if the children have application-
wide platform theme overrides.
Change-Id: I179a652b735e85bba3fafc30098d08d61684f488
Fixes: QTBUG-82125
Reviewed-by: Allan Sandfeld Jensen <allan.jensen@qt.io>
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
Reviewed-by: Vitaly Fanaskov <vitaly.fanaskov@qt.io>
2020-02-19 13:11:12 +00:00
QFont childFont = firstChild . font ( ) ;
2011-04-27 10:05:43 +00:00
childFont . setBold ( ! childFont . bold ( ) ) ;
Fix font and palette propagation for themed children created at runtime
Widgets have a default palette and font that is influenced by several
factors: theme, style, QApplication-wide overrides, and the parent's.
If an application sets a font or palette on a parent, then widgets
inherit those settings, no matter when they are added to the parent.
The bug is that this is not true for widgets that have an application-
wide override defined by the platform theme. For those, we need to merge
parent palette and font attributes with the theme, and this is currently
not done correctly, as the respective masks are not merged and inherited.
This change fixes this for fonts and palettes. Children are inheriting
their parent's inheritance masks, combined with the mask for the
attributes set explicitly on the parent. This makes the font and palette
resolving code correctly adopt those attributes that are set explicily,
while leaving everything else as per the theme override.
The test verifies that this works for children and grand children added
to a widget that has a palette or font set, both when themed and
unthemed. Children with own entries don't inherit from parent.
The QFont::resetFont test had to be changed, as it was testing the
wrong behavior. If the child would be added to the parent before the
font property was set, then the test would have failed. Since this
change makes sure that children inherit fonts in the same way, no
matter on when they are added to their parent, the test is now
modified to cover both cases, and ensures that they return identical
results.
[ChangeLog][QtWidgets][QWidget] Fonts and palette settings are inherited
by children from their parents even if the children have application-
wide platform theme overrides.
Change-Id: I179a652b735e85bba3fafc30098d08d61684f488
Fixes: QTBUG-82125
Reviewed-by: Allan Sandfeld Jensen <allan.jensen@qt.io>
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
Reviewed-by: Vitaly Fanaskov <vitaly.fanaskov@qt.io>
2020-02-19 13:11:12 +00:00
firstChild . setFont ( childFont ) ;
QWidget secondChild ( & parent ) ;
secondChild . setFont ( childFont ) ;
2011-04-27 10:05:43 +00:00
2020-08-19 09:52:07 +00:00
QVERIFY ( parentFont . resolveMask ( ) ! = 0 ) ;
QVERIFY ( childFont . resolveMask ( ) ! = 0 ) ;
2011-04-27 10:05:43 +00:00
QVERIFY ( childFont ! = parentFont ) ;
Fix font and palette propagation for themed children created at runtime
Widgets have a default palette and font that is influenced by several
factors: theme, style, QApplication-wide overrides, and the parent's.
If an application sets a font or palette on a parent, then widgets
inherit those settings, no matter when they are added to the parent.
The bug is that this is not true for widgets that have an application-
wide override defined by the platform theme. For those, we need to merge
parent palette and font attributes with the theme, and this is currently
not done correctly, as the respective masks are not merged and inherited.
This change fixes this for fonts and palettes. Children are inheriting
their parent's inheritance masks, combined with the mask for the
attributes set explicitly on the parent. This makes the font and palette
resolving code correctly adopt those attributes that are set explicily,
while leaving everything else as per the theme override.
The test verifies that this works for children and grand children added
to a widget that has a palette or font set, both when themed and
unthemed. Children with own entries don't inherit from parent.
The QFont::resetFont test had to be changed, as it was testing the
wrong behavior. If the child would be added to the parent before the
font property was set, then the test would have failed. Since this
change makes sure that children inherit fonts in the same way, no
matter on when they are added to their parent, the test is now
modified to cover both cases, and ensures that they return identical
results.
[ChangeLog][QtWidgets][QWidget] Fonts and palette settings are inherited
by children from their parents even if the children have application-
wide platform theme overrides.
Change-Id: I179a652b735e85bba3fafc30098d08d61684f488
Fixes: QTBUG-82125
Reviewed-by: Allan Sandfeld Jensen <allan.jensen@qt.io>
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
Reviewed-by: Vitaly Fanaskov <vitaly.fanaskov@qt.io>
2020-02-19 13:11:12 +00:00
// reset font on both children
firstChild . setFont ( QFont ( ) ) ;
secondChild . setFont ( QFont ( ) ) ;
2011-04-27 10:05:43 +00:00
2020-08-19 09:52:07 +00:00
QCOMPARE ( firstChild . font ( ) . resolveMask ( ) , QFont : : SizeResolved ) ;
QCOMPARE ( secondChild . font ( ) . resolveMask ( ) , QFont : : SizeResolved ) ;
2018-06-29 11:42:27 +00:00
# ifdef Q_OS_ANDROID
QEXPECT_FAIL ( " " , " QTBUG-69214 " , Continue ) ;
# endif
Fix font and palette propagation for themed children created at runtime
Widgets have a default palette and font that is influenced by several
factors: theme, style, QApplication-wide overrides, and the parent's.
If an application sets a font or palette on a parent, then widgets
inherit those settings, no matter when they are added to the parent.
The bug is that this is not true for widgets that have an application-
wide override defined by the platform theme. For those, we need to merge
parent palette and font attributes with the theme, and this is currently
not done correctly, as the respective masks are not merged and inherited.
This change fixes this for fonts and palettes. Children are inheriting
their parent's inheritance masks, combined with the mask for the
attributes set explicitly on the parent. This makes the font and palette
resolving code correctly adopt those attributes that are set explicily,
while leaving everything else as per the theme override.
The test verifies that this works for children and grand children added
to a widget that has a palette or font set, both when themed and
unthemed. Children with own entries don't inherit from parent.
The QFont::resetFont test had to be changed, as it was testing the
wrong behavior. If the child would be added to the parent before the
font property was set, then the test would have failed. Since this
change makes sure that children inherit fonts in the same way, no
matter on when they are added to their parent, the test is now
modified to cover both cases, and ensures that they return identical
results.
[ChangeLog][QtWidgets][QWidget] Fonts and palette settings are inherited
by children from their parents even if the children have application-
wide platform theme overrides.
Change-Id: I179a652b735e85bba3fafc30098d08d61684f488
Fixes: QTBUG-82125
Reviewed-by: Allan Sandfeld Jensen <allan.jensen@qt.io>
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
Reviewed-by: Vitaly Fanaskov <vitaly.fanaskov@qt.io>
2020-02-19 13:11:12 +00:00
QCOMPARE ( firstChild . font ( ) . pointSize ( ) , parent . font ( ) . pointSize ( ) ) ;
QCOMPARE ( secondChild . font ( ) . pointSize ( ) , parent . font ( ) . pointSize ( ) ) ;
2020-08-19 09:52:07 +00:00
QVERIFY ( parent . font ( ) . resolveMask ( ) ! = 0 ) ;
2011-04-27 10:05:43 +00:00
}
2012-06-05 12:00:47 +00:00
# endif
2011-04-27 10:05:43 +00:00
void tst_QFont : : isCopyOf ( )
{
QFont font ;
2012-06-05 12:00:47 +00:00
QVERIFY ( font . isCopyOf ( QGuiApplication : : font ( ) ) ) ;
2011-04-27 10:05:43 +00:00
QFont font2 ( " bogusfont " , 23 ) ;
2012-06-05 12:00:47 +00:00
QVERIFY ( ! font2 . isCopyOf ( QGuiApplication : : font ( ) ) ) ;
2011-04-27 10:05:43 +00:00
QFont font3 = font ;
QVERIFY ( font3 . isCopyOf ( font ) ) ;
font3 . setPointSize ( 256 ) ;
QVERIFY ( ! font3 . isCopyOf ( font ) ) ;
font3 . setPointSize ( font . pointSize ( ) ) ;
QVERIFY ( ! font3 . isCopyOf ( font ) ) ;
}
void tst_QFont : : insertAndRemoveSubstitutions ( )
{
2015-08-24 11:35:22 +00:00
QFont : : removeSubstitutions ( " BogusFontFamily " ) ;
2011-04-27 10:05:43 +00:00
// make sure it is empty before we start
QVERIFY ( QFont : : substitutes ( " BogusFontFamily " ) . isEmpty ( ) ) ;
QVERIFY ( QFont : : substitutes ( " bogusfontfamily " ) . isEmpty ( ) ) ;
// inserting Foo
QFont : : insertSubstitution ( " BogusFontFamily " , " Foo " ) ;
QCOMPARE ( QFont : : substitutes ( " BogusFontFamily " ) . count ( ) , 1 ) ;
QCOMPARE ( QFont : : substitutes ( " bogusfontfamily " ) . count ( ) , 1 ) ;
// inserting Bar and Baz
QStringList moreFonts ;
moreFonts < < " Bar " < < " Baz " ;
QFont : : insertSubstitutions ( " BogusFontFamily " , moreFonts ) ;
QCOMPARE ( QFont : : substitutes ( " BogusFontFamily " ) . count ( ) , 3 ) ;
QCOMPARE ( QFont : : substitutes ( " bogusfontfamily " ) . count ( ) , 3 ) ;
2015-08-24 11:35:22 +00:00
QFont : : removeSubstitutions ( " BogusFontFamily " ) ;
2011-04-27 10:05:43 +00:00
// make sure it is empty again
QVERIFY ( QFont : : substitutes ( " BogusFontFamily " ) . isEmpty ( ) ) ;
QVERIFY ( QFont : : substitutes ( " bogusfontfamily " ) . isEmpty ( ) ) ;
}
2014-03-26 10:06:31 +00:00
Q_DECLARE_METATYPE ( QDataStream : : Version )
void tst_QFont : : serialize_data ( )
2011-04-27 10:05:43 +00:00
{
2014-03-26 10:06:31 +00:00
QTest : : addColumn < QFont > ( " font " ) ;
// The version in which the tested feature was added.
QTest : : addColumn < QDataStream : : Version > ( " minimumStreamVersion " ) ;
QFont basicFont ;
// Versions <= Qt 2.1 had broken point size serialization,
// so we set an integer point size.
basicFont . setPointSize ( 9 ) ;
2017-01-10 11:04:07 +00:00
// Versions <= Qt 5.4 didn't serialize styleName, so clear it
basicFont . setStyleName ( QString ( ) ) ;
2014-03-26 10:06:31 +00:00
QFont font = basicFont ;
QTest : : newRow ( " defaultConstructed " ) < < font < < QDataStream : : Qt_1_0 ;
2011-04-27 10:05:43 +00:00
font . setLetterSpacing ( QFont : : AbsoluteSpacing , 105 ) ;
2014-03-26 10:06:31 +00:00
QTest : : newRow ( " letterSpacing " ) < < font < < QDataStream : : Qt_4_5 ;
2011-04-27 10:05:43 +00:00
2014-03-26 10:06:31 +00:00
font = basicFont ;
2011-04-27 10:05:43 +00:00
font . setWordSpacing ( 50.0 ) ;
2014-03-26 10:06:31 +00:00
QTest : : newRow ( " wordSpacing " ) < < font < < QDataStream : : Qt_4_5 ;
font = basicFont ;
font . setPointSize ( 20 ) ;
QTest : : newRow ( " pointSize " ) < < font < < QDataStream : : Qt_1_0 ;
font = basicFont ;
font . setPixelSize ( 32 ) ;
QTest : : newRow ( " pixelSize " ) < < font < < QDataStream : : Qt_3_0 ;
font = basicFont ;
font . setStyleHint ( QFont : : Monospace ) ;
QTest : : newRow ( " styleHint " ) < < font < < QDataStream : : Qt_1_0 ;
font = basicFont ;
font . setStretch ( 4000 ) ;
QTest : : newRow ( " stretch " ) < < font < < QDataStream : : Qt_4_3 ;
font = basicFont ;
2020-08-05 08:47:33 +00:00
font . setWeight ( QFont : : Light ) ;
2014-03-26 10:06:31 +00:00
QTest : : newRow ( " weight " ) < < font < < QDataStream : : Qt_1_0 ;
font = basicFont ;
font . setUnderline ( true ) ;
QTest : : newRow ( " underline " ) < < font < < QDataStream : : Qt_1_0 ;
font = basicFont ;
font . setStrikeOut ( true ) ;
QTest : : newRow ( " strikeOut " ) < < font < < QDataStream : : Qt_1_0 ;
font = basicFont ;
font . setFixedPitch ( true ) ;
// This fails for versions less than this, as ignorePitch is set to false
// whenever setFixedPitch() is called, but ignorePitch is considered an
// extended bit, which were apparently not available until 4.4.
QTest : : newRow ( " fixedPitch " ) < < font < < QDataStream : : Qt_4_4 ;
font = basicFont ;
font . setLetterSpacing ( QFont : : AbsoluteSpacing , 10 ) ;
// Fails for 4.4 because letterSpacing wasn't read until 4.5.
QTest : : newRow ( " letterSpacing " ) < < font < < QDataStream : : Qt_4_5 ;
font = basicFont ;
font . setKerning ( false ) ;
QTest : : newRow ( " kerning " ) < < font < < QDataStream : : Qt_4_0 ;
font = basicFont ;
font . setStyleStrategy ( QFont : : NoFontMerging ) ;
// This wasn't read properly until 5.4.
QTest : : newRow ( " styleStrategy " ) < < font < < QDataStream : : Qt_5_4 ;
font = basicFont ;
font . setHintingPreference ( QFont : : PreferFullHinting ) ;
// This wasn't read until 5.4.
QTest : : newRow ( " hintingPreference " ) < < font < < QDataStream : : Qt_5_4 ;
font = basicFont ;
font . setStyleName ( " Regular Black Condensed " ) ;
// This wasn't read until 5.4.
QTest : : newRow ( " styleName " ) < < font < < QDataStream : : Qt_5_4 ;
2015-09-01 01:03:28 +00:00
font = basicFont ;
font . setCapitalization ( QFont : : AllUppercase ) ;
// This wasn't read until 5.6.
QTest : : newRow ( " capitalization " ) < < font < < QDataStream : : Qt_5_6 ;
2014-03-26 10:06:31 +00:00
}
2011-04-27 10:05:43 +00:00
2014-03-26 10:06:31 +00:00
void tst_QFont : : serialize ( )
{
QFETCH ( QFont , font ) ;
QFETCH ( QDataStream : : Version , minimumStreamVersion ) ;
QDataStream stream ;
const int thisVersion = stream . version ( ) ;
for ( int version = minimumStreamVersion ; version < = thisVersion ; + + version ) {
QBuffer buffer ;
buffer . open ( QIODevice : : WriteOnly ) ;
stream . setDevice ( & buffer ) ;
stream . setVersion ( version ) ;
stream < < font ;
buffer . close ( ) ;
buffer . open ( QIODevice : : ReadOnly ) ;
QFont readFont ;
stream > > readFont ;
QVERIFY2 ( readFont = = font , qPrintable ( QString : : fromLatin1 ( " Fonts do not compare equal for QDataStream version " ) +
QString : : fromLatin1 ( " %1: \n actual: %2 \n expected: %3 " ) . arg ( version ) . arg ( readFont . toString ( ) ) . arg ( font . toString ( ) ) ) ) ;
}
2011-04-27 10:05:43 +00:00
}
2011-06-04 12:47:22 +00:00
void tst_QFont : : styleName ( )
{
2012-01-16 11:02:14 +00:00
# if !defined(Q_OS_MAC)
2011-11-15 01:36:18 +00:00
QSKIP ( " Only tested on Mac " ) ;
# else
2011-06-04 12:47:22 +00:00
QFont font ( " Helvetica Neue " ) ;
font . setStyleName ( " UltraLight " ) ;
QCOMPARE ( QFontInfo ( font ) . styleName ( ) , QString ( " UltraLight " ) ) ;
2011-09-30 03:19:21 +00:00
# endif
2011-11-15 01:36:18 +00:00
}
2011-06-04 12:47:22 +00:00
2012-03-22 16:12:32 +00:00
QString getPlatformGenericFont ( const char * genericName )
{
2016-11-23 11:25:17 +00:00
# if defined(Q_OS_UNIX) && !defined(QT_NO_FONTCONFIG) && QT_CONFIG(process)
2012-03-22 16:12:32 +00:00
QProcess p ;
p . start ( QLatin1String ( " fc-match " ) , ( QStringList ( ) < < " -f%{family} " < < genericName ) ) ;
if ( ! p . waitForStarted ( ) )
qWarning ( " fc-match cannot be started: %s " , qPrintable ( p . errorString ( ) ) ) ;
if ( p . waitForFinished ( ) )
return QString : : fromLatin1 ( p . readAllStandardOutput ( ) ) ;
# endif
return QLatin1String ( genericName ) ;
}
static inline QByteArray msgNotAcceptableFont ( const QString & defaultFamily , const QStringList & acceptableFamilies )
{
2014-02-17 08:45:24 +00:00
QString res = QString : : fromLatin1 ( " Font family '%1' is not one of the following acceptable results: " ) . arg ( defaultFamily ) ;
2012-03-22 16:12:32 +00:00
Q_FOREACH ( const QString & family , acceptableFamilies )
2015-10-15 11:10:20 +00:00
res + = QLatin1String ( " \n " ) + family ;
2012-03-22 16:12:32 +00:00
return res . toLocal8Bit ( ) ;
}
2011-10-05 15:10:28 +00:00
Q_DECLARE_METATYPE ( QFont : : StyleHint )
void tst_QFont : : defaultFamily_data ( )
{
QTest : : addColumn < QFont : : StyleHint > ( " styleHint " ) ;
2012-03-22 16:12:32 +00:00
QTest : : addColumn < QStringList > ( " acceptableFamilies " ) ;
2016-03-16 08:09:16 +00:00
QTest : : newRow ( " serif " ) < < QFont : : Serif < < ( QStringList ( ) < < " Times New Roman " < < " Times " < < " Droid Serif " < < getPlatformGenericFont ( " serif " ) . split ( " , " ) ) ;
2019-09-12 13:52:06 +00:00
QTest : : newRow ( " monospace " ) < < QFont : : Monospace < < ( QStringList ( ) < < " Courier New " < < " Monaco " < < " Menlo " < < " Droid Sans Mono " < < getPlatformGenericFont ( " monospace " ) . split ( " , " ) ) ;
2016-03-16 08:09:16 +00:00
QTest : : newRow ( " cursive " ) < < QFont : : Cursive < < ( QStringList ( ) < < " Comic Sans MS " < < " Apple Chancery " < < " Roboto " < < " Droid Sans " < < getPlatformGenericFont ( " cursive " ) . split ( " , " ) ) ;
QTest : : newRow ( " fantasy " ) < < QFont : : Fantasy < < ( QStringList ( ) < < " Impact " < < " Zapfino " < < " Roboto " < < " Droid Sans " < < getPlatformGenericFont ( " fantasy " ) . split ( " , " ) ) ;
2019-09-12 13:52:06 +00:00
QTest : : newRow ( " sans-serif " ) < < QFont : : SansSerif < < ( QStringList ( ) < < " Arial " < < " Lucida Grande " < < " Helvetica " < < " Roboto " < < " Droid Sans " < < " Segoe UI " < < getPlatformGenericFont ( " sans-serif " ) . split ( " , " ) ) ;
2011-10-05 15:10:28 +00:00
}
void tst_QFont : : defaultFamily ( )
{
QFETCH ( QFont : : StyleHint , styleHint ) ;
2012-03-22 16:12:32 +00:00
QFETCH ( QStringList , acceptableFamilies ) ;
2011-10-05 15:10:28 +00:00
QFont f ;
f . setStyleHint ( styleHint ) ;
2012-03-22 16:12:32 +00:00
const QString familyForHint ( f . defaultFamily ( ) ) ;
2011-10-05 15:10:28 +00:00
2012-03-22 16:12:32 +00:00
// it should at least return a family that is available.
2020-11-02 18:48:25 +00:00
QVERIFY ( QFontDatabase : : hasFamily ( familyForHint ) ) ;
2012-03-22 16:12:32 +00:00
bool isAcceptable = false ;
Q_FOREACH ( const QString & family , acceptableFamilies ) {
if ( ! familyForHint . compare ( family , Qt : : CaseInsensitive ) ) {
isAcceptable = true ;
break ;
}
}
2013-08-07 11:14:32 +00:00
2018-06-29 11:42:27 +00:00
# ifdef Q_OS_ANDROID
QEXPECT_FAIL ( " serif " , " QTBUG-69215 " , Continue ) ;
# endif
2012-03-22 16:12:32 +00:00
QVERIFY2 ( isAcceptable , msgNotAcceptableFont ( familyForHint , acceptableFamilies ) ) ;
2011-10-05 15:10:28 +00:00
}
2016-07-29 06:24:58 +00:00
void tst_QFont : : toAndFromString ( )
{
QFont defaultFont = QGuiApplication : : font ( ) ;
QString family = defaultFont . family ( ) ;
2020-11-02 18:48:25 +00:00
const QStringList stylesList = QFontDatabase : : styles ( family ) ;
2016-07-29 06:24:58 +00:00
if ( stylesList . size ( ) = = 0 )
QSKIP ( " Default font doesn't have any styles " ) ;
for ( const QString & style : stylesList ) {
QFont result ;
2020-11-02 18:48:25 +00:00
QFont initial = QFontDatabase : : font ( family , style , defaultFont . pointSize ( ) ) ;
2016-07-29 06:24:58 +00:00
result . fromString ( initial . toString ( ) ) ;
QCOMPARE ( result , initial ) ;
}
2020-08-10 23:18:05 +00:00
// Since Qt 6.0 it was changed to include more information in the description, so
// this checks for compatibility
2020-11-18 12:45:19 +00:00
const QString fontStringFrom515 ( QLatin1String ( " Times New Roman,18,-1,5,75,1,0,0,1,0,Regular " ) ) ;
2020-08-10 23:18:05 +00:00
QFont fontFrom515 ( " Times New Roman " , 18 ) ;
fontFrom515 . setBold ( true ) ;
fontFrom515 . setItalic ( true ) ;
fontFrom515 . setFixedPitch ( true ) ;
fontFrom515 . setStyleName ( " Regular " ) ;
QFont from515String ;
from515String . fromString ( fontStringFrom515 ) ;
QCOMPARE ( from515String , fontFrom515 ) ;
2020-08-05 08:47:33 +00:00
const QString fontStringFrom60 (
QLatin1String ( " Times New Roman,18,-1,5,700,1,0,0,1,0,1,0,150.5,2.5,50,2,Regular " ) ) ;
2020-08-10 23:18:05 +00:00
QFont fontFrom60 = fontFrom515 ;
fontFrom60 . setStyleStrategy ( QFont : : PreferBitmap ) ;
fontFrom60 . setCapitalization ( QFont : : AllUppercase ) ;
fontFrom60 . setLetterSpacing ( QFont : : PercentageSpacing , 150.5 ) ;
fontFrom60 . setWordSpacing ( 2.5 ) ;
fontFrom60 . setStretch ( 50 ) ;
QFont from60String ;
from60String . fromString ( fontStringFrom60 ) ;
QCOMPARE ( fontFrom60 . toString ( ) , fontStringFrom60 ) ;
QCOMPARE ( from60String , fontFrom60 ) ;
2016-07-29 06:24:58 +00:00
}
2017-01-08 23:10:42 +00:00
void tst_QFont : : fromStringWithoutStyleName ( )
{
QFont font1 ;
2020-11-18 12:45:19 +00:00
font1 . fromString ( " Noto Sans,12,-1,5,50,0,0,0,0,0,Regular " ) ;
2017-01-08 23:10:42 +00:00
QFont font2 = font1 ;
2020-08-05 08:47:33 +00:00
const QString str = " Times,16,-1,5,400,0,0,0,0,0,0,0,0,0,0,1 " ;
2017-01-08 23:10:42 +00:00
font2 . fromString ( str ) ;
QCOMPARE ( font2 . toString ( ) , str ) ;
2020-08-10 23:18:05 +00:00
2020-08-05 08:47:33 +00:00
const QString fontStringFrom60 (
QLatin1String ( " Times New Roman,18,-1,5,700,1,0,0,1,0,1,0,150.5,2.5,50,2 " ) ) ;
2020-08-10 23:18:05 +00:00
QFont font3 ;
2020-11-18 12:45:19 +00:00
font3 . fromString ( " Noto Sans,12,-1,5,50,0,0,0,0,0,Regular " ) ;
2020-08-10 23:18:05 +00:00
QFont font4 = font3 ;
font4 . fromString ( fontStringFrom60 ) ;
QCOMPARE ( font4 . toString ( ) , fontStringFrom60 ) ;
2017-01-08 23:10:42 +00:00
}
2019-09-16 14:55:17 +00:00
void tst_QFont : : fromDegenerateString_data ( )
{
QTest : : addColumn < QString > ( " string " ) ;
QTest : : newRow ( " empty " ) < < QString ( ) ;
QTest : : newRow ( " justAComma " ) < < " , " ;
QTest : : newRow ( " commasAndSpaces " ) < < " , , " ;
QTest : : newRow ( " spaces " ) < < " " ;
QTest : : newRow ( " spacesTabsAndNewlines " ) < < " \t \n " ;
}
void tst_QFont : : fromDegenerateString ( )
{
QFETCH ( QString , string ) ;
QFont f ;
QTest : : ignoreMessage ( QtWarningMsg , QRegularExpression ( " .*Invalid description.* " ) ) ;
QCOMPARE ( f . fromString ( string ) , false ) ;
QCOMPARE ( f , QFont ( ) ) ;
}
2017-01-08 23:10:42 +00:00
2012-10-17 11:31:33 +00:00
void tst_QFont : : sharing ( )
{
2013-04-07 13:29:22 +00:00
// QFontCache references the engineData
int refs_by_cache = 1 ;
2012-10-17 11:31:33 +00:00
QFont f ;
f . setStyleHint ( QFont : : Serif ) ;
f . exactMatch ( ) ; // loads engine
2019-06-10 09:08:29 +00:00
QCOMPARE ( QFontPrivate : : get ( f ) - > ref . loadRelaxed ( ) , 1 ) ;
2012-10-17 11:31:33 +00:00
QVERIFY ( QFontPrivate : : get ( f ) - > engineData ) ;
2019-06-10 09:08:29 +00:00
QCOMPARE ( QFontPrivate : : get ( f ) - > engineData - > ref . loadRelaxed ( ) , 1 + refs_by_cache ) ;
2012-10-17 11:31:33 +00:00
QFont f2 ( f ) ;
2015-07-30 13:15:12 +00:00
QCOMPARE ( QFontPrivate : : get ( f2 ) , QFontPrivate : : get ( f ) ) ;
2019-06-10 09:08:29 +00:00
QCOMPARE ( QFontPrivate : : get ( f2 ) - > ref . loadRelaxed ( ) , 2 ) ;
2012-10-17 11:31:33 +00:00
QVERIFY ( QFontPrivate : : get ( f2 ) - > engineData ) ;
2015-07-30 13:15:12 +00:00
QCOMPARE ( QFontPrivate : : get ( f2 ) - > engineData , QFontPrivate : : get ( f ) - > engineData ) ;
2019-06-10 09:08:29 +00:00
QCOMPARE ( QFontPrivate : : get ( f2 ) - > engineData - > ref . loadRelaxed ( ) , 1 + refs_by_cache ) ;
2012-10-17 11:31:33 +00:00
f2 . setKerning ( ! f . kerning ( ) ) ;
QVERIFY ( QFontPrivate : : get ( f2 ) ! = QFontPrivate : : get ( f ) ) ;
2019-06-10 09:08:29 +00:00
QCOMPARE ( QFontPrivate : : get ( f2 ) - > ref . loadRelaxed ( ) , 1 ) ;
2012-10-17 11:31:33 +00:00
QVERIFY ( QFontPrivate : : get ( f2 ) - > engineData ) ;
2015-07-30 13:15:12 +00:00
QCOMPARE ( QFontPrivate : : get ( f2 ) - > engineData , QFontPrivate : : get ( f ) - > engineData ) ;
2019-06-10 09:08:29 +00:00
QCOMPARE ( QFontPrivate : : get ( f2 ) - > engineData - > ref . loadRelaxed ( ) , 2 + refs_by_cache ) ;
2012-10-17 11:31:33 +00:00
f2 = f ;
2015-07-30 13:15:12 +00:00
QCOMPARE ( QFontPrivate : : get ( f2 ) , QFontPrivate : : get ( f ) ) ;
2019-06-10 09:08:29 +00:00
QCOMPARE ( QFontPrivate : : get ( f2 ) - > ref . loadRelaxed ( ) , 2 ) ;
2012-10-17 11:31:33 +00:00
QVERIFY ( QFontPrivate : : get ( f2 ) - > engineData ) ;
2015-07-30 13:15:12 +00:00
QCOMPARE ( QFontPrivate : : get ( f2 ) - > engineData , QFontPrivate : : get ( f ) - > engineData ) ;
2019-06-10 09:08:29 +00:00
QCOMPARE ( QFontPrivate : : get ( f2 ) - > engineData - > ref . loadRelaxed ( ) , 1 + refs_by_cache ) ;
2012-10-17 11:31:33 +00:00
if ( f . pointSize ( ) > 0 )
f2 . setPointSize ( f . pointSize ( ) * 2 / 3 ) ;
else
f2 . setPixelSize ( f . pixelSize ( ) * 2 / 3 ) ;
QVERIFY ( QFontPrivate : : get ( f2 ) ! = QFontPrivate : : get ( f ) ) ;
2019-06-10 09:08:29 +00:00
QCOMPARE ( QFontPrivate : : get ( f2 ) - > ref . loadRelaxed ( ) , 1 ) ;
2012-10-17 11:31:33 +00:00
QVERIFY ( ! QFontPrivate : : get ( f2 ) - > engineData ) ;
QVERIFY ( QFontPrivate : : get ( f2 ) - > engineData ! = QFontPrivate : : get ( f ) - > engineData ) ;
}
2018-09-14 22:12:42 +00:00
void tst_QFont : : familyNameWithCommaQuote_data ( )
{
2020-10-19 09:39:24 +00:00
QTest : : addColumn < QString > ( " enteredFamilyName " ) ;
2018-09-14 22:12:42 +00:00
QTest : : addColumn < QString > ( " familyName " ) ;
QTest : : addColumn < QString > ( " chosenFamilyName " ) ;
const QString standardFont ( QFont ( ) . defaultFamily ( ) ) ;
if ( standardFont . isEmpty ( ) )
QSKIP ( " No default font available on the system " ) ;
const QString weirdFont ( QLatin1String ( " 'My, weird'' font name', " ) ) ;
2020-10-19 09:39:24 +00:00
const QString bogusFont ( QLatin1String ( " BogusFont " ) ) ;
2018-09-14 22:12:42 +00:00
const QString commaSeparated ( standardFont + QLatin1String ( " ,Times New Roman " ) ) ;
const QString commaSeparatedWeird ( weirdFont + QLatin1String ( " , " ) + standardFont ) ;
2020-10-19 09:39:24 +00:00
const QString commaSeparatedBogus ( bogusFont + QLatin1String ( " , " ) + standardFont ) ;
2018-09-14 22:12:42 +00:00
2020-10-19 09:39:24 +00:00
QTest : : newRow ( " standard " ) < < standardFont < < standardFont < < standardFont ;
QTest : : newRow ( " weird " ) < < weirdFont < < QString ( " 'My " ) < < standardFont ;
QTest : : newRow ( " commaSeparated " ) < < commaSeparated < < standardFont < < standardFont ;
QTest : : newRow ( " commaSeparatedWeird " ) < < commaSeparatedWeird < < QString ( " 'My " ) < < standardFont ;
QTest : : newRow ( " commaSeparatedBogus " ) < < commaSeparatedBogus < < bogusFont < < standardFont ;
2018-09-14 22:12:42 +00:00
}
void tst_QFont : : familyNameWithCommaQuote ( )
{
QFETCH ( QString , familyName ) ;
QFETCH ( QString , chosenFamilyName ) ;
const int weirdFontId = QFontDatabase : : addApplicationFont ( " :/weirdfont.otf " ) ;
QVERIFY ( weirdFontId ! = - 1 ) ;
QFont f ( familyName ) ;
QCOMPARE ( f . family ( ) , familyName ) ;
QCOMPARE ( QFontInfo ( f ) . family ( ) , chosenFamilyName ) ;
QFontDatabase : : removeApplicationFont ( weirdFontId ) ;
}
void tst_QFont : : setFamilies_data ( )
{
QTest : : addColumn < QStringList > ( " families " ) ;
QTest : : addColumn < QString > ( " chosenFamilyName " ) ;
const QString weirdFont ( QLatin1String ( " 'My, weird'' font name', " ) ) ;
const QString standardFont ( QFont ( ) . defaultFamily ( ) ) ;
if ( standardFont . isEmpty ( ) )
QSKIP ( " No default font available on the system " ) ;
2020-11-24 08:35:39 +00:00
QTest : : newRow ( " emptyFamily " ) < < ( QStringList ( ) ) < < QString ( ) ;
2018-09-14 22:12:42 +00:00
QTest : : newRow ( " standard " ) < < ( QStringList ( ) < < standardFont ) < < standardFont ;
QTest : : newRow ( " weird " ) < < ( QStringList ( ) < < weirdFont ) < < weirdFont ;
QTest : : newRow ( " standard-weird " ) < < ( QStringList ( ) < < standardFont < < weirdFont ) < < standardFont ;
QTest : : newRow ( " weird-standard " ) < < ( QStringList ( ) < < weirdFont < < standardFont ) < < weirdFont ;
QTest : : newRow ( " nonexist-weird " ) < < ( QStringList ( ) < < " NonExistentFont " < < weirdFont ) < < weirdFont ;
}
void tst_QFont : : setFamilies ( )
{
QFETCH ( QStringList , families ) ;
QFETCH ( QString , chosenFamilyName ) ;
const int weirdFontId = QFontDatabase : : addApplicationFont ( " :/weirdfont.otf " ) ;
QVERIFY ( weirdFontId ! = - 1 ) ;
QFont f ;
f . setFamilies ( families ) ;
2020-11-24 08:35:39 +00:00
if ( ! chosenFamilyName . isEmpty ( ) ) // Only check when it is not empty
QCOMPARE ( QFontInfo ( f ) . family ( ) , chosenFamilyName ) ;
2018-09-14 22:12:42 +00:00
QFontDatabase : : removeApplicationFont ( weirdFontId ) ;
}
void tst_QFont : : setFamiliesAndFamily_data ( )
{
QTest : : addColumn < QStringList > ( " families " ) ;
QTest : : addColumn < QString > ( " family " ) ;
QTest : : addColumn < QString > ( " chosenFamilyName " ) ;
const QString weirdFont ( QLatin1String ( " 'My, weird'' font name', " ) ) ;
const QString defaultFont ( QFont ( ) . defaultFamily ( ) ) ;
if ( defaultFont . isEmpty ( ) )
QSKIP ( " No default font available on the system " ) ;
const QString timesFont ( QLatin1String ( " Times " ) ) ;
const QString nonExistFont ( QLatin1String ( " NonExistentFont " ) ) ;
2020-11-24 08:35:39 +00:00
QTest : : newRow ( " emptyFamily " ) < < ( QStringList ( ) ) < < QString ( ) < < QString ( ) ;
2018-09-14 22:12:42 +00:00
QTest : : newRow ( " firstInFamilies " ) < < ( QStringList ( ) < < defaultFont < < timesFont ) < < weirdFont < < defaultFont ;
QTest : : newRow ( " secondInFamilies " ) < < ( QStringList ( ) < < nonExistFont < < weirdFont ) < < defaultFont < < weirdFont ;
QTest : : newRow ( " family " ) < < ( QStringList ( ) < < nonExistFont ) < < defaultFont < < defaultFont ;
}
void tst_QFont : : setFamiliesAndFamily ( )
{
QFETCH ( QStringList , families ) ;
QFETCH ( QString , family ) ;
QFETCH ( QString , chosenFamilyName ) ;
const int weirdFontId = QFontDatabase : : addApplicationFont ( " :/weirdfont.otf " ) ;
QVERIFY ( weirdFontId ! = - 1 ) ;
QFont f ;
f . setFamily ( family ) ;
2020-10-19 09:39:24 +00:00
f . setFamilies ( families ) ;
2020-11-24 08:35:39 +00:00
if ( ! family . isEmpty ( ) ) // Only check when it is not empty
QCOMPARE ( QFontInfo ( f ) . family ( ) , chosenFamilyName ) ;
2018-09-14 22:12:42 +00:00
QFontDatabase : : removeApplicationFont ( weirdFontId ) ;
}
2011-04-27 10:05:43 +00:00
QTEST_MAIN ( tst_QFont )
# include "tst_qfont.moc"