File indexing completed on 2024-05-12 05:42:41

0001 #include <cutehmi/Message.hpp>
0002 #include <cutehmi/Messenger.hpp>
0003 
0004 #include <cutehmi/test/random.hpp>
0005 
0006 #include <QtTest/QtTest>
0007 #include <QSignalSpy>
0008 
0009 namespace cutehmi {
0010 
0011 class test_Message:
0012     public QObject
0013 {
0014         Q_OBJECT
0015 
0016     private slots:
0017         void initTestCase();
0018 
0019         void properties();
0020 
0021         void response();
0022 
0023         void clone();
0024 
0025         void Info();
0026 
0027         void Warning();
0028 
0029         void Question();
0030 
0031         void Critical();
0032 };
0033 
0034 void test_Message::initTestCase()
0035 {
0036 }
0037 
0038 void test_Message::properties()
0039 {
0040     Message message(Message::WARNING, "Text.", "Informative text.", "Detailed text.", Message::BUTTON_ABORT);
0041 
0042     QSignalSpy typeSpy(& message, & Message::typeChanged);
0043     QCOMPARE(message.type(), Message::WARNING);
0044     message.setType(Message::INFO);
0045     QCOMPARE(message.type(), Message::INFO);
0046     QCOMPARE(typeSpy.count(), 1);
0047 
0048     QSignalSpy textSpy(& message, & Message::textChanged);
0049     QCOMPARE(message.text(), "Text.");
0050     message.setText("Modified text.");
0051     QCOMPARE(message.text(), "Modified text.");
0052     QCOMPARE(textSpy.count(), 1);
0053 
0054     QSignalSpy informativeTextSpy(& message, & Message::informativeTextChanged);
0055     QCOMPARE(message.informativeText(), "Informative text.");
0056     message.setInformativeText("Modified informative text.");
0057     QCOMPARE(message.informativeText(), "Modified informative text.");
0058     QCOMPARE(informativeTextSpy.count(), 1);
0059 
0060     QSignalSpy detailedTextSpy(& message, & Message::detailedTextChanged);
0061     QCOMPARE(message.detailedText(), "Detailed text.");
0062     message.setDetailedText("Modified detailed text.");
0063     QCOMPARE(message.detailedText(), "Modified detailed text.");
0064     QCOMPARE(detailedTextSpy.count(), 1);
0065 
0066     QSignalSpy buttonsSpy(& message, & Message::buttonsChanged);
0067     QCOMPARE(message.buttons(), Message::BUTTON_ABORT);
0068     message.setButtons(Message::BUTTON_APPLY);
0069     QCOMPARE(message.buttons(), Message::BUTTON_APPLY);
0070     QCOMPARE(buttonsSpy.count(), 1);
0071 }
0072 
0073 void test_Message::response()
0074 {
0075     Message message(Message::WARNING, "Text", Message::BUTTON_ABORT | Message::BUTTON_APPLY);
0076     QSignalSpy spy(& message, & Message::responseArrived);
0077 
0078     QCOMPARE(message.response(), Message::NO_BUTTON);
0079 
0080     message.acceptResponse(Message::BUTTON_APPLY);
0081     QCOMPARE(message.response(), Message::BUTTON_APPLY);
0082     QCOMPARE(spy.count(), 1);
0083     QCOMPARE(spy.at(0).at(0).value<Message::Button>(), Message::BUTTON_APPLY);
0084     spy.clear();
0085 
0086     message.acceptResponse(Message::BUTTON_ABORT);
0087     QCOMPARE(message.response(), Message::BUTTON_APPLY);
0088     QCOMPARE(spy.count(), 0);
0089     spy.clear();
0090 
0091     message.acceptResponse(Message::NO_BUTTON);
0092     QCOMPARE(message.response(), Message::BUTTON_APPLY);
0093     QCOMPARE(spy.count(), 0);
0094     spy.clear();
0095 
0096     Message dialogAcceptUnavailable(Message::WARNING, "Text", Message::BUTTON_ABORT | Message::BUTTON_APPLY);
0097     dialogAcceptUnavailable.acceptResponse(Message::BUTTON_CANCEL);
0098     QCOMPARE(dialogAcceptUnavailable.response(), Message::BUTTON_CANCEL);
0099 }
0100 
0101 void test_Message::clone()
0102 {
0103     Message message(Message::WARNING, "Text.", "Informative text.", "Detailed text.", Message::BUTTON_ABORT);
0104     std::unique_ptr<Message> copy = message.clone();
0105 
0106     QCOMPARE(message.type(), copy->type());
0107     QCOMPARE(message.text(), copy->text());
0108     QCOMPARE(message.informativeText(), copy->informativeText());
0109     QCOMPARE(message.detailedText(), copy->detailedText());
0110     QCOMPARE(message.buttons(), copy->buttons());
0111 }
0112 
0113 void test_Message::Info()
0114 {
0115     try {
0116         std::unique_ptr<Message> message = Message::Info("Info.");
0117         QCOMPARE(message->type(), Message::INFO);
0118         QCOMPARE(message->text(), "Info.");
0119         QCOMPARE(message->buttons(), Message::BUTTON_OK);
0120     } catch (const Messenger::NoAdvertiserException & ) {
0121     }
0122 }
0123 
0124 void test_Message::Warning()
0125 {
0126     try {
0127         std::unique_ptr<Message> message = Message::Warning("Warning.");
0128         QCOMPARE(message->type(), Message::WARNING);
0129         QCOMPARE(message->text(), "Warning.");
0130         QCOMPARE(message->buttons(), Message::BUTTON_OK);
0131     } catch (const Messenger::NoAdvertiserException & ) {
0132     }
0133 }
0134 
0135 void test_Message::Question()
0136 {
0137     try {
0138         std::unique_ptr<Message> message = Message::Question("Question?");
0139         QCOMPARE(message->type(), Message::QUESTION);
0140         QCOMPARE(message->text(), "Question?");
0141         QCOMPARE(message->buttons(), Message::BUTTON_YES | Message::BUTTON_NO);
0142     } catch (const Messenger::NoAdvertiserException & ) {
0143     }
0144 }
0145 
0146 void test_Message::Critical()
0147 {
0148     try {
0149         std::unique_ptr<Message> message = Message::Critical("Critical.");
0150         QCOMPARE(message->type(), Message::CRITICAL);
0151         QCOMPARE(message->text(), "Critical.");
0152         QCOMPARE(message->buttons(), Message::BUTTON_OK);
0153     } catch (const Messenger::NoAdvertiserException & ) {
0154     }
0155 
0156     try {
0157         ErrorInfo err{1, "class", "Error."};
0158         std::unique_ptr<Message> message2 = Message::Critical(err);
0159         QCOMPARE(message2->type(), Message::CRITICAL);
0160         QCOMPARE(message2->text(), err.toString());
0161         QCOMPARE(message2->buttons(), Message::BUTTON_OK);
0162     } catch (const Messenger::NoAdvertiserException & ) {
0163     }
0164 }
0165 
0166 }
0167 
0168 QTEST_MAIN(cutehmi::test_Message)
0169 #include "test_Message.moc"
0170 
0171 //(c)C: Copyright © 2019-2020, Michał Policht <michal@policht.pl>. All rights reserved.
0172 //(c)C: SPDX-License-Identifier: LGPL-3.0-or-later OR MIT
0173 //(c)C: This file is a part of CuteHMI.
0174 //(c)C: CuteHMI is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
0175 //(c)C: CuteHMI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more details.
0176 //(c)C: You should have received a copy of the GNU Lesser General Public License along with CuteHMI.  If not, see <https://www.gnu.org/licenses/>.
0177 //(c)C: Additionally, this file is licensed under terms of MIT license as expressed below.
0178 //(c)C: Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
0179 //(c)C: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
0180 //(c)C: THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.