~phablet-team/telephony-service/trunk

« back to all changes in this revision

Viewing changes to handler/tests/HandlerTest.cpp

Add the infrastructure to write tests for the handler, and write tests for the most important bits of it.

Approved by Tiago Salem Herrmann, PS Jenkins bot.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2013 Canonical, Ltd.
 
3
 *
 
4
 * This file is part of telephony-service.
 
5
 *
 
6
 * telephony-service is free software; you can redistribute it and/or modify
 
7
 * it under the terms of the GNU General Public License as published by
 
8
 * the Free Software Foundation; version 3.
 
9
 *
 
10
 * telephony-service is distributed in the hope that it will be useful,
 
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
 * GNU General Public License for more details.
 
14
 *
 
15
 * You should have received a copy of the GNU General Public License
 
16
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
17
 */
 
18
 
 
19
#include <QtCore/QObject>
 
20
#include <QtTest/QtTest>
 
21
#include "handlercontroller.h"
 
22
#include "mockcontroller.h"
 
23
#include "approver.h"
 
24
#include "telepathyhelper.h"
 
25
 
 
26
class HandlerTest : public QObject
 
27
{
 
28
    Q_OBJECT
 
29
 
 
30
private Q_SLOTS:
 
31
    void initTestCase();
 
32
    void testMakingCalls();
 
33
    void testHangUpCall();
 
34
    void testSendMessage();
 
35
 
 
36
private:
 
37
    Approver *mApprover;
 
38
};
 
39
 
 
40
void HandlerTest::initTestCase()
 
41
{
 
42
    QSignalSpy spy(TelepathyHelper::instance(), SIGNAL(accountReady()));
 
43
    QTRY_COMPARE(spy.count(), 1);
 
44
    QTRY_VERIFY(TelepathyHelper::instance()->connected());
 
45
 
 
46
    // register the approver
 
47
    mApprover = new Approver(this);
 
48
    TelepathyHelper::instance()->registerClient(mApprover, "TelephonyTestApprover");
 
49
    // Tp-qt does not set registered status to approvers
 
50
    QTRY_VERIFY(QDBusConnection::sessionBus().interface()->isServiceRegistered(TELEPHONY_SERVICE_APPROVER));
 
51
 
 
52
    // we need to wait in order to give telepathy time to notify about the approver
 
53
    QTest::qWait(3000);
 
54
}
 
55
 
 
56
void HandlerTest::testMakingCalls()
 
57
{
 
58
    QString callerId("1234567");
 
59
    QSignalSpy callReceivedSpy(MockController::instance(), SIGNAL(callReceived(QString)));
 
60
    HandlerController::instance()->startCall(callerId);
 
61
    QTRY_COMPARE(callReceivedSpy.count(), 1);
 
62
    QCOMPARE(callReceivedSpy.first().first().toString(), callerId);
 
63
 
 
64
    MockController::instance()->hangupCall(callerId);
 
65
}
 
66
 
 
67
void HandlerTest::testHangUpCall()
 
68
{
 
69
    QString callerId("7654321");
 
70
 
 
71
    QVariantMap properties;
 
72
    properties["Caller"] = callerId;
 
73
    properties["State"] = "incoming";
 
74
 
 
75
    QSignalSpy approverCallSpy(mApprover, SIGNAL(newCall()));
 
76
    MockController::instance()->placeCall(properties);
 
77
 
 
78
    // wait for the channel to hit the approver
 
79
    QTRY_COMPARE(approverCallSpy.count(), 1);
 
80
    mApprover->acceptCall();
 
81
 
 
82
    // wait until the call state is "accepted"
 
83
    QSignalSpy callStateSpy(MockController::instance(), SIGNAL(callStateChanged(QString,QString,QString)));
 
84
    QString state;
 
85
    QString objectPath;
 
86
    int tries = 0;
 
87
    while (state != "active" && tries < 5) {
 
88
        QTRY_COMPARE(callStateSpy.count(), 1);
 
89
        objectPath = callStateSpy.first()[1].toString();
 
90
        state = callStateSpy.first()[2].toString();
 
91
        callStateSpy.clear();
 
92
        tries++;
 
93
    }
 
94
 
 
95
    QCOMPARE(state, QString("active"));
 
96
    QVERIFY(!objectPath.isEmpty());
 
97
 
 
98
    // and finally request the hangup
 
99
    QSignalSpy callEndedSpy(MockController::instance(), SIGNAL(callEnded(QString)));
 
100
    HandlerController::instance()->hangUpCall(objectPath);
 
101
    QTRY_COMPARE(callEndedSpy.count(), 1);
 
102
}
 
103
 
 
104
void HandlerTest::testSendMessage()
 
105
{
 
106
    QString recipient("22222222");
 
107
    QString message("Hello, world!");
 
108
    QSignalSpy messageSentSpy(MockController::instance(), SIGNAL(messageSent(QString,QVariantMap)));
 
109
    HandlerController::instance()->sendMessage(recipient, message);
 
110
    QTRY_COMPARE(messageSentSpy.count(), 1);
 
111
    QString sentMessage = messageSentSpy.first().first().toString();
 
112
    QVariantMap messageProperties = messageSentSpy.first().last().value<QVariantMap>();
 
113
    QCOMPARE(sentMessage, message);
 
114
    QCOMPARE(messageProperties["Recipients"].value<QStringList>().count(), 1);
 
115
    QCOMPARE(messageProperties["Recipients"].value<QStringList>().first(), recipient);
 
116
}
 
117
 
 
118
QTEST_MAIN(HandlerTest)
 
119
#include "HandlerTest.moc"