File indexing completed on 2024-10-06 12:24:12
0001 /* 0002 This file is part of the KDE libraries 0003 SPDX-FileCopyrightText: 2011-2018 Dominik Haumann <dhaumann@kde.org> 0004 0005 SPDX-License-Identifier: LGPL-2.0-or-later 0006 */ 0007 0008 #include "modificationsystem_test.h" 0009 #include "moc_modificationsystem_test.cpp" 0010 0011 #include <katedocument.h> 0012 #include <kateglobal.h> 0013 #include <kateundomanager.h> 0014 0015 #include <QtTestWidgets> 0016 0017 QTEST_MAIN(ModificationSystemTest) 0018 0019 using namespace KTextEditor; 0020 0021 void ModificationSystemTest::initTestCase() 0022 { 0023 KTextEditor::EditorPrivate::enableUnitTestMode(); 0024 } 0025 0026 void ModificationSystemTest::cleanupTestCase() 0027 { 0028 } 0029 0030 static void clearModificationFlags(KTextEditor::DocumentPrivate *doc) 0031 { 0032 for (int i = 0; i < doc->lines(); ++i) { 0033 Kate::TextLine line = doc->plainKateTextLine(i); 0034 line->markAsModified(false); 0035 line->markAsSavedOnDisk(false); 0036 } 0037 } 0038 0039 static void markModifiedLinesAsSaved(KTextEditor::DocumentPrivate *doc) 0040 { 0041 for (int i = 0; i < doc->lines(); ++i) { 0042 Kate::TextLine textLine = doc->plainKateTextLine(i); 0043 if (textLine->markedAsModified()) { 0044 textLine->markAsSavedOnDisk(true); 0045 } 0046 } 0047 } 0048 0049 void ModificationSystemTest::testInsertText() 0050 { 0051 KTextEditor::DocumentPrivate doc; 0052 0053 const QString content("first line\n"); 0054 doc.setText(content); 0055 0056 // now all lines should have state "Modified" 0057 QVERIFY(doc.isLineModified(0)); 0058 QVERIFY(!doc.isLineSaved(0)); 0059 0060 // clear all modification flags, forces no flags 0061 doc.setModified(false); 0062 doc.undoManager()->updateLineModifications(); 0063 clearModificationFlags(&doc); 0064 0065 QVERIFY(!doc.isLineModified(0)); 0066 QVERIFY(!doc.isLineSaved(0)); 0067 0068 // now we have an a unmodified file, start real tests 0069 // insert text in line 0, then undo and redo 0070 doc.insertText(Cursor(0, 2), "_"); 0071 QVERIFY(doc.isLineModified(0)); 0072 QVERIFY(!doc.isLineSaved(0)); 0073 0074 doc.undo(); 0075 QVERIFY(!doc.isLineModified(0)); 0076 QVERIFY(doc.isLineSaved(0)); 0077 0078 doc.redo(); 0079 QVERIFY(doc.isLineModified(0)); 0080 QVERIFY(!doc.isLineSaved(0)); 0081 0082 // 0083 // now simulate "save", then do the undo/redo tests again 0084 // 0085 doc.setModified(false); 0086 markModifiedLinesAsSaved(&doc); 0087 doc.undoManager()->updateLineModifications(); 0088 0089 // now no line should have state "Modified" 0090 QVERIFY(!doc.isLineModified(0)); 0091 QVERIFY(doc.isLineSaved(0)); 0092 0093 // undo the text insertion 0094 doc.undo(); 0095 QVERIFY(doc.isLineModified(0)); 0096 QVERIFY(!doc.isLineSaved(0)); 0097 0098 doc.redo(); 0099 QVERIFY(!doc.isLineModified(0)); 0100 QVERIFY(doc.isLineSaved(0)); 0101 } 0102 0103 void ModificationSystemTest::testRemoveText() 0104 { 0105 KTextEditor::DocumentPrivate doc; 0106 0107 const QString content("first line\n"); 0108 doc.setText(content); 0109 0110 // now all lines should have state "Modified" 0111 QVERIFY(doc.isLineModified(0)); 0112 QVERIFY(!doc.isLineSaved(0)); 0113 0114 // clear all modification flags, forces no flags 0115 doc.setModified(false); 0116 doc.undoManager()->updateLineModifications(); 0117 clearModificationFlags(&doc); 0118 0119 QVERIFY(!doc.isLineModified(0)); 0120 QVERIFY(!doc.isLineSaved(0)); 0121 0122 // now we have an a unmodified file, start real tests 0123 // remove text in line 0, then undo and redo 0124 doc.removeText(Range(Cursor(0, 1), Cursor(0, 2))); 0125 QVERIFY(doc.isLineModified(0)); 0126 QVERIFY(!doc.isLineSaved(0)); 0127 0128 doc.undo(); 0129 QVERIFY(!doc.isLineModified(0)); 0130 QVERIFY(doc.isLineSaved(0)); 0131 0132 doc.redo(); 0133 QVERIFY(doc.isLineModified(0)); 0134 QVERIFY(!doc.isLineSaved(0)); 0135 0136 // 0137 // now simulate "save", then do the undo/redo tests again 0138 // 0139 doc.setModified(false); 0140 markModifiedLinesAsSaved(&doc); 0141 doc.undoManager()->updateLineModifications(); 0142 0143 // now no line should have state "Modified" 0144 QVERIFY(!doc.isLineModified(0)); 0145 QVERIFY(doc.isLineSaved(0)); 0146 0147 // undo the text insertion 0148 doc.undo(); 0149 QVERIFY(doc.isLineModified(0)); 0150 QVERIFY(!doc.isLineSaved(0)); 0151 0152 doc.redo(); 0153 QVERIFY(!doc.isLineModified(0)); 0154 QVERIFY(doc.isLineSaved(0)); 0155 } 0156 0157 void ModificationSystemTest::testInsertLine() 0158 { 0159 KTextEditor::DocumentPrivate doc; 0160 0161 const QString content( 0162 "0\n" 0163 "2"); 0164 doc.setText(content); 0165 0166 // clear all modification flags, forces no flags 0167 doc.setModified(false); 0168 doc.undoManager()->updateLineModifications(); 0169 clearModificationFlags(&doc); 0170 0171 // insert at line 1 0172 doc.insertLine(1, "1"); 0173 0174 QVERIFY(!doc.isLineModified(0)); 0175 QVERIFY(doc.isLineModified(1)); 0176 QVERIFY(!doc.isLineModified(2)); 0177 QVERIFY(!doc.isLineSaved(0)); 0178 QVERIFY(!doc.isLineSaved(1)); 0179 QVERIFY(!doc.isLineSaved(2)); 0180 0181 doc.undo(); 0182 QVERIFY(!doc.isLineModified(0)); 0183 QVERIFY(!doc.isLineModified(1)); 0184 QVERIFY(!doc.isLineSaved(0)); 0185 QVERIFY(!doc.isLineSaved(1)); 0186 0187 doc.redo(); 0188 QVERIFY(!doc.isLineModified(0)); 0189 QVERIFY(doc.isLineModified(1)); 0190 QVERIFY(!doc.isLineModified(2)); 0191 QVERIFY(!doc.isLineSaved(0)); 0192 QVERIFY(!doc.isLineSaved(1)); 0193 QVERIFY(!doc.isLineSaved(2)); 0194 0195 // 0196 // now simulate "save", then do the undo/redo tests again 0197 // 0198 doc.setModified(false); 0199 markModifiedLinesAsSaved(&doc); 0200 doc.undoManager()->updateLineModifications(); 0201 0202 // now no line should have state "Modified" 0203 QVERIFY(!doc.isLineModified(0)); 0204 QVERIFY(!doc.isLineModified(1)); 0205 QVERIFY(!doc.isLineModified(2)); 0206 QVERIFY(!doc.isLineSaved(0)); 0207 QVERIFY(doc.isLineSaved(1)); 0208 QVERIFY(!doc.isLineSaved(2)); 0209 0210 // undo the text insertion 0211 doc.undo(); 0212 QVERIFY(!doc.isLineModified(0)); 0213 QVERIFY(!doc.isLineModified(1)); 0214 QVERIFY(!doc.isLineSaved(0)); 0215 QVERIFY(!doc.isLineSaved(1)); 0216 0217 doc.redo(); 0218 QVERIFY(!doc.isLineModified(0)); 0219 QVERIFY(!doc.isLineModified(1)); 0220 QVERIFY(!doc.isLineModified(2)); 0221 QVERIFY(!doc.isLineSaved(0)); 0222 QVERIFY(doc.isLineSaved(1)); 0223 QVERIFY(!doc.isLineSaved(2)); 0224 } 0225 0226 void ModificationSystemTest::testRemoveLine() 0227 { 0228 KTextEditor::DocumentPrivate doc; 0229 0230 const QString content( 0231 "0\n" 0232 "1\n" 0233 "2"); 0234 doc.setText(content); 0235 0236 // clear all modification flags, forces no flags 0237 doc.setModified(false); 0238 doc.undoManager()->updateLineModifications(); 0239 clearModificationFlags(&doc); 0240 0241 // remove at line 1 0242 doc.removeLine(1); 0243 0244 QVERIFY(!doc.isLineModified(0)); 0245 QVERIFY(!doc.isLineModified(1)); 0246 QVERIFY(!doc.isLineSaved(0)); 0247 QVERIFY(!doc.isLineSaved(1)); 0248 0249 doc.undo(); 0250 QVERIFY(!doc.isLineModified(0)); 0251 QVERIFY(!doc.isLineModified(1)); 0252 QVERIFY(!doc.isLineModified(2)); 0253 QVERIFY(!doc.isLineSaved(0)); 0254 QVERIFY(doc.isLineSaved(1)); 0255 QVERIFY(!doc.isLineSaved(2)); 0256 0257 doc.redo(); 0258 QVERIFY(!doc.isLineModified(0)); 0259 QVERIFY(!doc.isLineModified(1)); 0260 QVERIFY(!doc.isLineSaved(0)); 0261 QVERIFY(!doc.isLineSaved(1)); 0262 0263 // 0264 // now simulate "save", then do the undo/redo tests again 0265 // 0266 doc.setModified(false); 0267 markModifiedLinesAsSaved(&doc); 0268 doc.undoManager()->updateLineModifications(); 0269 0270 // now no line should have state "Modified" 0271 QVERIFY(!doc.isLineModified(0)); 0272 QVERIFY(!doc.isLineModified(1)); 0273 QVERIFY(!doc.isLineSaved(0)); 0274 QVERIFY(!doc.isLineSaved(1)); 0275 0276 // undo the text insertion 0277 doc.undo(); 0278 QVERIFY(!doc.isLineModified(0)); 0279 QVERIFY(doc.isLineModified(1)); 0280 QVERIFY(!doc.isLineModified(2)); 0281 QVERIFY(!doc.isLineSaved(0)); 0282 QVERIFY(!doc.isLineSaved(1)); 0283 QVERIFY(!doc.isLineSaved(2)); 0284 0285 doc.redo(); 0286 QVERIFY(!doc.isLineModified(0)); 0287 QVERIFY(!doc.isLineModified(1)); 0288 QVERIFY(!doc.isLineSaved(0)); 0289 QVERIFY(!doc.isLineSaved(1)); 0290 } 0291 0292 void ModificationSystemTest::testWrapLineMid() 0293 { 0294 for (int i = 0; i < 2; ++i) { 0295 bool insertNewLine = (i == 1); 0296 KTextEditor::DocumentPrivate doc; 0297 0298 const QString content( 0299 "aaaa\n" 0300 "bbbb\n" 0301 "cccc"); 0302 doc.setText(content); 0303 0304 // clear all modification flags, forces no flags 0305 doc.setModified(false); 0306 doc.undoManager()->updateLineModifications(); 0307 clearModificationFlags(&doc); 0308 0309 // wrap line 1 at |: bb|bb 0310 doc.editWrapLine(1, 2, insertNewLine); 0311 0312 QVERIFY(!doc.isLineModified(0)); 0313 QVERIFY(doc.isLineModified(1)); 0314 QVERIFY(doc.isLineModified(2)); 0315 QVERIFY(!doc.isLineSaved(0)); 0316 QVERIFY(!doc.isLineSaved(1)); 0317 QVERIFY(!doc.isLineSaved(2)); 0318 0319 doc.undo(); 0320 QVERIFY(!doc.isLineModified(0)); 0321 QVERIFY(!doc.isLineModified(1)); 0322 QVERIFY(!doc.isLineSaved(0)); 0323 QVERIFY(doc.isLineSaved(1)); 0324 0325 doc.redo(); 0326 QVERIFY(!doc.isLineModified(0)); 0327 QVERIFY(doc.isLineModified(1)); 0328 QVERIFY(doc.isLineModified(2)); 0329 QVERIFY(!doc.isLineSaved(0)); 0330 QVERIFY(!doc.isLineSaved(1)); 0331 QVERIFY(!doc.isLineSaved(2)); 0332 0333 // 0334 // now simulate "save", then do the undo/redo tests again 0335 // 0336 doc.setModified(false); 0337 markModifiedLinesAsSaved(&doc); 0338 doc.undoManager()->updateLineModifications(); 0339 0340 // now no line should have state "Modified" 0341 QVERIFY(!doc.isLineModified(0)); 0342 QVERIFY(!doc.isLineModified(1)); 0343 QVERIFY(!doc.isLineModified(2)); 0344 QVERIFY(!doc.isLineSaved(0)); 0345 QVERIFY(doc.isLineSaved(1)); 0346 QVERIFY(doc.isLineSaved(2)); 0347 0348 // undo the text insertion 0349 doc.undo(); 0350 QVERIFY(!doc.isLineModified(0)); 0351 QVERIFY(doc.isLineModified(1)); 0352 QVERIFY(!doc.isLineSaved(0)); 0353 QVERIFY(!doc.isLineSaved(1)); 0354 0355 doc.redo(); 0356 QVERIFY(!doc.isLineModified(0)); 0357 QVERIFY(!doc.isLineModified(1)); 0358 QVERIFY(!doc.isLineModified(2)); 0359 QVERIFY(!doc.isLineSaved(0)); 0360 QVERIFY(doc.isLineSaved(1)); 0361 QVERIFY(doc.isLineSaved(2)); 0362 } 0363 } 0364 0365 void ModificationSystemTest::testWrapLineAtEnd() 0366 { 0367 KTextEditor::DocumentPrivate doc; 0368 0369 const QString content( 0370 "aaaa\n" 0371 "bbbb"); 0372 doc.setText(content); 0373 0374 // clear all modification flags, forces no flags 0375 doc.setModified(false); 0376 doc.undoManager()->updateLineModifications(); 0377 clearModificationFlags(&doc); 0378 0379 // wrap line 0 at end 0380 doc.editWrapLine(0, 4); 0381 0382 QVERIFY(!doc.isLineModified(0)); 0383 QVERIFY(doc.isLineModified(1)); 0384 QVERIFY(!doc.isLineModified(2)); 0385 QVERIFY(!doc.isLineSaved(0)); 0386 QVERIFY(!doc.isLineSaved(1)); 0387 QVERIFY(!doc.isLineSaved(2)); 0388 0389 doc.undo(); 0390 QVERIFY(!doc.isLineModified(0)); 0391 QVERIFY(!doc.isLineModified(1)); 0392 QVERIFY(!doc.isLineSaved(0)); 0393 QVERIFY(!doc.isLineSaved(1)); 0394 0395 doc.redo(); 0396 QVERIFY(!doc.isLineModified(0)); 0397 QVERIFY(doc.isLineModified(1)); 0398 QVERIFY(!doc.isLineModified(2)); 0399 QVERIFY(!doc.isLineSaved(0)); 0400 QVERIFY(!doc.isLineSaved(1)); 0401 QVERIFY(!doc.isLineSaved(2)); 0402 0403 // 0404 // now simulate "save", then do the undo/redo tests again 0405 // 0406 doc.setModified(false); 0407 markModifiedLinesAsSaved(&doc); 0408 doc.undoManager()->updateLineModifications(); 0409 0410 // now no line should have state "Modified" 0411 QVERIFY(!doc.isLineModified(0)); 0412 QVERIFY(!doc.isLineModified(1)); 0413 QVERIFY(!doc.isLineModified(2)); 0414 QVERIFY(!doc.isLineSaved(0)); 0415 QVERIFY(doc.isLineSaved(1)); 0416 QVERIFY(!doc.isLineSaved(2)); 0417 0418 // undo the text insertion 0419 doc.undo(); 0420 QVERIFY(!doc.isLineModified(0)); 0421 QVERIFY(!doc.isLineModified(1)); 0422 QVERIFY(!doc.isLineSaved(0)); 0423 QVERIFY(!doc.isLineSaved(1)); 0424 0425 doc.redo(); 0426 QVERIFY(!doc.isLineModified(0)); 0427 QVERIFY(!doc.isLineModified(1)); 0428 QVERIFY(!doc.isLineModified(2)); 0429 QVERIFY(!doc.isLineSaved(0)); 0430 QVERIFY(doc.isLineSaved(1)); 0431 QVERIFY(!doc.isLineSaved(2)); 0432 } 0433 0434 void ModificationSystemTest::testWrapLineAtStart() 0435 { 0436 KTextEditor::DocumentPrivate doc; 0437 0438 const QString content( 0439 "aaaa\n" 0440 "bbbb"); 0441 doc.setText(content); 0442 0443 // clear all modification flags, forces no flags 0444 doc.setModified(false); 0445 doc.undoManager()->updateLineModifications(); 0446 clearModificationFlags(&doc); 0447 0448 // wrap line 0 at end 0449 doc.editWrapLine(0, 0); 0450 0451 QVERIFY(doc.isLineModified(0)); 0452 QVERIFY(!doc.isLineModified(1)); 0453 QVERIFY(!doc.isLineModified(2)); 0454 QVERIFY(!doc.isLineSaved(0)); 0455 QVERIFY(!doc.isLineSaved(1)); 0456 QVERIFY(!doc.isLineSaved(2)); 0457 0458 doc.undo(); 0459 QVERIFY(!doc.isLineModified(0)); 0460 QVERIFY(!doc.isLineModified(1)); 0461 QVERIFY(!doc.isLineSaved(0)); 0462 QVERIFY(!doc.isLineSaved(1)); 0463 0464 doc.redo(); 0465 QVERIFY(doc.isLineModified(0)); 0466 QVERIFY(!doc.isLineModified(1)); 0467 QVERIFY(!doc.isLineModified(2)); 0468 QVERIFY(!doc.isLineSaved(0)); 0469 QVERIFY(!doc.isLineSaved(1)); 0470 QVERIFY(!doc.isLineSaved(2)); 0471 0472 // 0473 // now simulate "save", then do the undo/redo tests again 0474 // 0475 doc.setModified(false); 0476 markModifiedLinesAsSaved(&doc); 0477 doc.undoManager()->updateLineModifications(); 0478 0479 // now no line should have state "Modified" 0480 QVERIFY(!doc.isLineModified(0)); 0481 QVERIFY(!doc.isLineModified(1)); 0482 QVERIFY(!doc.isLineModified(2)); 0483 QVERIFY(doc.isLineSaved(0)); 0484 QVERIFY(!doc.isLineSaved(1)); 0485 QVERIFY(!doc.isLineSaved(2)); 0486 0487 // undo the text insertion 0488 doc.undo(); 0489 QVERIFY(!doc.isLineModified(0)); 0490 QVERIFY(!doc.isLineModified(1)); 0491 QVERIFY(!doc.isLineSaved(0)); 0492 QVERIFY(!doc.isLineSaved(1)); 0493 0494 doc.redo(); 0495 QVERIFY(!doc.isLineModified(0)); 0496 QVERIFY(!doc.isLineModified(1)); 0497 QVERIFY(!doc.isLineModified(2)); 0498 QVERIFY(doc.isLineSaved(0)); 0499 QVERIFY(!doc.isLineSaved(1)); 0500 QVERIFY(!doc.isLineSaved(2)); 0501 } 0502 0503 void ModificationSystemTest::testUnWrapLine() 0504 { 0505 KTextEditor::DocumentPrivate doc; 0506 0507 const QString content( 0508 "aaaa\n" 0509 "bbbb\n" 0510 "cccc"); 0511 doc.setText(content); 0512 0513 // clear all modification flags, forces no flags 0514 doc.setModified(false); 0515 doc.undoManager()->updateLineModifications(); 0516 clearModificationFlags(&doc); 0517 0518 // join line 0 and 1 0519 doc.editUnWrapLine(0); 0520 0521 QVERIFY(doc.isLineModified(0)); 0522 QVERIFY(!doc.isLineModified(1)); 0523 QVERIFY(!doc.isLineSaved(0)); 0524 QVERIFY(!doc.isLineSaved(1)); 0525 0526 doc.undo(); 0527 QVERIFY(!doc.isLineModified(0)); 0528 QVERIFY(!doc.isLineModified(1)); 0529 QVERIFY(!doc.isLineModified(2)); 0530 QVERIFY(doc.isLineSaved(0)); 0531 QVERIFY(doc.isLineSaved(1)); 0532 QVERIFY(!doc.isLineSaved(2)); 0533 0534 doc.redo(); 0535 QVERIFY(doc.isLineModified(0)); 0536 QVERIFY(!doc.isLineModified(1)); 0537 QVERIFY(!doc.isLineSaved(0)); 0538 QVERIFY(!doc.isLineSaved(1)); 0539 0540 // 0541 // now simulate "save", then do the undo/redo tests again 0542 // 0543 doc.setModified(false); 0544 markModifiedLinesAsSaved(&doc); 0545 doc.undoManager()->updateLineModifications(); 0546 0547 // now no line should have state "Modified" 0548 QVERIFY(!doc.isLineModified(0)); 0549 QVERIFY(!doc.isLineModified(1)); 0550 QVERIFY(doc.isLineSaved(0)); 0551 QVERIFY(!doc.isLineSaved(1)); 0552 0553 // undo the text insertion 0554 doc.undo(); 0555 QVERIFY(doc.isLineModified(0)); 0556 QVERIFY(doc.isLineModified(1)); 0557 QVERIFY(!doc.isLineModified(2)); 0558 QVERIFY(!doc.isLineSaved(0)); 0559 QVERIFY(!doc.isLineSaved(1)); 0560 QVERIFY(!doc.isLineSaved(2)); 0561 0562 doc.redo(); 0563 QVERIFY(!doc.isLineModified(0)); 0564 QVERIFY(!doc.isLineModified(1)); 0565 QVERIFY(doc.isLineSaved(0)); 0566 QVERIFY(!doc.isLineSaved(1)); 0567 } 0568 0569 void ModificationSystemTest::testUnWrapLine1Empty() 0570 { 0571 KTextEditor::DocumentPrivate doc; 0572 0573 const QString content( 0574 "aaaa\n" 0575 "\n" 0576 "bbbb"); 0577 doc.setText(content); 0578 0579 // clear all modification flags, forces no flags 0580 doc.setModified(false); 0581 doc.undoManager()->updateLineModifications(); 0582 clearModificationFlags(&doc); 0583 0584 // join line 1 and 2 0585 doc.editUnWrapLine(1); 0586 0587 QVERIFY(!doc.isLineModified(0)); 0588 QVERIFY(!doc.isLineModified(1)); 0589 QVERIFY(!doc.isLineSaved(0)); 0590 QVERIFY(!doc.isLineSaved(1)); 0591 0592 doc.undo(); 0593 QVERIFY(!doc.isLineModified(0)); 0594 QVERIFY(!doc.isLineModified(1)); 0595 QVERIFY(!doc.isLineModified(2)); 0596 QVERIFY(!doc.isLineSaved(0)); 0597 QVERIFY(doc.isLineSaved(1)); 0598 QVERIFY(!doc.isLineSaved(2)); 0599 0600 doc.redo(); 0601 QVERIFY(!doc.isLineModified(0)); 0602 QVERIFY(!doc.isLineModified(1)); 0603 QVERIFY(!doc.isLineSaved(0)); 0604 QVERIFY(!doc.isLineSaved(1)); 0605 0606 // 0607 // now simulate "save", then do the undo/redo tests again 0608 // 0609 doc.setModified(false); 0610 markModifiedLinesAsSaved(&doc); 0611 doc.undoManager()->updateLineModifications(); 0612 0613 // now no line should have state "Modified" 0614 QVERIFY(!doc.isLineModified(0)); 0615 QVERIFY(!doc.isLineModified(1)); 0616 QVERIFY(!doc.isLineSaved(0)); 0617 QVERIFY(!doc.isLineSaved(1)); 0618 0619 // undo the text insertion 0620 doc.undo(); 0621 QVERIFY(!doc.isLineModified(0)); 0622 QVERIFY(doc.isLineModified(1)); 0623 QVERIFY(!doc.isLineModified(2)); 0624 QVERIFY(!doc.isLineSaved(0)); 0625 QVERIFY(!doc.isLineSaved(1)); 0626 QVERIFY(!doc.isLineSaved(2)); 0627 0628 doc.redo(); 0629 QVERIFY(!doc.isLineModified(0)); 0630 QVERIFY(!doc.isLineModified(1)); 0631 QVERIFY(!doc.isLineSaved(0)); 0632 QVERIFY(!doc.isLineSaved(1)); 0633 } 0634 0635 void ModificationSystemTest::testUnWrapLine2Empty() 0636 { 0637 KTextEditor::DocumentPrivate doc; 0638 0639 const QString content( 0640 "aaaa\n" 0641 "\n" 0642 "bbbb"); 0643 doc.setText(content); 0644 0645 // clear all modification flags, forces no flags 0646 doc.setModified(false); 0647 doc.undoManager()->updateLineModifications(); 0648 clearModificationFlags(&doc); 0649 0650 // join line 0 and 1 0651 doc.editUnWrapLine(0); 0652 0653 QVERIFY(!doc.isLineModified(0)); 0654 QVERIFY(!doc.isLineModified(1)); 0655 QVERIFY(!doc.isLineSaved(0)); 0656 QVERIFY(!doc.isLineSaved(1)); 0657 0658 doc.undo(); 0659 QVERIFY(!doc.isLineModified(0)); 0660 QVERIFY(!doc.isLineModified(1)); 0661 QVERIFY(!doc.isLineModified(2)); 0662 QVERIFY(!doc.isLineSaved(0)); 0663 QVERIFY(doc.isLineSaved(1)); 0664 QVERIFY(!doc.isLineSaved(2)); 0665 0666 doc.redo(); 0667 QVERIFY(!doc.isLineModified(0)); 0668 QVERIFY(!doc.isLineModified(1)); 0669 QVERIFY(!doc.isLineSaved(0)); 0670 QVERIFY(!doc.isLineSaved(1)); 0671 0672 // 0673 // now simulate "save", then do the undo/redo tests again 0674 // 0675 doc.setModified(false); 0676 markModifiedLinesAsSaved(&doc); 0677 doc.undoManager()->updateLineModifications(); 0678 0679 // now no line should have state "Modified" 0680 QVERIFY(!doc.isLineModified(0)); 0681 QVERIFY(!doc.isLineModified(1)); 0682 QVERIFY(!doc.isLineSaved(0)); 0683 QVERIFY(!doc.isLineSaved(1)); 0684 0685 // undo the text insertion 0686 doc.undo(); 0687 QVERIFY(!doc.isLineModified(0)); 0688 QVERIFY(doc.isLineModified(1)); 0689 QVERIFY(!doc.isLineModified(2)); 0690 QVERIFY(!doc.isLineSaved(0)); 0691 QVERIFY(!doc.isLineSaved(1)); 0692 QVERIFY(!doc.isLineSaved(2)); 0693 0694 doc.redo(); 0695 QVERIFY(!doc.isLineModified(0)); 0696 QVERIFY(!doc.isLineModified(1)); 0697 QVERIFY(!doc.isLineSaved(0)); 0698 QVERIFY(!doc.isLineSaved(1)); 0699 } 0700 0701 void ModificationSystemTest::testNavigation() 0702 { 0703 KTextEditor::DocumentPrivate doc; 0704 0705 const QString content( 0706 "0\n" 0707 "1\n" 0708 "2"); 0709 doc.setText(content); 0710 0711 // clear all modification flags, forces no flags 0712 doc.setModified(false); 0713 doc.undoManager()->updateLineModifications(); 0714 clearModificationFlags(&doc); 0715 0716 // touch line 0 and line 2: 0717 doc.insertText(Cursor(0, 1), QLatin1String("-")); 0718 doc.insertText(Cursor(2, 1), QLatin1String("-")); 0719 0720 // test down navigation: 0721 const bool down = true; 0722 QCOMPARE(doc.findTouchedLine(-1, down), -1); 0723 QCOMPARE(doc.findTouchedLine(0, down), 0); 0724 QCOMPARE(doc.findTouchedLine(1, down), 2); 0725 QCOMPARE(doc.findTouchedLine(2, down), 2); 0726 QCOMPARE(doc.findTouchedLine(3, down), -1); 0727 0728 // test up navigation 0729 const bool up = false; 0730 QCOMPARE(doc.findTouchedLine(-1, up), -1); 0731 QCOMPARE(doc.findTouchedLine(0, up), 0); 0732 QCOMPARE(doc.findTouchedLine(1, up), 0); 0733 QCOMPARE(doc.findTouchedLine(2, up), 2); 0734 QCOMPARE(doc.findTouchedLine(3, up), -1); 0735 }