2 * Copyright (c) 2012 BMW
4 * \author Christian Mueller, christian.ei.mueller@bmw.de BMW 2011,2012
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.
15 * For further information see http://www.genivi.org/.
18 #include "CAmRoutingReceiverAsync.h"
20 #include "CAmRoutingReceiver.h"
21 #include "TAmPluginTemplate.h"
22 #include "MockIAmRoutingReceive.h"
23 #include "shared/CAmDltWrapper.h"
27 using namespace testing;
29 static CAmEnvironment* env;
31 am_domainID_t CAmEnvironment::mDomainIDCount = 0;
34 CAmEnvironment::CAmEnvironment() :
37 pReceiveInterface(NULL),
38 ptimerCallback(this, &CAmEnvironment::timerCallback)
40 DefaultValue<am_Error_e>::Set(E_OK); // Sets the default value to be returned.
44 CAmEnvironment::~CAmEnvironment()
48 void CAmEnvironment::SetUp()
50 logInfo("RoutingSendInterface Test started ");
52 pReceiveInterface = new MockIAmRoutingReceive();
54 std::vector<int> domainIDs;
55 domainIDs.push_back(0);
56 domainIDs.push_back(1);
58 EXPECT_CALL(*env->pReceiveInterface,getSocketHandler(_)).WillOnce(DoAll(SetArgReferee<0>(&env->pSocketHandler), Return(E_OK)));
59 EXPECT_CALL(*env->pReceiveInterface,registerDomain(_,_)).WillRepeatedly(Invoke(CAmEnvironment::handleDomainRegister));
60 EXPECT_CALL(*env->pReceiveInterface,registerSource(_,_)).WillRepeatedly(Invoke(CAmEnvironment::handleSourceRegister));
61 EXPECT_CALL(*env->pReceiveInterface,registerSink(_,_)).WillRepeatedly(Invoke(CAmEnvironment::handleSinkRegister));
62 EXPECT_CALL(*env->pReceiveInterface,confirmRoutingReady(_,_)).Times(1);
64 IAmRoutingSend* (*createFunc)();
65 void* tempLibHandle = NULL;
66 std::string libname("../plugins/routing/libPluginRoutingInterfaceAsync.so");
67 createFunc = getCreateFunction<IAmRoutingSend*()>(libname, tempLibHandle);
70 libname = "/usr/lib/audioManager/routing/libPluginRoutingInterfaceAsync.so";
71 createFunc = getCreateFunction<IAmRoutingSend*()>(libname, tempLibHandle);
74 delete pReceiveInterface, pReceiveInterface = NULL;
75 fprintf(stderr, "RoutingSendInterface Test Entry point of RoutingPlugin not found\n");
76 logError("RoutingSendInterface Test Entry point of RoutingPlugin not found");
80 pRouter = createFunc();
84 delete pReceiveInterface, pReceiveInterface = NULL;
85 fprintf(stderr, "RoutingSendInterface Test RoutingPlugin initialization failed. Entry Function not callable\n");
86 logError("RoutingSendInterface Test RoutingPlugin initialization failed. Entry Function not callable");
90 pRouter->startupInterface(env->pReceiveInterface);
91 pRouter->setRoutingReady(10);
94 t.tv_nsec = 500000000;
97 sh_timerHandle_t handle;
99 //lets use a timeout so the test will finish
100 env->pSocketHandler.addTimer(t, &ptimerCallback, handle, (void*) NULL);
101 env->pSocketHandler.start_listenting();
105 void CAmEnvironment::TearDown()
107 if(pReceiveInterface)
108 delete pReceiveInterface, pReceiveInterface = NULL;
110 delete pRouter, pRouter = NULL;
113 std::vector<std::string> CAmEnvironment::returnListPlugins()
115 std::vector<std::string> list;
116 list.push_back(std::string(DEFAULT_PLUGIN_ROUTING_DIR));
120 am_Error_e CAmEnvironment::handleSourceRegister(const am_Source_s & sourceData, am_sourceID_t & sourceID)
122 sourceID = sourceData.sourceID;
126 am_Error_e CAmEnvironment::handleSinkRegister(const am_Sink_s & sinkData, am_sinkID_t & sinkID)
128 sinkID = sinkData.sinkID;
132 am_Error_e CAmEnvironment::handleDomainRegister(const am_Domain_s & domainData, am_domainID_t & domainID)
134 am_Domain_s domain = domainData;
135 domainID = ++mDomainIDCount;
136 domain.domainID = domainID;
140 void CAmEnvironment::timerCallback(sh_timerHandle_t handle, void *userData)
144 env->pSocketHandler.restartTimer(handle);
145 env->pSocketHandler.stop_listening();
149 CAmRoutingReceiverAsync::CAmRoutingReceiverAsync() :
150 ptimerCallback(this, &CAmRoutingReceiverAsync::timerCallback)
154 CAmRoutingReceiverAsync::~CAmRoutingReceiverAsync()
158 void CAmRoutingReceiverAsync::timerCallback(sh_timerHandle_t handle, void *userData)
162 env->pSocketHandler.stop_listening();
165 void CAmRoutingReceiverAsync::SetUp()
171 // sh_timerHandle_t handle;
173 // shTimerCallBack *buf = &ptimerCallback;
174 // //lets use a timeout so the test will finish
175 // env->pSocketHandler.addTimer(t, buf, handle, (void*) NULL);
178 void CAmRoutingReceiverAsync::TearDown()
184 TEST_F(CAmRoutingReceiverAsync,setDomainState)
186 am_domainID_t domainID = 1;
187 am_DomainState_e state = DS_INDEPENDENT_RUNDOWN;
189 EXPECT_CALL(*env->pReceiveInterface,hookDomainStateChange(_,DS_INDEPENDENT_RUNDOWN)).Times(1);
191 ASSERT_EQ(E_OK, env->pRouter->setDomainState(domainID,state));
192 env->pSocketHandler.start_listenting();
196 TEST_F(CAmRoutingReceiverAsync,setSourceSoundProperty)
201 handle.handleType = H_SETSOURCESOUNDPROPERTY;
203 am_sourceID_t sourceID = 3;
204 am_SoundProperty_s property;
205 property.type = SP_GENIVI_MID;
208 EXPECT_CALL(*env->pReceiveInterface,ackSetSourceSoundProperty(_,E_OK)).Times(1);
210 ASSERT_EQ(E_OK, env->pRouter->asyncSetSourceSoundProperty(handle,sourceID,property));
211 env->pSocketHandler.start_listenting();
214 TEST_F(CAmRoutingReceiverAsync,setSinkSoundProperty)
219 handle.handleType = H_SETSINKSOUNDPROPERTY;
221 am_sinkID_t sinkID = 1;
222 am_SoundProperty_s property;
223 property.type = SP_GENIVI_MID;
226 EXPECT_CALL(*env->pReceiveInterface,ackSetSinkSoundProperty(_,E_OK)).Times(1);
228 ASSERT_EQ(E_OK, env->pRouter->asyncSetSinkSoundProperty(handle,sinkID,property));
229 env->pSocketHandler.start_listenting();
232 TEST_F(CAmRoutingReceiverAsync,setSourceState)
237 handle.handleType = H_SETSOURCESTATE;
239 am_sourceID_t sourceID = 1;
240 am_SourceState_e state = SS_OFF;
242 EXPECT_CALL(*env->pReceiveInterface,ackSetSourceState(_,E_OK)).Times(1);
244 ASSERT_EQ(E_OK, env->pRouter->asyncSetSourceState(handle,sourceID,state));
245 env->pSocketHandler.start_listenting();
248 TEST_F(CAmRoutingReceiverAsync,setSourceVolume)
253 handle.handleType = H_SETSOURCEVOLUME;
255 am_sourceID_t sourceID = 3;
256 am_volume_t volume = 3;
257 am_CustomRampType_t ramp = RAMP_GENIVI_DIRECT;
258 am_time_t myTime = 25;
260 EXPECT_CALL(*env->pReceiveInterface,ackSourceVolumeTick(_,sourceID,_)).Times(AtLeast(1));
261 EXPECT_CALL(*env->pReceiveInterface,ackSetSourceVolumeChange(_,volume,E_OK)).Times(1);
263 ASSERT_EQ(E_OK, env->pRouter->asyncSetSourceVolume(handle,sourceID,volume,ramp,myTime));
264 env->pSocketHandler.start_listenting();
267 TEST_F(CAmRoutingReceiverAsync,setSinkVolume)
272 handle.handleType = H_SETSINKVOLUME;
274 am_sinkID_t sinkID = 1;
275 am_volume_t volume = 9;
276 am_CustomRampType_t ramp = RAMP_GENIVI_DIRECT;
277 am_time_t myTime = 25;
279 EXPECT_CALL(*env->pReceiveInterface,ackSinkVolumeTick(_,sinkID,_)).Times(AtLeast(2));
280 EXPECT_CALL(*env->pReceiveInterface,ackSetSinkVolumeChange(_,volume,E_OK)).Times(1);
282 ASSERT_EQ(E_OK, env->pRouter->asyncSetSinkVolume(handle,sinkID,volume,ramp,myTime));
283 env->pSocketHandler.start_listenting();
286 TEST_F(CAmRoutingReceiverAsync,setSinkVolumeAbort)
291 handle.handleType = H_SETSINKVOLUME;
293 am_sinkID_t sinkID = 2;
294 am_volume_t volume = 25;
295 am_CustomRampType_t ramp = RAMP_GENIVI_DIRECT;
296 am_time_t myTime = 25;
298 EXPECT_CALL(*env->pReceiveInterface, ackSinkVolumeTick(_,sinkID,_));
299 EXPECT_CALL(*env->pReceiveInterface,ackSetSinkVolumeChange(_,AllOf(Ne(volume),Ne(0)),E_ABORTED)).Times(1);
301 ASSERT_EQ(E_OK, env->pRouter->asyncSetSinkVolume(handle,sinkID,volume,ramp,myTime));
303 ASSERT_EQ(E_OK, env->pRouter->asyncAbort(handle));
304 env->pSocketHandler.start_listenting();
307 TEST_F(CAmRoutingReceiverAsync,disconnectNonExisting)
312 handle.handleType = H_DISCONNECT;
314 am_connectionID_t connectionID = 4;
316 EXPECT_CALL(*env->pReceiveInterface,ackConnect(_,connectionID,E_OK)).Times(0);
317 EXPECT_CALL(*env->pReceiveInterface,ackDisconnect(_,connectionID,E_OK)).Times(0);
318 ASSERT_EQ(E_NON_EXISTENT, env->pRouter->asyncDisconnect(handle,connectionID));
319 env->pSocketHandler.start_listenting();
322 TEST_F(CAmRoutingReceiverAsync,disconnectTooEarly)
325 am_Handle_s handle_c;
327 handle_c.handleType = H_CONNECT;
331 handle.handleType = H_DISCONNECT;
333 am_connectionID_t connectionID = 4;
334 am_sourceID_t sourceID = 2;
335 am_sinkID_t sinkID = 1;
336 am_CustomConnectionFormat_t format = CF_GENIVI_ANALOG;
338 EXPECT_CALL(*env->pReceiveInterface, ackConnect(_,connectionID,E_OK));
339 EXPECT_CALL(*env->pReceiveInterface,ackDisconnect(_,connectionID,E_OK)).Times(0);
340 ASSERT_EQ(E_OK, env->pRouter->asyncConnect(handle_c,connectionID,sourceID,sinkID,format));
341 ASSERT_EQ(E_NON_EXISTENT, env->pRouter->asyncDisconnect(handle,connectionID));
342 env->pSocketHandler.start_listenting();
345 TEST_F(CAmRoutingReceiverAsync,disconnectAbort)
348 am_Handle_s handle_c;
350 handle_c.handleType = H_CONNECT;
354 handle.handleType = H_DISCONNECT;
356 am_connectionID_t connectionID = 5;
357 am_sourceID_t sourceID = 2;
358 am_sinkID_t sinkID = 1;
359 am_CustomConnectionFormat_t format = CF_GENIVI_ANALOG;
361 EXPECT_CALL(*env->pReceiveInterface, ackConnect(_,connectionID,E_OK));
362 EXPECT_CALL(*env->pReceiveInterface, ackDisconnect(_,connectionID,E_ABORTED));
363 ASSERT_EQ(E_OK, env->pRouter->asyncConnect(handle_c,connectionID,sourceID,sinkID,format));
365 ASSERT_EQ(E_OK, env->pRouter->asyncDisconnect(handle,connectionID));
366 ASSERT_EQ(E_OK, env->pRouter->asyncAbort(handle));
367 env->pSocketHandler.start_listenting();
370 TEST_F(CAmRoutingReceiverAsync,disconnect)
373 am_Handle_s handle_c;
375 handle_c.handleType = H_CONNECT;
379 handle.handleType = H_DISCONNECT;
381 am_connectionID_t connectionID = 4;
382 am_sourceID_t sourceID = 2;
383 am_sinkID_t sinkID = 1;
384 am_CustomConnectionFormat_t format = CF_GENIVI_ANALOG;
386 EXPECT_CALL(*env->pReceiveInterface, ackConnect(_,connectionID,E_OK));
387 EXPECT_CALL(*env->pReceiveInterface, ackDisconnect(_,connectionID,E_OK));
388 ASSERT_EQ(E_OK, env->pRouter->asyncConnect(handle_c,connectionID,sourceID,sinkID,format));
390 ASSERT_EQ(E_OK, env->pRouter->asyncDisconnect(handle,connectionID));
391 env->pSocketHandler.start_listenting();
394 TEST_F(CAmRoutingReceiverAsync,connectAbortTooLate)
399 handle.handleType = H_CONNECT;
401 am_connectionID_t connectionID = 4;
402 am_sourceID_t sourceID = 2;
403 am_sinkID_t sinkID = 1;
404 am_CustomConnectionFormat_t format = CF_GENIVI_ANALOG;
406 EXPECT_CALL(*env->pReceiveInterface,ackConnect(_,connectionID,E_OK)).Times(1);
407 ASSERT_EQ(E_OK, env->pRouter->asyncConnect(handle,connectionID,sourceID,sinkID,format));
409 ASSERT_EQ(E_NON_EXISTENT, env->pRouter->asyncAbort(handle));
410 env->pSocketHandler.start_listenting();
413 TEST_F(CAmRoutingReceiverAsync,connectAbort)
418 handle.handleType = H_CONNECT;
420 am_connectionID_t connectionID = 4;
421 am_sourceID_t sourceID = 2;
422 am_sinkID_t sinkID = 1;
423 am_CustomConnectionFormat_t format = CF_GENIVI_ANALOG;
425 EXPECT_CALL(*env->pReceiveInterface,ackConnect(_,connectionID,E_ABORTED)).Times(1);
426 ASSERT_EQ(E_OK, env->pRouter->asyncConnect(handle,connectionID,sourceID,sinkID,format));
428 ASSERT_EQ(E_OK, env->pRouter->asyncAbort(handle));
429 env->pSocketHandler.start_listenting();
432 TEST_F(CAmRoutingReceiverAsync,connectWrongFormat)
437 handle.handleType = H_CONNECT;
439 am_connectionID_t connectionID = 4;
440 am_sourceID_t sourceID = 2;
441 am_sinkID_t sinkID = 1;
442 am_CustomConnectionFormat_t format = CF_GENIVI_MONO;
444 EXPECT_CALL(*env->pReceiveInterface,ackConnect(_,connectionID,E_OK)).Times(0);
445 ASSERT_EQ(E_WRONG_FORMAT, env->pRouter->asyncConnect(handle,connectionID,sourceID,sinkID,format));
446 env->pSocketHandler.start_listenting();
449 TEST_F(CAmRoutingReceiverAsync,connectWrongSink)
454 handle.handleType = H_CONNECT;
456 am_connectionID_t connectionID = 4;
457 am_sourceID_t sourceID = 2;
458 am_sinkID_t sinkID = 122;
459 am_CustomConnectionFormat_t format = CF_GENIVI_ANALOG;
461 EXPECT_CALL(*env->pReceiveInterface,ackConnect(_,connectionID,E_OK)).Times(0);
462 ASSERT_EQ(E_NON_EXISTENT, env->pRouter->asyncConnect(handle,connectionID,sourceID,sinkID,format));
463 env->pSocketHandler.start_listenting();
466 TEST_F(CAmRoutingReceiverAsync,connectWrongSource)
470 handle.handleType = H_CONNECT;
472 am_connectionID_t connectionID = 4;
473 am_sourceID_t sourceID = 25;
474 am_sinkID_t sinkID = 1;
475 am_CustomConnectionFormat_t format = CF_GENIVI_ANALOG;
477 EXPECT_CALL(*env->pReceiveInterface,ackConnect(_,connectionID,E_OK)).Times(0);
478 ASSERT_EQ(E_NON_EXISTENT, env->pRouter->asyncConnect(handle,connectionID,sourceID,sinkID,format));
479 env->pSocketHandler.start_listenting();
482 TEST_F(CAmRoutingReceiverAsync,connect)
487 handle.handleType = H_CONNECT;
489 am_connectionID_t connectionID = 4;
490 am_sourceID_t sourceID = 2;
491 am_sinkID_t sinkID = 1;
492 am_CustomConnectionFormat_t format = CF_GENIVI_ANALOG;
494 EXPECT_CALL(*env->pReceiveInterface, ackConnect(_,connectionID,E_OK));
495 ASSERT_EQ(E_OK, env->pRouter->asyncConnect(handle,connectionID,sourceID,sinkID,format));
496 env->pSocketHandler.start_listenting();
499 TEST_F(CAmRoutingReceiverAsync,connectNoMoreThreads)
504 handle.handleType = H_CONNECT;
506 am_connectionID_t connectionID = 1;
507 am_sourceID_t sourceID = 2;
508 am_sinkID_t sinkID = 1;
509 am_CustomConnectionFormat_t format = CF_GENIVI_ANALOG;
511 EXPECT_CALL(*env->pReceiveInterface,ackConnect(_,_,E_OK)).Times(10);
512 for (int i = 0; i < 10; i++)
516 ASSERT_EQ(E_OK, env->pRouter->asyncConnect(handle,connectionID,sourceID,sinkID,format));
518 ASSERT_EQ(E_NOT_POSSIBLE, env->pRouter->asyncConnect(handle,connectionID,sourceID,sinkID,format));
519 env->pSocketHandler.start_listenting();
522 int main(int argc, char **argv)
524 ::testing::InitGoogleTest(&argc, argv);
525 CAmEnvironment* const env = (CAmEnvironment*)::testing::AddGlobalTestEnvironment(new CAmEnvironment);
527 return RUN_ALL_TESTS();