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 | |
31 | #include <QtNetwork/QNetworkAccessManager> |
32 | #include <QtNetwork/QNetworkReply> |
33 | #ifndef QT_NO_BEARERMANAGEMENT |
34 | #include <QtNetwork/QNetworkConfigurationManager> |
35 | #endif |
36 | |
37 | #include <QtCore/QDebug> |
38 | |
39 | #ifndef QT_NO_BEARERMANAGEMENT |
40 | Q_DECLARE_METATYPE(QNetworkAccessManager::NetworkAccessibility) |
41 | #endif |
42 | |
43 | class tst_QNetworkAccessManager : public QObject |
44 | { |
45 | Q_OBJECT |
46 | |
47 | public: |
48 | tst_QNetworkAccessManager(); |
49 | |
50 | private slots: |
51 | void networkAccessible(); |
52 | void alwaysCacheRequest(); |
53 | }; |
54 | |
55 | tst_QNetworkAccessManager::tst_QNetworkAccessManager() |
56 | { |
57 | } |
58 | |
59 | void tst_QNetworkAccessManager::networkAccessible() |
60 | { |
61 | #ifndef QT_NO_BEARERMANAGEMENT // ### Qt6: Remove section |
62 | QNetworkAccessManager manager; |
63 | |
64 | qRegisterMetaType<QNetworkAccessManager::NetworkAccessibility>(typeName: "QNetworkAccessManager::NetworkAccessibility" ); |
65 | |
66 | QSignalSpy spy(&manager, |
67 | SIGNAL(networkAccessibleChanged(QNetworkAccessManager::NetworkAccessibility))); |
68 | |
69 | // if there is no session, we cannot know in which state we are in |
70 | QNetworkAccessManager::NetworkAccessibility initialAccessibility = |
71 | manager.networkAccessible(); |
72 | |
73 | if (initialAccessibility == QNetworkAccessManager::UnknownAccessibility) |
74 | QSKIP("Unknown accessibility" , SkipAll); |
75 | |
76 | QCOMPARE(manager.networkAccessible(), initialAccessibility); |
77 | |
78 | manager.setNetworkAccessible(QNetworkAccessManager::NotAccessible); |
79 | |
80 | int expectedCount = (initialAccessibility == QNetworkAccessManager::Accessible) ? 1 : 0; |
81 | QCOMPARE(spy.count(), expectedCount); |
82 | if (expectedCount > 0) |
83 | QCOMPARE(spy.takeFirst().at(0).value<QNetworkAccessManager::NetworkAccessibility>(), |
84 | QNetworkAccessManager::NotAccessible); |
85 | QCOMPARE(manager.networkAccessible(), QNetworkAccessManager::NotAccessible); |
86 | |
87 | manager.setNetworkAccessible(QNetworkAccessManager::Accessible); |
88 | |
89 | QCOMPARE(spy.count(), expectedCount); |
90 | if (expectedCount > 0) |
91 | QCOMPARE(spy.takeFirst().at(0).value<QNetworkAccessManager::NetworkAccessibility>(), |
92 | initialAccessibility); |
93 | QCOMPARE(manager.networkAccessible(), initialAccessibility); |
94 | |
95 | QNetworkConfigurationManager configManager; |
96 | QNetworkConfiguration defaultConfig = configManager.defaultConfiguration(); |
97 | if (defaultConfig.isValid()) { |
98 | manager.setConfiguration(defaultConfig); |
99 | |
100 | QCOMPARE(spy.count(), 0); |
101 | |
102 | if (defaultConfig.state().testFlag(flag: QNetworkConfiguration::Active)) |
103 | QCOMPARE(manager.networkAccessible(), QNetworkAccessManager::Accessible); |
104 | else |
105 | QCOMPARE(manager.networkAccessible(), QNetworkAccessManager::NotAccessible); |
106 | |
107 | manager.setNetworkAccessible(QNetworkAccessManager::NotAccessible); |
108 | |
109 | if (defaultConfig.state().testFlag(flag: QNetworkConfiguration::Active)) { |
110 | QCOMPARE(spy.count(), 1); |
111 | QCOMPARE(QNetworkAccessManager::NetworkAccessibility(spy.takeFirst().at(0).toInt()), |
112 | QNetworkAccessManager::NotAccessible); |
113 | } else { |
114 | QCOMPARE(spy.count(), 0); |
115 | } |
116 | } |
117 | QCOMPARE(manager.networkAccessible(), QNetworkAccessManager::NotAccessible); |
118 | #endif |
119 | } |
120 | |
121 | void tst_QNetworkAccessManager::alwaysCacheRequest() |
122 | { |
123 | QNetworkAccessManager manager; |
124 | |
125 | QNetworkRequest req; |
126 | req.setAttribute(code: QNetworkRequest::CacheLoadControlAttribute, value: QNetworkRequest::AlwaysCache); |
127 | QNetworkReply *reply = manager.get(request: req); |
128 | reply->close(); |
129 | delete reply; |
130 | } |
131 | |
132 | QTEST_MAIN(tst_QNetworkAccessManager) |
133 | #include "tst_qnetworkaccessmanager.moc" |
134 | |