diff --git a/autotests/src/katedocument_test.h b/autotests/src/katedocument_test.h --- a/autotests/src/katedocument_test.h +++ b/autotests/src/katedocument_test.h @@ -38,28 +38,20 @@ void testWrapParagraph(); void testReplaceQStringList(); void testMovingInterfaceSignals(); - void testSetTextPerformance(); void testRemoveTextPerformance(); - void testForgivingApiUsage(); - void testRemoveMultipleLines(); void testInsertNewline(); void testInsertAfterEOF(); - void testAutoBrackets(); - void testReplaceTabs(); - void testDigest(); void testModelines(); - void testDefStyleNum(); - void testTypeCharsWithSurrogateAndNewLine(); - void testRemoveComposedCharacters(); + void testAutoReload(); }; #endif // KATE_DOCUMENT_TEST_H diff --git a/autotests/src/katedocument_test.cpp b/autotests/src/katedocument_test.cpp --- a/autotests/src/katedocument_test.cpp +++ b/autotests/src/katedocument_test.cpp @@ -634,4 +634,50 @@ QCOMPARE(doc.text(), QString::fromUtf8(("क्ति"))); } +void KateDocumentTest::testAutoReload() +{ + QTemporaryFile file("AutoReloadTestFile"); + file.open(); + QTextStream stream(&file); + stream << "Hello"; + stream.flush(); + + KTextEditor::DocumentPrivate doc; + auto view = static_cast(doc.createView(nullptr)); + QVERIFY(doc.openUrl(QUrl::fromLocalFile(file.fileName()))); + QCOMPARE(doc.text(), "Hello"); + // The cursor should be and stay in the last line... + QCOMPARE(view->cursorPosition().line(), doc.documentEnd().line()); + + doc.autoReloadToggled(true); + + // Some magic value. You can wait as long as you like after write to file, + // without to wait before it doesn't work here. It mostly fails with 500, + // it tend to work with 600, but is not guarantied to work even with 800 + QTest::qWait(1000); + + stream << "\nTest"; + stream.flush(); + + // Hardcoded delay m_modOnHdTimer in DocumentPrivate + // + min val with which it looks working reliable here + QTest::qWait(200 + 800); + QCOMPARE(doc.text(), "Hello\nTest"); + // ...stay in the last line after reload! + QCOMPARE(view->cursorPosition().line(), doc.documentEnd().line()); + + // Now we have more then one line, set the cursor to some line != last line... + Cursor c(0, 3); + view->setCursorPosition(c); + + stream << "\nWorld!"; + stream.flush(); + file.close(); + + QTest::qWait(200 + 800); + QCOMPARE(doc.text(), "Hello\nTest\nWorld!"); + // ...and ensure we have not move around + QCOMPARE(view->cursorPosition(), c); +} + #include "katedocument_test.moc" diff --git a/src/document/katedocument.cpp b/src/document/katedocument.cpp --- a/src/document/katedocument.cpp +++ b/src/document/katedocument.cpp @@ -266,9 +266,10 @@ m_autoReloadMode = new KToggleAction(i18n("Auto Reload Document"), this); m_autoReloadMode->setWhatsThis(i18n("Automatic reload the document when it was changed on disk")); connect(m_autoReloadMode, &KToggleAction::triggered, this, &DocumentPrivate::autoReloadToggled); - // Prepare some reload amok protector + // Prepare some reload amok protector... m_autoReloadThrottle.setSingleShot(true); - m_autoReloadThrottle.setInterval(3000); + //...but keep the value small in unit tests + m_autoReloadThrottle.setInterval(KTextEditor::EditorPrivate::self()->unitTestMode() ? 50 : 3000); connect(&m_autoReloadThrottle, &QTimer::timeout, this, &DocumentPrivate::onModOnHdAutoReload); /**