1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2016 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the QtNfc module of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:GPL-EXCEPT$ |
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 https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://www.qt.io/contact-us. |
16 | ** |
17 | ** GNU General Public License Usage |
18 | ** Alternatively, this file may be used under the terms of the GNU |
19 | ** General Public License version 3 as published by the Free Software |
20 | ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT |
21 | ** included in the packaging of this file. Please review the following |
22 | ** information to ensure the GNU General Public License requirements will |
23 | ** be met: https://www.gnu.org/licenses/gpl-3.0.html. |
24 | ** |
25 | ** $QT_END_LICENSE$ |
26 | ** |
27 | ****************************************************************************/ |
28 | |
29 | #include <QtTest/QtTest> |
30 | |
31 | #include <private/qnearfieldmanager_emulator_p.h> |
32 | #include <qnearfieldmanager.h> |
33 | #include <qndefnfctextrecord.h> |
34 | #include <qndefnfcurirecord.h> |
35 | #include <qndefmessage.h> |
36 | #include <qndefrecord.h> |
37 | |
38 | QT_USE_NAMESPACE |
39 | |
40 | Q_DECLARE_METATYPE(QNearFieldTarget*) |
41 | Q_DECLARE_METATYPE(QNearFieldTarget::Type) |
42 | Q_DECLARE_METATYPE(QNdefFilter) |
43 | Q_DECLARE_METATYPE(QNdefRecord::TypeNameFormat) |
44 | |
45 | class tst_QNearFieldManager : public QObject |
46 | { |
47 | Q_OBJECT |
48 | |
49 | public: |
50 | tst_QNearFieldManager(); |
51 | |
52 | private slots: |
53 | void initTestCase(); |
54 | |
55 | void targetDetected_data(); |
56 | void targetDetected(); |
57 | |
58 | void unregisterNdefMessageHandler(); |
59 | |
60 | void registerNdefMessageHandler(); |
61 | |
62 | void registerNdefMessageHandler_type_data(); |
63 | void registerNdefMessageHandler_type(); |
64 | |
65 | void registerNdefMessageHandler_filter_data(); |
66 | void registerNdefMessageHandler_filter(); |
67 | }; |
68 | |
69 | tst_QNearFieldManager::tst_QNearFieldManager() |
70 | { |
71 | QDir::setCurrent(QLatin1String(SRCDIR)); |
72 | |
73 | qRegisterMetaType<QNdefMessage>(); |
74 | qRegisterMetaType<QNearFieldTarget *>(); |
75 | } |
76 | |
77 | void tst_QNearFieldManager::initTestCase() |
78 | { |
79 | QNearFieldManagerPrivateImpl *emulatorBackend = new QNearFieldManagerPrivateImpl; |
80 | QNearFieldManager manager(emulatorBackend, 0); |
81 | |
82 | QVERIFY(manager.isAvailable()); |
83 | } |
84 | |
85 | void tst_QNearFieldManager::targetDetected_data() |
86 | { |
87 | QTest::addColumn<bool>(name: "deleteTarget" ); |
88 | |
89 | QTest::newRow(dataTag: "AnyTarget" ) << false; |
90 | QTest::newRow(dataTag: "NfcTagType1" ) << false; |
91 | QTest::newRow(dataTag: "Delete Target" ) << true; |
92 | } |
93 | |
94 | void tst_QNearFieldManager::targetDetected() |
95 | { |
96 | QFETCH(bool, deleteTarget); |
97 | |
98 | QNearFieldManagerPrivateImpl *emulatorBackend = new QNearFieldManagerPrivateImpl; |
99 | QNearFieldManager manager(emulatorBackend, 0); |
100 | |
101 | QSignalSpy targetDetectedSpy(&manager, SIGNAL(targetDetected(QNearFieldTarget*))); |
102 | QSignalSpy targetLostSpy(&manager, SIGNAL(targetLost(QNearFieldTarget*))); |
103 | |
104 | manager.startTargetDetection(); |
105 | |
106 | QTRY_VERIFY(!targetDetectedSpy.isEmpty()); |
107 | |
108 | QNearFieldTarget *target = targetDetectedSpy.first().at(i: 0).value<QNearFieldTarget *>(); |
109 | |
110 | QSignalSpy disconnectedSpy(target, SIGNAL(disconnected())); |
111 | |
112 | QVERIFY(target); |
113 | |
114 | QVERIFY(!target->uid().isEmpty()); |
115 | |
116 | if (!deleteTarget) { |
117 | QTRY_VERIFY(!targetLostSpy.isEmpty()); |
118 | |
119 | QNearFieldTarget *lostTarget = targetLostSpy.first().at(i: 0).value<QNearFieldTarget *>(); |
120 | |
121 | QCOMPARE(target, lostTarget); |
122 | |
123 | QVERIFY(!disconnectedSpy.isEmpty()); |
124 | } else { |
125 | delete target; |
126 | |
127 | // wait for another targetDetected() without a targetLost() signal in between. |
128 | targetDetectedSpy.clear(); |
129 | targetLostSpy.clear(); |
130 | |
131 | QTRY_VERIFY(targetLostSpy.isEmpty() && !targetDetectedSpy.isEmpty()); |
132 | } |
133 | |
134 | manager.stopTargetDetection(); |
135 | } |
136 | |
137 | void tst_QNearFieldManager::unregisterNdefMessageHandler() |
138 | { |
139 | QNearFieldManagerPrivateImpl *emulatorBackend = new QNearFieldManagerPrivateImpl; |
140 | QNearFieldManager manager(emulatorBackend, 0); |
141 | |
142 | QVERIFY(!manager.unregisterNdefMessageHandler(-1)); |
143 | QVERIFY(!manager.unregisterNdefMessageHandler(0)); |
144 | } |
145 | |
146 | class MessageListener : public QObject |
147 | { |
148 | Q_OBJECT |
149 | |
150 | signals: |
151 | void matchedNdefMessage(const QNdefMessage &message, QNearFieldTarget *target); |
152 | }; |
153 | |
154 | void tst_QNearFieldManager::registerNdefMessageHandler() |
155 | { |
156 | QNearFieldManagerPrivateImpl *emulatorBackend = new QNearFieldManagerPrivateImpl; |
157 | QNearFieldManager manager(emulatorBackend, 0); |
158 | |
159 | MessageListener listener; |
160 | QSignalSpy messageSpy(&listener, SIGNAL(matchedNdefMessage(QNdefMessage,QNearFieldTarget*))); |
161 | |
162 | int id = manager.registerNdefMessageHandler(object: &listener, |
163 | SIGNAL(matchedNdefMessage(QNdefMessage,QNearFieldTarget*))); |
164 | |
165 | QVERIFY(id != -1); |
166 | |
167 | QTRY_VERIFY(!messageSpy.isEmpty()); |
168 | |
169 | const QNdefMessage message = messageSpy.first().at(i: 0).value<QNdefMessage>(); |
170 | QNearFieldTarget *target = messageSpy.first().at(i: 1).value<QNearFieldTarget *>(); |
171 | |
172 | QVERIFY(target); |
173 | |
174 | QVERIFY(manager.unregisterNdefMessageHandler(id)); |
175 | } |
176 | |
177 | void tst_QNearFieldManager::registerNdefMessageHandler_type_data() |
178 | { |
179 | QTest::addColumn<QNdefRecord::TypeNameFormat>(name: "typeNameFormat" ); |
180 | QTest::addColumn<QByteArray>(name: "type" ); |
181 | |
182 | QTest::newRow(dataTag: "Image" ) << QNdefRecord::Mime << QByteArray("image/png" ); |
183 | QTest::newRow(dataTag: "URI" ) << QNdefRecord::NfcRtd << QByteArray("U" ); |
184 | QTest::newRow(dataTag: "Text" ) << QNdefRecord::NfcRtd << QByteArray("T" ); |
185 | } |
186 | |
187 | void tst_QNearFieldManager::registerNdefMessageHandler_type() |
188 | { |
189 | QFETCH(QNdefRecord::TypeNameFormat, typeNameFormat); |
190 | QFETCH(QByteArray, type); |
191 | |
192 | QNearFieldManagerPrivateImpl *emulatorBackend = new QNearFieldManagerPrivateImpl; |
193 | QNearFieldManager manager(emulatorBackend, 0); |
194 | |
195 | MessageListener listener; |
196 | QSignalSpy messageSpy(&listener, SIGNAL(matchedNdefMessage(QNdefMessage,QNearFieldTarget*))); |
197 | |
198 | int id = manager.registerNdefMessageHandler(typeNameFormat, type, object: &listener, |
199 | SIGNAL(matchedNdefMessage(QNdefMessage,QNearFieldTarget*))); |
200 | |
201 | QVERIFY(id != -1); |
202 | |
203 | QTRY_VERIFY(!messageSpy.isEmpty()); |
204 | |
205 | const QNdefMessage message = messageSpy.first().at(i: 0).value<QNdefMessage>(); |
206 | |
207 | bool hasRecord = false; |
208 | for (const QNdefRecord &record : message) { |
209 | if (record.typeNameFormat() == typeNameFormat && record.type() == type) { |
210 | hasRecord = true; |
211 | break; |
212 | } |
213 | } |
214 | |
215 | QVERIFY(hasRecord); |
216 | |
217 | QNearFieldTarget *target = messageSpy.first().at(i: 1).value<QNearFieldTarget *>(); |
218 | |
219 | QVERIFY(target); |
220 | } |
221 | |
222 | void tst_QNearFieldManager::registerNdefMessageHandler_filter_data() |
223 | { |
224 | QTest::addColumn<QNdefFilter>(name: "filter" ); |
225 | |
226 | QNdefFilter filter; |
227 | |
228 | QTest::newRow(dataTag: "Empty" ) << filter; |
229 | |
230 | filter.clear(); |
231 | filter.setOrderMatch(true); |
232 | filter.appendRecord(typeNameFormat: QNdefRecord::Mime, type: "image/png" ); |
233 | filter.appendRecord<QNdefNfcTextRecord>(min: 2, max: 10); |
234 | filter.appendRecord<QNdefNfcUriRecord>(min: 1, max: 1); |
235 | QTest::newRow(dataTag: "Image + Multiple Text + URI" ) << filter; |
236 | |
237 | filter.clear(); |
238 | filter.setOrderMatch(true); |
239 | filter.appendRecord<QNdefNfcTextRecord>(min: 1, max: 1); |
240 | filter.appendRecord<QNdefNfcUriRecord>(min: 1, max: 1); |
241 | QTest::newRow(dataTag: "Text + URI" ) << filter; |
242 | |
243 | QNdefFilter::Record record; |
244 | |
245 | filter.clear(); |
246 | filter.setOrderMatch(false); |
247 | filter.appendRecord<QNdefNfcUriRecord>(min: 1, max: 1); |
248 | record.typeNameFormat = QNdefRecord::NfcRtd; |
249 | record.type = "T" ; |
250 | record.minimum = 1; |
251 | record.maximum = 1; |
252 | filter.appendRecord(record); |
253 | QTest::newRow(dataTag: "Unordered Text + URI" ) << filter; |
254 | } |
255 | |
256 | void tst_QNearFieldManager::registerNdefMessageHandler_filter() |
257 | { |
258 | QFETCH(QNdefFilter, filter); |
259 | |
260 | QNearFieldManagerPrivateImpl *emulatorBackend = new QNearFieldManagerPrivateImpl; |
261 | QNearFieldManager manager(emulatorBackend, 0); |
262 | |
263 | MessageListener listener; |
264 | QSignalSpy messageSpy(&listener, SIGNAL(matchedNdefMessage(QNdefMessage,QNearFieldTarget*))); |
265 | |
266 | int id = manager.registerNdefMessageHandler(filter, object: &listener, |
267 | SIGNAL(matchedNdefMessage(QNdefMessage,QNearFieldTarget*))); |
268 | |
269 | QVERIFY(id != -1); |
270 | |
271 | QTRY_VERIFY(!messageSpy.isEmpty()); |
272 | |
273 | const QNdefMessage message = messageSpy.first().at(i: 0).value<QNdefMessage>(); |
274 | |
275 | QNearFieldTarget *target = messageSpy.first().at(i: 1).value<QNearFieldTarget *>(); |
276 | |
277 | QVERIFY(target); |
278 | } |
279 | |
280 | QTEST_MAIN(tst_QNearFieldManager) |
281 | |
282 | // Unset the moc namespace which is not required for the following include. |
283 | #undef QT_BEGIN_MOC_NAMESPACE |
284 | #define QT_BEGIN_MOC_NAMESPACE |
285 | #undef QT_END_MOC_NAMESPACE |
286 | #define QT_END_MOC_NAMESPACE |
287 | |
288 | #include "tst_qnearfieldmanager.moc" |
289 | |