File indexing completed on 2024-04-21 04:41:08

0001 /*
0002     SPDX-FileCopyrightText: 2019 Volker Krause <vkrause@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "../src/lib/datatypes/notesutil.cpp"
0008 
0009 #include <KPublicTransport/Journey>
0010 #include <KPublicTransport/Stopover>
0011 
0012 #include <QJsonDocument>
0013 #include <QJsonObject>
0014 #include <QTest>
0015 
0016 #define s(x) QStringLiteral(x)
0017 
0018 using namespace KPublicTransport;
0019 
0020 class NotesTest : public QObject
0021 {
0022     Q_OBJECT
0023 private Q_SLOTS:
0024     void testSerialization()
0025     {
0026         Stopover dep;
0027         dep.addNote(s("line1"));
0028         dep.addNote(s("line2"));
0029         QCOMPARE(dep.notes().size(), 2);
0030         const auto json = Stopover::toJson(dep);
0031         //qDebug().noquote() << QJsonDocument(json).toJson();
0032         const auto dep2 = Stopover::fromJson(json);
0033         QCOMPARE(dep.notes(), dep2.notes());
0034     }
0035 
0036     void testAddNotes_data()
0037     {
0038         QTest::addColumn<QStringList>("in");
0039         QTest::addColumn<QStringList>("res");
0040 
0041         QTest::newRow("empty") << QStringList() << QStringList();
0042         QTest::newRow("one") << QStringList({s("line1")}) << QStringList({s("line1")});
0043         QTest::newRow("two") << QStringList({s("line1"), s("line2")}) << QStringList({s("line1"), s("line2")});
0044         QTest::newRow("double") << QStringList({s("line1"), s("line2"), s("line1")}) << QStringList({s("line1"), s("line2")});
0045         QTest::newRow("substring first") << QStringList({s("note"), s("line2"), s("note - detail")}) << QStringList({s("note - detail"), s("line2")});
0046         QTest::newRow("substring second") << QStringList({s("note - detail"), s("line2"), s("note")}) << QStringList({s("note - detail"), s("line2")});
0047     }
0048 
0049     void testAddNotes()
0050     {
0051         QFETCH(QStringList, in);
0052         QFETCH(QStringList, res);
0053 
0054         JourneySection sec;
0055         sec.addNotes(in);
0056         QCOMPARE(sec.notes(), res);
0057         sec.addNotes(in);
0058         QCOMPARE(sec.notes(), res);
0059 
0060         Stopover dep;
0061         for (const auto &note : std::as_const(in))
0062             dep.addNote(note);
0063         QCOMPARE(dep.notes(), res);
0064     }
0065 
0066     void testNotesMerge()
0067     {
0068         {
0069             Stopover dep1;
0070             Stopover dep2;
0071             dep1.addNote(s("line1"));
0072             dep2.addNotes({s("line2"), s("line1")});
0073 
0074             const auto dep3 = Stopover::merge(dep1, dep2);
0075             QCOMPARE(dep3.notes(), QStringList({s("line1"), s("line2")}));
0076         }
0077 
0078         {
0079             Stopover dep1;
0080             Stopover dep2;
0081             dep1.addNote(s("line1 - detail"));
0082             dep2.addNotes({s("line2"), s("line1")});
0083 
0084             const auto dep3 = Stopover::merge(dep1, dep2);
0085             QCOMPARE(dep3.notes(), QStringList({s("line1 - detail"), s("line2")}));
0086         }
0087     }
0088 
0089     void testNormalize_data()
0090     {
0091         QTest::addColumn<QString>("in");
0092         QTest::addColumn<QString>("out");
0093 
0094         QTest::newRow("empty") << QString() << QString();
0095         QTest::newRow("leading space") << s(" note") << s("note");
0096         QTest::newRow("trailing space") << s("note ") << s("note");
0097         QTest::newRow("double space") << s("note  1") << s("note 1");
0098     }
0099 
0100     void testNormalize()
0101     {
0102         QFETCH(QString, in);
0103         QFETCH(QString, out);
0104         QCOMPARE(NotesUtil::normalizeNote(in), out);
0105     }
0106 
0107     void testRichText_data()
0108     {
0109         QTest::addColumn<QString>("in");
0110         QTest::addColumn<QString>("out");
0111 
0112         QTest::newRow("full url") << s("Check-in here: http://www.kde.org") << s("Check-in here: <a href=\"http://www.kde.org\">http://www.kde.org</a>");
0113         QTest::newRow("existing link") << s("Check-in here: <a href=\"http://www.kde.org\">http://www.kde.org</a>") << s("Check-in here: <a href=\"http://www.kde.org\">http://www.kde.org</a>");
0114         QTest::newRow("missing scheme") << s("Check-in here: www.kde.org/donate") << s("Check-in here: <a href=\"https://www.kde.org/donate\">www.kde.org/donate</a>");
0115         QTest::newRow("too much html") << s("<p><span style=\"color: rgb(37, 48, 59); font-family: &quot;Open Sans&quot;, Arial, sans-serif; font-size: 14px;\"><b>Attention</b></span></p><p><span style=\"color: rgb(37, 48, 59); font-family: &quot;Open Sans&quot;, Arial, sans-serif; font-size: 14px;\">Les lignes de&nbsp;BUS DIRECT ne sont pas accessibles avec un pass Navigo. Un ticket peut être acheté dans toutes les stations Métro-RER, y compris à l'aéroport.</span></p>") << s("<p><b>Attention</b></p><p>Les lignes de BUS DIRECT ne sont pas accessibles avec un pass Navigo. Un ticket peut être acheté dans toutes les stations Métro-RER, y compris à l'aéroport.</p>");
0116         QTest::newRow("empty p 1") << s("foo <p> </p>bar") << s("foo bar");
0117         QTest::newRow("empty p 2") << s("foo <p></p>bar") << s("foo bar");
0118         QTest::newRow("linebreak 1") << s("foo<br/></p>") << s("foo</p>");
0119         QTest::newRow("linebreak 2") << s("foo <p><br></p>bar") << s("foo bar");
0120         QTest::newRow("linebreak 3") << s("… Bauarbeiten nicht angefahren werden.<br> <br><br>\n<b>Ersatzhaltestelle: </b> <br>\n<p>Die Haltestelle …") << s("… Bauarbeiten nicht angefahren werden.<br/><b>Ersatzhaltestelle: </b> <p>Die Haltestelle …");
0121         QTest::newRow("linebreak 4") << s("… verlegt.<br/><br/>Alle anderen Haltesellen …") << s("… verlegt.<br/>Alle anderen Haltesellen …");
0122 
0123         QTest::newRow("style stripping") << s(" &nbsp;\n<p style=\"font-family: MetaWeb-Normal, sans-serif; font-size: 12px; font-variant-numeric: normal; font-variant-east-asian: normal; background-color: #ffffff; color: #666666; line-height: 1.5; margin: 0px 0px 0.5em; padding: 0px;\">Die Hauptstra&szlig;e wird von der Einm&uuml;ndung Otto-Hahn-Stra&szlig;e bis einschlie&szlig;lich Rathausplatz komplett saniert.") << s("<p>Die Hauptstra&szlig;e wird von der Einm&uuml;ndung Otto-Hahn-Stra&szlig;e bis einschlie&szlig;lich Rathausplatz komplett saniert.");
0124         QTest::newRow("leading spaces") << s(" &nbsp; Wegen Bauarbeiten") << s("Wegen Bauarbeiten");
0125     }
0126 
0127     void testRichText()
0128     {
0129         QFETCH(QString, in);
0130         QFETCH(QString, out);
0131         QCOMPARE(NotesUtil::normalizeNote(in), out);
0132     }
0133 };
0134 
0135 QTEST_GUILESS_MAIN(NotesTest)
0136 
0137 #include "notestest.moc"