File indexing completed on 2024-04-21 03:59:31

0001 /*
0002     This file is part of the KDE libraries
0003     SPDX-FileCopyrightText: 2009 David Faure <faure@kde.org>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0006 */
0007 
0008 #ifndef TESTXMLGUIWINDOW_H
0009 #define TESTXMLGUIWINDOW_H
0010 
0011 #include <QAction>
0012 #include <QApplication>
0013 #include <QDebug>
0014 #include <QMenu>
0015 #include <QResizeEvent>
0016 #include <QTemporaryFile>
0017 #include <QTest>
0018 
0019 #include <kactioncollection.h>
0020 #include <ktoolbar.h>
0021 #include <kxmlguifactory.h>
0022 #include <kxmlguiwindow.h>
0023 
0024 class TestXmlGuiWindow : public KXmlGuiWindow
0025 {
0026 public:
0027     TestXmlGuiWindow(const QByteArray &xml, const char *localXmlFileName)
0028         : KXmlGuiWindow()
0029     {
0030         QVERIFY(m_userFile.open());
0031         m_userFile.write(xml);
0032         m_fileName = m_userFile.fileName(); // remember filename
0033         Q_ASSERT(!m_fileName.isEmpty());
0034         m_userFile.close(); // write to disk
0035         // just so that we can use kedittoolbar (because m_fileName is absolute)
0036         setLocalXMLFile(QString::fromLatin1(localXmlFileName));
0037     }
0038     void createGUI()
0039     {
0040         // This merges in ui_standards.rc, too.
0041         KXmlGuiWindow::createGUI(m_fileName);
0042     }
0043     void createGUIBad()
0044     {
0045         KXmlGuiWindow::createGUI(QStringLiteral("dontexist.rc"));
0046     }
0047 
0048     // Same as in KMainWindow_UnitTest
0049     void reallyResize(int width, int height)
0050     {
0051         const QSize oldSize = size();
0052         resize(width, height);
0053         // Send the pending resize event (resize() only sets Qt::WA_PendingResizeEvent)
0054         QResizeEvent e(size(), oldSize);
0055         QApplication::sendEvent(this, &e);
0056     }
0057 
0058     // KMainWindow::toolBar(name) creates it if not found, and we don't want that.
0059     // Also this way we test container() rather than just doing a findChild.
0060     KToolBar *toolBarByName(const QString &name)
0061     {
0062         KXMLGUIFactory *factory = guiFactory();
0063         // qDebug() << "containers:" << factory->containers("ToolBar");
0064         QWidget *toolBarW = factory->container(name, this);
0065         if (!toolBarW) {
0066             qWarning() << "No toolbar found with name" << name;
0067         }
0068         Q_ASSERT(toolBarW);
0069         KToolBar *toolBar = qobject_cast<KToolBar *>(toolBarW);
0070         Q_ASSERT(toolBar);
0071         return toolBar;
0072     }
0073     QMenu *menuByName(const QString &name)
0074     {
0075         KXMLGUIFactory *factory = guiFactory();
0076         QWidget *menuW = factory->container(name, this);
0077         Q_ASSERT(menuW);
0078         QMenu *menu = qobject_cast<QMenu *>(menuW);
0079         Q_ASSERT(menu);
0080         return menu;
0081     }
0082 
0083     void createActions(const QStringList &actionNames)
0084     {
0085         KActionCollection *coll = actionCollection();
0086         for (const QString &actionName : actionNames) {
0087             coll->addAction(actionName)->setText(QStringLiteral("Action"));
0088         }
0089     }
0090 
0091 private:
0092     QTemporaryFile m_userFile;
0093     QString m_fileName;
0094 };
0095 
0096 #endif /* TESTXMLGUIWINDOW_H */