File indexing completed on 2024-12-22 05:00:58

0001 /*
0002   SPDX-FileCopyrightText: 2014-2024 Laurent Montel <montel@kde.org>
0003 
0004   SPDX-License-Identifier: GPL-2.0-only
0005 */
0006 
0007 #include "displaymessageformatactionmenutest.h"
0008 #include "../widgets/displaymessageformatactionmenu.h"
0009 #include <KToggleAction>
0010 #include <QMenu>
0011 #include <QSignalSpy>
0012 #include <QTest>
0013 #include <qtestmouse.h>
0014 Q_DECLARE_METATYPE(MessageViewer::Viewer::DisplayFormatMessage)
0015 DisplayMessageFormatActionMenuTest::DisplayMessageFormatActionMenuTest(QObject *parent)
0016     : QObject(parent)
0017 {
0018     qRegisterMetaType<MessageViewer::Viewer::DisplayFormatMessage>();
0019 }
0020 
0021 void DisplayMessageFormatActionMenuTest::shouldHaveDefaultValue()
0022 {
0023     DisplayMessageFormatActionMenu menu;
0024     QVERIFY(menu.menu());
0025     auto prefereHtml = menu.findChild<KToggleAction *>(QStringLiteral("prefer-html-action"));
0026     QVERIFY(prefereHtml);
0027     auto prefereText = menu.findChild<KToggleAction *>(QStringLiteral("prefer-text-action"));
0028     QVERIFY(prefereText);
0029     auto useGlobalSetting = menu.findChild<KToggleAction *>(QStringLiteral("use-global-setting-action"));
0030     QVERIFY(useGlobalSetting);
0031     QCOMPARE(useGlobalSetting->isChecked(), true);
0032     QCOMPARE(menu.menu()->actions().count(), 3);
0033 }
0034 
0035 void DisplayMessageFormatActionMenuTest::shouldEmitSignalWhenClickOnSubMenu()
0036 {
0037     DisplayMessageFormatActionMenu menu;
0038     auto prefereHtml = menu.findChild<KToggleAction *>(QStringLiteral("prefer-html-action"));
0039     QSignalSpy spy(&menu, &DisplayMessageFormatActionMenu::changeDisplayMessageFormat);
0040     prefereHtml->trigger();
0041     QCOMPARE(spy.count(), 1);
0042     QCOMPARE(spy.at(0).at(0).value<MessageViewer::Viewer::DisplayFormatMessage>(), MessageViewer::Viewer::Html);
0043 }
0044 
0045 void DisplayMessageFormatActionMenuTest::shouldSelectItemWhenChangeFormat()
0046 {
0047     DisplayMessageFormatActionMenu menu;
0048     auto useGlobalSetting = menu.findChild<KToggleAction *>(QStringLiteral("use-global-setting-action"));
0049     QCOMPARE(useGlobalSetting->isChecked(), true);
0050     menu.setDisplayMessageFormat(MessageViewer::Viewer::Text);
0051     auto prefereText = menu.findChild<KToggleAction *>(QStringLiteral("prefer-text-action"));
0052     QCOMPARE(prefereText->isChecked(), true);
0053     auto prefereHtml = menu.findChild<KToggleAction *>(QStringLiteral("prefer-html-action"));
0054     QCOMPARE(prefereHtml->isChecked(), false);
0055     QCOMPARE(useGlobalSetting->isChecked(), false);
0056 }
0057 
0058 void DisplayMessageFormatActionMenuTest::shouldDontEmitSignalWhenChangeFormat()
0059 {
0060     DisplayMessageFormatActionMenu menu;
0061     QSignalSpy spy(&menu, &DisplayMessageFormatActionMenu::changeDisplayMessageFormat);
0062     menu.setDisplayMessageFormat(MessageViewer::Viewer::Text);
0063     QCOMPARE(spy.count(), 0);
0064 }
0065 
0066 QTEST_MAIN(DisplayMessageFormatActionMenuTest)
0067 
0068 #include "moc_displaymessageformatactionmenutest.cpp"