File indexing completed on 2024-04-14 03:57:05

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 #include "testguiclient.h"
0009 #include "testxmlguiwindow.h"
0010 
0011 #ifdef QT_DBUS_LIB
0012 #include <QDBusConnection>
0013 #endif
0014 #include <QDir>
0015 #include <QFile>
0016 #include <QFileInfo>
0017 #include <QSignalSpy>
0018 #include <QStandardPaths>
0019 #include <QTest>
0020 
0021 #include <KConfig>
0022 #include <KConfigGroup>
0023 #include <KIconLoader>
0024 #include <KSharedConfig>
0025 #include <kmainwindow.h>
0026 #include <ktoolbar.h>
0027 
0028 // We use the data types below in a QVariant, so Q_DECLARE_METATYPE is needed for them.
0029 Q_DECLARE_METATYPE(Qt::MouseButton)
0030 Q_DECLARE_METATYPE(Qt::MouseButtons)
0031 Q_DECLARE_METATYPE(Qt::KeyboardModifiers)
0032 
0033 // Ensure everything uses test paths, including stuff run before main, such as the KdePlatformThemePlugin
0034 void enableTestMode()
0035 {
0036     QStandardPaths::setTestModeEnabled(true);
0037 }
0038 Q_CONSTRUCTOR_FUNCTION(enableTestMode)
0039 
0040 class tst_KToolBar : public QObject
0041 {
0042     Q_OBJECT
0043 
0044 public Q_SLOTS:
0045     void initTestCase();
0046     void cleanupTestCase();
0047     void init();
0048     void cleanup();
0049 
0050 private Q_SLOTS:
0051     void ktoolbar();
0052 
0053     void testIconSizeNoXmlGui_data();
0054     void testIconSizeNoXmlGui();
0055     void testIconSizeXmlGui_data();
0056     void testIconSizeXmlGui();
0057     void testToolButtonStyleNoXmlGui_data();
0058     void testToolButtonStyleNoXmlGui();
0059     void testToolButtonStyleXmlGui_data();
0060     void testToolButtonStyleXmlGui();
0061     void testToolBarPosition();
0062     void testXmlGuiSwitching();
0063     void testKAuthorizedDisableToggleAction();
0064 
0065 Q_SIGNALS:
0066     void signalAppearanceChanged();
0067 
0068 protected:
0069     bool eventFilter(QObject *watched, QEvent *event) override;
0070 
0071 private:
0072     void changeGlobalToolButtonStyleSetting(const QString &, const QString &);
0073     void deleteGlobalToolButtonStyleSetting();
0074     QByteArray m_xml;
0075     bool m_showWasCalled;
0076 };
0077 
0078 QTEST_MAIN(tst_KToolBar)
0079 
0080 static void copy_dir(const QString &from, const QDir &to)
0081 {
0082     QDir src = QDir(from);
0083     QDir dest = QDir(to.filePath(src.dirName()));
0084     to.mkpath(src.dirName());
0085     const auto fileInfos = src.entryInfoList(QDir::Dirs | QDir::Files | QDir::NoDotAndDotDot);
0086     for (const QFileInfo &fileInfo : fileInfos) {
0087         if (fileInfo.isDir()) {
0088             copy_dir(fileInfo.filePath(), dest);
0089         } else {
0090             QFile::copy(fileInfo.filePath(), dest.filePath(fileInfo.fileName()));
0091         }
0092     }
0093 }
0094 
0095 // This will be called before the first test function is executed.
0096 // It is only called once.
0097 void tst_KToolBar::initTestCase()
0098 {
0099     // start with a clean place to put data
0100     QDir testDataDir = QDir(QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation));
0101     QVERIFY(testDataDir.absolutePath().contains(QLatin1String("qttest")));
0102     testDataDir.removeRecursively();
0103     testDataDir.mkpath(QStringLiteral("."));
0104 
0105     // setup action restriction so we can test whether this actually disables some functionality
0106     KConfigGroup actionRestrictions(KSharedConfig::openConfig(), QStringLiteral("KDE Action Restrictions"));
0107     actionRestrictions.writeEntry("action/options_show_toolbar", false);
0108 
0109     // copy a minimal icon theme to where KIconTheme will find it, in case oxygen-icons is not
0110     // installed
0111     copy_dir(QFINDTESTDATA("icons"), testDataDir);
0112 
0113     m_xml =
0114         "<?xml version = '1.0'?>\n"
0115         "<!DOCTYPE gui SYSTEM \"kpartgui.dtd\">\n"
0116         "<gui version=\"1\" name=\"foo\" >\n"
0117         "<MenuBar>\n"
0118         "</MenuBar>\n"
0119         "<ToolBar name=\"mainToolBar\">\n"
0120         "  <Action name=\"go_up\"/>\n"
0121         "</ToolBar>\n"
0122         "<ToolBar name=\"otherToolBar\" position=\"bottom\" iconText=\"TextUnderIcon\">\n"
0123         "  <Action name=\"go_up\"/>\n"
0124         "</ToolBar>\n"
0125         "<ToolBar name=\"cleanToolBar\">\n"
0126         "  <Action name=\"go_up\"/>\n"
0127         "</ToolBar>\n"
0128         "<ToolBar name=\"hiddenToolBar\" hidden=\"true\">\n"
0129         "  <Action name=\"go_up\"/>\n"
0130         "</ToolBar>\n"
0131         "<ToolBar name=\"secondHiddenToolBar\" hidden=\"true\">\n"
0132         "  <Action name=\"go_up\"/>\n"
0133         "</ToolBar>\n"
0134         "<ToolBar iconSize=\"32\" name=\"bigToolBar\">\n"
0135         "  <Action name=\"go_up\"/>\n"
0136         "</ToolBar>\n"
0137         "<ToolBar iconSize=\"32\" name=\"bigUnchangedToolBar\">\n"
0138         "  <Action name=\"go_up\"/>\n"
0139         "</ToolBar>\n"
0140         "</gui>\n";
0141 
0142     qRegisterMetaType<Qt::MouseButtons>("Qt::MouseButtons");
0143     qRegisterMetaType<Qt::KeyboardModifiers>("Qt::KeyboardModifiers");
0144 }
0145 
0146 // This will be called after the last test function is executed.
0147 // It is only called once.
0148 void tst_KToolBar::cleanupTestCase()
0149 {
0150     QDir testDataDir = QDir(QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation));
0151     QDir testIconsDir = QDir(testDataDir.absoluteFilePath(QStringLiteral("icons")));
0152     QVERIFY(testIconsDir.absolutePath().contains(QLatin1String("qttest")));
0153     testIconsDir.removeRecursively();
0154 }
0155 
0156 // This will be called before each test function is executed.
0157 void tst_KToolBar::init()
0158 {
0159 }
0160 
0161 // This will be called after every test function.
0162 void tst_KToolBar::cleanup()
0163 {
0164     QFile::remove(QStandardPaths::writableLocation(QStandardPaths::GenericConfigLocation) + QStringLiteral("/tst_KToolBar"));
0165     deleteGlobalToolButtonStyleSetting();
0166 }
0167 
0168 void tst_KToolBar::ktoolbar()
0169 {
0170     KMainWindow kmw;
0171     // Creating a KToolBar directly
0172     KToolBar bar(&kmw);
0173     QCOMPARE(bar.mainWindow(), &kmw);
0174     // Asking KMainWindow for a KToolBar (more common)
0175     KToolBar *mainToolBar = kmw.toolBar(QStringLiteral("mainToolBar"));
0176     QCOMPARE(mainToolBar->mainWindow(), &kmw);
0177 }
0178 
0179 Q_DECLARE_METATYPE(KConfigGroup)
0180 
0181 void tst_KToolBar::testIconSizeNoXmlGui_data()
0182 {
0183     QTest::addColumn<int>("iconSize");
0184     QTest::newRow("16") << 16;
0185     QTest::newRow("22") << 22;
0186     QTest::newRow("32") << 32;
0187     QTest::newRow("64") << 64;
0188 }
0189 
0190 void tst_KToolBar::testIconSizeNoXmlGui()
0191 {
0192     QFETCH(int, iconSize);
0193     KConfig config(QStringLiteral("tst_KToolBar"));
0194     KConfigGroup group(&config, QStringLiteral("group"));
0195     {
0196         KMainWindow kmw;
0197         KToolBar *mainToolBar = kmw.toolBar(QStringLiteral("mainToolBar"));
0198         KToolBar *otherToolBar = kmw.toolBar(QStringLiteral("otherToolBar"));
0199 
0200         // Default settings (applied by applyAppearanceSettings)
0201         QCOMPARE(mainToolBar->iconSize().width(), KIconLoader::global()->currentSize(KIconLoader::MainToolbar));
0202         QCOMPARE(otherToolBar->iconSize().width(), KIconLoader::global()->currentSize(KIconLoader::Toolbar));
0203         // check the actual values - update this if kicontheme's defaults are changed
0204         QCOMPARE(KIconLoader::global()->currentSize(KIconLoader::MainToolbar), 22);
0205         QCOMPARE(KIconLoader::global()->currentSize(KIconLoader::Toolbar), 22);
0206 
0207         // Changing settings for a given toolbar, as user
0208         mainToolBar->setIconDimensions(iconSize);
0209         otherToolBar->setIconDimensions(iconSize);
0210 
0211         // Save settings
0212         kmw.saveMainWindowSettings(group);
0213         // was it the default value?
0214         if (iconSize == KIconLoader::global()->currentSize(KIconLoader::MainToolbar)) {
0215             QCOMPARE(group.groupList().count(), 0); // nothing to save
0216             QVERIFY(!group.group(QStringLiteral("Toolbar mainToolBar")).hasKey("IconSize"));
0217         } else {
0218             QCOMPARE(group.groupList().count(), 2); // two subgroups (one for each toolbar)
0219             QVERIFY(group.group(QStringLiteral("Toolbar mainToolBar")).hasKey("IconSize"));
0220         }
0221     }
0222 
0223     {
0224         // Recreate, load, compare.
0225         KMainWindow kmw;
0226         KToolBar *mainToolBar = kmw.toolBar(QStringLiteral("mainToolBar"));
0227         KToolBar *otherToolBar = kmw.toolBar(QStringLiteral("otherToolBar"));
0228         KToolBar *cleanToolBar = kmw.toolBar(QStringLiteral("cleanToolBar"));
0229         QCOMPARE(mainToolBar->iconSize().width(), KIconLoader::global()->currentSize(KIconLoader::MainToolbar));
0230         QCOMPARE(otherToolBar->iconSize().width(), KIconLoader::global()->currentSize(KIconLoader::Toolbar));
0231         QCOMPARE(cleanToolBar->iconSize().width(), KIconLoader::global()->currentSize(KIconLoader::Toolbar));
0232         kmw.applyMainWindowSettings(group);
0233         QCOMPARE(mainToolBar->iconSize().width(), iconSize);
0234         QCOMPARE(otherToolBar->iconSize().width(), iconSize);
0235         QCOMPARE(cleanToolBar->iconSize().width(), KIconLoader::global()->currentSize(KIconLoader::Toolbar)); // unchanged
0236     }
0237 }
0238 
0239 void tst_KToolBar::testIconSizeXmlGui_data()
0240 {
0241     QTest::addColumn<int>("iconSize"); // set by user and saved to KConfig
0242     // When the user chose a specific size for the toolbar (!= its default size), the new kde-global size isn't applied to that toolbar.
0243     // So, only in case the toolbar was at iconSize already, and there was no setting in xml, we end up with kdeGlobal being used:
0244     QTest::newRow("16") << 16;
0245     QTest::newRow("22") << 22;
0246     QTest::newRow("32") << 32;
0247     QTest::newRow("64") << 64;
0248 }
0249 
0250 void tst_KToolBar::testIconSizeXmlGui()
0251 {
0252     QFETCH(int, iconSize);
0253     KConfig config(QStringLiteral("tst_KToolBar"));
0254     KConfigGroup group(&config, QStringLiteral("group"));
0255     {
0256         TestXmlGuiWindow kmw(m_xml, "tst_ktoolbar.rc");
0257         kmw.createActions(QStringList() << QStringLiteral("go_up"));
0258         kmw.createGUI();
0259         KToolBar *mainToolBar = kmw.toolBarByName(QStringLiteral("mainToolBar"));
0260         KToolBar *otherToolBar = kmw.toolBarByName(QStringLiteral("otherToolBar"));
0261         KToolBar *cleanToolBar = kmw.toolBarByName(QStringLiteral("cleanToolBar"));
0262         KToolBar *bigToolBar = kmw.toolBarByName(QStringLiteral("bigToolBar"));
0263         KToolBar *bigUnchangedToolBar = kmw.toolBarByName(QStringLiteral("bigUnchangedToolBar"));
0264 
0265         // Default settings (applied by applyAppearanceSettings)
0266         QCOMPARE(mainToolBar->iconSize().width(), KIconLoader::global()->currentSize(KIconLoader::MainToolbar));
0267         QCOMPARE(otherToolBar->iconSize().width(), KIconLoader::global()->currentSize(KIconLoader::Toolbar));
0268         // check the actual values - update this if kicontheme's defaults are changed
0269         QCOMPARE(mainToolBar->iconSize().width(), 22);
0270         QCOMPARE(otherToolBar->iconSize().width(), 22);
0271         QCOMPARE(cleanToolBar->iconSize().width(), 22);
0272         QCOMPARE(bigToolBar->iconSize().width(), 32);
0273         QCOMPARE(bigUnchangedToolBar->iconSize().width(), 32);
0274 
0275         // Changing settings for a given toolbar, as user (to test the initial report in #168480)
0276         mainToolBar->setIconDimensions(iconSize);
0277         otherToolBar->setIconDimensions(iconSize);
0278         bigToolBar->setIconDimensions(iconSize);
0279 
0280         // Save settings
0281         kmw.saveMainWindowSettings(group);
0282         // was it the default size? (for the main toolbar, we only check that one)
0283         const bool usingDefaultSize = iconSize == KIconLoader::global()->currentSize(KIconLoader::MainToolbar);
0284         if (usingDefaultSize) {
0285             QVERIFY(!group.groupList().contains(QLatin1String("Toolbar mainToolBar")));
0286             QVERIFY(!group.group(QStringLiteral("Toolbar mainToolBar")).hasKey("IconSize"));
0287         } else {
0288             QVERIFY(group.group(QStringLiteral("Toolbar mainToolBar")).hasKey("IconSize"));
0289         }
0290     }
0291 }
0292 
0293 Q_DECLARE_METATYPE(Qt::ToolButtonStyle)
0294 
0295 void tst_KToolBar::testToolButtonStyleNoXmlGui_data()
0296 {
0297     QTest::addColumn<Qt::ToolButtonStyle>("toolButtonStyle");
0298     QTest::newRow("Qt::ToolButtonIconOnly") << Qt::ToolButtonIconOnly;
0299     QTest::newRow("Qt::ToolButtonTextOnly") << Qt::ToolButtonTextOnly;
0300     QTest::newRow("Qt::ToolButtonTextBesideIcon") << Qt::ToolButtonTextBesideIcon;
0301     QTest::newRow("Qt::ToolButtonTextUnderIcon") << Qt::ToolButtonTextUnderIcon;
0302 }
0303 
0304 void tst_KToolBar::testToolButtonStyleNoXmlGui()
0305 {
0306     QFETCH(Qt::ToolButtonStyle, toolButtonStyle);
0307     const Qt::ToolButtonStyle mainToolBarDefaultStyle = Qt::ToolButtonTextBesideIcon; // was TextUnderIcon before KDE-4.4.0
0308     const bool selectedDefaultForMainToolbar = toolButtonStyle == mainToolBarDefaultStyle;
0309     const bool selectedDefaultForOtherToolbar = toolButtonStyle == Qt::ToolButtonTextBesideIcon;
0310 
0311     KConfig config(QStringLiteral("tst_KToolBar"));
0312     KConfigGroup group(&config, QStringLiteral("group"));
0313     {
0314         KMainWindow kmw;
0315         KToolBar *mainToolBar = kmw.toolBar(QStringLiteral("mainToolBar"));
0316         KToolBar *otherToolBar = kmw.toolBar(QStringLiteral("otherToolBar"));
0317 
0318         // Default settings (applied by applyAppearanceSettings)
0319         QCOMPARE((int)mainToolBar->toolButtonStyle(), (int)mainToolBarDefaultStyle);
0320         QCOMPARE((int)otherToolBar->toolButtonStyle(), (int)Qt::ToolButtonTextBesideIcon); // see r883541
0321         QCOMPARE(kmw.toolBarArea(mainToolBar), Qt::TopToolBarArea);
0322 
0323         // Changing settings for a given toolbar, as user
0324         mainToolBar->setToolButtonStyle(toolButtonStyle);
0325         otherToolBar->setToolButtonStyle(toolButtonStyle);
0326 
0327         // Save settings
0328         kmw.saveMainWindowSettings(group);
0329         if (selectedDefaultForMainToolbar) {
0330             QCOMPARE(group.groupList().count(), 0); // nothing to save
0331             QVERIFY(!group.group(QStringLiteral("Toolbar mainToolBar")).hasKey("ToolButtonStyle"));
0332         } else {
0333             QCOMPARE(group.groupList().count(), 2); // two subgroups (one for each toolbar)
0334             QVERIFY(group.group(QStringLiteral("Toolbar mainToolBar")).hasKey("ToolButtonStyle"));
0335         }
0336     }
0337 
0338     {
0339         // Recreate, load, compare.
0340         KMainWindow kmw;
0341         KToolBar *mainToolBar = kmw.toolBar(QStringLiteral("mainToolBar"));
0342         KToolBar *otherToolBar = kmw.toolBar(QStringLiteral("otherToolBar"));
0343         QCOMPARE((int)mainToolBar->toolButtonStyle(), (int)mainToolBarDefaultStyle);
0344         kmw.applyMainWindowSettings(group);
0345         QCOMPARE((int)mainToolBar->toolButtonStyle(), (int)toolButtonStyle);
0346         QCOMPARE((int)otherToolBar->toolButtonStyle(), (int)toolButtonStyle);
0347 
0348 #ifndef Q_OS_WIN // the change notification uses DBus
0349         // Now change KDE-global setting
0350         changeGlobalToolButtonStyleSetting(QStringLiteral("IconOnly"), QStringLiteral("TextOnly"));
0351 
0352         if (selectedDefaultForMainToolbar) {
0353             QCOMPARE((int)mainToolBar->toolButtonStyle(), (int)Qt::ToolButtonIconOnly);
0354         } else {
0355             QCOMPARE((int)mainToolBar->toolButtonStyle(), (int)toolButtonStyle);
0356         }
0357 
0358         if (selectedDefaultForOtherToolbar) {
0359             QCOMPARE((int)otherToolBar->toolButtonStyle(), (int)Qt::ToolButtonTextOnly);
0360         } else {
0361             QCOMPARE((int)otherToolBar->toolButtonStyle(), (int)toolButtonStyle);
0362         }
0363 #endif
0364     }
0365 }
0366 
0367 void tst_KToolBar::testToolButtonStyleXmlGui_data()
0368 {
0369     QTest::addColumn<Qt::ToolButtonStyle>("toolButtonStyle");
0370     // Expected style after KDE-global is changed to main=IconOnly/other=TextOnly
0371     QTest::addColumn<Qt::ToolButtonStyle>("expectedStyleMainToolbar");
0372     QTest::addColumn<Qt::ToolButtonStyle>("expectedStyleOtherToolbar"); // xml says text-under-icons, user-selected should always win
0373     QTest::addColumn<Qt::ToolButtonStyle>("expectedStyleCleanToolbar"); // should always follow kde-global -> always textonly.
0374 
0375     QTest::newRow("Qt::ToolButtonTextUnderIcon") << Qt::ToolButtonTextUnderIcon << Qt::ToolButtonTextUnderIcon << Qt::ToolButtonTextUnderIcon
0376                                                  << Qt::ToolButtonTextOnly;
0377     QTest::newRow("Qt::ToolButtonTextBesideIcon") << Qt::ToolButtonTextBesideIcon
0378                                                   << Qt::ToolButtonIconOnly /* was default -> using kde global */ << Qt::ToolButtonTextBesideIcon
0379                                                   << Qt::ToolButtonTextOnly;
0380     QTest::newRow("Qt::ToolButtonIconOnly") << Qt::ToolButtonIconOnly << Qt::ToolButtonIconOnly << Qt::ToolButtonIconOnly << Qt::ToolButtonTextOnly;
0381     QTest::newRow("Qt::ToolButtonTextOnly") << Qt::ToolButtonTextOnly << Qt::ToolButtonTextOnly << Qt::ToolButtonTextOnly << Qt::ToolButtonTextOnly;
0382 }
0383 
0384 void tst_KToolBar::testToolButtonStyleXmlGui()
0385 {
0386     QFETCH(Qt::ToolButtonStyle, toolButtonStyle);
0387     QFETCH(Qt::ToolButtonStyle, expectedStyleMainToolbar);
0388     QFETCH(Qt::ToolButtonStyle, expectedStyleOtherToolbar);
0389     QFETCH(Qt::ToolButtonStyle, expectedStyleCleanToolbar);
0390     const Qt::ToolButtonStyle mainToolBarDefaultStyle = Qt::ToolButtonTextBesideIcon; // was TextUnderIcon before KDE-4.4.0
0391     KConfig config(QStringLiteral("tst_KToolBar"));
0392     KConfigGroup group(&config, QStringLiteral("group"));
0393     {
0394         TestXmlGuiWindow kmw(m_xml, "tst_ktoolbar.rc");
0395         kmw.createActions(QStringList() << QStringLiteral("go_up"));
0396         kmw.createGUI();
0397         KToolBar *mainToolBar = kmw.toolBarByName(QStringLiteral("mainToolBar"));
0398         KToolBar *otherToolBar = kmw.toolBarByName(QStringLiteral("otherToolBar"));
0399         KToolBar *cleanToolBar = kmw.toolBarByName(QStringLiteral("cleanToolBar"));
0400 
0401         QCOMPARE((int)mainToolBar->toolButtonStyle(), (int)mainToolBarDefaultStyle);
0402         QCOMPARE((int)otherToolBar->toolButtonStyle(), (int)Qt::ToolButtonTextUnderIcon); // from xml
0403         QCOMPARE((int)cleanToolBar->toolButtonStyle(), (int)Qt::ToolButtonTextBesideIcon);
0404 
0405         // Changing settings for a given toolbar, as user
0406         mainToolBar->setToolButtonStyle(toolButtonStyle);
0407         otherToolBar->setToolButtonStyle(toolButtonStyle);
0408 
0409         // Save settings
0410         kmw.saveMainWindowSettings(group);
0411 
0412 #ifndef Q_OS_WIN // the change notification uses DBus
0413         // Now change KDE-global setting
0414         changeGlobalToolButtonStyleSetting(QStringLiteral("IconOnly"), QStringLiteral("TextOnly"));
0415 
0416         QCOMPARE((int)mainToolBar->toolButtonStyle(), (int)expectedStyleMainToolbar);
0417         QCOMPARE((int)otherToolBar->toolButtonStyle(), (int)expectedStyleOtherToolbar);
0418         QCOMPARE((int)cleanToolBar->toolButtonStyle(), (int)expectedStyleCleanToolbar);
0419 #endif
0420     }
0421 }
0422 
0423 void tst_KToolBar::changeGlobalToolButtonStyleSetting(const QString &mainToolBar, const QString &otherToolBars)
0424 {
0425     KConfigGroup group(KSharedConfig::openConfig(), QStringLiteral("Toolbar style"));
0426     group.writeEntry("ToolButtonStyle", mainToolBar);
0427     group.writeEntry("ToolButtonStyleOtherToolbars", otherToolBars);
0428     group.sync();
0429 
0430     // Same dbus connect as the one in KToolBar. We want our spy to be notified of receiving it.
0431 #ifdef QT_DBUS_LIB
0432     QDBusConnection::sessionBus().connect(QString(),
0433                                           QStringLiteral("/KToolBar"),
0434                                           QStringLiteral("org.kde.KToolBar"),
0435                                           QStringLiteral("styleChanged"),
0436                                           this,
0437                                           SIGNAL(signalAppearanceChanged()));
0438     QSignalSpy spy(this, &tst_KToolBar::signalAppearanceChanged);
0439 
0440     KToolBar::emitToolbarStyleChanged();
0441     spy.wait(2000);
0442 #endif
0443 }
0444 
0445 void tst_KToolBar::deleteGlobalToolButtonStyleSetting()
0446 {
0447     KConfigGroup group(KSharedConfig::openConfig(), QStringLiteral("Toolbar style"));
0448     group.deleteEntry("ToolButtonStyle");
0449     group.deleteEntry("ToolButtonStyleOtherToolbars");
0450     KSharedConfig::openConfig()->sync();
0451 }
0452 
0453 void tst_KToolBar::testToolBarPosition()
0454 {
0455     TestXmlGuiWindow kmw(m_xml, "tst_ktoolbar.rc");
0456     kmw.createActions(QStringList() << QStringLiteral("go_up"));
0457     kmw.createGUI();
0458     KToolBar *mainToolBar = kmw.toolBarByName(QStringLiteral("mainToolBar"));
0459     KToolBar *otherToolBar = kmw.toolBarByName(QStringLiteral("otherToolBar"));
0460     QCOMPARE(kmw.toolBarArea(mainToolBar), Qt::TopToolBarArea);
0461     QCOMPARE(kmw.toolBarArea(otherToolBar), Qt::BottomToolBarArea);
0462 }
0463 
0464 void tst_KToolBar::testXmlGuiSwitching()
0465 {
0466     const QByteArray windowXml =
0467         "<?xml version = '1.0'?>\n"
0468         "<!DOCTYPE gui SYSTEM \"kpartgui.dtd\">\n"
0469         "<gui version=\"1\" name=\"foo\" >\n"
0470         "<MenuBar>\n"
0471         "</MenuBar>\n"
0472         "</gui>\n";
0473     TestXmlGuiWindow kmw(windowXml, "tst_ktoolbar.rc");
0474     kmw.createActions(QStringList() << QStringLiteral("go_up"));
0475     kmw.createGUI();
0476     TestGuiClient firstClient(m_xml);
0477     kmw.guiFactory()->addClient(&firstClient);
0478 
0479     {
0480         // qDebug() << "Added gui client";
0481         KToolBar *mainToolBar = firstClient.toolBarByName(QStringLiteral("mainToolBar"));
0482         KToolBar *otherToolBar = firstClient.toolBarByName(QStringLiteral("otherToolBar"));
0483         KToolBar *bigToolBar = firstClient.toolBarByName(QStringLiteral("bigToolBar"));
0484         KToolBar *hiddenToolBar = firstClient.toolBarByName(QStringLiteral("hiddenToolBar"));
0485         KToolBar *secondHiddenToolBar = firstClient.toolBarByName(QStringLiteral("secondHiddenToolBar"));
0486         QCOMPARE(hiddenToolBar->isHidden(), true);
0487         QCOMPARE(secondHiddenToolBar->isHidden(), true);
0488         // Make (unsaved) changes as user
0489         QMetaObject::invokeMethod(mainToolBar, "slotContextTextRight"); // mainToolBar->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
0490         QMetaObject::invokeMethod(mainToolBar, "slotContextRight"); // kmw.addToolBar(Qt::RightToolBarArea, mainToolBar);
0491         otherToolBar->setIconDimensions(35);
0492         bigToolBar->setIconDimensions(35);
0493         bigToolBar->hide();
0494         hiddenToolBar->show();
0495     }
0496     kmw.guiFactory()->removeClient(&firstClient);
0497     // qDebug() << "Removed gui client";
0498     QVERIFY(!kmw.guiFactory()->container(QStringLiteral("mainToolBar"), &kmw));
0499     QVERIFY(!kmw.guiFactory()->container(QStringLiteral("otherToolBar"), &kmw));
0500     QVERIFY(!kmw.guiFactory()->container(QStringLiteral("bigToolBar"), &kmw));
0501     QVERIFY(!kmw.guiFactory()->container(QStringLiteral("mainToolBar"), &firstClient));
0502     QVERIFY(!kmw.guiFactory()->container(QStringLiteral("otherToolBar"), &firstClient));
0503     QVERIFY(!kmw.guiFactory()->container(QStringLiteral("bigToolBar"), &firstClient));
0504 
0505     kmw.guiFactory()->addClient(&firstClient);
0506     // qDebug() << "Re-added gui client";
0507     KToolBar *mainToolBar = firstClient.toolBarByName(QStringLiteral("mainToolBar"));
0508     KToolBar *otherToolBar = firstClient.toolBarByName(QStringLiteral("otherToolBar"));
0509     KToolBar *bigToolBar = firstClient.toolBarByName(QStringLiteral("bigToolBar"));
0510     KToolBar *cleanToolBar = firstClient.toolBarByName(QStringLiteral("cleanToolBar"));
0511     KToolBar *hiddenToolBar = firstClient.toolBarByName(QStringLiteral("hiddenToolBar"));
0512     KToolBar *secondHiddenToolBar = firstClient.toolBarByName(QStringLiteral("secondHiddenToolBar"));
0513     QCOMPARE((int)mainToolBar->toolButtonStyle(), (int)Qt::ToolButtonTextBesideIcon);
0514     QCOMPARE(mainToolBar->isHidden(), false);
0515     QCOMPARE(kmw.toolBarArea(mainToolBar), Qt::RightToolBarArea);
0516     QCOMPARE(mainToolBar->iconSize().width(), KIconLoader::global()->currentSize(KIconLoader::MainToolbar));
0517     QCOMPARE(otherToolBar->iconSize().width(), 35);
0518     QCOMPARE(bigToolBar->iconSize().width(), 35);
0519     QCOMPARE(cleanToolBar->iconSize().width(), KIconLoader::global()->currentSize(KIconLoader::Toolbar));
0520     QCOMPARE(bigToolBar->isHidden(), true);
0521     QCOMPARE(hiddenToolBar->isHidden(), false);
0522     QCOMPARE(secondHiddenToolBar->isHidden(), true);
0523 
0524     // Now save, and check what we saved
0525     KConfig config(QStringLiteral("tst_KToolBar"));
0526     KConfigGroup group(&config, QStringLiteral("group"));
0527     kmw.saveMainWindowSettings(group);
0528     QCOMPARE(group.group(QStringLiteral("Toolbar bigToolBar")).readEntry("IconSize", 0), 35);
0529     QCOMPARE(group.group(QStringLiteral("Toolbar otherToolBar")).readEntry("IconSize", 0), 35);
0530     QVERIFY(!group.group(QStringLiteral("Toolbar cleanToolBar")).hasKey("IconSize"));
0531     // QCOMPARE(group.group("Toolbar bigToolBar").readEntry("Hidden", false), true);
0532     // QVERIFY(!group.group("Toolbar cleanToolBar").hasKey("Hidden"));
0533     // QVERIFY(!group.group("Toolbar hiddenToolBar").hasKey("Hidden"));
0534 
0535     // Recreate window and apply config; is hidden toolbar shown as expected?
0536     {
0537         TestXmlGuiWindow kmw2(windowXml, "tst_ktoolbar.rc");
0538         kmw2.createActions(QStringList() << QStringLiteral("go_up"));
0539         kmw2.createGUI();
0540         TestGuiClient firstClient(m_xml);
0541         kmw2.guiFactory()->addClient(&firstClient);
0542 
0543         KToolBar *mainToolBar = firstClient.toolBarByName(QStringLiteral("mainToolBar"));
0544         KToolBar *otherToolBar = firstClient.toolBarByName(QStringLiteral("otherToolBar"));
0545         KToolBar *bigToolBar = firstClient.toolBarByName(QStringLiteral("bigToolBar"));
0546         KToolBar *hiddenToolBar = firstClient.toolBarByName(QStringLiteral("hiddenToolBar"));
0547         KToolBar *secondHiddenToolBar = firstClient.toolBarByName(QStringLiteral("secondHiddenToolBar"));
0548         QCOMPARE(bigToolBar->isHidden(), false);
0549         QCOMPARE(hiddenToolBar->isHidden(), true);
0550         QCOMPARE(secondHiddenToolBar->isHidden(), true);
0551 
0552         kmw2.show();
0553 
0554         // Check that secondHiddenToolBar is not shown+hidden immediately?
0555         m_showWasCalled = false;
0556         secondHiddenToolBar->installEventFilter(this);
0557         kmw2.applyMainWindowSettings(group);
0558 
0559         QCOMPARE(mainToolBar->isHidden(), false);
0560         QCOMPARE(kmw2.toolBarArea(mainToolBar), Qt::RightToolBarArea);
0561         QCOMPARE(otherToolBar->iconSize().width(), 35);
0562         QCOMPARE(bigToolBar->iconSize().width(), 35);
0563         QCOMPARE(bigToolBar->isHidden(), true);
0564         QCOMPARE(hiddenToolBar->isHidden(), false);
0565         QCOMPARE(secondHiddenToolBar->isHidden(), true);
0566         QVERIFY(!m_showWasCalled);
0567     }
0568 }
0569 
0570 void tst_KToolBar::testKAuthorizedDisableToggleAction()
0571 {
0572     TestXmlGuiWindow kmw(m_xml, "tst_ktoolbar.rc");
0573     kmw.createGUI();
0574 
0575     const auto toolbars = kmw.toolBars();
0576     for (KToolBar *toolbar : toolbars) {
0577         QVERIFY(!toolbar->toggleViewAction()->isEnabled());
0578     }
0579 }
0580 
0581 bool tst_KToolBar::eventFilter(QObject *watched, QEvent *event)
0582 {
0583     Q_UNUSED(watched);
0584     if (event->type() == QEvent::Show) {
0585         m_showWasCalled = true;
0586         return true;
0587     }
0588     return false;
0589 }
0590 
0591 #include "ktoolbar_unittest.moc"