void Ut_NotificationPreviewPresenter::testSignalConnections()
{
    NotificationPreviewPresenter presenter;
    QCOMPARE(disconnect(NotificationManager::instance(), SIGNAL(notificationModified(uint)), &presenter, SLOT(updateNotification(uint))), true);
    QCOMPARE(disconnect(NotificationManager::instance(), SIGNAL(notificationRemoved(uint)), &presenter, SLOT(removeNotification(uint))), true);
    QCOMPARE(disconnect(&presenter, SIGNAL(notificationPresented(uint)), presenter.notificationFeedbackPlayer, SLOT(addNotification(uint))), true);
}
NotificationPreviewPresenter::NotificationPreviewPresenter(QObject *parent) :
    QObject(parent),
    window(0),
    currentNotification(0),
    notificationFeedbackPlayer(new NotificationFeedbackPlayer(this)),
    locks(new MeeGo::QmLocks(this)),
    displayState(new MeeGo::QmDisplayState(this))
{
    connect(NotificationManager::instance(), SIGNAL(notificationModified(uint)), this, SLOT(updateNotification(uint)));
    connect(NotificationManager::instance(), SIGNAL(notificationRemoved(uint)), this, SLOT(removeNotification(uint)));
    connect(this, SIGNAL(notificationPresented(uint)), notificationFeedbackPlayer, SLOT(addNotification(uint)));

    QTimer::singleShot(0, this, SLOT(createWindowIfNecessary()));
}
void NotificationPreviewPresenter::showNextNotification()
{
    if (!LipstickCompositor::instance() && !notificationQueue.isEmpty()) {
        QTimer::singleShot(0, this, SLOT(showNextNotification()));
        return;
    }

    if (notificationQueue.isEmpty()) {
        // No more notifications to show: hide the notification window if it's visible
        if (window != 0 && window->isVisible()) {
            window->hide();
        }

        setCurrentNotification(0);
    } else {
        LipstickNotification *notification = notificationQueue.takeFirst();

        if (locks->getState(MeeGo::QmLocks::TouchAndKeyboard) == MeeGo::QmLocks::Locked && displayState->get() == MeeGo::QmDisplayState::Off) {
            // Screen locked and off: don't show the notification but just remove it from the queue
            emit notificationPresented(notification->property("id").toUInt());

            setCurrentNotification(0);

            showNextNotification();
        } else {
            // Show the notification window and the first queued notification in it
            if (!window->isVisible()) {
                window->show();
            }

            emit notificationPresented(notification->property("id").toUInt());

            setCurrentNotification(notification);
        }
    }
}
void Ut_NotificationPreviewPresenter::testNotificationNotShownIfTouchScreenIsLockedAndDisplayIsOff()
{
    QFETCH(MeeGo::QmDisplayState::DisplayState, displayState);
    QFETCH(MeeGo::QmLocks::State, lockState);
    QFETCH(int, notifications);
    QFETCH(int, presentedCount);

    gQmDisplayStateStub->stubSetReturnValue("get", displayState);
    gQmLocksStub->stubSetReturnValue("getState", lockState);

    NotificationPreviewPresenter presenter;
    QSignalSpy changedSpy(&presenter, SIGNAL(notificationChanged()));
    QSignalSpy presentedSpy(&presenter, SIGNAL(notificationPresented(uint)));

    createNotification(1, 2);
    presenter.updateNotification(1);
    QCOMPARE(homeWindows.count(), notifications);
    QCOMPARE(changedSpy.count(), notifications);
    QCOMPARE(presentedSpy.count(), presentedCount);
}
void Ut_NotificationPreviewPresenter::testUpdateNotification()
{
    NotificationPreviewPresenter presenter;

    // Create two notifications
    createNotification(1);
    createNotification(2);
    presenter.updateNotification(1);
    presenter.updateNotification(2);

    // Update both notifications
    QSignalSpy changedSpy(&presenter, SIGNAL(notificationChanged()));
    QSignalSpy presentedSpy(&presenter, SIGNAL(notificationPresented(uint)));
    presenter.updateNotification(1);
    presenter.updateNotification(2);

    // Check that no signals were sent
    QCOMPARE(changedSpy.count(), 0);
    QCOMPARE(presentedSpy.count(), 0);
}
void Ut_NotificationPreviewPresenter::testNotificationNotShownIfHidden()
{
    NotificationPreviewPresenter presenter;
    QSignalSpy changedSpy(&presenter, SIGNAL(notificationChanged()));
    QSignalSpy presentedSpy(&presenter, SIGNAL(notificationPresented(uint)));

    // Create notification
    QVariantHash hints;
    hints.insert(NotificationManager::HINT_PREVIEW_SUMMARY, "previewSummary");
    hints.insert(NotificationManager::HINT_PREVIEW_BODY, "previewBody");
    hints.insert(NotificationManager::HINT_HIDDEN, true);
    LipstickNotification *notification = new LipstickNotification("ut_notificationpreviewpresenter", 1, "", "", "", QStringList(), hints, -1);
    notificationManagerNotification.insert(1, notification);
    presenter.updateNotification(1);

    QCOMPARE(changedSpy.count(), 0);
    QCOMPARE(homeWindowVisible.isEmpty(), true);

    // The notification should be considered presented
    QCOMPARE(presentedSpy.count(), 1);
    QCOMPARE(presentedSpy.last().at(0).toUInt(), (uint)1);
}
void Ut_NotificationPreviewPresenter::testShowingOnlyCriticalNotifications()
{
    NotificationPreviewPresenter presenter;
    QSignalSpy changedSpy(&presenter, SIGNAL(notificationChanged()));
    QSignalSpy presentedSpy(&presenter, SIGNAL(notificationPresented(uint)));

    // Create normal urgency notification
    LipstickNotification *notification = new LipstickNotification;
    QVariantHash hints;
    hints.insert(NotificationManager::HINT_PREVIEW_SUMMARY, "previewSummary");
    hints.insert(NotificationManager::HINT_PREVIEW_BODY, "previewBody");
    hints.insert(NotificationManager::HINT_URGENCY, 1);
    notification->setHints(hints);
    notificationManagerNotification.insert(1, notification);
    QCOMPARE(homeWindowVisible.isEmpty(), true);

    // When the screen or device is locked and the urgency is not high enough, so the notification shouldn't be shown
    gQmLocksStub->stubSetReturnValue("getState", MeeGo::QmLocks::Locked);
    presenter.updateNotification(1);
    QCOMPARE(changedSpy.count(), 0);
    QCOMPARE(homeWindowVisible.isEmpty(), true);

    // The notification should be considered presented
    QCOMPARE(presentedSpy.count(), 1);
    QCOMPARE(presentedSpy.last().at(0).toUInt(), (uint)1);

    // Urgency set to critical, so the notification should be shown
    hints.insert(NotificationManager::HINT_URGENCY, 2);
    notification->setHints(hints);
    presenter.updateNotification(1);
    QCOMPARE(changedSpy.count(), 1);
    QCOMPARE(homeWindowVisible.isEmpty(), false);
    QCOMPARE(homeWindowVisible[homeWindows.first()], true);
    QCOMPARE(presentedSpy.count(), 2);
    QCOMPARE(presentedSpy.last().at(0).toUInt(), (uint)1);
}