void Ut_NotificationManager::testAddMissedCallNotification()
{
    CommHistory::Event event = createMissedCallEvent(CONTACT_1_REMOTE_ID, internalId(contact1));
    // One notification from contact 1
    NotificationManager* nm = NotificationManager::instance();
    nm->addNotification(createPersonalNotification(event));
    NotificationGroup group = nm->notificationGroup(event.type());

    QVERIFY(nm->countNotifications(group) == 1);
    QVERIFY(nm->countContacts(group) == 1);
    QVERIFY(nm->removeNotificationGroup(event.type()));
    QVERIFY(nm->countNotifications(group) == 0);
    QVERIFY(nm->countContacts(group) == 0);

    // Multiple notifications from contact 1
    CommHistory::Event event1 = createMissedCallEvent(CONTACT_1_REMOTE_ID, internalId(contact1));
    CommHistory::Event event2 = createMissedCallEvent(CONTACT_1_REMOTE_ID, internalId(contact1));
    nm->addNotification(createPersonalNotification(event1));
    nm->addNotification(createPersonalNotification(event2));
    QVERIFY(nm->countNotifications(group) == 2);
    QVERIFY(nm->countContacts(group) == 1);
    QVERIFY(nm->removeNotificationGroup(event1.type()));
    QVERIFY(nm->countNotifications(group) == 0);
    QVERIFY(nm->countContacts(group) == 0);
}
/*!
 * This function will be called before the first testfunction is executed.
 */
