File indexing completed on 2024-05-12 16:15:57
0001 /* 0002 SPDX-FileCopyrightText: 2014-2023 Laurent Montel <montel@kde.org> 0003 0004 SPDX-License-Identifier: LGPL-2.0-or-later 0005 */ 0006 0007 #include "texttospeechwidgettest.h" 0008 #include "texttospeechinterface.h" 0009 #include "texttospeechsliderwidget.h" 0010 #include "texttospeechwidget.h" 0011 #include <QHBoxLayout> 0012 #include <QSignalSpy> 0013 #include <QTest> 0014 #include <QToolButton> 0015 #include <qtestmouse.h> 0016 0017 Q_DECLARE_METATYPE(TextEditTextToSpeech::TextToSpeechWidget::State) 0018 QTEST_MAIN(TextToSpeechWidgetTest) 0019 TextToSpeechWidgetTest::TextToSpeechWidgetTest(QObject *parent) 0020 : QObject(parent) 0021 { 0022 qRegisterMetaType<TextEditTextToSpeech::TextToSpeechWidget::State>(); 0023 QIcon::setThemeName(QStringLiteral("breeze")); 0024 } 0025 0026 void TextToSpeechWidgetTest::addInterface(TextEditTextToSpeech::TextToSpeechWidget *widget) 0027 { 0028 auto interface = new TextEditTextToSpeech::TextToSpeechInterface(widget, this); 0029 widget->setTextToSpeechInterface(interface); 0030 } 0031 0032 void TextToSpeechWidgetTest::shouldHaveDefaultValue() 0033 { 0034 TextEditTextToSpeech::TextToSpeechWidget textToSpeechWidget; 0035 addInterface(&textToSpeechWidget); 0036 0037 auto hbox = textToSpeechWidget.findChild<QHBoxLayout *>(QStringLiteral("hbox")); 0038 QVERIFY(hbox); 0039 QCOMPARE(hbox->contentsMargins(), QMargins{}); 0040 0041 QCOMPARE(textToSpeechWidget.state(), TextEditTextToSpeech::TextToSpeechWidget::Stop); 0042 0043 auto closeButton = textToSpeechWidget.findChild<QToolButton *>(QStringLiteral("close-button")); 0044 QVERIFY(closeButton); 0045 0046 auto stopButton = textToSpeechWidget.findChild<QToolButton *>(QStringLiteral("stopbutton")); 0047 QVERIFY(stopButton); 0048 QVERIFY(stopButton->isEnabled()); 0049 QVERIFY(!stopButton->icon().isNull()); 0050 0051 auto playPauseButton = textToSpeechWidget.findChild<QToolButton *>(QStringLiteral("playpausebutton")); 0052 QVERIFY(playPauseButton); 0053 QVERIFY(!playPauseButton->isEnabled()); 0054 QVERIFY(!playPauseButton->icon().isNull()); 0055 0056 auto volume = textToSpeechWidget.findChild<TextEditTextToSpeech::TextToSpeechSliderWidget *>(QStringLiteral("volumeslider")); 0057 QVERIFY(volume); 0058 0059 auto configureButton = textToSpeechWidget.findChild<QToolButton *>(QStringLiteral("configurebutton")); 0060 QVERIFY(configureButton); 0061 QVERIFY(!configureButton->icon().isNull()); 0062 } 0063 0064 void TextToSpeechWidgetTest::shouldChangeButtonEnableStateWhenChangeState() 0065 { 0066 TextEditTextToSpeech::TextToSpeechWidget textToSpeechWidget; 0067 addInterface(&textToSpeechWidget); 0068 textToSpeechWidget.setState(TextEditTextToSpeech::TextToSpeechWidget::Play); 0069 0070 auto stopButton = textToSpeechWidget.findChild<QToolButton *>(QStringLiteral("stopbutton")); 0071 QVERIFY(stopButton->isEnabled()); 0072 0073 auto playPauseButton = textToSpeechWidget.findChild<QToolButton *>(QStringLiteral("playpausebutton")); 0074 QVERIFY(playPauseButton->isEnabled()); 0075 0076 textToSpeechWidget.setState(TextEditTextToSpeech::TextToSpeechWidget::Pause); 0077 QVERIFY(stopButton->isEnabled()); 0078 QVERIFY(playPauseButton->isEnabled()); 0079 0080 textToSpeechWidget.setState(TextEditTextToSpeech::TextToSpeechWidget::Stop); 0081 QVERIFY(stopButton->isEnabled()); 0082 QVERIFY(!playPauseButton->isEnabled()); 0083 } 0084 0085 void TextToSpeechWidgetTest::shouldChangeStateWhenClickOnPlayPause() 0086 { 0087 TextEditTextToSpeech::TextToSpeechWidget textToSpeechWidget; 0088 addInterface(&textToSpeechWidget); 0089 0090 textToSpeechWidget.setState(TextEditTextToSpeech::TextToSpeechWidget::Play); 0091 auto playPauseButton = textToSpeechWidget.findChild<QToolButton *>(QStringLiteral("playpausebutton")); 0092 QCOMPARE(textToSpeechWidget.state(), TextEditTextToSpeech::TextToSpeechWidget::Play); 0093 0094 QTest::mouseClick(playPauseButton, Qt::LeftButton); 0095 QCOMPARE(textToSpeechWidget.state(), TextEditTextToSpeech::TextToSpeechWidget::Pause); 0096 0097 QTest::mouseClick(playPauseButton, Qt::LeftButton); 0098 QCOMPARE(textToSpeechWidget.state(), TextEditTextToSpeech::TextToSpeechWidget::Play); 0099 } 0100 0101 void TextToSpeechWidgetTest::shouldChangeStateWhenClickOnStop() 0102 { 0103 TextEditTextToSpeech::TextToSpeechWidget textToSpeechWidget; 0104 addInterface(&textToSpeechWidget); 0105 textToSpeechWidget.setState(TextEditTextToSpeech::TextToSpeechWidget::Play); 0106 0107 auto stopButton = textToSpeechWidget.findChild<QToolButton *>(QStringLiteral("stopbutton")); 0108 QTest::mouseClick(stopButton, Qt::LeftButton); 0109 QCOMPARE(textToSpeechWidget.state(), TextEditTextToSpeech::TextToSpeechWidget::Stop); 0110 } 0111 0112 void TextToSpeechWidgetTest::shouldEmitStateChanged() 0113 { 0114 TextEditTextToSpeech::TextToSpeechWidget textToSpeechWidget; 0115 addInterface(&textToSpeechWidget); 0116 QSignalSpy spy(&textToSpeechWidget, &TextEditTextToSpeech::TextToSpeechWidget::stateChanged); 0117 textToSpeechWidget.setState(TextEditTextToSpeech::TextToSpeechWidget::Play); 0118 QCOMPARE(spy.count(), 0); 0119 0120 auto stopButton = textToSpeechWidget.findChild<QToolButton *>(QStringLiteral("stopbutton")); 0121 0122 auto playPauseButton = textToSpeechWidget.findChild<QToolButton *>(QStringLiteral("playpausebutton")); 0123 0124 QTest::mouseClick(playPauseButton, Qt::LeftButton); 0125 QCOMPARE(spy.count(), 1); 0126 QCOMPARE(spy.at(0).at(0).value<TextEditTextToSpeech::TextToSpeechWidget::State>(), TextEditTextToSpeech::TextToSpeechWidget::Pause); 0127 QTest::mouseClick(playPauseButton, Qt::LeftButton); 0128 QCOMPARE(spy.count(), 2); 0129 QCOMPARE(spy.at(1).at(0).value<TextEditTextToSpeech::TextToSpeechWidget::State>(), TextEditTextToSpeech::TextToSpeechWidget::Play); 0130 QTest::mouseClick(playPauseButton, Qt::LeftButton); 0131 QCOMPARE(spy.count(), 3); 0132 QCOMPARE(spy.at(2).at(0).value<TextEditTextToSpeech::TextToSpeechWidget::State>(), TextEditTextToSpeech::TextToSpeechWidget::Pause); 0133 QTest::mouseClick(stopButton, Qt::LeftButton); 0134 QCOMPARE(spy.count(), 4); 0135 QCOMPARE(spy.at(3).at(0).value<TextEditTextToSpeech::TextToSpeechWidget::State>(), TextEditTextToSpeech::TextToSpeechWidget::Stop); 0136 } 0137 0138 #include "moc_texttospeechwidgettest.cpp"