2323
2424import android .view .View ;
2525import android .view .ViewGroup ;
26+ import android .widget .Button ;
27+
28+ import androidx .test .platform .app .InstrumentationRegistry ;
2629
2730import com .smartdevicelink .managers .ISdl ;
31+ import com .smartdevicelink .managers .lifecycle .OnSystemCapabilityListener ;
32+ import com .smartdevicelink .managers .lifecycle .SystemCapabilityManager ;
2833import com .smartdevicelink .proxy .rpc .HapticRect ;
2934import com .smartdevicelink .proxy .rpc .Rectangle ;
3035import com .smartdevicelink .proxy .rpc .SendHapticData ;
4752import java .util .ArrayList ;
4853import java .util .List ;
4954
55+ import static org .mockito .ArgumentMatchers .anyBoolean ;
56+ import static org .mockito .ArgumentMatchers .eq ;
57+ import static org .mockito .ArgumentMatchers .isNull ;
5058import static org .mockito .Mockito .any ;
5159import static org .mockito .Mockito .doAnswer ;
60+ import static org .mockito .Mockito .doReturn ;
5261import static org .mockito .Mockito .mock ;
5362import static org .mockito .Mockito .times ;
5463import static org .mockito .Mockito .verify ;
6069@ RunWith (MockitoJUnitRunner .Strict .class )
6170public class HapticInterfaceManagerTest extends TestCase {
6271 @ Mock
63- private ISdl mockProxy ;
72+ private ISdl interalInterface ;
6473
6574 @ Captor
6675 private ArgumentCaptor <SendHapticData > captor ;
@@ -69,7 +78,7 @@ public class HapticInterfaceManagerTest extends TestCase {
6978
7079 @ Before
7180 public void setUp () throws Exception {
72- hapticMgr = new HapticInterfaceManager (mockProxy );
81+ hapticMgr = new HapticInterfaceManager (interalInterface );
7382 }
7483
7584 @ After
@@ -89,14 +98,14 @@ public void testSetHapticData() throws Exception {
8998 hRect .setRect (rect );
9099 rects .add (hRect );
91100 hapticMgr .setHapticData (rects );
92- verify (mockProxy ). sendRPCRequest (any (SendHapticData .class ));
101+ verify (interalInterface ). sendRPC (any (SendHapticData .class ));
93102 }
94103
95104 @ Test
96105 public void testRefreshHapticData () throws Exception {
97106 View root = createViews ();
98107 hapticMgr .refreshHapticData (root );
99- verify (mockProxy ).sendRPC (captor .capture ());
108+ verify (interalInterface ).sendRPC (captor .capture ());
100109 SendHapticData data = captor .getValue ();
101110 assertNotNull ("SendHapticData RPC" , data );
102111 List <HapticRect > list = data .getHapticRectData ();
@@ -107,11 +116,11 @@ public void testRefreshHapticData() throws Exception {
107116 @ Test
108117 public void testRefreshHapticDataNull () throws Exception {
109118 hapticMgr .refreshHapticData (null );
110- verify (mockProxy ).sendRPC (captor .capture ());
119+ verify (interalInterface ).sendRPC (captor .capture ());
111120 SendHapticData data = captor .getValue ();
112121 assertNotNull ("SendHapticData RPC" , data );
113122 List <HapticRect > list = data .getHapticRectData ();
114- assertNull ("List" , list );
123+ assertTrue ("List" , list . isEmpty () );
115124 }
116125
117126 @ Test
@@ -205,11 +214,11 @@ public void testRefreshWithUserData() throws Exception {
205214 hRect .setRect (rect );
206215 rects .add (hRect );
207216 hapticMgr .setHapticData (rects );
208- verify (mockProxy ). sendRPCRequest (any (SendHapticData .class ));
217+ verify (interalInterface ). sendRPC (any (SendHapticData .class ));
209218
210219 View root = createViews ();
211220 hapticMgr .refreshHapticData (root );
212- verify (mockProxy , times (1 )).sendRPCRequest (any (SendHapticData .class ));
221+ verify (interalInterface , times (1 )).sendRPC (any (SendHapticData .class ));
213222 }
214223
215224 private View createViews () {
@@ -231,55 +240,45 @@ private View createViews() {
231240 when (parent2 .getChildAt (0 )).thenReturn (view );
232241 when (parent2 .getChildAt (1 )).thenReturn (view );
233242
234- when (view .isFocusable ()).then (new Answer <Boolean >() {
235- private int count = 0 ;
236243
237- @ Override
238- public Boolean answer (InvocationOnMock invocation ) throws Throwable {
239- int curCount = count ++;
240- return (curCount == 1 ) || (curCount == 2 ) || (curCount == 3 );
241- }
242- });
243- when (view .isClickable ()).then (new Answer <Boolean >() {
244+ doAnswer (new Answer <Boolean >() {
244245 private int count = 0 ;
245246
246247 @ Override
247248 public Boolean answer (InvocationOnMock invocation ) throws Throwable {
248249 int curCount = count ++;
249- return (curCount == 0 ) || (curCount == 3 );
250+ return (curCount >= 1 ) && (curCount <= 4 );
250251 }
251- });
252+ }). when ( view ). isClickable () ;
252253
253254 return parent1 ;
254255 }
255256
256257
257258 private View createView (final int x , final int y , int w , int h ) {
258- View button = mock (View .class );
259- when (button .isFocusable ()).thenReturn (true );
260- doAnswer (new Answer () {
259+ View button = new Button (InstrumentationRegistry .getInstrumentation ().getContext ()) {
261260 @ Override
262- public int [] answer (InvocationOnMock invocation ) throws Throwable {
263- int [] args = (int []) (invocation .getArguments ()[0 ]);
264- args [0 ] = x ;
265- args [1 ] = y ;
266- return args ;
261+ public void getLocationOnScreen (int [] outLocation ) {
262+ super .getLocationOnScreen (outLocation );
263+ outLocation [0 ] = x ;
264+ outLocation [1 ] = y ;
267265 }
268- }). when ( button ). getLocationOnScreen ( any ( int []. class )) ;
269-
270- when ( button .getWidth ()). thenReturn (w );
271- when ( button .getHeight ()). thenReturn (h );
266+ };
267+ button . setClickable ( true );
268+ button .setRight (w );
269+ button .setBottom (h );
272270 return button ;
273271 }
274272
275273 private void assertViewWithCapability (int x , int y , int w , int h , Rectangle expected , VideoStreamingCapability capability ) {
276- when (mockProxy .getCapability (SystemCapabilityType .VIDEO_STREAMING )).thenReturn (capability );
274+ SystemCapabilityManager systemCapabilityManager = mock (SystemCapabilityManager .class );
275+ when (systemCapabilityManager .getCapability (eq (SystemCapabilityType .VIDEO_STREAMING ), (OnSystemCapabilityListener ) isNull (), anyBoolean ())).thenReturn (capability );
276+ doReturn (systemCapabilityManager ).when (interalInterface ).getSystemCapabilityManager ();
277277
278278 View button = createView (x , y , w , h );
279279
280-
281280 hapticMgr .refreshHapticData (button );
282- verify (mockProxy ).sendRPC (captor .capture ());
281+ verify (interalInterface ).sendRPC (captor .capture ());
283282
284283 SendHapticData data = captor .getValue ();
285284 List <HapticRect > list = data .getHapticRectData ();
0 commit comments