File indexing completed on 2024-04-28 05:50:35

0001 /*
0002     SPDX-FileCopyrightText: 2008 Robert Knight <robertknight@gmail.com>
0003     SPDX-FileCopyrightText: 2013, 2018 Kurt Hindenburg <kurt.hindenburg@gmail.com>
0004 
0005     SPDX-License-Identifier: GPL-2.0-or-later
0006 */
0007 
0008 // Own
0009 #include "TerminalCharacterDecoderTest.h"
0010 
0011 // Konsole
0012 #include "../decoders/HTMLDecoder.h"
0013 #include "../decoders/PlainTextDecoder.h"
0014 #include "colorscheme/ColorScheme.h"
0015 
0016 // Qt
0017 #include <QTextStream>
0018 
0019 // KDE
0020 #include <QTest>
0021 
0022 using namespace Konsole;
0023 
0024 void TerminalCharacterDecoderTest::convertToCharacter(Character *charResult, const QString &text, QVector<RenditionFlags> renditions)
0025 {
0026     // Force renditions size to match that of text; default will be DEFAULT_RENDITION.
0027     if (renditions.size() < text.size()) {
0028         renditions.resize(text.size());
0029     }
0030     for (int i = 0; i < text.size(); ++i) {
0031         charResult[i] = Character(text.at(i).unicode());
0032         charResult[i].rendition.all = renditions.at(i);
0033     }
0034 }
0035 
0036 void TerminalCharacterDecoderTest::testPlainTextDecoder_data()
0037 {
0038     QTest::addColumn<QString>("text");
0039     QTest::addColumn<QVector<RenditionFlags>>("renditions");
0040     QTest::addColumn<QString>("result");
0041 
0042     /* Notes:
0043      * - rendition has no effect on plain decoded text
0044      *
0045      * TODO: need to add foregroundColor, backgroundColor, and isRealCharacter
0046      */
0047     QTest::newRow("simple text with default rendition") << "hello" << QVector<RenditionFlags>(6).fill(DEFAULT_RENDITION) << "hello";
0048     QTest::newRow("simple text with bold rendition") << "hello" << QVector<RenditionFlags>(6).fill(RE_BOLD) << "hello";
0049     QTest::newRow("simple text with underline and italic rendition") << "hello" << QVector<RenditionFlags>(6).fill(RE_UNDERLINE_BIT | RE_ITALIC) << "hello";
0050 
0051     QTest::newRow("simple text with default rendition (shorten)") << "hello" << QVector<RenditionFlags>(4).fill(DEFAULT_RENDITION) << "hello";
0052     QTest::newRow("simple text with underline rendition (shorten)") << "hello" << QVector<RenditionFlags>(4).fill(RE_UNDERLINE_BIT) << "hello";
0053 }
0054 
0055 void TerminalCharacterDecoderTest::testPlainTextDecoder()
0056 {
0057     QFETCH(QString, text);
0058     QFETCH(QVector<RenditionFlags>, renditions);
0059     QFETCH(QString, result);
0060 
0061     TerminalCharacterDecoder *decoder = new PlainTextDecoder();
0062     auto testCharacters = new Character[text.size()];
0063     convertToCharacter(testCharacters, text, renditions);
0064     QString outputString;
0065     QTextStream outputStream(&outputString);
0066     decoder->begin(&outputStream);
0067     decoder->decodeLine(testCharacters, text.size(), /* ignored */ LineProperty());
0068     decoder->end();
0069     delete[] testCharacters;
0070     delete decoder;
0071     QCOMPARE(outputString, result);
0072 }
0073 
0074 void TerminalCharacterDecoderTest::testHTMLDecoder_data()
0075 {
0076     QTest::addColumn<QString>("text");
0077     QTest::addColumn<QVector<RenditionFlags>>("renditions");
0078     QTest::addColumn<QString>("result");
0079 
0080     /* Notes:
0081      * TODO: need to add foregroundColor, backgroundColor, and isRealCharacter
0082      */
0083     QTest::newRow("simple text with default rendition")
0084         << "hello" << QVector<RenditionFlags>(6).fill(DEFAULT_RENDITION)
0085         << R"(<span style="font-family:monospace"><span style="color:#000000;background-color:#ffffff;">hello</span><br></span>)";
0086     QTest::newRow("simple text with bold rendition")
0087         << "hello" << QVector<RenditionFlags>(6).fill(RE_BOLD)
0088         << R"(<span style="font-family:monospace"><span style="font-weight:bold;color:#000000;background-color:#ffffff;">hello</span><br></span>)";
0089     // The below is wrong; only the first rendition is used (eg ignores the |)
0090     QTest::newRow("simple text with underline and italic rendition")
0091         << "hello" << QVector<RenditionFlags>(6).fill(RE_UNDERLINE_BIT | RE_ITALIC)
0092         << R"(<span style="font-family:monospace"><span style="font-decoration:underline;color:#000000;background-color:#ffffff;">hello</span><br></span>)";
0093 
0094     QTest::newRow("text with &")
0095         << "hello &there" << QVector<RenditionFlags>(6).fill(DEFAULT_RENDITION)
0096         << R"(<span style="font-family:monospace"><span style="color:#000000;background-color:#ffffff;">hello &amp;there</span><br></span>)";
0097 }
0098 
0099 void TerminalCharacterDecoderTest::testHTMLDecoder()
0100 {
0101     QFETCH(QString, text);
0102     QFETCH(QVector<RenditionFlags>, renditions);
0103     QFETCH(QString, result);
0104 
0105     TerminalCharacterDecoder *decoder = new HTMLDecoder(ColorScheme::defaultTable);
0106     auto testCharacters = new Character[text.size()];
0107     convertToCharacter(testCharacters, text, renditions);
0108     QString outputString;
0109     QTextStream outputStream(&outputString);
0110     decoder->begin(&outputStream);
0111     decoder->decodeLine(testCharacters, text.size(), /* ignored */ LineProperty());
0112     decoder->end();
0113     delete[] testCharacters;
0114     delete decoder;
0115     QCOMPARE(outputString, result);
0116 }
0117 
0118 QTEST_GUILESS_MAIN(TerminalCharacterDecoderTest)
0119 
0120 #include "moc_TerminalCharacterDecoderTest.cpp"