Added qmafw-gst-subtitles-renderer-0.0.55 for Meego Harmattan 1.2
[mafwsubrenderer] / qmafw-gst-subtitles-renderer / unittests / ut_MafwGstRendererVolume / ut_MafwGstRendererVolume.cpp
1 /*
2  * This file is part of QMAFW
3  *
4  * Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). All rights
5  * reserved.
6  *
7  * Contact: Visa Smolander <visa.smolander@nokia.com>
8  *
9  * This software, including documentation, is protected by copyright controlled
10  * by Nokia Corporation. All rights are reserved. Copying, including
11  * reproducing, storing, adapting or translating, any or all of this material
12  * requires the prior written consent of Nokia Corporation. This material also
13  * contains confidential information which may not be disclosed to others
14  * without the prior written consent of Nokia.
15  *
16  */
17 #include "ut_MafwGstRendererVolume.h"
18
19 #include <QDebug>
20 #include <QVariant>
21 #include <dbus/dbus.h>
22
23 #include "MafwGstRendererVolume.h"
24 #include "MafwStubHelper.h"
25
26 extern void setStubHelper(MafwStubHelper* stubHlp);
27 extern void signalVolumeUpdated(uint channel, uint value);
28 extern void giveRestoreEntryReply();
29 extern void giveVolumeReply(QMap<uint, uint> volumes);
30 extern DBusMessage* m_setVolumeMethod;
31
32 void Ut_MafwGstRendererVolume::initTestCase()
33 {
34     m_stubHelper = new MafwStubHelper;
35     setStubHelper(m_stubHelper);
36
37     //Tests the case where first pulseaudio connect fails and
38     //DBUSConnectionEventLoop::addConnection fails.
39     m_stubHelper->expect("dbus_connection_open", false);
40     m_stubHelper->expect("dbus_error_is_set", true);
41     m_volume = new MafwGstRendererVolume();
42     QVERIFY(m_stubHelper->allCallsConsumed());
43     //Wait that connecting to pulseaudio is retried
44
45     m_stubHelper->expect("dbus_connection_open", true);
46     m_stubHelper->expect("dbus_error_is_set", false);
47     m_stubHelper->expect("addConnection", false);
48     QTest::qWait(3000);
49     QVERIFY(m_stubHelper->allCallsConsumed());
50
51     delete m_volume;
52     //Tests the successfull case
53     m_stubHelper->expect("dbus_connection_open", true);
54     m_stubHelper->expect("dbus_error_is_set", false);
55     m_stubHelper->expect("addConnection", true);
56     m_volume = new MafwGstRendererVolume();
57     QVERIFY(m_stubHelper->allCallsConsumed());
58 }
59
60 void Ut_MafwGstRendererVolume::cleanupTestCase()
61 {
62     m_stubHelper->expect("removeConnection", false);
63     delete m_volume;
64     QVERIFY(m_stubHelper->allCallsConsumed());
65     delete m_stubHelper;
66 }
67
68 void Ut_MafwGstRendererVolume::testVolumeReply()
69 {
70     QMap<uint, uint> map;
71 //Error reply for "GetEntryByName"
72     m_stubHelper->expect("dbus_set_error_from_message", true);
73     
74     //Pulseaudio reconnect is done
75     m_stubHelper->expect("removeConnection", false);
76     m_stubHelper->expect("dbus_connection_open", true);
77     m_stubHelper->expect("dbus_error_is_set", false);
78     m_stubHelper->expect("addConnection", true);
79     giveRestoreEntryReply();    
80     QTest::qWait(500);
81     QVERIFY(m_stubHelper->allCallsConsumed());    
82     QCOMPARE(m_volume->getVolume(), (uint)0);
83 //Error reply for "Get Volume"
84     m_stubHelper->expect("dbus_set_error_from_message", false);
85     m_stubHelper->expect("dbus_set_error_from_message", true);
86     giveRestoreEntryReply();
87     giveVolumeReply(map);
88     QVERIFY(m_stubHelper->allCallsConsumed());
89     QCOMPARE(m_volume->getVolume(), (uint)0);
90 //Invalid reply for "Get Volume"
91     m_stubHelper->expect("dbus_set_error_from_message", false);
92     m_stubHelper->expect("dbus_set_error_from_message", false);
93     giveRestoreEntryReply();
94     giveVolumeReply(map);
95     QVERIFY(m_stubHelper->allCallsConsumed());
96     QCOMPARE(m_volume->getVolume(), (uint)0);
97
98 //Maximum value
99     giveRestoreEntryReply();
100     map[0] = 0x10000;
101     giveVolumeReply(map);
102     QCOMPARE(m_volume->getVolume(), (uint)100);
103 //Maximum value-1
104     giveRestoreEntryReply();
105     map[0] = 0x10000;
106     giveVolumeReply(map);
107     QCOMPARE(m_volume->getVolume(), (uint)100);
108 //Value above range
109     giveRestoreEntryReply();
110     map[0] = 0x10001;
111     giveVolumeReply(map);
112     QCOMPARE(m_volume->getVolume(), (uint)100);
113 //Huge value
114     map[0] = 0xFF000;
115     giveRestoreEntryReply();
116     giveVolumeReply(map);
117     QCOMPARE(m_volume->getVolume(), (uint)100);
118 //Zero
119     map[0] = 0;
120     giveRestoreEntryReply();
121     giveVolumeReply(map);
122     QCOMPARE(m_volume->getVolume(), (uint)0);
123 //Many channels, containing mono channel
124     map[0] = 0x10000/2;
125     map[1] = 20;
126     map[2] = 30;
127     map[3] = 40;
128     giveRestoreEntryReply();
129     giveVolumeReply(map);
130     QCOMPARE(m_volume->getVolume(), (uint)50);
131 //Many channels, without mono channel
132     map.remove(0);
133     map[1] = 0x10000/3;
134     map[2] = 0x10000/4;
135     map[3] = 0x10000/5;
136     giveRestoreEntryReply();
137     giveVolumeReply(map);
138     QCOMPARE(m_volume->getVolume(), (uint)(100/3));
139 }
140
141 void Ut_MafwGstRendererVolume::testSignal()
142 {    
143     QSignalSpy spy(m_volume, SIGNAL(volumeChanged(uint)));
144     QFETCH(uint, volume);
145     QFETCH(uint, channel);
146     // Volume updated signal gives volume level as pulse audio's native value
147     // Which has range 0 - 0x10000
148     signalVolumeUpdated(channel, 0x10000*volume/100);
149
150     QCOMPARE(spy.count(), 1);
151     QList<QVariant> arguments = spy.takeFirst();
152
153     if (volume > 100) //Volume must not be set out of range
154     {
155         volume = 100;
156     }
157     QCOMPARE(arguments.at(0).toUInt(), volume);
158     QCOMPARE(m_volume->getVolume(), volume);
159 }
160
161 void Ut_MafwGstRendererVolume::testSignal_data()
162 {
163     QTest::addColumn<uint>("volume");
164     QTest::addColumn<uint>("channel");
165
166     QTest::newRow("Maximum value") << (uint)100 << (uint)0;
167     QTest::newRow("Maximum value -1") << (uint)99 << (uint)0;
168     QTest::newRow("Value above range") << (uint)101 << (uint)0;
169     QTest::newRow("Huge value") << (uint)-1 << (uint)0;
170     QTest::newRow("Zero") << (uint)0 << (uint)0;
171     QTest::newRow("One") << (uint)1 << (uint)0;
172     QTest::newRow("Another channel") << (uint)2 << (uint)1;
173 }
174
175 //one MAFW volume step is 0x10000/100 = 655,36 native volume steps
176 void Ut_MafwGstRendererVolume::testSetVolume()
177 {    
178     QFETCH(uint, volume);
179     quint32 value = 1;
180
181     m_volume->setVolume(volume);
182     QVERIFY(m_setVolumeMethod != 0);
183     DBusMessageIter iter, array_iterator, struct_iterator, value_iterator;
184     dbus_message_iter_init (m_setVolumeMethod, &iter);
185     QCOMPARE(dbus_message_iter_get_arg_type(&iter), DBUS_TYPE_STRING);//interface
186     dbus_message_iter_next(&iter);
187     QCOMPARE(dbus_message_iter_get_arg_type(&iter), DBUS_TYPE_STRING);//property
188     dbus_message_iter_next(&iter);
189     dbus_message_iter_recurse (&iter, &array_iterator);
190     QCOMPARE(dbus_message_iter_get_arg_type(&array_iterator), DBUS_TYPE_ARRAY);
191     dbus_message_iter_recurse (&array_iterator, &struct_iterator);
192     QCOMPARE(dbus_message_iter_get_arg_type(&struct_iterator), DBUS_TYPE_STRUCT);
193
194     dbus_message_iter_recurse (&struct_iterator, &value_iterator);
195     QCOMPARE(dbus_message_iter_get_arg_type (&value_iterator), DBUS_TYPE_UINT32);
196     dbus_message_iter_get_basic (&value_iterator, &value);
197     QCOMPARE(value, (quint32)0); //mono channel
198     dbus_message_iter_next (&value_iterator);
199     QCOMPARE(dbus_message_iter_get_arg_type (&value_iterator), DBUS_TYPE_UINT32);
200     dbus_message_iter_get_basic (&value_iterator, &value);
201
202     if (volume > 100) //Volume must not be set out of range
203     {
204         volume = 100;
205     }
206
207     QCOMPARE(value, (quint32)(volume*0x10000/100));
208 }
209
210
211 void Ut_MafwGstRendererVolume::testSetVolume_data()
212 {        
213     QTest::addColumn<uint>("volume");
214
215     QTest::newRow("Set volume to 0") << (uint)0;
216     QTest::newRow("Set volume to 1") << (uint)1;
217     QTest::newRow("Set volume to 99") << (uint)99;
218     QTest::newRow("Set volume to 100") << (uint)100;
219     QTest::newRow("Set volume to huge value") << (uint)-1;
220 }
221
222 QTEST_MAIN(Ut_MafwGstRendererVolume)
223