From 41cab406395094d37ea22f6c33b30368facc87aa Mon Sep 17 00:00:00 2001 From: Ronan Abhamon Date: Wed, 16 Nov 2016 16:31:23 +0100 Subject: [PATCH] feat(notifier): supports visible property changed to false --- tests/src/components/notifier/Notifier.cpp | 79 ++++++++++++++----- tests/src/components/notifier/Notifier.hpp | 9 ++- .../Notifications/Notification.spec.qml | 6 +- 3 files changed, 69 insertions(+), 25 deletions(-) diff --git a/tests/src/components/notifier/Notifier.cpp b/tests/src/components/notifier/Notifier.cpp index 39151cd0e..dc5c785fa 100644 --- a/tests/src/components/notifier/Notifier.cpp +++ b/tests/src/components/notifier/Notifier.cpp @@ -1,3 +1,4 @@ +#include #include #include @@ -13,6 +14,7 @@ // Arbitrary hardcoded values. #define NOTIFICATION_SPACING 10 #define N_MAX_NOTIFICATIONS 15 +#define MAX_TIMEOUT 60000 // =================================================================== @@ -71,24 +73,18 @@ Notifier::~Notifier () { // ------------------------------------------------------------------- -void Notifier::showCallMessage ( - int timeout, - const QString &sip_address -) { - qDebug() << "Show call notification message. (addr=" << - sip_address << ")"; - +QObject *Notifier::createNotification (Notifier::NotificationType type) { m_mutex.lock(); // Check existing instances. if (m_n_instances >= N_MAX_NOTIFICATIONS) { qWarning() << "Unable to create another notification"; m_mutex.unlock(); - return; + return nullptr; } // Create instance and set attributes. - QObject *object = m_components[Notifier::Call]->create(); + QObject *object = m_components[type]->create(); int offset = getNotificationSize(*object, NOTIFICATION_HEIGHT_PROPERTY); if ( @@ -97,7 +93,7 @@ void Notifier::showCallMessage ( ) { delete object; m_mutex.unlock(); - return; + return nullptr; } m_offset = (offset + m_offset) + NOTIFICATION_SPACING; @@ -105,22 +101,63 @@ void Notifier::showCallMessage ( m_mutex.unlock(); + return object; +} + +void Notifier::showNotification (QObject *notification, int timeout) { + if (timeout > MAX_TIMEOUT) { + timeout = MAX_TIMEOUT; + } + // Display notification. QMetaObject::invokeMethod( - object, NOTIFICATION_SHOW_METHOD_NAME, + notification, NOTIFICATION_SHOW_METHOD_NAME, Qt::DirectConnection ); + // Called explicitly (by a click on notification for example) + // or when single shot happen and if notification is visible. + QObject::connect( + notification->findChild(), + &QQuickWindow::visibleChanged, + [this](const bool &value) { + qDebug() << "Update notifications counter, hidden notification detected."; + + if (value) { + qFatal("A notification cannot be visible twice!"); + return; + } + + m_mutex.lock(); + + m_n_instances--; + + if (m_n_instances == 0) + m_offset = 0; + + m_mutex.unlock(); + } + ); + // Destroy it after timeout. - QTimer::singleShot(timeout, this, [object, this]() { - delete object; - - m_mutex.lock(); - m_n_instances--; - - if (m_n_instances == 0) - m_offset = 0; - - m_mutex.unlock(); + QTimer::singleShot(timeout, this, [notification]() { + delete notification; }); } + +// ------------------------------------------------------------------- + +void Notifier::showCallMessage ( + int timeout, + const QString &sip_address +) { + qDebug() << "Show call notification message. (addr=" << + sip_address << ")"; + + QObject *object = createNotification(Notifier::Call); + + if (!object) + return; + + showNotification(object, timeout); +} diff --git a/tests/src/components/notifier/Notifier.hpp b/tests/src/components/notifier/Notifier.hpp index df7a17a6e..a3edbce3c 100644 --- a/tests/src/components/notifier/Notifier.hpp +++ b/tests/src/components/notifier/Notifier.hpp @@ -14,20 +14,23 @@ public: Notifier (QObject *parent = Q_NULLPTR); virtual ~Notifier (); - enum Type { + enum NotificationType { Call, MaxNbTypes }; - Q_ENUM(Type); public slots: void showCallMessage (int timeout, const QString &sip_address); private: + QObject *createNotification (NotificationType type); + void handleNotificationHidden (); + void showNotification (QObject *notification, int timeout); + QQmlComponent *m_components[MaxNbTypes]; int m_offset = 0; - int m_n_instances = 0; + unsigned int m_n_instances = 0; QMutex m_mutex; }; diff --git a/tests/ui/modules/Linphone/Notifications/Notification.spec.qml b/tests/ui/modules/Linphone/Notifications/Notification.spec.qml index 1323f496d..56bc7bbfc 100644 --- a/tests/ui/modules/Linphone/Notifications/Notification.spec.qml +++ b/tests/ui/modules/Linphone/Notifications/Notification.spec.qml @@ -13,7 +13,7 @@ TestCase { function test_notificationHeightProperty () { compare(Utils.isInteger(notification.notificationHeight), true) - } + } function test_notificationOffsetProperty () { compare(Utils.isInteger(notification.notificationOffset), true) @@ -22,4 +22,8 @@ TestCase { function test_notificationShowMethod () { compare(Utils.isFunction(notification.show), true) } + + function test_childWindow () { + compare(Utils.qmlTypeof(notification.data[0], 'QQuickWindowQmlImpl'), true) + } }