File indexing completed on 2023-12-03 10:52:38

0001 /*
0002     File                 : TextLabelTest.cpp
0003     Project              : LabPlot
0004     Description          : Tests for TextLabel
0005     --------------------------------------------------------------------
0006     SPDX-FileCopyrightText: 2022 Stefan Gerlach <stefan.gerlach@uni.kn>
0007 
0008     SPDX-License-Identifier: GPL-2.0-or-later
0009 */
0010 
0011 #include "TextLabelTest.h"
0012 #include "backend/core/Project.h"
0013 #include "backend/lib/trace.h"
0014 #include "backend/worksheet/TextLabel.h"
0015 #include "backend/worksheet/TextLabelPrivate.h"
0016 #include "kdefrontend/widgets/LabelWidget.h"
0017 
0018 struct TextProperties {
0019     QColor fontColor;
0020     QColor backgroundColor;
0021     QString text;
0022     QString plainText;
0023     QFont font;
0024     int weigth;
0025     bool italic;
0026     bool underline;
0027 };
0028 
0029 QColor getColorFromHTMLText(const QString& text, const QString& colortype) {
0030     const QString htmlColorPattern(QStringLiteral("#[0-9A-Fa-f]{6}"));
0031     QRegularExpression fontColorPattern(colortype + QStringLiteral(":") + htmlColorPattern);
0032     QRegularExpressionMatch matchColor = fontColorPattern.match(text);
0033     // QVERIFY(matchColor.hasMatch());
0034     // QCOMPARE(matchColor.capturedTexts().count(), 1);
0035     QString color = matchColor.capturedTexts().at(0).split(colortype + QStringLiteral(":#")).at(1);
0036 
0037     int r = color.leftRef(2).toInt(nullptr, 16);
0038     int g = color.midRef(2, 2).toInt(nullptr, 16);
0039     int b = color.midRef(4, 2).toInt(nullptr, 16);
0040 
0041     QColor c(r, g, b);
0042     return c;
0043 }
0044 
0045 #define STORETEXTPROPERTIES(label, propertyVariable)                                                                                                           \
0046     TextProperties propertyVariable;                                                                                                                           \
0047     {                                                                                                                                                          \
0048         propertyVariable.text = label->text().text;                                                                                                            \
0049         QTextEdit te(label->text().text);                                                                                                                      \
0050         propertyVariable.font = te.font();                                                                                                                     \
0051         /* For some reason the two below don't work */                                                                                                         \
0052         /* propertyVariable.fontColor = te.textColor(); */                                                                                                     \
0053         /* propertyVariable.backgroundColor = te.textBackgroundColor(); */                                                                                     \
0054         propertyVariable.fontColor = getColorFromHTMLText(label->text().text, QStringLiteral("color"));                                                        \
0055         propertyVariable.backgroundColor = getColorFromHTMLText(label->text().text, QStringLiteral("background-color"));                                       \
0056         propertyVariable.italic = te.fontItalic();                                                                                                             \
0057         propertyVariable.underline = te.fontUnderline();                                                                                                       \
0058         propertyVariable.plainText = te.toPlainText();                                                                                                         \
0059         propertyVariable.weigth = te.fontWeight();                                                                                                             \
0060     }
0061 
0062 #define COMPARETEXTPROPERTIES(actual, expected)                                                                                                                \
0063     QCOMPARE(actual.backgroundColor, expected.backgroundColor);                                                                                                \
0064     QCOMPARE(actual.fontColor, expected.fontColor);                                                                                                            \
0065     QCOMPARE(actual.font, expected.font);                                                                                                                      \
0066     QCOMPARE(actual.italic, expected.italic);                                                                                                                  \
0067     QCOMPARE(actual.plainText, expected.plainText);                                                                                                            \
0068     QCOMPARE(actual.underline, expected.underline);                                                                                                            \
0069     QCOMPARE(actual.weigth, expected.weigth);                                                                                                                  \
0070     /* QCOMPARE(actual.text, expected.text); Cannot be used, because then also in the expected html text the color must be replaced*/
0071 
0072 #define COMPARETEXTPROPERTIESLABEL(label, expected)                                                                                                            \
0073     {                                                                                                                                                          \
0074         STORETEXTPROPERTIES(label, propertyVariable)                                                                                                           \
0075         COMPARETEXTPROPERTIES(propertyVariable, expected)                                                                                                      \
0076     }
0077 
0078 #define VERIFYLABELCOLORS(label, fontcolor_, backgroundColor_)                                                                                                 \
0079     {                                                                                                                                                          \
0080         QCOMPARE(getColorFromHTMLText(label->text().text, QStringLiteral("color")), fontcolor_);                                                               \
0081         QCOMPARE(getColorFromHTMLText(label->text().text, QStringLiteral("background-color")), backgroundColor_);                                              \
0082     }
0083 
0084 void TextLabelTest::addPlot() {
0085     Project project;
0086     auto* ws = new Worksheet(QStringLiteral("worksheet"));
0087     QVERIFY(ws != nullptr);
0088     project.addChild(ws);
0089 
0090     auto* p = new CartesianPlot(QStringLiteral("plot"));
0091     QVERIFY(p != nullptr);
0092     ws->addChild(p);
0093 
0094     auto* l = new TextLabel(QStringLiteral("Label"));
0095     QVERIFY(l != nullptr);
0096     l->setText(QStringLiteral("TextLabelText"));
0097     ws->addChild(l);
0098 
0099     QCOMPARE(l->text().mode, TextLabel::Mode::Text);
0100     VERIFYLABELCOLORS(l, Qt::black, Qt::white);
0101     QCOMPARE(l->fontColor(), Qt::black);
0102     QCOMPARE(l->backgroundColor(), Qt::white);
0103 
0104     // add title?
0105 
0106     // add axes?
0107     // check axis label
0108 }
0109 
0110 /*!
0111  * \brief TextLabelTest::multiLabelEdit
0112  * Test if changing the background color of one label
0113  * only the background color will be changed for the second
0114  * label, but no other properties
0115  */
0116 void TextLabelTest::multiLabelEditColorChange() {
0117     Project project;
0118     auto* ws = new Worksheet(QStringLiteral("worksheet"));
0119     QVERIFY(ws != nullptr);
0120     project.addChild(ws);
0121 
0122     auto* p = new CartesianPlot(QStringLiteral("plot"));
0123     QVERIFY(p != nullptr);
0124     ws->addChild(p);
0125 
0126     auto* l1 = new TextLabel(QStringLiteral("Label"));
0127     QVERIFY(l1 != nullptr);
0128     l1->setText(QStringLiteral("Text1"));
0129     ws->addChild(l1);
0130 
0131     VERIFYLABELCOLORS(l1, Qt::black, Qt::white);
0132 
0133     auto* l2 = new TextLabel(QStringLiteral("Label"));
0134     QVERIFY(l2 != nullptr);
0135     l2->setText(QStringLiteral("Text2"));
0136     ws->addChild(l2);
0137 
0138     STORETEXTPROPERTIES(l1, l1InitProperties);
0139     STORETEXTPROPERTIES(l2, l2InitProperties);
0140 
0141     LabelWidget w(nullptr);
0142     w.setLabels({l1}); // change only one textlabel
0143     w.fontColorChanged(Qt::red);
0144     w.backgroundColorChanged(Qt::blue);
0145 
0146     TextProperties l1p = l1InitProperties;
0147     l1p.fontColor = Qt::red;
0148     l1p.backgroundColor = Qt::blue;
0149     COMPARETEXTPROPERTIESLABEL(l1, l1p);
0150     COMPARETEXTPROPERTIESLABEL(l2, l2InitProperties);
0151 
0152     w.setLabels({l1, l2});
0153     w.fontColorChanged(Qt::cyan);
0154     w.backgroundColorChanged(Qt::green);
0155 
0156     l1p = l1InitProperties;
0157     l1p.fontColor = Qt::cyan;
0158     l1p.backgroundColor = Qt::green;
0159     COMPARETEXTPROPERTIESLABEL(l1, l1p);
0160     TextProperties l2p = l2InitProperties;
0161     l2p.fontColor = Qt::cyan;
0162     l2p.backgroundColor = Qt::green;
0163     COMPARETEXTPROPERTIESLABEL(l2, l2p);
0164 }
0165 
0166 /*!
0167  * \brief TextLabelTest::multiLabelEditTextChange
0168  * Changing the text of two labels changes the text content and more text related
0169  * properties
0170  */
0171 void TextLabelTest::multiLabelEditTextChange() {
0172     Project project;
0173     auto* ws = new Worksheet(QStringLiteral("worksheet"));
0174     QVERIFY(ws != nullptr);
0175     project.addChild(ws);
0176 
0177     auto* p = new CartesianPlot(QStringLiteral("plot"));
0178     QVERIFY(p != nullptr);
0179     ws->addChild(p);
0180 
0181     auto* l1 = new TextLabel(QStringLiteral("Label"));
0182     QVERIFY(l1 != nullptr);
0183     l1->setText(QStringLiteral("Text1"));
0184     ws->addChild(l1);
0185 
0186     auto* l2 = new TextLabel(QStringLiteral("Label"));
0187     QVERIFY(l2 != nullptr);
0188     l2->setText(QStringLiteral("Text2"));
0189     ws->addChild(l2);
0190 
0191     LabelWidget w(nullptr);
0192     w.setLabels({l1}); // change only one textlabel
0193     w.fontColorChanged(Qt::red);
0194     w.backgroundColorChanged(Qt::blue);
0195     w.fontSuperScriptChanged(true);
0196     w.fontStrikeOutChanged(true);
0197     w.fontUnderlineChanged(true);
0198     w.fontItalicChanged(true);
0199     w.fontBoldChanged(true);
0200     w.fontChanged(QFont(QStringLiteral("AkrutiTml2")));
0201 
0202     STORETEXTPROPERTIES(l1, l1InitProperties);
0203 
0204     w.setLabels({l1, l2});
0205     w.ui.teLabel->setText(QStringLiteral("New text"));
0206 
0207     TextProperties l1p = l1InitProperties;
0208     l1p.plainText = QStringLiteral("New text");
0209     COMPARETEXTPROPERTIESLABEL(l1, l1p);
0210     COMPARETEXTPROPERTIESLABEL(l2, l1p); // textlabel2 received all properties of the first label, because the text content changes
0211 }
0212 
0213 void TextLabelTest::multiLabelEditColorChangeSelection() {
0214     Project project;
0215     auto* ws = new Worksheet(QStringLiteral("worksheet"));
0216     QVERIFY(ws != nullptr);
0217     project.addChild(ws);
0218 
0219     auto* p = new CartesianPlot(QStringLiteral("plot"));
0220     QVERIFY(p != nullptr);
0221     ws->addChild(p);
0222 
0223     LabelWidget w(nullptr);
0224 
0225     auto* l1 = new TextLabel(QStringLiteral("Label"));
0226     QVERIFY(l1 != nullptr);
0227     ws->addChild(l1);
0228     l1->setText(QStringLiteral("This is the text of label 1"));
0229 
0230     VERIFYLABELCOLORS(l1, Qt::black, Qt::white);
0231 
0232     auto* l2 = new TextLabel(QStringLiteral("Label"));
0233     QVERIFY(l2 != nullptr);
0234     ws->addChild(l2);
0235     w.setLabels({l2}); // This setlabels is important, otherwise a specific scenario does not occur
0236     l2->setText(QStringLiteral("Text label 2"));
0237 
0238     STORETEXTPROPERTIES(l1, l1InitProperties);
0239     STORETEXTPROPERTIES(l2, l2InitProperties);
0240 
0241     w.setLabels({l1, l2});
0242     auto cursor = w.ui.teLabel->textCursor();
0243     // marks character 2 to 4
0244     cursor.setPosition(2);
0245     cursor.movePosition(QTextCursor::Right, QTextCursor::KeepAnchor, 2);
0246     w.ui.teLabel->setTextCursor(cursor);
0247     w.fontColorChanged(Qt::cyan);
0248     w.backgroundColorChanged(Qt::green);
0249     w.fontBoldChanged(true);
0250     w.fontItalicChanged(true);
0251 
0252     {
0253         QTextEdit te(l1->text().text);
0254         QTextCursor cTest = te.textCursor();
0255         cTest.setPosition(2);
0256         cTest.movePosition(QTextCursor::Right, QTextCursor::KeepAnchor, 2);
0257         te.setTextCursor(cTest);
0258         auto ccf = te.currentCharFormat();
0259         QCOMPARE(ccf.foreground().color(), Qt::cyan);
0260         QCOMPARE(ccf.background().color(), Qt::green);
0261         QCOMPARE(te.fontItalic(), true);
0262         QCOMPARE(te.fontWeight(), QFont::Bold);
0263         QCOMPARE(te.toPlainText(), QStringLiteral("This is the text of label 1"));
0264     }
0265 
0266     {
0267         QTextEdit te(l2->text().text);
0268         QTextCursor cTest = te.textCursor();
0269         cTest.setPosition(2);
0270         cTest.movePosition(QTextCursor::Right, QTextCursor::KeepAnchor, 2);
0271         te.setTextCursor(cTest);
0272         auto ccf = te.currentCharFormat();
0273         QCOMPARE(ccf.foreground().color(), Qt::cyan);
0274         QCOMPARE(ccf.background().color(), Qt::green);
0275         QCOMPARE(te.fontItalic(), true);
0276         QCOMPARE(te.fontWeight(), QFont::Bold);
0277         QCOMPARE(te.toPlainText(), QStringLiteral("Text label 2"));
0278     }
0279 
0280     w.fontBoldChanged(false);
0281     w.fontItalicChanged(false);
0282 
0283     {
0284         QTextEdit te(l1->text().text);
0285         QTextCursor cTest = te.textCursor();
0286         cTest.setPosition(2);
0287         cTest.movePosition(QTextCursor::Right, QTextCursor::KeepAnchor, 2);
0288         te.setTextCursor(cTest);
0289         auto ccf = te.currentCharFormat();
0290         QCOMPARE(ccf.foreground().color(), Qt::cyan);
0291         QCOMPARE(ccf.background().color(), Qt::green);
0292         QCOMPARE(te.fontItalic(), false);
0293         QCOMPARE(te.fontWeight(), QFont::Normal);
0294         QCOMPARE(te.toPlainText(), QStringLiteral("This is the text of label 1"));
0295     }
0296 
0297     {
0298         QTextEdit te(l2->text().text);
0299         QTextCursor cTest = te.textCursor();
0300         cTest.setPosition(2);
0301         cTest.movePosition(QTextCursor::Right, QTextCursor::KeepAnchor, 2);
0302         te.setTextCursor(cTest);
0303         auto ccf = te.currentCharFormat();
0304         QCOMPARE(ccf.foreground().color(), Qt::cyan);
0305         QCOMPARE(ccf.background().color(), Qt::green);
0306         QCOMPARE(te.fontItalic(), false);
0307         QCOMPARE(te.fontWeight(), QFont::Normal);
0308         QCOMPARE(te.toPlainText(), QStringLiteral("Text label 2"));
0309     }
0310 }
0311 
0312 QTEST_MAIN(TextLabelTest)