diff --git a/rpm/lipstick-qt5.spec b/rpm/lipstick-qt5.spec index 44dc4991..0221fe43 100644 --- a/rpm/lipstick-qt5.spec +++ b/rpm/lipstick-qt5.spec @@ -27,6 +27,7 @@ BuildRequires: pkgconfig(Qt5Sensors) BuildRequires: pkgconfig(contentaction5) BuildRequires: pkgconfig(mlite5) >= 0.2.19 BuildRequires: pkgconfig(mce) >= 1.22.0 +BuildRequires: pkgconfig(mce-qt5) >= 1.2.0 BuildRequires: pkgconfig(keepalive) BuildRequires: pkgconfig(dsme_dbus_if) >= 0.63.2 BuildRequires: pkgconfig(thermalmanager_dbus_if) @@ -35,7 +36,6 @@ BuildRequires: pkgconfig(dbus-1) BuildRequires: pkgconfig(dbus-glib-1) BuildRequires: pkgconfig(libresourceqt5) BuildRequires: pkgconfig(ngf-qt5) -BuildRequires: pkgconfig(contextkit-statefs) >= 0.2.7 BuildRequires: pkgconfig(systemd) BuildRequires: pkgconfig(wayland-server) BuildRequires: pkgconfig(usb-moded-qt5) >= 1.8 diff --git a/src/notifications/batterynotifier.cpp b/src/notifications/batterynotifier.cpp index dfbebf38..c431c658 100644 --- a/src/notifications/batterynotifier.cpp +++ b/src/notifications/batterynotifier.cpp @@ -1,7 +1,9 @@ /*************************************************************************** ** ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). -** Copyright (C) 2012 Jolla Ltd. +** Copyright (C) 2012-2019 Jolla Ltd. +** Copyright (c) 2019 Open Mobile Platform LLC. +** ** Contact: Robin Burchell ** ** This file is part of lipstick. @@ -16,194 +18,380 @@ #include #include #include -#include -#include "lowbatterynotifier.h" #include "notificationmanager.h" #include "lipsticknotification.h" #include "batterynotifier.h" -#include - -// timeout to check is charging being started -// -// TODO There should be some environment variable used by different -// parties to share charging start-up timeouts -// -// - for usb cable - enough time for user to choose usb mode or -// mode the be chosen automatically -static int checkChargingStartedTimeoutUsb = 15 * 1000; - -// - for wall charger - just enough time for charging being started -static int checkChargingStartedTimeoutWall = 3 * 1000; +#include +#include + +// How much slack to include in keepalive wakeup ranges [s] +static const int HEARTBEAT_INTERVAL = 12; + +// Delay range for repeating battery low warnings [s] +static const int LOW_BATTERY_INTERVAL_LOW = 30 * 60; +static const int LOW_BATTERY_INTERVAL_HIGH = (LOW_BATTERY_INTERVAL_LOW + + HEARTBEAT_INTERVAL); + +// Maximum expected delay between charger connect and start of charging [ms] +static const int CHARGING_FAILURE_DELAY = 3 * 1000; + +// Delay between 1st property change notification and state evaluation [ms] +static const int STATE_EVALUATION_DELAY = 100; + +BatteryNotifier::BatteryNotifier(QObject *parent) + : QObject(parent) + , m_lowBatteryRepeatLevel(0) + , m_notificationManager(NotificationManager::instance()) + , m_mceChargerType(new QMceChargerType(this)) + , m_mceChargerState(new QMceChargerState(this)) + , m_mceBatteryStatus(new QMceBatteryStatus(this)) + , m_mceBatteryLevel(new QMceBatteryLevel(this)) + , m_mcePowerSaveMode(new QMcePowerSaveMode(this)) + , m_mceDisplay(new QMceDisplay(this)) + , m_mceTkLock(new QMceTkLock(this)) + , m_mceCallState(new QMceCallState(this)) + , m_usbModed(new QUsbModed(this)) + , m_lowBatteryRepeatActivity(new BackgroundActivity(this)) +{ + connect(m_notificationManager, &NotificationManager::NotificationClosed, + this, &BatteryNotifier::onNotificationClosed); + connect(m_mceChargerType, &QMceChargerType::validChanged, + this, &BatteryNotifier::onChargerTypeChanged); + connect(m_mceChargerType, &QMceChargerType::typeChanged, + this, &BatteryNotifier::onChargerTypeChanged); + connect(m_mceChargerState, &QMceChargerState::validChanged, + this, &BatteryNotifier::onChargerStateChanged); + connect(m_mceChargerState, &QMceChargerState::chargingChanged, + this, &BatteryNotifier::onChargerStateChanged); + connect(m_mceBatteryStatus, &QMceBatteryStatus::validChanged, + this, &BatteryNotifier::onBatteryStatusChanged); + connect(m_mceBatteryStatus, &QMceBatteryStatus::statusChanged, + this, &BatteryNotifier::onBatteryStatusChanged); + connect(m_mceBatteryLevel, &QMceBatteryLevel::validChanged, + this, &BatteryNotifier::onBatteryLevelChanged); + connect(m_mceBatteryLevel, &QMceBatteryLevel::percentChanged, + this, &BatteryNotifier::onBatteryLevelChanged); + connect(m_mcePowerSaveMode, &QMcePowerSaveMode::validChanged, + this, &BatteryNotifier::onPowerSaveModeChanged); + connect(m_mcePowerSaveMode, &QMcePowerSaveMode::activeChanged, + this, &BatteryNotifier::onPowerSaveModeChanged); + connect(m_mceDisplay, &QMceDisplay::validChanged, + this, &BatteryNotifier::onDisplayChanged); + connect(m_mceDisplay, &QMceDisplay::stateChanged, + this, &BatteryNotifier::onDisplayChanged); + connect(m_mceTkLock, &QMceTkLock::validChanged, + this, &BatteryNotifier::onTkLockChanged); + connect(m_mceTkLock, &QMceTkLock::lockedChanged, + this, &BatteryNotifier::onTkLockChanged); + connect(m_mceCallState, &QMceCallState::validChanged, + this, &BatteryNotifier::onCallStateChanged); + connect(m_mceCallState, &QMceCallState::stateChanged, + this, &BatteryNotifier::onCallStateChanged); + connect(m_mceCallState, &QMceCallState::typeChanged, + this, &BatteryNotifier::onCallStateChanged); + connect(m_usbModed, &QUsbModed::targetModeChanged, + this, &BatteryNotifier::onTargetUsbModeChanged); + + m_evaluateStateTimer.setInterval(STATE_EVALUATION_DELAY); + m_evaluateStateTimer.setSingleShot(true); + connect(&m_evaluateStateTimer, &QTimer::timeout, + this, &BatteryNotifier::onEvaluateStateTimeout); + + m_chargingFailureTimer.setInterval(CHARGING_FAILURE_DELAY); + m_chargingFailureTimer.setSingleShot(true); + connect(&m_chargingFailureTimer, &QTimer::timeout, + this, &BatteryNotifier::onChargingFailureTimeout); + + m_lowBatteryRepeatActivity->setWakeupRange(LOW_BATTERY_INTERVAL_LOW, + LOW_BATTERY_INTERVAL_HIGH); + connect(m_lowBatteryRepeatActivity, &BackgroundActivity::running, + this, &BatteryNotifier::onBatteryLowTimeout); + + scheduleStateEvaluation(); +} +BatteryNotifier::~BatteryNotifier() +{ +} -static inline QString propertyString(ContextProperty *p) +void BatteryNotifier::onNotificationClosed(uint id, uint reason) { - return p->value().toString().trimmed(); + Q_UNUSED(reason); + for (QList::iterator queuedNotification = m_notifications.begin(); + queuedNotification != m_notifications.end();) { + if (queuedNotification->m_id == id) + queuedNotification = m_notifications.erase(queuedNotification); + else + ++queuedNotification; + } } -BatteryNotifier::BatteryNotifier(QObject *parent) : - QObject(parent), - m_lowBatteryNotifier(0), - m_touchScreenLockActive(false), - m_batteryLevel(new ContextProperty("Battery.Level", this)), - m_chargingState(new ContextProperty("Battery.ChargingState", this)), - m_chargerType(new ContextProperty("Battery.ChargerType", this)), - m_psm(new ContextProperty("System.PowerSaveMode", this)), - m_lastState({BatteryUnknown, StateUnknown, ChargerNo}), - m_mode(ModeNormal), - m_chargingCompletion(NeedsCharging) +void BatteryNotifier::onChargerTypeChanged() { - connect(m_batteryLevel, SIGNAL(valueChanged()), this, SLOT(onPropertyChanged())); - connect(m_chargingState, SIGNAL(valueChanged()), this, SLOT(onPropertyChanged())); - connect(m_chargerType, SIGNAL(valueChanged()), this, SLOT(onPropertyChanged())); - connect(m_psm, SIGNAL(valueChanged()), this, SLOT(onPowerSaveModeChanged())); + if (m_mceChargerType->valid()) { + m_currentState.m_chargerType = m_mceChargerType->type(); + scheduleStateEvaluation(); + } +} - m_timeline.start(); +void BatteryNotifier::onChargerStateChanged() +{ + if (m_mceChargerState->valid()) { + m_currentState.m_chargerState = m_mceChargerState->charging(); + scheduleStateEvaluation(); + } +} - m_preNotificationTimer.setInterval(1000); - m_preNotificationTimer.setSingleShot(true); - connect(&m_preNotificationTimer, SIGNAL(timeout()), - this, SLOT(prepareNotification())); +void BatteryNotifier::onBatteryStatusChanged() +{ + if (m_mceBatteryStatus->valid()) { + m_currentState.m_batteryStatus = m_mceBatteryStatus->status(); + scheduleStateEvaluation(); + } +} - QTimer::singleShot(0, this, SLOT(initBattery())); +void BatteryNotifier::onBatteryLevelChanged() +{ + if (m_mceBatteryLevel->valid()) { + m_currentState.m_batteryLevel = m_mceBatteryLevel->percent(); + scheduleStateEvaluation(); + } } -BatteryNotifier::~BatteryNotifier() +void BatteryNotifier::onPowerSaveModeChanged() { + if (m_mcePowerSaveMode->valid()) { + m_currentState.m_powerSaveMode = m_mcePowerSaveMode->active(); + scheduleStateEvaluation(); + } } -void BatteryNotifier::initBattery() +void BatteryNotifier::onDisplayChanged() { - onPropertyChanged(); + if (m_mceDisplay->valid()) { + m_currentState.m_displayState = m_mceDisplay->state(); + scheduleStateEvaluation(); + } } -void BatteryNotifier::lowBatteryAlert() +void BatteryNotifier::onTkLockChanged() { - sendNotification(NotificationLowBattery); + if (m_mceTkLock->valid()) { + m_currentState.m_tkLock = m_mceTkLock->locked(); + scheduleStateEvaluation(); + } } -void BatteryNotifier::prepareNotification() +void BatteryNotifier::onCallStateChanged() { - State newState; - newState.state = getState(); - newState.level = getLevel(); - newState.charger = getCharger(); - - bool isStateChanged = newState.state != m_lastState.state, - isLevelChanged = newState.level != m_lastState.level, - isChargerChanged = newState.charger != m_lastState.charger, - isAnyChargingState = (newState.state == StateCharging - || newState.state == StateIdle); - NotificationList toRemove; - QList toSend; - - if (isStateChanged) { - if (newState.state == StateIdle) { - stopLowBatteryNotifier(); - if (m_chargingCompletion == NeedsCharging) { - toRemove << NotificationCharging; - toSend << NotificationChargingComplete; - } - } else if (newState.state == StateCharging) { - stopLowBatteryNotifier(); - toRemove << NotificationRemoveCharger - << NotificationChargingComplete - << NotificationLowBattery; - // In the case of USB devices, charging notifications on - // connection are handled by USBModeSelector rather than here. - if (m_chargingCompletion == NeedsCharging && !isUsbDevice()) { - toSend << NotificationCharging; - } - } else { - toRemove << NotificationChargingComplete - << NotificationRemoveCharger - << NotificationCharging - << NotificationChargingNotStarted; - } + if (m_mceCallState->valid()) { + m_currentState.m_callState = m_mceCallState->state(); + m_currentState.m_callType = m_mceCallState->type(); + scheduleStateEvaluation(); } +} - if ((isLevelChanged || isStateChanged) && !isAnyChargingState) { - if (newState.level == BatteryLow) { - // lowBatteryAlert is called immediately, and will send NotificationLowBattery - startLowBatteryNotifier(); - } else if (isLevelChanged && m_lastState.state == StateDischarging) { - toRemove << NotificationLowBattery; +void BatteryNotifier::onTargetUsbModeChanged() +{ + const QString mode(m_usbModed->targetMode()); - if (newState.level == BatteryEmpty) { - toSend << NotificationRechargeBattery; - } else { - stopLowBatteryNotifier(); - toRemove << NotificationRechargeBattery; - } - } + if (m_currentState.m_usbMode != mode) { + m_currentState.m_usbMode = mode; + scheduleStateEvaluation(); } +} - if (isChargerChanged) { - if (newState.charger == ChargerNo) { - m_checkChargingTimer.reset(); - toRemove << NotificationCharging - << NotificationChargingComplete; - - if (m_lastState.charger == ChargerWall) - toSend << NotificationRemoveCharger; - } else if (m_lastState.charger == ChargerNo && !isAnyChargingState) { - // charger is inserted but battery is still discharging - m_checkChargingTimer.reset(new QTimer()); - m_checkChargingTimer->setSingleShot(true); - // unknown charger is also checked after wall charger - // timeout - int timeout = (newState.charger == ChargerUsb - ? checkChargingStartedTimeoutUsb - : checkChargingStartedTimeoutWall); - m_checkChargingTimer->setInterval(timeout); - connect(m_checkChargingTimer.data(), SIGNAL(timeout()), - this, SLOT(checkIsChargingStarted())); - m_checkChargingTimer->start(); - } - } - if (isStateChanged) { - if (!isAnyChargingState) { - m_chargingCompletion = NeedsCharging; - } else if (newState.state == StateIdle) { - m_chargingCompletion = FullyCharged; - } - } +void BatteryNotifier::onBatteryLowTimeout() +{ + sendNotification(NotificationLowBattery); + m_lowBatteryRepeatLevel = m_currentState.m_batteryLevel - 2; + m_lowBatteryRepeatActivity->wait(); +} - // call always to cleanup expired notifications - removeNotification(toRemove); +void BatteryNotifier::onChargingFailureTimeout() +{ + sendNotification(NotificationChargingNotStarted); +} + +void BatteryNotifier::onEvaluateStateTimeout() +{ + NotificationTypeSet toRemove; + NotificationTypeList toSend; + updateDerivedProperties(); + for (int type = NotificationFirst; type <= NotificationLast; ++type) + evaluateNotificationTriggering(static_cast(type), + m_previousState, m_currentState, + toRemove, toSend); + removeNotifications(toRemove); + foreach(NotificationType type, toSend) + sendNotification(type); + m_previousState = m_currentState; + updateLowBatteryNotifier(); +} - foreach(NotificationID id, toSend) - sendNotification(id); +void BatteryNotifier::scheduleStateEvaluation() +{ + if (!m_evaluateStateTimer.isActive()) + m_evaluateStateTimer.start(); +} - m_lastState = newState; +void BatteryNotifier::updateDerivedProperties() +{ + /* Update minimum battery level we expect to see while charging. + * + * While discharging / doing battery full maintenance charging: + * -> track current battery level value + * + * While charging: + * -> track maximum battery level value + * + * Allow one to two percent drops to make false positives less likely. + */ + if (m_currentState.m_chargerState == false + || m_currentState.m_batteryStatus == QMceBatteryStatus::Full + || m_currentState.m_batteryLevel > m_currentState.m_minimumBatteryLevel) + m_currentState.m_minimumBatteryLevel = m_currentState.m_batteryLevel - 1; + + /* Suppress / remove charging notifications when it is expected + * that usb mode selection specific notifications will pop up. + * + * Due to dynamic nature of available usb modes, the condition: + * "suppress on dynamic mode activation" + * needs to be implemented in sort of reversed logic: + * "suppress when target mode is not one of known built-in modes" + * + * Basically: + * + * - Undefined (disconnected), Charger (wall charger), Charging + * (connected to pc, for charging only): Notifications in + * these situatios are handled here -> no suppressing. + * + * - ChargingFallback: Shows up when device lock is preventing + * either Ask or automatic mode selection -> usb mode handling + * will issue "unlock first" notification -> suppress charging + * notification to make room for it. + * + * - Ask: Mode selection dialog is on screen, for now it is + * assumed that it is ok to show charging notification banner + * on top of it -> no suppressing + * + * - Everything else: Assume it is dynamic mode, that will have + * an associated notification from usb mode handling -> suppress + * charging notifications + */ + const QString mode(m_currentState.m_usbMode); + m_currentState.m_suppressCharging = (mode != QUsbMode::Mode::Undefined + && mode != QUsbMode::Mode::Ask + && mode != QUsbMode::Mode::Charging + && mode != QUsbMode::Mode::Charger); } -void BatteryNotifier::checkIsChargingStarted() +bool BatteryNotifier::notificationTriggeringEdge(BatteryNotifier::NotificationType type) { - if (m_lastState.charger != ChargerNo && m_lastState.state == StateDischarging) { - sendNotification(NotificationChargingNotStarted); + switch (type) { + case BatteryNotifier::NotificationRemoveCharger: + return false; + default: + return true; } } -void BatteryNotifier::onPropertyChanged() +bool BatteryNotifier::evaluateNotificationLevel(BatteryNotifier::NotificationType type, + const BatteryNotifier::State &state) { - if (!m_preNotificationTimer.isActive()) { - m_preNotificationTimer.start(); + bool level = false; + switch (type) { + case BatteryNotifier::NotificationCharging: + /* Charging, battery is not full, and we are not expecting + * usb-mode related notifications. */ + level = (state.m_chargerState == true + && state.m_batteryStatus != QMceBatteryStatus::Full + && state.m_suppressCharging == false); + break; + case BatteryNotifier::NotificationChargingComplete: + /* Battery is full (implies charging), and we are not + * expecting usb-mode related notifications. */ + level = (state.m_batteryStatus == QMceBatteryStatus::Full + && state.m_suppressCharging == false); + break; + case BatteryNotifier::NotificationRechargeBattery: + /* Battery empty (implies not charging) */ + level = (state.m_batteryStatus == QMceBatteryStatus::Empty); + break; + case BatteryNotifier::NotificationLowBattery: + /* Battery low (implies not charging) */ + level = (state.m_batteryStatus == QMceBatteryStatus::Low); + break; + case BatteryNotifier::NotificationRemoveCharger: + /* Condition is "connected to wall charger", but we trigger + * on falling edge i.e. on wall charger disconnect. */ + level = (state.m_chargerType == QMceChargerType::DCP + || state.m_chargerType == QMceChargerType::HVDCP); + break; + case BatteryNotifier::NotificationChargingNotStarted: + /* Charger is connected, but charging has not commenced + * within expected time frame. */ + level = (state.m_chargerType != QMceChargerType::None + && state.m_chargerState == false); + break; + case BatteryNotifier::NotificationEnteringPSM: + level = (state.m_powerSaveMode == true); + break; + case BatteryNotifier::NotificationExitingPSM: + level = (state.m_powerSaveMode == false); + break; + case BatteryNotifier::NotificationNotEnoughPower: + /* Battery level has dropped since charger was connected. */ + level = (state.m_batteryLevel < state.m_minimumBatteryLevel); + break; } + return level; } -void BatteryNotifier::onPowerSaveModeChanged() +void BatteryNotifier::evaluateNotificationTriggering(NotificationType type, + const State &previousState, + const State ¤tState, + NotificationTypeSet &toRemove, + NotificationTypeList &toSend) { - Mode newMode = m_psm->value().toInt() ? ModePSM : ModeNormal; - if (m_mode != newMode) { - sendNotification(newMode == ModePSM - ? NotificationEnteringPSM - : NotificationExitingPSM); - m_mode = newMode; + bool previousLevel = evaluateNotificationLevel(type, previousState); + bool currentLevel = evaluateNotificationLevel(type, currentState); + if (previousLevel != currentLevel) { + if (currentLevel == notificationTriggeringEdge(type)) { + switch (type) { + case NotificationLowBattery: + startLowBatteryNotifier(); + break; + case NotificationChargingNotStarted: + m_chargingFailureTimer.start(); + break; + case NotificationCharging: + /* Make sure 'disconnect charger' notification gets + * hidden also on connect to pc */ + toRemove << NotificationRemoveCharger; + toSend << type; + break; + default: + toSend << type; + break; + } + } else { + switch (type) { + case NotificationLowBattery: + stopLowBatteryNotifier(); + break; + case NotificationChargingNotStarted: + m_chargingFailureTimer.stop(); + break; + default: + break; + } + toRemove << type; + } } } -void BatteryNotifier::sendNotification(BatteryNotifier::NotificationID id) +void BatteryNotifier::sendNotification(BatteryNotifier::NotificationType type) { static const struct NotificationInfo { QString category; @@ -242,112 +430,81 @@ void BatteryNotifier::sendNotification(BatteryNotifier::NotificationID id) //% "Low battery" qtTrId("qtn_ener_lowbatt"), ""}, - {"x-nemo.battery.notenoughpower", // NotificationNoEnoughPower + {"x-nemo.battery.notenoughpower", // NotificationNotEnoughPower //% "Not enough power to charge" qtTrId("qtn_ener_nopowcharge"), "icon-m-energy-management-insufficient-power"} }; - Q_ASSERT(id < sizeof(description) / sizeof(description[0])); - NotificationInfo const &info = description[id]; + Q_ASSERT(type < sizeof(description) / sizeof(description[0])); + NotificationInfo const &info = description[type]; + + /* Purge any existing notification items of the same type */ + for (QList::iterator queuedNotification = m_notifications.begin(); + queuedNotification != m_notifications.end();) { + if (queuedNotification->m_type == type) { + uint id = queuedNotification->m_id; + queuedNotification = m_notifications.erase(queuedNotification); + m_notificationManager->CloseNotification(id); + } else { + ++queuedNotification; + } + } - NotificationManager *manager = NotificationManager::instance(); + /* Add fresh notification item */ QVariantHash hints; hints.insert(LipstickNotification::HINT_CATEGORY, info.category); hints.insert(LipstickNotification::HINT_PREVIEW_BODY, info.message); - QueuedNotification queued; - queued.number = manager->Notify(manager->systemApplicationName(), 0, info.icon, - QString(), QString(), QStringList(), hints, -1); - queued.id = id; - queued.time = m_timeline.elapsed(); - if (m_notifications.size() == 3) // saves only last 3 - m_notifications.pop_front(); - m_notifications.push_back(queued); + QueuedNotification queuedNotification; + queuedNotification.m_type = type; + queuedNotification.m_id = m_notificationManager->Notify(m_notificationManager->systemApplicationName(), + 0, + info.icon, + QString(), + QString(), + QStringList(), + hints, + -1); + m_notifications.push_back(queuedNotification); } -void BatteryNotifier::removeNotification(const NotificationList &ids) +void BatteryNotifier::removeNotifications(const NotificationTypeSet &toRemove) { - NotificationManager *manager = NotificationManager::instance(); - qint64 now = m_timeline.elapsed(); - for (QList::iterator it = m_notifications.begin(); - it != m_notifications.end();) { - // inherited: notification is shown for < 5 sec? - if (now - it->time < 5000) { - if (!ids.contains(it->id)) { - ++it; - continue; - } - - manager->CloseNotification(it->number); + for (QList::iterator queuedNotification = m_notifications.begin(); + queuedNotification != m_notifications.end();) { + if (toRemove.contains(queuedNotification->m_type)) { + uint id = queuedNotification->m_id; + queuedNotification = m_notifications.erase(queuedNotification); + m_notificationManager->CloseNotification(id); + } else { + ++queuedNotification; } - it = m_notifications.erase(it); - } -} - -void BatteryNotifier::setTouchScreenLockActive(bool active) -{ - m_touchScreenLockActive = active; - if (m_lowBatteryNotifier != NULL) { - m_lowBatteryNotifier->setTouchScreenLockActive(active); } } void BatteryNotifier::startLowBatteryNotifier() { - if (m_lowBatteryNotifier == NULL) { - m_lowBatteryNotifier = new LowBatteryNotifier(); - connect(m_lowBatteryNotifier, SIGNAL(lowBatteryAlert()), this, SLOT(lowBatteryAlert())); - } - - m_lowBatteryNotifier->setTouchScreenLockActive(m_touchScreenLockActive); - m_lowBatteryNotifier->sendLowBatteryAlert(); + m_lowBatteryRepeatActivity->run(); } void BatteryNotifier::stopLowBatteryNotifier() { - if (m_lowBatteryNotifier != NULL) { - delete m_lowBatteryNotifier; - m_lowBatteryNotifier = NULL; - } -} - -BatteryNotifier::BatteryLevel BatteryNotifier::getLevel() const -{ - QString name(propertyString(m_batteryLevel)); - return (name == "normal" - ? BatteryNormal - : (name == "low" - ? BatteryLow - : (name == "empty" - ? BatteryEmpty - : BatteryUnknown))); -} - -BatteryNotifier::ChargingState BatteryNotifier::getState() const -{ - QString name(propertyString(m_chargingState)); - return (name == "charging" - ? StateCharging - : (name == "discharging" - ? StateDischarging - : (name == "idle" - ? StateIdle - : StateUnknown))); -} - -BatteryNotifier::ChargerType BatteryNotifier::getCharger() const -{ - QString name(propertyString(m_chargerType)); - return ((name == "dcp" || name == "cdp") - ? ChargerWall - : (name == "usb" - ? ChargerUsb - : (name == "" - ? ChargerNo - : ChargerUnknown))); + m_lowBatteryRepeatActivity->stop(); } -bool BatteryNotifier::isUsbDevice() const +void BatteryNotifier::updateLowBatteryNotifier() { - QString name(propertyString(m_chargerType)); - return (name == "dcp" || name == "cdp" || name == "usb"); + if (m_lowBatteryRepeatActivity->isWaiting()) { + /* We have ongoing battery low warning repeat cycle */ + bool active = (m_currentState.m_displayState != QMceDisplay::DisplayOff + && m_currentState.m_tkLock == false); + bool incall = m_currentState.m_callState != QMceCallState::None; + if (active || incall) { + /* Device is in "active use" */ + if (m_currentState.m_batteryLevel <= m_lowBatteryRepeatLevel) { + /* Significant battery level drop since the last warning + * -> repeat the warning immediately. */ + m_lowBatteryRepeatActivity->run(); + } + } + } } diff --git a/src/notifications/batterynotifier.h b/src/notifications/batterynotifier.h index eeb9c7a0..3d5cf83d 100644 --- a/src/notifications/batterynotifier.h +++ b/src/notifications/batterynotifier.h @@ -1,7 +1,9 @@ /*************************************************************************** ** ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). -** Copyright (C) 2012,2015 Jolla Ltd. +** Copyright (C) 2012-2019 Jolla Ltd. +** Copyright (c) 2019 Open Mobile Platform LLC. +** ** Contact: Robin Burchell ** ** This file is part of lipstick. @@ -18,12 +20,18 @@ #include #include -#include -#include -#include - -class LowBatteryNotifier; -class ContextProperty; +#include +#include +#include +#include +#include +#include +#include +#include +#include + +class NotificationManager; +class BackgroundActivity; /*! * Implements the configuration and state for the battery, the power save mode. @@ -45,8 +53,23 @@ class BatteryNotifier : public QObject */ virtual ~BatteryNotifier(); +private slots: + void onNotificationClosed(uint id, uint reason); + void onChargerTypeChanged(); + void onChargerStateChanged(); + void onBatteryStatusChanged(); + void onBatteryLevelChanged(); + void onPowerSaveModeChanged(); + void onDisplayChanged(); + void onTkLockChanged(); + void onCallStateChanged(); + void onTargetUsbModeChanged(); + void onBatteryLowTimeout(); + void onChargingFailureTimeout(); + void onEvaluateStateTimeout(); - enum NotificationID { +private: + enum NotificationType { NotificationCharging, NotificationChargingComplete, NotificationRemoveCharger, @@ -55,65 +78,39 @@ class BatteryNotifier : public QObject NotificationEnteringPSM, NotificationExitingPSM, NotificationLowBattery, - NotificationNoEnoughPower - }; - enum PropertyID { - PropertyFirst_ = 0, - PropertyLevel = PropertyFirst_, - PropertyState, - PropertyCharger, - PropertyLast_ = PropertyCharger + NotificationNotEnoughPower, + NotificationFirst = NotificationCharging, + NotificationLast = NotificationNotEnoughPower, }; - enum BatteryLevel { - BatteryNormal, - BatteryLow, - BatteryEmpty, - BatteryUnknown - }; - enum ChargingState { - StateCharging, - StateDischarging, - StateIdle, - StateUnknown - }; - enum ChargerType { - ChargerUnknown, - ChargerUsb, - ChargerWall, - ChargerNo + struct QueuedNotification { + NotificationType m_type; + uint m_id; }; - enum Mode { - ModeNormal, - ModePSM + struct State { + QMceChargerType::Type m_chargerType = QMceChargerType::None; + bool m_chargerState = false; + QMceBatteryStatus::Status m_batteryStatus = QMceBatteryStatus::Ok; + int m_batteryLevel = 50; + int m_minimumBatteryLevel = 0; + bool m_powerSaveMode = false; + QMceDisplay::State m_displayState = QMceDisplay::DisplayOn; + bool m_tkLock = false; + QMceCallState::State m_callState = QMceCallState::None; + QMceCallState::Type m_callType = QMceCallState::Normal; + QString m_usbMode; + bool m_suppressCharging = false; }; -public slots: - //! Sends a low battery notification - void lowBatteryAlert(); + typedef QSet NotificationTypeSet; + typedef QList NotificationTypeList; - /*! - * Sets the touch screen lock active state so notifications can be enabled/disabled based on that. - * - * \param active \c true if the touch screen lock is active, \c false otherwise - */ - void setTouchScreenLockActive(bool active); - -private slots: - void initBattery(); - void onPowerSaveModeChanged(); - void onPropertyChanged(); - void prepareNotification(); - void checkIsChargingStarted(); - -private: - //! Sends a notification based on the notification ID - void sendNotification(BatteryNotifier::NotificationID id); + //! Sends a notification based on the notification type + void sendNotification(BatteryNotifier::NotificationType type); - //! Removes the current notification if its type is one listed in eventTypes - typedef QSet NotificationList; - void removeNotification(const NotificationList &); + //! Removes any active notifications in the given type set + void removeNotifications(const NotificationTypeSet &toRemove); //! Starts the low battery notifier if not already started void startLowBatteryNotifier(); @@ -121,71 +118,36 @@ private slots: //! Stops the low battery notifier if not already stopped void stopLowBatteryNotifier(); - BatteryLevel getLevel() const; - ChargingState getState() const; - ChargerType getCharger() const; + //! Adjust delay for the next repeated low battery warning + void updateLowBatteryNotifier(); - /*! Determines whether the current device is a USB device that will by - * handled by usb_moded. - * - * \returns true if the device will be handled by usb_moded, false o/w. - */ - bool isUsbDevice() const; + static bool notificationTriggeringEdge(NotificationType type); + static bool evaluateNotificationLevel(NotificationType type, + const State &state); + void evaluateNotificationTriggering(NotificationType type, + const State &previousState, + const State ¤tState, + NotificationTypeSet &toRemove, + NotificationTypeList &toSend); + void updateDerivedProperties(); + void scheduleStateEvaluation(); - //! Low battery notifier for getting notifications about low battery state - LowBatteryNotifier *m_lowBatteryNotifier; - - struct QueuedNotification { - uint number; - NotificationID id; - qint64 time; - }; - /*! There can be several queued notifications (e.g. psm and - * charging one at the same time) and the only one should be - * cancelled - */ QList m_notifications; - - //! Timer for checking whether the current notification can be removed or not - QElapsedTimer m_timeline; - - //! Whether the touch screen lock is active or not - bool m_touchScreenLockActive; - - ContextProperty *m_batteryLevel; - ContextProperty *m_chargingState; - ContextProperty *m_chargerType; - - //! To get device mode - ContextProperty *m_psm; - - struct State { - BatteryLevel level; - ChargingState state; - ChargerType charger; - }; - State m_lastState; - Mode m_mode; - enum ChargingCompletion { - NeedsCharging, - FullyCharged - }; - ChargingCompletion m_chargingCompletion; - - /*! Notification is postponed by means of this timer to skip - * frequent state changes during energy management state - * changes - */ - QTimer m_preNotificationTimer; - - /*! used if charging is not signaled as started immediately after - * charger is inserted to check is it finally started - */ - QScopedPointer m_checkChargingTimer; - -#ifdef UNIT_TEST - friend class Ut_BatteryNotifier; -#endif + QTimer m_evaluateStateTimer; + QTimer m_chargingFailureTimer; + State m_currentState; + State m_previousState; + int m_lowBatteryRepeatLevel; + NotificationManager *m_notificationManager; + QMceChargerType *m_mceChargerType; + QMceChargerState *m_mceChargerState; + QMceBatteryStatus *m_mceBatteryStatus;; + QMceBatteryLevel *m_mceBatteryLevel; + QMcePowerSaveMode *m_mcePowerSaveMode; + QMceDisplay *m_mceDisplay; + QMceTkLock *m_mceTkLock; + QMceCallState *m_mceCallState; + QUsbModed *m_usbModed; + BackgroundActivity *m_lowBatteryRepeatActivity; }; - #endif diff --git a/src/notifications/lowbatterynotifier.cpp b/src/notifications/lowbatterynotifier.cpp deleted file mode 100644 index fd83928e..00000000 --- a/src/notifications/lowbatterynotifier.cpp +++ /dev/null @@ -1,101 +0,0 @@ -/**************************************************************************** - ** - ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). - ** All rights reserved. - ** Contact: Nokia Corporation (directui@nokia.com) - ** - ** This file is part of systemui. - ** - ** If you have questions regarding the use of this file, please contact - ** Nokia at directui@nokia.com. - ** - ** This library is free software; you can redistribute it and/or - ** modify it under the terms of the GNU Lesser General Public - ** License version 2.1 as published by the Free Software Foundation - ** and appearing in the file LICENSE.LGPL included in the packaging - ** of this file. - ** - ****************************************************************************/ -#include "lowbatterynotifier.h" -#include - -static const int CALL_ACTIVE_NOTIFICATION_INTERVAL = 2 * 60 * 1000; -static const int DEVICE_ACTIVE_NOTIFICATION_INTERVAL = 5 * 60 * 1000; -static const int DEVICE_INACTIVE_NOTIFICATION_INTERVAL = 30 * 60 * 1000; - -LowBatteryNotifier::LowBatteryNotifier(QObject *parent) : - QObject(parent), - m_displayState(new MeeGo::QmDisplayState(this)), -#ifdef HAVE_CONTEXTSUBSCRIBER - m_callContextItem("Phone.Call"), -#endif - m_notificationTimer(new QTimer(this)), - m_previousNotificationTime(QTime::currentTime()), - m_notificationInterval(DEVICE_ACTIVE_NOTIFICATION_INTERVAL), - m_deviceInactive(false), - m_touchScreenLockActive(false), - m_callActive(false) -{ - connect(m_notificationTimer, SIGNAL(timeout()), this, SLOT(sendLowBatteryAlert())); - - setNotificationInterval(); - - connect(m_displayState, SIGNAL(displayStateChanged(MeeGo::QmDisplayState::DisplayState)), this, SLOT(setNotificationInterval())); - -#ifdef HAVE_CONTEXTSUBSCRIBER - connect(&m_callContextItem, SIGNAL(valueChanged()), this, SLOT(setNotificationInterval())); - m_callContextItem.subscribe(); -#endif -} - -LowBatteryNotifier::~LowBatteryNotifier() -{ -} - -void LowBatteryNotifier::sendLowBatteryAlert() -{ - emit lowBatteryAlert(); - - m_previousNotificationTime.start(); - m_notificationTimer->start(m_notificationInterval); -} - -void LowBatteryNotifier::setNotificationInterval() -{ - bool deviceCurrentlyInactive = m_touchScreenLockActive; -#ifdef HAVE_CONTEXTSUBSCRIBER - bool callCurrentlyActive = m_callContextItem.value().toString() == "active"; -#else - bool callCurrentlyActive = false; -#endif - - // Device can be considered inactive only if the touch screen lock is active AND the display is off - deviceCurrentlyInactive &= m_displayState->get() == MeeGo::QmDisplayState::Off; - - if (deviceCurrentlyInactive != m_deviceInactive || callCurrentlyActive != m_callActive) { - // Device activity or call activity has changed - m_deviceInactive = deviceCurrentlyInactive; - m_callActive = callCurrentlyActive; - - // Set the new notification interval based on the device and call state - if (m_callActive) { - m_notificationInterval = CALL_ACTIVE_NOTIFICATION_INTERVAL; - } else { - m_notificationInterval = m_deviceInactive ? DEVICE_INACTIVE_NOTIFICATION_INTERVAL : DEVICE_ACTIVE_NOTIFICATION_INTERVAL; - } - - if (m_previousNotificationTime.elapsed() < m_notificationInterval) { - // Elapsed time has not reached the notification interval so just set the new interval - m_notificationTimer->setInterval(m_notificationInterval - m_previousNotificationTime.elapsed()); - } else { - // Elapsed time has reached the notification interval, so send the notification immediately (which will also set the new interval) - sendLowBatteryAlert(); - } - } -} - -void LowBatteryNotifier::setTouchScreenLockActive(bool active) -{ - m_touchScreenLockActive = active; - setNotificationInterval(); -} diff --git a/src/notifications/lowbatterynotifier.h b/src/notifications/lowbatterynotifier.h deleted file mode 100644 index 303f1dfa..00000000 --- a/src/notifications/lowbatterynotifier.h +++ /dev/null @@ -1,101 +0,0 @@ -/**************************************************************************** - ** - ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary (-ies). - ** All rights reserved. - ** Contact: Nokia Corporation (directui@nokia.com) - ** - ** This file is part of systemui. - ** - ** If you have questions regarding the use of this file, please contact - ** Nokia at directui@nokia.com. - ** - ** This library is free software; you can redistribute it and/or - ** modify it under the terms of the GNU Lesser General Public - ** License version 2.1 as published by the Free Software Foundation - ** and appearing in the file LICENSE.LGPL included in the packaging - ** of this file. - ** - ****************************************************************************/ -#ifndef LOWBATTERYNOTIFIER_H -#define LOWBATTERYNOTIFIER_H - -#include -#include -#include - -#ifdef HAVE_CONTEXTSUBSCRIBER -#include -#endif - -class QTimer; - -/*! - * The LowBatteryNotifier will emit a lowBatteryAlert() when the user should - * be alerted about a low battery condition. - */ -class LowBatteryNotifier : public QObject -{ - Q_OBJECT - -public: - /*! - * Creates a low battery notifier. - */ - LowBatteryNotifier(QObject *parent = NULL); - - /*! - * Destroys the low battery notifier. - */ - virtual ~LowBatteryNotifier(); - - /*! - * Sets the touch screen lock active state so notifications can be enabled/disabled based on that. - * - * \param active \c true if the touch screen lock is active, \c false otherwise - */ - void setTouchScreenLockActive(bool active); - -public slots: - //! Requests a low battery notification to be shown - void sendLowBatteryAlert(); - -private slots: - //! Sets the notification interval based on the device activity state - void setNotificationInterval(); - -signals: - //! Sent when the user should be notified about a low battery state - void lowBatteryAlert(); - -private: - //! For getting the display state - MeeGo::QmDisplayState *m_displayState; - -#ifdef HAVE_CONTEXTSUBSCRIBER - //! Call state context framework key - ContextProperty m_callContextItem; -#endif - - //! Timer for sending low battery alerts - QTimer *m_notificationTimer; - - //! Time of the previous notification - QTime m_previousNotificationTime; - - //! Notification interval in milliseconds based on the device and call state - int m_notificationInterval; - - //! Whether the device is currently inactive or not - bool m_deviceInactive; - - //! Whether the touch screen lock is active or not - bool m_touchScreenLockActive; - - //! Whether a call is in progress or not - bool m_callActive; - -#ifdef UNIT_TEST - friend class Ut_LowBatteryNotifier; -#endif -}; -#endif diff --git a/src/notifications/notificationmanager.cpp b/src/notifications/notificationmanager.cpp index 8d63c314..e4dfd265 100644 --- a/src/notifications/notificationmanager.cpp +++ b/src/notifications/notificationmanager.cpp @@ -1,6 +1,8 @@ /*************************************************************************** ** -** Copyright (C) 2012 Jolla Ltd. +** Copyright (C) 2012-2019 Jolla Ltd. +** Copyright (c) 2019 Open Mobile Platform LLC. +** ** Contact: Robin Burchell ** ** This file is part of lipstick. @@ -498,19 +500,19 @@ QString NotificationManager::systemApplicationName() const uint NotificationManager::nextAvailableNotificationID() { - bool idIncreased = false; - - // Try to find an unused ID. Increase the ID at least once but only up to 2^32-1 times. - for (uint i = 0; i < UINT32_MAX && (!idIncreased || m_notifications.contains(m_previousNotificationID)); i++, idIncreased = true) { - m_previousNotificationID++; - - if (m_previousNotificationID == 0) { - // 0 is not a valid ID so skip it - m_previousNotificationID = 1; - } + /* Find an unused ID. It is assumed that we will never end up + * in a situation where even close to significant portion of + * all possible ID numbers would be in use. If that ever happens + * this will turn into forever loop. + */ + for (;;) { + uint id = ++m_previousNotificationID; + // 0 is not a valid ID so skip it + if (!id) + continue; + if (!m_notifications.contains(id)) + return id; } - - return m_previousNotificationID; } void NotificationManager::removeNotificationsWithCategory(const QString &category) diff --git a/src/src.pro b/src/src.pro index 5d63f3d9..1467708f 100644 --- a/src/src.pro +++ b/src/src.pro @@ -68,7 +68,6 @@ HEADERS += \ notifications/notificationmanageradaptor.h \ notifications/categorydefinitionstore.h \ notifications/batterynotifier.h \ - notifications/lowbatterynotifier.h \ notifications/notificationfeedbackplayer.h \ notifications/androidprioritystore.h \ screenlock/screenlock.h \ @@ -112,7 +111,6 @@ SOURCES += \ notifications/notificationlistmodel.cpp \ notifications/notificationpreviewpresenter.cpp \ notifications/batterynotifier.cpp \ - notifications/lowbatterynotifier.cpp \ notifications/androidprioritystore.cpp \ screenlock/screenlock.cpp \ screenlock/screenlockadaptor.cpp \ @@ -141,7 +139,6 @@ SOURCES += \ CONFIG += link_pkgconfig mobility qt warn_on depend_includepath qmake_cache target_qt CONFIG -= link_prl PKGCONFIG += \ - contextkit-statefs \ dbus-1 \ dbus-glib-1 \ dsme_dbus_if \ @@ -150,6 +147,7 @@ PKGCONFIG += \ libsystemd-daemon \ mlite5 \ mce \ + mce-qt5 \ nemodevicelock \ ngf-qt5 \ Qt5SystemInfo \ @@ -167,13 +165,6 @@ packagesExist(contentaction5) { warning("contentaction doesn't exist; falling back to exec - this may not work so great") } -packagesExist(contextkit-statefs) { - PKGCONFIG += contextkit-statefs - DEFINES += HAVE_CONTEXTSUBSCRIBER -} else { - warning("Contextsubscriber not found") -} - QT += dbus xml qml quick sql gui gui-private sensors QMAKE_CXXFLAGS += \ diff --git a/src/usbmodeselector.cpp b/src/usbmodeselector.cpp index 48d50356..6bcfade6 100644 --- a/src/usbmodeselector.cpp +++ b/src/usbmodeselector.cpp @@ -1,7 +1,9 @@ /*************************************************************************** ** ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). -** Copyright (C) 2012-2015 Jolla Ltd. +** Copyright (C) 2012-2019 Jolla Ltd. +** Copyright (c) 2019 Open Mobile Platform LLC. +** ** Contact: Robin Burchell ** ** This file is part of lipstick. @@ -17,7 +19,6 @@ #include #include #include -#include #include "homewindow.h" #include "utilities/closeeventeater.h" #include "notifications/notificationmanager.h" @@ -27,19 +28,12 @@ #include -static inline QString propertyString(ContextProperty *p) -{ - return p->value().toString().trimmed(); -} - USBModeSelector::USBModeSelector(NemoDeviceLock::DeviceLock *deviceLock, QObject *parent) : QObject(parent), m_usbMode(new QUsbModed(this)), m_deviceLock(deviceLock), m_windowVisible(false), - m_preparingMode(), - m_chargingState(new ContextProperty("Battery.ChargingState", this)), - m_needsCharging(true) + m_preparingMode() { connect(m_usbMode, &QUsbModed::eventReceived, this, &USBModeSelector::handleUSBEvent); connect(m_usbMode, &QUsbModed::currentModeChanged, this, &USBModeSelector::handleUSBState); @@ -47,11 +41,9 @@ USBModeSelector::USBModeSelector(NemoDeviceLock::DeviceLock *deviceLock, QObject connect(m_usbMode, SIGNAL(usbStateError(QString)), this, SIGNAL(showError(QString))); connect(m_usbMode, SIGNAL(supportedModesChanged()), this, SIGNAL(supportedModesChanged())); connect(m_usbMode, &QUsbModed::availableModesChanged, this, &USBModeSelector::availableModesChanged); - connect(m_chargingState, &ContextProperty::valueChanged, this, &USBModeSelector::batteryStateChanged); // Lazy initialize to improve startup time QTimer::singleShot(500, this, &USBModeSelector::handleUSBState); - QTimer::singleShot(500, this, &USBModeSelector::batteryStateChanged); } void USBModeSelector::setWindowVisible(bool visible) @@ -103,10 +95,6 @@ void USBModeSelector::handleUSBEvent(const QString &event) } else if (event == QUsbMode::Mode::ChargerConnected) { // Hide the mode selection dialog and show a mode notification setWindowVisible(false); - // Check and notification originally from batterynotifier.cpp - if (chargingAndNotFull()) { - emit showNotification(Notification::Charging); - } } } @@ -118,6 +106,7 @@ void USBModeSelector::handleUSBState() // Diag, Adb, PCSuite, Charging, Charger, ChargingFallback, Busy QString mode = m_usbMode->currentMode(); + USBModeSelector::Notification type = Notification::Invalid; updateModePreparing(); @@ -125,22 +114,18 @@ void USBModeSelector::handleUSBState() // This probably isn't necessary, as it'll be handled by ModeRequest setWindowVisible(true); } else if (mode == QUsbMode::Mode::ChargingFallback) { - // Check and notification originally from batterynotifier.cpp - if (chargingAndNotFull()) { - emit showNotification(Notification::Charging); - } + // Do nothing } else if (mode == QUsbMode::Mode::Charging) { // Hide the mode selection dialog and show a mode notification setWindowVisible(false); - // Check and notification originally from batterynotifier.cpp - if (chargingAndNotFull()) { - emit showNotification(Notification::Charging); - } } else if (QUsbMode::isFinalState(mode)) { // Hide the mode selection dialog and show a mode notification setWindowVisible(false); - emit showNotification(convertModeToNotification(mode)); + type = convertModeToNotification(mode); } + + if (type != Notification::Invalid && type != Notification::Charging) + emit showNotification(type); } void USBModeSelector::setMode(const QString &mode) @@ -199,7 +184,7 @@ QUsbModed * USBModeSelector::getUsbModed() USBModeSelector::Notification USBModeSelector::convertModeToNotification(const QString &mode) const { - Notification type; + Notification type = Notification::Invalid; if (mode == QUsbModed::Mode::Disconnected) { type = Notification::Disconnected; @@ -219,31 +204,7 @@ USBModeSelector::Notification USBModeSelector::convertModeToNotification(const Q type = Notification::Diag; } else if (mode == QUsbModed::Mode::Host) { type = Notification::Host; - } else if ((mode == QUsbMode::Mode::ChargingFallback) || - (mode == QUsbMode::Mode::Charging) || - (mode == QUsbMode::Mode::ChargerConnected)) { - type = Notification::Charging; - } else { - type = Notification::Invalid; } return type; } - -bool USBModeSelector::chargingAndNotFull() -{ - QString name(propertyString(m_chargingState)); - - return (name == "charging") && m_needsCharging; -} - -void USBModeSelector::batteryStateChanged() -{ - QString name(propertyString(m_chargingState)); - - if (!(name == "charging" || name == "idle")) { - m_needsCharging = true; - } else if (name == "idle") { - m_needsCharging = false; - } -} diff --git a/src/usbmodeselector.h b/src/usbmodeselector.h index d3883d6b..d84eac2d 100644 --- a/src/usbmodeselector.h +++ b/src/usbmodeselector.h @@ -1,7 +1,9 @@ /*************************************************************************** ** ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). -** Copyright (C) 2012-2015 Jolla Ltd. +** Copyright (C) 2012-2019 Jolla Ltd. +** Copyright (c) 2019 Open Mobile Platform LLC. +** ** Contact: Robin Burchell ** ** This file is part of lipstick. @@ -22,7 +24,6 @@ class HomeWindow; class QUsbModed; -class ContextProperty; namespace NemoDeviceLock { class DeviceLock; @@ -191,24 +192,6 @@ class LIPSTICK_EXPORT USBModeSelector : public QObject */ void updateModePreparing(); - /*! - * Callback triggered by a change in state of the statefs - * "Battery.ChargingState" context property. This allows us to decide - * whether or not the battery needs charging, or is fully charged. - * The derived result is stored in \l m_needsCharging and accessed - * using \l chargingAndNotFull(). - */ - void batteryStateChanged(); - - /*! - * Used to determine whether or not the charge is increasing in the - * battery. This is equivalent to the battery being in a charging state, - * and not yet fullly charged. - * - * \returns true if the battery is charging and not full, false o/w. - */ - bool chargingAndNotFull(); - private: //! For getting and setting the USB mode QUsbModed *m_usbMode; @@ -222,12 +205,6 @@ class LIPSTICK_EXPORT USBModeSelector : public QObject //! State indicating whether the device is preparing a USB mode or not QString m_preparingMode; - //! Charging state obtained from statefs, used to derive \c m_needsCharging. - ContextProperty *m_chargingState; - - //! Indicates whether the battery is full or needs charging, from statefs. - bool m_needsCharging; - #ifdef UNIT_TEST friend class Ut_USBModeSelector; #endif diff --git a/tests/stubs/contextproperty_stub.h b/tests/stubs/contextproperty_stub.h deleted file mode 100644 index c1e0892f..00000000 --- a/tests/stubs/contextproperty_stub.h +++ /dev/null @@ -1,187 +0,0 @@ -#ifndef CONTEXTPROPERTY_STUB -#define CONTEXTPROPERTY_STUB - -#include "contextproperty.h" -#include - - -// 1. DECLARE STUB -// FIXME - stubgen is not yet finished -class ContextPropertyStub : public StubBase -{ -public: - virtual void ContextPropertyConstructor(const QString &key, QObject *parent); - virtual void ContextPropertyDestructor(); - virtual QString key() const; - virtual QVariant value(const QVariant &def) const; - virtual QVariant value() const; - virtual const ContextPropertyInfo *info() const; - virtual void subscribe() const; - virtual void unsubscribe() const; - virtual void waitForSubscription() const; - virtual void waitForSubscription(bool block) const; - virtual void ignoreCommander(); - virtual void setTypeCheck(bool typeCheck); -}; - -// 2. IMPLEMENT STUB -void ContextPropertyStub::ContextPropertyConstructor(const QString &key, QObject *parent) -{ - Q_UNUSED(key); - Q_UNUSED(parent); - -} -void ContextPropertyStub::ContextPropertyDestructor() -{ - -} -QString ContextPropertyStub::key() const -{ - stubMethodEntered("key"); - return stubReturnValue("key"); -} - -QVariant ContextPropertyStub::value(const QVariant &def) const -{ - QList params; - params.append( new Parameter(def)); - stubMethodEntered("value", params); - return stubReturnValue("value"); -} - -QVariant ContextPropertyStub::value() const -{ - stubMethodEntered("value"); - return stubReturnValue("value"); -} - -const ContextPropertyInfo *ContextPropertyStub::info() const -{ - stubMethodEntered("info"); - return stubReturnValue("info"); -} - -void ContextPropertyStub::subscribe() const -{ - stubMethodEntered("subscribe"); -} - -void ContextPropertyStub::unsubscribe() const -{ - stubMethodEntered("unsubscribe"); -} - -void ContextPropertyStub::waitForSubscription() const -{ - stubMethodEntered("waitForSubscription"); -} - -void ContextPropertyStub::waitForSubscription(bool block) const -{ - QList params; - params.append( new Parameter(block)); - stubMethodEntered("waitForSubscription", params); -} - -void ContextPropertyStub::ignoreCommander() -{ - stubMethodEntered("ignoreCommander"); -} - -void ContextPropertyStub::setTypeCheck(bool typeCheck) -{ - QList params; - params.append( new Parameter(typeCheck)); - stubMethodEntered("setTypeCheck", params); -} - - - -// 3. CREATE A STUB INSTANCE -ContextPropertyStub gDefaultContextPropertyStub; -typedef QMap > ContextPropertyStubMap; -static ContextPropertyStubMap stubs; -QSharedPointer getContextPropertyStub(QString const &name) -{ - ContextPropertyStubMap::iterator it = stubs.find(name); - if (it == stubs.end()) { - QSharedPointer stub(new ContextPropertyStub()); - it = stubs.insert(name, stub); - } - return *it; -} - -class ContextPropertyPrivate -{ -public: - ContextPropertyPrivate(QString const &k) - : key(k), stub(getContextPropertyStub(key)) - {} - QString key; - QSharedPointer stub; -}; - -// 4. CREATE A PROXY WHICH CALLS THE STUB -ContextProperty::ContextProperty(const QString &key, QObject *parent) - : priv(new ContextPropertyPrivate(key)) -{ - priv->stub->ContextPropertyConstructor(key, parent); -} - -ContextProperty::~ContextProperty() -{ - priv->stub->ContextPropertyDestructor(); -} - -QString ContextProperty::key() const -{ - return priv->key; -} - -QVariant ContextProperty::value(const QVariant &def) const -{ - return priv->stub->value(def); -} - -QVariant ContextProperty::value() const -{ - return priv->stub->value(); -} - -const ContextPropertyInfo *ContextProperty::info() const -{ - return priv->stub->info(); -} - -void ContextProperty::subscribe() const -{ - priv->stub->subscribe(); -} - -void ContextProperty::unsubscribe() const -{ - priv->stub->unsubscribe(); -} - -void ContextProperty::waitForSubscription() const -{ - priv->stub->waitForSubscription(); -} - -void ContextProperty::waitForSubscription(bool block) const -{ - priv->stub->waitForSubscription(block); -} - -void ContextProperty::ignoreCommander() -{ - // do nothing -} - -void ContextProperty::setTypeCheck(bool) -{ - // do nothing -} - - -#endif diff --git a/tests/stubs/lowbatterynotifier_stub.h b/tests/stubs/lowbatterynotifier_stub.h deleted file mode 100644 index 7f3059a4..00000000 --- a/tests/stubs/lowbatterynotifier_stub.h +++ /dev/null @@ -1,82 +0,0 @@ -#ifndef LOWBATTERYNOTIFIER_STUB -#define LOWBATTERYNOTIFIER_STUB - -#include "lowbatterynotifier.h" -#include - - -// 1. DECLARE STUB -// FIXME - stubgen is not yet finished -class LowBatteryNotifierStub : public StubBase -{ -public: - virtual void LowBatteryNotifierConstructor(QObject *parent); - virtual void LowBatteryNotifierDestructor(); - virtual void sendLowBatteryAlert(); - virtual void setNotificationInterval(); - virtual void setTouchScreenLockActive(bool active); -}; - -// 2. IMPLEMENT STUB -void LowBatteryNotifierStub::LowBatteryNotifierConstructor(QObject *parent) -{ - Q_UNUSED(parent); - -} -void LowBatteryNotifierStub::LowBatteryNotifierDestructor() -{ - -} -void LowBatteryNotifierStub::sendLowBatteryAlert() -{ - stubMethodEntered("sendLowBatteryAlert"); -} - -void LowBatteryNotifierStub::setNotificationInterval() -{ - stubMethodEntered("setNotificationInterval"); -} - -void LowBatteryNotifierStub::setTouchScreenLockActive(bool active) -{ - QList params; - params.append( new Parameter(active)); - stubMethodEntered("setTouchScreenLockActive", params); -} - - -// 3. CREATE A STUB INSTANCE -LowBatteryNotifierStub gDefaultLowBatteryNotifierStub; -LowBatteryNotifierStub *gLowBatteryNotifierStub = &gDefaultLowBatteryNotifierStub; - - -// 4. CREATE A PROXY WHICH CALLS THE STUB -LowBatteryNotifier::LowBatteryNotifier(QObject *parent) -#ifdef HAVE_CONTEXTSUBSCRIBER - : m_callContextItem("") -#endif -{ - gLowBatteryNotifierStub->LowBatteryNotifierConstructor(parent); -} - -LowBatteryNotifier::~LowBatteryNotifier() -{ - gLowBatteryNotifierStub->LowBatteryNotifierDestructor(); -} - -void LowBatteryNotifier::sendLowBatteryAlert() -{ - gLowBatteryNotifierStub->sendLowBatteryAlert(); -} - -void LowBatteryNotifier::setNotificationInterval() -{ - gLowBatteryNotifierStub->setNotificationInterval(); -} - -void LowBatteryNotifier::setTouchScreenLockActive(bool active) -{ - gLowBatteryNotifierStub->setTouchScreenLockActive(active); -} - -#endif diff --git a/tests/tests.pro b/tests/tests.pro index a97fdb82..95e11917 100644 --- a/tests/tests.pro +++ b/tests/tests.pro @@ -1,10 +1,8 @@ TEMPLATE = subdirs SUBDIRS = \ - ut_batterynotifier \ ut_closeeventeater \ ut_launchermodel \ ut_lipsticksettings \ - ut_lowbatterynotifier \ ut_lipsticknotification \ ut_notificationfeedbackplayer \ ut_notificationlistmodel \ diff --git a/tests/ut_batterynotifier/.gitignore b/tests/ut_batterynotifier/.gitignore deleted file mode 100644 index 77294aa3..00000000 --- a/tests/ut_batterynotifier/.gitignore +++ /dev/null @@ -1 +0,0 @@ -ut_batterynotifier diff --git a/tests/ut_batterynotifier/ut_batterynotifier.cpp b/tests/ut_batterynotifier/ut_batterynotifier.cpp deleted file mode 100644 index 1ba54859..00000000 --- a/tests/ut_batterynotifier/ut_batterynotifier.cpp +++ /dev/null @@ -1,326 +0,0 @@ -/*************************************************************************** -** -** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). -** Copyright (C) 2012 Jolla Ltd. -** Contact: Robin Burchell -** -** This file is part of lipstick. -** -** This library is free software; you can redistribute it and/or -** modify it under the terms of the GNU Lesser General Public -** License version 2.1 as published by the Free Software Foundation -** and appearing in the file LICENSE.LGPL included in the packaging -** of this file. -** -****************************************************************************/ - -#include - -#include "lowbatterynotifier_stub.h" -#include "notificationmanager_stub.h" -#include "lipsticknotification.h" -#include "qmdisplaystate_stub.h" -#include "batterynotifier.h" -#include "ut_batterynotifier.h" - -#ifdef HAVE_CONTEXTSUBSCRIBER -#include "contextproperty_stub.h" -#endif - -static void setStubProperty(QString const &name, QString const &value) -{ - getContextPropertyStub(name)->stubSetReturnValue("value", QVariant(value)); -} - -void Ut_BatteryNotifier::setNewStubState -(QString const &level, QString const &state, QString const &charger) -{ - if (!level.isNull()) setStubProperty("Battery.Level", level); - if (!state.isNull()) setStubProperty("Battery.ChargingState", state); - if (!charger.isNull()) setStubProperty("Battery.ChargerType", charger); - batteryNotifier->prepareNotification(); -} - -static QString getNotificationHint(QString const &h) -{ - return gNotificationManagerStub->stubLastCallTo("Notify") - .parameter(6).value(h).toString(); -} - -static QString getNotificationCategory() -{ - return getNotificationHint(LipstickNotification::HINT_CATEGORY); -} - -static QString getNotificationPreviewBody() -{ - return getNotificationHint(LipstickNotification::HINT_PREVIEW_BODY); -} - -void QTimer::start() -{ - id = 1; -} - -void Ut_BatteryNotifier::initTestCase() -{ -} - -void Ut_BatteryNotifier::cleanupTestCase() -{ -} - -void Ut_BatteryNotifier::init() -{ -#ifdef HAVE_CONTEXTSUBSCRIBER - setStubProperty("Phone.Call", "inactive"); -#endif - batteryNotifier.reset(new BatteryNotifier); - - gNotificationManagerStub->stubReset(); - gNotificationManagerStub->stubSetReturnValue("Notify", (uint)1); -} - -void Ut_BatteryNotifier::cleanup() -{ - gNotificationManagerStub->stubReset(); - gLowBatteryNotifierStub->stubReset(); -} - -void Ut_BatteryNotifier::testInitBattery() -{ - batteryNotifier->prepareNotification(); - QCOMPARE(gNotificationManagerStub->stubCallCount("Notify"), 0); -} - -void Ut_BatteryNotifier::testLowBatteryAlert() -{ - batteryNotifier->lowBatteryAlert(); - - QCOMPARE(gNotificationManagerStub->stubCallCount("Notify"), 1); - QCOMPARE(getNotificationCategory(), QString("x-nemo.battery.lowbattery")); - QCOMPARE(getNotificationPreviewBody(), qtTrId("qtn_ener_lowbatt")); - QCOMPARE(gNotificationManagerStub->stubLastCallTo("Notify").parameter(2), QString()); -} - -void Ut_BatteryNotifier::testBatteryStateChanged() -{ - QCOMPARE(gNotificationManagerStub->stubCallCount("Notify"), 0); - setStubProperty("Battery.ChargingState", "idle"); - batteryNotifier->prepareNotification(); - - /* StateFull, full */ - QCOMPARE(gNotificationManagerStub->stubCallCount("Notify"), 1); - QCOMPARE(getNotificationCategory(), QString("x-nemo.battery.chargingcomplete")); - QCOMPARE(getNotificationPreviewBody(), qtTrId("qtn_ener_charcomp")); - QCOMPARE(gNotificationManagerStub->stubLastCallTo("Notify").parameter(2), QString()); - gNotificationManagerStub->stubReset(); - - /* StateOK */ - setStubProperty("Battery.ChargingState", "discharging"); - batteryNotifier->prepareNotification(); - /* no notifications should be published, just silently no-op */ - QCOMPARE(gNotificationManagerStub->stubCallCount("Notify"), 0); - - /* StateEmpty */ - setStubProperty("Battery.Level", "empty"); - batteryNotifier->prepareNotification(); - QCOMPARE(gNotificationManagerStub->stubCallCount("Notify"), 1); - QCOMPARE(getNotificationCategory(), QString("x-nemo.battery.recharge")); - QCOMPARE(getNotificationPreviewBody(), qtTrId("qtn_ener_rebatt")); - QCOMPARE(gNotificationManagerStub->stubLastCallTo("Notify").parameter(2), QString()); - gNotificationManagerStub->stubReset(); - - /* StateError */ - setStubProperty("Battery.Level", "unknown"); - batteryNotifier->prepareNotification(); - /* no notifications should be published, just silently no-op */ - QCOMPARE(gNotificationManagerStub->stubCallCount("Notify"), 0); - - /* StateLow and charging */ - setNewStubState("low", "charging", "dcp"); - QCOMPARE(gNotificationManagerStub->stubCallCount("Notify"), 1); - QCOMPARE(getNotificationPreviewBody(), qtTrId("qtn_ener_charging")); - gNotificationManagerStub->stubReset(); - - /* StateLow and not charging */ - batteryNotifier->setTouchScreenLockActive(true); - setStubProperty("Battery.ChargingState", "discharging"); - batteryNotifier->prepareNotification(); - - QCOMPARE(gLowBatteryNotifierStub->stubCallCount("setTouchScreenLockActive"), 1); - QCOMPARE(gLowBatteryNotifierStub->stubLastCallTo("setTouchScreenLockActive").parameter(0), true); - QCOMPARE(gLowBatteryNotifierStub->stubCallCount("sendLowBatteryAlert"), 1); -} - -void Ut_BatteryNotifier::testChargingStateChanged() -{ - setNewStubState("unknown", "discharging", ""); - gNotificationManagerStub->stubReset(); - - static const QString levels[] = { "normal", "low", "empty" }; - for (size_t i = 0; i < sizeof(levels) / sizeof(levels[0]); ++i) { - /* StateCharging */ - setNewStubState(levels[i], "charging", "dcp"); - QCOMPARE(gNotificationManagerStub->stubCallCount("Notify"), 1); - QCOMPARE(getNotificationCategory(), QString("x-nemo.battery")); - QCOMPARE(getNotificationPreviewBody(), qtTrId("qtn_ener_charging")); - gNotificationManagerStub->stubReset(); - - setNewStubState("unknown", "discharging", ""); - QCOMPARE(gNotificationManagerStub->stubCallCount("Notify"), 1); - QCOMPARE(getNotificationCategory(), QString("x-nemo.battery.removecharger")); - QCOMPARE(getNotificationPreviewBody(), qtTrId("qtn_ener_remcha")); - gNotificationManagerStub->stubReset(); - } - - for (size_t i = 0; i < sizeof(levels) / sizeof(levels[0]); ++i) { - /* StateCharging */ - setNewStubState(levels[i], "charging", "usb"); - QCOMPARE(gNotificationManagerStub->stubCallCount("Notify"), 1); - QCOMPARE(getNotificationCategory(), QString("x-nemo.battery")); - QCOMPARE(getNotificationPreviewBody(), qtTrId("qtn_ener_charging")); - gNotificationManagerStub->stubReset(); - - setNewStubState("unknown", "discharging", ""); - QCOMPARE(gNotificationManagerStub->stubCallCount("Notify"), 0); - } - - /* StateUnknownChargingState */ - setNewStubState(QString(), "unknown", ""); - QCOMPARE(gNotificationManagerStub->stubCallCount("Notify"), 0); -} - -void Ut_BatteryNotifier::testPSMStateChanged() -{ - setStubProperty("System.PowerSaveMode", "0"); - batteryNotifier->onPowerSaveModeChanged(); - QCOMPARE(gNotificationManagerStub->stubCallCount("Notify"), 0); - - /* Entering to power-save mode */ - setStubProperty("System.PowerSaveMode", "1"); - batteryNotifier->onPowerSaveModeChanged(); - - QCOMPARE(gNotificationManagerStub->stubCallCount("Notify"), 1); - QCOMPARE(getNotificationCategory(), QString("x-nemo.battery.enterpsm")); - QCOMPARE(getNotificationPreviewBody(), qtTrId("qtn_ener_ent_psnote")); - - /* Entering to power-save mode */ - setStubProperty("System.PowerSaveMode", "0"); - batteryNotifier->onPowerSaveModeChanged(); - - QCOMPARE(gNotificationManagerStub->stubCallCount("Notify"), 2); - QCOMPARE(getNotificationCategory(), QString("x-nemo.battery.exitpsm")); - QCOMPARE(getNotificationPreviewBody(), qtTrId("qtn_ener_exit_psnote")); -} - -void Ut_BatteryNotifier::testLowBatteryNotifierConnection() -{ - setNewStubState("normal", "charging", "usb"); - - /* LowBatteryNotifier shouldn't be instantiated at first */ - QCOMPARE(batteryNotifier->m_lowBatteryNotifier, (LowBatteryNotifier *)NULL); - - /* Simulate battery-state-low change */ - batteryNotifier->setTouchScreenLockActive(true); - setNewStubState("low", "discharging", ""); - - /* LowBatteryNotifier should be exists now... */ - QVERIFY(batteryNotifier->m_lowBatteryNotifier != NULL); - QCOMPARE(gLowBatteryNotifierStub->stubCallCount("setTouchScreenLockActive"), 1); - QCOMPARE(gLowBatteryNotifierStub->stubLastCallTo("setTouchScreenLockActive").parameter(0), true); - - /* And should send a low-battery notification */ - QCOMPARE(gLowBatteryNotifierStub->stubCallCount("sendLowBatteryAlert"), 1); - - /* Simulate now a charging event */ - setNewStubState(QString(), "charging", "usb"); - - /* After this call LowBatteryNotifier should be destroyed */ - QCOMPARE(batteryNotifier->m_lowBatteryNotifier, (LowBatteryNotifier *)NULL); - - /* State OK should stop notifications */ - setNewStubState("low", "discharging", ""); - QVERIFY(batteryNotifier->m_lowBatteryNotifier != NULL); - QCOMPARE(gLowBatteryNotifierStub->stubCallCount("sendLowBatteryAlert"), 2); - setStubProperty("Battery.Level", "normal"); - batteryNotifier->prepareNotification(); - QCOMPARE(batteryNotifier->m_lowBatteryNotifier, (LowBatteryNotifier *)NULL); -} - -void Ut_BatteryNotifier::testWhenChargingStopsThenNotificationRemoved() -{ - setNewStubState("normal", "unknown", ""); - gNotificationManagerStub->stubReset(); - - setNewStubState("normal", "charging", "usb"); - QCOMPARE(gNotificationManagerStub->stubCallCount("Notify"), 1); - QCOMPARE(getNotificationCategory(), QString("x-nemo.battery")); - QCOMPARE(getNotificationPreviewBody(), qtTrId("qtn_ener_charging")); - - setNewStubState("normal", "discharging", ""); - - QCOMPARE(gNotificationManagerStub->stubCallCount("CloseNotification"), 1); -} - -void Ut_BatteryNotifier::testWhenChargingStopsWhenConnectedToWallChargerThenNotificationRemoved() -{ - setNewStubState("normal", "unknown", ""); - gNotificationManagerStub->stubReset(); - - setNewStubState(QString(), "charging", "dcp"); - setNewStubState(QString(), "discharging", ""); - QCOMPARE(gNotificationManagerStub->stubCallCount("CloseNotification"), 1); - - setNewStubState(QString(), "charging", "dcp"); - QCOMPARE(gNotificationManagerStub->stubCallCount("CloseNotification"), 2); -} - -void Ut_BatteryNotifier::testWhenChargingStopsAndBatteryIsLowNotifierIsCreated() -{ - setNewStubState("low", "charging", "dcp"); - setNewStubState(QString(), "discharging", ""); - QVERIFY(batteryNotifier->m_lowBatteryNotifier != NULL); -} - -void Ut_BatteryNotifier::testWhenStateChargingLowBatteryNotificationRemoved() -{ - setNewStubState("low", "discharging", ""); - QVERIFY(batteryNotifier->m_lowBatteryNotifier != NULL); - gNotificationManagerStub->stubReset(); - batteryNotifier->m_notifications.clear(); - batteryNotifier->lowBatteryAlert(); - setNewStubState("low", "charging", "usb"); - QCOMPARE(gNotificationManagerStub->stubCallCount("CloseNotification"), 1); -} - -void Ut_BatteryNotifier::testWhenBatteryFullWhenChargingNotifiedThenNotificationRemoved() -{ - setNewStubState("normal", "discharging", ""); - setNewStubState("normal", "charging", "usb"); - QCOMPARE(gNotificationManagerStub->stubCallCount("CloseNotification"), 0); - setNewStubState("normal", "idle", "usb"); - QCOMPARE(gNotificationManagerStub->stubCallCount("CloseNotification"), 1); -} - -void Ut_BatteryNotifier::testSetTouchScreenLockActive() -{ - setNewStubState("normal", "discharging", ""); - batteryNotifier->setTouchScreenLockActive(true); - QCOMPARE(gLowBatteryNotifierStub->stubCallCount("setTouchScreenLockActive"), 0); - - setNewStubState("low", QString(), QString()); - - QCOMPARE(gLowBatteryNotifierStub->stubCallCount("setTouchScreenLockActive"), 1); - QCOMPARE(gLowBatteryNotifierStub->stubLastCallTo("setTouchScreenLockActive").parameter(0), true); - - batteryNotifier->setTouchScreenLockActive(false); - QCOMPARE(gLowBatteryNotifierStub->stubCallCount("setTouchScreenLockActive"), 2); - QCOMPARE(gLowBatteryNotifierStub->stubLastCallTo("setTouchScreenLockActive").parameter(0), false); - - batteryNotifier->setTouchScreenLockActive(true); - QCOMPARE(gLowBatteryNotifierStub->stubCallCount("setTouchScreenLockActive"), 3); - QCOMPARE(gLowBatteryNotifierStub->stubLastCallTo("setTouchScreenLockActive").parameter(0), true); -} - -QTEST_MAIN(Ut_BatteryNotifier) diff --git a/tests/ut_batterynotifier/ut_batterynotifier.h b/tests/ut_batterynotifier/ut_batterynotifier.h deleted file mode 100644 index 5a052440..00000000 --- a/tests/ut_batterynotifier/ut_batterynotifier.h +++ /dev/null @@ -1,53 +0,0 @@ -/*************************************************************************** -** -** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). -** Copyright (C) 2012 Jolla Ltd. -** Contact: Robin Burchell -** -** This file is part of lipstick. -** -** This library is free software; you can redistribute it and/or -** modify it under the terms of the GNU Lesser General Public -** License version 2.1 as published by the Free Software Foundation -** and appearing in the file LICENSE.LGPL included in the packaging -** of this file. -** -****************************************************************************/ -#ifndef UT_BATTERYNOTIFIER_H -#define UT_BATTERYNOTIFIER_H - -#include -#include -#include "batterynotifier.h" - -class BatteryNotifier; - -class Ut_BatteryNotifier : public QObject -{ - Q_OBJECT - -private slots: - void init(); - void cleanup(); - void initTestCase(); - void cleanupTestCase(); - - void testInitBattery(); - void testLowBatteryAlert(); - void testBatteryStateChanged(); - void testChargingStateChanged(); - void testPSMStateChanged(); - void testLowBatteryNotifierConnection(); - void testWhenChargingStopsThenNotificationRemoved(); - void testWhenChargingStopsWhenConnectedToWallChargerThenNotificationRemoved(); - void testWhenChargingStopsAndBatteryIsLowNotifierIsCreated(); - void testWhenBatteryFullWhenChargingNotifiedThenNotificationRemoved(); - void testSetTouchScreenLockActive(); - void testWhenStateChargingLowBatteryNotificationRemoved(); - -private: - void setNewStubState(QString const &, QString const &, QString const &); - QScopedPointer batteryNotifier; -}; - -#endif diff --git a/tests/ut_batterynotifier/ut_batterynotifier.pro b/tests/ut_batterynotifier/ut_batterynotifier.pro deleted file mode 100644 index cae26aca..00000000 --- a/tests/ut_batterynotifier/ut_batterynotifier.pro +++ /dev/null @@ -1,27 +0,0 @@ -include(../common.pri) -TARGET = ut_batterynotifier -CONFIG += link_pkgconfig -INCLUDEPATH += $$NOTIFICATIONSRCDIR /usr/include/QtSystemInfo $$QMSYSTEM2 -QT += dbus systeminfo - -HEADERS += \ - $$NOTIFICATIONSRCDIR/batterynotifier.h \ - $$NOTIFICATIONSRCDIR/lowbatterynotifier.h \ - $$NOTIFICATIONSRCDIR/notificationmanager.h \ - $$NOTIFICATIONSRCDIR/lipsticknotification.h \ - $$QMSYSTEM2/qmdisplaystate.h \ - $$STUBSDIR/stubbase.h \ - ut_batterynotifier.h - -SOURCES += \ - $$NOTIFICATIONSRCDIR/batterynotifier.cpp \ - $$NOTIFICATIONSRCDIR/lipsticknotification.cpp \ - $$STUBSDIR/stubbase.cpp \ - ut_batterynotifier.cpp - -packagesExist(contextkit-statefs) { - DEFINES += HAVE_CONTEXTSUBSCRIBER - HEADERS += /usr/include/contextproperty.h -} else { - warning("Contextsubscriber not found") -} diff --git a/tests/ut_lowbatterynotifier/.gitignore b/tests/ut_lowbatterynotifier/.gitignore deleted file mode 100644 index 50a9be3e..00000000 --- a/tests/ut_lowbatterynotifier/.gitignore +++ /dev/null @@ -1 +0,0 @@ -ut_lowbatterynotifier diff --git a/tests/ut_lowbatterynotifier/ut_lowbatterynotifier.cpp b/tests/ut_lowbatterynotifier/ut_lowbatterynotifier.cpp deleted file mode 100644 index cd72a069..00000000 --- a/tests/ut_lowbatterynotifier/ut_lowbatterynotifier.cpp +++ /dev/null @@ -1,157 +0,0 @@ -/**************************************************************************** - ** - ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). - ** All rights reserved. - ** Contact: Nokia Corporation (directui@nokia.com) - ** - ** This file is part of systemui. - ** - ** If you have questions regarding the use of this file, please contact - ** Nokia at directui@nokia.com. - ** - ** This library is free software; you can redistribute it and/or - ** modify it under the terms of the GNU Lesser General Public - ** License version 2.1 as published by the Free Software Foundation - ** and appearing in the file LICENSE.LGPL included in the packaging - ** of this file. - ** - ****************************************************************************/ -#include - -#include "qmdisplaystate_stub.h" -#ifdef HAVE_CONTEXTSUBSCRIBER -#include "contextproperty_stub.h" -#endif - -#include "lowbatterynotifier.h" -#include "ut_lowbatterynotifier.h" - -static QSharedPointer gContextPropertyStub = - getContextPropertyStub("Phone.Call"); - -void QTimer::start() -{ - id = 1; -} - -int qTimerStartMsec = 0; -void QTimer::start(int msec) -{ - qTimerStartMsec = msec; - setInterval(msec); - id = 1; -} - -int qTimeElapsed = 0; -int QTime::elapsed() const -{ - return qTimeElapsed; -} - -void Ut_LowBatteryNotifier::init() -{ -#ifdef HAVE_CONTEXTSUBSCRIBER - gContextPropertyStub->stubSetReturnValue("value", QVariant("inactive")); -#endif - m_subject = new LowBatteryNotifier; -} - -void Ut_LowBatteryNotifier::cleanup() -{ - delete m_subject; - qTimerStartMsec = 0; - qTimeElapsed = 0; -} - -void Ut_LowBatteryNotifier::initTestCase() -{ -} - -void Ut_LowBatteryNotifier::cleanupTestCase() -{ -} - -void Ut_LowBatteryNotifier::testSignalConnections() -{ - QCOMPARE(disconnect(m_subject->m_displayState, SIGNAL(displayStateChanged(MeeGo::QmDisplayState::DisplayState)), m_subject, SLOT(setNotificationInterval())), true); - -#ifdef HAVE_CONTEXTSUBSCRIBER - QCOMPARE(disconnect(&m_subject->m_callContextItem, SIGNAL(valueChanged()), m_subject, SLOT(setNotificationInterval())), true); -#endif - - QCOMPARE(disconnect(m_subject->m_notificationTimer, SIGNAL(timeout()), m_subject, SLOT(sendLowBatteryAlert())), true); -} - -void Ut_LowBatteryNotifier::testSendLowBatteryAlert() -{ - QSignalSpy spy(m_subject, SIGNAL(lowBatteryAlert())); - m_subject->m_notificationInterval = 12345; - m_subject->sendLowBatteryAlert(); - QCOMPARE(spy.count(), 1); - QCOMPARE(qTimerStartMsec, m_subject->m_notificationInterval); -} - -#ifdef HAVE_CONTEXTSUBSCRIBER -Q_DECLARE_METATYPE(MeeGo::QmDisplayState::DisplayState) - -void Ut_LowBatteryNotifier::testSetNotificationInterval_data() -{ - QTest::addColumn("touchScreenLockActive"); - QTest::addColumn("displayState"); - QTest::addColumn("callActive"); - QTest::addColumn("timeSincePreviousNotification"); - QTest::addColumn("notificationTimerInterval"); - QTest::addColumn("lowBatteryAlertCount"); - - QTest::newRow("Device inactive because touch screen lock is active and display is off") << true << MeeGo::QmDisplayState::Off << false << 1000 << (30 * 60 * 1000 - 1000) << 0; - QTest::newRow("Device active because touch screen lock is active but display is on") << true << MeeGo::QmDisplayState::On << false << 2000 << (5 * 60 * 1000 - 2000) << 0; - QTest::newRow("Device active because touch screen lock is not active but display is off") << false << MeeGo::QmDisplayState::Off << false << 3000 << (5 * 60 * 1000 - 3000) << 0; - QTest::newRow("Device inactive but call active") << true << MeeGo::QmDisplayState::Off << true << 4000 << (2 * 60 * 1000 - 4000) << 0; - QTest::newRow("Device active but call active") << false << MeeGo::QmDisplayState::On << true << 5000 << (2 * 60 * 1000 - 5000) << 0; - QTest::newRow("Device inactive, interval elapsed") << true << MeeGo::QmDisplayState::Off << false << (31 * 60 * 1000) << (30 * 60 * 1000) << 1; - QTest::newRow("Device active, interval elapsed") << false << MeeGo::QmDisplayState::On << false << (6 * 60 * 1000) << (5 * 60 * 1000) << 1; - QTest::newRow("Call active, interval elapsed") << false << MeeGo::QmDisplayState::On << true << (3 * 60 * 1000) << (2 * 60 * 1000) << 1; -} - -void Ut_LowBatteryNotifier::testSetNotificationInterval() -{ - QFETCH(bool, touchScreenLockActive); - QFETCH(MeeGo::QmDisplayState::DisplayState, displayState); - QFETCH(bool, callActive); - QFETCH(int, timeSincePreviousNotification); - QFETCH(int, notificationTimerInterval); - QFETCH(int, lowBatteryAlertCount); - - // Force a change by first setting the activity state to the opposite value - gQmDisplayStateStub->stubSetReturnValue("get", displayState == MeeGo::QmDisplayState::On ? MeeGo::QmDisplayState::Off : MeeGo::QmDisplayState::On); - gContextPropertyStub->stubSetReturnValue("value", QVariant(callActive ? "inactive" : "active")); - m_subject->setTouchScreenLockActive(!touchScreenLockActive); - - // Set the values according to the test data and verify - QSignalSpy spy(m_subject, SIGNAL(lowBatteryAlert())); - gQmDisplayStateStub->stubSetReturnValue("get", displayState); - gContextPropertyStub->stubSetReturnValue("value", QVariant(callActive ? "active" : "inactive")); - qTimeElapsed = timeSincePreviousNotification; - m_subject->setTouchScreenLockActive(touchScreenLockActive); - QCOMPARE(m_subject->m_notificationTimer->interval(), notificationTimerInterval); - QCOMPARE(spy.count(), lowBatteryAlertCount); -} - -void Ut_LowBatteryNotifier::testSetNotificationIntervalDoesNothingWhenStateDoesNotChange() -{ - // Set some initial values - gQmDisplayStateStub->stubSetReturnValue("get", MeeGo::QmDisplayState::On); - gContextPropertyStub->stubSetReturnValue("value", QVariant("inactive")); - qTimeElapsed = 0; - m_subject->setTouchScreenLockActive(false); - - // Set the timer to a new value but don't change anything else: setting the notification interval should do nothing - QSignalSpy spy(m_subject, SIGNAL(lowBatteryAlert())); - m_subject->m_notificationTimer->setInterval(12345); - m_subject->setNotificationInterval(); - QCOMPARE(spy.count(), 0); - QCOMPARE(m_subject->m_notificationTimer->interval(), 12345); -} -#endif - -QTEST_MAIN(Ut_LowBatteryNotifier) diff --git a/tests/ut_lowbatterynotifier/ut_lowbatterynotifier.h b/tests/ut_lowbatterynotifier/ut_lowbatterynotifier.h deleted file mode 100644 index bf284c2c..00000000 --- a/tests/ut_lowbatterynotifier/ut_lowbatterynotifier.h +++ /dev/null @@ -1,54 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). -** All rights reserved. -** Contact: Nokia Corporation (directui@nokia.com) -** -** This file is part of systemui. -** -** If you have questions regarding the use of this file, please contact -** Nokia at directui@nokia.com. -** -** This library is free software; you can redistribute it and/or -** modify it under the terms of the GNU Lesser General Public -** License version 2.1 as published by the Free Software Foundation -** and appearing in the file LICENSE.LGPL included in the packaging -** of this file. -** -****************************************************************************/ -#ifndef UT_LOWBATTERYNOTIFIER_H -#define UT_LOWBATTERYNOTIFIER_H - -#include - -class LowBatteryNotifier; - -#ifdef HAVE_CONTEXTSUBSCRIBER -class ContextItem; -#endif - -class Ut_LowBatteryNotifier : public QObject -{ - Q_OBJECT - -private slots: - void init(); - void cleanup(); - void initTestCase(); - void cleanupTestCase(); - void testSignalConnections(); - void testSendLowBatteryAlert(); -#ifdef HAVE_CONTEXTSUBSCRIBER - void testSetNotificationInterval_data(); - void testSetNotificationInterval(); - void testSetNotificationIntervalDoesNothingWhenStateDoesNotChange(); -#endif - -private: - LowBatteryNotifier *m_subject; -#ifdef HAVE_CONTEXTSUBSCRIBER - ContextItem *contextItem; -#endif -}; - -#endif diff --git a/tests/ut_lowbatterynotifier/ut_lowbatterynotifier.pro b/tests/ut_lowbatterynotifier/ut_lowbatterynotifier.pro deleted file mode 100644 index 4f22122a..00000000 --- a/tests/ut_lowbatterynotifier/ut_lowbatterynotifier.pro +++ /dev/null @@ -1,20 +0,0 @@ -include(../common.pri) -TARGET = ut_lowbatterynotifier -INCLUDEPATH += $$NOTIFICATIONSRCDIR $$QMSYSTEM2 - -SOURCES += \ - ut_lowbatterynotifier.cpp \ - $$NOTIFICATIONSRCDIR/lowbatterynotifier.cpp \ - $$STUBSDIR/stubbase.cpp - -HEADERS += \ - ut_lowbatterynotifier.h \ - $$NOTIFICATIONSRCDIR/lowbatterynotifier.h \ - $$QMSYSTEM2/qmdisplaystate.h \ - -packagesExist(contextkit-statefs) { - DEFINES += HAVE_CONTEXTSUBSCRIBER - HEADERS += /usr/include/contextproperty.h -} else { - warning("Contextsubscriber not found") -} diff --git a/tests/ut_usbmodeselector/ut_usbmodeselector.cpp b/tests/ut_usbmodeselector/ut_usbmodeselector.cpp index 321fbed4..21d097e9 100644 --- a/tests/ut_usbmodeselector/ut_usbmodeselector.cpp +++ b/tests/ut_usbmodeselector/ut_usbmodeselector.cpp @@ -28,15 +28,6 @@ #include -#ifdef HAVE_CONTEXTSUBSCRIBER -#include "contextproperty_stub.h" -#endif - -static void setStubProperty(QString const &name, QString const &value) -{ - getContextPropertyStub(name)->stubSetReturnValue("value", QVariant(value)); -} - HomeWindow::HomeWindow() { } @@ -107,9 +98,6 @@ void Ut_USBModeSelector::cleanupTestCase() void Ut_USBModeSelector::init() { -#ifdef HAVE_CONTEXTSUBSCRIBER - setStubProperty("Battery.ChargingState", "idle"); -#endif deviceLock = new NemoDeviceLock::DeviceLock(this); usbModeSelector = new USBModeSelector(deviceLock); usbModeSelector->m_usbMode->setCurrentMode(QUsbModed::Mode::Undefined); diff --git a/tests/ut_usbmodeselector/ut_usbmodeselector.pro b/tests/ut_usbmodeselector/ut_usbmodeselector.pro index 6f70c87a..73daa1d5 100644 --- a/tests/ut_usbmodeselector/ut_usbmodeselector.pro +++ b/tests/ut_usbmodeselector/ut_usbmodeselector.pro @@ -23,10 +23,3 @@ HEADERS += \ $$STUBSDIR/nemo-devicelock/devicelock.h \ ut_usbmodeselector.h \ $$SRCDIR/homewindow.h \ - -packagesExist(contextkit-statefs) { - DEFINES += HAVE_CONTEXTSUBSCRIBER - HEADERS += /usr/include/contextproperty.h -} else { - warning("Contextsubscriber not found") -}