summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJulian Kast <julian@livio.com>2020-08-20 18:02:40 -0400
committerJulian Kast <julian@livio.com>2020-08-20 18:02:40 -0400
commitd567337df1f59bd9148616eee909e9840324a0f1 (patch)
treeb11b8757f0eda68d474fc506da3d67bf474bbb0f
parent9fd2effd9fb94feb32f23d0a57ca93a2f98adcae (diff)
downloadsdl_android-d567337df1f59bd9148616eee909e9840324a0f1.tar.gz
Added test for operation
-rw-r--r--android/sdl_android/src/androidTest/java/com/smartdevicelink/managers/screen/TextAndGraphicUpdateOperationTest.java117
1 files changed, 111 insertions, 6 deletions
diff --git a/android/sdl_android/src/androidTest/java/com/smartdevicelink/managers/screen/TextAndGraphicUpdateOperationTest.java b/android/sdl_android/src/androidTest/java/com/smartdevicelink/managers/screen/TextAndGraphicUpdateOperationTest.java
index c1c241c99..0973a98f2 100644
--- a/android/sdl_android/src/androidTest/java/com/smartdevicelink/managers/screen/TextAndGraphicUpdateOperationTest.java
+++ b/android/sdl_android/src/androidTest/java/com/smartdevicelink/managers/screen/TextAndGraphicUpdateOperationTest.java
@@ -8,14 +8,18 @@ import androidx.test.ext.junit.runners.AndroidJUnit4;
import com.smartdevicelink.managers.CompletionListener;
import com.smartdevicelink.managers.ManagerUtility;
import com.smartdevicelink.managers.file.FileManager;
+import com.smartdevicelink.managers.file.MultipleFileCompletionListener;
import com.smartdevicelink.managers.file.filetypes.SdlArtwork;
+import com.smartdevicelink.managers.file.filetypes.SdlFile;
import com.smartdevicelink.proxy.RPCRequest;
import com.smartdevicelink.proxy.interfaces.ISdl;
+import com.smartdevicelink.proxy.rpc.ImageField;
import com.smartdevicelink.proxy.rpc.Show;
import com.smartdevicelink.proxy.rpc.ShowResponse;
import com.smartdevicelink.proxy.rpc.TextField;
import com.smartdevicelink.proxy.rpc.WindowCapability;
import com.smartdevicelink.proxy.rpc.enums.FileType;
+import com.smartdevicelink.proxy.rpc.enums.ImageFieldName;
import com.smartdevicelink.proxy.rpc.enums.MetadataType;
import com.smartdevicelink.proxy.rpc.enums.TextAlignment;
import com.smartdevicelink.proxy.rpc.enums.TextFieldName;
@@ -34,6 +38,9 @@ import static junit.framework.TestCase.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
@RunWith(AndroidJUnit4.class)
public class TextAndGraphicUpdateOperationTest {
@@ -49,6 +56,7 @@ public class TextAndGraphicUpdateOperationTest {
private Show currentScreenData;
private CompletionListener listener;
ISdl internalInterface;
+ FileManager fileManager;
private Answer<Void> onShowSuccess = new Answer<Void>() {
@Override
@@ -65,13 +73,40 @@ public class TextAndGraphicUpdateOperationTest {
}
};
+ private Answer<Void> onShowSuccessCanceled = new Answer<Void>() {
+ @Override
+ public Void answer(InvocationOnMock invocation) {
+ Object[] args = invocation.getArguments();
+ RPCRequest message = (RPCRequest) args[0];
+ if (message instanceof Show) {
+ int correlationId = message.getCorrelationID();
+ ShowResponse showResponse = new ShowResponse();
+ showResponse.setSuccess(true);
+ message.getOnRPCResponseListener().onResponse(correlationId, showResponse);
+ }
+ return null;
+ }
+ };
+
+
+ private Answer<Void> onImageUploadSuccessTaskCanceled = new Answer<Void>() {
+ @Override
+ public Void answer(InvocationOnMock invocation) throws Throwable {
+ Object[] args = invocation.getArguments();
+ MultipleFileCompletionListener listener = (MultipleFileCompletionListener) args[1];
+ textAndGraphicUpdateOperation.cancelTask();
+ listener.onComplete(null);
+ return null;
+ }
+ };
+
@Before
public void setUp() throws Exception {
Context mTestContext = getInstrumentation().getContext();
// mock things
internalInterface = mock(ISdl.class);
- FileManager fileManager = mock(FileManager.class);
+ fileManager = mock(FileManager.class);
setUpCompletionListener();
textField1 = "It is";
textField2 = "wednesday";
@@ -87,26 +122,27 @@ public class TextAndGraphicUpdateOperationTest {
textAlignment = TextAlignment.CENTERED;
+
testArtwork1 = new SdlArtwork();
- testArtwork1.setName("testFile");
+ testArtwork1.setName("testFile1");
Uri uri1 = Uri.parse("android.resource://" + mTestContext.getPackageName() + "/drawable/ic_sdl");
testArtwork1.setUri(uri1);
testArtwork1.setType(FileType.GRAPHIC_PNG);
testArtwork2 = new SdlArtwork();
- testArtwork2.setName("testFile");
+ testArtwork2.setName("testFile2");
Uri uri2 = Uri.parse("android.resource://" + mTestContext.getPackageName() + "/drawable/ic_sdl");
testArtwork2.setUri(uri2);
testArtwork2.setType(FileType.GRAPHIC_PNG);
testArtwork3 = new SdlArtwork();
- testArtwork3.setName("testFile");
+ testArtwork3.setName("testFile3");
Uri uri3 = Uri.parse("android.resource://" + mTestContext.getPackageName() + "/drawable/ic_sdl");
testArtwork3.setUri(uri3);
testArtwork3.setType(FileType.GRAPHIC_PNG);
testArtwork4 = new SdlArtwork();
- testArtwork4.setName("testFile");
+ testArtwork4.setName("testFile4");
Uri uri4 = Uri.parse("android.resource://" + mTestContext.getPackageName() + "/drawable/ic_sdl");
testArtwork4.setUri(uri4);
testArtwork4.setType(FileType.GRAPHIC_PNG);
@@ -167,6 +203,17 @@ public class TextAndGraphicUpdateOperationTest {
WindowCapability windowCapability = new WindowCapability();
windowCapability.setTextFields(returnList);
+ ImageField imageField = new ImageField();
+ imageField.setName(ImageFieldName.graphic);
+ ImageField imageField2 = new ImageField();
+ imageField2.setName(ImageFieldName.secondaryGraphic);
+ List<ImageField> imageFieldList = new ArrayList<>();
+ imageFieldList.add(imageField);
+ imageFieldList.add(imageField2);
+ windowCapability.setImageFields(imageFieldList);
+
+ windowCapability.setImageFields(imageFieldList);
+
return windowCapability;
}
@@ -182,11 +229,69 @@ public class TextAndGraphicUpdateOperationTest {
}
@Test
- public void testOnExecute() {
+ public void testUpload() {
doAnswer(onShowSuccess).when(internalInterface).sendRPC(any(Show.class));
+ // doAnswer(onImageUploadSuccess).when(fileManager).uploadArtworks(any(List.class), any(MultipleFileCompletionListener.class));
+ // doAnswer(onHasUploadedFile).when(fileManager).hasUploadedFile(any(SdlFile.class));
+
+ // when(fileManager.hasUploadedFile(any(SdlFile.class))).thenReturn(true);
+ // verify(fileManager.uploadArtwork(any(List.class), any(MultipleFileCompletionListener.class)),1)
+// assertEquals(textAndGraphicUpdateOperation.getCurrentScreenData().getMetadataTags().getMainField1(), textField1Type);
+
+ // Test Images need to be uploaded, sending text and uploading images
+ textAndGraphicUpdateOperation.onExecute();
+ assertEquals(textAndGraphicUpdateOperation.getCurrentScreenData().getMainField1(), textField1);
+ assertEquals(textAndGraphicUpdateOperation.getCurrentScreenData().getMainField2(), textField2);
+ assertEquals(textAndGraphicUpdateOperation.getCurrentScreenData().getMainField3(), textField3);
+ assertEquals(textAndGraphicUpdateOperation.getCurrentScreenData().getMainField4(), textField4);
+ assertEquals(textAndGraphicUpdateOperation.getCurrentScreenData().getAlignment(), textAlignment);
+
+ // Test The files to be updated are already uploaded, send the full show immediately
+ String textField11 = "It's not";
+ when(fileManager.hasUploadedFile(any(SdlFile.class))).thenReturn(true);
+ TextsAndGraphicsState textsAndGraphicsState = new TextsAndGraphicsState(textField11, textField2, textField3, textField4,
+ mediaTrackField, title, testArtwork3, testArtwork4, textAlignment, textField1Type, textField2Type, textField3Type, textField4Type);
+ textAndGraphicUpdateOperation = new TextAndGraphicUpdateOperation(internalInterface, fileManager, defaultMainWindowCapability, currentScreenData, textsAndGraphicsState, listener);
+ textAndGraphicUpdateOperation.onExecute();
+ assertEquals(textAndGraphicUpdateOperation.getCurrentScreenData().getMainField1(), textField11);
+
+ //Test: If there are no images to update, just send the text
+ TextsAndGraphicsState textsAndGraphicsStateNullImages = new TextsAndGraphicsState(textField1, textField2, textField3, textField4,
+ mediaTrackField, title, null, null, textAlignment, textField1Type, textField2Type, textField3Type, textField4Type);
+ textAndGraphicUpdateOperation = new TextAndGraphicUpdateOperation(internalInterface, fileManager, defaultMainWindowCapability, currentScreenData, textsAndGraphicsStateNullImages, listener);
textAndGraphicUpdateOperation.onExecute();
assertEquals(textAndGraphicUpdateOperation.getCurrentScreenData().getMainField1(), textField1);
+
+ // Verifies that uploadArtworks gets called only with the fist textAndGraphicsUpdateOperation.onExecute call
+ verify(fileManager, times(1)).uploadArtworks(any(List.class),any(MultipleFileCompletionListener.class));
+
+ }
+
+ @Test
+ public void testCanceledRightAway() {
+ textAndGraphicUpdateOperation.cancelTask();
+ textAndGraphicUpdateOperation.onExecute();
+ assertEquals(textAndGraphicUpdateOperation.getCurrentScreenData().getMainField1(),"Old");
+ }
+
+ @Test
+ public void testTaskCanceledAfterImageUpload() {
+ doAnswer(onShowSuccess).when(internalInterface).sendRPC(any(Show.class));
+ doAnswer(onImageUploadSuccessTaskCanceled).when(fileManager).uploadArtworks(any(List.class), any(MultipleFileCompletionListener.class));
+
+ // Test Canceled after Image upload
+ textAndGraphicUpdateOperation.onExecute();
+ verify(internalInterface, times(1)).sendRPC(any(Show.class));
+ assertEquals(textAndGraphicUpdateOperation.getCurrentScreenData().getMainField1(),textField1);
+
+ }
+
+ public void testTaskCanceledAfterTextSent() {
+ doAnswer(onShowSuccessCanceled).when(internalInterface).sendRPC(any(Show.class));
+ textAndGraphicUpdateOperation.onExecute();
+ verify(fileManager, times(0)).uploadArtworks(any(List.class),any(MultipleFileCompletionListener.class));
+
}
/**