File indexing completed on 2024-09-22 04:08:57

0001 /*
0002  * SPDX-FileCopyrightText: 2023 Wolthera van Hövell tot Westerflier <griffinvalley@gmail.com>
0003  *
0004  * SPDX-License-Identifier: GPL-3.0-or-later
0005  */
0006 #include "SvgTextCursorTest.h"
0007 
0008 #include <SvgTextCursor.h>
0009 #include <SvgTextInsertCommand.h>
0010 #include <SvgTextRemoveCommand.h>
0011 
0012 #include <KoSvgTextShape.h>
0013 #include <KoSvgTextShapeMarkupConverter.h>
0014 #include <KoFontRegistry.h>
0015 
0016 #include <tests/MockShapes.h>
0017 #include <simpletest.h>
0018 #include <testui.h>
0019 void SvgTextCursorTest::initTestCase()
0020 {
0021     QString fileName = QString(FILES_DATA_DIR) + '/' + "DejaVuSans.ttf";
0022     bool res = KoFontRegistry::instance()->addFontFilePathToRegistery(fileName);
0023 
0024     QVERIFY2(res, QString("KoFontRegistry could not add the test font %1").arg(fileName).toLatin1());
0025 }
0026 
0027 void SvgTextCursorTest::test_ltr_data()
0028 {
0029     QTest::addColumn<SvgTextCursor::MoveMode>("mode");
0030     QTest::addColumn<bool>("visual");
0031     QTest::addColumn<int>("result");
0032 
0033     QTest::addRow("down")  << SvgTextCursor::MoveDown << false << 16;
0034     QTest::addRow("up  ")  << SvgTextCursor::MoveUp << false  << 0;
0035     QTest::addRow("left")  << SvgTextCursor::MoveLeft << false  << 4;
0036     QTest::addRow("right") << SvgTextCursor::MoveRight << false  << 6;
0037     QTest::addRow("word left")  << SvgTextCursor::MoveWordLeft << false  << 4;
0038     QTest::addRow("word right") << SvgTextCursor::MoveWordRight << false  << 9;
0039     QTest::addRow("line end")   << SvgTextCursor::MoveLineEnd << false  << 10;
0040     QTest::addRow("line start") << SvgTextCursor::MoveLineStart << false  << 0;
0041     QTest::addRow("paragraph end")   << SvgTextCursor::ParagraphEnd << false  << 48;
0042     QTest::addRow("paragraph start") << SvgTextCursor::ParagraphStart << false  << 0;
0043     QTest::addRow("visual - down")  << SvgTextCursor::MoveDown << true << 16;
0044     QTest::addRow("visual - up  ")  << SvgTextCursor::MoveUp << true  << 0;
0045     QTest::addRow("visual - left")  << SvgTextCursor::MoveLeft << true  << 4;
0046     QTest::addRow("visual - right") << SvgTextCursor::MoveRight << true  << 6;
0047     QTest::addRow("visual - word left")  << SvgTextCursor::MoveWordLeft << true  << 4;
0048     QTest::addRow("visual - word right") << SvgTextCursor::MoveWordRight << true  << 9;
0049     QTest::addRow("visual - line end")   << SvgTextCursor::MoveLineEnd << true  << 10;
0050     QTest::addRow("visual - line start") << SvgTextCursor::MoveLineStart << true  << 0;
0051     QTest::addRow("visual - paragraph end")   << SvgTextCursor::ParagraphEnd << true  << 48;
0052     QTest::addRow("visual - paragraph start") << SvgTextCursor::ParagraphStart << true  << 0;
0053 }
0054 
0055 void SvgTextCursorTest::test_ltr()
0056 {
0057     KoSvgTextShape *textShape = new KoSvgTextShape();
0058     QString ref ("<text style=\"inline-size:50.0; font-size:10.0;font-family:Deja Vu Sans\">The quick brown fox jumps over the lazy dog.</text>");
0059     KoSvgTextShapeMarkupConverter converter(textShape);
0060     converter.convertFromSvg(ref, QString(), QRectF(0, 0, 300, 300), 72.0);
0061 
0062     MockCanvas canvas;
0063     SvgTextCursor cursor(&canvas);
0064     cursor.setShape(textShape);
0065 
0066     QFETCH(SvgTextCursor::MoveMode, mode);
0067     QFETCH(bool, visual);
0068     QFETCH(int, result);
0069     cursor.setVisualMode(visual);
0070 
0071     //The current textcursor sets the pos to the text end...
0072     //QCOMPARE(cursor.getPos(), 0);
0073     cursor.setPos(5, 5);
0074 
0075     cursor.moveCursor(mode);
0076     QCOMPARE(cursor.getPos(), result);
0077 }
0078 
0079 void SvgTextCursorTest::test_rtl_data()
0080 {
0081     QTest::addColumn<SvgTextCursor::MoveMode>("mode");
0082     QTest::addColumn<bool>("visual");
0083     QTest::addColumn<int>("result");
0084 
0085     QTest::addRow("down")  << SvgTextCursor::MoveDown << false << 22;
0086     QTest::addRow("up  ")  << SvgTextCursor::MoveUp << false  << 6;
0087     QTest::addRow("left")  << SvgTextCursor::MoveLeft << false  << 11;
0088     QTest::addRow("right") << SvgTextCursor::MoveRight << false  << 9;
0089     QTest::addRow("word left")  << SvgTextCursor::MoveWordLeft << false  << 11;
0090     QTest::addRow("word right") << SvgTextCursor::MoveWordRight << false  << 8;
0091     QTest::addRow("line end")   << SvgTextCursor::MoveLineEnd << false  << 16;
0092     QTest::addRow("line start") << SvgTextCursor::MoveLineStart << false  << 8;
0093     QTest::addRow("paragraph end")   << SvgTextCursor::ParagraphEnd << false  << 33;
0094     QTest::addRow("paragraph start") << SvgTextCursor::ParagraphStart << false  << 0;
0095     QTest::addRow("visual -down")  << SvgTextCursor::MoveDown << true << 22;
0096     QTest::addRow("visual - up  ")  << SvgTextCursor::MoveUp << true  << 6;
0097     QTest::addRow("visual - left")  << SvgTextCursor::MoveLeft << true  << 9;
0098     QTest::addRow("visual - right") << SvgTextCursor::MoveRight << true  << 11;
0099     QTest::addRow("visual - word left")  << SvgTextCursor::MoveWordLeft << true  << 11;
0100     QTest::addRow("visual - word right") << SvgTextCursor::MoveWordRight << true  << 8;
0101     QTest::addRow("visual - line end")   << SvgTextCursor::MoveLineEnd << true  << 16;
0102     QTest::addRow("visual - line start") << SvgTextCursor::MoveLineStart << true  << 8;
0103     QTest::addRow("visual - paragraph end")   << SvgTextCursor::ParagraphEnd << true  << 33;
0104     QTest::addRow("visual - paragraph start") << SvgTextCursor::ParagraphStart << true  << 0;
0105 }
0106 
0107 void SvgTextCursorTest::test_rtl()
0108 {
0109     KoSvgTextShape *textShape = new KoSvgTextShape();
0110     QString ref ("<text style=\"inline-size:50.0; font-size:10.0; direction:rtl; font-family:Deja Vu Sans\">داستان SVG 1.1 SE طولا ني است.</text>");
0111     KoSvgTextShapeMarkupConverter converter(textShape);
0112     converter.convertFromSvg(ref, QString(), QRectF(0, 0, 300, 300), 72.0);
0113 
0114     MockCanvas canvas;
0115     SvgTextCursor cursor(&canvas);
0116     cursor.setShape(textShape);
0117 
0118     QFETCH(SvgTextCursor::MoveMode, mode);
0119     QFETCH(bool, visual);
0120     QFETCH(int, result);
0121     cursor.setVisualMode(visual);
0122 
0123     //The current textcursor sets the pos to the text end...
0124     //QCOMPARE(cursor.getPos(), 0);
0125     cursor.setPos(10, 10);
0126 
0127     cursor.moveCursor(mode);
0128     QCOMPARE(cursor.getPos(), result);
0129 }
0130 void SvgTextCursorTest::test_ttb_rl_data()
0131 {
0132     QTest::addColumn<SvgTextCursor::MoveMode>("mode");
0133     QTest::addColumn<bool>("visual");
0134     QTest::addColumn<int>("result");
0135 
0136     QTest::addRow("down")  << SvgTextCursor::MoveDown << false << 6;
0137     QTest::addRow("up  ")  << SvgTextCursor::MoveUp << false  << 4;
0138     QTest::addRow("left")  << SvgTextCursor::MoveLeft << false  << 12;
0139     QTest::addRow("right") << SvgTextCursor::MoveRight << false  << 0;
0140     QTest::addRow("word left")  << SvgTextCursor::MoveWordLeft << false  << 12;
0141     QTest::addRow("word right") << SvgTextCursor::MoveWordRight << false  << 0;
0142     QTest::addRow("line end")   << SvgTextCursor::MoveLineEnd << false  << 6;
0143     QTest::addRow("line start") << SvgTextCursor::MoveLineStart << false  << 0;
0144     QTest::addRow("paragraph end")   << SvgTextCursor::ParagraphEnd << false  << 36;
0145     QTest::addRow("paragraph start") << SvgTextCursor::ParagraphStart << false  << 0;
0146     QTest::addRow("visual - down")  << SvgTextCursor::MoveDown << true << 6;
0147     QTest::addRow("visual - up  ")  << SvgTextCursor::MoveUp << true  << 4;
0148     QTest::addRow("visual - left")  << SvgTextCursor::MoveLeft << true  << 12;
0149     QTest::addRow("visual - right") << SvgTextCursor::MoveRight << true  << 0;
0150     QTest::addRow("visual - word left")  << SvgTextCursor::MoveWordLeft << true  << 12;
0151     QTest::addRow("visual - word right") << SvgTextCursor::MoveWordRight << true  << 0;
0152     QTest::addRow("visual - line end")   << SvgTextCursor::MoveLineEnd << true  << 6;
0153     QTest::addRow("visual - line start") << SvgTextCursor::MoveLineStart << true  << 0;
0154     QTest::addRow("visual - paragraph end")   << SvgTextCursor::ParagraphEnd << true  << 36;
0155     QTest::addRow("visual - paragraph start") << SvgTextCursor::ParagraphStart << true  << 0;
0156 }
0157 
0158 void SvgTextCursorTest::test_ttb_rl()
0159 {
0160     KoSvgTextShape *textShape = new KoSvgTextShape();
0161     QString ref ("<text style=\"inline-size:50.0; font-size:10.0; writing-mode:vertical-rl; font-family:Deja Vu Sans\">A B C D E F G H I J K L M N O P</text>");
0162     KoSvgTextShapeMarkupConverter converter(textShape);
0163     converter.convertFromSvg(ref, QString(), QRectF(0, 0, 300, 300), 72.0);
0164 
0165     MockCanvas canvas;
0166     SvgTextCursor cursor(&canvas);
0167     cursor.setShape(textShape);
0168 
0169     QFETCH(SvgTextCursor::MoveMode, mode);
0170     QFETCH(bool, visual);
0171     QFETCH(int, result);
0172     cursor.setVisualMode(visual);
0173 
0174     //The current textcursor sets the pos to the text end...
0175     //QCOMPARE(cursor.getPos(), 0);
0176     cursor.setPos(5, 5);
0177 
0178     cursor.moveCursor(mode);
0179     QCOMPARE(cursor.getPos(), result);
0180 }
0181 void SvgTextCursorTest::test_ttb_lr_data()
0182 {
0183     QTest::addColumn<SvgTextCursor::MoveMode>("mode");
0184     QTest::addColumn<bool>("visual");
0185     QTest::addColumn<int>("result");
0186 
0187     QTest::addRow("down")  << SvgTextCursor::MoveDown << false << 6;
0188     QTest::addRow("up  ")  << SvgTextCursor::MoveUp << false  << 4;
0189     QTest::addRow("left")  << SvgTextCursor::MoveLeft << false  << 0;
0190     QTest::addRow("right") << SvgTextCursor::MoveRight << false  << 12;
0191     QTest::addRow("word left")  << SvgTextCursor::MoveWordLeft << false  << 0;
0192     QTest::addRow("word right") << SvgTextCursor::MoveWordRight << false  << 12;
0193     QTest::addRow("line end")   << SvgTextCursor::MoveLineEnd << false  << 6;
0194     QTest::addRow("line start") << SvgTextCursor::MoveLineStart << false  << 0;
0195     QTest::addRow("paragraph end")   << SvgTextCursor::ParagraphEnd << false  << 36;
0196     QTest::addRow("paragraph start") << SvgTextCursor::ParagraphStart << false  << 0;
0197     QTest::addRow("visual - down")  << SvgTextCursor::MoveDown << true << 6;
0198     QTest::addRow("visual - up  ")  << SvgTextCursor::MoveUp << true  << 4;
0199     QTest::addRow("visual - left")  << SvgTextCursor::MoveLeft << true  << 0;
0200     QTest::addRow("visual - right") << SvgTextCursor::MoveRight << true  << 12;
0201     QTest::addRow("visual - word left")  << SvgTextCursor::MoveWordLeft << true  << 0;
0202     QTest::addRow("visual - word right") << SvgTextCursor::MoveWordRight << true  << 12;
0203     QTest::addRow("visual - line end")   << SvgTextCursor::MoveLineEnd << true  << 6;
0204     QTest::addRow("visual - line start") << SvgTextCursor::MoveLineStart << true  << 0;
0205     QTest::addRow("visual - paragraph end")   << SvgTextCursor::ParagraphEnd << true  << 36;
0206     QTest::addRow("visual - paragraph start") << SvgTextCursor::ParagraphStart << true  << 0;
0207 }
0208 
0209 void SvgTextCursorTest::test_ttb_lr()
0210 {
0211     KoSvgTextShape *textShape = new KoSvgTextShape();
0212     QString ref ("<text style=\"inline-size:50.0; font-size:10.0; writing-mode:vertical-lr; font-family:Deja Vu Sans\">A B C D E F G H I J K L M N O P</text>");
0213     KoSvgTextShapeMarkupConverter converter(textShape);
0214     converter.convertFromSvg(ref, QString(), QRectF(0, 0, 300, 300), 72.0);
0215 
0216     MockCanvas canvas;
0217     SvgTextCursor cursor(&canvas);
0218     cursor.setShape(textShape);
0219 
0220     QFETCH(SvgTextCursor::MoveMode, mode);
0221     QFETCH(bool, visual);
0222     QFETCH(int, result);
0223     cursor.setVisualMode(visual);
0224 
0225     //The current textcursor sets the pos to the text end...
0226     //QCOMPARE(cursor.getPos(), 0);
0227     cursor.setPos(5, 5);
0228 
0229     cursor.moveCursor(mode);
0230     QCOMPARE(cursor.getPos(), result);
0231 }
0232 
0233 // Test basic text insertion in a horizontal ltr wrapped text;
0234 void SvgTextCursorTest::test_text_insert_command()
0235 {
0236     KoSvgTextShape *textShape = new KoSvgTextShape();
0237     QString ref ("<text style=\"inline-size:50.0; font-size:10.0;font-family:Deja Vu Sans\">The quick brown fox jumps over the lazy dog.</text>");
0238     KoSvgTextShapeMarkupConverter converter(textShape);
0239     converter.convertFromSvg(ref, QString(), QRectF(0, 0, 300, 300), 72.0);
0240 
0241     int pos = textShape->posForIndex(25, false, true);
0242     SvgTextInsertCommand *cmd = new SvgTextInsertCommand(textShape, pos, pos, " badly");
0243     QString test = "The quick brown fox jumps over the lazy dog.";
0244     QString test2 = test;
0245     test.insert(25, " badly");
0246     cmd->redo();
0247     QCOMPARE(test, textShape->plainText());
0248 
0249     cmd->undo();
0250     QCOMPARE(test2, textShape->plainText());
0251 }
0252 
0253 // Test basic text removal in a horizontal ltr wrapped text;
0254 void SvgTextCursorTest::test_text_remove_command()
0255 {
0256     KoSvgTextShape *textShape = new KoSvgTextShape();
0257     QString ref ("<text style=\"inline-size:50.0; font-size:10.0;font-family:Deja Vu Sans\">The quick brown fox jumps over the lazy dog.</text>");
0258     KoSvgTextShapeMarkupConverter converter(textShape);
0259     converter.convertFromSvg(ref, QString(), QRectF(0, 0, 300, 300), 72.0);
0260     QString test = "The quick brown fox jumps over the lazy dog.";
0261     QString test2 = test;
0262 
0263     SvgTextRemoveCommand *cmd = new SvgTextRemoveCommand(textShape, 15, 10, 10, 5);
0264     test.remove(10, 5);
0265 
0266     cmd->redo();
0267     QCOMPARE(test, textShape->plainText());
0268 
0269     cmd->undo();
0270     QCOMPARE(test2, textShape->plainText());
0271 }
0272 
0273 void SvgTextCursorTest::test_text_remove_dedicated_data()
0274 {
0275     QTest::addColumn<int>("pos");
0276     QTest::addColumn<SvgTextCursor::MoveMode>("mode1");
0277     QTest::addColumn<SvgTextCursor::MoveMode>("mode2");
0278     QTest::addColumn<int>("length");
0279 
0280     QTest::addRow("backspace") << 11 << SvgTextCursor::MovePreviousChar << SvgTextCursor::MoveNone << 1;
0281     QTest::addRow("delete") << 10 << SvgTextCursor::MoveNone << SvgTextCursor::MoveNextChar << 1;
0282     QTest::addRow("start-of-word") << 5 << SvgTextCursor::MoveWordStart << SvgTextCursor::MoveNone << 1;
0283     QTest::addRow("end-of-word") << 5 << SvgTextCursor::MoveNone << SvgTextCursor::MoveWordEnd << 4;
0284     QTest::addRow("end-of-line") << 5 << SvgTextCursor::MoveNone << SvgTextCursor::MoveLineEnd << 5;
0285     QTest::addRow("delete-line") << 5 << SvgTextCursor::MoveLineStart << SvgTextCursor::MoveLineEnd << 10;
0286 }
0287 
0288 void SvgTextCursorTest::test_text_remove_dedicated()
0289 {
0290     KoSvgTextShape *textShape = new KoSvgTextShape();
0291     QString ref ("<text style=\"inline-size:50.0; font-size:10.0;font-family:Deja Vu Sans\">The quick brown fox jumps over the lazy dog.</text>");
0292     KoSvgTextShapeMarkupConverter converter(textShape);
0293     converter.convertFromSvg(ref, QString(), QRectF(0, 0, 300, 300), 72.0);
0294 
0295     QFETCH(int, pos);
0296     QFETCH(SvgTextCursor::MoveMode, mode1);
0297     QFETCH(SvgTextCursor::MoveMode, mode2);
0298     QFETCH(int, length);
0299 
0300     MockCanvas canvas;
0301     SvgTextCursor cursor(&canvas);
0302     cursor.setShape(textShape);
0303     cursor.setPos(pos, pos);
0304     cursor.moveCursor(mode1);
0305     int posA = textShape->indexForPos(cursor.getPos());
0306     cursor.setPos(pos, pos);
0307     cursor.moveCursor(mode2);
0308     int posB = textShape->indexForPos(cursor.getPos());
0309     int posStart = qMin(posA, posB);
0310     int posEnd = qMax(posA, posB);
0311 
0312     QCOMPARE(posEnd - posStart, length);
0313 
0314 }
0315 
0316 SIMPLE_TEST_MAIN(SvgTextCursorTest)