File: testxmlguiwindow.h

package info (click to toggle)
kf6-kxmlgui 6.13.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 26,992 kB
  • sloc: cpp: 16,703; xml: 45; python: 41; ansic: 22; sh: 18; makefile: 7
file content (96 lines) | stat: -rw-r--r-- 2,903 bytes parent folder | download | duplicates (3)
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
/*
    This file is part of the KDE libraries
    SPDX-FileCopyrightText: 2009 David Faure <faure@kde.org>

    SPDX-License-Identifier: LGPL-2.0-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
*/

#ifndef TESTXMLGUIWINDOW_H
#define TESTXMLGUIWINDOW_H

#include <QAction>
#include <QApplication>
#include <QDebug>
#include <QMenu>
#include <QResizeEvent>
#include <QTemporaryFile>
#include <QTest>

#include <kactioncollection.h>
#include <ktoolbar.h>
#include <kxmlguifactory.h>
#include <kxmlguiwindow.h>

class TestXmlGuiWindow : public KXmlGuiWindow
{
public:
    TestXmlGuiWindow(const QByteArray &xml, const char *localXmlFileName)
        : KXmlGuiWindow()
    {
        QVERIFY(m_userFile.open());
        m_userFile.write(xml);
        m_fileName = m_userFile.fileName(); // remember filename
        Q_ASSERT(!m_fileName.isEmpty());
        m_userFile.close(); // write to disk
        // just so that we can use kedittoolbar (because m_fileName is absolute)
        setLocalXMLFile(QString::fromLatin1(localXmlFileName));
    }
    void createGUI()
    {
        // This merges in ui_standards.rc, too.
        KXmlGuiWindow::createGUI(m_fileName);
    }
    void createGUIBad()
    {
        KXmlGuiWindow::createGUI(QStringLiteral("dontexist.rc"));
    }

    // Same as in KMainWindow_UnitTest
    void reallyResize(int width, int height)
    {
        const QSize oldSize = size();
        resize(width, height);
        // Send the pending resize event (resize() only sets Qt::WA_PendingResizeEvent)
        QResizeEvent e(size(), oldSize);
        QApplication::sendEvent(this, &e);
    }

    // KMainWindow::toolBar(name) creates it if not found, and we don't want that.
    // Also this way we test container() rather than just doing a findChild.
    KToolBar *toolBarByName(const QString &name)
    {
        KXMLGUIFactory *factory = guiFactory();
        // qDebug() << "containers:" << factory->containers("ToolBar");
        QWidget *toolBarW = factory->container(name, this);
        if (!toolBarW) {
            qWarning() << "No toolbar found with name" << name;
        }
        Q_ASSERT(toolBarW);
        KToolBar *toolBar = qobject_cast<KToolBar *>(toolBarW);
        Q_ASSERT(toolBar);
        return toolBar;
    }
    QMenu *menuByName(const QString &name)
    {
        KXMLGUIFactory *factory = guiFactory();
        QWidget *menuW = factory->container(name, this);
        Q_ASSERT(menuW);
        QMenu *menu = qobject_cast<QMenu *>(menuW);
        Q_ASSERT(menu);
        return menu;
    }

    void createActions(const QStringList &actionNames)
    {
        KActionCollection *coll = actionCollection();
        for (const QString &actionName : actionNames) {
            coll->addAction(actionName)->setText(QStringLiteral("Action"));
        }
    }

private:
    QTemporaryFile m_userFile;
    QString m_fileName;
};

#endif /* TESTXMLGUIWINDOW_H */