summaryrefslogtreecommitdiff
path: root/android/sdl_android/src/androidTest/java/com/smartdevicelink/managers/video/VideoStreamManagerTests.java
blob: 76bce2e7a1eae6ab80b1eaca13e0cf282cf7be74 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
package com.smartdevicelink.managers.video;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.Display;
import android.view.MotionEvent;
import android.view.View;

import com.smartdevicelink.AndroidTestCase2;
import com.smartdevicelink.managers.CompletionListener;
import com.smartdevicelink.protocol.enums.FunctionID;
import com.smartdevicelink.protocol.enums.SessionType;
import com.smartdevicelink.proxy.interfaces.ISdl;
import com.smartdevicelink.proxy.interfaces.ISdlServiceListener;
import com.smartdevicelink.proxy.interfaces.IVideoStreamListener;
import com.smartdevicelink.proxy.interfaces.OnSystemCapabilityListener;
import com.smartdevicelink.proxy.rpc.OnHMIStatus;
import com.smartdevicelink.proxy.rpc.OnTouchEvent;
import com.smartdevicelink.proxy.rpc.TouchCoord;
import com.smartdevicelink.proxy.rpc.TouchEvent;
import com.smartdevicelink.proxy.rpc.enums.HMILevel;
import com.smartdevicelink.proxy.rpc.enums.SystemCapabilityType;
import com.smartdevicelink.proxy.rpc.enums.TouchType;
import com.smartdevicelink.proxy.rpc.listeners.OnRPCNotificationListener;
import com.smartdevicelink.streaming.video.SdlRemoteDisplay;
import com.smartdevicelink.streaming.video.VideoStreamingParameters;
import com.smartdevicelink.test.Test;
import com.smartdevicelink.util.Version;

import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;

import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

/**
 * This is a unit test class for the SmartDeviceLink video streaming manager class :
 * {@link VideoStreamManager}
 */
public class VideoStreamManagerTests extends AndroidTestCase2 {
	public static final String TAG = "VideoStreamManagerTests";
	private Context mTestContext;
	private static boolean touchEventOccured = false;

	// SETUP / HELPERS

	@Override
	public void setUp() throws Exception{
		super.setUp();
		mTestContext = this.getContext();
	}

	@Override
	public void tearDown() throws Exception {
		super.tearDown();
	}

	// TEST CLASSES

	public static class TestPresentation extends SdlRemoteDisplay {
		View simulatedView = new View(this.getContext());

		public TestPresentation(Context context, Display display) {
			super(context, display);
		}

		@Override
		protected void onCreate(Bundle savedInstanceState) {
			super.onCreate(savedInstanceState);
			setContentView(simulatedView);
		}

		@Override
		public boolean onTouchEvent(@NonNull MotionEvent event) {
			touchEventOccured = true;
			return super.onTouchEvent(event);
		}
	}

	// TESTS

	public void testInitialization(){
		ISdl internalInterface = mock(ISdl.class);
		when(internalInterface.getProtocolVersion()).thenReturn(new Version(5,1,0));

		Answer<Void> onAddServiceListener = new Answer<Void>() {
			@Override
			public Void answer(InvocationOnMock invocation) {
				Object[] args = invocation.getArguments();
				SessionType sessionType = (SessionType) args[0];
				ISdlServiceListener sdlServiceListener = (ISdlServiceListener) args[1];
				assertEquals(sessionType, SessionType.NAV);
				assertNotNull(sdlServiceListener);
				return null;
			}
		};

		doAnswer(onAddServiceListener).when(internalInterface).addServiceListener(any(SessionType.class), any(ISdlServiceListener.class));

		VideoStreamManager videoStreamManager = new VideoStreamManager(internalInterface);
		videoStreamManager.start(new CompletionListener() {
			@Override
			public void onComplete(boolean success) {
				assertTrue(success);
			}
		});
	}

