File indexing completed on 2024-04-14 14:20:59

0001 /*
0002     SPDX-FileCopyrightText: 2003, 2008 David Faure <faure@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005  */
0006 
0007 #include <QFontMetrics>
0008 
0009 #include <QTest>
0010 
0011 #include "kwordwrap.h"
0012 #include <QObject>
0013 
0014 class KWordWrap_UnitTest : public QObject
0015 {
0016     Q_OBJECT
0017 
0018 private Q_SLOTS:
0019     void initTestCase()
0020     {
0021         // Qt5 TODO: how to set the dpi?
0022         // Only found readonly QScreen::logicalDotsPerInch...
0023 #if 0
0024         QX11Info::setAppDpiX(0, 96);
0025         QX11Info::setAppDpiY(0, 96);
0026 #endif
0027     }
0028 
0029     void oldTruncationTest()
0030     {
0031         QFont font(QStringLiteral("helvetica"), 12); // let's hope we all have the same...
0032         QFontMetrics fm(font);
0033         QRect r(0, 0, 100, -1);
0034         QString str = QStringLiteral("test wadabada [/foo/bar/waba] and some more text here");
0035         KWordWrap ww = KWordWrap::formatText(fm, r, 0, str);
0036         // qDebug() << str << " => " << ww.truncatedString();
0037         QVERIFY(ww.truncatedString().endsWith("..."));
0038 
0039         str = QStringLiteral("</p></p></p></p>");
0040         for (; r.width() > 0; r.setWidth(r.width() - 10)) {
0041             ww = KWordWrap::formatText(fm, r, 0, str);
0042             // qDebug() << str << " => " << ww.truncatedString();
0043             QVERIFY(ww.truncatedString().endsWith("..."));
0044         }
0045     }
0046 
0047     void testWithExistingNewlines() // when the input string has \n already
0048     {
0049         QRect r(0, 0, 1000, -1); // very wide
0050         QFont font(QStringLiteral("helvetica"), 12); // let's hope we all have the same...
0051         QFontMetrics fm(font);
0052         QString inputString = QStringLiteral("The title here\nFoo (bar)\nFoo2 (bar2)");
0053         KWordWrap ww = KWordWrap::formatText(fm, r, 0, inputString);
0054         QString str = ww.wrappedString();
0055         QCOMPARE(str, inputString);
0056     }
0057 };
0058 
0059 QTEST_MAIN(KWordWrap_UnitTest)
0060 
0061 #include "kwordwraptest.moc"