11package com .smartdevicelink .managers .screen ;
22
33
4+ import com .livio .taskmaster .Taskmaster ;
45import com .smartdevicelink .AndroidTestCase2 ;
5- import com .smartdevicelink .managers .CompletionListener ;
66import com .smartdevicelink .managers .file .FileManager ;
77import com .smartdevicelink .managers .file .MultipleFileCompletionListener ;
88import com .smartdevicelink .managers .file .filetypes .SdlArtwork ;
99import com .smartdevicelink .protocol .enums .FunctionID ;
1010import com .smartdevicelink .proxy .interfaces .ISdl ;
11+ import com .smartdevicelink .proxy .interfaces .OnSystemCapabilityListener ;
12+ import com .smartdevicelink .proxy .rpc .DisplayCapability ;
1113import com .smartdevicelink .proxy .rpc .Image ;
1214import com .smartdevicelink .proxy .rpc .OnButtonEvent ;
1315import com .smartdevicelink .proxy .rpc .OnButtonPress ;
1416import com .smartdevicelink .proxy .rpc .OnHMIStatus ;
1517import com .smartdevicelink .proxy .rpc .Show ;
18+ import com .smartdevicelink .proxy .rpc .ShowResponse ;
1619import com .smartdevicelink .proxy .rpc .SoftButton ;
1720import com .smartdevicelink .proxy .rpc .SoftButtonCapabilities ;
1821import com .smartdevicelink .proxy .rpc .WindowCapability ;
1922import com .smartdevicelink .proxy .rpc .enums .FileType ;
2023import com .smartdevicelink .proxy .rpc .enums .HMILevel ;
2124import com .smartdevicelink .proxy .rpc .enums .ImageType ;
25+ import com .smartdevicelink .proxy .rpc .enums .Result ;
2226import com .smartdevicelink .proxy .rpc .enums .SoftButtonType ;
2327import com .smartdevicelink .proxy .rpc .enums .StaticIconName ;
28+ import com .smartdevicelink .proxy .rpc .enums .SystemAction ;
29+ import com .smartdevicelink .proxy .rpc .enums .SystemCapabilityType ;
2430import com .smartdevicelink .proxy .rpc .listeners .OnRPCNotificationListener ;
2531import com .smartdevicelink .test .Validator ;
2632
3238import java .util .Collections ;
3339import java .util .List ;
3440
41+ import static org .junit .Assert .assertNotEquals ;
3542import static org .mockito .ArgumentMatchers .any ;
3643import static org .mockito .ArgumentMatchers .eq ;
3744import static org .mockito .Mockito .doAnswer ;
3845import static org .mockito .Mockito .mock ;
46+ import static org .mockito .Mockito .when ;
3947
4048/**
4149 * This is a unit test class for the SmartDeviceLink library manager class :
4452public class SoftButtonManagerTests extends AndroidTestCase2 {
4553
4654 private SoftButtonManager softButtonManager ;
47- private boolean fileManagerUploadArtworksGotCalled ;
48- private boolean internalInterfaceSendRPCGotCalled ;
49- private boolean softButtonMangerUpdateCompleted ;
55+ private int fileManagerUploadArtworksListenerCalledCounter ;
56+ private int internalInterfaceSendRPCListenerCalledCounter ;
5057 private int softButtonObject1Id = 1000 , softButtonObject2Id = 2000 ;
5158 private SoftButtonObject softButtonObject1 , softButtonObject2 ;
5259 private SoftButtonState softButtonState1 , softButtonState2 , softButtonState3 , softButtonState4 ;
@@ -73,13 +80,35 @@ public Void answer(InvocationOnMock invocation) {
7380 doAnswer (onHMIStatusAnswer ).when (internalInterface ).addOnRPCNotificationListener (eq (FunctionID .ON_HMI_STATUS ), any (OnRPCNotificationListener .class ));
7481
7582
83+ // When internalInterface.addOnSystemCapabilityListener(SystemCapabilityType.DISPLAYS, onSystemCapabilityListener) is called
84+ // inside SoftButtonManager, respond with a fake response to let the SoftButtonManager continue working.
85+ Answer <Void > onSystemCapabilityAnswer = new Answer <Void >() {
86+ @ Override
87+ public Void answer (InvocationOnMock invocation ) {
88+ Object [] args = invocation .getArguments ();
89+ OnSystemCapabilityListener onSystemCapabilityListener = (OnSystemCapabilityListener ) args [1 ];
90+ SoftButtonCapabilities softButtonCapabilities = new SoftButtonCapabilities ();
91+ softButtonCapabilities .setImageSupported (true );
92+ softButtonCapabilities .setTextSupported (true );
93+ WindowCapability windowCapability = new WindowCapability ();
94+ windowCapability .setSoftButtonCapabilities (Collections .singletonList (softButtonCapabilities ));
95+ DisplayCapability displayCapability = new DisplayCapability ();
96+ displayCapability .setWindowCapabilities (Collections .singletonList (windowCapability ));
97+ List <DisplayCapability > capabilities = Collections .singletonList (displayCapability );
98+ onSystemCapabilityListener .onCapabilityRetrieved (capabilities );
99+ return null ;
100+ }
101+ };
102+ doAnswer (onSystemCapabilityAnswer ).when (internalInterface ).addOnSystemCapabilityListener (eq (SystemCapabilityType .DISPLAYS ), any (OnSystemCapabilityListener .class ));
103+
104+
76105 // When fileManager.uploadArtworks() is called inside the SoftButtonManager, respond with
77106 // a fake onComplete() callback to let the SoftButtonManager continue working.
78107 FileManager fileManager = mock (FileManager .class );
79108 Answer <Void > onFileManagerUploadAnswer = new Answer <Void >() {
80109 @ Override
81110 public Void answer (InvocationOnMock invocation ) {
82- fileManagerUploadArtworksGotCalled = true ;
111+ fileManagerUploadArtworksListenerCalledCounter ++ ;
83112 Object [] args = invocation .getArguments ();
84113 MultipleFileCompletionListener multipleFileCompletionListener = (MultipleFileCompletionListener ) args [1 ];
85114 multipleFileCompletionListener .onComplete (null );
@@ -90,6 +119,9 @@ public Void answer(InvocationOnMock invocation) {
90119
91120
92121 // Create softButtonManager
122+ Taskmaster taskmaster = new Taskmaster .Builder ().build ();
123+ taskmaster .start ();
124+ when (internalInterface .getTaskmaster ()).thenReturn (taskmaster );
93125 softButtonManager = new SoftButtonManager (internalInterface , fileManager );
94126
95127
@@ -99,14 +131,14 @@ public Void answer(InvocationOnMock invocation) {
99131 Answer <Void > onSendShowRPCAnswer = new Answer <Void >() {
100132 @ Override
101133 public Void answer (InvocationOnMock invocation ) {
102- internalInterfaceSendRPCGotCalled = true ;
134+ internalInterfaceSendRPCListenerCalledCounter ++ ;
103135 Object [] args = invocation .getArguments ();
104136 Show show = (Show ) args [0 ];
105137
106- show .getOnRPCResponseListener ().onResponse (0 , null );
138+ show .getOnRPCResponseListener ().onResponse (0 , new ShowResponse ( true , Result . SUCCESS ) );
107139
108140 assertEquals (show .getMainField1 (), softButtonManager .getCurrentMainField1 ());
109- assertEquals (show .getSoftButtons ().size (), softButtonManager .createSoftButtonsForCurrentState ().size ());
141+ assertEquals (show .getSoftButtons ().size (), softButtonManager .getSoftButtonObjects ().size ());
110142
111143 return null ;
112144 }
@@ -130,42 +162,39 @@ public void tearDown() throws Exception {
130162 super .tearDown ();
131163 }
132164
165+ private void sleep () {
166+ try {
167+ Thread .sleep (100 );
168+ } catch (InterruptedException e ) {
169+ e .printStackTrace ();
170+ }
171+ }
172+
133173 public void testSoftButtonManagerUpdate () {
134174 // Reset the boolean variables
135- fileManagerUploadArtworksGotCalled = false ;
136- internalInterfaceSendRPCGotCalled = false ;
137- softButtonMangerUpdateCompleted = false ;
138-
139- SoftButtonCapabilities softCap = new SoftButtonCapabilities ();
140- softCap .setImageSupported (true );
141- WindowCapability defaultCap = new WindowCapability ();
142- defaultCap .setSoftButtonCapabilities (Collections .singletonList (softCap ));
143- softButtonManager .defaultMainWindowCapability = defaultCap ;
175+ fileManagerUploadArtworksListenerCalledCounter = 0 ;
176+ internalInterfaceSendRPCListenerCalledCounter = 0 ;
144177
145178
146179 // Test batch update
147180 softButtonManager .setBatchUpdates (true );
148181 List <SoftButtonObject > softButtonObjects = Arrays .asList (softButtonObject1 , softButtonObject2 );
149182 softButtonManager .setSoftButtonObjects (Arrays .asList (softButtonObject1 , softButtonObject2 ));
150183 softButtonManager .setBatchUpdates (false );
151- softButtonManager .update (new CompletionListener () {
152- @ Override
153- public void onComplete (boolean success ) {
154- softButtonMangerUpdateCompleted = true ;
155- }
156- });
157184
158185
159186 // Test single update, setCurrentMainField1, and transitionToNextState
160187 softButtonManager .setCurrentMainField1 ("It is Wednesday my dudes!" );
161188 softButtonObject1 .transitionToNextState ();
162189
163190
164- // Check that everything got called as expected
191+ // Sleep to give time to Taskmaster to run the operations
192+ sleep ();
165193
166- assertTrue ("FileManager.uploadArtworks() did get called" , fileManagerUploadArtworksGotCalled );
167- assertTrue ("InternalInterface.sendRPC() did not get called" , internalInterfaceSendRPCGotCalled );
168- assertTrue ("SoftButtonManger update onComplete() did not get called" , softButtonMangerUpdateCompleted );
194+
195+ // Check that everything got called as expected
196+ assertEquals ("FileManager.uploadArtworks() did not get called correctly" , 1 , fileManagerUploadArtworksListenerCalledCounter );
197+ assertEquals ("InternalInterface.sendRPC() did not get called correctly" ,2 , internalInterfaceSendRPCListenerCalledCounter );
169198
170199
171200 // Test getSoftButtonObjects
@@ -209,8 +238,9 @@ public void testSoftButtonState(){
209238 SoftButton softButtonExpectedValue = new SoftButton (SoftButtonType .SBT_BOTH , SoftButtonObject .SOFT_BUTTON_ID_NOT_SET_VALUE );
210239 softButtonExpectedValue .setText ("o1s1" );
211240 softButtonExpectedValue .setImage (new Image (artworkExpectedValue .getName (), ImageType .DYNAMIC ));
241+ softButtonExpectedValue .setSystemAction (SystemAction .DEFAULT_ACTION );
212242 SoftButton actual = softButtonState1 .getSoftButton ();
213- assertTrue ("Returned SoftButton doesn't match the expected value" , Validator .validateSoftButton (softButtonExpectedValue , softButtonState1 . getSoftButton () ));
243+ assertTrue ("Returned SoftButton doesn't match the expected value" , Validator .validateSoftButton (softButtonExpectedValue , actual ));
214244 }
215245
216246 public void testSoftButtonObject (){
@@ -233,6 +263,7 @@ public void testSoftButtonObject(){
233263 // Test SoftButtonObject.getCurrentStateSoftButton()
234264 SoftButton softButtonExpectedValue = new SoftButton (SoftButtonType .SBT_TEXT , softButtonObject2Id );
235265 softButtonExpectedValue .setText ("o2s1" );
266+ softButtonExpectedValue .setSystemAction (SystemAction .DEFAULT_ACTION );
236267 assertTrue ("Returned current state SoftButton doesn't match the expected value" , Validator .validateSoftButton (softButtonExpectedValue , softButtonObject2 .getCurrentStateSoftButton ()));
237268
238269
@@ -339,14 +370,14 @@ public void onEvent(SoftButtonObject softButtonObject, OnButtonEvent onButtonEve
339370 // Case 1: object is null, assertFalse
340371 softButtonObject1 = new SoftButtonObject ("test" , softButtonState1 , null );
341372 softButtonObject2 = null ;
342- assertFalse (softButtonObject1 . equals ( softButtonObject2 ) );
373+ assertNotEquals (softButtonObject1 , softButtonObject2 );
343374
344375 // Case 2 SoftButtonObjects are the same, assertTrue
345- assertTrue (softButtonObject1 . equals ( softButtonObject1 ) );
376+ assertEquals (softButtonObject1 , softButtonObject1 );
346377
347378 // Case 3: object is not an instance of SoftButtonObject assertFalse
348379 SdlArtwork artwork = new SdlArtwork ("image1" , FileType .GRAPHIC_PNG , 1 , true );
349- assertFalse (softButtonObject1 . equals ( artwork ) );
380+ assertNotEquals (softButtonObject1 , artwork );
350381
351382 // Case 4: SoftButtonObjectState List are not same size, assertFalse
352383 List <SoftButtonState > softButtonStateList = new ArrayList <>();
@@ -356,53 +387,53 @@ public void onEvent(SoftButtonObject softButtonObject, OnButtonEvent onButtonEve
356387 softButtonStateList2 .add (softButtonState2 );
357388 softButtonObject1 = new SoftButtonObject ("hi" , softButtonStateList , "Hi" , null );
358389 softButtonObject2 = new SoftButtonObject ("hi" , softButtonStateList2 , "Hi" , null );
359- assertFalse (softButtonObject1 . equals ( softButtonObject2 ) );
390+ assertNotEquals (softButtonObject1 , softButtonObject2 );
360391
361392 // Case 5: SoftButtonStates are not the same, assertFalse
362393 softButtonObject1 = new SoftButtonObject ("test" , softButtonState1 , null );
363394 softButtonObject2 = new SoftButtonObject ("test" , softButtonState2 , null );
364- assertFalse (softButtonObject1 . equals ( softButtonObject2 ) );
395+ assertNotEquals (softButtonObject1 , softButtonObject2 );
365396
366397 // Case 6: SoftButtonObject names are not same, assertFalse
367398 softButtonObject1 = new SoftButtonObject ("test" , softButtonState1 , null );
368399 softButtonObject2 = new SoftButtonObject ("test23123" , softButtonState1 , null );
369- assertFalse (softButtonObject1 . equals ( softButtonObject2 ) );
400+ assertNotEquals (softButtonObject1 , softButtonObject2 );
370401
371402 // Case 7: SoftButtonObject currentStateName not same, assertFalse
372403 softButtonObject1 = new SoftButtonObject ("hi" , softButtonStateList , "Hi" , null );
373404 softButtonObject2 = new SoftButtonObject ("hi" , softButtonStateList , "Hi2" , null );
374- assertFalse (softButtonObject1 . equals ( softButtonObject2 ) );
405+ assertNotEquals (softButtonObject1 , softButtonObject2 );
375406 }
376407
377408 /**
378409 * Test custom overridden softButtonState equals method
379410 */
380411 public void testSoftButtonStateEquals () {
381- assertFalse (softButtonState1 . equals ( softButtonState2 ) );
412+ assertNotEquals (softButtonState1 , softButtonState2 );
382413 SdlArtwork artwork1 = new SdlArtwork ("image1" , FileType .GRAPHIC_PNG , 1 , true );
383414 SdlArtwork artwork2 = new SdlArtwork ("image2" , FileType .GRAPHIC_PNG , 1 , true );
384415
385416 // Case 1: object is null, assertFalse
386417 softButtonState1 = new SoftButtonState ("object1-state1" , "o1s1" , artwork1 );
387418 softButtonState2 = null ;
388- assertFalse (softButtonState1 . equals ( softButtonState2 ) );
419+ assertNotEquals (softButtonState1 , softButtonState2 );
389420
390421 // Case 2 SoftButtonObjects are the same, assertTrue
391- assertTrue (softButtonState1 . equals ( softButtonState1 ) );
422+ assertEquals (softButtonState1 , softButtonState1 );
392423
393424 // Case 3: object is not an instance of SoftButtonState, assertFalse
394- assertFalse (softButtonState1 . equals ( artwork1 ) );
425+ assertNotEquals (softButtonState1 , artwork1 );
395426
396427 // Case 4: different artwork, assertFalse
397428 softButtonState2 = new SoftButtonState ("object1-state1" , "o1s1" , artwork2 );
398- assertFalse (softButtonState1 . equals ( softButtonState2 ) );
429+ assertNotEquals (softButtonState1 , softButtonState2 );
399430
400431 // Case 5: different name, assertFalse
401432 softButtonState2 = new SoftButtonState ("object1-state1 different name" , "o1s1" , artwork1 );
402- assertFalse (softButtonState1 . equals ( softButtonState2 ) );
433+ assertNotEquals (softButtonState1 , softButtonState2 );
403434
404435 // Case 6 they are equal, assertTrue
405436 softButtonState2 = new SoftButtonState ("object1-state1" , "o1s1" , artwork1 );
406- assertTrue (softButtonState1 . equals ( softButtonState2 ) );
437+ assertEquals (softButtonState1 , softButtonState2 );
407438 }
408439}
0 commit comments