* remove some warnings
[profile/ivi/genivi/genivi-audio-manager.git] / PluginCommandInterfaceDbus / test / CAmCommandSenderDbusSignalTest.cpp
1 /**
2  *  Copyright (c) 2012 BMW
3  *
4  *  \author Christian Mueller, christian.ei.mueller@bmw.de BMW 2011,2012
5  *
6  *  \copyright
7  *  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction,
8  *  including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
9  *  subject to the following conditions:
10  *  The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
11  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
12  *  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
13  *  THE USE OR OTHER DEALINGS IN THE SOFTWARE.
14  *
15  *  For further information see http://www.genivi.org/.
16  */
17
18 #include "CAmCommandSenderDbusSignalTest.h"
19 #include <Python.h>
20 #include <iostream>
21 #include <fstream>
22 #include <algorithm>
23 #include <string>
24 #include <vector>
25 #include <set>
26 #include "CAmCommandSenderDbusBackdoor.h"
27 #include "MockIAmCommandReceive.h"
28 #include "../include/CAmCommandSenderDbus.h"
29 #include "../include/CAmDbusMessageHandler.h"
30 #include "../../AudioManagerDaemon/include/TAmPluginTemplate.h"
31 #include "shared/CAmDltWrapper.h"
32 #include "configCommandDbus.h"
33
34 using namespace am;
35 using namespace testing;
36
37 CAmCommandSenderDbusSignalTest::CAmCommandSenderDbusSignalTest() :
38         ppCommandSend(NULL) //
39 {
40     CAmDltWrapper::instance()->registerApp("dbusTest", "dbusTest");
41     logInfo("dbusCommandInterfaceSignalTest started");
42 }
43
44 CAmCommandSenderDbusSignalTest::~CAmCommandSenderDbusSignalTest()
45 {
46 }
47
48 void* NumberOfMainConnectionsChanged(void* ppCommandSend)
49 {
50     sleep(3);
51     IAmCommandSend* sender=static_cast<IAmCommandSend*>(ppCommandSend);
52     am_MainConnectionType_s mainConnection;
53     sender->cbNewMainConnection(mainConnection);
54     return (NULL);
55 }
56
57 void* cbSinkAdded(void* ppCommandSend)
58 {
59     sleep(1);
60     IAmCommandSend* sender=static_cast<IAmCommandSend*>(ppCommandSend);
61     std::vector<am_SinkType_s> list;
62     am_SinkType_s mysink;
63     mysink.name = "MySink";
64     mysink.sinkID = 23;
65     mysink.availability.availability = A_MAX;
66     mysink.availability.availabilityReason = AR_UNKNOWN;
67     mysink.muteState = MS_UNKNOWN;
68     mysink.sinkClassID = 3;
69     mysink.volume = 234;
70     sender->cbNewSink(mysink);
71     return (NULL);
72 }
73
74 void* cbSourceAdded(void* ppCommandSend)
75 {
76     sleep(1);
77     IAmCommandSend* sender=static_cast<IAmCommandSend*>(ppCommandSend);
78     am_SourceType_s mysource;
79     mysource.name = "MySink";
80     mysource.sourceID = 42;
81     mysource.availability.availability = A_MAX;
82     mysource.availability.availabilityReason = AR_UNKNOWN;
83     mysource.sourceClassID = 15;
84     sender->cbNewSource(mysource);
85     return (NULL);
86 }
87
88 void* cbSourceRemoved(void* ppCommandSend)
89 {
90     sleep(1);
91     IAmCommandSend* sender=static_cast<IAmCommandSend*>(ppCommandSend);
92     am_SourceType_s mysource;
93     mysource.name = "MySink";
94     mysource.sourceID = 42;
95     sender->cbRemovedSource(mysource.sourceID);
96     return (NULL);
97 }
98
99 void* cbSinkRemoved(void* ppCommandSend)
100 {
101     sleep(1);
102     IAmCommandSend* sender=static_cast<IAmCommandSend*>(ppCommandSend);
103     am_SinkType_s mysink;
104     mysink.name = "MySink";
105     mysink.sinkID = 23;
106     sender->cbRemovedSink(mysink.sinkID);
107     return (NULL);
108 }
109
110 void* NumberOfSinkClassesChanged(void* ppCommandSend)
111 {
112     sleep(1);
113     IAmCommandSend* sender=static_cast<IAmCommandSend*>(ppCommandSend);
114     sender->cbNumberOfSinkClassesChanged();
115     return (NULL);
116 }
117
118 void* NumberOfSourceClassesChanged(void* ppCommandSend)
119 {
120     sleep(1);
121     IAmCommandSend* sender=static_cast<IAmCommandSend*>(ppCommandSend);
122     sender->cbNumberOfSourceClassesChanged();
123     return (NULL);
124 }
125
126 void* MainConnectionStateChanged(void* ppCommandSend)
127 {
128     sleep(1);
129     IAmCommandSend* sender=static_cast<IAmCommandSend*>(ppCommandSend);
130     am_connectionID_t id = 4;
131     am_ConnectionState_e state = CS_CONNECTING;
132     sender->cbMainConnectionStateChanged(id, state);
133     return (NULL);
134 }
135
136 void* MainSinkSoundPropertyChanged(void* ppCommandSend)
137 {
138     sleep(1);
139     IAmCommandSend* sender=static_cast<IAmCommandSend*>(ppCommandSend);
140     am_sinkID_t sinkID = 3;
141     am_MainSoundProperty_s soundProperty;
142     soundProperty.type = MSP_UNKNOWN;
143     soundProperty.value = 23;
144     sender->cbMainSinkSoundPropertyChanged(sinkID, soundProperty);
145     return (NULL);
146 }
147
148 void* MainSourceSoundPropertyChanged(void* ppCommandSend)
149 {
150     sleep(1);
151     IAmCommandSend* sender=static_cast<IAmCommandSend*>(ppCommandSend);
152     am_sourceID_t sourceID = 35;
153     am_MainSoundProperty_s soundProperty;
154     soundProperty.type = MSP_UNKNOWN;
155     soundProperty.value = 233;
156     sender->cbMainSourceSoundPropertyChanged(sourceID, soundProperty);
157     return (NULL);
158 }
159
160 void* cbSinkAvailabilityChangedLoop(void* ppCommandSend)
161 {
162     sleep(1);
163     IAmCommandSend* sender=static_cast<IAmCommandSend*>(ppCommandSend);
164     am_Availability_s av;
165     av.availability = A_AVAILABLE;
166     av.availabilityReason = AR_UNKNOWN;
167     sender->cbSinkAvailabilityChanged(4, av);
168     return (NULL);
169 }
170
171 void* VolumeChanged(void* ppCommandSend)
172 {
173     sleep(1);
174     IAmCommandSend* sender=static_cast<IAmCommandSend*>(ppCommandSend);
175     am_volume_t volume = 344;
176     sender->cbVolumeChanged(23, volume);
177     return (NULL);
178 }
179
180 void* cbSourceAvailabilityChangedLoop(void* ppCommandSend)
181 {
182     sleep(1);
183     IAmCommandSend* sender=static_cast<IAmCommandSend*>(ppCommandSend);
184     am_Availability_s av;
185     av.availability = A_AVAILABLE;
186     av.availabilityReason = AR_UNKNOWN;
187     sender->cbSourceAvailabilityChanged(2, av);
188     return (NULL);
189 }
190
191 void* SinkMuteStateChanged(void* ppCommandSend)
192 {
193     sleep(1);
194     IAmCommandSend* sender=static_cast<IAmCommandSend*>(ppCommandSend);
195     sender->cbSinkMuteStateChanged(42, MS_MUTED);
196     return (NULL);
197 }
198
199 void* SystemPropertyChanged(void* ppCommandSend)
200 {
201     sleep(1);
202     IAmCommandSend* sender=static_cast<IAmCommandSend*>(ppCommandSend);
203     am_SystemProperty_s property;
204     property.type = SYP_UNKNOWN;
205     property.value = 355;
206     sender->cbSystemPropertyChanged(property);
207     return (NULL);
208 }
209
210 void* TimingInformationChanged(void* ppCommandSend)
211 {
212     sleep(1);
213     IAmCommandSend* sender=static_cast<IAmCommandSend*>(ppCommandSend);
214     sender->cbTimingInformationChanged(42, 233);
215     return (NULL);
216 }
217
218 TEST_F(CAmCommandSenderDbusSignalTest,cbSourceAvailabilityChanged)
219 {
220
221     //ok, now we want to test the signals. It did not work out that the python receiver worked in an own thread (as originally intended)
222     //so it is running in the main context and the signals are send from threads...
223     CAmSocketHandler pSocketHandler;
224     CAmDbusWrapper pDBusWrapper(&pSocketHandler);
225     MockIAmCommandReceive pReceiveInterface;
226
227     IAmCommandSend* (*createFunc)();
228     void* tempLibHandle = NULL;
229     std::string libname(LIBRARY_OUTPUT_PATH);
230     libname.append("/libPluginCommandInterfaceDbus.so");
231     createFunc = getCreateFunction<IAmCommandSend*()>(libname, tempLibHandle);
232
233     if (!createFunc)
234     {
235         logError("CommandSendInterface Test Entry point of CommandPlugin not found");
236         exit(1);
237     }
238
239     ppCommandSend = createFunc();
240
241     if (!ppCommandSend)
242     {
243         logError("CommandSendInterface Test CommandPlugin initialization failed. Entry Function not callable");
244         exit(1);
245     }
246
247     //  ok, here we give the DBusWrapper pointer to the Plugin and start the interface
248     EXPECT_CALL(pReceiveInterface,getDBusConnectionWrapper(_)).WillRepeatedly(DoAll(SetArgReferee<0>(&pDBusWrapper), Return(E_OK)));
249     EXPECT_CALL(pReceiveInterface, confirmCommandReady(10,_));
250
251     ppCommandSend->startupInterface(&pReceiveInterface);
252     ppCommandSend->setCommandReady(10);
253
254     pthread_t pythonloop;
255     pthread_create(&pythonloop, NULL, NumberOfMainConnectionsChanged, (void*) ppCommandSend);
256     PyRun_SimpleStringFlags("import sys\n"
257             "import traceback\n"
258             "import gobject\n"
259             "import dbus\n"
260             "import dbus.mainloop.glib\n"
261             "print('Started thread') \n"
262             "loop = gobject.MainLoop()\n"
263             "def catchsignal(*arg, **kwarg):\n"
264             "   print ('Caught NumberOfMainConnectionsChanged') \n"
265             "   loop.quit()\n"
266             "dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)\n"
267             "bus = dbus.SessionBus()\n"
268             "bus.add_signal_receiver(catchsignal, signal_name='NumberOfMainConnectionsChanged', dbus_interface = 'org.genivi.audiomanager.CommandInterface', message_keyword='dbus_message')\n"
269             "print('Started thread') \n"
270             "loop.run()\n", NULL);
271     pthread_join(pythonloop, NULL);
272
273     pthread_create(&pythonloop, NULL, cbSinkAdded, (void*) ppCommandSend);
274     PyRun_SimpleStringFlags("import sys\n"
275             "import traceback\n"
276             "import gobject\n"
277             "import dbus\n"
278             "import dbus.mainloop.glib\n"
279             "loop = gobject.MainLoop()\n"
280             "def catchSinkAdded(*arg, **karg):\n"
281             "   print ('Caught signal (in SinkAdded handler) ') \n"
282             "   print (arg[0])\n"
283             "   f = open('/tmp/result.txt','w')\n"
284             "   f.write(str(arg[0]));\n"
285             "   f.close()\n"
286             "   loop.quit()\n"
287             "dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)\n"
288             "bus = dbus.SessionBus()\n"
289             "bus.add_signal_receiver(catchSinkAdded, signal_name='SinkAdded', dbus_interface = 'org.genivi.audiomanager.CommandInterface', message_keyword='dbus_message')\n"
290             "loop.run()\n", NULL);
291     pthread_join(pythonloop, NULL);
292
293     std::ifstream ifs("/tmp/result.txt");
294     std::string line;
295     int lineCounter = 0, result = 0;
296     while (std::getline(ifs, line))
297     {
298         ASSERT_EQ(line.compare("dbus.Struct((dbus.UInt16(23), dbus.String(u'MySink'), dbus.Struct((dbus.Int16(3), dbus.Int16(0)), signature=None), dbus.Int16(234), dbus.Int16(0), dbus.UInt16(3)), signature=None)"), 0);
299     }
300     ifs.close();
301
302     pthread_create(&pythonloop, NULL, cbSinkRemoved, (void*) ppCommandSend);
303     PyRun_SimpleStringFlags("import sys\n"
304             "import traceback\n"
305             "import gobject\n"
306             "import dbus\n"
307             "import dbus.mainloop.glib\n"
308             "loop = gobject.MainLoop()\n"
309             "def catchSinkRemoved(*arg, **karg):\n"
310             "   print ('Caught signal (in SinkRemoved handler) ') \n"
311             "   print (arg[0])\n"
312             "   f = open('/tmp/result.txt','w')\n"
313             "   f.write(str(arg[0]));\n"
314             "   f.close()\n"
315             "   loop.quit()\n"
316             "dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)\n"
317             "bus = dbus.SessionBus()\n"
318             "bus.add_signal_receiver(catchSinkRemoved, signal_name='SinkRemoved', dbus_interface = 'org.genivi.audiomanager.CommandInterface', message_keyword='dbus_message')\n"
319             "loop.run()\n", NULL);
320     pthread_join(pythonloop, NULL);
321
322     ifs.open("/tmp/result.txt");
323     lineCounter = 0;
324     result = 0;
325     while (std::getline(ifs, line))
326     {
327         std::stringstream(line) >> result;
328         ASSERT_EQ(result, 23);
329     }
330     ifs.close();
331
332     pthread_create(&pythonloop, NULL, cbSourceAdded, (void*) ppCommandSend);
333     PyRun_SimpleStringFlags("import sys\n"
334             "import traceback\n"
335             "import gobject\n"
336             "import dbus\n"
337             "import dbus.mainloop.glib\n"
338             "loop = gobject.MainLoop()\n"
339             "def catchSourceAdded(*arg, **karg):\n"
340             "   print ('Caught signal (in SourceAdded handler) ') \n"
341             "   print (arg[0])\n"
342             "   f = open('/tmp/result.txt','w')\n"
343             "   f.write(str(arg[0]));\n"
344             "   f.close()\n"
345             "   loop.quit()\n"
346             "dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)\n"
347             "bus = dbus.SessionBus()\n"
348             "bus.add_signal_receiver(catchSourceAdded, signal_name='SourceAdded', dbus_interface = 'org.genivi.audiomanager.CommandInterface', message_keyword='dbus_message')\n"
349             "loop.run()\n", NULL);
350     pthread_join(pythonloop, NULL);
351
352     ifs.open("/tmp/result.txt");
353     lineCounter = 0;
354     result = 0;
355     while (std::getline(ifs, line))
356     {
357         ASSERT_EQ(line.compare("dbus.Struct((dbus.UInt16(42), dbus.String(u'MySink'), dbus.Struct((dbus.Int16(3), dbus.Int16(0)), signature=None), dbus.UInt16(15)), signature=None)"), 0);
358     }
359     ifs.close();
360
361     pthread_create(&pythonloop, NULL, cbSourceRemoved, (void*) ppCommandSend);
362     PyRun_SimpleStringFlags("import sys\n"
363             "import traceback\n"
364             "import gobject\n"
365             "import dbus\n"
366             "import dbus.mainloop.glib\n"
367             "loop = gobject.MainLoop()\n"
368             "def catchSourceRemoved(*arg, **karg):\n"
369             "   print ('Caught signal (in SinkRemoved handler) ') \n"
370             "   print (arg[0])\n"
371             "   f = open('/tmp/result.txt','w')\n"
372             "   f.write(str(arg[0]));\n"
373             "   f.close()\n"
374             "   loop.quit()\n"
375             "dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)\n"
376             "bus = dbus.SessionBus()\n"
377             "bus.add_signal_receiver(catchSourceRemoved, signal_name='SourceRemoved', dbus_interface = 'org.genivi.audiomanager.CommandInterface', message_keyword='dbus_message')\n"
378             "loop.run()\n", NULL);
379     pthread_join(pythonloop, NULL);
380
381     ifs.open("/tmp/result.txt");
382     lineCounter = 0;
383     result = 0;
384     while (std::getline(ifs, line))
385     {
386         std::stringstream(line) >> result;
387         ASSERT_EQ(result, 42);
388     }
389     ifs.close();
390
391     pthread_create(&pythonloop, NULL, NumberOfSinkClassesChanged, (void*) ppCommandSend);
392     PyRun_SimpleStringFlags("import sys\n"
393             "import traceback\n"
394             "import gobject\n"
395             "import dbus\n"
396             "import dbus.mainloop.glib\n"
397             "loop = gobject.MainLoop()\n"
398             "def catchNumberOfSinkClassesChanged(*arg, **kwarg):\n"
399             "   print ('Caught catchNumberOfSinkClassesChanged') \n"
400             "   loop.quit()\n"
401             "dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)\n"
402             "bus = dbus.SessionBus()\n"
403             "bus.add_signal_receiver(catchNumberOfSinkClassesChanged, signal_name='NumberOfSinkClassesChanged', dbus_interface = 'org.genivi.audiomanager.CommandInterface', message_keyword='dbus_message')\n"
404             "loop.run()\n", NULL);
405     pthread_join(pythonloop, NULL);
406
407     pthread_create(&pythonloop, NULL, NumberOfSourceClassesChanged, (void*) ppCommandSend);
408     PyRun_SimpleStringFlags("import sys\n"
409             "import traceback\n"
410             "import gobject\n"
411             "import dbus\n"
412             "import dbus.mainloop.glib\n"
413             "loop = gobject.MainLoop()\n"
414             "def CatchNumberOfSourceClassesChanged(*arg, **kwarg):\n"
415             "   print ('Caught CatchNumberOfSourceClassesChanged') \n"
416             "   loop.quit()\n"
417             "dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)\n"
418             "bus = dbus.SessionBus()\n"
419             "bus.add_signal_receiver(CatchNumberOfSourceClassesChanged, signal_name='NumberOfSourceClassesChanged', dbus_interface = 'org.genivi.audiomanager.CommandInterface', message_keyword='dbus_message')\n"
420             "loop.run()\n", NULL);
421     pthread_join(pythonloop, NULL);
422
423     //ok, now we want to test the signals. It did not work out that the python receiver worked in an own thread (as originally intended)
424     //so it is running in the main context and the signals are send from threads...
425     pthread_create(&pythonloop, NULL, MainConnectionStateChanged, (void*) ppCommandSend);
426     PyRun_SimpleStringFlags("import sys\n"
427             "import traceback\n"
428             "import gobject\n"
429             "import dbus\n"
430             "import dbus.mainloop.glib\n"
431             "loop = gobject.MainLoop()\n"
432             "def catchMainConnectionStateChanged(*arg, **karg):\n"
433             "   print ('Caught signal (in catchMainConnectionStateChanged handler) ') \n"
434             "   print (arg[0])\n"
435             "   print (arg[1])\n"
436             "   f = open('/tmp/result.txt','w')\n"
437             "   f.write(str(arg[0]) + '\\n' + str (arg[1]));\n"
438             "   f.close()\n"
439             "   loop.quit()\n"
440             "dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)\n"
441             "bus = dbus.SessionBus()\n"
442             "bus.add_signal_receiver(catchMainConnectionStateChanged, signal_name='MainConnectionStateChanged', dbus_interface = 'org.genivi.audiomanager.CommandInterface', message_keyword='dbus_message')\n"
443             "loop.run()\n", NULL);
444     pthread_join(pythonloop, NULL);
445
446     ifs.open("/tmp/result.txt");
447     lineCounter = 0;
448     result = 0;
449     while (std::getline(ifs, line))
450     {
451         if (lineCounter == 0)
452         {
453             std::stringstream(line) >> result;
454             ASSERT_EQ(result, 4);
455         }
456         else if (lineCounter == 1)
457         {
458             std::stringstream(line) >> result;
459             ASSERT_EQ(result, CS_CONNECTING);
460         }
461         lineCounter++;
462     }
463     ifs.close();
464
465     //ok, now we want to test the signals. It did not work out that the python receiver worked in an own thread (as originally intended)
466     //so it is running in the main context and the signals are send from threads...
467     pthread_create(&pythonloop, NULL, MainSinkSoundPropertyChanged, (void*) ppCommandSend);
468     PyRun_SimpleStringFlags("import sys\n"
469             "import traceback\n"
470             "import gobject\n"
471             "import dbus\n"
472             "import dbus.mainloop.glib\n"
473             "loop = gobject.MainLoop()\n"
474             "def catchMainSinkSoundPropertyChanged(*arg, **karg):\n"
475             "   print ('Caught signal (in catchMainSinkSoundPropertyChanged handler) ') \n"
476             "   print (arg[0])\n"
477             "   print (arg[1])\n"
478             "   f = open('/tmp/result.txt','w')\n"
479             "   f.write(str(arg[0]) + '\\n' + str (arg[1]));\n"
480             "   f.close()\n"
481             "   loop.quit()\n"
482             "dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)\n"
483             "bus = dbus.SessionBus()\n"
484             "bus.add_signal_receiver(catchMainSinkSoundPropertyChanged, signal_name='MainSinkSoundPropertyChanged', dbus_interface = 'org.genivi.audiomanager.CommandInterface', message_keyword='dbus_message')\n"
485             "loop.run()\n", NULL);
486     pthread_join(pythonloop, NULL);
487
488     ifs.open("/tmp/result.txt");
489     lineCounter = 0;
490     result = 0;
491     while (std::getline(ifs, line))
492     {
493         if (lineCounter == 0)
494         {
495             std::stringstream(line) >> result;
496             ASSERT_EQ(result, 3);
497         }
498         else if (lineCounter == 1)
499         {
500             ASSERT_EQ(line.compare("dbus.Struct((dbus.Int16(0), dbus.Int16(23)), signature=None)"), 0);
501         }
502         lineCounter++;
503     }
504     ifs.close();
505
506     //ok, now we want to test the signals. It did not work out that the python receiver worked in an own thread (as originally intended)
507     //so it is running in the main context and the signals are send from threads...
508     pthread_create(&pythonloop, NULL, MainSourceSoundPropertyChanged, (void*) ppCommandSend);
509     PyRun_SimpleStringFlags("import sys\n"
510             "import traceback\n"
511             "import gobject\n"
512             "import dbus\n"
513             "import dbus.mainloop.glib\n"
514             "loop = gobject.MainLoop()\n"
515             "def catchMainSourceSoundPropertyChanged(*arg, **karg):\n"
516             "   print ('Caught signal (in catchMainSourceSoundPropertyChanged handler) ') \n"
517             "   print (arg[0])\n"
518             "   print (arg[1])\n"
519             "   f = open('/tmp/result.txt','w')\n"
520             "   f.write(str(arg[0]) + '\\n' + str (arg[1]));\n"
521             "   f.close()\n"
522             "   loop.quit()\n"
523             "dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)\n"
524             "bus = dbus.SessionBus()\n"
525             "bus.add_signal_receiver(catchMainSourceSoundPropertyChanged, signal_name='MainSourceSoundPropertyChanged', dbus_interface = 'org.genivi.audiomanager.CommandInterface', message_keyword='dbus_message')\n"
526             "loop.run()\n", NULL);
527     pthread_join(pythonloop, NULL);
528
529     ifs.open("/tmp/result.txt");
530     lineCounter = 0;
531     result = 0;
532     while (std::getline(ifs, line))
533     {
534         if (lineCounter == 0)
535         {
536             std::stringstream(line) >> result;
537             ASSERT_EQ(result, 35);
538         }
539         else if (lineCounter == 1)
540         {
541             ASSERT_EQ(line.compare("dbus.Struct((dbus.Int16(0), dbus.Int16(233)), signature=None)"), 0);
542         }
543         lineCounter++;
544     }
545     ifs.close();
546
547     //ok, now we want to test the signals. It did not work out that the python receiver worked in an own thread (as originally intended)
548     //so it is running in the main context and the signals are send from threads...
549     pthread_create(&pythonloop, NULL, cbSinkAvailabilityChangedLoop, (void*) ppCommandSend);
550     PyRun_SimpleStringFlags("import sys\n"
551             "import traceback\n"
552             "import gobject\n"
553             "import dbus\n"
554             "import dbus.mainloop.glib\n"
555             "loop = gobject.MainLoop()\n"
556             "def catchSinkAvailabilityChanged(*arg, **karg):\n"
557             "   print ('Caught signal (in catchSinkAvailabilityChanged handler) ') \n"
558             "   print (arg[0])\n"
559             "   print (arg[1])\n"
560             "   f = open('/tmp/result.txt','w')\n"
561             "   f.write(str(arg[0]) + '\\n' + str (arg[1]));\n"
562             "   f.close()\n"
563             "   loop.quit()\n"
564             "dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)\n"
565             "bus = dbus.SessionBus()\n"
566             "bus.add_signal_receiver(catchSinkAvailabilityChanged, signal_name='SinkAvailabilityChanged', dbus_interface = 'org.genivi.audiomanager.CommandInterface', message_keyword='dbus_message')\n"
567             "loop.run()\n", NULL);
568     pthread_join(pythonloop, NULL);
569
570     ifs.open("/tmp/result.txt");
571     lineCounter = 0;
572     result = 0;
573     while (std::getline(ifs, line))
574     {
575         if (lineCounter == 0)
576         {
577             std::stringstream(line) >> result;
578             ASSERT_EQ(result, 4);
579         }
580         else if (lineCounter == 1)
581         {
582             ASSERT_EQ(line.compare("dbus.Struct((dbus.Int16(1), dbus.Int16(0)), signature=None)"), 0);
583         }
584         lineCounter++;
585     }
586     ifs.close();
587
588     //ok, now we want to test the signals. It did not work out that the python receiver worked in an own thread (as originally intended)
589     //so it is running in the main context and the signals are send from threads...
590     pthread_create(&pythonloop, NULL, cbSourceAvailabilityChangedLoop, (void*) ppCommandSend);
591     PyRun_SimpleStringFlags("import sys\n"
592             "import traceback\n"
593             "import gobject\n"
594             "import dbus\n"
595             "import dbus.mainloop.glib\n"
596             "loop = gobject.MainLoop()\n"
597             "def catchSourceAvailability(*arg, **karg):\n"
598             "   print ('Caught signal (in catchSourceAvailability handler) ') \n"
599             "   print (arg[0])\n"
600             "   print (arg[1])\n"
601             "   f = open('/tmp/result.txt','w')\n"
602             "   f.write(str(arg[0]) + '\\n' + str (arg[1]));\n"
603             "   f.close()\n"
604             "   loop.quit()\n"
605             "dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)\n"
606             "bus = dbus.SessionBus()\n"
607             "bus.add_signal_receiver(catchSourceAvailability, signal_name='SourceAvailabilityChanged', dbus_interface = 'org.genivi.audiomanager.CommandInterface', message_keyword='dbus_message')\n"
608             "loop.run()\n", NULL);
609     pthread_join(pythonloop, NULL);
610
611     ifs.open("/tmp/result.txt");
612     lineCounter = 0;
613     result = 0;
614     while (std::getline(ifs, line))
615     {
616         if (lineCounter == 0)
617         {
618             std::stringstream(line) >> result;
619             ASSERT_EQ(result, 2);
620         }
621         else if (lineCounter == 1)
622         {
623             ASSERT_EQ(line.compare("dbus.Struct((dbus.Int16(1), dbus.Int16(0)), signature=None)"), 0);
624         }
625         lineCounter++;
626     }
627     ifs.close();
628
629     //ok, now we want to test the signals. It did not work out that the python receiver worked in an own thread (as originally intended)
630     //so it is running in the main context and the signals are send from threads...
631     pthread_create(&pythonloop, NULL, VolumeChanged, (void*) ppCommandSend);
632     PyRun_SimpleStringFlags("import sys\n"
633             "import traceback\n"
634             "import gobject\n"
635             "import dbus\n"
636             "import dbus.mainloop.glib\n"
637             "loop = gobject.MainLoop()\n"
638             "def catchVolumeChanged(*arg, **karg):\n"
639             "   print ('Caught signal (in catchVolumeChanged handler) ') \n"
640             "   print (arg[0])\n"
641             "   print (arg[1])\n"
642             "   f = open('/tmp/result.txt','w')\n"
643             "   f.write(str(arg[0]) + '\\n' + str (arg[1]));\n"
644             "   f.close()\n"
645             "   loop.quit()\n"
646             "dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)\n"
647             "bus = dbus.SessionBus()\n"
648             "bus.add_signal_receiver(catchVolumeChanged, signal_name='VolumeChanged', dbus_interface = 'org.genivi.audiomanager.CommandInterface', message_keyword='dbus_message')\n"
649             "loop.run()\n", NULL);
650     pthread_join(pythonloop, NULL);
651
652     ifs.open("/tmp/result.txt");
653     lineCounter = 0;
654     result = 0;
655     while (std::getline(ifs, line))
656     {
657         if (lineCounter == 0)
658         {
659             std::stringstream(line) >> result;
660             ASSERT_EQ(result, 23);
661         }
662         else if (lineCounter == 1)
663         {
664             std::stringstream(line) >> result;
665             ASSERT_EQ(result, 344);
666         }
667         lineCounter++;
668     }
669     ifs.close();
670
671     //ok, now we want to test the signals. It did not work out that the python receiver worked in an own thread (as originally intended)
672     //so it is running in the main context and the signals are send from threads...
673     pthread_create(&pythonloop, NULL, SinkMuteStateChanged, (void*) ppCommandSend);
674     PyRun_SimpleStringFlags("import sys\n"
675             "import traceback\n"
676             "import gobject\n"
677             "import dbus\n"
678             "import dbus.mainloop.glib\n"
679             "loop = gobject.MainLoop()\n"
680             "def catchSinkMuteStateChanged(*arg, **karg):\n"
681             "   print ('Caught signal (in catchSinkMuteStateChanged handler) ') \n"
682             "   print (arg[0])\n"
683             "   print (arg[1])\n"
684             "   f = open('/tmp/result.txt','w')\n"
685             "   f.write(str(arg[0]) + '\\n' + str (arg[1]));\n"
686             "   f.close()\n"
687             "   loop.quit()\n"
688             "dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)\n"
689             "bus = dbus.SessionBus()\n"
690             "bus.add_signal_receiver(catchSinkMuteStateChanged, signal_name='SinkMuteStateChanged', dbus_interface = 'org.genivi.audiomanager.CommandInterface', message_keyword='dbus_message')\n"
691             "loop.run()\n", NULL);
692     pthread_join(pythonloop, NULL);
693
694     ifs.open("/tmp/result.txt");
695     lineCounter = 0;
696     result = 0;
697     while (std::getline(ifs, line))
698     {
699         if (lineCounter == 0)
700         {
701             std::stringstream(line) >> result;
702             ASSERT_EQ(result, 42);
703         }
704         else if (lineCounter == 1)
705         {
706             std::stringstream(line) >> result;
707             ASSERT_EQ(result, MS_MUTED);
708         }
709         lineCounter++;
710     }
711     ifs.close();
712
713     //ok, now we want to test the signals. It did not work out that the python receiver worked in an own thread (as originally intended)
714     //so it is running in the main context and the signals are send from threads...
715     pthread_create(&pythonloop, NULL, SystemPropertyChanged, (void*) ppCommandSend);
716     PyRun_SimpleStringFlags("import sys\n"
717             "import traceback\n"
718             "import gobject\n"
719             "import dbus\n"
720             "import dbus.mainloop.glib\n"
721             "loop = gobject.MainLoop()\n"
722             "def catchSystemPropertyChanged(*arg, **karg):\n"
723             "   print ('Caught signal (in catchSystemPropertyChanged handler) ') \n"
724             "   print (arg[0])\n"
725             "   f = open('/tmp/result.txt','w')\n"
726             "   f.write(str(arg[0]));\n"
727             "   f.close()\n"
728             "   loop.quit()\n"
729             "dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)\n"
730             "bus = dbus.SessionBus()\n"
731             "bus.add_signal_receiver(catchSystemPropertyChanged, signal_name='SystemPropertyChanged', dbus_interface = 'org.genivi.audiomanager.CommandInterface', message_keyword='dbus_message')\n"
732             "loop.run()\n", NULL);
733     pthread_join(pythonloop, NULL);
734
735     ifs.open("/tmp/result.txt");
736     lineCounter = 0;
737     result = 0;
738     while (std::getline(ifs, line))
739     {
740         ASSERT_EQ(line.compare("dbus.Struct((dbus.Int16(0), dbus.Int16(355)), signature=None)"), 0);
741     }
742     ifs.close();
743
744     //ok, now we want to test the signals. It did not work out that the python receiver worked in an own thread (as originally intended)
745     //so it is running in the main context and the signals are send from threads...
746     pthread_create(&pythonloop, NULL, TimingInformationChanged, (void*) ppCommandSend);
747     PyRun_SimpleStringFlags("import sys\n"
748             "import traceback\n"
749             "import gobject\n"
750             "import dbus\n"
751             "import dbus.mainloop.glib\n"
752             "loop = gobject.MainLoop()\n"
753             "def catchTimingInformationChanged(*arg, **karg):\n"
754             "   print ('Caught signal (in catchTimingInformationChanged handler) ') \n"
755             "   print (arg[0])\n"
756             "   print (arg[1])\n"
757             "   f = open('/tmp/result.txt','w')\n"
758             "   f.write(str(arg[0]) + '\\n' + str (arg[1]));\n"
759             "   f.close()\n"
760             "   loop.quit()\n"
761             "dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)\n"
762             "bus = dbus.SessionBus()\n"
763             "bus.add_signal_receiver(catchTimingInformationChanged, signal_name='TimingInformationChanged', dbus_interface = 'org.genivi.audiomanager.CommandInterface', message_keyword='dbus_message')\n"
764             "loop.run()\n", NULL);
765     pthread_join(pythonloop, NULL);
766
767     ifs.open("/tmp/result.txt");
768     lineCounter = 0;
769     result = 0;
770     while (std::getline(ifs, line))
771     {
772         if (lineCounter == 0)
773         {
774             std::stringstream(line) >> result;
775             ASSERT_EQ(result, 42);
776         }
777         else if (lineCounter == 1)
778         {
779             std::stringstream(line) >> result;
780             ASSERT_EQ(result, 233);
781         }
782         lineCounter++;
783     }
784     ifs.close();
785
786 }
787
788 int main(int argc, char **argv)
789 {
790     ::testing::InitGoogleTest(&argc, argv);
791     return RUN_ALL_TESTS();
792 }
793
794 void CAmCommandSenderDbusSignalTest::SetUp()
795 {
796     Py_Initialize();
797 }
798
799 void CAmCommandSenderDbusSignalTest::TearDown()
800 {
801     Py_Finalize();
802 }
803