forked from qt-creator/qt-creator
TextEditor: add tests for the code assistant
Task-number: QTCREATORBUG-28989 Change-Id: Id76d5df589ab50c7deb96b8ad29d1f29bc368e59 Reviewed-by: Christian Stenger <christian.stenger@qt.io>
This commit is contained in:
@@ -113,5 +113,7 @@ add_qtc_plugin(TextEditor
|
||||
|
||||
extend_qtc_plugin(TextEditor
|
||||
CONDITION WITH_TESTS
|
||||
SOURCES texteditor_test.cpp
|
||||
SOURCES
|
||||
codeassist/codeassist_test.cpp codeassist/codeassist_test.h
|
||||
texteditor_test.cpp
|
||||
)
|
||||
|
||||
152
src/plugins/texteditor/codeassist/codeassist_test.cpp
Normal file
152
src/plugins/texteditor/codeassist/codeassist_test.cpp
Normal file
@@ -0,0 +1,152 @@
|
||||
// Copyright (C) 2016 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
|
||||
|
||||
#ifdef WITH_TESTS
|
||||
|
||||
#include "codeassist_test.h"
|
||||
|
||||
#include "../texteditor.h"
|
||||
|
||||
#include "assistinterface.h"
|
||||
#include "assistproposaliteminterface.h"
|
||||
#include "asyncprocessor.h"
|
||||
#include "completionassistprovider.h"
|
||||
#include "genericproposal.h"
|
||||
#include "genericproposalwidget.h"
|
||||
|
||||
#include <coreplugin/coreconstants.h>
|
||||
#include <coreplugin/editormanager/editormanager.h>
|
||||
#include <utils/temporarydirectory.h>
|
||||
|
||||
#include <QtTest/QtTest>
|
||||
|
||||
namespace TextEditor::Internal {
|
||||
|
||||
class TestProposalItem : public AssistProposalItemInterface
|
||||
{
|
||||
public:
|
||||
QString m_text = "test";
|
||||
bool m_implicitlyApplies = false;
|
||||
bool m_prematurelyApplies = false;
|
||||
QIcon m_icon;
|
||||
QString m_detail = "detail";
|
||||
bool m_isSnippet = false;
|
||||
bool m_isValid = true;
|
||||
|
||||
QString text() const override { return m_text; }
|
||||
bool implicitlyApplies() const override { return m_implicitlyApplies; }
|
||||
bool prematurelyApplies(const QChar &) const override { return m_prematurelyApplies; }
|
||||
QIcon icon() const override { return m_icon; }
|
||||
QString detail() const override { return m_detail; }
|
||||
bool isSnippet() const override { return m_isSnippet; }
|
||||
bool isValid() const override { return m_isValid; }
|
||||
quint64 hash() const override { return 0; } // used to remove duplicates
|
||||
};
|
||||
|
||||
class OpenEditorItem : public TestProposalItem
|
||||
{
|
||||
public:
|
||||
void apply(TextDocumentManipulatorInterface &, int) const override
|
||||
{
|
||||
m_openedEditor = Core::EditorManager::openEditor(m_filePath,
|
||||
Core::Constants::K_DEFAULT_TEXT_EDITOR_ID);
|
||||
}
|
||||
|
||||
mutable Core::IEditor *m_openedEditor = nullptr;
|
||||
Utils::FilePath m_filePath;
|
||||
};
|
||||
|
||||
class TestProposalWidget : public GenericProposalWidget
|
||||
{
|
||||
public:
|
||||
void showProposal(const QString &prefix) override
|
||||
{
|
||||
GenericProposalModelPtr proposalModel = model();
|
||||
if (proposalModel && proposalModel->size() == 1) {
|
||||
emit proposalItemActivated(proposalModel->proposalItem(0));
|
||||
deleteLater();
|
||||
return;
|
||||
}
|
||||
GenericProposalWidget::showProposal(prefix);
|
||||
}
|
||||
};
|
||||
|
||||
class TestProposal : public GenericProposal
|
||||
{
|
||||
public:
|
||||
TestProposal(int pos, const QList<AssistProposalItemInterface *> &items)
|
||||
: GenericProposal(pos, items)
|
||||
{}
|
||||
IAssistProposalWidget *createWidget() const override { return new TestProposalWidget; }
|
||||
};
|
||||
|
||||
class TestProcessor : public AsyncProcessor
|
||||
{
|
||||
public:
|
||||
TestProcessor(const QList<AssistProposalItemInterface *> &items)
|
||||
: m_items(items)
|
||||
{}
|
||||
IAssistProposal *performAsync() override
|
||||
{ return new TestProposal(interface()->position(), m_items); }
|
||||
QList<AssistProposalItemInterface *> m_items;
|
||||
};
|
||||
|
||||
class TestProvider : public CompletionAssistProvider
|
||||
{
|
||||
public:
|
||||
IAssistProcessor *createProcessor(const AssistInterface *assistInterface) const override
|
||||
{
|
||||
Q_UNUSED(assistInterface);
|
||||
return new TestProcessor(m_items);
|
||||
}
|
||||
QList<AssistProposalItemInterface *> m_items;
|
||||
};
|
||||
|
||||
void CodeAssistTests::initTestCase()
|
||||
{
|
||||
Core::IEditor *editor = Core::EditorManager::openEditorWithContents(
|
||||
Core::Constants::K_DEFAULT_TEXT_EDITOR_ID);
|
||||
QVERIFY(editor);
|
||||
m_editor = qobject_cast<BaseTextEditor *>(editor);
|
||||
QVERIFY(m_editor);
|
||||
m_editorsToClose << m_editor;
|
||||
m_testProvider = new TestProvider();
|
||||
}
|
||||
|
||||
static Utils::FilePath createBigFile()
|
||||
{
|
||||
constexpr int textChunkSize = 65536; // from utils/textfileformat.cpp
|
||||
|
||||
const Utils::FilePath result = Utils::TemporaryDirectory::masterDirectoryFilePath() / "BigFile";
|
||||
QByteArray data;
|
||||
data.reserve(textChunkSize);
|
||||
while (data.size() < textChunkSize)
|
||||
data.append("bigfile line\n");
|
||||
result.writeFileContents(data);
|
||||
return result;
|
||||
}
|
||||
|
||||
void CodeAssistTests::testFollowSymbolBigFile()
|
||||
{
|
||||
auto item = new OpenEditorItem;
|
||||
item->m_filePath = createBigFile();
|
||||
m_testProvider->m_items = {item};
|
||||
auto editorWidget = m_editor->editorWidget();
|
||||
|
||||
editorWidget->invokeAssist(FollowSymbol, m_testProvider);
|
||||
QSignalSpy spy(editorWidget, &TextEditorWidget::assistFinished);
|
||||
QVERIFY(spy.wait(1000));
|
||||
QVERIFY(item->m_openedEditor);
|
||||
m_editorsToClose << item->m_openedEditor;
|
||||
}
|
||||
|
||||
void CodeAssistTests::cleanupTestCase()
|
||||
{
|
||||
m_testProvider->m_items.clear();
|
||||
Core::EditorManager::closeEditors(m_editorsToClose);
|
||||
QVERIFY(Core::EditorManager::currentEditor() == nullptr);
|
||||
}
|
||||
|
||||
} // namespace TextEditor::Internal
|
||||
|
||||
#endif // ifdef WITH_TESTS
|
||||
37
src/plugins/texteditor/codeassist/codeassist_test.h
Normal file
37
src/plugins/texteditor/codeassist/codeassist_test.h
Normal file
@@ -0,0 +1,37 @@
|
||||
// Copyright (C) 2016 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0+ OR GPL-3.0 WITH Qt-GPL-exception-1.0
|
||||
|
||||
#pragma once
|
||||
|
||||
#ifdef WITH_TESTS
|
||||
|
||||
#include <QObject>
|
||||
|
||||
namespace Core { class IEditor; }
|
||||
namespace TextEditor { class BaseTextEditor; }
|
||||
|
||||
namespace TextEditor::Internal {
|
||||
|
||||
class TestProvider;
|
||||
|
||||
class CodeAssistTests : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
|
||||
private slots:
|
||||
void initTestCase();
|
||||
|
||||
void testFollowSymbolBigFile();
|
||||
|
||||
void cleanupTestCase();
|
||||
|
||||
private:
|
||||
TextEditor::BaseTextEditor *m_editor = nullptr;
|
||||
QList<Core::IEditor *> m_editorsToClose;
|
||||
TestProvider *m_testProvider = nullptr;
|
||||
};
|
||||
|
||||
} // namespace TextEditor::Internal
|
||||
|
||||
#endif
|
||||
@@ -224,6 +224,8 @@ Project {
|
||||
|
||||
QtcTestFiles {
|
||||
files: [
|
||||
"codeassist/codeassist_test.cpp",
|
||||
"codeassist/codeassist_test.h",
|
||||
"texteditor_test.cpp",
|
||||
]
|
||||
}
|
||||
|
||||
@@ -3,20 +3,13 @@
|
||||
|
||||
#ifdef WITH_TESTS
|
||||
|
||||
#include <QGuiApplication>
|
||||
#include <QClipboard>
|
||||
#include <QString>
|
||||
#include "tabsettings.h"
|
||||
#include "texteditorplugin.h"
|
||||
|
||||
#include <QTextDocument>
|
||||
#include <QtTest/QtTest>
|
||||
|
||||
#include <coreplugin/editormanager/editormanager.h>
|
||||
#include <coreplugin/coreconstants.h>
|
||||
|
||||
#include "texteditor.h"
|
||||
#include "texteditorplugin.h"
|
||||
#include "textdocument.h"
|
||||
#include "tabsettings.h"
|
||||
|
||||
using namespace TextEditor;
|
||||
namespace TextEditor {
|
||||
|
||||
QString tabPolicyToString(TabSettings::TabPolicy policy)
|
||||
{
|
||||
@@ -149,4 +142,6 @@ void Internal::TextEditorPlugin::testIndentationClean()
|
||||
QCOMPARE(settings.isIndentationClean(block, indentSize), clean);
|
||||
}
|
||||
|
||||
} // namespace TextEditor
|
||||
|
||||
#endif // ifdef WITH_TESTS
|
||||
|
||||
@@ -20,6 +20,10 @@
|
||||
#include "texteditorsettings.h"
|
||||
#include "texteditortr.h"
|
||||
|
||||
#ifdef WITH_TESTS
|
||||
#include "codeassist/codeassist_test.h"
|
||||
#endif
|
||||
|
||||
#include <coreplugin/actionmanager/actioncontainer.h>
|
||||
#include <coreplugin/actionmanager/actionmanager.h>
|
||||
#include <coreplugin/actionmanager/command.h>
|
||||
@@ -142,6 +146,10 @@ void TextEditorPlugin::initialize()
|
||||
Tr::tr("Text", "SnippetProvider"));
|
||||
|
||||
d->createStandardContextMenu();
|
||||
|
||||
#ifdef WITH_TESTS
|
||||
addTest<CodeAssistTests>();
|
||||
#endif
|
||||
}
|
||||
|
||||
void TextEditorPluginPrivate::extensionsInitialized()
|
||||
|
||||
Reference in New Issue
Block a user