1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
|
/*
SPDX-FileCopyrightText: 2015 Milian Wolff <mail@milianw.de>
SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
*/
#include "test_ktexteditorpluginintegration.h"
#include <QTest>
#include <QLoggingCategory>
#include <QSignalSpy>
#include <tests/autotestshell.h>
#include <tests/testcore.h>
#include <shell/plugincontroller.h>
#include <shell/uicontroller.h>
#include <KTextEditor/Application>
#include <KTextEditor/Editor>
#include <KTextEditor/MainWindow>
#include <KTextEditor/Plugin>
#include <KTextEditor/View>
#include <KTextEditor/Document>
using namespace KDevelop;
namespace {
template<typename T>
QPointer<T> makeQPointer(T *ptr)
{
return {ptr};
}
IToolViewFactory *findToolView(const QString &id)
{
const auto uiController = Core::self()->uiControllerInternal();
const auto map = uiController->factoryDocuments();
for (auto it = map.begin(); it != map.end(); ++it) {
if (it.key()->id() == id) {
return it.key();
}
}
return nullptr;
}
class TestPlugin : public KTextEditor::Plugin
{
Q_OBJECT
public:
explicit TestPlugin(QObject *parent)
: Plugin(parent)
{
}
QObject *createView(KTextEditor::MainWindow * mainWindow) override
{
return new QObject(mainWindow);
}
};
}
void TestKTextEditorPluginIntegration::initTestCase()
{
QLoggingCategory::setFilterRules(QStringLiteral("*.debug=false\ndefault.debug=true\n"));
AutoTestShell::init({QStringLiteral("katesnippetsplugin")});
TestCore::initialize();
QVERIFY(KTextEditor::Editor::instance());
}
void TestKTextEditorPluginIntegration::cleanupTestCase()
{
auto controller = Core::self()->pluginController();
const auto id = QStringLiteral("katesnippetsplugin");
auto plugin = makeQPointer(controller->loadPlugin(id));
const auto editor = makeQPointer(KTextEditor::Editor::instance());
const auto application = makeQPointer(editor->application());
const auto window = makeQPointer(application->activeMainWindow());
TestCore::shutdown();
QVERIFY(!plugin);
QVERIFY(!window);
QVERIFY(!application);
// editor lives by design until QCoreApplication terminates, then autodeletes
}
void TestKTextEditorPluginIntegration::testApplication()
{
auto app = KTextEditor::Editor::instance()->application();
QVERIFY(app);
QVERIFY(app->parent());
QCOMPARE(app->parent()->metaObject()->className(), "KTextEditorIntegration::Application");
QVERIFY(app->activeMainWindow());
QCOMPARE(app->mainWindows().size(), 1);
QVERIFY(app->mainWindows().contains(app->activeMainWindow()));
}
void TestKTextEditorPluginIntegration::testMainWindow()
{
auto window = KTextEditor::Editor::instance()->application()->activeMainWindow();
QVERIFY(window);
QVERIFY(window->parent());
QCOMPARE(window->parent()->metaObject()->className(), "KTextEditorIntegration::MainWindow");
const auto id = QStringLiteral("kte_integration_toolview");
const auto icon = QIcon::fromTheme(QStringLiteral("kdevelop"));
const auto text = QStringLiteral("some text");
QVERIFY(!findToolView(id));
auto plugin = new TestPlugin(this);
auto toolView = makeQPointer(window->createToolView(plugin, id, KTextEditor::MainWindow::Bottom, icon, text));
QVERIFY(toolView);
auto factory = findToolView(id);
QVERIFY(factory);
// we reuse the same view
QWidget parent;
auto kdevToolView = makeQPointer(factory->create(&parent));
QCOMPARE(kdevToolView->parentWidget(), &parent);
QCOMPARE(toolView->parentWidget(), kdevToolView.data());
// the children are kept alive when the tool view gets destroyed
delete kdevToolView;
QVERIFY(toolView);
kdevToolView = factory->create(&parent);
// and we reuse the ktexteditor tool view for the new kdevelop tool view
QCOMPARE(toolView->parentWidget(), kdevToolView.data());
delete toolView;
delete kdevToolView;
delete plugin;
QVERIFY(!findToolView(id));
}
void TestKTextEditorPluginIntegration::testPlugin()
{
auto controller = Core::self()->pluginController();
const auto id = QStringLiteral("katesnippetsplugin");
auto plugin = makeQPointer(controller->loadPlugin(id));
if (!plugin) {
QSKIP("Cannot continue without katesnippetsplugin, install Kate");
}
auto app = KTextEditor::Editor::instance()->application();
auto ktePlugin = makeQPointer(app->plugin(id));
QVERIFY(ktePlugin);
auto view = makeQPointer(app->activeMainWindow()->pluginView(id));
QVERIFY(view);
const auto rawView = view.data();
QSignalSpy spy(app->activeMainWindow(), &KTextEditor::MainWindow::pluginViewDeleted);
QVERIFY(controller->unloadPlugin(id));
QVERIFY(!ktePlugin);
QCOMPARE(spy.count(), 1);
QCOMPARE(spy.first().count(), 2);
QCOMPARE(spy.first().at(0), QVariant::fromValue(id));
QCOMPARE(spy.first().at(1), QVariant::fromValue(rawView));
QVERIFY(!view);
}
void TestKTextEditorPluginIntegration::testPluginUnload()
{
auto controller = Core::self()->pluginController();
const auto id = QStringLiteral("katesnippetsplugin");
auto plugin = makeQPointer(controller->loadPlugin(id));
if (!plugin) {
QSKIP("Cannot continue without katesnippetsplugin, install Kate");
}
auto app = KTextEditor::Editor::instance()->application();
auto ktePlugin = makeQPointer(app->plugin(id));
QVERIFY(ktePlugin);
delete ktePlugin;
// don't crash
plugin->unload();
}
QTEST_MAIN(TestKTextEditorPluginIntegration)
#include <test_ktexteditorpluginintegration.moc>
#include "moc_test_ktexteditorpluginintegration.cpp"
|