File indexing completed on 2024-05-12 04:38:22

0001 /*
0002     SPDX-FileCopyrightText: 2008 Alexander Dymo <adymo@kdevelop.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "test_shelldocumentoperation.h"
0008 
0009 #include <QTest>
0010 #include <QAction>
0011 
0012 #include <sublime/area.h>
0013 #include <sublime/view.h>
0014 
0015 #include <tests/testcore.h>
0016 #include <tests/autotestshell.h>
0017 
0018 #include "../documentcontroller.h"
0019 #include "../uicontroller.h"
0020 
0021 #include <KActionCollection>
0022 #include <KXMLGUIFactory>
0023 #include <KParts/MainWindow>
0024 #include <KTextEditor/View>
0025 #include <KTextEditor/Document>
0026 #include <KTextEditor/MovingInterface>
0027 
0028 using namespace KDevelop;
0029 
0030 void TestShellDocumentOperation::initTestCase()
0031 {
0032     AutoTestShell::init({{}}); // do not load plugins at all
0033     TestCore::initialize();
0034 }
0035 
0036 void TestShellDocumentOperation::cleanupTestCase()
0037 {
0038     TestCore::shutdown();
0039 }
0040 
0041 void TestShellDocumentOperation::testOpenDocumentFromText()
0042 {
0043     //open some docs
0044     IDocumentController *documentController = Core::self()->documentController();
0045     documentController->openDocumentFromText(QStringLiteral("Test1"));
0046 
0047     //test that we have this document in the list, signals are emitted and so on
0048     QCOMPARE(documentController->openDocuments().count(), 1);
0049     QCOMPARE(documentController->openDocuments().at(0)->textDocument()->text(), QStringLiteral("Test1"));
0050 
0051     Sublime::Area *area = Core::self()->uiControllerInternal()->activeArea();
0052     QCOMPARE(area->views().count(), 1);
0053     documentController->openDocuments().at(0)->close(IDocument::Discard);
0054 
0055     // We used to have a bug where closing document failed to remove its
0056     // views from area, so check it here.
0057     QCOMPARE(area->views().count(), 0);
0058 }
0059 
0060 void TestShellDocumentOperation::testClosing()
0061 {
0062     // Test that both the view and the view widget is deleted when closing
0063     // document.
0064     {
0065         IDocumentController *documentController = Core::self()->documentController();
0066         documentController->openDocumentFromText(QStringLiteral("Test1"));
0067         Sublime::Area *area = Core::self()->uiControllerInternal()->activeArea();
0068         QCOMPARE(area->views().count(), 1);
0069         QPointer<Sublime::View> the_view = area->views().at(0);
0070         QPointer<QWidget> the_widget = the_view->widget();
0071         documentController->openDocuments().at(0)->close(IDocument::Discard);
0072         QCOMPARE(the_view.data(), (Sublime::View*)nullptr);
0073         QCOMPARE(the_widget.data(), (QWidget*)nullptr);
0074     }
0075 
0076     // Now try the same, where there are two open documents.
0077     {
0078         IDocumentController *documentController = Core::self()->documentController();
0079         // Annoying, the order of documents in
0080         // documentController->openDocuments() depends on how URLs hash. So,
0081         // to reliably close the second one, get hold of a pointer.
0082         IDocument* doc1 = documentController->openDocumentFromText(QStringLiteral("Test1"));
0083         IDocument* doc2 = documentController->openDocumentFromText(QStringLiteral("Test2"));
0084         Sublime::Area *area = Core::self()->uiControllerInternal()->activeArea();
0085         QCOMPARE(area->views().count(), 2);
0086 
0087         QPointer<Sublime::View> the_view = area->views().at(1);
0088         qDebug() << this << "see views " << area->views().at(0)
0089                      << " " << area->views().at(1);
0090         QPointer<QWidget> the_widget = the_view->widget();
0091         doc2->close(IDocument::Discard);
0092         QCOMPARE(the_view.data(), (Sublime::View*)nullptr);
0093         QCOMPARE(the_widget.data(), (QWidget*)nullptr);
0094         doc1->close(IDocument::Discard);
0095     }
0096 }
0097 
0098 void TestShellDocumentOperation::testKateDocumentAndViewCreation()
0099 {
0100     //create one document
0101     IDocumentController *documentController = Core::self()->documentController();
0102     documentController->openDocumentFromText(QString());
0103     QCOMPARE(documentController->openDocuments().count(), 1);
0104 
0105     //assure we have only one kate view for the newly created document
0106     KTextEditor::Document *doc = documentController->openDocuments().at(0)->textDocument();
0107     QCOMPARE(doc->views().count(), 1);
0108     QCOMPARE(qobject_cast<KTextEditor::MovingInterface*>(doc)->revision(), qint64(0));
0109 
0110     //also assure the view's xmlgui is plugged in
0111     KParts::MainWindow *main = Core::self()->uiControllerInternal()->activeMainWindow();
0112     QVERIFY(main);
0113     QVERIFY(main->guiFactory()->clients().contains(doc->views().at(0)));
0114 
0115     //KTextEditor::views is internally a QHash::keys() call: so the order of the views will vary
0116     const auto originalView = doc->views().at(0);
0117 
0118     //create the new view and activate it (using split action from mainwindow)
0119     QAction *splitAction = main->actionCollection()->action(QStringLiteral("split_vertical"));
0120     QVERIFY(splitAction);
0121     splitAction->trigger();
0122     const auto viewList = doc->views();
0123     QCOMPARE(viewList.count(), 2);
0124 
0125     const auto newlySplitView = originalView == viewList[0] ? viewList[1] : viewList[0];
0126 
0127     //check that we did switch to the new xmlguiclient
0128     QVERIFY(!main->guiFactory()->clients().contains(originalView));
0129     QVERIFY(main->guiFactory()->clients().contains(newlySplitView));
0130 
0131     documentController->openDocuments().at(0)->close(IDocument::Discard);
0132 }
0133 
0134 QTEST_MAIN(TestShellDocumentOperation)
0135 
0136 #include "moc_test_shelldocumentoperation.cpp"