Added qmafw-gst-subtitles-renderer-0.0.55 for Meego Harmattan 1.2
[mafwsubrenderer] / qmafw-gst-subtitles-renderer / unittests / ut_MafwGstRendererPlugin / Ut_MafwGstRendererPlugin.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
18 #include <MafwInternalRegistry.h>
19 #include <MafwRenderer.h>
20 #include <QVariant>
21 #include <QtTest/QtTest>
22 #include <glib-object.h>
23 #include <QSettings>
24
25 #include "Ut_MafwGstRendererPlugin.h"
26 #include "MafwGstRendererPlugin.h"
27 #include "MafwGstRenderer.h"
28 #include "MafwGstRendererVolume.h"
29 #include "MafwGstRendererDolby.h"
30 #include "../common/MafwStubHelper.h"
31
32 Q_DECLARE_METATYPE(MafwRenderer*)
33
34 extern void setStubHelper(MafwStubHelper* stubHlp);
35 QMap<QString,QString> renderers;
36 int renderer_index = 0;
37
38 void Ut_MafwGstRendererPlugin::initTestCase()
39 {
40     qRegisterMetaType<MafwRenderer*>();
41     m_stubHelper = new MafwStubHelper;
42     setStubHelper(m_stubHelper);
43
44     m_plugin = new MafwGstRendererPlugin();    
45 }
46
47 void Ut_MafwGstRendererPlugin::testInProcess()
48 {
49     QSignalSpy addedSpy( MafwRegistry::instance(), SIGNAL(rendererAdded(const QString&, const QString&)) );
50     QSignalSpy removedSpy( MafwRegistry::instance(), SIGNAL(rendererRemoved(const QString&, const QString&)) );
51     //MafwBasicRenderer::initialize is called twice:
52     //from its own constructor and from MafwGstRenderer::initialize
53
54     //Basic renderer initialize fails
55     m_stubHelper->expect("initialize", false);
56     m_stubHelper->expect("initialize", false);
57     m_plugin->initialize( MafwInternalRegistry::internalInstance() );
58     QCOMPARE( addedSpy.size(), 0 );
59     addedSpy.clear();
60     removedSpy.clear();
61
62     m_stubHelper->expect("initialize", true);
63     m_stubHelper->expect("initialize", true);
64     m_plugin->initialize( MafwInternalRegistry::internalInstance() );
65     QCOMPARE( addedSpy.size(), 1 );
66     QCOMPARE( removedSpy.size(), 0 );
67     QVariantList arguments = addedSpy.takeFirst();
68
69     QString uuid = arguments.at(0).toString();
70     QCOMPARE(uuid, QString("mafw_gst_renderer"));
71     QCOMPARE(arguments.at(1).toString(), QString("QMAFW GStreamer Renderer"));
72
73     MafwRenderer* rnd = MafwRegistry::instance()->renderer(uuid);
74     QVERIFY(rnd != 0);
75     QCOMPARE(rnd->pluginName(), m_plugin->name());
76
77     MafwInternalRegistry::internalInstance()->removeExtension(uuid);
78     QCOMPARE( removedSpy.size(), 1 );
79 }
80
81 void Ut_MafwGstRendererPlugin::testOutProcess()
82 {
83     QCoreApplication::setApplicationName("qmafw-dbus-wrapper");    
84     //TODO empty settings case
85
86     renderers.insert("mafw-gst-renderer", "Mafw-GStreamer-Renderer");
87     renderers.insert("mafw-gst-video-suite-renderer", "Mafw-GStreamer-Renderer-For-Video");
88
89     QSignalSpy addedSpy( MafwRegistry::instance(), SIGNAL(rendererAdded(const QString&, const QString&)) );
90     QSignalSpy removedSpy( MafwRegistry::instance(), SIGNAL(rendererRemoved(const QString&, const QString&)) );
91     //MafwBasicRenderer::initialize is called twice:
92     //from its own constructor and from MafwGstRenderer::initialize
93
94     //Basic renderer initialize fails
95     m_stubHelper->expect("initialize", false);
96     m_stubHelper->expect("initialize", false);
97     m_stubHelper->expect("initialize", false);
98     m_stubHelper->expect("initialize", false);
99     m_plugin->initialize( MafwInternalRegistry::internalInstance() );
100     QCOMPARE( addedSpy.size(), 0 );
101     addedSpy.clear();
102     removedSpy.clear();
103
104     m_stubHelper->expect("initialize", true);
105     m_stubHelper->expect("initialize", true);
106     m_stubHelper->expect("initialize", true);
107     m_stubHelper->expect("initialize", true);
108     m_plugin->initialize( MafwInternalRegistry::internalInstance() );
109     QCOMPARE( addedSpy.size(), 2 );
110     QCOMPARE( removedSpy.size(), 0 );
111
112     QVariantList arguments = addedSpy.takeFirst();
113     QString uuid1 = arguments.at(0).toString();
114     QString name1 = arguments.at(1).toString();
115     arguments = addedSpy.takeFirst();
116     QString uuid2 = arguments.at(0).toString();
117     QString name2 = arguments.at(1).toString();
118
119     MafwRenderer* rnd1 = MafwRegistry::instance()->renderer(uuid1);
120     QVERIFY(rnd1 != 0);
121     QCOMPARE(rnd1->pluginName(), m_plugin->name());
122     MafwRenderer* rnd2 = MafwRegistry::instance()->renderer(uuid2);
123     QVERIFY(rnd2 != 0);
124     QCOMPARE(rnd2->pluginName(), m_plugin->name());
125
126     QCOMPARE(uuid1, rnd1->uuid());
127     QCOMPARE(name1, rnd1->name());
128     QCOMPARE(uuid2, rnd2->uuid());
129     QCOMPARE(name2, rnd2->name());
130
131     MafwInternalRegistry::internalInstance()->removeExtension(uuid1);
132     MafwInternalRegistry::internalInstance()->removeExtension(uuid2);
133
134     // Test huge amount of renderers
135     renderers.clear();
136     addedSpy.clear();
137     removedSpy.clear();
138     for(int i = 0; i < 100; i++)
139     {
140         QString rndId = QString("rndId %1").arg(i);
141         QString rndName = QString("rndName %1").arg(i);
142         renderers.insert(rndId, rndName);
143         m_stubHelper->expect("initialize", true);
144         m_stubHelper->expect("initialize", true);
145     }
146     m_plugin->initialize( MafwInternalRegistry::internalInstance());
147     QCOMPARE(removedSpy.size(), 0);
148     QCOMPARE(addedSpy.size(), 100);
149
150     // Let's only remove 99 renderers
151     for(int i = 0; i < 99; i++)
152     {
153         QString rndId = QString("rndId %1").arg(i);
154         MafwInternalRegistry::internalInstance()->removeExtension(rndId);
155     }
156     QCOMPARE(removedSpy.size(), 99);
157     // Check that there is still one renderer left
158     MafwRenderer* rnd99 = MafwRegistry::instance()->renderer("rndId 99");
159     QVERIFY(rnd99 != 0);
160     MafwInternalRegistry::internalInstance()->removeExtension("rndId 99");
161
162     addedSpy.clear();
163     removedSpy.clear();
164
165     //Test no renderers
166     renderers.clear();
167     m_plugin->initialize(MafwInternalRegistry::internalInstance());
168     QCOMPARE(addedSpy.size(), 0);
169
170     //Test 2 renderers with same uuid and name
171     renderers.insert("rndId1", "rndName1");
172     renderers.insert("rndId1", "rndName1");
173     m_stubHelper->expect("initialize", true);
174     m_stubHelper->expect("initialize", true);
175     m_stubHelper->expect("initialize", false);
176     m_stubHelper->expect("initialize", false);
177     m_plugin->initialize( MafwInternalRegistry::internalInstance());
178     QCOMPARE(addedSpy.size(), 1);
179
180 }
181
182 void Ut_MafwGstRendererPlugin::cleanupTestCase()
183 {
184     delete m_plugin;
185     while(QCoreApplication::hasPendingEvents())
186     {
187         QCoreApplication::sendPostedEvents();
188         QCoreApplication::sendPostedEvents(0, QEvent::DeferredDelete);
189         QCoreApplication::processEvents();
190     }
191 }
192
193 void gst_init(int *argc, char **argv[])
194 {
195     Q_UNUSED(argc);
196     Q_UNUSED(argv);
197 }
198
199 QTEST_MAIN(Ut_MafwGstRendererPlugin)
200
201 //Volume stub
202 MafwGstRendererVolume::MafwGstRendererVolume()
203 {
204     qDebug() << "Volume stub ctor";
205 }
206
207 void MafwGstRendererVolume::connectToPulseAudio()
208 {
209 }
210
211 MafwGstRendererVolume::~MafwGstRendererVolume()
212 {
213 }
214
215 uint MafwGstRendererVolume::getVolume()
216 {
217     qDebug(__PRETTY_FUNCTION__);
218     return (uint)1;
219 }
220
221 bool MafwGstRendererVolume::setVolume (uint value)
222 {
223     Q_UNUSED(value);
224     return true;
225 }
226
227 //QSettings stub
228 QSettings::QSettings( const QString&, const QString&, QObject* ){}
229 //Hope we will never need below ctor stub, because it is used somehow by unit test framework.
230 //QSettings::QSettings(QSettings::Scope, const QString&, const QString&, QObject*){}
231 QSettings::QSettings(QSettings::Format, QSettings::Scope, const QString&, const QString&, QObject*){}
232 QSettings::QSettings(const QString&, QSettings::Format, QObject*){}
233 QSettings::QSettings(QObject*){}
234 QSettings::~QSettings(){}
235
236 QVariant QSettings::value(const QString& key, const QVariant& ) const
237 {
238     if(renderers.size() == 0)
239     {
240         return QString();
241     }
242     QMap<QString, QString>::const_iterator renderers_iterator = renderers.constBegin();
243     for(int i = 0; i < renderer_index; i++)
244     {
245         renderers_iterator++;
246     }
247
248     if(key == "Id")
249     {
250         return renderers_iterator.key();
251     }
252     else
253     {
254         return renderers_iterator.value();
255     }
256 }
257
258 int QSettings::beginReadArray(const QString&)
259 {
260     return renderers.count();
261 }
262
263 void QSettings::setArrayIndex(int i)
264 {
265     renderer_index = i;
266 }
267
268 void QSettings::endArray()
269 {
270 }
271
272 void QSettings::beginGroup(const QString &prefix)
273 {
274     Q_UNUSED(prefix);
275 }
276
277 void QSettings::endGroup()
278 {
279
280 }
281
282 bool QSettings::contains(const QString &key) const
283 {
284     return true;
285 }