File indexing completed on 2024-04-14 03:54:52

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 "bug317111.h"
0009 
0010 #include <katedocument.h>
0011 #include <kateglobal.h>
0012 #include <kateview.h>
0013 #include <kmainwindow.h>
0014 
0015 #include <QJSEngine>
0016 #include <QTest>
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 "bug317111.txt"));
0051     doc->openUrl(url);
0052 
0053     // load buggy script
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     // view must be visible...
0061     view->show();
0062     view->resize(900, 800);
0063     view->setCursorPosition(Cursor(0, 0));
0064 
0065     // evaluate test-script
0066     qDebug() << "attempting crash by calling KTextEditor::DocumentPrivate::defStyle(-1, 0)";
0067     QFile sourceFile(QLatin1String(TEST_DATA_DIR "bug317111.js"));
0068     QVERIFY(sourceFile.open(QFile::ReadOnly));
0069     QTextStream stream(&sourceFile);
0070     QString code = stream.readAll();
0071     sourceFile.close();
0072     // execute script
0073     result = env->engine()->evaluate(code, QStringLiteral(TEST_DATA_DIR "bug317111.txt"), 1);
0074     QVERIFY2(!result.isError(), result.toString().toUtf8().constData());
0075 
0076     qDebug() << "PASS (no crash)";
0077 }
0078 
0079 #include "moc_bug317111.cpp"