Rewrite ModemManager test
[profile/ivi/ofono-qt.git] / tests / test_ofonointerface.cpp
1 /*
2  * This file is part of ofono-qt
3  *
4  * Copyright (C) 2010 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
27 #include <ofonointerface.h>
28
29 #include <QtDebug>
30
31 class TestOfonoInterface : public QObject
32 {
33     Q_OBJECT
34
35 private slots:
36
37     void initTestCase()
38     {
39         oi = new OfonoInterface("/phonesim", "org.ofono.Modem", OfonoInterface::GetAllOnStartup, this);
40         oi_async = new OfonoInterface("/phonesim", "org.ofono.Modem", OfonoInterface::GetAllOnFirstRequest, this);
41
42     }
43
44     void testGetProperties()
45     {
46         QCOMPARE(oi->properties()["Manufacturer"].toString(), QString("MeeGo"));
47
48         QCOMPARE(oi_async->properties().count(), 0);
49
50         QSignalSpy spy_request(oi_async, SIGNAL(requestPropertyComplete(bool, const QString &, const QVariant &)));
51         oi_async->requestProperty("Manufacturer");
52         oi_async->requestProperty("Model");
53
54         QCOMPARE(spy_request.count(), 1);
55         QVariantList list = spy_request.takeFirst();
56         QCOMPARE(list[0].toBool(), false);
57         QCOMPARE(list[1].toString(), QString("Model"));
58         QCOMPARE(oi_async->errorMessage(), QString("Already in progress"));
59
60         while (spy_request.count() != 1) {
61             QTest::qWait(100);
62         }
63         list = spy_request.takeFirst();
64         QCOMPARE(list[0].toBool(), true);
65         QCOMPARE(list[1].toString(), QString("Manufacturer"));
66         QCOMPARE(list[2].value<QVariant>().toString(), QString("MeeGo"));
67
68         oi_async->requestProperty("Model");
69         QCOMPARE(spy_request.count(), 1);
70         list = spy_request.takeFirst();
71         QCOMPARE(list[0].toBool(), true);
72         QCOMPARE(list[1].toString(), QString("Model"));
73         QCOMPARE(list[2].value<QVariant>().toString(), QString("Synthetic Device"));
74         
75         oi_async->requestProperty("UnknownProperty");
76         while (spy_request.count() != 1) {
77             QTest::qWait(100);
78         }
79         QCOMPARE(spy_request.count(), 1);
80         list = spy_request.takeFirst();
81         QCOMPARE(list[0].toBool(), false);
82         QCOMPARE(list[1].toString(), QString("UnknownProperty"));
83         QCOMPARE(oi_async->errorMessage(), QString("Property not available"));
84     }
85     
86     void testSetProperty()
87     {
88         QSignalSpy spy_changed(oi, SIGNAL(propertyChanged(const QString &, const QVariant &)));
89         QSignalSpy spy_failed(oi, SIGNAL(setPropertyFailed(const QString &)));
90         QVariantList list;
91         bool online;
92         bool online_found;
93     
94         oi->setProperty("Online", qVariantFromValue(false));
95         while (spy_changed.count() != 3 && spy_failed.count() == 0) {
96             QTest::qWait(100);
97         }
98         QCOMPARE(spy_changed.count(), 3);
99         QCOMPARE(spy_failed.count(), 0);
100         online = false;
101         online_found = false;
102         while (spy_changed.count() > 0) {
103             list = spy_changed.takeFirst();
104             if (list[0] == "Online") {
105                 online = list[1].value<QVariant>().toBool();
106                 online_found = true;
107             }
108         }
109         QCOMPARE(online_found, true);
110         QCOMPARE(online, false);
111         
112         oi->setProperty("Online", qVariantFromValue(true));
113         while (spy_changed.count() < 3 && spy_failed.count() == 0) {
114             QTest::qWait(100);
115         }
116         QVERIFY(spy_changed.count() > 3);
117         QCOMPARE(spy_failed.count(), 0);
118         online = false;
119         online_found = false;
120         while (spy_changed.count() > 0) {
121             list = spy_changed.takeFirst();
122             if (list[0] == "Online") {
123                 online = list[1].value<QVariant>().toBool();
124                 online_found = true;
125             }
126         }
127         QCOMPARE(online_found, true);
128         QCOMPARE(online, true);
129         QTest::qWait(5000);
130     }
131
132     void testSetPropertyFailed()
133     {
134         QSignalSpy spy_changed(oi, SIGNAL(propertyChanged(const QString &, const QVariant &)));
135         QSignalSpy spy_failed(oi, SIGNAL(setPropertyFailed(const QString &)));
136         QVariantList list;
137     
138         oi->setProperty("Manufacturer", qVariantFromValue(QString("Nokia")));
139         while (spy_changed.count() == 0 && spy_failed.count() == 0) {
140             QTest::qWait(100);
141         }
142         QCOMPARE(spy_changed.count(), 0);
143         QCOMPARE(spy_failed.count(), 1);
144         list = spy_failed.takeFirst();
145         QCOMPARE(list[0].toString(), QString("Manufacturer"));
146         QCOMPARE(oi->errorName(), QString("org.ofono.Error.InvalidArguments"));
147         QCOMPARE(oi->errorMessage(), QString("Invalid arguments in method call"));
148     }
149
150
151     void cleanupTestCase()
152     {
153
154     }
155
156
157 private:
158     OfonoInterface *oi;
159     OfonoInterface *oi_async;
160 };
161
162 QTEST_MAIN(TestOfonoInterface)
163 #include "test_ofonointerface.moc"