	public void testHMILevelNotFull(){
		final ISdl internalInterface = mock(ISdl.class);

		when(internalInterface.getProtocolVersion()).thenReturn((new Version(5,0,0)));
		when(internalInterface.isCapabilitySupported(SystemCapabilityType.VIDEO_STREAMING)).thenReturn(true);

		final VideoStreamManager videoStreamManager = new VideoStreamManager(internalInterface);
		videoStreamManager.start(new CompletionListener() {
			@Override
			public void onComplete(boolean success) {
				VideoStreamingParameters params = new VideoStreamingParameters();
				boolean encrypted = false;
				videoStreamManager.startStreaming(params, encrypted);
				verify(internalInterface, times(0)).startVideoService(params, encrypted);
			}
		});
	}

	public void testRemoteDisplayStream(){
		ISdl internalInterface = mock(ISdl.class);

		final Set<Object> listenerSet = new HashSet<>();

		when(internalInterface.getProtocolVersion()).thenReturn(new Version(5,0,0));
		when(internalInterface.isCapabilitySupported(SystemCapabilityType.VIDEO_STREAMING)).thenReturn(true);

		Answer<Void> onGetCapability = new Answer<Void>() {
			@Override
			public Void answer(InvocationOnMock invocation) {
				Object[] args = invocation.getArguments();
				OnSystemCapabilityListener systemCapabilityListener = (OnSystemCapabilityListener) args[1];
				systemCapabilityListener.onCapabilityRetrieved(Test.GENERAL_VIDEOSTREAMINGCAPABILITY);
				return null;
			}
		};

		doAnswer(onGetCapability).when(internalInterface).getCapability(eq(SystemCapabilityType.VIDEO_STREAMING), any(OnSystemCapabilityListener.class));

		Answer<Void> onAddServiceListener = new Answer<Void>() {
			@Override
			public Void answer(InvocationOnMock invocation) {
				Object[] args = invocation.getArguments();
				listenerSet.add(args[1]);
				return null;
			}
		};

		doAnswer(onAddServiceListener).when(internalInterface).addServiceListener(eq(SessionType.NAV), any(ISdlServiceListener.class));

		final OnRPCNotificationListener[] hmiListener = {null};

		Answer<Void> onAddHMIListener = new Answer<Void>() {
			@Override
			public Void answer(InvocationOnMock invocation) {
				Object[] args = invocation.getArguments();
				hmiListener[0] = (OnRPCNotificationListener) args[1];
				listenerSet.add(args[1]);
				return null;
			}
		};

		doAnswer(onAddHMIListener).when(internalInterface).addOnRPCNotificationListener(eq(FunctionID.ON_HMI_STATUS), any(OnRPCNotificationListener.class));

		Answer<Void> onAddTouchListener = new Answer<Void>() {
			@Override
			public Void answer(InvocationOnMock invocation) {
				Object[] args = invocation.getArguments();
				listenerSet.add(args[1]);
				return null;
			}
		};

		doAnswer(onAddTouchListener).when(internalInterface).addOnRPCNotificationListener(eq(FunctionID.ON_TOUCH_EVENT), any(OnRPCNotificationListener.class));

		Answer<Void> onRemoveRPCNotificationListener = new Answer<Void>() {
			@Override
			public Void answer(InvocationOnMock invocation) {
				Object[] args = invocation.getArguments();
				listenerSet.remove(args[1]);
				return null;
			}
		};

		doAnswer(onRemoveRPCNotificationListener).when(internalInterface).removeOnRPCNotificationListener(eq(FunctionID.ON_HMI_STATUS), any(OnRPCNotificationListener.class));
		doAnswer(onRemoveRPCNotificationListener).when(internalInterface).removeOnRPCNotificationListener(eq(FunctionID.ON_TOUCH_EVENT), any(OnRPCNotificationListener.class));

		Answer<Void> onRemoveServiceListener = new Answer<Void>() {
			@Override
			public Void answer(InvocationOnMock invocation) {
				Object[] args = invocation.getArguments();
				listenerSet.remove(args[1]);
				return null;
			}
		};

		doAnswer(onRemoveServiceListener).when(internalInterface).removeServiceListener(eq(SessionType.NAV), any(ISdlServiceListener.class));

		when(internalInterface.startVideoStream(anyBoolean(), any(VideoStreamingParameters.class))).thenReturn(new IVideoStreamListener() {
			@Override
			public void sendFrame(byte[] data, int offset, int length, long presentationTimeUs) throws ArrayIndexOutOfBoundsException {}
			@Override
			public void sendFrame(ByteBuffer data, long presentationTimeUs) {}
		});

		when(internalInterface.getCapability(SystemCapabilityType.VIDEO_STREAMING)).thenReturn(Test.GENERAL_VIDEOSTREAMINGCAPABILITY);

		final VideoStreamManager videoStreamManager = new VideoStreamManager(internalInterface);
		videoStreamManager.start(new CompletionListener() {
			@Override
			public void onComplete(boolean success) {
				assertTrue(success);
				assertTrue(listenerSet.size() == 3);

				OnHMIStatus fullNotification = new OnHMIStatus();
				fullNotification.setHmiLevel(HMILevel.HMI_FULL);
				hmiListener[0].onNotified(fullNotification);

				videoStreamManager.startRemoteDisplayStream(mTestContext, TestPresentation.class, null, false);

				//assertTrue(touchEventOccured);

				videoStreamManager.dispose();
				assertTrue(listenerSet.isEmpty());
			}
		});

	}

