Merge commit 'refs/merge-requests/1' of gitorious.org:meego-cellular/ofono-qt into...
[profile/ivi/ofono-qt.git] / tests / test_ofonomessagemanager.cpp
1 /*
2  * This file is part of ofono-qt
3  *
4  * Copyright (C) 2010-2011 Nokia Corporation and/or its subsidiary(-ies).
5  *
6  * Contact: Alexander Kanavin <alexander.kanavin@nokia.com>
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public License
10  * version 2.1 as published by the Free Software Foundation.
11  *
12  * This library is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20  * 02110-1301 USA
21  *
22  */
23
24 #include <QtTest/QtTest>
25 #include <QtCore/QObject>
26 #include <QDBusObjectPath>
27
28 #include <ofonomessagemanager.h>
29 #include <ofonomessage.h>
30
31 #include <QtDebug>
32
33
34 class TestOfonoMessageManager : public QObject
35 {
36     Q_OBJECT
37
38 private slots:
39
40     void initTestCase()
41     {
42         m = new OfonoMessageManager(OfonoModem::ManualSelect, "/phonesim", this);
43         QCOMPARE(m->modem()->isValid(), true);  
44
45         if (!m->modem()->powered()) {
46             m->modem()->setPowered(true);
47             QTest::qWait(5000);
48         }
49         if (!m->modem()->online()) {
50             m->modem()->setOnline(true);
51             QTest::qWait(5000);
52         }
53         QCOMPARE(m->isValid(), true);    
54     }
55
56     void testOfonoMessageManager()
57     {
58         QSignalSpy sendMessage(m, SIGNAL(sendMessageComplete(bool, QDBusObjectPath)));
59         QSignalSpy messageAdded(m, SIGNAL(messageAdded(QString)));
60         QSignalSpy messageRemoved(m, SIGNAL(messageRemoved(QString)));
61         QSignalSpy immediateMessage(m, SIGNAL(immediateMessage(QString, QVariantMap)));
62         QSignalSpy incomingMessage(m, SIGNAL(incomingMessage(QString, QVariantMap)));
63
64
65         QStringList messages = m->getMessages();
66         QVERIFY(messages.count() == 0);
67
68         m->sendMessage("99999", "success");
69
70         QTest::qWait(1000);
71
72         QCOMPARE(sendMessage.count(), 1);
73         QVariantList params = sendMessage.takeFirst();
74         QCOMPARE(params.at(0).toBool(), true);
75         QDBusObjectPath objectPath = params.at(1).value<QDBusObjectPath>();
76         QVERIFY(objectPath.path().length() > 0);
77         qDebug() << objectPath.path();
78
79         QCOMPARE(messageAdded.count(), 1);
80         QCOMPARE(messageRemoved.count(), 1);
81
82         QString messageId = messageAdded.takeFirst().at(0).toString();
83         OfonoMessage* message = new OfonoMessage(messageId);
84         QSignalSpy state(message, SIGNAL(stateChanged(const QString)));
85         qDebug() << message->state();
86
87         m->sendMessage("abc", "fail");
88
89         QTest::qWait(1000);
90
91         QCOMPARE(sendMessage.count(), 1);
92         params = sendMessage.takeFirst();
93         QCOMPARE(params.at(0).toBool(), false);
94         objectPath = params.at(1).value<QDBusObjectPath>();
95         QVERIFY(objectPath.path().length() == 0);
96     }
97
98     void testOfonoMessageManagerSca()
99     {
100         QSignalSpy scaComplete(m, SIGNAL(serviceCenterAddressComplete(bool, QString)));
101         m->requestServiceCenterAddress();
102         for (int i=0; i<30; i++) {
103             if (scaComplete.count() > 0)
104                 break;
105             QTest::qWait(1000);
106         }
107         QCOMPARE(scaComplete.count(), 1);
108         QVariantList params = scaComplete.takeFirst();
109         QCOMPARE(params.at(0).toBool(), true);
110         QString sca = params.at(1).toString();
111         QVERIFY(sca.length() > 0);
112         qDebug() << sca;
113
114         QSignalSpy setScaFailed(m, SIGNAL(setServiceCenterAddressFailed()));
115         QSignalSpy scaChanged(m, SIGNAL(serviceCenterAddressChanged(QString)));
116
117         m->setServiceCenterAddress("+12345678");
118         for (int i=0; i<30; i++) {
119             if (setScaFailed.count() > 0 || scaChanged.count() > 0)
120                 break;
121             QTest::qWait(1000);
122         }
123         QCOMPARE(setScaFailed.count(), 0);
124         QCOMPARE(scaChanged.count(), 1);
125         QCOMPARE(scaChanged.takeFirst().at(0).toString(), QString("+12345678"));
126
127         m->setServiceCenterAddress("");
128         for (int i=0; i<30; i++) {
129             if (setScaFailed.count() > 0 || scaChanged.count() > 0)
130                 break;
131             QTest::qWait(1000);
132         }
133         QCOMPARE(setScaFailed.count(), 1);
134         setScaFailed.takeFirst();
135         QCOMPARE(scaChanged.count(), 0);
136         QCOMPARE(m->errorName(), QString("org.ofono.Error.InvalidFormat"));
137         QCOMPARE(m->errorMessage(), QString("Argument format is not recognized"));
138         
139         m->setServiceCenterAddress(sca);
140         for (int i=0; i<30; i++) {
141             if (setScaFailed.count() > 0 || scaChanged.count() > 0)
142                 break;
143             QTest::qWait(1000);
144         }
145         QCOMPARE(setScaFailed.count(), 0);
146         QCOMPARE(scaChanged.count(), 1);
147         QCOMPARE(scaChanged.takeFirst().at(0).toString(), sca);
148     }
149
150     void testOfonoMessageManagerUseDeliveryReports()
151     {
152         QSignalSpy useDeliveryReportsComplete(m, SIGNAL(useDeliveryReportsComplete(bool, bool)));
153         QSignalSpy setUseDeliveryReportsFailed(m, SIGNAL(setUseDeliveryReportsFailed()));
154         QSignalSpy useDeliveryReportsChanged(m, SIGNAL(useDeliveryReportsChanged(bool)));
155
156         m->requestUseDeliveryReports();
157         for (int i=0; i<30; i++) {
158             if (useDeliveryReportsComplete.count() > 0)
159                 break;
160             QTest::qWait(1000);
161         }
162         QCOMPARE(useDeliveryReportsComplete.count(), 1);
163         QVariantList params = useDeliveryReportsComplete.takeFirst();
164         QCOMPARE(params.at(0).toBool(), true);
165         QCOMPARE(params.at(1).toBool(), false);
166
167         m->setUseDeliveryReports(true);
168         for (int i=0; i<30; i++) {
169             if (setUseDeliveryReportsFailed.count() > 0 || useDeliveryReportsChanged.count() > 0)
170                 break;
171             QTest::qWait(1000);
172         }
173         QCOMPARE(setUseDeliveryReportsFailed.count(), 0);
174         QCOMPARE(useDeliveryReportsChanged.count(), 1);
175         QCOMPARE(useDeliveryReportsChanged.takeFirst().at(0).toBool(), true);
176
177          m->setUseDeliveryReports(false);
178          for (int i=0; i<30; i++) {
179              if (setUseDeliveryReportsFailed.count() > 0 || useDeliveryReportsChanged.count() > 0)
180                  break;
181              QTest::qWait(1000);
182          }
183          QCOMPARE(setUseDeliveryReportsFailed.count(), 0);
184          QCOMPARE(useDeliveryReportsChanged.count(), 1);
185          QCOMPARE(useDeliveryReportsChanged.takeFirst().at(0).toBool(), false);
186     }
187
188     void testOfonoMessageManagerAlphabet()
189     {
190         QSignalSpy alphabetComplete(m, SIGNAL(alphabetComplete(bool, QString)));
191         QSignalSpy setAlphabetFailed(m, SIGNAL(setAlphabetFailed()));
192         QSignalSpy alphabetChanged(m, SIGNAL(alphabetChanged(QString)));
193
194         m->requestAlphabet();
195         for (int i=0; i<30; i++) {
196             if (alphabetComplete.count() > 0)
197                 break;
198             QTest::qWait(1000);
199         }
200
201         QCOMPARE(alphabetComplete.count(), 1);
202         QVariantList params = alphabetComplete.takeFirst();
203         QCOMPARE(params.at(0).toBool(), true);
204         QString alphabet = params.at(1).toString();
205         QVERIFY(alphabet.length() > 0);
206         QCOMPARE(alphabet, QString("default"));
207         qDebug() << alphabet;
208
209         m->setAlphabet("spanish");
210         for (int i=0; i<30; i++) {
211             if (setAlphabetFailed.count() > 0 || alphabetChanged.count() > 0)
212                 break;
213             QTest::qWait(1000);
214         }
215
216         QCOMPARE(setAlphabetFailed.count(), 0);
217         QCOMPARE(alphabetChanged.count(), 1);
218         QCOMPARE(alphabetChanged.takeFirst().at(0).toString(), QString("spanish"));
219
220         m->setAlphabet(alphabet);
221         for (int i=0; i<30; i++) {
222             if (setAlphabetFailed.count() > 0 || alphabetChanged.count() > 0)
223                 break;
224             QTest::qWait(1000);
225         }
226
227         QCOMPARE(setAlphabetFailed.count(), 0);
228         QCOMPARE(alphabetChanged.count(), 1);
229         QCOMPARE(alphabetChanged.takeFirst().at(0).toString(), alphabet);
230     }
231
232     void testOfonoMessageManagerBearer()
233     {
234         QSignalSpy bearerComplete(m, SIGNAL(bearerComplete(bool, QString)));
235         QSignalSpy setBearerFailed(m, SIGNAL(setBearerFailed()));
236         QSignalSpy bearerChanged(m, SIGNAL(bearerChanged(QString)));
237
238         m->requestBearer();
239         for (int i=0; i<30; i++) {
240             if (bearerComplete.count() > 0)
241                 break;
242             QTest::qWait(1000);
243         }
244
245         QCOMPARE(bearerComplete.count(), 1);
246         QVariantList params = bearerComplete.takeFirst();
247         QCOMPARE(params.at(0).toBool(), true);
248         QString bearer = params.at(1).toString();
249         QVERIFY(bearer.length() > 0);
250         QCOMPARE(bearer, QString("cs-preferred"));
251         qDebug() << bearer;
252
253         m->setBearer("ps-preferred");
254         for (int i=0; i<30; i++) {
255             if (setBearerFailed.count() > 0 || bearerChanged.count() > 0)
256                 break;
257             QTest::qWait(1000);
258         }
259
260         QCOMPARE(setBearerFailed.count(), 0);
261         QCOMPARE(bearerChanged.count(), 1);
262         QCOMPARE(bearerChanged.takeFirst().at(0).toString(), QString("ps-preferred"));
263
264         m->setBearer(bearer); // change value
265         for (int i=0; i<30; i++) {
266             if (setBearerFailed.count() > 0 || bearerChanged.count() > 0)
267                 break;
268             QTest::qWait(1000);
269         }
270
271         QCOMPARE(setBearerFailed.count(), 0);
272         QCOMPARE(bearerChanged.count(), 1);
273         QCOMPARE(bearerChanged.takeFirst().at(0).toString(), bearer);        
274     }
275
276     void cleanupTestCase()
277     {
278
279     }
280
281
282 private:
283     OfonoMessageManager *m;
284 };
285
286 QTEST_MAIN(TestOfonoMessageManager)
287 #include "test_ofonomessagemanager.moc"