diff options
author | Bilal Alsharifi <599206+bilal-alsharifi@users.noreply.github.com> | 2019-09-11 16:56:43 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-09-11 16:56:43 -0400 |
commit | 3b610a85645f8c890305eb3b2dc986a25fb465ed (patch) | |
tree | 0ed9cbf5e32bc8df02598041fedbb02b1af30d29 | |
parent | d72bb683888740174337c174c4f3b0e4c743fbef (diff) | |
parent | 0433b332da81f43b9aa9fb31cfacf7107f070792 (diff) | |
download | sdl_android-feature/blocking_operations.tar.gz |
Merge pull request #1162 from smartdevicelink/feature/blocking_operations_test_casesfeature/blocking_operations
Fix blocking operations test cases
3 files changed, 377 insertions, 241 deletions
diff --git a/android/sdl_android/src/androidTest/java/com/smartdevicelink/managers/screen/choiceset/PresentChoiceSetOperationTests.java b/android/sdl_android/src/androidTest/java/com/smartdevicelink/managers/screen/choiceset/PresentChoiceSetOperationTests.java index 509659656..d8cf5be9d 100644 --- a/android/sdl_android/src/androidTest/java/com/smartdevicelink/managers/screen/choiceset/PresentChoiceSetOperationTests.java +++ b/android/sdl_android/src/androidTest/java/com/smartdevicelink/managers/screen/choiceset/PresentChoiceSetOperationTests.java @@ -54,32 +54,42 @@ import org.mockito.invocation.InvocationOnMock; import org.mockito.stubbing.Answer; import java.util.Collections; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.TimeUnit; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.doAnswer; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.never; +import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; public class PresentChoiceSetOperationTests extends AndroidTestCase2 { private PresentChoiceSetOperation presentChoiceSetOperation; private ChoiceSet choiceSet; private ISdl internalInterface; + private KeyboardListener keyboardListener; + private ChoiceSetSelectionListener choiceSetSelectionListener; + + private ExecutorService executor; @Override public void setUp() throws Exception{ super.setUp(); internalInterface = mock(ISdl.class); - KeyboardListener keyboardListener = mock(KeyboardListener.class); - ChoiceSetSelectionListener choiceSetSelectionListener = mock(ChoiceSetSelectionListener.class); + + keyboardListener = mock(KeyboardListener.class); + choiceSetSelectionListener = mock(ChoiceSetSelectionListener.class); ChoiceCell cell1 = new ChoiceCell("Cell1"); cell1.setChoiceId(0); choiceSet = new ChoiceSet("Test", Collections.singletonList(cell1), choiceSetSelectionListener); - presentChoiceSetOperation = new PresentChoiceSetOperation(internalInterface, choiceSet, InteractionMode.MANUAL_ONLY, getKeyBoardProperties(), keyboardListener, choiceSetSelectionListener, Test.GENERAL_INTEGER); - presentChoiceSetOperation.sdlMsgVersion = new SdlMsgVersion(6, 0); + + executor = Executors.newCachedThreadPool(); } @Override @@ -87,14 +97,26 @@ public class PresentChoiceSetOperationTests extends AndroidTestCase2 { super.tearDown(); } - public void testGetLayoutMode(){ + private KeyboardProperties getKeyBoardProperties(){ + KeyboardProperties properties = new KeyboardProperties(); + properties.setLanguage(Language.EN_US); + properties.setKeyboardLayout(KeyboardLayout.QWERTZ); + properties.setKeypressMode(KeypressMode.RESEND_CURRENT_ENTRY); + return properties; + } + + public void testGetLayoutMode(){ // First we will check knowing our keyboard listener is NOT NULL + presentChoiceSetOperation = new PresentChoiceSetOperation(internalInterface, choiceSet, InteractionMode.MANUAL_ONLY, getKeyBoardProperties(), keyboardListener, choiceSetSelectionListener, Test.GENERAL_INTEGER); + assertEquals(presentChoiceSetOperation.getLayoutMode(), LayoutMode.LIST_WITH_SEARCH); presentChoiceSetOperation.keyboardListener = null; assertEquals(presentChoiceSetOperation.getLayoutMode(), LayoutMode.LIST_ONLY); } public void testGetPerformInteraction(){ + presentChoiceSetOperation = new PresentChoiceSetOperation(internalInterface, choiceSet, InteractionMode.MANUAL_ONLY, getKeyBoardProperties(), keyboardListener, choiceSetSelectionListener, Test.GENERAL_INTEGER); + PerformInteraction pi = presentChoiceSetOperation.getPerformInteraction(); assertEquals(pi.getInitialText(), "Test"); assertNull(pi.getHelpPrompt()); @@ -106,127 +128,178 @@ public class PresentChoiceSetOperationTests extends AndroidTestCase2 { } public void testSetSelectedCellWithId(){ + presentChoiceSetOperation = new PresentChoiceSetOperation(internalInterface, choiceSet, InteractionMode.MANUAL_ONLY, getKeyBoardProperties(), keyboardListener, choiceSetSelectionListener, Test.GENERAL_INTEGER); + assertNull(presentChoiceSetOperation.selectedCellRow); presentChoiceSetOperation.setSelectedCellWithId(0); assertEquals(presentChoiceSetOperation.selectedCellRow, Integer.valueOf(0)); } - private KeyboardProperties getKeyBoardProperties(){ - KeyboardProperties properties = new KeyboardProperties(); - properties.setLanguage(Language.EN_US); - properties.setKeyboardLayout(KeyboardLayout.QWERTZ); - properties.setKeypressMode(KeypressMode.RESEND_CURRENT_ENTRY); - return properties; + public void testCancelingChoiceSetSuccessfullyIfThreadIsRunning(){ + when(internalInterface.getSdlMsgVersion()).thenReturn(new SdlMsgVersion(6, 0)); + presentChoiceSetOperation = new PresentChoiceSetOperation(internalInterface, choiceSet, InteractionMode.MANUAL_ONLY, null, null, choiceSetSelectionListener, Test.GENERAL_INTEGER); + executor.execute(presentChoiceSetOperation); + + try { + executor.awaitTermination(1, TimeUnit.SECONDS); + } catch (InterruptedException e) {} + + assertTrue(presentChoiceSetOperation.isExecuting()); + assertFalse(presentChoiceSetOperation.isFinished()); + assertFalse(presentChoiceSetOperation.isCancelled()); + + choiceSet.cancel(); + Answer<Void> cancelInteractionAnswer = new Answer<Void>() { + @Override + public Void answer(InvocationOnMock invocation) { + Object[] args = invocation.getArguments(); + CancelInteraction cancelInteraction = (CancelInteraction) args[0]; + + assertEquals(cancelInteraction.getCancelID(), Test.GENERAL_INTEGER); + assertEquals(cancelInteraction.getInteractionFunctionID().intValue(), FunctionID.PERFORM_INTERACTION.getId()); + + RPCResponse response = new RPCResponse(FunctionID.CANCEL_INTERACTION.toString()); + response.setSuccess(true); + cancelInteraction.getOnRPCResponseListener().onResponse(0, response); + + return null; + } + }; + doAnswer(cancelInteractionAnswer).when(internalInterface).sendRPC(any(CancelInteraction.class)); + + verify(internalInterface, times(1)).sendRPC(any(CancelInteraction.class)); + verify(internalInterface, times(1)).sendRPC(any(PerformInteraction.class)); + + assertTrue(presentChoiceSetOperation.isExecuting()); + assertFalse(presentChoiceSetOperation.isFinished()); + assertFalse(presentChoiceSetOperation.isCancelled()); } -// public void testCancelingChoiceSetSuccessfullyIfThreadIsRunning(){ -// presentChoiceSetOperation.run(); -// -// Answer<Void> cancelInteractionAnswer = new Answer<Void>() { -// @Override -// public Void answer(InvocationOnMock invocation) { -// Object[] args = invocation.getArguments(); -// CancelInteraction cancelInteraction = (CancelInteraction) args[0]; -// -// assertEquals(cancelInteraction.getCancelID(), Test.GENERAL_INTEGER); -// assertEquals(cancelInteraction.getInteractionFunctionID().intValue(), FunctionID.PERFORM_INTERACTION.getId()); -// -// RPCResponse response = new RPCResponse(FunctionID.CANCEL_INTERACTION.toString()); -// response.setSuccess(true); -// cancelInteraction.getOnRPCResponseListener().onResponse(0, response); -// -// return null; -// } -// }; -// doAnswer(cancelInteractionAnswer).when(internalInterface).sendRPC(any(CancelInteraction.class)); -// -// choiceSet.cancel(); -// -// assertTrue(presentChoiceSetOperation.isExecuting()); -// assertFalse(presentChoiceSetOperation.isFinished()); -// } -// -// public void testCancelingChoiceSetUnsuccessfullyIfThreadIsRunning(){ -// presentChoiceSetOperation.run(); -// -// Answer<Void> cancelInteractionAnswer = new Answer<Void>() { -// @Override -// public Void answer(InvocationOnMock invocation) { -// Object[] args = invocation.getArguments(); -// CancelInteraction cancelInteraction = (CancelInteraction) args[0]; -// -// assertEquals(cancelInteraction.getCancelID(), Test.GENERAL_INTEGER); -// assertEquals(cancelInteraction.getInteractionFunctionID().intValue(), FunctionID.PERFORM_INTERACTION.getId()); -// -// RPCResponse response = new RPCResponse(FunctionID.CANCEL_INTERACTION.toString()); -// response.setSuccess(false); -// cancelInteraction.getOnRPCResponseListener().onResponse(0, response); -// -// return null; -// } -// }; -// doAnswer(cancelInteractionAnswer).when(internalInterface).sendRPC(any(CancelInteraction.class)); -// -// choiceSet.cancel(); -// -// assertTrue(presentChoiceSetOperation.isExecuting()); -// assertFalse(presentChoiceSetOperation.isFinished()); -// } -// -// public void testCancelingChoiceSetIfThreadHasFinished(){ -// presentChoiceSetOperation.run(); -// presentChoiceSetOperation.finishOperation(); -// -// choiceSet.cancel(); -// -// verify(internalInterface, never()).sendRPC(any(CancelInteraction.class)); -// -// assertFalse(presentChoiceSetOperation.isExecuting()); -// assertTrue(presentChoiceSetOperation.isFinished()); -// } -// -// public void testCancelingChoiceSetIfThreadHasNotYetRun(){ -// choiceSet.cancel(); -// -// verify(internalInterface, never()).sendRPC(any(CancelInteraction.class)); -// -// // Once the thread has started -// presentChoiceSetOperation.run(); -// -// // Make sure neither a `CancelInteraction` or `PerformInteraction` RPC is sent -// verify(internalInterface, never()).sendRPC(any(CancelInteraction.class)); -// verify(internalInterface, never()).sendRPC(any(PerformInteraction.class)); -// -// assertFalse(presentChoiceSetOperation.isExecuting()); -// assertTrue(presentChoiceSetOperation.isFinished()); -// } -// -// public void testCancelingChoiceSetIfHeadUnitDoesNotSupportFeature(){ -// // Only supported with RPC spec versions 6.0.0+ -// presentChoiceSetOperation.sdlMsgVersion = new SdlMsgVersion(5, 3); -// presentChoiceSetOperation.run(); -// -// choiceSet.cancel(); -// -// verify(internalInterface, never()).sendRPC(any(CancelInteraction.class)); -// } -// -// public void testCancelingChoiceSetIfHeadUnitDoesNotSupportFeatureButThreadIsNotRunning(){ -// // Only supported with RPC spec versions 6.0.0+ -// presentChoiceSetOperation.sdlMsgVersion = new SdlMsgVersion(5, 3); -// -// choiceSet.cancel(); -// -// verify(internalInterface, never()).sendRPC(any(CancelInteraction.class)); -// -// // Once the thread has started -// presentChoiceSetOperation.run(); -// -// // Make sure neither a `CancelInteraction` or `PerformInteraction` RPC is sent -// verify(internalInterface, never()).sendRPC(any(CancelInteraction.class)); -// verify(internalInterface, never()).sendRPC(any(PerformInteraction.class)); -// -// assertFalse(presentChoiceSetOperation.isExecuting()); -// assertTrue(presentChoiceSetOperation.isFinished()); -// } -} + public void testCancelingChoiceSetUnsuccessfullyIfThreadIsRunning(){ + when(internalInterface.getSdlMsgVersion()).thenReturn(new SdlMsgVersion(6, 0)); + presentChoiceSetOperation = new PresentChoiceSetOperation(internalInterface, choiceSet, InteractionMode.MANUAL_ONLY, null, null, choiceSetSelectionListener, Test.GENERAL_INTEGER); + executor.execute(presentChoiceSetOperation); + try { + executor.awaitTermination(1, TimeUnit.SECONDS); + } catch (InterruptedException e) {} + + assertTrue(presentChoiceSetOperation.isExecuting()); + assertFalse(presentChoiceSetOperation.isFinished()); + assertFalse(presentChoiceSetOperation.isCancelled()); + + choiceSet.cancel(); + Answer<Void> cancelInteractionAnswer = new Answer<Void>() { + @Override + public Void answer(InvocationOnMock invocation) { + Object[] args = invocation.getArguments(); + CancelInteraction cancelInteraction = (CancelInteraction) args[0]; + + assertEquals(cancelInteraction.getCancelID(), Test.GENERAL_INTEGER); + assertEquals(cancelInteraction.getInteractionFunctionID().intValue(), FunctionID.PERFORM_INTERACTION.getId()); + + RPCResponse response = new RPCResponse(FunctionID.CANCEL_INTERACTION.toString()); + response.setSuccess(false); + cancelInteraction.getOnRPCResponseListener().onResponse(0, response); + + return null; + } + }; + doAnswer(cancelInteractionAnswer).when(internalInterface).sendRPC(any(CancelInteraction.class)); + + verify(internalInterface, times(1)).sendRPC(any(CancelInteraction.class)); + verify(internalInterface, times(1)).sendRPC(any(PerformInteraction.class)); + + assertTrue(presentChoiceSetOperation.isExecuting()); + assertFalse(presentChoiceSetOperation.isFinished()); + assertFalse(presentChoiceSetOperation.isCancelled()); + } + + public void testCancelingChoiceSetIfThreadHasFinished(){ + when(internalInterface.getSdlMsgVersion()).thenReturn(new SdlMsgVersion(6, 0)); + presentChoiceSetOperation = new PresentChoiceSetOperation(internalInterface, choiceSet, InteractionMode.MANUAL_ONLY, null, null, choiceSetSelectionListener, Test.GENERAL_INTEGER); + presentChoiceSetOperation.finishOperation(); + + assertFalse(presentChoiceSetOperation.isExecuting()); + assertTrue(presentChoiceSetOperation.isFinished()); + assertFalse(presentChoiceSetOperation.isCancelled()); + + choiceSet.cancel(); + verify(internalInterface, never()).sendRPC(any(CancelInteraction.class)); + + assertFalse(presentChoiceSetOperation.isExecuting()); + assertTrue(presentChoiceSetOperation.isFinished()); + assertFalse(presentChoiceSetOperation.isCancelled()); + } + + public void testCancelingChoiceSetIfThreadHasNotYetRun(){ + when(internalInterface.getSdlMsgVersion()).thenReturn(new SdlMsgVersion(6, 0)); + presentChoiceSetOperation = new PresentChoiceSetOperation(internalInterface, choiceSet, InteractionMode.MANUAL_ONLY, null, null, choiceSetSelectionListener, Test.GENERAL_INTEGER); + + assertFalse(presentChoiceSetOperation.isExecuting()); + assertFalse(presentChoiceSetOperation.isFinished()); + assertFalse(presentChoiceSetOperation.isCancelled()); + + choiceSet.cancel(); + + // Once the operation has started + executor.execute(presentChoiceSetOperation); + try { + executor.awaitTermination(1, TimeUnit.SECONDS); + } catch (InterruptedException e) {} + + assertFalse(presentChoiceSetOperation.isExecuting()); + assertTrue(presentChoiceSetOperation.isFinished()); + assertFalse(presentChoiceSetOperation.isCancelled()); + + // Make sure neither a `CancelInteraction` or `PerformInteraction` RPC is ever sent + verify(internalInterface, never()).sendRPC(any(CancelInteraction.class)); + verify(internalInterface, never()).sendRPC(any(PerformInteraction.class)); + } + + public void testCancelingChoiceSetIfHeadUnitDoesNotSupportFeature(){ + // Cancel Interaction is only supported on RPC specs v.6.0.0+ + when(internalInterface.getSdlMsgVersion()).thenReturn(new SdlMsgVersion(5, 3)); + presentChoiceSetOperation = new PresentChoiceSetOperation(internalInterface, choiceSet, InteractionMode.MANUAL_ONLY, null, null, choiceSetSelectionListener, Test.GENERAL_INTEGER); + executor.execute(presentChoiceSetOperation); + try { + executor.awaitTermination(1, TimeUnit.SECONDS); + } catch (InterruptedException e) {} + + assertTrue(presentChoiceSetOperation.isExecuting()); + assertFalse(presentChoiceSetOperation.isFinished()); + assertFalse(presentChoiceSetOperation.isCancelled()); + + choiceSet.cancel(); + + verify(internalInterface, never()).sendRPC(any(CancelInteraction.class)); + verify(internalInterface, times(1)).sendRPC(any(PerformInteraction.class)); + } + + public void testCancelingChoiceSetIfHeadUnitDoesNotSupportFeatureButThreadIsNotRunning(){ + // Cancel Interaction is only supported on RPC specs v.6.0.0+ + when(internalInterface.getSdlMsgVersion()).thenReturn(new SdlMsgVersion(5, 3)); + presentChoiceSetOperation = new PresentChoiceSetOperation(internalInterface, choiceSet, InteractionMode.MANUAL_ONLY, null, null, choiceSetSelectionListener, Test.GENERAL_INTEGER); + + assertFalse(presentChoiceSetOperation.isExecuting()); + assertFalse(presentChoiceSetOperation.isFinished()); + assertFalse(presentChoiceSetOperation.isCancelled()); + + choiceSet.cancel(); + + verify(internalInterface, never()).sendRPC(any(CancelInteraction.class)); + + // Once the operation has started + executor.execute(presentChoiceSetOperation); + try { + executor.awaitTermination(1, TimeUnit.SECONDS); + } catch (InterruptedException e) {} + + assertFalse(presentChoiceSetOperation.isExecuting()); + assertTrue(presentChoiceSetOperation.isFinished()); + assertFalse(presentChoiceSetOperation.isCancelled()); + + // Make sure neither a `CancelInteraction` or `PerformInteraction` RPC is ever sent + verify(internalInterface, never()).sendRPC(any(CancelInteraction.class)); + verify(internalInterface, never()).sendRPC(any(PerformInteraction.class)); + } +}
\ No newline at end of file diff --git a/android/sdl_android/src/androidTest/java/com/smartdevicelink/managers/screen/choiceset/PresentKeyboardOperationTests.java b/android/sdl_android/src/androidTest/java/com/smartdevicelink/managers/screen/choiceset/PresentKeyboardOperationTests.java index ce3e785f3..486a1c361 100644 --- a/android/sdl_android/src/androidTest/java/com/smartdevicelink/managers/screen/choiceset/PresentKeyboardOperationTests.java +++ b/android/sdl_android/src/androidTest/java/com/smartdevicelink/managers/screen/choiceset/PresentKeyboardOperationTests.java @@ -53,25 +53,32 @@ import com.smartdevicelink.test.Test; import org.mockito.invocation.InvocationOnMock; import org.mockito.stubbing.Answer; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.TimeUnit; + import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.doAnswer; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.never; +import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; public class PresentKeyboardOperationTests extends AndroidTestCase2 { private PresentKeyboardOperation presentKeyboardOperation; + private KeyboardListener keyboardListener; private ISdl internalInterface; + private ExecutorService executor; + @Override public void setUp() throws Exception{ super.setUp(); internalInterface = mock(ISdl.class); - KeyboardListener keyboardListener = mock(KeyboardListener.class); - - presentKeyboardOperation = new PresentKeyboardOperation(internalInterface, getKeyBoardProperties(), "Test", null, keyboardListener, Test.GENERAL_INTEGER); + keyboardListener = mock(KeyboardListener.class); Answer<Void> setGlobalPropertiesAnswer = new Answer<Void>() { @Override @@ -88,7 +95,7 @@ public class PresentKeyboardOperationTests extends AndroidTestCase2 { }; doAnswer(setGlobalPropertiesAnswer).when(internalInterface).sendRPC(any(SetGlobalProperties.class)); - presentKeyboardOperation.sdlMsgVersion = new SdlMsgVersion(6,0); + executor = Executors.newCachedThreadPool(); } @Override @@ -96,7 +103,17 @@ public class PresentKeyboardOperationTests extends AndroidTestCase2 { super.tearDown(); } + private KeyboardProperties getKeyBoardProperties(){ + KeyboardProperties properties = new KeyboardProperties(); + properties.setLanguage(Language.EN_US); + properties.setKeyboardLayout(KeyboardLayout.QWERTZ); + properties.setKeypressMode(KeypressMode.RESEND_CURRENT_ENTRY); + return properties; + } + public void testGetPerformInteraction(){ + presentKeyboardOperation = new PresentKeyboardOperation(internalInterface, getKeyBoardProperties(), "Test", null, keyboardListener, Test.GENERAL_INTEGER); + PerformInteraction pi = presentKeyboardOperation.getPerformInteraction(); assertEquals(pi.getInitialText(), "Test"); assertNull(pi.getHelpPrompt()); @@ -106,120 +123,166 @@ public class PresentKeyboardOperationTests extends AndroidTestCase2 { assertEquals(pi.getCancelID(), Test.GENERAL_INTEGER); } - private KeyboardProperties getKeyBoardProperties(){ - KeyboardProperties properties = new KeyboardProperties(); - properties.setLanguage(Language.EN_US); - properties.setKeyboardLayout(KeyboardLayout.QWERTZ); - properties.setKeypressMode(KeypressMode.RESEND_CURRENT_ENTRY); - return properties; + public void testCancelingKeyboardSuccessfullyIfThreadIsRunning(){ + when(internalInterface.getSdlMsgVersion()).thenReturn(new SdlMsgVersion(6, 0)); + presentKeyboardOperation = new PresentKeyboardOperation(internalInterface, null, "Test", null, null, Test.GENERAL_INTEGER); + executor.execute(presentKeyboardOperation); + try { + executor.awaitTermination(1, TimeUnit.SECONDS); + } catch (InterruptedException e) {} + + assertTrue(presentKeyboardOperation.isExecuting()); + assertFalse(presentKeyboardOperation.isFinished()); + assertFalse(presentKeyboardOperation.isCancelled()); + + presentKeyboardOperation.dismissKeyboard(); + Answer<Void> cancelInteractionAnswer = new Answer<Void>() { + @Override + public Void answer(InvocationOnMock invocation) { + Object[] args = invocation.getArguments(); + CancelInteraction cancelInteraction = (CancelInteraction) args[0]; + + assertEquals(cancelInteraction.getCancelID(), Test.GENERAL_INTEGER); + assertEquals(cancelInteraction.getInteractionFunctionID().intValue(), FunctionID.PERFORM_INTERACTION.getId()); + + RPCResponse response = new RPCResponse(FunctionID.CANCEL_INTERACTION.toString()); + response.setSuccess(true); + cancelInteraction.getOnRPCResponseListener().onResponse(0, response); + + return null; + } + }; + doAnswer(cancelInteractionAnswer).when(internalInterface).sendRPC(any(CancelInteraction.class)); + + verify(internalInterface, times(1)).sendRPC(any(CancelInteraction.class)); + verify(internalInterface, times(1)).sendRPC(any(PerformInteraction.class)); + + assertTrue(presentKeyboardOperation.isExecuting()); + assertFalse(presentKeyboardOperation.isFinished()); + assertFalse(presentKeyboardOperation.isCancelled()); + } + + public void testCancelingKeyboardUnsuccessfullyIfThreadIsRunning(){ + when(internalInterface.getSdlMsgVersion()).thenReturn(new SdlMsgVersion(6, 0)); + presentKeyboardOperation = new PresentKeyboardOperation(internalInterface, null, "Test", null, null, Test.GENERAL_INTEGER); + executor.execute(presentKeyboardOperation); + try { + executor.awaitTermination(1, TimeUnit.SECONDS); + } catch (InterruptedException e) {} + + presentKeyboardOperation.dismissKeyboard(); + Answer<Void> cancelInteractionAnswer = new Answer<Void>() { + @Override + public Void answer(InvocationOnMock invocation) { + Object[] args = invocation.getArguments(); + CancelInteraction cancelInteraction = (CancelInteraction) args[0]; + + assertEquals(cancelInteraction.getCancelID(), Test.GENERAL_INTEGER); + assertEquals(cancelInteraction.getInteractionFunctionID().intValue(), FunctionID.PERFORM_INTERACTION.getId()); + + RPCResponse response = new RPCResponse(FunctionID.CANCEL_INTERACTION.toString()); + response.setSuccess(false); + cancelInteraction.getOnRPCResponseListener().onResponse(0, response); + + return null; + } + }; + doAnswer(cancelInteractionAnswer).when(internalInterface).sendRPC(any(CancelInteraction.class)); + + verify(internalInterface, times(1)).sendRPC(any(CancelInteraction.class)); + verify(internalInterface, times(1)).sendRPC(any(PerformInteraction.class)); + + assertTrue(presentKeyboardOperation.isExecuting()); + assertFalse(presentKeyboardOperation.isFinished()); + assertFalse(presentKeyboardOperation.isCancelled()); + } + + public void testCancelingKeyboardIfThreadHasFinished(){ + when(internalInterface.getSdlMsgVersion()).thenReturn(new SdlMsgVersion(6, 0)); + presentKeyboardOperation = new PresentKeyboardOperation(internalInterface, null, "Test", null, null, Test.GENERAL_INTEGER); + presentKeyboardOperation.finishOperation(); + + assertFalse(presentKeyboardOperation.isExecuting()); + assertTrue(presentKeyboardOperation.isFinished()); + assertFalse(presentKeyboardOperation.isCancelled()); + + presentKeyboardOperation.dismissKeyboard(); + verify(internalInterface, never()).sendRPC(any(CancelInteraction.class)); + + assertFalse(presentKeyboardOperation.isExecuting()); + assertTrue(presentKeyboardOperation.isFinished()); + assertFalse(presentKeyboardOperation.isCancelled()); + } + + public void testCancelingKeyboardIfThreadHasNotYetRun(){ + when(internalInterface.getSdlMsgVersion()).thenReturn(new SdlMsgVersion(6, 0)); + presentKeyboardOperation = new PresentKeyboardOperation(internalInterface, null, "Test", null, null, Test.GENERAL_INTEGER); + + assertFalse(presentKeyboardOperation.isExecuting()); + assertFalse(presentKeyboardOperation.isFinished()); + assertFalse(presentKeyboardOperation.isCancelled()); + + presentKeyboardOperation.dismissKeyboard(); + + // Once the operation has started + executor.execute(presentKeyboardOperation); + try { + executor.awaitTermination(1, TimeUnit.SECONDS); + } catch (InterruptedException e) {} + + assertFalse(presentKeyboardOperation.isExecuting()); + assertTrue(presentKeyboardOperation.isFinished()); + assertFalse(presentKeyboardOperation.isCancelled()); + + // Make sure neither a `CancelInteraction` or `PerformInteraction` RPC is ever sent + verify(internalInterface, never()).sendRPC(any(CancelInteraction.class)); + verify(internalInterface, never()).sendRPC(any(PerformInteraction.class)); + } + + public void testCancelingChoiceSetIfHeadUnitDoesNotSupportFeature(){ + // Cancel Interaction is only supported on RPC specs v.6.0.0+ + when(internalInterface.getSdlMsgVersion()).thenReturn(new SdlMsgVersion(5, 3)); + presentKeyboardOperation = new PresentKeyboardOperation(internalInterface, null, "Test", null, null, Test.GENERAL_INTEGER); + executor.execute(presentKeyboardOperation); + try { + executor.awaitTermination(1, TimeUnit.SECONDS); + } catch (InterruptedException e) {} + + assertTrue(presentKeyboardOperation.isExecuting()); + assertFalse(presentKeyboardOperation.isFinished()); + assertFalse(presentKeyboardOperation.isCancelled()); + + presentKeyboardOperation.dismissKeyboard(); + + verify(internalInterface, never()).sendRPC(any(CancelInteraction.class)); + verify(internalInterface, times(1)).sendRPC(any(PerformInteraction.class)); } -// public void testCancelingKeyboardSuccessfullyIfThreadIsRunning(){ -// presentKeyboardOperation.run(); -// -// Answer<Void> cancelInteractionAnswer = new Answer<Void>() { -// @Override -// public Void answer(InvocationOnMock invocation) { -// Object[] args = invocation.getArguments(); -// CancelInteraction cancelInteraction = (CancelInteraction) args[0]; -// -// assertEquals(cancelInteraction.getCancelID(), Test.GENERAL_INTEGER); -// assertEquals(cancelInteraction.getInteractionFunctionID().intValue(), FunctionID.PERFORM_INTERACTION.getId()); -// -// RPCResponse response = new RPCResponse(FunctionID.CANCEL_INTERACTION.toString()); -// response.setSuccess(true); -// cancelInteraction.getOnRPCResponseListener().onResponse(0, response); -// -// return null; -// } -// }; -// doAnswer(cancelInteractionAnswer).when(internalInterface).sendRPC(any(CancelInteraction.class)); -// -// presentKeyboardOperation.dismissKeyboard(); -// -// assertTrue(presentKeyboardOperation.isExecuting()); -// assertFalse(presentKeyboardOperation.isFinished()); -// } -// -// public void testCancelingKeyboardUnsuccessfullyIfThreadIsRunning(){ -// presentKeyboardOperation.run(); -// -// Answer<Void> cancelInteractionAnswer = new Answer<Void>() { -// @Override -// public Void answer(InvocationOnMock invocation) { -// Object[] args = invocation.getArguments(); -// CancelInteraction cancelInteraction = (CancelInteraction) args[0]; -// -// assertEquals(cancelInteraction.getCancelID(), Test.GENERAL_INTEGER); -// assertEquals(cancelInteraction.getInteractionFunctionID().intValue(), FunctionID.PERFORM_INTERACTION.getId()); -// -// RPCResponse response = new RPCResponse(FunctionID.CANCEL_INTERACTION.toString()); -// response.setSuccess(false); -// cancelInteraction.getOnRPCResponseListener().onResponse(0, response); -// -// return null; -// } -// }; -// doAnswer(cancelInteractionAnswer).when(internalInterface).sendRPC(any(CancelInteraction.class)); -// -// presentKeyboardOperation.dismissKeyboard(); -// -// assertTrue(presentKeyboardOperation.isExecuting()); -// assertFalse(presentKeyboardOperation.isFinished()); -// } -// -// public void testCancelingKeyboardIfThreadHasFinished(){ -// presentKeyboardOperation.run(); -// presentKeyboardOperation.finishOperation(); -// -// presentKeyboardOperation.dismissKeyboard(); -// -// verify(internalInterface, never()).sendRPC(any(CancelInteraction.class)); -// -// assertFalse(presentKeyboardOperation.isExecuting()); -// assertTrue(presentKeyboardOperation.isFinished()); -// } -// -// public void testCancelingKeyboardIfThreadHasNotYetRun(){ -// presentKeyboardOperation.dismissKeyboard(); -// -// // Make sure neither a `CancelInteraction` or `PerformInteraction` RPC is sent -// verify(internalInterface, never()).sendRPC(any(CancelInteraction.class)); -// verify(internalInterface, never()).sendRPC(any(PerformInteraction.class)); -// -// assertFalse(presentKeyboardOperation.isExecuting()); -// assertFalse(presentKeyboardOperation.isFinished()); -// } -// -// public void testCancelingChoiceSetIfHeadUnitDoesNotSupportFeature(){ -// // Only supported with RPC spec versions 6.0.0+ -// presentKeyboardOperation.sdlMsgVersion = new SdlMsgVersion(5, 3); -// presentKeyboardOperation.run(); -// -// assertTrue(presentKeyboardOperation.isExecuting()); -// assertFalse(presentKeyboardOperation.isFinished()); -// -// presentKeyboardOperation.dismissKeyboard(); -// -// verify(internalInterface, never()).sendRPC(any(CancelInteraction.class)); -// } -// -// public void testCancelingChoiceSetIfHeadUnitDoesNotSupportFeatureButThreadIsNotRunning(){ -// // Only supported with RPC spec versions 6.0.0+ -// presentKeyboardOperation.sdlMsgVersion = new SdlMsgVersion(5, 3); -// -// presentKeyboardOperation.dismissKeyboard(); -// -// verify(internalInterface, never()).sendRPC(any(CancelInteraction.class)); -// -// // Once the thread has started -// presentKeyboardOperation.run(); -// -// // Make sure neither a `CancelInteraction` or `PerformInteraction` RPC is sent -// verify(internalInterface, never()).sendRPC(any(CancelInteraction.class)); -// verify(internalInterface, never()).sendRPC(any(PerformInteraction.class)); -// -// assertFalse(presentKeyboardOperation.isExecuting()); -// assertTrue(presentKeyboardOperation.isFinished()); -// } + public void testCancelingChoiceSetIfHeadUnitDoesNotSupportFeatureButThreadIsNotRunning(){ + // Cancel Interaction is only supported on RPC specs v.6.0.0+ + when(internalInterface.getSdlMsgVersion()).thenReturn(new SdlMsgVersion(5, 3)); + presentKeyboardOperation = new PresentKeyboardOperation(internalInterface, null, "Test", null, null, Test.GENERAL_INTEGER); + + assertFalse(presentKeyboardOperation.isExecuting()); + assertFalse(presentKeyboardOperation.isFinished()); + assertFalse(presentKeyboardOperation.isCancelled()); + + presentKeyboardOperation.dismissKeyboard(); + + verify(internalInterface, never()).sendRPC(any(CancelInteraction.class)); + + // Once the operation has started + executor.execute(presentKeyboardOperation); + try { + executor.awaitTermination(1, TimeUnit.SECONDS); + } catch (InterruptedException e) {} + + assertFalse(presentKeyboardOperation.isExecuting()); + assertTrue(presentKeyboardOperation.isFinished()); + assertFalse(presentKeyboardOperation.isCancelled()); + + // Make sure neither a `CancelInteraction` or `PerformInteraction` RPC is ever sent + verify(internalInterface, never()).sendRPC(any(CancelInteraction.class)); + verify(internalInterface, never()).sendRPC(any(PerformInteraction.class)); + } } diff --git a/base/src/main/java/com/smartdevicelink/managers/screen/choiceset/AsynchronousOperation.java b/base/src/main/java/com/smartdevicelink/managers/screen/choiceset/AsynchronousOperation.java index 90f955374..c0a1dbb25 100644 --- a/base/src/main/java/com/smartdevicelink/managers/screen/choiceset/AsynchronousOperation.java +++ b/base/src/main/java/com/smartdevicelink/managers/screen/choiceset/AsynchronousOperation.java @@ -117,6 +117,6 @@ class AsynchronousOperation implements Runnable { @Override public String toString() { - return this.getClass().getSimpleName() + " (OpId: " + System.identityHashCode(this) + ", OpThread:" + thread.getName() + ", currentThread:" + Thread.currentThread().getName() + ", blocked:" + blocked + ", executing:" + executing + ", finished:" + finished + ", cancelled:" + cancelled + ")"; + return this.getClass().getSimpleName() + " (OpId: " + System.identityHashCode(this) + ", OpThread:" + (thread != null ? thread.getName() : "no operating thread") + ", currentThread:" + Thread.currentThread().getName() + ", blocked:" + blocked + ", executing:" + executing + ", finished:" + finished + ", cancelled:" + cancelled + ")"; } } |