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 QtBluetooth 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 <QDebug>
32#include <QString>
33
34#include <qbluetoothhostinfo.h>
35
36QT_USE_NAMESPACE
37
38class tst_QBluetoothHostInfo : public QObject
39{
40 Q_OBJECT
41
42public:
43 tst_QBluetoothHostInfo();
44 ~tst_QBluetoothHostInfo();
45
46private slots:
47 void tst_address_data();
48 void tst_address();
49
50 void tst_name_data();
51 void tst_name();
52
53 void tst_construction_data();
54 void tst_construction();
55
56 void tst_copy();
57
58 void tst_compare_data();
59 void tst_compare();
60};
61
62tst_QBluetoothHostInfo::tst_QBluetoothHostInfo()
63{
64}
65
66tst_QBluetoothHostInfo::~tst_QBluetoothHostInfo()
67{
68}
69
70void tst_QBluetoothHostInfo::tst_address()
71{
72 QFETCH(QString, addressString);
73
74 QBluetoothAddress address(addressString);
75 QVERIFY(!address.isNull());
76 QCOMPARE(address.toString(), addressString);
77
78 QBluetoothHostInfo info;
79 QBluetoothAddress result = info.address();
80 QVERIFY(result.isNull());
81 info.setAddress(address);
82 QCOMPARE(info.address().toString(), addressString);
83
84}
85
86void tst_QBluetoothHostInfo::tst_address_data()
87{
88 QTest::addColumn<QString>(name: "addressString");
89
90 QTest::newRow(dataTag: "11:22:33:44:55:66") << QString("11:22:33:44:55:66");
91 QTest::newRow(dataTag: "AA:BB:CC:DD:EE:FF") << QString("AA:BB:CC:DD:EE:FF");
92 QTest::newRow(dataTag: "aa:bb:cc:dd:ee:ff") << QString("AA:BB:CC:DD:EE:FF");
93 QTest::newRow(dataTag: "FF:FF:FF:FF:FF:FF") << QString("FF:FF:FF:FF:FF:FF");
94}
95
96void tst_QBluetoothHostInfo::tst_name()
97{
98 QFETCH(QString, name);
99
100 QBluetoothHostInfo info;
101 QString result = info.name();
102 QVERIFY(result.isNull());
103 QVERIFY(result.isEmpty());
104
105 info.setName(name);
106 QCOMPARE(info.name(), name);
107}
108
109void tst_QBluetoothHostInfo::tst_name_data()
110{
111 QTest::addColumn<QString>(name: "name");
112
113 QTest::newRow(dataTag: "empty/default name") << QString();
114 QTest::newRow(dataTag: "empty name") << QString("");
115 QTest::newRow(dataTag: "ABCD") << QString("ABCD");
116 QTest::newRow(dataTag: "Very long name") << QString("ThisIsAVeryLongNameString-abcdefghijklmnopqrstuvwxyz");
117 QTest::newRow(dataTag: "special chars") << QString("gh\nfg i-+.,/;");
118}
119
120void tst_QBluetoothHostInfo::tst_construction_data()
121{
122 QTest::addColumn<QString>(name: "btAddress");
123 QTest::addColumn<QString>(name: "name");
124 QTest::addColumn<bool>(name: "validBtAddress");
125
126 QTest::newRow(dataTag: "11:22:33:44:55:66") << QString("11:22:33:44:55:66") << QString() << true;
127 QTest::newRow(dataTag: "AA:BB:CC:DD:EE:FF") << QString("AA:BB:CC:DD:EE:FF") << QString("") << true;
128 QTest::newRow(dataTag: "aa:bb:cc:dd:ee:ff") << QString("AA:BB:CC:DD:EE:FF") << QString("foobar") << true;
129 QTest::newRow(dataTag: "FF:FF:FF:FF:FF:FF") << QString("FF:FF:FF:FF:FF:FF") << QString("WeUseAlongStringAsName_FFFFFFFFFFFFFFFFFFFF") << true;
130 QTest::newRow(dataTag: "00:00:00:00:00:00") << QString("00:00:00:00:00:00") << QString("foobar2") << false;
131}
132
133void tst_QBluetoothHostInfo::tst_construction()
134{
135 QFETCH(QString, btAddress);
136 QFETCH(QString, name);
137 QFETCH(bool, validBtAddress);
138
139 QBluetoothAddress empty;
140 QVERIFY(empty.isNull());
141
142 QBluetoothHostInfo setter;
143 QBluetoothAddress addr(btAddress);
144 setter.setName(name);
145 setter.setAddress(addr);
146 QCOMPARE(setter.name(), name);
147 QCOMPARE(setter.address().toString(), btAddress);
148 QCOMPARE(setter.address().isNull(), !validBtAddress);
149
150 setter.setAddress(empty);
151 QCOMPARE(setter.name(), name);
152 QCOMPARE(setter.address().toString(), QString("00:00:00:00:00:00"));
153 QCOMPARE(setter.address().isNull(), true);
154
155 setter.setName(QString());
156 QCOMPARE(setter.name(), QString());
157 QCOMPARE(setter.address().toString(), QString("00:00:00:00:00:00"));
158 QCOMPARE(setter.address().isNull(), true);
159
160 setter.setAddress(addr);
161 QCOMPARE(setter.name(), QString());
162 QCOMPARE(setter.address().toString(), btAddress);
163 QCOMPARE(setter.address().isNull(), !validBtAddress);
164}
165
166void tst_QBluetoothHostInfo::tst_copy()
167{
168 QBluetoothHostInfo original;
169 original.setAddress(QBluetoothAddress("11:22:33:44:55:66"));
170 original.setName(QStringLiteral("FunkyName"));
171
172 QBluetoothHostInfo assignConstructor(original);
173 QCOMPARE(assignConstructor.name(), original.name());
174 QCOMPARE(assignConstructor.address(), original.address());
175
176 QBluetoothHostInfo assignOperator;
177 assignOperator = original;
178 QCOMPARE(assignOperator.name(), original.name());
179 QCOMPARE(assignOperator.address(), original.address());
180}
181
182void tst_QBluetoothHostInfo::tst_compare_data()
183{
184 QTest::addColumn<QString>(name: "btAddress1");
185 QTest::addColumn<QString>(name: "name1");
186 QTest::addColumn<QString>(name: "btAddress2");
187 QTest::addColumn<QString>(name: "name2");
188 QTest::addColumn<bool>(name: "sameHostInfo");
189
190 QTest::newRow(dataTag: "11:22:33:44:55:66 - same") << QString("11:22:33:44:55:66") << QString("same")
191 << QString("11:22:33:44:55:66") << QString("same")
192 << true;
193 QTest::newRow(dataTag: "11:22:33:44:55:66 - address") << QString("11:22:33:44:55:66") << QString("same")
194 << QString("11:22:33:44:55:77") << QString("same")
195 << false;
196 QTest::newRow(dataTag: "11:22:33:44:55:66 - name") << QString("11:22:33:44:55:66") << QString("same")
197 << QString("11:22:33:44:55:66") << QString("different")
198 << false;
199 QTest::newRow(dataTag: "11:22:33:44:55:66 - name/address") << QString("11:22:33:44:55:66") << QString("same")
200 << QString("11:22:33:44:55:77") << QString("different")
201 << false;
202 QTest::newRow(dataTag: "empty") << QString() << QString() << QString() << QString() << true;
203 QTest::newRow(dataTag: "empty left") << QString() << QString()
204 << QString("11:22:33:44:55:66") << QString("same") << false;
205 QTest::newRow(dataTag: "empty right") << QString("11:22:33:44:55:66") << QString("same")
206 << QString() << QString() << false;
207 QTest::newRow(dataTag: "00:00:00:00:00:00") << QString("00:00:00:00:00:00") << QString("foobar1")
208 << QString("") << QString("foobar1") << true;
209 QTest::newRow(dataTag: "00:00:00:00:00:00") << QString("00:00:00:00:00:00") << QString("foobar1")
210 << QString("") << QString("foobar2") << false;
211 QTest::newRow(dataTag: "00:00:00:00:00:00") << QString("00:00:00:00:00:00") << QString("")
212 << QString("") << QString("") << true;
213}
214
215void tst_QBluetoothHostInfo::tst_compare()
216{
217 QFETCH(QString, btAddress1);
218 QFETCH(QString, name1);
219 QFETCH(QString, btAddress2);
220 QFETCH(QString, name2);
221 QFETCH(bool, sameHostInfo);
222
223 QVERIFY(QBluetoothHostInfo() == QBluetoothHostInfo());
224
225 QBluetoothHostInfo info1;
226 info1.setAddress(QBluetoothAddress(btAddress1));
227 info1.setName(name1);
228
229 QBluetoothHostInfo info2;
230 info2.setAddress(QBluetoothAddress(btAddress2));
231 info2.setName(name2);
232
233 QCOMPARE(info1 == info2, sameHostInfo);
234 QCOMPARE(info1 != info2, !sameHostInfo);
235}
236
237QTEST_MAIN(tst_QBluetoothHostInfo)
238
239#include "tst_qbluetoothhostinfo.moc"
240

source code of qtconnectivity/tests/auto/qbluetoothhostinfo/tst_qbluetoothhostinfo.cpp