summaryrefslogtreecommitdiffstats
path: root/tests/auto/qnetworksession
diff options
context:
space:
mode:
authorAaron McCarthy <aaron.mccarthy@nokia.com>2010-03-01 07:45:35 (GMT)
committerAaron McCarthy <aaron.mccarthy@nokia.com>2010-03-02 05:42:32 (GMT)
commitd042a00b5828961e1e1fa82017717b5f72dde9ef (patch)
tree600353f03174c0bfe80d964c5a6d2500e5df235d /tests/auto/qnetworksession
parentef30a6f336d55c813423bf139d8363f50181179f (diff)
downloadQt-d042a00b5828961e1e1fa82017717b5f72dde9ef.zip
Qt-d042a00b5828961e1e1fa82017717b5f72dde9ef.tar.gz
Qt-d042a00b5828961e1e1fa82017717b5f72dde9ef.tar.bz2
Bearer management changes from Qt Mobility (fca9891).
fca98911b75ce12e70d93cfc2932a9759758a605
Diffstat (limited to 'tests/auto/qnetworksession')
-rw-r--r--tests/auto/qnetworksession/lackey/main.cpp13
-rw-r--r--tests/auto/qnetworksession/test/tst_qnetworksession.cpp293
2 files changed, 299 insertions, 7 deletions
diff --git a/tests/auto/qnetworksession/lackey/main.cpp b/tests/auto/qnetworksession/lackey/main.cpp
index 41e935a..66d6dd4 100644
--- a/tests/auto/qnetworksession/lackey/main.cpp
+++ b/tests/auto/qnetworksession/lackey/main.cpp
@@ -69,14 +69,15 @@ int main(int argc, char** argv)
QNetworkConfigurationManager manager;
QList<QNetworkConfiguration> discovered =
-#if defined (Q_OS_SYMBIAN)
- // On Symbian, on the first query (before updateConfigurations() call
- // the discovered-states are not correct, so defined-state will do.
- manager.allConfigurations(QNetworkConfiguration::Defined);
-#else
manager.allConfigurations(QNetworkConfiguration::Discovered);
-#endif
+
+ foreach(QNetworkConfiguration config, discovered) {
+ qDebug() << "Lackey: Name of the config enumerated: " << config.name();
+ qDebug() << "Lackey: State of the config enumerated: " << config.state();
+ }
+
if (discovered.isEmpty()) {
+ qDebug("Lackey: no discovered configurations, returning empty error.");
return NO_DISCOVERED_CONFIGURATIONS_ERROR;
}
diff --git a/tests/auto/qnetworksession/test/tst_qnetworksession.cpp b/tests/auto/qnetworksession/test/tst_qnetworksession.cpp
index 4ef3a4f..58b1a48 100644
--- a/tests/auto/qnetworksession/test/tst_qnetworksession.cpp
+++ b/tests/auto/qnetworksession/test/tst_qnetworksession.cpp
@@ -54,6 +54,7 @@
QT_USE_NAMESPACE
Q_DECLARE_METATYPE(QNetworkConfiguration)
+Q_DECLARE_METATYPE(QNetworkConfiguration::Type);
Q_DECLARE_METATYPE(QNetworkSession::State);
Q_DECLARE_METATYPE(QNetworkSession::SessionError);
@@ -70,6 +71,11 @@ private slots:
void outOfProcessSession();
void invalidSession();
+ void repeatedOpenClose_data();
+ void repeatedOpenClose();
+
+ void roamingErrorCodes();
+
void sessionProperties_data();
void sessionProperties();
@@ -94,11 +100,17 @@ private:
#endif
};
+// Helper functions
+bool openSession(QNetworkSession *session);
+bool closeSession(QNetworkSession *session, bool lastSessionOnConfiguration = true);
+QNetworkConfiguration suitableConfiguration(QString bearerType, QNetworkConfiguration::Type configType);
+
void tst_QNetworkSession::initTestCase()
{
qRegisterMetaType<QNetworkSession::State>("QNetworkSession::State");
qRegisterMetaType<QNetworkSession::SessionError>("QNetworkSession::SessionError");
qRegisterMetaType<QNetworkConfiguration>("QNetworkConfiguration");
+ qRegisterMetaType<QNetworkConfiguration::Type>("QNetworkConfiguration::Type");
#ifdef Q_WS_MAEMO_6
iapconf = new Maemo::IAPConf("007");
@@ -211,9 +223,42 @@ void tst_QNetworkSession::cleanupTestCase()
void tst_QNetworkSession::invalidSession()
{
+ // Verify that session created with invalid configuration remains in invalid state
QNetworkSession session(QNetworkConfiguration(), 0);
QVERIFY(!session.isOpen());
QVERIFY(session.state() == QNetworkSession::Invalid);
+ QVERIFY(session.error() == QNetworkSession::InvalidConfigurationError);
+
+ // Verify that opening session with invalid configuration both 1) emits invalidconfigurationerror
+ // and 2) sets session's state as invalid.
+ QSignalSpy errorSpy(&session, SIGNAL(error(QNetworkSession::SessionError)));
+ session.open();
+ session.waitForOpened(1000); // Should bail out right away
+ QVERIFY(errorSpy.count() == 1);
+ QNetworkSession::SessionError error =
+ qvariant_cast<QNetworkSession::SessionError> (errorSpy.first().at(0));
+ QVERIFY(error == QNetworkSession::InvalidConfigurationError);
+ QVERIFY(session.error() == QNetworkSession::InvalidConfigurationError);
+ QVERIFY(session.state() == QNetworkSession::Invalid);
+
+ // Check same thing with a config from platform (there are subtle differences
+ // because emtpy configuration does not have private pointer). Test with config
+ // in '(un)defined' state
+ QList<QNetworkConfiguration> allConfigs = manager.allConfigurations();
+ foreach(QNetworkConfiguration config, allConfigs) {
+ if ((config.state() & QNetworkConfiguration::Discovered) != QNetworkConfiguration::Discovered) {
+ QNetworkSession session2(config);
+ QSignalSpy errorSpy2(&session2, SIGNAL(error(QNetworkSession::SessionError)));
+ session2.open();
+ session2.waitForOpened(1000); // Should bail out right away
+ QVERIFY(errorSpy2.count() == 1);
+ QNetworkSession::SessionError error2 =
+ qvariant_cast<QNetworkSession::SessionError> (errorSpy2.first().at(0));
+ QVERIFY(error2 == QNetworkSession::InvalidConfigurationError);
+ QVERIFY(session2.state() == QNetworkSession::Invalid);
+ break; // Once is enough
+ }
+ }
}
void tst_QNetworkSession::sessionProperties_data()
@@ -300,6 +345,100 @@ void tst_QNetworkSession::sessionProperties()
}
}
+void tst_QNetworkSession::repeatedOpenClose_data() {
+ QTest::addColumn<QString>("bearerType");
+ QTest::addColumn<QNetworkConfiguration::Type>("configurationType");
+ QTest::addColumn<int>("repeatTimes");
+
+ QTest::newRow("WLAN_IAP") << "WLAN" << QNetworkConfiguration::InternetAccessPoint << 3;
+ // QTest::newRow("Cellular_IAP") << "cellular" << QNetworkConfiguration::InternetAccessPoint << 3;
+ // QTest::newRow("SNAP") << "bearer_type_not_relevant_with_SNAPs" << QNetworkConfiguration::ServiceNetwork << 3;
+}
+
+// Tests repeated-open close.
+void tst_QNetworkSession::repeatedOpenClose() {
+ QFETCH(QString, bearerType);
+ QFETCH(QNetworkConfiguration::Type, configurationType);
+ QFETCH(int, repeatTimes);
+
+ // First check that opening once succeeds and determine if repeatable testing is doable
+ QNetworkConfiguration config = suitableConfiguration(bearerType, configurationType);
+ if (!config.isValid()) {
+ QSKIP("No suitable configurations, skipping this round of repeated open-close test.", SkipSingle);
+ }
+ qDebug() << "Using following configuratio to repeatedly open and close: " << config.name();
+ QNetworkSession permanentSession(config);
+ if (!openSession(&permanentSession) ||
+ !closeSession(&permanentSession)) {
+ QSKIP("Unable to open/close session, skipping this round of repeated open-close test.", SkipSingle);
+ }
+ for (int i = repeatTimes; i > 0; i--) {
+ QVERIFY(openSession(&permanentSession));
+ QVERIFY(closeSession(&permanentSession));
+ }
+}
+
+void tst_QNetworkSession::roamingErrorCodes() {
+
+#ifndef Q_OS_SYMBIAN
+ QSKIP("Roaming supported on Symbian.", SkipAll);
+#else
+ QNetworkConfiguration wlanIapConfig = suitableConfiguration("WLAN", QNetworkConfiguration::InternetAccessPoint);
+ if (!wlanIapConfig.isValid()) {
+ QSKIP("No WLAN IAP accessible, skipping test.", SkipAll);
+ }
+ // Check that opening and closing two sessions on same config work gracefully:
+ QNetworkSession iapSession(wlanIapConfig);
+ QVERIFY(openSession(&iapSession));
+ QNetworkSession adminIapSession(wlanIapConfig);
+ QVERIFY(openSession(&adminIapSession));
+ QVERIFY(closeSession(&iapSession, false)); // false == not a last session based on the configuration
+ QVERIFY(closeSession(&adminIapSession));
+
+ // Open configurations again, force close bearer and check that errors are emitted correctly
+ // on the other session
+ QVERIFY(openSession(&iapSession));
+ QVERIFY(openSession(&adminIapSession));
+ QSignalSpy errorSpy(&iapSession, SIGNAL(error(QNetworkSession::SessionError)));
+ adminIapSession.stop(); // requires NetworkControl capabilities
+ QTRY_VERIFY(!errorSpy.isEmpty()); // wait for error signals
+ QNetworkSession::SessionError error = qvariant_cast<QNetworkSession::SessionError>(errorSpy.first().at(0));
+ QVERIFY(error == QNetworkSession::SessionAbortedError);
+ QVERIFY(iapSession.state() == QNetworkSession::Disconnected);
+ QVERIFY(adminIapSession.state() == QNetworkSession::Disconnected);
+#endif // Q_OS_SYMBIAN
+ /*
+ // Check for roaming error. Challenging to automate, therefore commented out.
+ // Case requires that you have controllable WLAN in Internet SNAP (only).
+ QNetworkConfiguration snapConfig = suitableConfiguration("bearer_not_relevant_with_snaps", QNetworkConfiguration::ServiceNetwork);
+ if (!snapConfig.isValid()) {
+ QSKIP("No SNAP accessible, skipping test.", SkipAll);
+ }
+ QNetworkSession snapSession(snapConfig);
+ QVERIFY(openSession(&snapSession));
+ QSignalSpy errorSpySnap(&snapSession, SIGNAL(error(QNetworkSession::SessionError)));
+ qDebug("Disconnect the WLAN now");
+ QTRY_VERIFY(!errorSpySnap.isEmpty()); // wait for error signals
+ QVERIFY(errorSpySnap.count() == 1);
+ error = qvariant_cast<QNetworkSession::SessionError>(errorSpySnap.first().at(0));
+ qDebug() << "Error received when turning off wlan on SNAP: " << error;
+ QVERIFY(error == QNetworkSession::RoamingError);
+
+ qDebug("Connect the WLAN now");
+ QTest::qWait(60000); // Wait for WLAN to get up
+ QNetworkConfiguration wlanIapConfig2 = suitableConfiguration("WLAN", QNetworkConfiguration::InternetAccessPoint);
+ QNetworkSession iapSession2(wlanIapConfig2);
+ QVERIFY(openSession(&iapSession2));
+ QSignalSpy errorSpy2(&iapSession2, SIGNAL(error(QNetworkSession::SessionError)));
+ qDebug("Disconnect the WLAN now");
+ QTRY_VERIFY(!errorSpy2.isEmpty()); // wait for error signals
+ QVERIFY(errorSpy2.count() == 1);
+ error = qvariant_cast<QNetworkSession::SessionError>(errorSpy2.first().at(0));
+ QVERIFY(error == QNetworkSession::SessionAbortedError);
+ QVERIFY(iapSession2.state() == QNetworkSession::Disconnected);
+ */
+}
+
void tst_QNetworkSession::userChoiceSession_data()
{
QTest::addColumn<QNetworkConfiguration>("configuration");
@@ -810,7 +949,7 @@ QDebug operator<<(QDebug debug, const QList<QNetworkConfiguration> &list)
}
// Note: outOfProcessSession requires that at least one configuration is
-// at Discovered -state (Defined is ok for symbian as well, as long as it is possible to open).
+// at Discovered -state.
void tst_QNetworkSession::outOfProcessSession()
{
qDebug() << "START";
@@ -913,6 +1052,158 @@ void tst_QNetworkSession::outOfProcessSession()
qDebug("STOP");
}
+// A convinience / helper function for testcases. Return the first matching configuration.
+// Ignores configurations in other than 'discovered' -state. Returns invalid (QNetworkConfiguration())
+// if none found.
+QNetworkConfiguration suitableConfiguration(QString bearerType, QNetworkConfiguration::Type configType) {
+ // Refresh configurations and derive configurations matching given parameters.
+ QNetworkConfigurationManager mgr;
+ QSignalSpy updateSpy(&mgr, SIGNAL(updateCompleted()));
+ mgr.updateConfigurations();
+ QTRY_NOOP(updateSpy.count() == 1);
+ if (updateSpy.count() != 1) {
+ qDebug("tst_QNetworkSession::suitableConfiguration() failure: unable to update configurations");
+ return QNetworkConfiguration();
+ }
+ QList<QNetworkConfiguration> discoveredConfigs = mgr.allConfigurations(QNetworkConfiguration::Discovered);
+ foreach(QNetworkConfiguration config, discoveredConfigs) {
+ if ((config.state() & QNetworkConfiguration::Active) == QNetworkConfiguration::Active) {
+ // qDebug() << "Dumping config because is active: " << config.name();
+ discoveredConfigs.removeOne(config);
+ } else if (config.type() != configType) {
+ // qDebug() << "Dumping config because type (IAP/SNAP) mismatches: " << config.name();
+ discoveredConfigs.removeOne(config);
+ } else if ((config.type() == QNetworkConfiguration::InternetAccessPoint) &&
+ bearerType == "cellular") { // 'cellular' bearertype is for convinience
+ if (config.bearerName() != "2G" &&
+ config.bearerName() != "CDMA2000" &&
+ config.bearerName() != "WCDMA" &&
+ config.bearerName() != "HSPA") {
+ // qDebug() << "Dumping config because bearer mismatches (cellular): " << config.name();
+ discoveredConfigs.removeOne(config);
+ }
+ } else if ((config.type() == QNetworkConfiguration::InternetAccessPoint) &&
+ bearerType != config.bearerName()) {
+ // qDebug() << "Dumping config because bearer mismatches (WLAN): " << config.name();
+ discoveredConfigs.removeOne(config);
+ }
+ }
+ if (discoveredConfigs.isEmpty()) {
+ qDebug("tst_QNetworkSession::suitableConfiguration() failure: no suitable configurations present.");
+ return QNetworkConfiguration();
+ } else {
+ return discoveredConfigs.first();
+ }
+}
+
+// A convinience function for test-cases: opens the given configuration and return
+// true if it was done gracefully.
+bool openSession(QNetworkSession *session) {
+ QNetworkConfigurationManager mgr;
+ QSignalSpy openedSpy(session, SIGNAL(opened()));
+ QSignalSpy stateChangeSpy(session, SIGNAL(stateChanged(QNetworkSession::State)));
+ QSignalSpy errorSpy(session, SIGNAL(error(QNetworkSession::SessionError)));
+ QSignalSpy configChangeSpy(&mgr, SIGNAL(configurationChanged(QNetworkConfiguration)));
+ // Store some initial statuses, because expected signals differ if the config is already
+ // active by some other session
+ QNetworkConfiguration::StateFlags configInitState = session->configuration().state();
+ QNetworkSession::State sessionInitState = session->state();
+
+ if (session->isOpen() ||
+ !session->sessionProperty("ActiveConfiguration").toString().isEmpty()) {
+ qDebug("tst_QNetworkSession::openSession() failure: session was already open / active.");
+ return false;
+ } else {
+ session->open();
+ session->waitForOpened(120000); // Bringing interfaces up and down may take time at platform
+ }
+ // Check that connection opening went by the book. Add checks here if more strictness needed.
+ if (!session->isOpen()) {
+ qDebug("tst_QNetworkSession::openSession() failure: QNetworkSession::open() failed.");
+ return false;
+ }
+ if (openedSpy.count() != 1) {
+ qDebug("tst_QNetworkSession::openSession() failure: QNetworkSession::opened() - signal not received.");
+ return false;
+ }
+ if (!errorSpy.isEmpty()) {
+ qDebug("tst_QNetworkSession::openSession() failure: QNetworkSession::error() - signal was detected.");
+ return false;
+ }
+ if (sessionInitState != QNetworkSession::Connected &&
+ stateChangeSpy.isEmpty()) {
+ qDebug("tst_QNetworkSession::openSession() failure: QNetworkSession::stateChanged() - signals not detected.");
+ return false;
+ }
+ if (configInitState != QNetworkConfiguration::Active &&
+ configChangeSpy.isEmpty()) {
+ qDebug("tst_QNetworkSession::openSession() failure: QNetworkConfigurationManager::configurationChanged() - signals not detected.");
+ return false;
+ }
+ if (session->configuration().state() != QNetworkConfiguration::Active) {
+ qDebug("tst_QNetworkSession::openSession() failure: session's configuration is not in 'Active' -state.");
+ return false;
+ }
+ return true;
+}
+
+// Helper function for closing opened session. Performs checks that
+// session is closed gradefully (e.g. signals). Function does not delete
+// the session. The lastSessionOnConfiguration (true by default) is used to
+// tell if there are more sessions open, basing on same configration. This
+// impacts the checks made.
+bool closeSession(QNetworkSession *session, bool lastSessionOnConfiguration) {
+ if (!session) {
+ qDebug("tst_QNetworkSession::closeSession() failure: NULL session given");
+ return false;
+ }
+ if (session->state() != QNetworkSession::Connected ||
+ !session->isOpen()) {
+ qDebug("tst_QNetworkSession::closeSession() failure: session is not opened.");
+ return false;
+ }
+ QNetworkConfigurationManager mgr;
+ QSignalSpy sessionClosedSpy(session, SIGNAL(closed()));
+ QSignalSpy sessionStateChangedSpy(session, SIGNAL(stateChanged(QNetworkSession::State)));
+ QSignalSpy sessionErrorSpy(session, SIGNAL(error(QNetworkSession::SessionError)));
+ QSignalSpy configChangeSpy(&mgr, SIGNAL(configurationChanged(QNetworkConfiguration)));
+
+ session->close();
+
+ if (!sessionErrorSpy.isEmpty()) {
+ qDebug("tst_QNetworkSession::closeSession() failure: QNetworkSession::error() received.");
+ return false;
+ }
+ if (sessionClosedSpy.count() != 1) {
+ qDebug("tst_QNetworkSession::closeSession() failure: QNetworkSession::closed() signal not received.");
+ return false;
+ }
+ if (lastSessionOnConfiguration &&
+ sessionStateChangedSpy.isEmpty()) {
+ qDebug("tst_QNetworkSession::closeSession() failure: QNetworkSession::stateChanged() signals not received.");
+ return false;
+ }
+ if (lastSessionOnConfiguration &&
+ session->state() != QNetworkSession::Disconnected) {
+ qDebug("tst_QNetworkSession::closeSession() failure: QNetworkSession is not in Disconnected -state");
+ return false;
+ }
+ QTRY_NOOP(!configChangeSpy.isEmpty());
+ if (lastSessionOnConfiguration &&
+ configChangeSpy.isEmpty()) {
+ qDebug("tst_QNetworkSession::closeSession() failure: QNetworkConfigurationManager::configurationChanged() - signal not detected.");
+ return false;
+ }
+ if (lastSessionOnConfiguration &&
+ session->configuration().state() != QNetworkConfiguration::Discovered) {
+ qDebug("tst_QNetworkSession::closeSession() failure: session's configuration is not back in 'Discovered' -state.");
+ return false;
+ }
+ return true;
+}
+
+
+
QTEST_MAIN(tst_QNetworkSession)
#include "tst_qnetworksession.moc"