* formatting all the source code with eclipse source code style
[profile/ivi/genivi/genivi-audio-manager.git] / PluginRoutingInterfaceAsync / test / testRoutingInterfaceAsync.cpp
1 /**
2  * Copyright (C) 2011, BMW AG
3  *
4  * GeniviAudioMananger AudioManagerDaemon
5  *
6  * \file testRoutingItnerfaceAsync.cpp
7  *
8  * \date 20-Oct-2011 3:42:04 PM
9  * \author Christian Mueller (christian.ei.mueller@bmw.de)
10  *
11  * \section License
12  * GNU Lesser General Public License, version 2.1, with special exception (GENIVI clause)
13  * Copyright (C) 2011, BMW AG Christian Mueller  Christian.ei.mueller@bmw.de
14  *
15  * This program is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License, version 2.1, as published by the Free Software Foundation.
16  * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License, version 2.1, for more details.
17  * You should have received a copy of the GNU Lesser General Public License, version 2.1, along with this program; if not, see <http://www.gnu.org/licenses/lgpl-2.1.html>.
18  * Note that the copyright holders assume that the GNU Lesser General Public License, version 2.1, may also be applicable to programs even in cases in which the program is not a library in the technical sense.
19  * Linking AudioManager statically or dynamically with other modules is making a combined work based on AudioManager. You may license such other modules under the GNU Lesser General Public License, version 2.1. If you do not want to license your linked modules under the GNU Lesser General Public License, version 2.1, you may use the program under the following exception.
20  * As a special exception, the copyright holders of AudioManager give you permission to combine AudioManager with software programs or libraries that are released under any license unless such a combination is not permitted by the license of such a software program or library. You may copy and distribute such a system following the terms of the GNU Lesser General Public License, version 2.1, including this special exception, for AudioManager and the licenses of the other code concerned.
21  * Note that people who make modified versions of AudioManager are not obligated to grant this special exception for their modified versions; it is their choice whether to do so. The GNU Lesser General Public License, version 2.1, gives permission to release a modified version without this exception; this exception also makes it possible to release a modified version which carries forward this exception.
22  *
23  */
24
25 #include "testRoutingInterfaceAsync.h"
26 #include "config.h"
27
28 using namespace am;
29 using namespace testing;
30
31 DLT_DECLARE_CONTEXT(DLT_CONTEXT)
32
33 std::vector<std::string> testRoutingInterfaceAsync::pListRoutingPluginDirs = returnListPlugins();
34 am_domainID_t testRoutingInterfaceAsync::mDomainIDCount = 0;
35 RoutingSender testRoutingInterfaceAsync::pRoutingSender = RoutingSender(pListRoutingPluginDirs);
36
37 testRoutingInterfaceAsync::testRoutingInterfaceAsync() :
38         pSocketHandler(), //
39         pReceiveInterface(), //
40         ptimerCallback(this, &testRoutingInterfaceAsync::timerCallback)
41 {
42 }
43
44 testRoutingInterfaceAsync::~testRoutingInterfaceAsync()
45 {
46 }
47
48 void testRoutingInterfaceAsync::SetUp()
49 {
50     DLT_REGISTER_APP("DPtest", "RoutingInterfacetest");
51     DLT_REGISTER_CONTEXT(DLT_CONTEXT, "Main", "Main Context");
52     DLT_LOG(DLT_CONTEXT, DLT_LOG_INFO, DLT_STRING("RoutingSendInterface Test started "));
53
54     std::vector<int> domainIDs;
55     domainIDs.push_back(0);
56     domainIDs.push_back(1);
57
58     EXPECT_CALL(pReceiveInterface,getSocketHandler(_)).WillOnce(DoAll(SetArgReferee<0>(&pSocketHandler), Return(E_OK)));
59     EXPECT_CALL(pReceiveInterface,registerDomain(_,_)).WillRepeatedly(Invoke(testRoutingInterfaceAsync::handleDomainRegister));
60     EXPECT_CALL(pReceiveInterface,registerSource(_,_)).WillRepeatedly(Invoke(testRoutingInterfaceAsync::handleSourceRegister));
61     EXPECT_CALL(pReceiveInterface,registerSink(_,_)).WillRepeatedly(Invoke(testRoutingInterfaceAsync::handleSinkRegister));
62
63     pRoutingSender.startupRoutingInterface(&pReceiveInterface);
64     pRoutingSender.routingInterfacesReady();
65
66     timespec t;
67     t.tv_nsec = 0;
68     t.tv_sec = 4;
69
70     sh_timerHandle_t handle;
71
72     shTimerCallBack *buf = &ptimerCallback;
73     //lets use a timeout so the test will finish
74     pSocketHandler.addTimer(t, buf, handle, (void*) NULL);
75 }
76
77 std::vector<std::string> am::testRoutingInterfaceAsync::returnListPlugins()
78 {
79     std::vector<std::string> list;
80     list.push_back(std::string(DEFAULT_PLUGIN_ROUTING_DIR));
81     return (list);
82 }
83
84 am_Error_e am::testRoutingInterfaceAsync::handleSourceRegister(const am_Source_s & sourceData, am_sourceID_t & sourceID)
85 {
86     sourceID = sourceData.sourceID;
87     pRoutingSender.addSourceLookup(sourceData);
88     return (E_OK);
89 }
90
91 am_Error_e am::testRoutingInterfaceAsync::handleSinkRegister(const am_Sink_s & sinkData, am_sinkID_t & sinkID)
92 {
93     sinkID = sinkData.sinkID;
94     pRoutingSender.addSinkLookup(sinkData);
95     return (E_OK);
96 }
97
98 am_Error_e am::testRoutingInterfaceAsync::handleDomainRegister(const am_Domain_s & domainData, am_domainID_t & domainID)
99 {
100     am_Domain_s domain = domainData;
101     domainID = mDomainIDCount++;
102     domain.domainID = domainID;
103     pRoutingSender.addDomainLookup(domain);
104     return (E_OK);
105 }
106
107 void am::testRoutingInterfaceAsync::timerCallback(sh_timerHandle_t handle, void *userData)
108 {
109     pSocketHandler.stop_listening();
110 }
111
112 void testRoutingInterfaceAsync::TearDown()
113 {
114     DLT_UNREGISTER_CONTEXT(DLT_CONTEXT);
115 }
116
117 TEST_F(testRoutingInterfaceAsync,setDomainState)
118 {
119     am_domainID_t domainID = 1;
120     am_DomainState_e state = DS_INDEPENDENT_RUNDOWN;
121
122     EXPECT_CALL(pReceiveInterface,hookDomainStateChange(_,DS_INDEPENDENT_RUNDOWN)).Times(1);
123
124     ASSERT_EQ(E_OK, pRoutingSender.setDomainState(domainID,state));
125     pSocketHandler.start_listenting();
126 }
127
128 TEST_F(testRoutingInterfaceAsync,setSourceSoundProperty)
129 {
130
131     am_Handle_s handle;
132     handle.handle = 1;
133     handle.handleType = H_SETSOURCESOUNDPROPERTY;
134
135     am_sourceID_t sourceID = 3;
136     am_SoundProperty_s property;
137     property.type = SP_MID;
138     property.value = 24;
139
140     EXPECT_CALL(pReceiveInterface,ackSetSourceSoundProperty(_,E_OK)).Times(1);
141
142     ASSERT_EQ(E_OK, pRoutingSender.asyncSetSourceSoundProperty(handle,sourceID,property));
143     pSocketHandler.start_listenting();
144 }
145
146 TEST_F(testRoutingInterfaceAsync,setSinkSoundProperty)
147 {
148
149     am_Handle_s handle;
150     handle.handle = 1;
151     handle.handleType = H_SETSINKSOUNDPROPERTY;
152
153     am_sinkID_t sinkID = 1;
154     am_SoundProperty_s property;
155     property.type = SP_MID;
156     property.value = 24;
157
158     EXPECT_CALL(pReceiveInterface,ackSetSinkSoundProperty(_,E_OK)).Times(1);
159
160     ASSERT_EQ(E_OK, pRoutingSender.asyncSetSinkSoundProperty(handle,sinkID,property));
161     pSocketHandler.start_listenting();
162 }
163
164 TEST_F(testRoutingInterfaceAsync,setSourceState)
165 {
166
167     am_Handle_s handle;
168     handle.handle = 1;
169     handle.handleType = H_SETSOURCEVOLUME;
170
171     am_sourceID_t sourceID = 1;
172     am_SourceState_e state = SS_OFF;
173
174     EXPECT_CALL(pReceiveInterface,ackSetSourceState(_,E_OK)).Times(1);
175
176     ASSERT_EQ(E_OK, pRoutingSender.asyncSetSourceState(handle,sourceID,state));
177     pSocketHandler.start_listenting();
178 }
179
180 TEST_F(testRoutingInterfaceAsync,setSourceVolume)
181 {
182
183     am_Handle_s handle;
184     handle.handle = 1;
185     handle.handleType = H_SETSOURCEVOLUME;
186
187     am_sourceID_t sourceID = 3;
188     am_volume_t volume = 3;
189     am_RampType_e ramp = RAMP_DIRECT;
190     am_time_t myTime = 25;
191
192     EXPECT_CALL(pReceiveInterface,ackSourceVolumeTick(_,sourceID,_)).Times(3);
193     EXPECT_CALL(pReceiveInterface,ackSetSourceVolumeChange(_,volume,E_OK)).Times(1);
194
195     ASSERT_EQ(E_OK, pRoutingSender.asyncSetSourceVolume(handle,sourceID,volume,ramp,myTime));
196     pSocketHandler.start_listenting();
197 }
198
199 TEST_F(testRoutingInterfaceAsync,setSinkVolume)
200 {
201
202     am_Handle_s handle;
203     handle.handle = 1;
204     handle.handleType = H_SETSINKVOLUME;
205
206     am_sinkID_t sinkID = 1;
207     am_volume_t volume = 9;
208     am_RampType_e ramp = RAMP_DIRECT;
209     am_time_t myTime = 25;
210
211     EXPECT_CALL(pReceiveInterface,ackSinkVolumeTick(_,sinkID,_)).Times(9);
212     EXPECT_CALL(pReceiveInterface,ackSetSinkVolumeChange(_,volume,E_OK)).Times(1);
213
214     ASSERT_EQ(E_OK, pRoutingSender.asyncSetSinkVolume(handle,sinkID,volume,ramp,myTime));
215     pSocketHandler.start_listenting();
216 }
217
218 TEST_F(testRoutingInterfaceAsync,setSinkVolumeAbort)
219 {
220
221     am_Handle_s handle;
222     handle.handle = 1;
223     handle.handleType = H_SETSINKVOLUME;
224
225     am_sinkID_t sinkID = 2;
226     am_volume_t volume = 25;
227     am_RampType_e ramp = RAMP_DIRECT;
228     am_time_t myTime = 25;
229
230     EXPECT_CALL(pReceiveInterface, ackSinkVolumeTick(_,sinkID,_));
231     EXPECT_CALL(pReceiveInterface,ackSetSinkVolumeChange(_,AllOf(Ne(volume),Ne(0)),E_ABORTED)).Times(1);
232
233     ASSERT_EQ(E_OK, pRoutingSender.asyncSetSinkVolume(handle,sinkID,volume,ramp,myTime));
234     sleep(0.5);
235     ASSERT_EQ(E_OK, pRoutingSender.asyncAbort(handle));
236     pSocketHandler.start_listenting();
237 }
238
239 TEST_F(testRoutingInterfaceAsync,disconnectTooEarly)
240 {
241
242     am_Handle_s handle;
243     handle.handle = 1;
244     handle.handleType = H_CONNECT;
245
246     am_connectionID_t connectionID = 4;
247     am_sourceID_t sourceID = 2;
248     am_sinkID_t sinkID = 1;
249     am_ConnectionFormat_e format = CF_ANALOG;
250
251     EXPECT_CALL(pReceiveInterface, ackConnect(_,connectionID,E_OK));
252     EXPECT_CALL(pReceiveInterface,ackDisconnect(_,connectionID,E_OK)).Times(0);
253     ASSERT_EQ(E_OK, pRoutingSender.asyncConnect(handle,connectionID,sourceID,sinkID,format));
254     ASSERT_EQ(E_NON_EXISTENT, pRoutingSender.asyncDisconnect(handle,connectionID));
255     pSocketHandler.start_listenting();
256 }
257
258 TEST_F(testRoutingInterfaceAsync,disconnectAbort)
259 {
260
261     am_Handle_s handle;
262     handle.handle = 1;
263     handle.handleType = H_CONNECT;
264
265     am_connectionID_t connectionID = 4;
266     am_sourceID_t sourceID = 2;
267     am_sinkID_t sinkID = 1;
268     am_ConnectionFormat_e format = CF_ANALOG;
269
270     EXPECT_CALL(pReceiveInterface, ackConnect(_,connectionID,E_OK));
271     EXPECT_CALL(pReceiveInterface, ackDisconnect(_,connectionID,E_ABORTED));
272     ASSERT_EQ(E_OK, pRoutingSender.asyncConnect(handle,connectionID,sourceID,sinkID,format));
273     sleep(2);
274     ASSERT_EQ(E_OK, pRoutingSender.asyncDisconnect(handle,connectionID));
275     ASSERT_EQ(E_OK, pRoutingSender.asyncAbort(handle));
276     pSocketHandler.start_listenting();
277 }
278
279 TEST_F(testRoutingInterfaceAsync,disconnectNonExisting)
280 {
281
282     am_Handle_s handle;
283     handle.handle = 1;
284     handle.handleType = H_CONNECT;
285
286     am_connectionID_t connectionID = 4;
287     am_sourceID_t sourceID = 2;
288     am_sinkID_t sinkID = 1;
289     am_ConnectionFormat_e format = CF_ANALOG;
290
291     EXPECT_CALL(pReceiveInterface,ackConnect(_,connectionID,E_OK)).Times(0);
292     EXPECT_CALL(pReceiveInterface,ackDisconnect(_,connectionID,E_OK)).Times(0);
293     ASSERT_EQ(E_NON_EXISTENT, pRoutingSender.asyncDisconnect(handle,connectionID));
294     pSocketHandler.start_listenting();
295 }
296
297 TEST_F(testRoutingInterfaceAsync,disconnect)
298 {
299
300     am_Handle_s handle;
301     handle.handle = 1;
302     handle.handleType = H_CONNECT;
303
304     am_connectionID_t connectionID = 4;
305     am_sourceID_t sourceID = 2;
306     am_sinkID_t sinkID = 1;
307     am_ConnectionFormat_e format = CF_ANALOG;
308
309     EXPECT_CALL(pReceiveInterface, ackConnect(_,connectionID,E_OK));
310     EXPECT_CALL(pReceiveInterface, ackDisconnect(_,connectionID,E_OK));
311     ASSERT_EQ(E_OK, pRoutingSender.asyncConnect(handle,connectionID,sourceID,sinkID,format));
312     sleep(2);
313     ASSERT_EQ(E_OK, pRoutingSender.asyncDisconnect(handle,connectionID));
314     pSocketHandler.start_listenting();
315 }
316
317 TEST_F(testRoutingInterfaceAsync,connectNoMoreThreads)
318 {
319
320     am_Handle_s handle;
321     handle.handle = 1;
322     handle.handleType = H_CONNECT;
323
324     am_connectionID_t connectionID = 1;
325     am_sourceID_t sourceID = 2;
326     am_sinkID_t sinkID = 1;
327     am_ConnectionFormat_e format = CF_ANALOG;
328
329     EXPECT_CALL(pReceiveInterface,ackConnect(_,_,E_OK)).Times(10);
330     for (int i = 0; i < 10; i++)
331     {
332         handle.handle++;
333         connectionID++;
334         ASSERT_EQ(E_OK, pRoutingSender.asyncConnect(handle,connectionID,sourceID,sinkID,format));
335     }ASSERT_EQ(E_NOT_POSSIBLE, pRoutingSender.asyncConnect(handle,connectionID,sourceID,sinkID,format));
336     pSocketHandler.start_listenting();
337 }
338
339 TEST_F(testRoutingInterfaceAsync,connectAbortTooLate)
340 {
341
342     am_Handle_s handle;
343     handle.handle = 1;
344     handle.handleType = H_CONNECT;
345
346     am_connectionID_t connectionID = 4;
347     am_sourceID_t sourceID = 2;
348     am_sinkID_t sinkID = 1;
349     am_ConnectionFormat_e format = CF_ANALOG;
350
351     EXPECT_CALL(pReceiveInterface,ackConnect(_,connectionID,E_OK)).Times(1);
352     ASSERT_EQ(E_OK, pRoutingSender.asyncConnect(handle,connectionID,sourceID,sinkID,format));
353     sleep(3);
354     ASSERT_EQ(E_NON_EXISTENT, pRoutingSender.asyncAbort(handle));
355     pSocketHandler.start_listenting();
356 }
357
358 TEST_F(testRoutingInterfaceAsync,connectAbort)
359 {
360
361     am_Handle_s handle;
362     handle.handle = 1;
363     handle.handleType = H_CONNECT;
364
365     am_connectionID_t connectionID = 4;
366     am_sourceID_t sourceID = 2;
367     am_sinkID_t sinkID = 1;
368     am_ConnectionFormat_e format = CF_ANALOG;
369
370     EXPECT_CALL(pReceiveInterface,ackConnect(_,connectionID,E_ABORTED)).Times(1);
371     ASSERT_EQ(E_OK, pRoutingSender.asyncConnect(handle,connectionID,sourceID,sinkID,format));
372     sleep(0.5);
373     ASSERT_EQ(E_OK, pRoutingSender.asyncAbort(handle));
374     pSocketHandler.start_listenting();
375 }
376
377 TEST_F(testRoutingInterfaceAsync,connectWrongFormat)
378 {
379
380     am_Handle_s handle;
381     handle.handle = 1;
382     handle.handleType = H_CONNECT;
383
384     am_connectionID_t connectionID = 4;
385     am_sourceID_t sourceID = 2;
386     am_sinkID_t sinkID = 1;
387     am_ConnectionFormat_e format = CF_MONO;
388
389     EXPECT_CALL(pReceiveInterface,ackConnect(_,connectionID,E_OK)).Times(0);
390     ASSERT_EQ(E_WRONG_FORMAT, pRoutingSender.asyncConnect(handle,connectionID,sourceID,sinkID,format));
391     pSocketHandler.start_listenting();
392 }
393
394 TEST_F(testRoutingInterfaceAsync,connectWrongSink)
395 {
396
397     am_Handle_s handle;
398     handle.handle = 1;
399     handle.handleType = H_CONNECT;
400
401     am_connectionID_t connectionID = 4;
402     am_sourceID_t sourceID = 2;
403     am_sinkID_t sinkID = 122;
404     am_ConnectionFormat_e format = CF_ANALOG;
405
406     EXPECT_CALL(pReceiveInterface,ackConnect(_,connectionID,E_OK)).Times(0);
407     ASSERT_EQ(E_NON_EXISTENT, pRoutingSender.asyncConnect(handle,connectionID,sourceID,sinkID,format));
408     pSocketHandler.start_listenting();
409 }
410
411 TEST_F(testRoutingInterfaceAsync,connectWrongSource)
412 {
413     am_Handle_s handle;
414     handle.handle = 1;
415     handle.handleType = H_CONNECT;
416
417     am_connectionID_t connectionID = 4;
418     am_sourceID_t sourceID = 25;
419     am_sinkID_t sinkID = 1;
420     am_ConnectionFormat_e format = CF_ANALOG;
421
422     EXPECT_CALL(pReceiveInterface,ackConnect(_,connectionID,E_OK)).Times(0);
423     ASSERT_EQ(E_NON_EXISTENT, pRoutingSender.asyncConnect(handle,connectionID,sourceID,sinkID,format));
424     pSocketHandler.start_listenting();
425 }
426
427 TEST_F(testRoutingInterfaceAsync,connect)
428 {
429
430     am_Handle_s handle;
431     handle.handle = 1;
432     handle.handleType = H_CONNECT;
433
434     am_connectionID_t connectionID = 4;
435     am_sourceID_t sourceID = 2;
436     am_sinkID_t sinkID = 1;
437     am_ConnectionFormat_e format = CF_ANALOG;
438
439     EXPECT_CALL(pReceiveInterface, ackConnect(_,connectionID,E_OK));
440     ASSERT_EQ(E_OK, pRoutingSender.asyncConnect(handle,connectionID,sourceID,sinkID,format));
441     pSocketHandler.start_listenting();
442 }
443
444 int main(int argc, char **argv)
445 {
446     ::testing::InitGoogleTest(&argc, argv);
447     return RUN_ALL_TESTS();
448 }
449