File indexing completed on 2024-12-01 04:18:38

0001 /* AtCore Unit Tests
0002  * SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0003  * SPDX-FileCopyrightText: 2017 Tomaz Canabrava <tcanabrava@kde.org>
0004  * SPDX-FileCopyrightText: 2017-2019 Chris Rizzitello <rizzitello@kde.org>
0005  * SPDX-FileCopyrightText: 2018 Patrick José Pereira <patrickjp@kde.org>
0006  */
0007 
0008 #include <algorithm>
0009 
0010 #include <QtTest>
0011 
0012 #include "atcoretests.h"
0013 
0014 void AtCoreTests::initTestCase()
0015 {
0016     core = new AtCore();
0017 }
0018 
0019 void AtCoreTests::cleanupTestCase()
0020 {
0021 }
0022 
0023 void AtCoreTests::testInitState()
0024 {
0025     QVERIFY(core->state() == AtCore::DISCONNECTED);
0026 }
0027 
0028 void AtCoreTests::testPluginDetect()
0029 {
0030     QStringList fwPluginsFound = core->availableFirmwarePlugins();
0031     QStringList fwPluginsActual = {QStringLiteral("aprinter"), QStringLiteral("grbl"), QStringLiteral("marlin"), QStringLiteral("repetier"), QStringLiteral("smoothie"), QStringLiteral("sprinter"), QStringLiteral("teacup")};
0032 
0033     std::sort(fwPluginsFound.begin(), fwPluginsFound.end());
0034     std::sort(fwPluginsActual.begin(), fwPluginsActual.end());
0035     QVERIFY(fwPluginsFound == fwPluginsActual);
0036 }
0037 
0038 void AtCoreTests::testConnectInvalidDevice()
0039 {
0040     QEXPECT_FAIL("", "Invalid Device Attempt", Continue);
0041     QVERIFY(core->newConnection(QStringLiteral("/dev/ptyp5"), 9600, QStringLiteral("FirmwareNAME")));
0042 }
0043 
0044 void AtCoreTests::testStateChange()
0045 {
0046     QList<QVariant> args;
0047     QSignalSpy sSpy(core, SIGNAL(stateChanged(AtCore::STATES)));
0048     QVERIFY(sSpy.isValid());
0049 
0050     core->setState(AtCore::CONNECTING);
0051     args = sSpy.takeFirst();
0052     QVERIFY(args.at(0).toInt() == AtCore::CONNECTING);
0053 
0054     core->setState(AtCore::IDLE);
0055     args = sSpy.takeFirst();
0056     QVERIFY(args.at(0).toInt() == AtCore::IDLE);
0057 
0058     core->setState(AtCore::BUSY);
0059     args = sSpy.takeFirst();
0060     QVERIFY(args.at(0).toInt() == AtCore::BUSY);
0061 
0062     core->setState(AtCore::PAUSE);
0063     args = sSpy.takeFirst();
0064     QVERIFY(args.at(0).toInt() == AtCore::PAUSE);
0065 
0066     core->setState(AtCore::STOP);
0067     args = sSpy.takeFirst();
0068     QVERIFY(args.at(0).toInt() == AtCore::STOP);
0069 
0070     core->setState(AtCore::ERRORSTATE);
0071     args = sSpy.takeFirst();
0072     QVERIFY(args.at(0).toInt() == AtCore::ERRORSTATE);
0073 
0074     core->setState(AtCore::STARTPRINT);
0075     args = sSpy.takeFirst();
0076     QVERIFY(args.at(0).toInt() == AtCore::STARTPRINT);
0077 
0078     core->setState(AtCore::FINISHEDPRINT);
0079     args = sSpy.takeFirst();
0080     QVERIFY(args.at(0).toInt() == AtCore::FINISHEDPRINT);
0081 
0082     core->setState(AtCore::DISCONNECTED);
0083     args = sSpy.takeFirst();
0084     QVERIFY(args.at(0).toInt() == AtCore::DISCONNECTED);
0085 }
0086 
0087 void AtCoreTests::testSdMountChanged()
0088 {
0089     QList<QVariant> args;
0090     QSignalSpy sSpy(core, SIGNAL(sdMountChanged(bool)));
0091     QVERIFY(sSpy.isValid());
0092 
0093     core->setSdMounted(true);
0094     args = sSpy.takeFirst();
0095     QVERIFY(args.at(0).toBool() == true);
0096 
0097     core->setSdMounted(false);
0098     args = sSpy.takeFirst();
0099     QVERIFY(args.at(0).toBool() == false);
0100 }
0101 
0102 void AtCoreTests::testSdFileList()
0103 {
0104     QSignalSpy sSpy(core, SIGNAL(sdCardFileListChanged(QStringList)));
0105     QVERIFY(sSpy.isValid());
0106     core->appendSdCardFileList(QStringLiteral("FILE1"));
0107     core->appendSdCardFileList(QStringLiteral("FILE2"));
0108     core->appendSdCardFileList(QStringLiteral("FILE3"));
0109     core->appendSdCardFileList(QStringLiteral("FILE4"));
0110     core->appendSdCardFileList(QStringLiteral("FILE5"));
0111 
0112     QList<QVariant> args = sSpy.takeLast();
0113     QStringList fileList = {QStringLiteral("FILE1"), QStringLiteral("FILE2"), QStringLiteral("FILE3"), QStringLiteral("FILE4"), QStringLiteral("FILE5")};
0114     QVERIFY(args.at(0).toStringList() == fileList);
0115 
0116     core->clearSdCardFileList();
0117     args = sSpy.takeLast();
0118     QVERIFY(args.at(0).toStringList().isEmpty());
0119 }
0120 
0121 void AtCoreTests::testSerialTimerIntervalChanged()
0122 {
0123     QSignalSpy sSpy(core, SIGNAL(serialTimerIntervalChanged(int)));
0124     QVERIFY(sSpy.isValid());
0125     core->setSerialTimerInterval(1000);
0126     core->setSerialTimerInterval(1000);
0127     core->setSerialTimerInterval(2000);
0128     core->setSerialTimerInterval(0);
0129     QVERIFY(sSpy.count() == 3);
0130 }
0131 
0132 void AtCoreTests::testExtruderCountChanged()
0133 {
0134     QSignalSpy sSpy(core, SIGNAL(extruderCountChanged(int)));
0135     QVERIFY(sSpy.isValid());
0136     core->setExtruderCount(1);
0137     core->setExtruderCount(2);
0138     core->setExtruderCount(1);
0139     QVERIFY(sSpy.count() == 2);
0140 }
0141 
0142 void AtCoreTests::testPluginAprinter_load()
0143 {
0144     core->loadFirmwarePlugin(QStringLiteral("aprinter"));
0145     QVERIFY(core->firmwarePlugin()->name() == QStringLiteral("Aprinter"));
0146 }
0147 
0148 void AtCoreTests::testPluginAprinter_validate()
0149 {
0150     QSignalSpy sSpy(core->firmwarePlugin(), SIGNAL(readyForCommand()));
0151     QVERIFY(sSpy.isValid() == true);
0152     core->firmwarePlugin()->validateCommand(QStringLiteral("ok"));
0153     core->firmwarePlugin()->validateCommand(QStringLiteral("other text"));
0154     QVERIFY(sSpy.count() == 1);
0155 }
0156 
0157 void AtCoreTests::testPluginGrbl_load()
0158 {
0159     core->loadFirmwarePlugin(QStringLiteral("grbl"));
0160     QVERIFY(core->firmwarePlugin()->name() == QStringLiteral("Grbl"));
0161 }
0162 
0163 void AtCoreTests::testPluginGrbl_validate()
0164 {
0165     QSignalSpy sSpy(core->firmwarePlugin(), SIGNAL(readyForCommand()));
0166     QVERIFY(sSpy.isValid() == true);
0167     core->firmwarePlugin()->validateCommand(QStringLiteral("ok"));
0168     core->firmwarePlugin()->validateCommand(QStringLiteral("other text"));
0169     QVERIFY(sSpy.count() == 1);
0170 }
0171 
0172 void AtCoreTests::testPluginGrbl_translate()
0173 {
0174     QVERIFY(core->firmwarePlugin()->translate(QStringLiteral("G28")) == "G28");
0175     QVERIFY(core->firmwarePlugin()->translate(QStringLiteral("M104 S50 G28 X")) == "M104 S50\r\nG28 X");
0176     QVERIFY(core->firmwarePlugin()->translate(QStringLiteral("G00 G43 H0  Z0.1")) == "G00\r\nG43 H0 Z0.1");
0177     QVERIFY(core->firmwarePlugin()->translate(QStringLiteral("M6 T0")) == "M6 T0");
0178     QVERIFY(core->firmwarePlugin()->translate(QStringLiteral("G0 G49 G40 G17 G80 G50 G90")) == "G0\r\nG49\r\nG40\r\nG17\r\nG80\r\nG50\r\nG90");
0179     QVERIFY(core->firmwarePlugin()->translate(QStringLiteral("G0 S49 XY G40")) == "G0 S49 XY\r\nG40");
0180 }
0181 
0182 void AtCoreTests::testPluginMarlin_load()
0183 {
0184     core->loadFirmwarePlugin(QStringLiteral("marlin"));
0185     QVERIFY(core->firmwarePlugin()->name() == QStringLiteral("Marlin"));
0186 }
0187 
0188 void AtCoreTests::testPluginMarlin_validate()
0189 {
0190     QSignalSpy sSpy(core->firmwarePlugin(), SIGNAL(readyForCommand()));
0191     QVERIFY(sSpy.isValid() == true);
0192     core->firmwarePlugin()->validateCommand(QStringLiteral("ok"));
0193     core->firmwarePlugin()->validateCommand(QStringLiteral("other text"));
0194     QVERIFY(sSpy.count() == 1);
0195 }
0196 
0197 void AtCoreTests::testPluginRepetier_load()
0198 {
0199     core->loadFirmwarePlugin(QStringLiteral("repetier"));
0200     QVERIFY(core->firmwarePlugin()->name() == QStringLiteral("Repetier"));
0201 }
0202 
0203 void AtCoreTests::testPluginRepetier_validate()
0204 {
0205     QSignalSpy sSpy(core->firmwarePlugin(), SIGNAL(readyForCommand()));
0206     QVERIFY(sSpy.isValid() == true);
0207     core->firmwarePlugin()->validateCommand(QStringLiteral("ok"));
0208     core->firmwarePlugin()->validateCommand(QStringLiteral("other text"));
0209     QVERIFY(sSpy.count() == 1);
0210 }
0211 
0212 void AtCoreTests::testPluginSmoothie_load()
0213 {
0214     core->loadFirmwarePlugin(QStringLiteral("smoothie"));
0215     QVERIFY(core->firmwarePlugin()->name() == QStringLiteral("Smoothie"));
0216 }
0217 
0218 void AtCoreTests::testPluginSmoothie_validate()
0219 {
0220     QSignalSpy sSpy(core->firmwarePlugin(), SIGNAL(readyForCommand()));
0221     QVERIFY(sSpy.isValid() == true);
0222     core->firmwarePlugin()->validateCommand(QStringLiteral("ok"));
0223     core->firmwarePlugin()->validateCommand(QStringLiteral("other text"));
0224     QVERIFY(sSpy.count() == 1);
0225 }
0226 
0227 void AtCoreTests::testPluginSprinter_load()
0228 {
0229     core->loadFirmwarePlugin(QStringLiteral("sprinter"));
0230     QVERIFY(core->firmwarePlugin()->name() == QStringLiteral("Sprinter"));
0231 }
0232 
0233 void AtCoreTests::testPluginSprinter_validate()
0234 {
0235     QSignalSpy sSpy(core->firmwarePlugin(), SIGNAL(readyForCommand()));
0236     QVERIFY(sSpy.isValid() == true);
0237     core->firmwarePlugin()->validateCommand(QStringLiteral("ok"));
0238     core->firmwarePlugin()->validateCommand(QStringLiteral("other text"));
0239     QVERIFY(sSpy.count() == 1);
0240 }
0241 
0242 void AtCoreTests::testPluginTeacup_load()
0243 {
0244     core->loadFirmwarePlugin(QStringLiteral("teacup"));
0245     QVERIFY(core->firmwarePlugin()->name() == QStringLiteral("Teacup"));
0246 }
0247 
0248 void AtCoreTests::testPluginTeacup_validate()
0249 {
0250     QSignalSpy sSpy(core->firmwarePlugin(), SIGNAL(readyForCommand()));
0251     QVERIFY(sSpy.isValid() == true);
0252     core->firmwarePlugin()->validateCommand(QStringLiteral("ok"));
0253     core->firmwarePlugin()->validateCommand(QStringLiteral("other text"));
0254     QVERIFY(sSpy.count() == 1);
0255 }
0256 
0257 void AtCoreTests::testPluginTeacup_translate()
0258 {
0259     QVERIFY(core->firmwarePlugin()->translate(QStringLiteral("G28")) == "G28");
0260     QVERIFY(core->firmwarePlugin()->translate(QStringLiteral("M109 S50")) == "M104 S50\r\nM116");
0261     QVERIFY(core->firmwarePlugin()->translate(QStringLiteral("M190 S50")) == "M140 S50\r\nM116");
0262 }
0263 
0264 QTEST_MAIN(AtCoreTests)