void Ut_MessageReviver::initTestCase()
{
    groupModel.enableContactChanges(false);
    CommHistory::Group group;
    group.setLocalUid(ACCOUNT_PATH);
    group.setRemoteUids(QStringList() << NUMBER);
    groupModel.addGroup(group);

    CommHistory::EventModel model;

    QSignalSpy commit(&model,
                      SIGNAL(eventsCommitted(const QList<CommHistory::Event>&, bool)));
    CommHistory::Event event;
    event.setType(CommHistory::Event::SMSEvent);
    event.setDirection(CommHistory::Event::Inbound);
    event.setStartTime(QDateTime::currentDateTime());
    event.setEndTime(QDateTime::currentDateTime());
    event.setLocalUid(ACCOUNT_PATH);
    event.setGroupId(group.id());
    event.setRemoteUid(NUMBER);
    event.setFreeText("blah");
    event.setMessageToken("mrtc1");
    model.addEvent(event, false);
    waitSignal(commit, 5000);
}
void Ut_NotificationManager::testMissedCallNotification()
{
    CommHistory::Event event = createMissedCallEvent(CONTACT_1_REMOTE_ID, internalId(contact1));
    // One notification from contact 1
    NotificationManager* nm = NotificationManager::instance();
    nm->m_NotificationTimer.stop();
    nm->removeNotificationGroup(event.type());
    removeGroup(event.type());

    QVERIFY(getGroup(event.type(), 10) == 0);

    nm->showNotification(event, CONTACT_1_REMOTE_ID);

    waitForCompletion(nm);

    MNotificationGroup *mgtGroup = getGroup(event.type(), 5000);
    QVERIFY(mgtGroup);
    QVERIFY(mgtGroup->isPublished());
    QVERIFY(mgtGroup->summary().contains(CONTACT_1_REMOTE_ID));
    QCOMPARE(mgtGroup->body(), txt_qtn_call_missed(1));

    NotificationGroup group = nm->notificationGroup(event.type());

    QVERIFY(nm->countContacts(group) == 1);
    QVERIFY(nm->countNotifications(group) == 1);

    waitForCompletion(nm);

    // Multiple notifications from contact 1
    CommHistory::Event event1 = createMissedCallEvent(CONTACT_1_REMOTE_ID, internalId(contact1));
    nm->showNotification(event1, CONTACT_1_REMOTE_ID);

    waitForCompletion(nm);

    mgtGroup = getGroup(event.type(), 5000);
    QVERIFY(mgtGroup);
    QVERIFY(mgtGroup->isPublished());
    QVERIFY(mgtGroup->summary().contains(CONTACT_1_REMOTE_ID));
    QCOMPARE(mgtGroup->body(), txt_qtn_call_missed(2));

    QVERIFY(nm->countContacts(group) == 1);
    QVERIFY(nm->countNotifications(group) == 2);

    waitForCompletion(nm);

    CommHistory::Event event2 = createMissedCallEvent(CONTACT_2_REMOTE_ID, internalId(contact2));
    nm->showNotification(event2, CONTACT_2_REMOTE_ID);

    waitForCompletion(nm); //wait for contact resolving

    mgtGroup = getGroup(event.type(), 5000);
    QVERIFY(mgtGroup);
    QVERIFY(mgtGroup->isPublished());
    QVERIFY(mgtGroup->summary().contains(CONTACT_1_REMOTE_ID));
    QVERIFY(mgtGroup->summary().contains(CONTACT_2_REMOTE_ID));
    QCOMPARE(mgtGroup->body(), txt_qtn_call_missed(3));

    QVERIFY(nm->countContacts(group) == 2);
    QVERIFY(nm->countNotifications(group) == 3);
}
CommHistory::Event Ut_NotificationManager::createMissedCallEvent(const QString& remoteUid, int contactId)
{
    CommHistory::Event event;
    event.setType(CommHistory::Event::CallEvent);
    event.setDirection(CommHistory::Event::Inbound);
    event.setIsMissedCall (true);
    event.setStartTime(QDateTime::currentDateTime());
    event.setEndTime(QDateTime::currentDateTime());
    event.setLocalUid(DUT_ACCOUNT_PATH);
    event.setRemoteUid(remoteUid);
    event.setContactId(contactId);
    event.setId(eventId);
    eventId++;
    return event;
}
void Ut_NotificationManager::testRemoveNotificationGrouop()
{
    NotificationManager* nm = NotificationManager::instance();

    // One IM notification from contact 1
    CommHistory::Event event = createImEvent(CONTACT_1_REMOTE_ID, internalId(contact1));
    nm->addNotification(createPersonalNotification(event));

    // 1st time to remove NotificationGroup, return true
    QVERIFY(nm->removeNotificationGroup(event.type()));
    // 2nd time to remove the same NotificationGroup, return false
    QVERIFY(!nm->removeNotificationGroup(event.type()));

    //Mutile IM notifications from contact 1
    CommHistory::Event event1 = createMissedCallEvent(CONTACT_1_REMOTE_ID, internalId(contact1));
    CommHistory::Event event2 = createMissedCallEvent(CONTACT_1_REMOTE_ID, internalId(contact1));
    nm->addNotification(createPersonalNotification(event1));
    nm->addNotification(createPersonalNotification(event2));

    // 1st time to remove NotificationGroup, return true
    QVERIFY(nm->removeNotificationGroup(event1.type()));
    // 2nd time to remove the same NotificationGroup, return false
    QVERIFY(!nm->removeNotificationGroup(event1.type()));
}
PersonalNotification Ut_NotificationManager::createPersonalNotification(const CommHistory::Event &event)
{
    return PersonalNotification(event.remoteUid(), event.localUid(), event.type(),
                                event.remoteUid(), CommHistory::Group::ChatTypeP2P,
                                event.contactId());
}
CommHistory::Event Ut_NotificationManager::createImEvent(const QString& remoteUid, int contactId)
{
    eventId++;
    CommHistory::Event event;
    event.setType(CommHistory::Event::IMEvent);
    event.setDirection(CommHistory::Event::Inbound);
    event.setStartTime(QDateTime::currentDateTime());
    event.setEndTime(QDateTime::currentDateTime());
    event.setLocalUid(DUT_ACCOUNT_PATH);
    event.setRemoteUid(remoteUid);
    event.setFreeText(MESSAGE_TEXT);
    event.setMessageToken(MESSAGE_TEXT + QString::number(eventId));
    event.setContactId(contactId);
    event.setGroupId(1);
    event.setId(eventId);
    return event;
}