diff options
author | Lorn Potter <lorn.potter@nokia.com> | 2010-11-04 12:53:16 +1000 |
---|---|---|
committer | Lorn Potter <lorn.potter@nokia.com> | 2010-11-04 12:53:16 +1000 |
commit | df6b2c776f93aecb212d2798b77beb9b8dbfce29 (patch) | |
tree | b9463932eea039b80015caaba07beac1855b4272 /tests/auto/qsystemdeviceinfo/tst_qsystemdeviceinfo.cpp | |
parent | 58decc06634b42bd2de609c7259eb233db7256e1 (diff) |
add signal testing for QSystemNetworkInfo and QSystemDeviceInfo.
Diffstat (limited to 'tests/auto/qsystemdeviceinfo/tst_qsystemdeviceinfo.cpp')
-rw-r--r-- | tests/auto/qsystemdeviceinfo/tst_qsystemdeviceinfo.cpp | 492 |
1 files changed, 459 insertions, 33 deletions
diff --git a/tests/auto/qsystemdeviceinfo/tst_qsystemdeviceinfo.cpp b/tests/auto/qsystemdeviceinfo/tst_qsystemdeviceinfo.cpp index 32d8191faf..4fe6e16955 100644 --- a/tests/auto/qsystemdeviceinfo/tst_qsystemdeviceinfo.cpp +++ b/tests/auto/qsystemdeviceinfo/tst_qsystemdeviceinfo.cpp @@ -44,6 +44,7 @@ #include <QtTest/QtTest> #include "qsysteminfo.h" #include "../qsysteminfotestcommon.h" +#include "qsysteminfo_simulator_p.h" QTM_USE_NAMESPACE Q_DECLARE_METATYPE(QSystemDeviceInfo::BatteryStatus); @@ -53,6 +54,62 @@ Q_DECLARE_METATYPE(QSystemDeviceInfo::Profile); Q_DECLARE_METATYPE(QSystemDeviceInfo::SimStatus); Q_DECLARE_METATYPE(QSystemDeviceInfo::KeyboardTypeFlags); +Q_DECLARE_METATYPE(QSystemDeviceInfo::LockType); + +/** + * Starts an event loop that runs until the given signal is received. + * Optionally the event loop can return earlier on a timeout. + * + * \return \p true if the requested signal was received + * \p false on timeout + */ +static bool waitForSignal(QObject *obj, const char *signal, int timeout = 0) +{ + QEventLoop loop; + QObject::connect(obj, signal, &loop, SLOT(quit())); + QTimer timer; + QSignalSpy timeoutSpy(&timer, SIGNAL(timeout())); + if (timeout > 0) { + QObject::connect(&timer, SIGNAL(timeout()), &loop, SLOT(quit())); + timer.setSingleShot(true); + timer.start(timeout); + } + loop.exec(); + return timeoutSpy.isEmpty(); +} + +class ChangeDeviceThread : public QThread +{ +public: + void run() + { + QMutexLocker locker(&mutex); + SystemInfoConnection si; + QSystemDeviceInfoPrivate *d = si.deviceInfoPrivate(); + d->setBatteryLevel(batteryLevel); + d->setCurrentPowerState(powerState); + d->setCurrentProfile(profile); + d->setBluetoothPower(btPower); + d->setWirelessKeyboardConnected(keyboardConnected); + d->setKeyboardFlipOpen(flip); + d->setDeviceLocked(locked); + d->setTypeOfLock(lockType, lockTypeOn); + + this->exit(); + + } + + QMutex mutex; + QSystemDeviceInfo::PowerState powerState; + QSystemDeviceInfo::Profile profile; + int batteryLevel; + bool btPower; + bool keyboardConnected; + bool flip; + bool locked; + QSystemDeviceInfo::LockType lockType; + bool lockTypeOn; +}; class tst_QSystemDeviceInfo : public QObject { @@ -90,14 +147,51 @@ private slots: void tst_hostId(); void tst_typeOfLock(); + + void tst_batteryLevelChanged_data(); + void tst_batteryLevelChanged(); + + void tst_batteryStatusChanged_data(); + void tst_batteryStatusChanged(); + + void tst_powerStateChanged_data(); + void tst_powerStateChanged(); + + void tst_currentProfileChanged_data(); + void tst_currentProfileChanged(); + + void tst_bluetoothStateChanged(); + + void tst_wirelessKeyboardConnected(); + void tst_keyboardFlip(); + void tst_deviceLocked(); + + void tst_lockTypeChanged_data(); + void tst_lockTypeChanged(); + + + void batteryLevelChanged(int level); + void batteryStatusChanged(QSystemDeviceInfo::BatteryStatus batteryStatus); + void powerStateChanged(QSystemDeviceInfo::PowerState powerState); + void currentProfileChanged(QSystemDeviceInfo::Profile currentProfile); + void bluetoothStateChanged(bool on); + + void wirelessKeyboardConnected(bool connected); + void keyboardFlip(bool open); + void deviceLocked(bool isLocked); + void lockChanged(QSystemDeviceInfo::LockType, bool); + +private: + int curBatLevel; + QSystemDeviceInfo::PowerState curPowerState; + QSystemDeviceInfo::Profile curProfile; + bool btpower; + bool keyboardConnect; + bool keyFLip; + bool deviceLock; + QSystemDeviceInfo::LockType lockType; + bool lockTypeOn; }; -/* -signal todo: -// void profileChanged(QSystemDeviceInfo::Profile); - void batteryLevelChanged(QSystemDeviceInfo::BatteryLevel); - void batteryLevelCritical(qint32); - void powerStateChanged(QSystemDeviceInfo::PowerState); - */ void tst_QSystemDeviceInfo::initTestCase() { @@ -126,7 +220,9 @@ void tst_QSystemDeviceInfo::tst_imei() QSystemDeviceInfo di; QString imeiStr =di.imei(); QVERIFY(!imeiStr.isEmpty() || imeiStr.isEmpty()); - +#ifdef TESTR + QVERIFY(imeiStr == "12-345678-901234-5"); +#endif } void tst_QSystemDeviceInfo::tst_imsi() @@ -135,6 +231,9 @@ void tst_QSystemDeviceInfo::tst_imsi() QString imsiStr = di.imsi(); QVERIFY(!imsiStr.isEmpty() || imsiStr.isEmpty()); +#ifdef TESTR + QVERIFY(imsiStr == "12345679012345"); +#endif } void tst_QSystemDeviceInfo::tst_manufacturer() @@ -143,6 +242,9 @@ void tst_QSystemDeviceInfo::tst_manufacturer() QString manu = di.manufacturer(); QVERIFY(!manu.isEmpty() || manu.isEmpty()); +#ifdef TESTR + QVERIFY(manu == "simulator manufacturer"); +#endif } void tst_QSystemDeviceInfo::tst_model() @@ -151,6 +253,9 @@ void tst_QSystemDeviceInfo::tst_model() QString model = di.model(); QVERIFY(!model.isEmpty() || model.isEmpty()); +#ifdef TESTR + QVERIFY(model == "simulator model"); +#endif } void tst_QSystemDeviceInfo::tst_productName() @@ -159,20 +264,18 @@ void tst_QSystemDeviceInfo::tst_productName() QString product = di.productName(); QVERIFY(!product.isEmpty() | product.isEmpty()); +#ifdef TESTR + QVERIFY(product == "simulator product name"); +#endif } void tst_QSystemDeviceInfo::tst_batteryLevel() { QSystemDeviceInfo di; QVERIFY(di.batteryLevel() > -1); - -// until we simulate this, or wait the signalspy to olong, this will always fail -// if(di.currentPowerState() == QSystemDeviceInfo::WallPowerChargingBattery) { -// QSignalSpy batSpy(&di, SIGNAL(batteryLevelChanged(int))); -// QVERIFY(!batSpy.isEmpty()); -// int level = batSpy.first().at(0).toInt(); -// QVERIFY( level > -1 || level < 101); -// } +#ifdef TESTR + QVERIFY(di.batteryLevel() == 84); +#endif } void tst_QSystemDeviceInfo::tst_batteryStatus() @@ -188,18 +291,6 @@ void tst_QSystemDeviceInfo::tst_batteryStatus() } else if(level > 40) { QVERIFY(di.batteryStatus() == QSystemDeviceInfo::BatteryNormal); } - - // until we simulate this, or wait the signalspy to olong, this will always fail -// if(di.currentPowerState() == QSystemDeviceInfo::WallPowerChargingBattery) { -// QSignalSpy batSpy(&di, SIGNAL(batteryStatusChanged(QSystemDeviceInfo::BatteryStatus))); -// QVERIFY(!batSpy.isEmpty()); -// QSystemDeviceInfo::BatteryStatus status = qvariant_cast<QSystemDeviceInfo::BatteryStatus>(batSpy.first().at(0)); -// QVERIFY( status == QSystemDeviceInfo::NoBatteryLevel -// || status == QSystemDeviceInfo::BatteryCritical -// || status == QSystemDeviceInfo::BatteryVeryLow -// || status == QSystemDeviceInfo::BatteryLow -// || status == QSystemDeviceInfo::BatteryNormal); -// } } void tst_QSystemDeviceInfo::tst_currentProfile() @@ -214,6 +305,9 @@ void tst_QSystemDeviceInfo::tst_currentProfile() || profile == QSystemDeviceInfo::OfflineProfile || profile == QSystemDeviceInfo::PowersaveProfile || profile == QSystemDeviceInfo::CustomProfile); +#ifdef TESTR + QVERIFY(di.currentProfile() == QSystemDeviceInfo::NormalProfile); +#endif } void tst_QSystemDeviceInfo::tst_simStatus() @@ -221,7 +315,9 @@ void tst_QSystemDeviceInfo::tst_simStatus() QSystemDeviceInfo di; bool simStat = di.simStatus(); QVERIFY(simStat == true || simStat == false); - +#ifdef TESTR + QVERIFY(di.simStatus() == QSystemDeviceInfo::SimNotAvailable); +#endif } void tst_QSystemDeviceInfo::tst_isDeviceLocked() @@ -229,6 +325,9 @@ void tst_QSystemDeviceInfo::tst_isDeviceLocked() QSystemDeviceInfo di; bool devLock = di.isDeviceLocked(); QVERIFY(devLock == true || devLock == false); +#ifdef TESTR + QVERIFY(!di.isDeviceLocked()); +#endif } void tst_QSystemDeviceInfo::tst_currentPowerState() @@ -239,20 +338,26 @@ void tst_QSystemDeviceInfo::tst_currentPowerState() || state == QSystemDeviceInfo::BatteryPower || state == QSystemDeviceInfo::WallPower || state == QSystemDeviceInfo::WallPowerChargingBattery); +#ifdef TESTR + QVERIFY(di.currentPowerState() == QSystemDeviceInfo::WallPower); +#endif } void tst_QSystemDeviceInfo::tst_currentBluetoothPowerState() { QSystemDeviceInfo di; - bool state = di.currentPowerState(); + bool state = di.currentBluetoothPowerState(); QVERIFY(state || !state); +#ifdef TESTR + QVERIFY(!di.currentBluetoothPowerState()); +#endif } void tst_QSystemDeviceInfo::tst_keyboardType() { QSystemDeviceInfo di; - QSystemDeviceInfo::KeyboardTypeFlags flags = di.keyboardType(); + QSystemDeviceInfo::KeyboardTypeFlags flags = di.keyboardType(); QVERIFY( (flags && QSystemDeviceInfo::UnknownKeyboard == QSystemDeviceInfo::UnknownKeyboard) || (flags && QSystemDeviceInfo::SoftwareKeyboard == QSystemDeviceInfo::SoftwareKeyboard) @@ -265,8 +370,8 @@ void tst_QSystemDeviceInfo::tst_keyboardType() void tst_QSystemDeviceInfo::tst_isWirelessKeyboardConnected() { QSystemDeviceInfo di; - bool on = di.isWirelessKeyboardConnected(); - QVERIFY(on || !on); + bool on = di.isWirelessKeyboardConnected(); + QVERIFY(on || !on); } void tst_QSystemDeviceInfo::tst_isKeyboardFlipOpen() @@ -309,5 +414,326 @@ void tst_QSystemDeviceInfo::tst_typeOfLock() } } +void tst_QSystemDeviceInfo::tst_batteryLevelChanged_data() +{ + QTest::addColumn<int>("batLevel"); + + QTest::newRow("1c") << 1;//critical + QTest::newRow("4c") << 4;//very low + QTest::newRow("11c") << 11;//low + QTest::newRow("41c") << 41;//normal + QTest::newRow("40d") << 40;//low + QTest::newRow("10d") << 10;//very low + QTest::newRow("3d") << 3;//critical +} + +void tst_QSystemDeviceInfo::tst_batteryLevelChanged() +{ + QSystemDeviceInfo di; + QFETCH(int, batLevel); + + connect(&di,SIGNAL(batteryLevelChanged(int)), + this,SLOT(batteryLevelChanged(int))); + + ChangeDeviceThread *changeDevThread = new ChangeDeviceThread(); + changeDevThread->batteryLevel = batLevel; + changeDevThread->start(); + + QSignalSpy errorSpy(&di, SIGNAL(batteryLevelChanged(int))); + QVERIFY(::waitForSignal(&di, SIGNAL(batteryLevelChanged(int)), 10 * 1000)); + + QVERIFY(errorSpy.count() == 1); + + curBatLevel = batLevel; + +} + +void tst_QSystemDeviceInfo::tst_batteryStatusChanged_data() +{ + SystemInfoConnection si; + QSystemDeviceInfoPrivate *d = si.deviceInfoPrivate(); + d->setInitialData(); + tst_batteryLevelChanged_data(); +} + +void tst_QSystemDeviceInfo::tst_batteryStatusChanged() +{ + QSystemDeviceInfo di; + + SystemInfoConnection si; + + QFETCH(int, batLevel); + + connect(&di,SIGNAL(batteryStatusChanged(QSystemDeviceInfo::BatteryStatus)), + this,SLOT(batteryStatusChanged(QSystemDeviceInfo::BatteryStatus))); + + ChangeDeviceThread *changeDevThread = new ChangeDeviceThread(); + changeDevThread->batteryLevel = batLevel; + changeDevThread->start(); + + QSignalSpy errorSpy(&di, SIGNAL(batteryStatusChanged(QSystemDeviceInfo::BatteryStatus))); + QVERIFY(::waitForSignal(&di, SIGNAL(batteryStatusChanged(QSystemDeviceInfo::BatteryStatus)), 10 * 1000)); + + QVERIFY(errorSpy.count() == 1); + + curBatLevel = batLevel; +} + +void tst_QSystemDeviceInfo::tst_powerStateChanged_data() +{ + QTest::addColumn<QSystemDeviceInfo::PowerState>("powerstate"); + + QTest::newRow("BatteryPower") << QSystemDeviceInfo::BatteryPower; + QTest::newRow("WallPower") << QSystemDeviceInfo::WallPower; + QTest::newRow("WallPowerChargingBattery") << QSystemDeviceInfo::WallPowerChargingBattery; + QTest::newRow("UnknownPower") << QSystemDeviceInfo::UnknownPower; +} + +void tst_QSystemDeviceInfo::tst_powerStateChanged() +{ + QSystemDeviceInfo di; + + SystemInfoConnection si; + + QFETCH(QSystemDeviceInfo::PowerState, powerstate); + + connect(&di,SIGNAL(powerStateChanged(QSystemDeviceInfo::PowerState)), + this,SLOT(powerStateChanged(QSystemDeviceInfo::PowerState))); + + ChangeDeviceThread *changeDevThread = new ChangeDeviceThread(); + changeDevThread->powerState = curPowerState = powerstate; + changeDevThread->start(); + + QSignalSpy errorSpy(&di, SIGNAL(powerStateChanged(QSystemDeviceInfo::PowerState))); + QVERIFY(::waitForSignal(&di, SIGNAL(powerStateChanged(QSystemDeviceInfo::PowerState)), 10 * 1000)); + + QVERIFY(errorSpy.count() == 1); + +} + +void tst_QSystemDeviceInfo::tst_currentProfileChanged_data() +{ + QTest::addColumn<QSystemDeviceInfo::Profile>("profile"); + + QTest::newRow("SilentProfile") << QSystemDeviceInfo::SilentProfile; + QTest::newRow("NormalProfile") << QSystemDeviceInfo::NormalProfile; + QTest::newRow("LoudProfile") << QSystemDeviceInfo::LoudProfile; + QTest::newRow("VibProfile") << QSystemDeviceInfo::VibProfile; + QTest::newRow("OfflineProfile") << QSystemDeviceInfo::OfflineProfile; + QTest::newRow("PowersaveProfile") << QSystemDeviceInfo::PowersaveProfile; + QTest::newRow("CustomProfile") << QSystemDeviceInfo::CustomProfile; + QTest::newRow("UnknownProfile") << QSystemDeviceInfo::UnknownProfile; + +} + +void tst_QSystemDeviceInfo::tst_currentProfileChanged() +{ + QSystemDeviceInfo di; + + SystemInfoConnection si; + QFETCH(QSystemDeviceInfo::Profile, profile); + + connect(&di,SIGNAL(currentProfileChanged(QSystemDeviceInfo::Profile)), + this,SLOT(currentProfileChanged(QSystemDeviceInfo::Profile))); + + ChangeDeviceThread *changeDevThread = new ChangeDeviceThread(); + changeDevThread->profile = curProfile = profile; + changeDevThread->start(); + + QSignalSpy errorSpy(&di, SIGNAL(currentProfileChanged(QSystemDeviceInfo::Profile))); + QVERIFY(::waitForSignal(&di, SIGNAL(currentProfileChanged(QSystemDeviceInfo::Profile)), 10 * 1000)); + + QVERIFY(errorSpy.count() == 1); +} + +void tst_QSystemDeviceInfo::tst_bluetoothStateChanged() +{ + QSystemDeviceInfo di; + + SystemInfoConnection si; + ChangeDeviceThread *changeDevThread = new ChangeDeviceThread(); + + connect(&di,SIGNAL(bluetoothStateChanged(bool)), + this,SLOT(bluetoothStateChanged(bool))); + + changeDevThread->btPower = btpower = true; + changeDevThread->start(); + + QSignalSpy errorSpy(&di, SIGNAL(bluetoothStateChanged(bool))); + QVERIFY(::waitForSignal(&di, SIGNAL(bluetoothStateChanged(bool)), 10 * 1000)); + + QVERIFY(errorSpy.count() == 1); + + changeDevThread->btPower = btpower = false; + changeDevThread->start(); + +} + + +void tst_QSystemDeviceInfo::tst_wirelessKeyboardConnected() +{ + QSystemDeviceInfo di; + + SystemInfoConnection si; + ChangeDeviceThread *changeDevThread = new ChangeDeviceThread(); + + connect(&di,SIGNAL(wirelessKeyboardConnected(bool)), + this,SLOT(wirelessKeyboardConnected(bool))); + + changeDevThread->keyboardConnected = keyboardConnect = true; + changeDevThread->start(); + + QSignalSpy errorSpy(&di, SIGNAL(wirelessKeyboardConnected(bool))); + QVERIFY(::waitForSignal(&di, SIGNAL(wirelessKeyboardConnected(bool)), 10 * 1000)); + + QVERIFY(errorSpy.count() == 1); + + changeDevThread->keyboardConnected = keyboardConnect = false; + changeDevThread->start(); + +} + +void tst_QSystemDeviceInfo::tst_keyboardFlip() +{ + QSystemDeviceInfo di; + + SystemInfoConnection si; + ChangeDeviceThread *changeDevThread = new ChangeDeviceThread(); + + connect(&di,SIGNAL(keyboardFlip(bool)), + this,SLOT(keyboardFlip(bool))); + + changeDevThread->flip = keyFLip = true; + changeDevThread->start(); + + QSignalSpy errorSpy(&di, SIGNAL(keyboardFlip(bool))); + QVERIFY(::waitForSignal(&di, SIGNAL(keyboardFlip(bool)), 10 * 1000)); + + QVERIFY(errorSpy.count() == 1); + + changeDevThread->flip = keyFLip = false; + changeDevThread->start(); + + +} + +void tst_QSystemDeviceInfo::tst_deviceLocked() +{ + QSystemDeviceInfo di; + + SystemInfoConnection si; + ChangeDeviceThread *changeDevThread = new ChangeDeviceThread(); + + connect(&di,SIGNAL(deviceLocked(bool)), + this,SLOT(deviceLocked(bool))); + + changeDevThread->locked = deviceLock = true; + changeDevThread->start(); + + QSignalSpy errorSpy(&di, SIGNAL(deviceLocked(bool))); + QVERIFY(::waitForSignal(&di, SIGNAL(deviceLocked(bool)), 10 * 1000)); + + QVERIFY(errorSpy.count() == 1); + + changeDevThread->locked = deviceLock = false; + changeDevThread->start(); + + +} + +void tst_QSystemDeviceInfo::tst_lockTypeChanged_data() +{ + QTest::addColumn<QSystemDeviceInfo::LockType>("locktype"); + + QTest::newRow("DeviceLocked") << QSystemDeviceInfo::DeviceLocked; + QTest::newRow("TouchAndKeyboardLocked") << QSystemDeviceInfo::TouchAndKeyboardLocked; + QTest::newRow("UnknownLock") << QSystemDeviceInfo::UnknownLock; +} + +void tst_QSystemDeviceInfo::tst_lockTypeChanged() +{ + QSystemDeviceInfo di; + + SystemInfoConnection si; + QFETCH(QSystemDeviceInfo::LockType, locktype); + + connect(&di,SIGNAL(lockChanged(QSystemDeviceInfo::LockType,bool)), + this,SLOT(lockChanged(QSystemDeviceInfo::LockType,bool))); + + ChangeDeviceThread *changeDevThread = new ChangeDeviceThread(); + changeDevThread->lockType = lockType = locktype; + changeDevThread->lockTypeOn = lockTypeOn = false; + changeDevThread->start(); + + QSignalSpy errorSpy(&di, SIGNAL(lockChanged(QSystemDeviceInfo::LockType,bool))); + QVERIFY(::waitForSignal(&di, SIGNAL(lockChanged(QSystemDeviceInfo::LockType,bool)), 10 * 1000)); + + QVERIFY(errorSpy.count() == 1); + + changeDevThread->lockTypeOn = lockTypeOn = false; + changeDevThread->start(); +} + +void tst_QSystemDeviceInfo::batteryLevelChanged(int level) +{ + QVERIFY(level != curBatLevel); +} + +void tst_QSystemDeviceInfo::batteryStatusChanged(QSystemDeviceInfo::BatteryStatus batteryStatus) +{ + QSystemDeviceInfo di; + int level = di.batteryLevel(); + if(level == -1) + QVERIFY(batteryStatus ==QSystemDeviceInfo::NoBatteryLevel); + if(level < 4) { + QVERIFY(batteryStatus == QSystemDeviceInfo::BatteryCritical); + } else if(level < 11) { + QVERIFY(batteryStatus == QSystemDeviceInfo::BatteryVeryLow); + } else if(level < 41) { + QVERIFY(batteryStatus == QSystemDeviceInfo::BatteryLow); + } else if(level > 40) { + QVERIFY(batteryStatus == QSystemDeviceInfo::BatteryNormal); + } +} + +void tst_QSystemDeviceInfo::powerStateChanged(QSystemDeviceInfo::PowerState powerState) +{ + QVERIFY(powerState == curPowerState); +} + +void tst_QSystemDeviceInfo::currentProfileChanged(QSystemDeviceInfo::Profile currentProfile) +{ + QVERIFY(currentProfile == curProfile); +} + +void tst_QSystemDeviceInfo::bluetoothStateChanged(bool on) +{ + QVERIFY(on == btpower); +} + +void tst_QSystemDeviceInfo::wirelessKeyboardConnected(bool connected) +{ + QVERIFY(connected == keyboardConnect); +} + +void tst_QSystemDeviceInfo::keyboardFlip(bool open) +{ + QVERIFY(open == keyFLip); +} + +void tst_QSystemDeviceInfo::deviceLocked(bool isLocked) +{ + QVERIFY(isLocked == deviceLock); +} + +void tst_QSystemDeviceInfo::lockChanged(QSystemDeviceInfo::LockType type, bool on) +{ + QVERIFY(type == lockType); + QVERIFY(on == lockTypeOn); + QSystemDeviceInfo di; + QVERIFY(di.isDeviceLocked() == lockTypeOn); +} + + QTEST_MAIN(tst_QSystemDeviceInfo) #include "tst_qsystemdeviceinfo.moc" |