2a331a6871bf0c813d090f8dffed4a79391ff64e
[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
29 using namespace am;
30 using namespace testing;
31
32
33 DLT_DECLARE_CONTEXT(DLT_CONTEXT)
34
35
36 std::vector<std::string> testRoutingInterfaceAsync::pListRoutingPluginDirs=returnListPlugins();
37 am_domainID_t testRoutingInterfaceAsync::mDomainIDCount=0;
38 RoutingSender testRoutingInterfaceAsync::pRoutingSender=RoutingSender(pListRoutingPluginDirs);
39
40 testRoutingInterfaceAsync::testRoutingInterfaceAsync()
41         :pSocketHandler(),
42          pReceiveInterface(),
43          ptimerCallback(this, &testRoutingInterfaceAsync::timerCallback)
44 {
45 }
46
47 testRoutingInterfaceAsync::~testRoutingInterfaceAsync()
48 {
49 }
50
51 void testRoutingInterfaceAsync::SetUp()
52 {
53         DLT_REGISTER_APP("DPtest","RoutingInterfacetest");
54         DLT_REGISTER_CONTEXT(DLT_CONTEXT,"Main","Main Context");
55         DLT_LOG(DLT_CONTEXT,DLT_LOG_INFO, DLT_STRING("RoutingSendInterface Test started "));
56
57         std::vector<int> domainIDs;
58         domainIDs.push_back(0);
59         domainIDs.push_back(1);
60
61         EXPECT_CALL(pReceiveInterface,getSocketHandler(_)).WillOnce(DoAll(SetArgReferee<0>(&pSocketHandler),Return(E_OK)));
62         EXPECT_CALL(pReceiveInterface,registerDomain(_,_)).WillRepeatedly(Invoke(testRoutingInterfaceAsync::handleDomainRegister));
63         EXPECT_CALL(pReceiveInterface,registerSource(_,_)).WillRepeatedly(Invoke(testRoutingInterfaceAsync::handleSourceRegister));
64         EXPECT_CALL(pReceiveInterface,registerSink(_,_)).WillRepeatedly(Invoke(testRoutingInterfaceAsync::handleSinkRegister));
65
66         pRoutingSender.startupRoutingInterface(&pReceiveInterface);
67         pRoutingSender.routingInterfacesReady();
68
69         timespec t;
70         t.tv_nsec=0;
71         t.tv_sec=4;
72
73         sh_timerHandle_t handle;
74
75         shTimerCallBack *buf=&ptimerCallback;
76         //lets use a timeout so the test will finish
77         pSocketHandler.addTimer(t,buf,handle,(void*)NULL);
78 }
79
80 std::vector<std::string> am::testRoutingInterfaceAsync::returnListPlugins()
81 {
82         std::vector<std::string> list;
83         list.push_back(std::string(DEFAULT_PLUGIN_ROUTING_DIR));
84         return (list);
85 }
86
87 am_Error_e am::testRoutingInterfaceAsync::handleSourceRegister(const am_Source_s & sourceData, am_sourceID_t & sourceID)
88 {
89         sourceID=sourceData.sourceID;
90         pRoutingSender.addSourceLookup(sourceData);
91         return (E_OK);
92 }
93
94 am_Error_e am::testRoutingInterfaceAsync::handleSinkRegister(const am_Sink_s & sinkData, am_sinkID_t & sinkID)
95 {
96         sinkID=sinkData.sinkID;
97         pRoutingSender.addSinkLookup(sinkData);
98         return(E_OK);
99 }
100
101 am_Error_e am::testRoutingInterfaceAsync::handleDomainRegister(const am_Domain_s & domainData, am_domainID_t & domainID)
102 {
103         am_Domain_s domain=domainData;
104         domainID=mDomainIDCount++;
105         domain.domainID=domainID;
106         pRoutingSender.addDomainLookup(domain);
107         return (E_OK);
108 }
109
110 void am::testRoutingInterfaceAsync::timerCallback(sh_timerHandle_t handle, void *userData)
111 {
112         pSocketHandler.stop_listening();
113 }
114
115 void testRoutingInterfaceAsync::TearDown()
116 {
117         DLT_UNREGISTER_CONTEXT(DLT_CONTEXT);
118 }
119
120
121 TEST_F(testRoutingInterfaceAsync,setDomainState)
122 {
123         am_domainID_t domainID=1;
124         am_DomainState_e state=DS_INDEPENDENT_RUNDOWN;
125
126         EXPECT_CALL(pReceiveInterface,hookDomainStateChange(_,DS_INDEPENDENT_RUNDOWN)).Times(1);
127
128         ASSERT_EQ(E_OK,pRoutingSender.setDomainState(domainID,state));
129         pSocketHandler.start_listenting();
130 }
131
132 TEST_F(testRoutingInterfaceAsync,setSourceSoundProperty)
133 {
134
135     am_Handle_s handle;
136         handle.handle=1;
137         handle.handleType=H_SETSOURCESOUNDPROPERTY;
138
139         am_sourceID_t sourceID=3;
140         am_SoundProperty_s property;
141         property.type=SP_MID;
142         property.value=24;
143
144         EXPECT_CALL(pReceiveInterface,ackSetSourceSoundProperty(_,E_OK)).Times(1);
145
146         ASSERT_EQ(E_OK,pRoutingSender.asyncSetSourceSoundProperty(handle,sourceID,property));
147         pSocketHandler.start_listenting();
148 }
149
150 TEST_F(testRoutingInterfaceAsync,setSinkSoundProperty)
151 {
152
153     am_Handle_s handle;
154         handle.handle=1;
155         handle.handleType=H_SETSINKSOUNDPROPERTY;
156
157         am_sinkID_t sinkID=1;
158         am_SoundProperty_s property;
159         property.type=SP_MID;
160         property.value=24;
161
162         EXPECT_CALL(pReceiveInterface,ackSetSinkSoundProperty(_,E_OK)).Times(1);
163
164         ASSERT_EQ(E_OK,pRoutingSender.asyncSetSinkSoundProperty(handle,sinkID,property));
165         pSocketHandler.start_listenting();
166 }
167
168 TEST_F(testRoutingInterfaceAsync,setSourceState)
169 {
170
171     am_Handle_s handle;
172         handle.handle=1;
173         handle.handleType=H_SETSOURCEVOLUME;
174
175         am_sourceID_t sourceID=1;
176         am_SourceState_e state=SS_OFF;
177
178         EXPECT_CALL(pReceiveInterface,ackSetSourceState(_,E_OK)).Times(1);
179
180         ASSERT_EQ(E_OK,pRoutingSender.asyncSetSourceState(handle,sourceID,state));
181         pSocketHandler.start_listenting();
182 }
183
184 TEST_F(testRoutingInterfaceAsync,setSourceVolume)
185 {
186
187     am_Handle_s handle;
188         handle.handle=1;
189         handle.handleType=H_SETSOURCEVOLUME;
190
191         am_sourceID_t sourceID=3;
192         am_volume_t volume=3;
193         am_RampType_e ramp=RAMP_DIRECT;
194         am_time_t myTime=25;
195
196         EXPECT_CALL(pReceiveInterface,ackSourceVolumeTick(_,sourceID,_)).Times(3);
197         EXPECT_CALL(pReceiveInterface,ackSetSourceVolumeChange(_,volume,E_OK)).Times(1);
198
199         ASSERT_EQ(E_OK,pRoutingSender.asyncSetSourceVolume(handle,sourceID,volume,ramp,myTime));
200         pSocketHandler.start_listenting();
201 }
202
203 TEST_F(testRoutingInterfaceAsync,setSinkVolume)
204 {
205
206     am_Handle_s handle;
207         handle.handle=1;
208         handle.handleType=H_SETSINKVOLUME;
209
210         am_sinkID_t sinkID=1;
211         am_volume_t volume=9;
212         am_RampType_e ramp=RAMP_DIRECT;
213         am_time_t myTime=25;
214
215         EXPECT_CALL(pReceiveInterface,ackSinkVolumeTick(_,sinkID,_)).Times(9);
216         EXPECT_CALL(pReceiveInterface,ackSetSinkVolumeChange(_,volume,E_OK)).Times(1);
217
218         ASSERT_EQ(E_OK,pRoutingSender.asyncSetSinkVolume(handle,sinkID,volume,ramp,myTime));
219         pSocketHandler.start_listenting();
220 }
221
222 TEST_F(testRoutingInterfaceAsync,setSinkVolumeAbort)
223 {
224
225     am_Handle_s handle;
226         handle.handle=1;
227         handle.handleType=H_SETSINKVOLUME;
228
229         am_sinkID_t sinkID=2;
230         am_volume_t volume=25;
231         am_RampType_e ramp=RAMP_DIRECT;
232         am_time_t myTime=25;
233
234         EXPECT_CALL(pReceiveInterface,ackSinkVolumeTick(_,sinkID,_));
235         EXPECT_CALL(pReceiveInterface,ackSetSinkVolumeChange(_,AllOf(Ne(volume),Ne(0)),E_ABORTED)).Times(1);
236
237         ASSERT_EQ(E_OK,pRoutingSender.asyncSetSinkVolume(handle,sinkID,volume,ramp,myTime));
238         sleep(0.5);
239         ASSERT_EQ(E_OK,pRoutingSender.asyncAbort(handle));
240         pSocketHandler.start_listenting();
241 }
242
243
244 TEST_F(testRoutingInterfaceAsync,disconnectTooEarly)
245 {
246
247     am_Handle_s handle;
248         handle.handle=1;
249         handle.handleType=H_CONNECT;
250
251         am_connectionID_t connectionID=4;
252         am_sourceID_t sourceID=2;
253         am_sinkID_t sinkID=1;
254         am_ConnectionFormat_e format=CF_ANALOG;
255
256         EXPECT_CALL(pReceiveInterface,ackConnect(_,connectionID,E_OK));
257         EXPECT_CALL(pReceiveInterface,ackDisconnect(_,connectionID,E_OK)).Times(0);
258         ASSERT_EQ(E_OK,pRoutingSender.asyncConnect(handle,connectionID,sourceID,sinkID,format));
259         ASSERT_EQ(E_NON_EXISTENT,pRoutingSender.asyncDisconnect(handle,connectionID));
260         pSocketHandler.start_listenting();
261 }
262
263 TEST_F(testRoutingInterfaceAsync,disconnectAbort)
264 {
265
266     am_Handle_s handle;
267         handle.handle=1;
268         handle.handleType=H_CONNECT;
269
270         am_connectionID_t connectionID=4;
271         am_sourceID_t sourceID=2;
272         am_sinkID_t sinkID=1;
273         am_ConnectionFormat_e format=CF_ANALOG;
274
275         EXPECT_CALL(pReceiveInterface,ackConnect(_,connectionID,E_OK));
276         EXPECT_CALL(pReceiveInterface,ackDisconnect(_,connectionID,E_ABORTED));
277         ASSERT_EQ(E_OK,pRoutingSender.asyncConnect(handle,connectionID,sourceID,sinkID,format));
278         sleep(2);
279         ASSERT_EQ(E_OK,pRoutingSender.asyncDisconnect(handle,connectionID));
280         ASSERT_EQ(E_OK,pRoutingSender.asyncAbort(handle));
281         pSocketHandler.start_listenting();
282 }
283
284 TEST_F(testRoutingInterfaceAsync,disconnectNonExisting)
285 {
286
287     am_Handle_s handle;
288         handle.handle=1;
289         handle.handleType=H_CONNECT;
290
291         am_connectionID_t connectionID=4;
292         am_sourceID_t sourceID=2;
293         am_sinkID_t sinkID=1;
294         am_ConnectionFormat_e format=CF_ANALOG;
295
296         EXPECT_CALL(pReceiveInterface,ackConnect(_,connectionID,E_OK)).Times(0);
297         EXPECT_CALL(pReceiveInterface,ackDisconnect(_,connectionID,E_OK)).Times(0);
298         ASSERT_EQ(E_NON_EXISTENT,pRoutingSender.asyncDisconnect(handle,connectionID));
299         pSocketHandler.start_listenting();
300 }
301
302 TEST_F(testRoutingInterfaceAsync,disconnect)
303 {
304
305     am_Handle_s handle;
306         handle.handle=1;
307         handle.handleType=H_CONNECT;
308
309         am_connectionID_t connectionID=4;
310         am_sourceID_t sourceID=2;
311         am_sinkID_t sinkID=1;
312         am_ConnectionFormat_e format=CF_ANALOG;
313
314         EXPECT_CALL(pReceiveInterface,ackConnect(_,connectionID,E_OK));
315         EXPECT_CALL(pReceiveInterface,ackDisconnect(_,connectionID,E_OK));
316         ASSERT_EQ(E_OK,pRoutingSender.asyncConnect(handle,connectionID,sourceID,sinkID,format));
317         sleep(2);
318         ASSERT_EQ(E_OK,pRoutingSender.asyncDisconnect(handle,connectionID));
319         pSocketHandler.start_listenting();
320 }
321
322 TEST_F(testRoutingInterfaceAsync,connectNoMoreThreads)
323 {
324
325     am_Handle_s handle;
326         handle.handle=1;
327         handle.handleType=H_CONNECT;
328
329         am_connectionID_t connectionID=1;
330         am_sourceID_t sourceID=2;
331         am_sinkID_t sinkID=1;
332         am_ConnectionFormat_e format=CF_ANALOG;
333
334         EXPECT_CALL(pReceiveInterface,ackConnect(_,_,E_OK)).Times(10);
335         for(int i=0;i<10;i++)
336         {
337                 handle.handle++;
338                 connectionID++;
339                 ASSERT_EQ(E_OK,pRoutingSender.asyncConnect(handle,connectionID,sourceID,sinkID,format));
340         }
341         ASSERT_EQ(E_NOT_POSSIBLE,pRoutingSender.asyncConnect(handle,connectionID,sourceID,sinkID,format));
342         pSocketHandler.start_listenting();
343 }
344
345 TEST_F(testRoutingInterfaceAsync,connectAbortTooLate)
346 {
347
348     am_Handle_s handle;
349         handle.handle=1;
350         handle.handleType=H_CONNECT;
351
352         am_connectionID_t connectionID=4;
353         am_sourceID_t sourceID=2;
354         am_sinkID_t sinkID=1;
355         am_ConnectionFormat_e format=CF_ANALOG;
356
357         EXPECT_CALL(pReceiveInterface,ackConnect(_,connectionID,E_OK)).Times(1);
358         ASSERT_EQ(E_OK,pRoutingSender.asyncConnect(handle,connectionID,sourceID,sinkID,format));
359         sleep(3);
360         ASSERT_EQ(E_NON_EXISTENT,pRoutingSender.asyncAbort(handle));
361         pSocketHandler.start_listenting();
362 }
363
364 TEST_F(testRoutingInterfaceAsync,connectAbort)
365 {
366
367     am_Handle_s handle;
368         handle.handle=1;
369         handle.handleType=H_CONNECT;
370
371         am_connectionID_t connectionID=4;
372         am_sourceID_t sourceID=2;
373         am_sinkID_t sinkID=1;
374         am_ConnectionFormat_e format=CF_ANALOG;
375
376         EXPECT_CALL(pReceiveInterface,ackConnect(_,connectionID,E_ABORTED)).Times(1);
377         ASSERT_EQ(E_OK,pRoutingSender.asyncConnect(handle,connectionID,sourceID,sinkID,format));
378         sleep(0.5);
379         ASSERT_EQ(E_OK,pRoutingSender.asyncAbort(handle));
380         pSocketHandler.start_listenting();
381 }
382
383 TEST_F(testRoutingInterfaceAsync,connectWrongFormat)
384 {
385
386     am_Handle_s handle;
387         handle.handle=1;
388         handle.handleType=H_CONNECT;
389
390         am_connectionID_t connectionID=4;
391         am_sourceID_t sourceID=2;
392         am_sinkID_t sinkID=1;
393         am_ConnectionFormat_e format=CF_MONO;
394
395         EXPECT_CALL(pReceiveInterface,ackConnect(_,connectionID,E_OK)).Times(0);
396         ASSERT_EQ(E_WRONG_FORMAT,pRoutingSender.asyncConnect(handle,connectionID,sourceID,sinkID,format));
397         pSocketHandler.start_listenting();
398 }
399
400 TEST_F(testRoutingInterfaceAsync,connectWrongSink)
401 {
402
403     am_Handle_s handle;
404         handle.handle=1;
405         handle.handleType=H_CONNECT;
406
407         am_connectionID_t connectionID=4;
408         am_sourceID_t sourceID=2;
409         am_sinkID_t sinkID=122;
410         am_ConnectionFormat_e format=CF_ANALOG;
411
412         EXPECT_CALL(pReceiveInterface,ackConnect(_,connectionID,E_OK)).Times(0);
413         ASSERT_EQ(E_NON_EXISTENT,pRoutingSender.asyncConnect(handle,connectionID,sourceID,sinkID,format));
414         pSocketHandler.start_listenting();
415 }
416
417 TEST_F(testRoutingInterfaceAsync,connectWrongSource)
418 {
419     am_Handle_s handle;
420         handle.handle=1;
421         handle.handleType=H_CONNECT;
422
423         am_connectionID_t connectionID=4;
424         am_sourceID_t sourceID=25;
425         am_sinkID_t sinkID=1;
426         am_ConnectionFormat_e format=CF_ANALOG;
427
428         EXPECT_CALL(pReceiveInterface,ackConnect(_,connectionID,E_OK)).Times(0);
429         ASSERT_EQ(E_NON_EXISTENT,pRoutingSender.asyncConnect(handle,connectionID,sourceID,sinkID,format));
430         pSocketHandler.start_listenting();
431 }
432
433 TEST_F(testRoutingInterfaceAsync,connect)
434 {
435
436     am_Handle_s handle;
437         handle.handle=1;
438         handle.handleType=H_CONNECT;
439
440         am_connectionID_t connectionID=4;
441         am_sourceID_t sourceID=2;
442         am_sinkID_t sinkID=1;
443         am_ConnectionFormat_e format=CF_ANALOG;
444
445         EXPECT_CALL(pReceiveInterface,ackConnect(_,connectionID,E_OK));
446         ASSERT_EQ(E_OK,pRoutingSender.asyncConnect(handle,connectionID,sourceID,sinkID,format));
447         pSocketHandler.start_listenting();
448 }
449
450 int main(int argc, char **argv)
451 {
452         ::testing::InitGoogleTest(&argc, argv);
453         return RUN_ALL_TESTS();
454 }
455
456