diff options
author | Henigan <rheniga1@MGC12Z921DLVCG.fbpld77.ford.com> | 2021-09-21 15:26:19 -0400 |
---|---|---|
committer | Henigan <rheniga1@MGC12Z921DLVCG.fbpld77.ford.com> | 2021-09-21 15:26:19 -0400 |
commit | d1a77c902d7dd76cad3ea0386dea59edb4db0603 (patch) | |
tree | 06dbafedb4eda0d31e436cab272042c394fcc56e /android/sdl_android | |
parent | 49b8180e7f0aad647251380406acae69f8856414 (diff) | |
parent | 470e703dffd3e672cedf1315a0a03e04cc680fe5 (diff) | |
download | sdl_android-d1a77c902d7dd76cad3ea0386dea59edb4db0603.tar.gz |
Merge branch 'develop' into feature/android_12_fixes
Diffstat (limited to 'android/sdl_android')
6 files changed, 496 insertions, 642 deletions
diff --git a/android/sdl_android/src/androidTest/java/com/smartdevicelink/managers/screen/choiceset/ChoiceCellTests.java b/android/sdl_android/src/androidTest/java/com/smartdevicelink/managers/screen/choiceset/ChoiceCellTests.java index 0dde875ea..a761b1f88 100644 --- a/android/sdl_android/src/androidTest/java/com/smartdevicelink/managers/screen/choiceset/ChoiceCellTests.java +++ b/android/sdl_android/src/androidTest/java/com/smartdevicelink/managers/screen/choiceset/ChoiceCellTests.java @@ -61,7 +61,7 @@ public class ChoiceCellTests { choiceCell.setVoiceCommands(TestValues.GENERAL_STRING_LIST); choiceCell.setArtwork(artwork); choiceCell.setSecondaryArtwork(artwork); - choiceCell.setUniqueText(TestValues.GENERAL_STRING); + choiceCell.setUniqueTextId(TestValues.GENERAL_INT); // use getters and assert equality assertEquals(choiceCell.getText(), TestValues.GENERAL_STRING); @@ -71,7 +71,7 @@ public class ChoiceCellTests { assertEquals(choiceCell.getArtwork(), artwork); assertEquals(choiceCell.getSecondaryArtwork(), artwork); assertEquals(choiceCell.getChoiceId(), MAX_ID); - assertEquals(choiceCell.getUniqueText(), TestValues.GENERAL_STRING); + assertEquals(choiceCell.getUniqueTextId(), TestValues.GENERAL_INTEGER); } @Test @@ -121,14 +121,27 @@ public class ChoiceCellTests { choiceCell3.setSecondaryText(TestValues.GENERAL_STRING); choiceCell3.setTertiaryText(TestValues.GENERAL_STRING); - //UniqueText should not be taken into consideration when checking equality - choiceCell.setUniqueText(TestValues.GENERAL_STRING); - choiceCell2.setUniqueText(TestValues.GENERAL_STRING); - choiceCell3.setUniqueText(TestValues.GENERAL_STRING); - // Make sure our overridden method works, even though these are different objects in memory assertTrue(choiceCell.equals(choiceCell2)); assertFalse(choiceCell.equals(choiceCell3)); } + + @Test + public void testGetUniqueCellText() { + ChoiceCell choiceCell = new ChoiceCell("Test"); + ChoiceCell choiceCell2 = new ChoiceCell("Test"); + choiceCell2.setUniqueTextId(2); + ChoiceCell choiceCell3 = new ChoiceCell("Test"); + choiceCell3.setUniqueTextId(3); + + assertEquals((int) choiceCell.getUniqueTextId(), 1); + assertEquals(choiceCell.getUniqueText(), "Test"); + + assertEquals((int) choiceCell2.getUniqueTextId(), 2); + assertEquals(choiceCell2.getUniqueText(), "Test (2)"); + + assertEquals((int) choiceCell3.getUniqueTextId(), 3); + assertEquals(choiceCell3.getUniqueText(), "Test (3)"); + } } diff --git a/android/sdl_android/src/androidTest/java/com/smartdevicelink/managers/screen/choiceset/ChoiceSetManagerTests.java b/android/sdl_android/src/androidTest/java/com/smartdevicelink/managers/screen/choiceset/ChoiceSetManagerTests.java index 81f912361..f26a7a5c5 100644 --- a/android/sdl_android/src/androidTest/java/com/smartdevicelink/managers/screen/choiceset/ChoiceSetManagerTests.java +++ b/android/sdl_android/src/androidTest/java/com/smartdevicelink/managers/screen/choiceset/ChoiceSetManagerTests.java @@ -38,25 +38,21 @@ import androidx.test.ext.junit.runners.AndroidJUnit4; import com.livio.taskmaster.Taskmaster; import com.smartdevicelink.managers.BaseSubManager; +import com.smartdevicelink.managers.CompletionListener; import com.smartdevicelink.managers.ISdl; import com.smartdevicelink.managers.file.FileManager; -import com.smartdevicelink.proxy.rpc.ImageField; import com.smartdevicelink.proxy.rpc.KeyboardCapabilities; import com.smartdevicelink.proxy.rpc.KeyboardLayoutCapability; import com.smartdevicelink.proxy.rpc.KeyboardProperties; import com.smartdevicelink.proxy.rpc.SdlMsgVersion; -import com.smartdevicelink.proxy.rpc.TextField; import com.smartdevicelink.proxy.rpc.WindowCapability; import com.smartdevicelink.proxy.rpc.enums.HMILevel; -import com.smartdevicelink.proxy.rpc.enums.ImageFieldName; import com.smartdevicelink.proxy.rpc.enums.KeyboardInputMask; import com.smartdevicelink.proxy.rpc.enums.KeyboardLayout; import com.smartdevicelink.proxy.rpc.enums.KeypressMode; import com.smartdevicelink.proxy.rpc.enums.Language; import com.smartdevicelink.proxy.rpc.enums.SystemContext; -import com.smartdevicelink.proxy.rpc.enums.TextFieldName; import com.smartdevicelink.proxy.rpc.enums.TriggerSource; -import com.smartdevicelink.test.TestValues; import org.junit.After; import org.junit.Before; @@ -68,13 +64,10 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.HashSet; -import java.util.LinkedHashSet; -import java.util.List; import static junit.framework.TestCase.assertEquals; import static junit.framework.TestCase.assertFalse; import static junit.framework.TestCase.assertNotNull; -import static junit.framework.TestCase.assertNotSame; import static junit.framework.TestCase.assertNull; import static junit.framework.TestCase.assertTrue; import static org.mockito.Mockito.doReturn; @@ -103,16 +96,12 @@ public class ChoiceSetManagerTests { assertEquals(csm.getState(), BaseSubManager.SETTING_UP); assertEquals(csm.currentSystemContext, SystemContext.SYSCTXT_MAIN); assertEquals(csm.currentHMILevel, HMILevel.HMI_NONE); - assertEquals(csm.choiceCellIdMin, 1); - assertEquals(csm.nextChoiceId, 1); assertFalse(csm.isVROptional); assertNotNull(csm.fileManager); assertNotNull(csm.preloadedChoices); - assertNotNull(csm.pendingPreloadChoices); assertNotNull(csm.transactionQueue); assertNotNull(csm.hmiListener); assertNotNull(csm.onDisplayCapabilityListener); - assertNull(csm.pendingPresentOperation); } @After @@ -123,11 +112,8 @@ public class ChoiceSetManagerTests { assertNull(csm.currentHMILevel); assertNull(csm.currentSystemContext); assertNull(csm.defaultMainWindowCapability); - assertNull(csm.pendingPresentationSet); - assertNull(csm.pendingPresentOperation); assertEquals(csm.transactionQueue.getTasksAsList().size(), 0); - assertEquals(csm.nextChoiceId, 1); assertFalse(csm.isVROptional); @@ -160,12 +146,6 @@ public class ChoiceSetManagerTests { ChoiceSet choiceSet1 = new ChoiceSet("test", Collections.<ChoiceCell>emptyList(), choiceSetSelectionListener); assertFalse(csm.setUpChoiceSet(choiceSet1)); - // Identical cells will not be allowed - ChoiceCell cell1 = new ChoiceCell("test"); - ChoiceCell cell2 = new ChoiceCell("test"); - ChoiceSet choiceSet2 = new ChoiceSet("test", Arrays.asList(cell1, cell2), choiceSetSelectionListener); - assertFalse(csm.setUpChoiceSet(choiceSet2)); - // cells that have duplicate text will be allowed if there is another property to make them unique because a unique name will be assigned and used ChoiceCell cell3 = new ChoiceCell("test"); cell3.setSecondaryText("text 1"); @@ -208,103 +188,56 @@ public class ChoiceSetManagerTests { } @Test - public void testUpdateIdsOnChoices() { - + public void preloadChoicesAddsToQueue() { ChoiceCell cell1 = new ChoiceCell("test"); ChoiceCell cell2 = new ChoiceCell("test2"); ChoiceCell cell3 = new ChoiceCell("test3"); - LinkedHashSet<ChoiceCell> cellSet = new LinkedHashSet<>(); + ArrayList<ChoiceCell> cellSet = new ArrayList<>(); cellSet.add(cell1); cellSet.add(cell2); cellSet.add(cell3); - // Cells are initially set to MAX_ID - assertEquals(cell1.getChoiceId(), 2000000000); - assertEquals(cell2.getChoiceId(), 2000000000); - assertEquals(cell3.getChoiceId(), 2000000000); - csm.updateIdsOnChoices(cellSet); - // We are looking for unique IDs - assertNotSame(cell1.getChoiceId(), 2000000000); - assertNotSame(cell2.getChoiceId(), 2000000000); - assertNotSame(cell3.getChoiceId(), 2000000000); - } + csm.preloadChoices(cellSet, new CompletionListener() { + @Override + public void onComplete(boolean success) { - @Test - public void testAddUniqueNamesToCells() { - ChoiceCell cell1 = new ChoiceCell("McDonalds", "1 mile away", null, null, null, null); - ChoiceCell cell2 = new ChoiceCell("McDonalds", "2 mile away", null, null, null, null); - ChoiceCell cell3 = new ChoiceCell("Starbucks", "3 mile away", null, null, null, null); - ChoiceCell cell4 = new ChoiceCell("McDonalds", "4 mile away", null, null, null, null); - ChoiceCell cell5 = new ChoiceCell("Starbucks", "5 mile away", null, null, null, null); - ChoiceCell cell6 = new ChoiceCell("Meijer", "6 mile away", null, null, null, null); - List<ChoiceCell> cellList = new ArrayList<>(); - - cellList.add(cell1); - cellList.add(cell2); - cellList.add(cell3); - cellList.add(cell4); - cellList.add(cell5); - cellList.add(cell6); - - csm.addUniqueNamesToCells(cellList); - - assertEquals(cell1.getUniqueText(), "McDonalds"); - assertEquals(cell2.getUniqueText(), "McDonalds (2)"); - assertEquals(cell3.getUniqueText(), "Starbucks"); - assertEquals(cell4.getUniqueText(), "McDonalds (3)"); - assertEquals(cell5.getUniqueText(), "Starbucks (2)"); - assertEquals(cell6.getUniqueText(), "Meijer"); + } + }); + assertEquals(csm.transactionQueue.getTasksAsList().size(), 1); } @Test - public void testChoicesToBeRemovedFromPendingWithArray() { - - ChoiceCell cell1 = new ChoiceCell("test"); - ChoiceCell cell2 = new ChoiceCell("test2"); - ChoiceCell cell3 = new ChoiceCell("test3"); - - HashSet<ChoiceCell> pendingPreloadSet = new HashSet<>(); - pendingPreloadSet.add(cell1); - pendingPreloadSet.add(cell2); - pendingPreloadSet.add(cell3); - - csm.pendingPreloadChoices.clear(); - csm.pendingPreloadChoices = pendingPreloadSet; - - List<ChoiceCell> choices = new ArrayList<>(); - choices.add(cell2); - - HashSet<ChoiceCell> returnedChoices = csm.choicesToBeRemovedFromPendingWithArray(choices); + public void preloadChoicesQueueEmptyWhenNoChoiceCells() { + ArrayList<ChoiceCell> cellSet = new ArrayList<>(); + csm.preloadChoices(cellSet, new CompletionListener() { + @Override + public void onComplete(boolean success) { - assertEquals(returnedChoices.size(), 1); - for (ChoiceCell cell : returnedChoices) { - assertEquals(cell.getText(), "test2"); - } + } + }); + assertEquals(csm.transactionQueue.getTasksAsList().size(), 0); } @Test - public void testChoicesToBeUploadedWithArray() { - + public void testPreloadChoicesQueueEmptyIfFileManagerNull() { ChoiceCell cell1 = new ChoiceCell("test"); ChoiceCell cell2 = new ChoiceCell("test2"); ChoiceCell cell3 = new ChoiceCell("test3"); + ArrayList<ChoiceCell> cellSet = new ArrayList<>(); + cellSet.add(cell1); + cellSet.add(cell2); + cellSet.add(cell3); - HashSet<ChoiceCell> pendingDeleteSet = new HashSet<>(); - pendingDeleteSet.add(cell1); - pendingDeleteSet.add(cell2); - pendingDeleteSet.add(cell3); - - csm.preloadedChoices.clear(); - csm.preloadedChoices = pendingDeleteSet; - - List<ChoiceCell> choices = new ArrayList<>(); - choices.add(cell2); - - HashSet<ChoiceCell> returnedChoices = csm.choicesToBeDeletedWithArray(choices); + ISdl internalInterface = mock(ISdl.class); + when(internalInterface.getTaskmaster()).thenReturn(taskmaster); + FileManager fileManager = null; + ChoiceSetManager newCSM = new ChoiceSetManager(internalInterface, fileManager); + newCSM.preloadChoices(cellSet, new CompletionListener() { + @Override + public void onComplete(boolean success) { - assertEquals(returnedChoices.size(), 1); - for (ChoiceCell cell : returnedChoices) { - assertEquals(cell.getText(), "test2"); - } + } + }); + assertEquals(csm.transactionQueue.getTasksAsList().size(), 0); } @Test @@ -474,51 +407,4 @@ public class ChoiceSetManagerTests { verify(testKeyboardOp, times(0)).dismissKeyboard(); verify(testKeyboardOp2, times(1)).dismissKeyboard(); } - - @Test - public void testUniquenessForAvailableFields() { - WindowCapability windowCapability = new WindowCapability(); - TextField secondaryText = new TextField(); - secondaryText.setName(TextFieldName.secondaryText); - TextField tertiaryText = new TextField(); - tertiaryText.setName(TextFieldName.tertiaryText); - - List<TextField> textFields = new ArrayList<>(); - textFields.add(secondaryText); - textFields.add(tertiaryText); - windowCapability.setTextFields(textFields); - - ImageField choiceImage = new ImageField(); - choiceImage.setName(ImageFieldName.choiceImage); - ImageField choiceSecondaryImage = new ImageField(); - choiceSecondaryImage.setName(ImageFieldName.choiceSecondaryImage); - List<ImageField> imageFieldList = new ArrayList<>(); - imageFieldList.add(choiceImage); - imageFieldList.add(choiceSecondaryImage); - windowCapability.setImageFields(imageFieldList); - - csm.defaultMainWindowCapability = windowCapability; - - ChoiceCell cell1 = new ChoiceCell("Item 1", "null", "tertiaryText", null, TestValues.GENERAL_ARTWORK, TestValues.GENERAL_ARTWORK); - ChoiceCell cell2 = new ChoiceCell("Item 1", "null2", "tertiaryText2", null, null, null); - List<ChoiceCell> choiceCellList = new ArrayList<>(); - choiceCellList.add(cell1); - choiceCellList.add(cell2); - - List<ChoiceCell> removedProperties = csm.removeUnusedProperties(choiceCellList); - assertNotNull(removedProperties.get(0).getSecondaryText()); - - textFields.remove(secondaryText); - textFields.remove(tertiaryText); - imageFieldList.remove(choiceImage); - imageFieldList.remove(choiceSecondaryImage); - - removedProperties = csm.removeUnusedProperties(choiceCellList); - csm.addUniqueNamesBasedOnStrippedCells(removedProperties, choiceCellList); - assertEquals(choiceCellList.get(1).getUniqueText(), "Item 1 (2)"); - - - } - - } diff --git a/android/sdl_android/src/androidTest/java/com/smartdevicelink/managers/screen/choiceset/DeleteChoicesOperationTests.java b/android/sdl_android/src/androidTest/java/com/smartdevicelink/managers/screen/choiceset/DeleteChoicesOperationTests.java index 2b472eda8..fdb141b07 100644 --- a/android/sdl_android/src/androidTest/java/com/smartdevicelink/managers/screen/choiceset/DeleteChoicesOperationTests.java +++ b/android/sdl_android/src/androidTest/java/com/smartdevicelink/managers/screen/choiceset/DeleteChoicesOperationTests.java @@ -66,7 +66,7 @@ public class DeleteChoicesOperationTests { cellsToDelete.add(cell2); ISdl internalInterface = mock(ISdl.class); - deleteChoicesOperation = new DeleteChoicesOperation(internalInterface, cellsToDelete, null); + deleteChoicesOperation = new DeleteChoicesOperation(internalInterface, cellsToDelete, null, null); } diff --git a/android/sdl_android/src/androidTest/java/com/smartdevicelink/managers/screen/choiceset/PreloadChoicesOperationTests.java b/android/sdl_android/src/androidTest/java/com/smartdevicelink/managers/screen/choiceset/PreloadChoicesOperationTests.java deleted file mode 100644 index 9e879a73a..000000000 --- a/android/sdl_android/src/androidTest/java/com/smartdevicelink/managers/screen/choiceset/PreloadChoicesOperationTests.java +++ /dev/null @@ -1,193 +0,0 @@ -/* - * Copyright (c) 2019 Livio, Inc. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * Redistributions of source code must retain the above copyright notice, this - * list of conditions and the following disclaimer. - * - * Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following - * disclaimer in the documentation and/or other materials provided with the - * distribution. - * - * Neither the name of the Livio Inc. nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - * - * Created by brettywhite on 6/12/19 1:52 PM - * - */ - -package com.smartdevicelink.managers.screen.choiceset; - -import androidx.test.ext.junit.runners.AndroidJUnit4; - -import com.smartdevicelink.managers.ISdl; -import com.smartdevicelink.managers.file.FileManager; -import com.smartdevicelink.managers.file.filetypes.SdlArtwork; -import com.smartdevicelink.managers.file.filetypes.SdlFile; -import com.smartdevicelink.proxy.rpc.ImageField; -import com.smartdevicelink.proxy.rpc.TextField; -import com.smartdevicelink.proxy.rpc.WindowCapability; -import com.smartdevicelink.proxy.rpc.enums.CharacterSet; -import com.smartdevicelink.proxy.rpc.enums.FileType; -import com.smartdevicelink.proxy.rpc.enums.ImageFieldName; -import com.smartdevicelink.proxy.rpc.enums.ImageType; -import com.smartdevicelink.proxy.rpc.enums.TextFieldName; -import com.smartdevicelink.test.TestValues; - -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; - -import java.util.Arrays; -import java.util.Collections; -import java.util.HashSet; -import java.util.LinkedHashSet; -import java.util.List; - -import static junit.framework.TestCase.assertEquals; -import static junit.framework.TestCase.assertFalse; -import static junit.framework.TestCase.assertNotNull; -import static junit.framework.TestCase.assertTrue; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; - -@RunWith(AndroidJUnit4.class) -public class PreloadChoicesOperationTests { - - private PreloadChoicesOperation preloadChoicesOperation; - private PreloadChoicesOperation preloadChoicesOperationNullCapability; - private PreloadChoicesOperation preloadChoicesOperationEmptyCapability; - - - @Before - public void setUp() throws Exception { - - ChoiceCell cell1 = new ChoiceCell("cell 1"); - ChoiceCell cell2 = new ChoiceCell("cell 2", null, TestValues.GENERAL_ARTWORK); - LinkedHashSet<ChoiceCell> cellsToPreload = new LinkedHashSet<>(); - cellsToPreload.add(cell1); - cellsToPreload.add(cell2); - - ImageField imageField = new ImageField(ImageFieldName.choiceImage, Arrays.asList(FileType.GRAPHIC_PNG, FileType.GRAPHIC_JPEG)); - ImageField imageField2 = new ImageField(); - imageField2.setName(ImageFieldName.choiceSecondaryImage); - TextField textField = new TextField(TextFieldName.menuName, CharacterSet.CID1SET, 2, 2); - - TextField textField2 = new TextField(); - TextField textField3 = new TextField(); - - textField2.setName(TextFieldName.secondaryText); - textField3.setName(TextFieldName.tertiaryText); - - - WindowCapability windowCapability = new WindowCapability(); - windowCapability.setImageFields(Arrays.asList(imageField, imageField2)); - windowCapability.setImageTypeSupported(Arrays.asList(ImageType.STATIC, ImageType.DYNAMIC)); - windowCapability.setTextFields(Arrays.asList(textField, textField2, textField3)); - - ISdl internalInterface = mock(ISdl.class); - FileManager fileManager = mock(FileManager.class); - - // We still want the mock fileManager to use the real implementation for fileNeedsUpload() - when(fileManager.fileNeedsUpload(any(SdlFile.class))).thenCallRealMethod(); - - preloadChoicesOperation = new PreloadChoicesOperation(internalInterface, fileManager, null, windowCapability, true, cellsToPreload, null); - } - - /** - * Sets up PreloadChoicesOperation with WindowCapability being null - */ - public void setUpNullWindowCapability() { - - ChoiceCell cell1 = new ChoiceCell("cell 1"); - ChoiceCell cell2 = new ChoiceCell("cell 2", null, TestValues.GENERAL_ARTWORK); - LinkedHashSet<ChoiceCell> cellsToPreload = new LinkedHashSet<>(); - cellsToPreload.add(cell1); - cellsToPreload.add(cell2); - - ISdl internalInterface = mock(ISdl.class); - FileManager fileManager = mock(FileManager.class); - preloadChoicesOperationNullCapability = new PreloadChoicesOperation(internalInterface, fileManager, null, null, true, cellsToPreload, null); - } - - /** - * Sets up PreloadChoicesOperation with an Capability not being set - * certain imageFields and TextFields - */ - public void setUpEmptyWindowCapability() { - - ChoiceCell cell1 = new ChoiceCell("cell 1"); - ChoiceCell cell2 = new ChoiceCell("cell 2", null, TestValues.GENERAL_ARTWORK); - LinkedHashSet<ChoiceCell> cellsToPreload = new LinkedHashSet<>(); - cellsToPreload.add(cell1); - cellsToPreload.add(cell2); - - ImageField imageField = new ImageField(); - imageField.setName(ImageFieldName.alertIcon); - - TextField textField = new TextField(); - textField.setName(TextFieldName.mainField1); - - WindowCapability windowCapability = new WindowCapability(); - windowCapability.setImageFields(Collections.singletonList(imageField)); - windowCapability.setTextFields(Collections.singletonList(textField)); - - ISdl internalInterface = mock(ISdl.class); - FileManager fileManager = mock(FileManager.class); - preloadChoicesOperationEmptyCapability = new PreloadChoicesOperation(internalInterface, fileManager, null, windowCapability, true, cellsToPreload, null); - } - - @Test - public void testArtworksToUpload() { - List<SdlArtwork> artworksToUpload = preloadChoicesOperation.artworksToUpload(); - assertNotNull(artworksToUpload); - assertEquals(artworksToUpload.size(), 1); - } - - /** - * Testing shouldSend method's with varying WindowCapability set. - */ - @Test - public void testShouldSendText() { - - setUpNullWindowCapability(); - assertTrue(preloadChoicesOperationNullCapability.shouldSendChoicePrimaryImage()); - assertTrue(preloadChoicesOperationNullCapability.shouldSendChoiceSecondaryImage()); - assertTrue(preloadChoicesOperationNullCapability.shouldSendChoiceSecondaryText()); - assertTrue(preloadChoicesOperationNullCapability.shouldSendChoiceTertiaryText()); - assertTrue(preloadChoicesOperationNullCapability.shouldSendChoiceText()); - - - assertTrue(preloadChoicesOperation.shouldSendChoicePrimaryImage()); - assertTrue(preloadChoicesOperation.shouldSendChoiceSecondaryImage()); - assertTrue(preloadChoicesOperation.shouldSendChoiceSecondaryText()); - assertTrue(preloadChoicesOperation.shouldSendChoiceTertiaryText()); - assertTrue(preloadChoicesOperation.shouldSendChoiceText()); - - setUpEmptyWindowCapability(); - assertFalse(preloadChoicesOperationEmptyCapability.shouldSendChoicePrimaryImage()); - assertFalse(preloadChoicesOperationEmptyCapability.shouldSendChoiceSecondaryImage()); - assertFalse(preloadChoicesOperationEmptyCapability.shouldSendChoiceSecondaryText()); - assertFalse(preloadChoicesOperationEmptyCapability.shouldSendChoiceTertiaryText()); - assertFalse(preloadChoicesOperationEmptyCapability.shouldSendChoiceText()); - } - -} diff --git a/android/sdl_android/src/androidTest/java/com/smartdevicelink/managers/screen/choiceset/PreloadPresentChoicesOperationTests.java b/android/sdl_android/src/androidTest/java/com/smartdevicelink/managers/screen/choiceset/PreloadPresentChoicesOperationTests.java new file mode 100644 index 000000000..0f3725f53 --- /dev/null +++ b/android/sdl_android/src/androidTest/java/com/smartdevicelink/managers/screen/choiceset/PreloadPresentChoicesOperationTests.java @@ -0,0 +1,443 @@ +package com.smartdevicelink.managers.screen.choiceset; + +import androidx.test.ext.junit.runners.AndroidJUnit4; + +import com.livio.taskmaster.Queue; +import com.livio.taskmaster.Task; +import com.livio.taskmaster.Taskmaster; +import com.smartdevicelink.managers.ISdl; +import com.smartdevicelink.managers.file.FileManager; +import com.smartdevicelink.managers.file.filetypes.SdlArtwork; +import com.smartdevicelink.managers.file.filetypes.SdlFile; +import com.smartdevicelink.protocol.enums.FunctionID; +import com.smartdevicelink.proxy.RPCResponse; +import com.smartdevicelink.proxy.rpc.CancelInteraction; +import com.smartdevicelink.proxy.rpc.ImageField; +import com.smartdevicelink.proxy.rpc.KeyboardProperties; +import com.smartdevicelink.proxy.rpc.PerformInteraction; +import com.smartdevicelink.proxy.rpc.SdlMsgVersion; +import com.smartdevicelink.proxy.rpc.TextField; +import com.smartdevicelink.proxy.rpc.WindowCapability; +import com.smartdevicelink.proxy.rpc.enums.CharacterSet; +import com.smartdevicelink.proxy.rpc.enums.FileType; +import com.smartdevicelink.proxy.rpc.enums.ImageFieldName; +import com.smartdevicelink.proxy.rpc.enums.ImageType; +import com.smartdevicelink.proxy.rpc.enums.InteractionMode; +import com.smartdevicelink.proxy.rpc.enums.KeyboardLayout; +import com.smartdevicelink.proxy.rpc.enums.KeypressMode; +import com.smartdevicelink.proxy.rpc.enums.Language; +import com.smartdevicelink.proxy.rpc.enums.LayoutMode; +import com.smartdevicelink.proxy.rpc.enums.TextFieldName; +import com.smartdevicelink.test.TestValues; +import com.smartdevicelink.util.Version; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.invocation.InvocationOnMock; +import org.mockito.stubbing.Answer; + +import java.util.Arrays; +import java.util.Collections; +import java.util.HashSet; +import java.util.LinkedHashSet; + +import static junit.framework.TestCase.assertEquals; +import static junit.framework.TestCase.assertFalse; +import static junit.framework.TestCase.assertNotNull; +import static junit.framework.TestCase.assertNull; +import static junit.framework.TestCase.assertTrue; +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; + +@RunWith(AndroidJUnit4.class) +public class PreloadPresentChoicesOperationTests { + + private PreloadPresentChoicesOperation preloadChoicesOperation; + private PreloadPresentChoicesOperation preloadChoicesOperationNullCapability; + private PreloadPresentChoicesOperation preloadChoicesOperationEmptyCapability; + + private PreloadPresentChoicesOperation presentChoicesOperation; + private ChoiceSet choiceSet; + private ISdl internalInterface; + private FileManager fileManager; + private KeyboardListener keyboardListener; + private ChoiceSetSelectionListener choiceSetSelectionListener; + + private Taskmaster taskmaster; + private Queue queue; + + @Before + public void setUp() throws Exception { + ChoiceCell cell1 = new ChoiceCell("cell 1"); + ChoiceCell cell2 = new ChoiceCell("cell 2", null, TestValues.GENERAL_ARTWORK); + LinkedHashSet<ChoiceCell> cellsToPreload = new LinkedHashSet<>(); + cellsToPreload.add(cell1); + cellsToPreload.add(cell2); + + ImageField imageField = new ImageField(ImageFieldName.choiceImage, Arrays.asList(FileType.GRAPHIC_PNG, FileType.GRAPHIC_JPEG)); + ImageField imageField2 = new ImageField(); + imageField2.setName(ImageFieldName.choiceSecondaryImage); + TextField textField = new TextField(TextFieldName.menuName, CharacterSet.CID1SET, 2, 2); + + TextField textField2 = new TextField(); + TextField textField3 = new TextField(); + + textField2.setName(TextFieldName.secondaryText); + textField3.setName(TextFieldName.tertiaryText); + + + WindowCapability windowCapability = new WindowCapability(); + windowCapability.setImageFields(Arrays.asList(imageField, imageField2)); + windowCapability.setImageTypeSupported(Arrays.asList(ImageType.STATIC, ImageType.DYNAMIC)); + windowCapability.setTextFields(Arrays.asList(textField, textField2, textField3)); + + internalInterface = mock(ISdl.class); + fileManager = mock(FileManager.class); + + // We still want the mock fileManager to use the real implementation for fileNeedsUpload() + when(fileManager.fileNeedsUpload(any(SdlFile.class))).thenCallRealMethod(); + + preloadChoicesOperation = new PreloadPresentChoicesOperation(internalInterface, fileManager, null, windowCapability, true, cellsToPreload, null, null); + + keyboardListener = mock(KeyboardListener.class); + choiceSetSelectionListener = mock(ChoiceSetSelectionListener.class); + + ChoiceCell cell = new ChoiceCell("Cell1"); + cell.setChoiceId(0); + choiceSet = new ChoiceSet("Test", Collections.singletonList(cell), choiceSetSelectionListener); + + taskmaster = new Taskmaster.Builder().build(); + queue = taskmaster.createQueue("test", 100, false); + taskmaster.start(); + } + + private KeyboardProperties getKeyBoardProperties() { + KeyboardProperties properties = new KeyboardProperties(); + properties.setLanguage(Language.EN_US); + properties.setKeyboardLayout(KeyboardLayout.QWERTZ); + properties.setKeypressMode(KeypressMode.RESEND_CURRENT_ENTRY); + return properties; + } + + /** + * Sets up PreloadChoicesOperation with WindowCapability being null + */ + public void setUpNullWindowCapability() { + + ChoiceCell cell1 = new ChoiceCell("cell 1"); + ChoiceCell cell2 = new ChoiceCell("cell 2", null, TestValues.GENERAL_ARTWORK); + LinkedHashSet<ChoiceCell> cellsToPreload = new LinkedHashSet<>(); + cellsToPreload.add(cell1); + cellsToPreload.add(cell2); + + ISdl internalInterface = mock(ISdl.class); + preloadChoicesOperationNullCapability = new PreloadPresentChoicesOperation(internalInterface, fileManager, null, null, true, cellsToPreload, null, null); + } + + /** + * Sets up PreloadChoicesOperation with an Capability not being set + * certain imageFields and TextFields + */ + public void setUpEmptyWindowCapability() { + + ChoiceCell cell1 = new ChoiceCell("cell 1"); + ChoiceCell cell2 = new ChoiceCell("cell 2", null, TestValues.GENERAL_ARTWORK); + LinkedHashSet<ChoiceCell> cellsToPreload = new LinkedHashSet<>(); + cellsToPreload.add(cell1); + cellsToPreload.add(cell2); + + ImageField imageField = new ImageField(); + imageField.setName(ImageFieldName.alertIcon); + + TextField textField = new TextField(); + textField.setName(TextFieldName.mainField1); + + WindowCapability windowCapability = new WindowCapability(); + windowCapability.setImageFields(Collections.singletonList(imageField)); + windowCapability.setTextFields(Collections.singletonList(textField)); + + ISdl internalInterface = mock(ISdl.class); + preloadChoicesOperationEmptyCapability = new PreloadPresentChoicesOperation(internalInterface, fileManager, null, windowCapability, true, cellsToPreload, null, null); + } + + @Test + public void testArtworksToUpload() { + HashSet<SdlArtwork> artworksToUpload = preloadChoicesOperation.artworksToUpload(); + assertNotNull(artworksToUpload); + assertEquals(artworksToUpload.size(), 1); + } + + /** + * Testing shouldSend method's with varying WindowCapability set. + */ + @Test + public void testShouldSendText() { + + setUpNullWindowCapability(); + assertTrue(preloadChoicesOperationNullCapability.shouldSendChoicePrimaryImage()); + assertTrue(preloadChoicesOperationNullCapability.shouldSendChoiceSecondaryImage()); + assertTrue(preloadChoicesOperationNullCapability.shouldSendChoiceSecondaryText()); + assertTrue(preloadChoicesOperationNullCapability.shouldSendChoiceTertiaryText()); + assertTrue(preloadChoicesOperationNullCapability.shouldSendChoiceText()); + + + assertTrue(preloadChoicesOperation.shouldSendChoicePrimaryImage()); + assertTrue(preloadChoicesOperation.shouldSendChoiceSecondaryImage()); + assertTrue(preloadChoicesOperation.shouldSendChoiceSecondaryText()); + assertTrue(preloadChoicesOperation.shouldSendChoiceTertiaryText()); + assertTrue(preloadChoicesOperation.shouldSendChoiceText()); + + setUpEmptyWindowCapability(); + assertFalse(preloadChoicesOperationEmptyCapability.shouldSendChoicePrimaryImage()); + assertFalse(preloadChoicesOperationEmptyCapability.shouldSendChoiceSecondaryImage()); + assertFalse(preloadChoicesOperationEmptyCapability.shouldSendChoiceSecondaryText()); + assertFalse(preloadChoicesOperationEmptyCapability.shouldSendChoiceTertiaryText()); + assertFalse(preloadChoicesOperationEmptyCapability.shouldSendChoiceText()); + } + + + @Test + public void testGetLayoutMode() { + when(internalInterface.getSdlMsgVersion()).thenReturn(new SdlMsgVersion(7, 1)); + // First we will check knowing our keyboard listener is NOT NULL + WindowCapability windowCapability = new WindowCapability(); + HashSet<ChoiceCell> loadedCells = new HashSet<>(); + presentChoicesOperation = new PreloadPresentChoicesOperation(internalInterface, fileManager, choiceSet, InteractionMode.MANUAL_ONLY, getKeyBoardProperties(), keyboardListener, TestValues.GENERAL_INTEGER, null, windowCapability, true, loadedCells, null, null); + + assertEquals(presentChoicesOperation.getLayoutMode(), LayoutMode.LIST_WITH_SEARCH); + presentChoicesOperation.keyboardListener = null; + assertEquals(presentChoicesOperation.getLayoutMode(), LayoutMode.LIST_ONLY); + } + + @Test + public void testGetPerformInteraction() { + when(internalInterface.getSdlMsgVersion()).thenReturn(new SdlMsgVersion(7, 1)); + WindowCapability windowCapability = new WindowCapability(); + HashSet<ChoiceCell> loadedCells = new HashSet<>(); + presentChoicesOperation = new PreloadPresentChoicesOperation(internalInterface, fileManager, choiceSet, InteractionMode.MANUAL_ONLY, getKeyBoardProperties(), keyboardListener, TestValues.GENERAL_INTEGER, null, windowCapability, true, loadedCells, null, null); + + PerformInteraction pi = presentChoicesOperation.getPerformInteraction(); + assertEquals(pi.getInitialText(), "Test"); + assertNull(pi.getHelpPrompt()); + assertNull(pi.getTimeoutPrompt()); + assertNull(pi.getVrHelp()); + assertEquals(pi.getTimeout(), Integer.valueOf(10000)); + assertEquals(pi.getCancelID(), TestValues.GENERAL_INTEGER); + assertEquals(presentChoicesOperation.getLayoutMode(), LayoutMode.LIST_WITH_SEARCH); + } + + @Test + public void testSetSelectedCellWithId() { + when(internalInterface.getSdlMsgVersion()).thenReturn(new SdlMsgVersion(7, 1)); + WindowCapability windowCapability = new WindowCapability(); + HashSet<ChoiceCell> loadedCells = new HashSet<>(); + presentChoicesOperation = new PreloadPresentChoicesOperation(internalInterface, fileManager, choiceSet, InteractionMode.MANUAL_ONLY, getKeyBoardProperties(), keyboardListener, TestValues.GENERAL_INTEGER, null, windowCapability, true, loadedCells, null, null); + + assertNull(presentChoicesOperation.selectedCellRow); + presentChoicesOperation.setSelectedCellWithId(0); + assertEquals(presentChoicesOperation.selectedCellRow, Integer.valueOf(0)); + } + + private void sleep() { + try { + Thread.sleep(100); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + + @Test + public void testCancelingChoiceSetSuccessfullyIfThreadIsRunning() { + when(internalInterface.getSdlMsgVersion()).thenReturn(new SdlMsgVersion(6, 0)); + WindowCapability windowCapability = new WindowCapability(); + HashSet<ChoiceCell> loadedCells = new HashSet<>(); + BaseChoiceSetManager.ChoicesOperationCompletionListener listener = new BaseChoiceSetManager.ChoicesOperationCompletionListener() { + @Override + public void onComplete(boolean success, HashSet<ChoiceCell> loadedChoiceCells) { + choiceSet.cancel(); + sleep(); + + verify(internalInterface, times(1)).sendRPC(any(CancelInteraction.class)); + verify(internalInterface, times(1)).sendRPC(any(PerformInteraction.class)); + + assertEquals(Task.IN_PROGRESS, presentChoicesOperation.getState()); + } + }; + presentChoicesOperation = new PreloadPresentChoicesOperation(internalInterface, fileManager, choiceSet, InteractionMode.MANUAL_ONLY, null, null, TestValues.GENERAL_INTEGER, null, windowCapability, true, loadedCells, listener, null); + presentChoicesOperation.setLoadedCells(new HashSet<ChoiceCell>()); + queue.add(presentChoicesOperation, false); + + sleep(); + + assertEquals(Task.IN_PROGRESS, presentChoicesOperation.getState()); + + Answer<Void> cancelInteractionAnswer = new Answer<Void>() { + @Override + public Void answer(InvocationOnMock invocation) { + Object[] args = invocation.getArguments(); + CancelInteraction cancelInteraction = (CancelInteraction) args[0]; + + assertEquals(cancelInteraction.getCancelID(), TestValues.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)); + } + + @Test + public void testCancelingChoiceSetUnsuccessfullyIfThreadIsRunning() { + when(internalInterface.getSdlMsgVersion()).thenReturn(new SdlMsgVersion(6, 0)); + WindowCapability windowCapability = new WindowCapability(); + HashSet<ChoiceCell> loadedCells = new HashSet<>(); + BaseChoiceSetManager.ChoicesOperationCompletionListener listener = new BaseChoiceSetManager.ChoicesOperationCompletionListener() { + @Override + public void onComplete(boolean success, HashSet<ChoiceCell> loadedChoiceCells) { + choiceSet.cancel(); + sleep(); + + verify(internalInterface, times(1)).sendRPC(any(CancelInteraction.class)); + verify(internalInterface, times(1)).sendRPC(any(PerformInteraction.class)); + + assertEquals(Task.IN_PROGRESS, presentChoicesOperation.getState()); + } + }; + presentChoicesOperation = new PreloadPresentChoicesOperation(internalInterface, fileManager, choiceSet, InteractionMode.MANUAL_ONLY, null, null, TestValues.GENERAL_INTEGER, null, windowCapability, true, loadedCells, listener, null); + presentChoicesOperation.setLoadedCells(new HashSet<ChoiceCell>()); + queue.add(presentChoicesOperation, false); + sleep(); + + assertEquals(Task.IN_PROGRESS, presentChoicesOperation.getState()); + + Answer<Void> cancelInteractionAnswer = new Answer<Void>() { + @Override + public Void answer(InvocationOnMock invocation) { + Object[] args = invocation.getArguments(); + CancelInteraction cancelInteraction = (CancelInteraction) args[0]; + + assertEquals(cancelInteraction.getCancelID(), TestValues.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)); + } + + @Test + public void testCancelingChoiceSetIfThreadHasFinished() { + when(internalInterface.getSdlMsgVersion()).thenReturn(new SdlMsgVersion(6, 0)); + WindowCapability windowCapability = new WindowCapability(); + HashSet<ChoiceCell> loadedCells = new HashSet<>(); + presentChoicesOperation = new PreloadPresentChoicesOperation(internalInterface, fileManager, choiceSet, InteractionMode.MANUAL_ONLY, null, null, TestValues.GENERAL_INTEGER,null, windowCapability, true, loadedCells, null, null); + presentChoicesOperation.finishOperation(false); + + assertEquals(Task.FINISHED, presentChoicesOperation.getState()); + + choiceSet.cancel(); + verify(internalInterface, never()).sendRPC(any(CancelInteraction.class)); + + assertEquals(Task.FINISHED, presentChoicesOperation.getState()); + } + + @Test + public void testCancelingChoiceSetIfThreadHasCanceled() { + when(internalInterface.getSdlMsgVersion()).thenReturn(new SdlMsgVersion(6, 0)); + WindowCapability windowCapability = new WindowCapability(); + HashSet<ChoiceCell> loadedCells = new HashSet<>(); + presentChoicesOperation = new PreloadPresentChoicesOperation(internalInterface, fileManager, choiceSet, InteractionMode.MANUAL_ONLY, null, null, TestValues.GENERAL_INTEGER,null, windowCapability, true, loadedCells, null, null); + presentChoicesOperation.cancelTask(); + + assertEquals(Task.CANCELED, presentChoicesOperation.getState()); + + choiceSet.cancel(); + verify(internalInterface, never()).sendRPC(any(CancelInteraction.class)); + + assertEquals(Task.CANCELED, presentChoicesOperation.getState()); + } + + @Test + public void testCancelingChoiceSetIfThreadHasNotYetRun() { + when(internalInterface.getSdlMsgVersion()).thenReturn(new SdlMsgVersion(6, 0)); + WindowCapability windowCapability = new WindowCapability(); + HashSet<ChoiceCell> loadedCells = new HashSet<>(); + presentChoicesOperation = new PreloadPresentChoicesOperation(internalInterface, fileManager, choiceSet, InteractionMode.MANUAL_ONLY, null, null, TestValues.GENERAL_INTEGER, null, windowCapability, true, loadedCells, null, null); + + assertEquals(Task.BLOCKED, presentChoicesOperation.getState()); + + choiceSet.cancel(); + + // Once the operation has started + queue.add(presentChoicesOperation, false); + sleep(); + + assertEquals(Task.CANCELED, presentChoicesOperation.getState()); + + // 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)); + } + + @Test + public void testCancelingChoiceSetIfHeadUnitDoesNotSupportFeature() { + // Cancel Interaction is only supported on RPC specs v.6.0.0+ + when(internalInterface.getSdlMsgVersion()).thenReturn(new SdlMsgVersion(5, 3)); + WindowCapability windowCapability = new WindowCapability(); + HashSet<ChoiceCell> loadedCells = new HashSet<>(); + BaseChoiceSetManager.ChoicesOperationCompletionListener listener = new BaseChoiceSetManager.ChoicesOperationCompletionListener() { + @Override + public void onComplete(boolean success, HashSet<ChoiceCell> loadedChoiceCells) { + choiceSet.cancel(); + sleep(); + + assertEquals(Task.IN_PROGRESS, presentChoicesOperation.getState()); + + verify(internalInterface, never()).sendRPC(any(CancelInteraction.class)); + verify(internalInterface, times(1)).sendRPC(any(PerformInteraction.class)); + } + }; + presentChoicesOperation = new PreloadPresentChoicesOperation(internalInterface, fileManager, choiceSet, InteractionMode.MANUAL_ONLY, null, null, TestValues.GENERAL_INTEGER, null, windowCapability, true, loadedCells, listener, null); + presentChoicesOperation.setLoadedCells(new HashSet<ChoiceCell>()); + queue.add(presentChoicesOperation, false); + } + + @Test + public void testCancelingChoiceSetIfHeadUnitDoesNotSupportFeatureButThreadIsNotRunning() { + // Cancel Interaction is only supported on RPC specs v.6.0.0+ + when(internalInterface.getSdlMsgVersion()).thenReturn(new SdlMsgVersion(5, 3)); + WindowCapability windowCapability = new WindowCapability(); + HashSet<ChoiceCell> loadedCells = new HashSet<>(); + presentChoicesOperation = new PreloadPresentChoicesOperation(internalInterface, fileManager, choiceSet, InteractionMode.MANUAL_ONLY, null, null, TestValues.GENERAL_INTEGER, null, windowCapability, true, loadedCells, null, null); + + assertEquals(Task.BLOCKED, presentChoicesOperation.getState()); + + choiceSet.cancel(); + + verify(internalInterface, never()).sendRPC(any(CancelInteraction.class)); + + // Once the operation has started + queue.add(presentChoicesOperation, false); + sleep(); + + assertEquals(Task.CANCELED, presentChoicesOperation.getState()); + + // 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/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 deleted file mode 100644 index 3d22f352b..000000000 --- a/android/sdl_android/src/androidTest/java/com/smartdevicelink/managers/screen/choiceset/PresentChoiceSetOperationTests.java +++ /dev/null @@ -1,295 +0,0 @@ -/* - * Copyright (c) 2019 Livio, Inc. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * Redistributions of source code must retain the above copyright notice, this - * list of conditions and the following disclaimer. - * - * Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following - * disclaimer in the documentation and/or other materials provided with the - * distribution. - * - * Neither the name of the Livio Inc. nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - * - * Created by brettywhite on 6/12/19 1:52 PM - * - */ - -package com.smartdevicelink.managers.screen.choiceset; - -import androidx.test.ext.junit.runners.AndroidJUnit4; - -import com.livio.taskmaster.Queue; -import com.livio.taskmaster.Task; -import com.livio.taskmaster.Taskmaster; -import com.smartdevicelink.managers.ISdl; -import com.smartdevicelink.protocol.enums.FunctionID; -import com.smartdevicelink.proxy.RPCResponse; -import com.smartdevicelink.proxy.rpc.CancelInteraction; -import com.smartdevicelink.proxy.rpc.KeyboardProperties; -import com.smartdevicelink.proxy.rpc.PerformInteraction; -import com.smartdevicelink.proxy.rpc.SdlMsgVersion; -import com.smartdevicelink.proxy.rpc.enums.InteractionMode; -import com.smartdevicelink.proxy.rpc.enums.KeyboardLayout; -import com.smartdevicelink.proxy.rpc.enums.KeypressMode; -import com.smartdevicelink.proxy.rpc.enums.Language; -import com.smartdevicelink.proxy.rpc.enums.LayoutMode; -import com.smartdevicelink.test.TestValues; - -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.invocation.InvocationOnMock; -import org.mockito.stubbing.Answer; - -import java.util.Collections; - -import static junit.framework.TestCase.assertEquals; -import static junit.framework.TestCase.assertNull; -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; - -@RunWith(AndroidJUnit4.class) -public class PresentChoiceSetOperationTests { - - private PresentChoiceSetOperation presentChoiceSetOperation; - private ChoiceSet choiceSet; - private ISdl internalInterface; - private KeyboardListener keyboardListener; - private ChoiceSetSelectionListener choiceSetSelectionListener; - - private Taskmaster taskmaster; - private Queue queue; - - @Before - public void setUp() throws Exception { - - internalInterface = mock(ISdl.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); - - taskmaster = new Taskmaster.Builder().build(); - queue = taskmaster.createQueue("test", 100, false); - taskmaster.start(); - } - - - private KeyboardProperties getKeyBoardProperties() { - KeyboardProperties properties = new KeyboardProperties(); - properties.setLanguage(Language.EN_US); - properties.setKeyboardLayout(KeyboardLayout.QWERTZ); - properties.setKeypressMode(KeypressMode.RESEND_CURRENT_ENTRY); - return properties; - } - - @Test - 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, TestValues.GENERAL_INTEGER); - - assertEquals(presentChoiceSetOperation.getLayoutMode(), LayoutMode.LIST_WITH_SEARCH); - presentChoiceSetOperation.keyboardListener = null; - assertEquals(presentChoiceSetOperation.getLayoutMode(), LayoutMode.LIST_ONLY); - } - - @Test - public void testGetPerformInteraction() { - presentChoiceSetOperation = new PresentChoiceSetOperation(internalInterface, choiceSet, InteractionMode.MANUAL_ONLY, getKeyBoardProperties(), keyboardListener, choiceSetSelectionListener, TestValues.GENERAL_INTEGER); - - PerformInteraction pi = presentChoiceSetOperation.getPerformInteraction(); - assertEquals(pi.getInitialText(), "Test"); - assertNull(pi.getHelpPrompt()); - assertNull(pi.getTimeoutPrompt()); - assertNull(pi.getVrHelp()); - assertEquals(pi.getTimeout(), Integer.valueOf(10000)); - assertEquals(pi.getCancelID(), TestValues.GENERAL_INTEGER); - assertEquals(presentChoiceSetOperation.getLayoutMode(), LayoutMode.LIST_WITH_SEARCH); - } - - @Test - public void testSetSelectedCellWithId() { - presentChoiceSetOperation = new PresentChoiceSetOperation(internalInterface, choiceSet, InteractionMode.MANUAL_ONLY, getKeyBoardProperties(), keyboardListener, choiceSetSelectionListener, TestValues.GENERAL_INTEGER); - - assertNull(presentChoiceSetOperation.selectedCellRow); - presentChoiceSetOperation.setSelectedCellWithId(0); - assertEquals(presentChoiceSetOperation.selectedCellRow, Integer.valueOf(0)); - } - - private void sleep() { - try { - Thread.sleep(100); - } catch (InterruptedException e) { - e.printStackTrace(); - } - } - - @Test - public void testCancelingChoiceSetSuccessfullyIfThreadIsRunning() { - when(internalInterface.getSdlMsgVersion()).thenReturn(new SdlMsgVersion(6, 0)); - presentChoiceSetOperation = new PresentChoiceSetOperation(internalInterface, choiceSet, InteractionMode.MANUAL_ONLY, null, null, choiceSetSelectionListener, TestValues.GENERAL_INTEGER); - queue.add(presentChoiceSetOperation, false); - - sleep(); - - assertEquals(Task.IN_PROGRESS, presentChoiceSetOperation.getState()); - - 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(), TestValues.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)); - - assertEquals(Task.IN_PROGRESS, presentChoiceSetOperation.getState()); - } - - @Test - public void testCancelingChoiceSetUnsuccessfullyIfThreadIsRunning() { - when(internalInterface.getSdlMsgVersion()).thenReturn(new SdlMsgVersion(6, 0)); - presentChoiceSetOperation = new PresentChoiceSetOperation(internalInterface, choiceSet, InteractionMode.MANUAL_ONLY, null, null, choiceSetSelectionListener, TestValues.GENERAL_INTEGER); - queue.add(presentChoiceSetOperation, false); - sleep(); - - assertEquals(Task.IN_PROGRESS, presentChoiceSetOperation.getState()); - - 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(), TestValues.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)); - - assertEquals(Task.IN_PROGRESS, presentChoiceSetOperation.getState()); - } - - @Test - public void testCancelingChoiceSetIfThreadHasFinished() { - when(internalInterface.getSdlMsgVersion()).thenReturn(new SdlMsgVersion(6, 0)); - presentChoiceSetOperation = new PresentChoiceSetOperation(internalInterface, choiceSet, InteractionMode.MANUAL_ONLY, null, null, choiceSetSelectionListener, TestValues.GENERAL_INTEGER); - presentChoiceSetOperation.finishOperation(); - - assertEquals(Task.FINISHED, presentChoiceSetOperation.getState()); - - choiceSet.cancel(); - verify(internalInterface, never()).sendRPC(any(CancelInteraction.class)); - - assertEquals(Task.FINISHED, presentChoiceSetOperation.getState()); - } - - @Test - public void testCancelingChoiceSetIfThreadHasNotYetRun() { - when(internalInterface.getSdlMsgVersion()).thenReturn(new SdlMsgVersion(6, 0)); - presentChoiceSetOperation = new PresentChoiceSetOperation(internalInterface, choiceSet, InteractionMode.MANUAL_ONLY, null, null, choiceSetSelectionListener, TestValues.GENERAL_INTEGER); - - assertEquals(Task.BLOCKED, presentChoiceSetOperation.getState()); - - choiceSet.cancel(); - - // Once the operation has started - queue.add(presentChoiceSetOperation, false); - sleep(); - - assertEquals(Task.CANCELED, presentChoiceSetOperation.getState()); - - // 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)); - } - - @Test - 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, TestValues.GENERAL_INTEGER); - queue.add(presentChoiceSetOperation, false); - sleep(); - - assertEquals(Task.IN_PROGRESS, presentChoiceSetOperation.getState()); - - choiceSet.cancel(); - - verify(internalInterface, never()).sendRPC(any(CancelInteraction.class)); - verify(internalInterface, times(1)).sendRPC(any(PerformInteraction.class)); - } - - @Test - 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, TestValues.GENERAL_INTEGER); - - assertEquals(Task.BLOCKED, presentChoiceSetOperation.getState()); - - choiceSet.cancel(); - - verify(internalInterface, never()).sendRPC(any(CancelInteraction.class)); - - // Once the operation has started - queue.add(presentChoiceSetOperation, false); - sleep(); - - assertEquals(Task.CANCELED, presentChoiceSetOperation.getState()); - - // 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 |