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 test suite 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#include "../qbearertestcommon.h"
31
32#ifndef QT_NO_BEARERMANAGEMENT
33#include <QtNetwork/qnetworkconfiguration.h>
34#include <QtNetwork/qnetworkconfigmanager.h>
35#endif
36
37/*
38 Although this unit test doesn't use QNetworkAccessManager
39 this include is used to ensure that bearer continues to compile against
40 Qt 4.7+ which has a QNetworkConfiguration enabled QNetworkAccessManager
41*/
42#include <QNetworkAccessManager>
43
44QT_USE_NAMESPACE
45
46class tst_QNetworkConfiguration : public QObject
47{
48 Q_OBJECT
49
50private slots:
51#ifndef QT_NO_BEARERMANAGEMENT
52 void invalidPoint();
53 void comparison();
54 void children();
55 void isRoamingAvailable();
56 void connectTimeout();
57#endif
58};
59
60#ifndef QT_NO_BEARERMANAGEMENT
61void tst_QNetworkConfiguration::invalidPoint()
62{
63 QNetworkConfiguration pt;
64
65 QVERIFY(pt.name().isEmpty());
66 QVERIFY(!pt.isValid());
67 QCOMPARE(pt.type(), QNetworkConfiguration::Invalid);
68 QVERIFY(!(pt.state() & QNetworkConfiguration::Defined));
69 QVERIFY(!(pt.state() & QNetworkConfiguration::Discovered));
70 QVERIFY(!(pt.state() & QNetworkConfiguration::Active));
71 QVERIFY(!pt.isRoamingAvailable());
72
73 QNetworkConfiguration pt2(pt);
74 QVERIFY(pt2.name().isEmpty());
75 QVERIFY(!pt2.isValid());
76 QCOMPARE(pt2.type(), QNetworkConfiguration::Invalid);
77 QVERIFY(!(pt2.state() & QNetworkConfiguration::Defined));
78 QVERIFY(!(pt2.state() & QNetworkConfiguration::Discovered));
79 QVERIFY(!(pt2.state() & QNetworkConfiguration::Active));
80 QVERIFY(!pt2.isRoamingAvailable());
81
82}
83
84void tst_QNetworkConfiguration::comparison()
85{
86 //test copy constructor and assignment operator
87 //compare invalid connection points
88 QNetworkConfiguration pt1;
89 QVERIFY(!pt1.isValid());
90 QCOMPARE(pt1.type(), QNetworkConfiguration::Invalid);
91
92 QNetworkConfiguration pt2(pt1);
93 QVERIFY(pt1==pt2);
94 QVERIFY(!(pt1!=pt2));
95 QCOMPARE(pt1.name(), pt2.name());
96 QCOMPARE(pt1.isValid(), pt2.isValid());
97 QCOMPARE(pt1.type(), pt2.type());
98 QCOMPARE(pt1.state(), pt2.state());
99 QCOMPARE(pt1.purpose(), pt2.purpose());
100
101
102 QNetworkConfiguration pt3;
103 pt3 = pt1;
104 QVERIFY(pt1==pt3);
105 QVERIFY(!(pt1!=pt3));
106 QCOMPARE(pt1.name(), pt3.name());
107 QCOMPARE(pt1.isValid(), pt3.isValid());
108 QCOMPARE(pt1.type(), pt3.type());
109 QCOMPARE(pt1.state(), pt3.state());
110 QCOMPARE(pt1.purpose(), pt3.purpose());
111
112 //test case must run on machine that has valid connection points
113 QNetworkConfigurationManager manager;
114 QList<QNetworkConfiguration> preScanConfigs = manager.allConfigurations();
115
116 QSignalSpy spy(&manager, SIGNAL(updateCompleted()));
117 manager.updateConfigurations(); //initiate scans
118 QTRY_VERIFY_WITH_TIMEOUT(spy.count() >= 1, TestTimeOut); //wait for scan to complete
119
120 QList<QNetworkConfiguration> configs = manager.allConfigurations(flags: QNetworkConfiguration::Discovered);
121 QVERIFY(configs.count());
122 QNetworkConfiguration defaultConfig = manager.defaultConfiguration();
123 QVERIFY(defaultConfig.isValid());
124 QVERIFY(defaultConfig.type() != QNetworkConfiguration::Invalid);
125 QVERIFY(!defaultConfig.name().isEmpty());
126
127 pt3 = defaultConfig;
128 QVERIFY(defaultConfig==pt3);
129 QVERIFY(!(defaultConfig!=pt3));
130 QCOMPARE(defaultConfig.name(), pt3.name());
131 QCOMPARE(defaultConfig.isValid(), pt3.isValid());
132 QCOMPARE(defaultConfig.type(), pt3.type());
133 QCOMPARE(defaultConfig.state(), pt3.state());
134 QCOMPARE(defaultConfig.purpose(), pt3.purpose());
135}
136
137void tst_QNetworkConfiguration::children()
138{
139 QNetworkConfigurationManager manager;
140 QList<QNetworkConfiguration> configs = manager.allConfigurations();
141
142 foreach(QNetworkConfiguration c, configs)
143 {
144 if ( c.type() == QNetworkConfiguration::ServiceNetwork ) {
145 qDebug() << "found service network" << c.name() << c.children().count();
146 QVERIFY(c.isValid());
147 QList<QNetworkConfiguration> members = c.children();
148 foreach(QNetworkConfiguration child, members) {
149 QVERIFY(child.isValid());
150 QVERIFY(configs.contains(child));
151 qDebug() << "\t" << child.name();
152 }
153 }
154 }
155}
156
157void tst_QNetworkConfiguration::isRoamingAvailable()
158{
159 QNetworkConfigurationManager manager;
160 QList<QNetworkConfiguration> configs = manager.allConfigurations();
161
162 //force update to get maximum list
163 QSignalSpy spy(&manager, SIGNAL(updateCompleted()));
164 manager.updateConfigurations(); //initiate scans
165 QTRY_VERIFY_WITH_TIMEOUT(spy.count() >= 1, TestTimeOut); //wait for scan to complete
166
167 foreach(QNetworkConfiguration c, configs)
168 {
169 QVERIFY(QNetworkConfiguration::UserChoice != c.type());
170 QVERIFY(QNetworkConfiguration::Invalid != c.type());
171 if ( c.type() == QNetworkConfiguration::ServiceNetwork ) {
172 //cannot test flag as some SNAPs may not support roaming anyway
173 //QVERIFY(c.roamingavailable())
174 if ( c.children().count() <= 1 )
175 QVERIFY(!c.isRoamingAvailable());
176 foreach(QNetworkConfiguration child, c.children()) {
177 QCOMPARE(QNetworkConfiguration::InternetAccessPoint, child.type());
178 QCOMPARE(child.children().count(), 0);
179 }
180 } else {
181 QVERIFY(!c.isRoamingAvailable());
182 }
183 }
184}
185
186void tst_QNetworkConfiguration::connectTimeout()
187{
188 QNetworkConfigurationManager manager;
189 QList<QNetworkConfiguration> configs = manager.allConfigurations();
190
191 foreach (QNetworkConfiguration networkConfiguration, configs) {
192 QCOMPARE(networkConfiguration.connectTimeout(), 30000);
193
194 bool result = networkConfiguration.setConnectTimeout(100);
195 QVERIFY(result);
196
197 QCOMPARE(networkConfiguration.connectTimeout(), 100);
198 }
199}
200#endif
201
202QTEST_MAIN(tst_QNetworkConfiguration)
203#include "tst_qnetworkconfiguration.moc"
204

source code of qtbase/tests/auto/network/bearer/qnetworkconfiguration/tst_qnetworkconfiguration.cpp