2 * This file is part of QMAFW
4 * Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). All rights
7 * Contact: Visa Smolander <visa.smolander@nokia.com>
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.
17 #include "ut_MafwGstRendererVolume.h"
21 #include <dbus/dbus.h>
23 #include "MafwGstRendererVolume.h"
24 #include "MafwStubHelper.h"
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;
32 void Ut_MafwGstRendererVolume::initTestCase()
34 m_stubHelper = new MafwStubHelper;
35 setStubHelper(m_stubHelper);
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
45 m_stubHelper->expect("dbus_connection_open", true);
46 m_stubHelper->expect("dbus_error_is_set", false);
47 m_stubHelper->expect("addConnection", false);
49 QVERIFY(m_stubHelper->allCallsConsumed());
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());
60 void Ut_MafwGstRendererVolume::cleanupTestCase()
62 m_stubHelper->expect("removeConnection", false);
64 QVERIFY(m_stubHelper->allCallsConsumed());
68 void Ut_MafwGstRendererVolume::testVolumeReply()
71 //Error reply for "GetEntryByName"
72 m_stubHelper->expect("dbus_set_error_from_message", true);
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();
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();
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();
95 QVERIFY(m_stubHelper->allCallsConsumed());
96 QCOMPARE(m_volume->getVolume(), (uint)0);
99 giveRestoreEntryReply();
101 giveVolumeReply(map);
102 QCOMPARE(m_volume->getVolume(), (uint)100);
104 giveRestoreEntryReply();
106 giveVolumeReply(map);
107 QCOMPARE(m_volume->getVolume(), (uint)100);
109 giveRestoreEntryReply();
111 giveVolumeReply(map);
112 QCOMPARE(m_volume->getVolume(), (uint)100);
115 giveRestoreEntryReply();
116 giveVolumeReply(map);
117 QCOMPARE(m_volume->getVolume(), (uint)100);
120 giveRestoreEntryReply();
121 giveVolumeReply(map);
122 QCOMPARE(m_volume->getVolume(), (uint)0);
123 //Many channels, containing mono channel
128 giveRestoreEntryReply();
129 giveVolumeReply(map);
130 QCOMPARE(m_volume->getVolume(), (uint)50);
131 //Many channels, without mono channel
136 giveRestoreEntryReply();
137 giveVolumeReply(map);
138 QCOMPARE(m_volume->getVolume(), (uint)(100/3));
141 void Ut_MafwGstRendererVolume::testSignal()
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);
150 QCOMPARE(spy.count(), 1);
151 QList<QVariant> arguments = spy.takeFirst();
153 if (volume > 100) //Volume must not be set out of range
157 QCOMPARE(arguments.at(0).toUInt(), volume);
158 QCOMPARE(m_volume->getVolume(), volume);
161 void Ut_MafwGstRendererVolume::testSignal_data()
163 QTest::addColumn<uint>("volume");
164 QTest::addColumn<uint>("channel");
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;
175 //one MAFW volume step is 0x10000/100 = 655,36 native volume steps
176 void Ut_MafwGstRendererVolume::testSetVolume()
178 QFETCH(uint, volume);
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);
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);
202 if (volume > 100) //Volume must not be set out of range
207 QCOMPARE(value, (quint32)(volume*0x10000/100));
211 void Ut_MafwGstRendererVolume::testSetVolume_data()
213 QTest::addColumn<uint>("volume");
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;
222 QTEST_MAIN(Ut_MafwGstRendererVolume)