File indexing completed on 2024-05-12 04:33:26

0001 /*
0002     SPDX-FileCopyrightText: 2013 Jon Mease <jon.mease@gmail.com>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include <QTest>
0008 
0009 #include "../settings_core.h"
0010 #include "core/annotations.h"
0011 #include "core/document.h"
0012 #include <QMimeDatabase>
0013 #include <QMimeType>
0014 
0015 class MockEditor;
0016 
0017 class EditAnnotationContentsTest : public QObject
0018 {
0019     Q_OBJECT
0020 
0021 private Q_SLOTS:
0022     void initTestCase();
0023     void cleanupTestCase();
0024     void init();
0025     void cleanup();
0026     void testConsecutiveCharBackspacesMerged();
0027     void testConsecutiveNewlineBackspacesNotMerged();
0028     void testConsecutiveCharInsertionsMerged();
0029     void testConsecutiveNewlineInsertionsNotMerged();
0030     void testConsecutiveCharDeletesMerged();
0031     void testConsecutiveNewlineDeletesNotMerged();
0032     void testConsecutiveEditsNotMergedAcrossDifferentAnnotations();
0033     void testInsertWithSelection();
0034     void testCombinations();
0035 
0036 private:
0037     Okular::Document *m_document;
0038     Okular::TextAnnotation *m_annot1;
0039     Okular::TextAnnotation *m_annot2;
0040     MockEditor *m_editor1;
0041     MockEditor *m_editor2;
0042 };
0043 
0044 /*
0045  * Simple class that receives the Document::annotationContentsChangedByUndoRedo
0046  * signal that would normally be directed to an annotation's
0047  * contents editor (For example AnnotWindow)
0048  */
0049 class MockEditor : public QObject
0050 {
0051     Q_OBJECT
0052 
0053 public:
0054     MockEditor(Okular::Annotation *annot, Okular::Document *doc);
0055     QString contents()
0056     {
0057         return m_contents;
0058     }
0059     int cursorPos()
0060     {
0061         return m_cursorPos;
0062     }
0063     int anchorPos()
0064     {
0065         return m_anchorPos;
0066     }
0067 
0068 private Q_SLOTS:
0069     void slotAnnotationContentsChangedByUndoRedo(Okular::Annotation *annotation, const QString &contents, int cursorPos, int anchorPos);
0070 
0071 private:
0072     Okular::Document *m_document;
0073     Okular::Annotation *m_annot;
0074 
0075     QString m_contents;
0076     int m_cursorPos;
0077     int m_anchorPos;
0078 };
0079 
0080 MockEditor::MockEditor(Okular::Annotation *annot, Okular::Document *doc)
0081 {
0082     m_annot = annot;
0083     m_document = doc;
0084     connect(m_document, &Okular::Document::annotationContentsChangedByUndoRedo, this, &MockEditor::slotAnnotationContentsChangedByUndoRedo);
0085     m_cursorPos = 0;
0086     m_anchorPos = 0;
0087     m_contents = annot->contents();
0088 }
0089 
0090 void MockEditor::slotAnnotationContentsChangedByUndoRedo(Okular::Annotation *annotation, const QString &contents, int cursorPos, int anchorPos)
0091 {
0092     if (annotation == m_annot) {
0093         m_contents = contents;
0094         m_cursorPos = cursorPos;
0095         m_anchorPos = anchorPos;
0096     }
0097 }
0098 
0099 void EditAnnotationContentsTest::initTestCase()
0100 {
0101     Okular::SettingsCore::instance(QStringLiteral("editannotationcontentstest"));
0102     m_document = new Okular::Document(nullptr);
0103 }
0104 
0105 void EditAnnotationContentsTest::cleanupTestCase()
0106 {
0107     delete m_document;
0108 }
0109 
0110 void EditAnnotationContentsTest::init()
0111 {
0112     const QString testFile = QStringLiteral(KDESRCDIR "data/file1.pdf");
0113     QMimeDatabase db;
0114     const QMimeType mime = db.mimeTypeForFile(testFile);
0115     QCOMPARE(m_document->openDocument(testFile, QUrl(), mime), Okular::Document::OpenSuccess);
0116 
0117     // Undo and Redo should be unavailable when docuemnt is first opened.
0118     QVERIFY(!m_document->canUndo());
0119     QVERIFY(!m_document->canRedo());
0120 
0121     // Create two distinct text annotations
0122     m_annot1 = new Okular::TextAnnotation();
0123     m_annot1->setBoundingRectangle(Okular::NormalizedRect(0.1, 0.1, 0.15, 0.15));
0124     m_annot1->setContents(QStringLiteral("Hello, World"));
0125     m_document->addPageAnnotation(0, m_annot1);
0126 
0127     m_annot2 = new Okular::TextAnnotation();
0128     m_annot2->setBoundingRectangle(Okular::NormalizedRect(0.1, 0.1, 0.15, 0.15));
0129     m_annot2->setContents(QStringLiteral("Hello, World"));
0130     m_document->addPageAnnotation(0, m_annot2);
0131 
0132     // setup editors
0133     m_editor1 = new MockEditor(m_annot1, m_document);
0134     m_editor2 = new MockEditor(m_annot2, m_document);
0135 }
0136 
0137 void EditAnnotationContentsTest::cleanup()
0138 {
0139     m_document->closeDocument();
0140     delete m_editor1;
0141     delete m_editor2;
0142     // m_annot1 and m_annot2 are deleted when document is closed
0143 }
0144 
0145 void EditAnnotationContentsTest::testConsecutiveCharBackspacesMerged()
0146 {
0147     // Hello, World| -> Hello, Worl|
0148     m_document->editPageAnnotationContents(0, m_annot1, QStringLiteral("Hello, Worl"), 11, 12, 12);
0149     QCOMPARE(QStringLiteral("Hello, Worl"), m_annot1->contents());
0150 
0151     // Hello, Worl| -> Hello, Wor|
0152     m_document->editPageAnnotationContents(0, m_annot1, QStringLiteral("Hello, Wor"), 10, 11, 11);
0153     QCOMPARE(QStringLiteral("Hello, Wor"), m_annot1->contents());
0154 
0155     // undo and verify that consecutive backspace operations are merged together
0156     m_document->undo();
0157     QCOMPARE(QStringLiteral("Hello, World"), m_annot1->contents());
0158     QCOMPARE(QStringLiteral("Hello, World"), m_editor1->contents());
0159     QCOMPARE(12, m_editor1->cursorPos());
0160     QCOMPARE(12, m_editor1->anchorPos());
0161 
0162     m_document->redo();
0163     QCOMPARE(QStringLiteral("Hello, Wor"), m_annot1->contents());
0164     QCOMPARE(QStringLiteral("Hello, Wor"), m_editor1->contents());
0165     QCOMPARE(10, m_editor1->cursorPos());
0166     QCOMPARE(10, m_editor1->anchorPos());
0167 
0168     // Hello, Wor| -> Hello, Wo|
0169     m_document->editPageAnnotationContents(0, m_annot1, QStringLiteral("Hello, Wo"), 9, 10, 10);
0170     QCOMPARE(QStringLiteral("Hello, Wo"), m_annot1->contents());
0171 
0172     // Hello, Wo| -> Hello, W|
0173     m_document->editPageAnnotationContents(0, m_annot1, QStringLiteral("Hello, W"), 8, 9, 9);
0174     QCOMPARE(QStringLiteral("Hello, W"), m_annot1->contents());
0175 
0176     // Hello, W| -> Hello, |
0177     m_document->editPageAnnotationContents(0, m_annot1, QStringLiteral("Hello, "), 7, 8, 8);
0178     QCOMPARE(QStringLiteral("Hello, "), m_annot1->contents());
0179 
0180     // undo and verify that consecutive backspace operations are merged together
0181     m_document->undo();
0182     QCOMPARE(QStringLiteral("Hello, World"), m_annot1->contents());
0183     QCOMPARE(QStringLiteral("Hello, World"), m_editor1->contents());
0184     QCOMPARE(12, m_editor1->cursorPos());
0185     QCOMPARE(12, m_editor1->anchorPos());
0186 
0187     m_document->redo();
0188     QCOMPARE(QStringLiteral("Hello, "), m_annot1->contents());
0189     QCOMPARE(QStringLiteral("Hello, "), m_editor1->contents());
0190     QCOMPARE(7, m_editor1->cursorPos());
0191     QCOMPARE(7, m_editor1->anchorPos());
0192 }
0193 
0194 void EditAnnotationContentsTest::testConsecutiveNewlineBackspacesNotMerged()
0195 {
0196     // Set contents to Hello, \n\n|World
0197     m_document->editPageAnnotationContents(0, m_annot1, QStringLiteral("Hello, \n\nWorld"), 0, 0, 0);
0198     QCOMPARE(QStringLiteral("Hello, \n\nWorld"), m_annot1->contents());
0199 
0200     // Hello, \n\n|World -> Hello, \n|World
0201     m_document->editPageAnnotationContents(0, m_annot1, QStringLiteral("Hello, \nWorld"), 8, 9, 9);
0202     QCOMPARE(QStringLiteral("Hello, \nWorld"), m_annot1->contents());
0203 
0204     // Hello, \n|World -> Hello, |World
0205     m_document->editPageAnnotationContents(0, m_annot1, QStringLiteral("Hello, World"), 7, 8, 8);
0206     QCOMPARE(QStringLiteral("Hello, World"), m_annot1->contents());
0207 
0208     // Hello, |World -> Hello,|World
0209     m_document->editPageAnnotationContents(0, m_annot1, QStringLiteral("Hello,World"), 6, 7, 7);
0210     QCOMPARE(QStringLiteral("Hello,World"), m_annot1->contents());
0211 
0212     // Hello,|World -> Hello|World
0213     m_document->editPageAnnotationContents(0, m_annot1, QStringLiteral("HelloWorld"), 5, 6, 6);
0214     QCOMPARE(QStringLiteral("HelloWorld"), m_annot1->contents());
0215 
0216     // Backspace operations of non-newline characters should be merged
0217     m_document->undo();
0218     QCOMPARE(QStringLiteral("Hello, World"), m_annot1->contents());
0219     QCOMPARE(QStringLiteral("Hello, World"), m_editor1->contents());
0220     QCOMPARE(7, m_editor1->cursorPos());
0221     QCOMPARE(7, m_editor1->anchorPos());
0222 
0223     // Backspace operations on newline characters should not be merged
0224     m_document->undo();
0225     QCOMPARE(QStringLiteral("Hello, \nWorld"), m_annot1->contents());
0226     QCOMPARE(QStringLiteral("Hello, \nWorld"), m_editor1->contents());
0227     QCOMPARE(8, m_editor1->cursorPos());
0228     QCOMPARE(8, m_editor1->anchorPos());
0229 
0230     m_document->undo();
0231     QCOMPARE(QStringLiteral("Hello, \n\nWorld"), m_annot1->contents());
0232     QCOMPARE(QStringLiteral("Hello, \n\nWorld"), m_editor1->contents());
0233     QCOMPARE(9, m_editor1->cursorPos());
0234     QCOMPARE(9, m_editor1->anchorPos());
0235 }
0236 
0237 void EditAnnotationContentsTest::testConsecutiveCharInsertionsMerged()
0238 {
0239     // Hello, |World -> Hello, B|World
0240     m_document->editPageAnnotationContents(0, m_annot1, QStringLiteral("Hello, BWorld"), 8, 7, 7);
0241     QCOMPARE(QStringLiteral("Hello, BWorld"), m_annot1->contents());
0242 
0243     // Hello, l| -> Hello, li|
0244     m_document->editPageAnnotationContents(0, m_annot1, QStringLiteral("Hello, BiWorld"), 9, 8, 8);
0245     QCOMPARE(QStringLiteral("Hello, BiWorld"), m_annot1->contents());
0246 
0247     // Hello, li| -> Hello, lin|
0248     m_document->editPageAnnotationContents(0, m_annot1, QStringLiteral("Hello, BigWorld"), 10, 9, 9);
0249     QCOMPARE(QStringLiteral("Hello, BigWorld"), m_annot1->contents());
0250 
0251     // Hello, lin| -> Hello, line|
0252     m_document->editPageAnnotationContents(0, m_annot1, QStringLiteral("Hello, Big World"), 11, 10, 10);
0253     QCOMPARE(QStringLiteral("Hello, Big World"), m_annot1->contents());
0254 
0255     // Verify undo/redo operations merged
0256     m_document->undo();
0257     QCOMPARE(QStringLiteral("Hello, World"), m_annot1->contents());
0258     QCOMPARE(QStringLiteral("Hello, World"), m_editor1->contents());
0259     QCOMPARE(7, m_editor1->cursorPos());
0260     QCOMPARE(7, m_editor1->anchorPos());
0261 
0262     m_document->redo();
0263     QCOMPARE(QStringLiteral("Hello, Big World"), m_annot1->contents());
0264     QCOMPARE(QStringLiteral("Hello, Big World"), m_editor1->contents());
0265     QCOMPARE(11, m_editor1->cursorPos());
0266     QCOMPARE(11, m_editor1->anchorPos());
0267 }
0268 
0269 void EditAnnotationContentsTest::testConsecutiveNewlineInsertionsNotMerged()
0270 {
0271     // Hello, |World -> Hello, \n|World
0272     m_document->editPageAnnotationContents(0, m_annot1, QStringLiteral("Hello, \nWorld"), 8, 7, 7);
0273     QCOMPARE(QStringLiteral("Hello, \nWorld"), m_annot1->contents());
0274 
0275     // Hello, |World -> Hello, \n|World
0276     m_document->editPageAnnotationContents(0, m_annot1, QStringLiteral("Hello, \n\nWorld"), 9, 8, 8);
0277     QCOMPARE(QStringLiteral("Hello, \n\nWorld"), m_annot1->contents());
0278 
0279     m_document->undo();
0280     QCOMPARE(QStringLiteral("Hello, \nWorld"), m_annot1->contents());
0281     QCOMPARE(QStringLiteral("Hello, \nWorld"), m_editor1->contents());
0282     QCOMPARE(8, m_editor1->cursorPos());
0283     QCOMPARE(8, m_editor1->anchorPos());
0284 
0285     m_document->undo();
0286     QCOMPARE(QStringLiteral("Hello, World"), m_annot1->contents());
0287     QCOMPARE(QStringLiteral("Hello, World"), m_editor1->contents());
0288     QCOMPARE(7, m_editor1->cursorPos());
0289     QCOMPARE(7, m_editor1->anchorPos());
0290 
0291     m_document->redo();
0292     QCOMPARE(QStringLiteral("Hello, \nWorld"), m_annot1->contents());
0293     QCOMPARE(QStringLiteral("Hello, \nWorld"), m_editor1->contents());
0294     QCOMPARE(8, m_editor1->cursorPos());
0295     QCOMPARE(8, m_editor1->anchorPos());
0296 
0297     m_document->redo();
0298     QCOMPARE(QStringLiteral("Hello, \n\nWorld"), m_annot1->contents());
0299     QCOMPARE(QStringLiteral("Hello, \n\nWorld"), m_editor1->contents());
0300     QCOMPARE(9, m_editor1->cursorPos());
0301     QCOMPARE(9, m_editor1->anchorPos());
0302 }
0303 
0304 void EditAnnotationContentsTest::testConsecutiveCharDeletesMerged()
0305 {
0306     // Hello, |World -> Hello, |orld
0307     m_document->editPageAnnotationContents(0, m_annot1, QStringLiteral("Hello, orld"), 7, 7, 7);
0308     QCOMPARE(QStringLiteral("Hello, orld"), m_annot1->contents());
0309 
0310     // Hello, |orld -> Hello, |rld
0311     m_document->editPageAnnotationContents(0, m_annot1, QStringLiteral("Hello, rld"), 7, 7, 7);
0312     QCOMPARE(QStringLiteral("Hello, rld"), m_annot1->contents());
0313 
0314     // Hello, |rld -> Hello, |ld
0315     m_document->editPageAnnotationContents(0, m_annot1, QStringLiteral("Hello, ld"), 7, 7, 7);
0316     QCOMPARE(QStringLiteral("Hello, ld"), m_annot1->contents());
0317 
0318     // Hello, |ld -> Hello, |d
0319     m_document->editPageAnnotationContents(0, m_annot1, QStringLiteral("Hello, d"), 7, 7, 7);
0320     QCOMPARE(QStringLiteral("Hello, d"), m_annot1->contents());
0321 
0322     // Hello, | -> Hello, |
0323     m_document->editPageAnnotationContents(0, m_annot1, QStringLiteral("Hello, "), 7, 7, 7);
0324     QCOMPARE(QStringLiteral("Hello, "), m_annot1->contents());
0325 
0326     // Verify undo/redo operations merged
0327     m_document->undo();
0328     QCOMPARE(QStringLiteral("Hello, World"), m_annot1->contents());
0329     QCOMPARE(QStringLiteral("Hello, World"), m_editor1->contents());
0330     QCOMPARE(7, m_editor1->cursorPos());
0331     QCOMPARE(7, m_editor1->anchorPos());
0332 
0333     m_document->redo();
0334     QCOMPARE(QStringLiteral("Hello, "), m_annot1->contents());
0335     QCOMPARE(QStringLiteral("Hello, "), m_editor1->contents());
0336     QCOMPARE(7, m_editor1->cursorPos());
0337     QCOMPARE(7, m_editor1->anchorPos());
0338 }
0339 
0340 void EditAnnotationContentsTest::testConsecutiveNewlineDeletesNotMerged()
0341 {
0342     // Set contents to Hello, \n\n|World
0343     m_document->editPageAnnotationContents(0, m_annot1, QStringLiteral("Hello, \n\nWorld"), 0, 0, 0);
0344     QCOMPARE(QStringLiteral("Hello, \n\nWorld"), m_annot1->contents());
0345 
0346     // He|llo, \n\nWorld ->  He|lo, \n\nWorld
0347     m_document->editPageAnnotationContents(0, m_annot1, QStringLiteral("Helo, \n\nWorld"), 2, 2, 2);
0348     QCOMPARE(QStringLiteral("Helo, \n\nWorld"), m_annot1->contents());
0349 
0350     // He|lo, \n\nWorld ->  He|o, \n\nWorld
0351     m_document->editPageAnnotationContents(0, m_annot1, QStringLiteral("Heo, \n\nWorld"), 2, 2, 2);
0352     QCOMPARE(QStringLiteral("Heo, \n\nWorld"), m_annot1->contents());
0353 
0354     // He|o, \n\nWorld ->  He|, \n\nWorld
0355     m_document->editPageAnnotationContents(0, m_annot1, QStringLiteral("He, \n\nWorld"), 2, 2, 2);
0356     QCOMPARE(QStringLiteral("He, \n\nWorld"), m_annot1->contents());
0357 
0358     // He|, \n\nWorld ->  He| \n\nWorld
0359     m_document->editPageAnnotationContents(0, m_annot1, QStringLiteral("He \n\nWorld"), 2, 2, 2);
0360     QCOMPARE(QStringLiteral("He \n\nWorld"), m_annot1->contents());
0361 
0362     // He| \n\nWorld ->  He|\n\nWorld
0363     m_document->editPageAnnotationContents(0, m_annot1, QStringLiteral("He\n\nWorld"), 2, 2, 2);
0364     QCOMPARE(QStringLiteral("He\n\nWorld"), m_annot1->contents());
0365 
0366     // He|\n\nWorld ->  He|\nWorld
0367     m_document->editPageAnnotationContents(0, m_annot1, QStringLiteral("He\nWorld"), 2, 2, 2);
0368     QCOMPARE(QStringLiteral("He\nWorld"), m_annot1->contents());
0369 
0370     // He|\nWorld ->  He|World
0371     m_document->editPageAnnotationContents(0, m_annot1, QStringLiteral("HeWorld"), 2, 2, 2);
0372     QCOMPARE(QStringLiteral("HeWorld"), m_annot1->contents());
0373 
0374     // Verify that deletions of newlines are not merged, but deletions of other characters are
0375     m_document->undo();
0376     QCOMPARE(QStringLiteral("He\nWorld"), m_annot1->contents());
0377     QCOMPARE(QStringLiteral("He\nWorld"), m_editor1->contents());
0378     QCOMPARE(2, m_editor1->cursorPos());
0379     QCOMPARE(2, m_editor1->anchorPos());
0380 
0381     m_document->undo();
0382     QCOMPARE(QStringLiteral("He\n\nWorld"), m_annot1->contents());
0383     QCOMPARE(QStringLiteral("He\n\nWorld"), m_editor1->contents());
0384     QCOMPARE(2, m_editor1->cursorPos());
0385     QCOMPARE(2, m_editor1->anchorPos());
0386 
0387     m_document->undo();
0388     QCOMPARE(QStringLiteral("Hello, \n\nWorld"), m_annot1->contents());
0389     QCOMPARE(QStringLiteral("Hello, \n\nWorld"), m_editor1->contents());
0390     QCOMPARE(2, m_editor1->cursorPos());
0391     QCOMPARE(2, m_editor1->anchorPos());
0392 }
0393 
0394 void EditAnnotationContentsTest::testConsecutiveEditsNotMergedAcrossDifferentAnnotations()
0395 {
0396     // Annot1: Hello, World| -> Hello, Worl|
0397     m_document->editPageAnnotationContents(0, m_annot1, QStringLiteral("Hello, Worl"), 11, 12, 12);
0398     QCOMPARE(QStringLiteral("Hello, Worl"), m_annot1->contents());
0399     // Annot1: Hello, Worl| -> Hello, Wor|
0400     m_document->editPageAnnotationContents(0, m_annot1, QStringLiteral("Hello, Wor"), 10, 11, 11);
0401     QCOMPARE(QStringLiteral("Hello, Wor"), m_annot1->contents());
0402 
0403     // Annot2: Hello, World| -> Hello, Worl|
0404     m_document->editPageAnnotationContents(0, m_annot2, QStringLiteral("Hello, Worl"), 11, 12, 12);
0405     QCOMPARE(QStringLiteral("Hello, Worl"), m_annot2->contents());
0406     // Annot2: Hello, Worl| -> Hello, Wor|
0407     m_document->editPageAnnotationContents(0, m_annot2, QStringLiteral("Hello, Wor"), 10, 11, 11);
0408     QCOMPARE(QStringLiteral("Hello, Wor"), m_annot2->contents());
0409 
0410     // Annot1: Hello, Wor| -> Hello, Wo|
0411     m_document->editPageAnnotationContents(0, m_annot1, QStringLiteral("Hello, Wo"), 9, 10, 10);
0412     QCOMPARE(QStringLiteral("Hello, Wo"), m_annot1->contents());
0413     // Annot1: Hello, Wo| -> Hello, W|
0414     m_document->editPageAnnotationContents(0, m_annot1, QStringLiteral("Hello, W"), 8, 9, 9);
0415     QCOMPARE(QStringLiteral("Hello, W"), m_annot1->contents());
0416     // Annot1: Hello, W| -> Hello, |
0417     m_document->editPageAnnotationContents(0, m_annot1, QStringLiteral("Hello, "), 7, 8, 8);
0418     QCOMPARE(QStringLiteral("Hello, "), m_annot1->contents());
0419 
0420     // Annot2: Hello, Wor| -> Hello, Wo|
0421     m_document->editPageAnnotationContents(0, m_annot2, QStringLiteral("Hello, Wo"), 9, 10, 10);
0422     QCOMPARE(QStringLiteral("Hello, Wo"), m_annot2->contents());
0423     // Annot2: Hello, Wo| -> Hello, W|
0424     m_document->editPageAnnotationContents(0, m_annot2, QStringLiteral("Hello, W"), 8, 9, 9);
0425     QCOMPARE(QStringLiteral("Hello, W"), m_annot2->contents());
0426     // Annot2: Hello, W| -> Hello, |
0427     m_document->editPageAnnotationContents(0, m_annot2, QStringLiteral("Hello, "), 7, 8, 8);
0428     QCOMPARE(QStringLiteral("Hello, "), m_annot2->contents());
0429 
0430     // undo and verify that consecutive backspace operations are merged together
0431     // m_annot2 -> "Hello, Wor|"
0432     m_document->undo();
0433     QCOMPARE(QStringLiteral("Hello, Wor"), m_annot2->contents());
0434     QCOMPARE(QStringLiteral("Hello, "), m_editor1->contents());
0435     QCOMPARE(QStringLiteral("Hello, Wor"), m_editor2->contents());
0436     QCOMPARE(10, m_editor2->cursorPos());
0437     QCOMPARE(10, m_editor2->anchorPos());
0438 
0439     // m_annot1 -> "Hello, Wor|"
0440     m_document->undo();
0441     QCOMPARE(QStringLiteral("Hello, Wor"), m_annot1->contents());
0442     QCOMPARE(QStringLiteral("Hello, Wor"), m_editor1->contents());
0443     QCOMPARE(QStringLiteral("Hello, Wor"), m_editor2->contents());
0444     QCOMPARE(10, m_editor1->cursorPos());
0445     QCOMPARE(10, m_editor1->anchorPos());
0446 
0447     // m_annot2 -> "Hello, World|"
0448     m_document->undo();
0449     QCOMPARE(QStringLiteral("Hello, World"), m_annot2->contents());
0450     QCOMPARE(QStringLiteral("Hello, Wor"), m_editor1->contents());
0451     QCOMPARE(QStringLiteral("Hello, World"), m_editor2->contents());
0452     QCOMPARE(12, m_editor2->cursorPos());
0453     QCOMPARE(12, m_editor2->anchorPos());
0454 
0455     // m_annot1 -> "Hello, World|"
0456     m_document->undo();
0457     QCOMPARE(QStringLiteral("Hello, World"), m_annot1->contents());
0458     QCOMPARE(QStringLiteral("Hello, World"), m_editor1->contents());
0459     QCOMPARE(QStringLiteral("Hello, World"), m_editor2->contents());
0460     QCOMPARE(12, m_editor1->cursorPos());
0461     QCOMPARE(12, m_editor1->anchorPos());
0462 }
0463 
0464 void EditAnnotationContentsTest::testInsertWithSelection()
0465 {
0466     // Annot1: |Hello|, World -> H|, World
0467     m_document->editPageAnnotationContents(0, m_annot1, QStringLiteral("H, World"), 1, 0, 5);
0468     QCOMPARE(QStringLiteral("H, World"), m_annot1->contents());
0469 
0470     // Annot1: H|, World -> Hi|, World
0471     m_document->editPageAnnotationContents(0, m_annot1, QStringLiteral("Hi, World"), 2, 1, 1);
0472     QCOMPARE(QStringLiteral("Hi, World"), m_annot1->contents());
0473 
0474     m_document->undo();
0475     QCOMPARE(QStringLiteral("H, World"), m_annot1->contents());
0476     QCOMPARE(QStringLiteral("H, World"), m_editor1->contents());
0477     QCOMPARE(1, m_editor1->cursorPos());
0478     QCOMPARE(1, m_editor1->anchorPos());
0479 
0480     m_document->undo();
0481     QCOMPARE(QStringLiteral("Hello, World"), m_annot1->contents());
0482     QCOMPARE(QStringLiteral("Hello, World"), m_editor1->contents());
0483     QCOMPARE(0, m_editor1->cursorPos());
0484     QCOMPARE(5, m_editor1->anchorPos());
0485 }
0486 
0487 void EditAnnotationContentsTest::testCombinations()
0488 {
0489     // Annot1: Hello, World| -> Hello, Worl|
0490     m_document->editPageAnnotationContents(0, m_annot1, QStringLiteral("Hello, Worl"), 11, 12, 12);
0491     QCOMPARE(QStringLiteral("Hello, Worl"), m_annot1->contents());
0492 
0493     // Annot1: Hello, Worl| -> Hello, Wor|
0494     m_document->editPageAnnotationContents(0, m_annot1, QStringLiteral("Hello, Wor"), 10, 11, 11);
0495     QCOMPARE(QStringLiteral("Hello, Wor"), m_annot1->contents());
0496 
0497     // Annot1: |He|llo, Wor -> |llo, Wor
0498     m_document->editPageAnnotationContents(0, m_annot1, QStringLiteral("llo, Wor"), 0, 2, 0);
0499     QCOMPARE(QStringLiteral("llo, Wor"), m_annot1->contents());
0500 
0501     // Annot1: |llo, Wor -> |lo, Wor
0502     m_document->editPageAnnotationContents(0, m_annot1, QStringLiteral("lo, Wor"), 0, 0, 0);
0503     QCOMPARE(QStringLiteral("lo, Wor"), m_annot1->contents());
0504 
0505     m_document->undo();
0506     QCOMPARE(QStringLiteral("llo, Wor"), m_annot1->contents());
0507     QCOMPARE(QStringLiteral("llo, Wor"), m_editor1->contents());
0508     QCOMPARE(0, m_editor1->cursorPos());
0509     QCOMPARE(0, m_editor1->anchorPos());
0510 
0511     m_document->undo();
0512     QCOMPARE(QStringLiteral("Hello, Wor"), m_annot1->contents());
0513     QCOMPARE(QStringLiteral("Hello, Wor"), m_editor1->contents());
0514     QCOMPARE(2, m_editor1->cursorPos());
0515     QCOMPARE(0, m_editor1->anchorPos());
0516 
0517     m_document->undo();
0518     QCOMPARE(QStringLiteral("Hello, World"), m_annot1->contents());
0519     QCOMPARE(QStringLiteral("Hello, World"), m_editor1->contents());
0520     QCOMPARE(12, m_editor1->cursorPos());
0521     QCOMPARE(12, m_editor1->anchorPos());
0522 }
0523 
0524 QTEST_MAIN(EditAnnotationContentsTest)
0525 #include "editannotationcontentstest.moc"