1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2017 The Qt Company Ltd. |
4 | ** Contact: http://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the QtSerialBus module of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:LGPL3$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and The Qt Company. For licensing terms |
14 | ** and conditions see http://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at http://www.qt.io/contact-us. |
16 | ** |
17 | ** GNU Lesser General Public License Usage |
18 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
19 | ** General Public License version 3 as published by the Free Software |
20 | ** Foundation and appearing in the file LICENSE.LGPLv3 included in the |
21 | ** packaging of this file. Please review the following information to |
22 | ** ensure the GNU Lesser General Public License version 3 requirements |
23 | ** will be met: https://www.gnu.org/licenses/lgpl.html. |
24 | ** |
25 | ** GNU General Public License Usage |
26 | ** Alternatively, this file may be used under the terms of the GNU |
27 | ** General Public License version 2.0 or later as published by the Free |
28 | ** Software Foundation and appearing in the file LICENSE.GPL included in |
29 | ** the packaging of this file. Please review the following information to |
30 | ** ensure the GNU General Public License version 2.0 requirements will be |
31 | ** met: http://www.gnu.org/licenses/gpl-2.0.html. |
32 | ** |
33 | ** $QT_END_LICENSE$ |
34 | ** |
35 | ****************************************************************************/ |
36 | |
37 | #include <QtTest/QtTest> |
38 | #include <QtSerialBus/QModbusReply> |
39 | |
40 | class tst_QModbusReply : public QObject |
41 | { |
42 | Q_OBJECT |
43 | |
44 | private slots: |
45 | void initTestCase(); |
46 | void tst_ctor(); |
47 | void tst_setFinished(); |
48 | void tst_setError_data(); |
49 | void tst_setError(); |
50 | void tst_setResult(); |
51 | }; |
52 | |
53 | void tst_QModbusReply::initTestCase() |
54 | { |
55 | qRegisterMetaType<QModbusDevice::Error>(); |
56 | } |
57 | |
58 | void tst_QModbusReply::tst_ctor() |
59 | { |
60 | QModbusReply r(QModbusReply::Common, 1, this); |
61 | QCOMPARE(r.type(), QModbusReply::Common); |
62 | QCOMPARE(r.serverAddress(), 1); |
63 | QCOMPARE(r.isFinished(), false); |
64 | QCOMPARE(r.result().isValid(), false); |
65 | QCOMPARE(r.rawResult().isValid(), false); |
66 | QCOMPARE(r.errorString(), QString()); |
67 | QCOMPARE(r.error(), QModbusDevice::NoError); |
68 | |
69 | QModbusReply r2(QModbusReply::Raw, 2, this); |
70 | QCOMPARE(r2.type(), QModbusReply::Raw); |
71 | QCOMPARE(r2.serverAddress(), 2); |
72 | QCOMPARE(r2.isFinished(), false); |
73 | QCOMPARE(r2.result().isValid(), false); |
74 | QCOMPARE(r2.rawResult().isValid(), false); |
75 | QCOMPARE(r2.errorString(), QString()); |
76 | QCOMPARE(r2.error(), QModbusDevice::NoError); |
77 | } |
78 | |
79 | void tst_QModbusReply::tst_setFinished() |
80 | { |
81 | QModbusReply replyTest(QModbusReply::Common, 1); |
82 | QCOMPARE(replyTest.serverAddress(), 1); |
83 | QSignalSpy finishedSpy(&replyTest, SIGNAL(finished())); |
84 | QSignalSpy errorSpy(&replyTest, SIGNAL(errorOccurred(QModbusDevice::Error))); |
85 | |
86 | QCOMPARE(replyTest.serverAddress(), 1); |
87 | QCOMPARE(replyTest.isFinished(), false); |
88 | QCOMPARE(replyTest.result().isValid(), false); |
89 | QCOMPARE(replyTest.rawResult().isValid(), false); |
90 | QCOMPARE(replyTest.errorString(), QString()); |
91 | QCOMPARE(replyTest.error(), QModbusDevice::NoError); |
92 | |
93 | QVERIFY(finishedSpy.isEmpty()); |
94 | QVERIFY(errorSpy.isEmpty()); |
95 | |
96 | replyTest.setFinished(true); |
97 | QVERIFY(finishedSpy.count() == 1); |
98 | QVERIFY(errorSpy.isEmpty()); |
99 | QCOMPARE(replyTest.serverAddress(), 1); |
100 | QCOMPARE(replyTest.isFinished(), true); |
101 | QCOMPARE(replyTest.result().isValid(), false); |
102 | QCOMPARE(replyTest.rawResult().isValid(), false); |
103 | QCOMPARE(replyTest.errorString(), QString()); |
104 | QCOMPARE(replyTest.error(), QModbusDevice::NoError); |
105 | |
106 | replyTest.setFinished(false); |
107 | QVERIFY(finishedSpy.count() == 1); // no further signal |
108 | QVERIFY(errorSpy.isEmpty()); |
109 | QCOMPARE(replyTest.serverAddress(), 1); |
110 | QCOMPARE(replyTest.isFinished(), false); |
111 | QCOMPARE(replyTest.result().isValid(), false); |
112 | QCOMPARE(replyTest.rawResult().isValid(), false); |
113 | QCOMPARE(replyTest.errorString(), QString()); |
114 | QCOMPARE(replyTest.error(), QModbusDevice::NoError); |
115 | } |
116 | |
117 | void tst_QModbusReply::tst_setError_data() |
118 | { |
119 | QTest::addColumn<QModbusDevice::Error>(name: "error" ); |
120 | QTest::addColumn<QString>(name: "errorString" ); |
121 | |
122 | QTest::newRow(dataTag: "ProtocolError" ) << QModbusDevice::ProtocolError << QString("ProtocolError" ); |
123 | QTest::newRow(dataTag: "NoError" ) << QModbusDevice::NoError << QString("NoError" ); |
124 | QTest::newRow(dataTag: "NoError-empty" ) << QModbusDevice::NoError << QString(); |
125 | QTest::newRow(dataTag: "TimeoutError" ) << QModbusDevice::TimeoutError << QString("TimeoutError" ); |
126 | QTest::newRow(dataTag: "ReplyAbortedError" ) << QModbusDevice::ReplyAbortedError << QString("AbortedError" ); |
127 | } |
128 | |
129 | void tst_QModbusReply::tst_setError() |
130 | { |
131 | QFETCH(QModbusDevice::Error, error); |
132 | QFETCH(QString, errorString); |
133 | |
134 | QModbusReply replyTest(QModbusReply::Common, 1); |
135 | QCOMPARE(replyTest.serverAddress(), 1); |
136 | QSignalSpy finishedSpy(&replyTest, SIGNAL(finished())); |
137 | QSignalSpy errorSpy(&replyTest, SIGNAL(errorOccurred(QModbusDevice::Error))); |
138 | |
139 | QVERIFY(finishedSpy.isEmpty()); |
140 | QVERIFY(errorSpy.isEmpty()); |
141 | |
142 | replyTest.setError(error, errorText: errorString); |
143 | QCOMPARE(finishedSpy.count(), 1); |
144 | QCOMPARE(errorSpy.count(), 1); |
145 | QCOMPARE(replyTest.rawResult().isValid(), false); |
146 | QCOMPARE(replyTest.error(), error); |
147 | QCOMPARE(replyTest.errorString(), errorString); |
148 | QCOMPARE(errorSpy.at(0).at(0).value<QModbusDevice::Error>(), error); |
149 | |
150 | replyTest.setError(error, errorText: errorString); |
151 | replyTest.setFinished(true); |
152 | QCOMPARE(finishedSpy.count(), 3); //setError() implies call to setFinished() |
153 | QCOMPARE(errorSpy.count(), 2); |
154 | } |
155 | |
156 | void tst_QModbusReply::tst_setResult() |
157 | { |
158 | QModbusDataUnit unit(QModbusDataUnit::Coils, 5, {4,5,6}); |
159 | QCOMPARE(unit.startAddress(), 5); |
160 | QCOMPARE(unit.valueCount(), 3u); |
161 | QCOMPARE(unit.registerType(), QModbusDataUnit::Coils); |
162 | QCOMPARE(unit.isValid(), true); |
163 | QVector<quint16> reference = { 4,5,6 }; |
164 | QCOMPARE(unit.values(), reference); |
165 | |
166 | QModbusReply replyTest(QModbusReply::Common, 1); |
167 | QCOMPARE(replyTest.serverAddress(), 1); |
168 | QSignalSpy finishedSpy(&replyTest, SIGNAL(finished())); |
169 | QSignalSpy errorSpy(&replyTest, SIGNAL(errorOccurred(QModbusDevice::Error))); |
170 | |
171 | QVERIFY(finishedSpy.isEmpty()); |
172 | QVERIFY(errorSpy.isEmpty()); |
173 | |
174 | QCOMPARE(replyTest.result().startAddress(), -1); |
175 | QCOMPARE(replyTest.result().valueCount(), 0u); |
176 | QCOMPARE(replyTest.result().registerType(), QModbusDataUnit::Invalid); |
177 | QCOMPARE(replyTest.result().isValid(), false); |
178 | QCOMPARE(replyTest.rawResult().isValid(), false); |
179 | QCOMPARE(replyTest.result().values(), QVector<quint16>()); |
180 | |
181 | QModbusResponse response(QModbusResponse::ReadExceptionStatus, quint16(0x0000)); |
182 | replyTest.setResult(unit); |
183 | replyTest.setRawResult(response); |
184 | QCOMPARE(finishedSpy.count(), 0); |
185 | QCOMPARE(errorSpy.count(), 0); |
186 | QCOMPARE(replyTest.result().startAddress(), 5); |
187 | QCOMPARE(replyTest.result().valueCount(), 3u); |
188 | QCOMPARE(replyTest.result().registerType(), QModbusDataUnit::Coils); |
189 | QCOMPARE(replyTest.result().isValid(), true); |
190 | QCOMPARE(replyTest.result().values(), reference); |
191 | QCOMPARE(replyTest.rawResult().isValid(), true); |
192 | |
193 | auto tmp = replyTest.rawResult(); |
194 | QCOMPARE(tmp.functionCode(), QModbusResponse::ReadExceptionStatus); |
195 | QCOMPARE(tmp.data(), QByteArray::fromHex("0000" )); |
196 | |
197 | QModbusReply replyRawTest(QModbusReply::Raw, 1); |
198 | QCOMPARE(replyRawTest.serverAddress(), 1); |
199 | QSignalSpy finishedSpyRaw(&replyRawTest, SIGNAL(finished())); |
200 | QSignalSpy errorSpyRaw(&replyRawTest, SIGNAL(errorOccurred(QModbusDevice::Error))); |
201 | |
202 | QVERIFY(finishedSpyRaw.isEmpty()); |
203 | QVERIFY(errorSpyRaw.isEmpty()); |
204 | |
205 | QCOMPARE(replyRawTest.result().startAddress(), -1); |
206 | QCOMPARE(replyRawTest.result().valueCount(), 0u); |
207 | QCOMPARE(replyRawTest.result().registerType(), QModbusDataUnit::Invalid); |
208 | QCOMPARE(replyRawTest.result().isValid(), false); |
209 | QCOMPARE(replyRawTest.rawResult().isValid(), false); |
210 | QCOMPARE(replyRawTest.result().values(), QVector<quint16>()); |
211 | |
212 | replyRawTest.setResult(unit); |
213 | replyRawTest.setRawResult(response); |
214 | QCOMPARE(finishedSpy.count(), 0); |
215 | QCOMPARE(errorSpyRaw.count(), 0); |
216 | QCOMPARE(replyRawTest.result().isValid(), true); |
217 | QCOMPARE(replyRawTest.rawResult().isValid(), true); |
218 | |
219 | tmp = replyRawTest.rawResult(); |
220 | QCOMPARE(tmp.functionCode(), QModbusResponse::ReadExceptionStatus); |
221 | QCOMPARE(tmp.data(), QByteArray::fromHex("0000" )); |
222 | } |
223 | |
224 | QTEST_MAIN(tst_QModbusReply) |
225 | |
226 | #include "tst_qmodbusreply.moc" |
227 | |