summaryrefslogtreecommitdiffstats
path: root/tests/auto/gui/text/qfont/tst_qfont.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'tests/auto/gui/text/qfont/tst_qfont.cpp')
-rw-r--r--tests/auto/gui/text/qfont/tst_qfont.cpp295
1 files changed, 227 insertions, 68 deletions
diff --git a/tests/auto/gui/text/qfont/tst_qfont.cpp b/tests/auto/gui/text/qfont/tst_qfont.cpp
index eb9d83c525..5426d7b117 100644
--- a/tests/auto/gui/text/qfont/tst_qfont.cpp
+++ b/tests/auto/gui/text/qfont/tst_qfont.cpp
@@ -1,34 +1,14 @@
-/****************************************************************************
-**
-** Copyright (C) 2016 The Qt Company Ltd.
-** Contact: https://www.qt.io/licensing/
-**
-** This file is part of the test suite of the Qt Toolkit.
-**
-** $QT_BEGIN_LICENSE:GPL-EXCEPT$
-** 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
-** a written agreement between you and The Qt Company. For licensing terms
-** and conditions see https://www.qt.io/terms-conditions. For further
-** information use the contact form at https://www.qt.io/contact-us.
-**
-** 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.
-**
-** $QT_END_LICENSE$
-**
-****************************************************************************/
-
-
-#include <QtTest/QtTest>
+// Copyright (C) 2016 The Qt Company Ltd.
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
+#undef QT_NO_FOREACH // this file contains unported legacy Q_FOREACH uses
+
+#include <QTest>
+#include <QBuffer>
+#include <QtEndian>
+#if QT_CONFIG(process)
+#include <QProcess>
+#endif
#include <qfont.h>
#include <private/qfont_p.h>
@@ -40,6 +20,10 @@
#include <qwidget.h>
#endif
#include <qlist.h>
+#include <QtTest/private/qemulationdetector_p.h>
+#include <private/qcomparisontesthelper_p.h>
+
+using namespace Qt::StringLiterals;
class tst_QFont : public QObject
{
@@ -58,6 +42,7 @@ private slots:
void insertAndRemoveSubstitutions();
void serialize_data();
void serialize();
+ void deserializeQt515();
void styleName();
void defaultFamily_data();
@@ -74,6 +59,9 @@ private slots:
void setFamilies();
void setFamiliesAndFamily_data();
void setFamiliesAndFamily();
+ void featureAccessors();
+ void tagCompares_data();
+ void tagCompares();
};
// Testing get/set functions
@@ -130,13 +118,25 @@ void tst_QFont::exactMatch()
QVERIFY(!QFont("serif").exactMatch());
QVERIFY(!QFont("monospace").exactMatch());
+ // Confirm that exactMatch is true for a valid font
+ 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);
+ QVERIFY(font.exactMatch());
+
+ if (QFontDatabase::families().contains("Arial")) {
+ font = QFont("Arial");
+ QVERIFY(font.exactMatch());
+ font = QFont(QString());
+ font.setFamilies({"Arial"});
+ QVERIFY(font.exactMatch());
+ }
}
void tst_QFont::italicOblique()
{
- QFontDatabase fdb;
-
- QStringList families = fdb.families();
+ QStringList families = QFontDatabase::families();
if (families.isEmpty())
return;
@@ -144,12 +144,12 @@ void tst_QFont::italicOblique()
for (f_it = families.begin(); f_it != f_end; ++f_it) {
QString family = *f_it;
- QStringList styles = fdb.styles(family);
+ QStringList styles = QFontDatabase::styles(family);
QStringList::ConstIterator s_it, s_end = styles.end();
for (s_it = styles.begin(); s_it != s_end; ++s_it) {
QString style = *s_it;
- if (fdb.isSmoothlyScalable(family, style)) {
+ if (QFontDatabase::isSmoothlyScalable(family, style)) {
if (style.contains("Oblique")) {
style.replace("Oblique", "Italic");
} else if (style.contains("Italic")) {
@@ -157,8 +157,8 @@ void tst_QFont::italicOblique()
} else {
continue;
}
- QFont f = fdb.font(family, style, 12);
- QVERIFY(f.italic());
+ QFont f = QFontDatabase::font(family, style, 12);
+ QVERIFY2(f.italic(), qPrintable(QString::asprintf("Failed for font \"%ls\"", qUtf16Printable(f.family()))));
}
}
}
@@ -299,10 +299,11 @@ void tst_QFont::resolve()
QCOMPARE(font6.families(), fontFamilies);
QFont font7, font8;
+ // This will call setFamilies() directly now
font7.setFamily(QLatin1String("Helvetica"));
font8.setFamilies(fontFamilies);
font7 = font7.resolve(font8);
- QCOMPARE(font7.families(), QStringList({"Helvetica", "Arial"}));
+ QCOMPARE(font7.families(), QStringList({"Helvetica"}));
QCOMPARE(font7.family(), "Helvetica");
}
@@ -322,22 +323,19 @@ void tst_QFont::resetFont()
QWidget secondChild(&parent);
secondChild.setFont(childFont);
- QVERIFY(parentFont.resolve() != 0);
- QVERIFY(childFont.resolve() != 0);
+ QVERIFY(parentFont.resolveMask() != 0);
+ QVERIFY(childFont.resolveMask() != 0);
QVERIFY(childFont != parentFont);
// reset font on both children
firstChild.setFont(QFont());
secondChild.setFont(QFont());
- QCOMPARE(firstChild.font().resolve(), QFont::SizeResolved);
- QCOMPARE(secondChild.font().resolve(), QFont::SizeResolved);
-#ifdef Q_OS_ANDROID
- QEXPECT_FAIL("", "QTBUG-69214", Continue);
-#endif
+ QCOMPARE(firstChild.font().resolveMask(), QFont::SizeResolved);
+ QCOMPARE(secondChild.font().resolveMask(), QFont::SizeResolved);
QCOMPARE(firstChild.font().pointSize(), parent.font().pointSize());
QCOMPARE(secondChild.font().pointSize(), parent.font().pointSize());
- QVERIFY(parent.font().resolve() != 0);
+ QVERIFY(parent.font().resolveMask() != 0);
}
#endif
@@ -367,15 +365,15 @@ void tst_QFont::insertAndRemoveSubstitutions()
// inserting Foo
QFont::insertSubstitution("BogusFontFamily", "Foo");
- QCOMPARE(QFont::substitutes("BogusFontFamily").count(), 1);
- QCOMPARE(QFont::substitutes("bogusfontfamily").count(), 1);
+ QCOMPARE(QFont::substitutes("BogusFontFamily").size(), 1);
+ QCOMPARE(QFont::substitutes("bogusfontfamily").size(), 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);
+ QCOMPARE(QFont::substitutes("BogusFontFamily").size(), 3);
+ QCOMPARE(QFont::substitutes("bogusfontfamily").size(), 3);
QFont::removeSubstitutions("BogusFontFamily");
// make sure it is empty again
@@ -402,7 +400,7 @@ void tst_QFont::serialize_data()
QTest::newRow("defaultConstructed") << font << QDataStream::Qt_1_0;
font.setLetterSpacing(QFont::AbsoluteSpacing, 105);
- QTest::newRow("letterSpacing") << font << QDataStream::Qt_4_5;
+ QTest::newRow("letterSpacing=105") << font << QDataStream::Qt_4_5;
font = basicFont;
font.setWordSpacing(50.0);
@@ -425,7 +423,7 @@ void tst_QFont::serialize_data()
QTest::newRow("stretch") << font << QDataStream::Qt_4_3;
font = basicFont;
- font.setWeight(99);
+ font.setWeight(QFont::Light);
QTest::newRow("weight") << font << QDataStream::Qt_1_0;
font = basicFont;
@@ -446,7 +444,7 @@ void tst_QFont::serialize_data()
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;
+ QTest::newRow("letterSpacing=10") << font << QDataStream::Qt_4_5;
font = basicFont;
font.setKerning(false);
@@ -497,6 +495,43 @@ void tst_QFont::serialize()
}
}
+void tst_QFont::deserializeQt515()
+{
+ QFile file;
+ file.setFileName(QFINDTESTDATA("datastream.515"));
+ QVERIFY(file.open(QIODevice::ReadOnly));
+
+ QFont font;
+ {
+ QDataStream stream(&file);
+ stream.setVersion(QDataStream::Qt_5_15);
+ stream >> font;
+ }
+
+ QCOMPARE(font.family(), QStringLiteral("FirstFamily"));
+ QCOMPARE(font.families().size(), 3);
+ QCOMPARE(font.families().at(0), QStringLiteral("FirstFamily"));
+ QCOMPARE(font.families().at(1), QStringLiteral("OtherFamily1"));
+ QCOMPARE(font.families().at(2), QStringLiteral("OtherFamily2"));
+ QCOMPARE(font.pointSize(), 12);
+
+ QVERIFY(file.reset());
+ QByteArray fileContent = file.readAll();
+ QByteArray serializedContent;
+ {
+ QBuffer buffer(&serializedContent);
+ QVERIFY(buffer.open(QIODevice::WriteOnly));
+
+ QDataStream stream(&buffer);
+ stream.setVersion(QDataStream::Qt_5_15);
+ stream << font;
+ }
+
+ QCOMPARE(serializedContent, fileContent);
+
+ file.close();
+}
+
void tst_QFont::styleName()
{
#if !defined(Q_OS_MAC)
@@ -549,12 +584,11 @@ void tst_QFont::defaultFamily()
QFETCH(QStringList, acceptableFamilies);
QFont f;
- QFontDatabase db;
f.setStyleHint(styleHint);
const QString familyForHint(f.defaultFamily());
// it should at least return a family that is available.
- QVERIFY(db.hasFamily(familyForHint));
+ QVERIFY(QFontDatabase::hasFamily(familyForHint));
bool isAcceptable = false;
Q_FOREACH (const QString& family, acceptableFamilies) {
@@ -564,6 +598,15 @@ void tst_QFont::defaultFamily()
}
}
+#if defined(Q_OS_UNIX) && defined(QT_NO_FONTCONFIG)
+ QSKIP("This platform does not support checking for default font acceptability");
+#endif
+
+#ifdef Q_PROCESSOR_ARM_32
+ if (QTestPrivate::isRunningArmOnX86())
+ QEXPECT_FAIL("", "Fails on ARMv7 QEMU (QTQAINFRA-4127)", Continue);
+#endif
+
#ifdef Q_OS_ANDROID
QEXPECT_FAIL("serif", "QTBUG-69215", Continue);
#endif
@@ -575,19 +618,43 @@ void tst_QFont::toAndFromString()
QFont defaultFont = QGuiApplication::font();
QString family = defaultFont.family();
- QFontDatabase fdb;
- const QStringList stylesList = fdb.styles(family);
+ const QStringList stylesList = QFontDatabase::styles(family);
if (stylesList.size() == 0)
QSKIP("Default font doesn't have any styles");
for (const QString &style : stylesList) {
QFont result;
- QFont initial = fdb.font(family, style, defaultFont.pointSize());
+ QFont initial = QFontDatabase::font(family, style, defaultFont.pointSize());
result.fromString(initial.toString());
QCOMPARE(result, initial);
}
+
+ // Since Qt 6.0 it was changed to include more information in the description, so
+ // this checks for compatibility
+ const QString fontStringFrom515(QLatin1String("Times New Roman,18,-1,5,75,1,0,0,1,0,Regular"));
+ 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);
+
+ 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"));
+ 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);
}
void tst_QFont::fromStringWithoutStyleName()
@@ -596,10 +663,18 @@ void tst_QFont::fromStringWithoutStyleName()
font1.fromString("Noto Sans,12,-1,5,50,0,0,0,0,0,Regular");
QFont font2 = font1;
- const QString str = "Times,16,-1,5,50,0,0,0,0,0";
+ const QString str = "Times,16,-1,5,400,0,0,0,0,0,0,0,0,0,0,1";
font2.fromString(str);
QCOMPARE(font2.toString(), str);
+
+ const QString fontStringFrom60(
+ QLatin1String("Times New Roman,18,-1,5,700,1,0,0,1,0,1,0,150.5,2.5,50,2"));
+ QFont font3;
+ font3.fromString("Noto Sans,12,-1,5,50,0,0,0,0,0,Regular");
+ QFont font4 = font3;
+ font4.fromString(fontStringFrom60);
+ QCOMPARE(font4.toString(), fontStringFrom60);
}
void tst_QFont::fromDegenerateString_data()
@@ -667,6 +742,7 @@ void tst_QFont::sharing()
void tst_QFont::familyNameWithCommaQuote_data()
{
+ QTest::addColumn<QString>("enteredFamilyName");
QTest::addColumn<QString>("familyName");
QTest::addColumn<QString>("chosenFamilyName");
@@ -674,15 +750,16 @@ void tst_QFont::familyNameWithCommaQuote_data()
if (standardFont.isEmpty())
QSKIP("No default font available on the system");
const QString weirdFont(QLatin1String("'My, weird'' font name',"));
+ const QString bogusFont(QLatin1String("BogusFont"));
const QString commaSeparated(standardFont + QLatin1String(",Times New Roman"));
const QString commaSeparatedWeird(weirdFont + QLatin1String(",") + standardFont);
- const QString commaSeparatedBogus(QLatin1String("BogusFont,") + standardFont);
+ const QString commaSeparatedBogus(bogusFont + QLatin1String(",") + standardFont);
- QTest::newRow("standard") << standardFont << standardFont;
- QTest::newRow("weird") << weirdFont << weirdFont;
- QTest::newRow("commaSeparated") << commaSeparated << standardFont;
- QTest::newRow("commaSeparatedWeird") << commaSeparatedWeird << weirdFont;
- QTest::newRow("commaSeparatedBogus") << commaSeparatedBogus << standardFont;
+ 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;
}
void tst_QFont::familyNameWithCommaQuote()
@@ -710,6 +787,7 @@ void tst_QFont::setFamilies_data()
if (standardFont.isEmpty())
QSKIP("No default font available on the system");
+ QTest::newRow("emptyFamily") << (QStringList()) << QString();
QTest::newRow("standard") << (QStringList() << standardFont) << standardFont;
QTest::newRow("weird") << (QStringList() << weirdFont) << weirdFont;
QTest::newRow("standard-weird") << (QStringList() << standardFont << weirdFont) << standardFont;
@@ -727,7 +805,8 @@ void tst_QFont::setFamilies()
QVERIFY(weirdFontId != -1);
QFont f;
f.setFamilies(families);
- QCOMPARE(QFontInfo(f).family(), chosenFamilyName);
+ if (!chosenFamilyName.isEmpty()) // Only check when it is not empty
+ QCOMPARE(QFontInfo(f).family(), chosenFamilyName);
QFontDatabase::removeApplicationFont(weirdFontId);
}
@@ -746,6 +825,7 @@ void tst_QFont::setFamiliesAndFamily_data()
const QString timesFont(QLatin1String("Times"));
const QString nonExistFont(QLatin1String("NonExistentFont"));
+ QTest::newRow("emptyFamily") << (QStringList()) << QString() << QString();
QTest::newRow("firstInFamilies") << (QStringList() << defaultFont << timesFont) << weirdFont << defaultFont;
QTest::newRow("secondInFamilies") << (QStringList() << nonExistFont << weirdFont) << defaultFont << weirdFont;
QTest::newRow("family") << (QStringList() << nonExistFont) << defaultFont << defaultFont;
@@ -761,12 +841,91 @@ void tst_QFont::setFamiliesAndFamily()
QVERIFY(weirdFontId != -1);
QFont f;
- f.setFamilies(families);
f.setFamily(family);
- QCOMPARE(QFontInfo(f).family(), chosenFamilyName);
+ f.setFamilies(families);
+ if (!family.isEmpty()) // Only check when it is not empty
+ QCOMPARE(QFontInfo(f).family(), chosenFamilyName);
QFontDatabase::removeApplicationFont(weirdFontId);
}
+void tst_QFont::featureAccessors()
+{
+ const QFont::Tag abcdTag("abcd");
+ QCOMPARE(abcdTag.toString(), "abcd");
+ QVERIFY(abcdTag.isValid());
+
+ QFont font;
+ QVERIFY(font.featureTags().isEmpty());
+ font.setFeature("abcd", 0xc0ffee);
+
+ QVERIFY(font.isFeatureSet(abcdTag));
+ QVERIFY(!font.isFeatureSet("bcde"));
+ QCOMPARE(font.featureTags().size(), 1);
+ QCOMPARE(font.featureTags().first(), abcdTag);
+ QCOMPARE(font.featureTags().first(), "abcd");
+ QCOMPARE(font.featureValue(abcdTag), 0xc0ffeeU);
+ QCOMPARE(font.featureValue("bcde"), 0U);
+ font.setFeature(abcdTag, 0xf00d);
+ QCOMPARE(font.featureTags().size(), 1);
+ QCOMPARE(font.featureValue(abcdTag), 0xf00dU);
+
+ QFont::Tag invalidTag;
+ QVERIFY(!invalidTag.isValid());
+ font.setFeature(invalidTag, 0xcaca0);
+ QVERIFY(!font.isFeatureSet(invalidTag));
+ QCOMPARE(font.featureTags().size(), 1);
+ QFont font2 = font;
+
+ font.unsetFeature("abcd");
+ QVERIFY(!font.isFeatureSet("abcd"));
+ QVERIFY(font.featureTags().isEmpty());
+
+ QVERIFY(font2.isFeatureSet("abcd"));
+ font2.clearFeatures();
+ QVERIFY(font.featureTags().isEmpty());
+
+ // various constructor compile tests
+ QFont::Tag tag;
+ tag = QFont::Tag("1234");
+ QVERIFY(QFont::Tag::fromString(QByteArray("abcd")));
+ QVERIFY(QFont::Tag::fromString(u"frac"_s));
+
+ // named constructors with invalid input
+ QTest::ignoreMessage(QtWarningMsg, "The tag name must be exactly 4 characters long!");
+ QVERIFY(!QFont::Tag::fromString(u"fraction"_s));
+ QVERIFY(!QFont::Tag::fromValue(0));
+ QVERIFY(QFont::Tag::fromValue(abcdTag.value()));
+
+ enum Features {
+ Frac = QFont::Tag("frac").value()
+ };
+}
+
+void tst_QFont::tagCompares_data()
+{
+ QTestPrivate::testAllComparisonOperatorsCompile<QFont::Tag>();
+
+ QTest::addColumn<QFont::Tag>("lhs");
+ QTest::addColumn<QFont::Tag>("rhs");
+ QTest::addColumn<Qt::strong_ordering>("expectedOrder");
+
+ auto row = [](QFont::Tag left, QFont::Tag right) {
+ QTest::addRow("%s<=>%s", left.toString().constData(), right.toString().constData())
+ << left << right << Qt::compareThreeWay(left.value(), right.value());
+ };
+ row("frac", "wght");
+}
+
+void tst_QFont::tagCompares()
+{
+ QFETCH(QFont::Tag, lhs);
+ QFETCH(QFont::Tag, rhs);
+ QFETCH(Qt::strong_ordering, expectedOrder);
+
+ QVERIFY(comparesEqual(lhs, lhs));
+ QCOMPARE(compareThreeWay(lhs, rhs), expectedOrder);
+}
+
QTEST_MAIN(tst_QFont)
#include "tst_qfont.moc"