	public void testConvertTouchEvent() {
		ISdl internalInterface = mock(ISdl.class);
		VideoStreamManager videoStreamManager = new VideoStreamManager(internalInterface);
		List<MotionEvent> motionEventList;
		long e1TS = 1558124390L, e2TS = 1558125390L, e3TS = 1558126390L;
		int e1x = 50, e1y = 100, e2x = 150, e2y = 200, e3x = 250, e3y = 300;
		int e1Id = 100, e2Id = 101, e3Id = 102;
		int movingStep = 10;
		OnTouchEvent testOnTouchEvent;
		MotionEvent motionEvent;
		TouchEvent touchEvent1 = new TouchEvent(e1Id, Collections.singletonList(e1TS), Collections.singletonList(new TouchCoord(e1x, e1y)));
		TouchEvent touchEvent2 = new TouchEvent(e2Id, Collections.singletonList(e2TS), Collections.singletonList(new TouchCoord(e2x, e2y)));
		TouchEvent touchEvent2AfterMovingPointer = new TouchEvent(e2Id, Collections.singletonList(e2TS), Collections.singletonList(new TouchCoord(e2x + movingStep, e2y + movingStep)));
		TouchEvent touchEvent3 = new TouchEvent(e3Id, Collections.singletonList(e3TS), Collections.singletonList(new TouchCoord(e3x, e3y)));



		/////////////////////////////////////////////////// First OnTouchEvent Notification ///////////////////////////////////////////////////
		testOnTouchEvent = new OnTouchEvent(TouchType.BEGIN, Arrays.asList(touchEvent1, touchEvent2));
		motionEventList = videoStreamManager.convertTouchEvent(testOnTouchEvent);


		// First MotionEvent should be ACTION_DOWN and have 1 pointer
		motionEvent = motionEventList.get(0);
		assertEquals(1, motionEvent.getPointerCount());
		assertEquals(e1x, Math.round(motionEvent.getX(0)));
		assertEquals(e1y, Math.round(motionEvent.getY(0)));
		assertEquals(MotionEvent.ACTION_DOWN, motionEvent.getActionMasked());


		// Second MotionEvent should be ACTION_POINTER_DOWN and have 2 pointers
		motionEvent = motionEventList.get(1);
		assertEquals(2, motionEvent.getPointerCount());
		assertEquals(e1x, Math.round(motionEvent.getX(0)));
		assertEquals(e1y, Math.round(motionEvent.getY(0)));
		assertEquals(e2x, Math.round(motionEvent.getX(1)));
		assertEquals(e2y, Math.round(motionEvent.getY(1)));
		assertEquals(MotionEvent.ACTION_POINTER_DOWN, motionEvent.getActionMasked());
		////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////



		/////////////////////////////////////////////////// Second OnTouchEvent Notification ///////////////////////////////////////////////////
		testOnTouchEvent = new OnTouchEvent(TouchType.BEGIN, Arrays.asList(touchEvent3));
		motionEventList = videoStreamManager.convertTouchEvent(testOnTouchEvent);


		// First MotionEvent should be ACTION_POINTER_DOWN and have 3 pointers
		motionEvent = motionEventList.get(0);
		assertEquals(3, motionEvent.getPointerCount());
		assertEquals(e1x, Math.round(motionEvent.getX(0)));
		assertEquals(e1y, Math.round(motionEvent.getY(0)));
		assertEquals(e2x, Math.round(motionEvent.getX(1)));
		assertEquals(e2y, Math.round(motionEvent.getY(1)));
		assertEquals(e3x, Math.round(motionEvent.getX(2)));
		assertEquals(e3y, Math.round(motionEvent.getY(2)));
		assertEquals(MotionEvent.ACTION_POINTER_DOWN, motionEvent.getActionMasked());
		////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////



		/////////////////////////////////////////////////// Third OnTouchEvent Notification ///////////////////////////////////////////////////
		testOnTouchEvent = new OnTouchEvent(TouchType.MOVE, Arrays.asList(touchEvent2AfterMovingPointer));
		motionEventList = videoStreamManager.convertTouchEvent(testOnTouchEvent);


		// First MotionEvent should be ACTION_MOVE and have 3 pointers
		motionEvent = motionEventList.get(0);
		assertEquals(3, motionEvent.getPointerCount());
		assertEquals(e1x, Math.round(motionEvent.getX(0)));
		assertEquals(e1y, Math.round(motionEvent.getY(0)));
		assertEquals(e2x + movingStep, Math.round(motionEvent.getX(1)));
		assertEquals(e2y + movingStep, Math.round(motionEvent.getY(1)));
		assertEquals(e3x, Math.round(motionEvent.getX(2)));
		assertEquals(e3y, Math.round(motionEvent.getY(2)));
		assertEquals(MotionEvent.ACTION_MOVE, motionEvent.getActionMasked());
		////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////



		/////////////////////////////////////////////////// Fourth OnTouchEvent Notification ///////////////////////////////////////////////////
		testOnTouchEvent = new OnTouchEvent(TouchType.END, Arrays.asList(touchEvent2AfterMovingPointer));
		motionEventList = videoStreamManager.convertTouchEvent(testOnTouchEvent);


		// First MotionEvent should be ACTION_POINTER_UP and have 3 pointers
		motionEvent = motionEventList.get(0);
		assertEquals(3, motionEvent.getPointerCount());
		assertEquals(e1x, Math.round(motionEvent.getX(0)));
		assertEquals(e1y, Math.round(motionEvent.getY(0)));
		assertEquals(e2x + movingStep, Math.round(motionEvent.getX(1)));
		assertEquals(e2y + movingStep, Math.round(motionEvent.getY(1)));
		assertEquals(e3x, Math.round(motionEvent.getX(2)));
		assertEquals(e3y, Math.round(motionEvent.getY(2)));
		assertEquals(MotionEvent.ACTION_POINTER_UP, motionEvent.getActionMasked());
		////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////



		/////////////////////////////////////////////////// Fifth OnTouchEvent Notification ///////////////////////////////////////////////////
		testOnTouchEvent = new OnTouchEvent(TouchType.END, Arrays.asList(touchEvent3));
		motionEventList = videoStreamManager.convertTouchEvent(testOnTouchEvent);


		// First MotionEvent should be ACTION_POINTER_UP and have 2 pointers
		motionEvent = motionEventList.get(0);
		assertEquals(2, motionEvent.getPointerCount());
		assertEquals(e1x, Math.round(motionEvent.getX(0)));
		assertEquals(e1y, Math.round(motionEvent.getY(0)));
		assertEquals(e3x, Math.round(motionEvent.getX(1)));
		assertEquals(e3y, Math.round(motionEvent.getY(1)));
		assertEquals(MotionEvent.ACTION_POINTER_UP, motionEvent.getActionMasked());
		////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////



		/////////////////////////////////////////////////// Sixth OnTouchEvent Notification ///////////////////////////////////////////////////
		testOnTouchEvent = new OnTouchEvent(TouchType.END, Arrays.asList(touchEvent3));
		motionEventList = videoStreamManager.convertTouchEvent(testOnTouchEvent);


		// First MotionEvent should be ACTION_UP and have 1 pointer
		motionEvent = motionEventList.get(0);
		assertEquals(1, motionEvent.getPointerCount());
		assertEquals(e1x, Math.round(motionEvent.getX(0)));
		assertEquals(e1y, Math.round(motionEvent.getY(0)));
		assertEquals(MotionEvent.ACTION_UP, motionEvent.getActionMasked());
		////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////



		/////////////////////////////////////////////////// Seventh OnTouchEvent Notification ///////////////////////////////////////////////////
		testOnTouchEvent = new OnTouchEvent(TouchType.BEGIN, Arrays.asList(touchEvent1, touchEvent2));
		motionEventList = videoStreamManager.convertTouchEvent(testOnTouchEvent);


		// First MotionEvent should be ACTION_DOWN and have 1 pointer
		motionEvent = motionEventList.get(0);
		assertEquals(1, motionEvent.getPointerCount());
		assertEquals(e1x, Math.round(motionEvent.getX(0)));
		assertEquals(e1y, Math.round(motionEvent.getY(0)));
		assertEquals(MotionEvent.ACTION_DOWN, motionEvent.getActionMasked());


		// Second MotionEvent should be ACTION_POINTER_DOWN and have 2 pointers
		motionEvent = motionEventList.get(1);
		assertEquals(2, motionEvent.getPointerCount());
		assertEquals(e1x, Math.round(motionEvent.getX(0)));
		assertEquals(e1y, Math.round(motionEvent.getY(0)));
		assertEquals(e2x, Math.round(motionEvent.getX(1)));
		assertEquals(e2y, Math.round(motionEvent.getY(1)));
		assertEquals(MotionEvent.ACTION_POINTER_DOWN, motionEvent.getActionMasked());
		////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////



		/////////////////////////////////////////////////// Eighth OnTouchEvent Notification ///////////////////////////////////////////////////
		testOnTouchEvent = new OnTouchEvent(TouchType.CANCEL, Arrays.asList(touchEvent3));
		motionEventList = videoStreamManager.convertTouchEvent(testOnTouchEvent);


		// First MotionEvent should be ACTION_CANCEL and have 2 pointers
		motionEvent = motionEventList.get(0);
		assertEquals(2, motionEvent.getPointerCount());
		assertEquals(e1x, Math.round(motionEvent.getX(0)));
		assertEquals(e1y, Math.round(motionEvent.getY(0)));
		assertEquals(e2x, Math.round(motionEvent.getX(1)));
		assertEquals(e2y, Math.round(motionEvent.getY(1)));
		assertEquals(MotionEvent.ACTION_CANCEL, motionEvent.getActionMasked());
		////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////



		/////////////////////////////////////////////////// Ninth OnTouchEvent Notification ///////////////////////////////////////////////////
		testOnTouchEvent = new OnTouchEvent(TouchType.BEGIN, Arrays.asList(touchEvent1));
		motionEventList = videoStreamManager.convertTouchEvent(testOnTouchEvent);


		// First MotionEvent should be ACTION_DOWN and have 1 pointer
		motionEvent = motionEventList.get(0);
		assertEquals(1, motionEvent.getPointerCount());
		assertEquals(e1x, Math.round(motionEvent.getX(0)));
		assertEquals(e1y, Math.round(motionEvent.getY(0)));
		assertEquals(MotionEvent.ACTION_DOWN, motionEvent.getActionMasked());
		////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////



		/////////////////////////////////////////////////// Tenth OnTouchEvent Notification ///////////////////////////////////////////////////
		testOnTouchEvent = new OnTouchEvent(TouchType.END, Arrays.asList(touchEvent1));
		motionEventList = videoStreamManager.convertTouchEvent(testOnTouchEvent);


		// First MotionEvent should be ACTION_UP and have 1 pointer
		motionEvent = motionEventList.get(0);
		assertEquals(1, motionEvent.getPointerCount());
		assertEquals(e1x, Math.round(motionEvent.getX(0)));
		assertEquals(e1y, Math.round(motionEvent.getY(0)));
		assertEquals(MotionEvent.ACTION_UP, motionEvent.getActionMasked());
		////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	}
}