File indexing completed on 2024-07-21 12:13:42

0001 /*
0002     This file is part of the KDE libraries
0003     SPDX-FileCopyrightText: 2013 Gerald Senarclens de Grancy <oss@senarclens.eu>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #include "bug313759.h"
0009 
0010 #include <katedocument.h>
0011 #include <kateglobal.h>
0012 #include <kateview.h>
0013 #include <kmainwindow.h>
0014 
0015 #include <QJSEngine>
0016 #include <QtTestWidgets>
0017 
0018 #include "testutils.h"
0019 
0020 QTEST_MAIN(BugTest)
0021 
0022 using namespace KTextEditor;
0023 
0024 BugTest::BugTest()
0025     : QObject()
0026 {
0027 }
0028 
0029 BugTest::~BugTest()
0030 {
0031 }
0032 
0033 void BugTest::initTestCase()
0034 {
0035     KTextEditor::EditorPrivate::enableUnitTestMode();
0036 }
0037 
0038 void BugTest::cleanupTestCase()
0039 {
0040 }
0041 
0042 void BugTest::tryCrash()
0043 {
0044     // set up document and view
0045     KMainWindow *toplevel = new KMainWindow();
0046     KTextEditor::DocumentPrivate *doc = new KTextEditor::DocumentPrivate(true, false, toplevel);
0047     KTextEditor::ViewPrivate *view = static_cast<KTextEditor::ViewPrivate *>(doc->createView(nullptr));
0048     bool outputWasCustomised = false;
0049     TestScriptEnv *env = new TestScriptEnv(doc, outputWasCustomised);
0050     const QUrl url = QUrl::fromLocalFile(QLatin1String(TEST_DATA_DIR "bug313759.txt"));
0051     doc->openUrl(url);
0052 
0053     // load moveLinesDown and moveLinesUp
0054     QFile scriptFile(QLatin1String(JS_DATA_DIR "commands/utils.js"));
0055     QVERIFY(scriptFile.exists());
0056     QVERIFY(scriptFile.open(QFile::ReadOnly));
0057     QJSValue result = env->engine()->evaluate(QString::fromLatin1(scriptFile.readAll()), scriptFile.fileName());
0058     QVERIFY2(!result.isError(), result.toString().toUtf8().constData());
0059 
0060     // enable on the fly spell checking
0061     doc->onTheFlySpellCheckingEnabled(true);
0062 
0063     // view must be visible...
0064     view->show();
0065     view->resize(900, 800);
0066     view->setCursorPosition(Cursor(0, 0));
0067     doc->editBegin();
0068     // QTest::qWait(200);
0069 
0070     // evaluate test-script
0071     qDebug() << "attempting crash by moving lines w/ otf spell checking enabled";
0072     QFile sourceFile(QLatin1String(TEST_DATA_DIR "bug313759.js"));
0073     QVERIFY(sourceFile.open(QFile::ReadOnly));
0074     QTextStream stream(&sourceFile);
0075     stream.setCodec("UTF8");
0076     QString code = stream.readAll();
0077     sourceFile.close();
0078     // execute script
0079     result = env->engine()->evaluate(code, QLatin1String(TEST_DATA_DIR "bug313759.txt"), 1);
0080     QVERIFY2(!result.isError(), result.toString().toUtf8().constData());
0081 
0082     doc->editEnd();
0083     qDebug() << "PASS (no crash)";
0084 }
0085 
0086 #include "moc_bug313759.cpp"