From ffa3e13e14aec7f2a4a03e8cff9ec44ef33416ce Mon Sep 17 00:00:00 2001 From: Julian Kast Date: Fri, 17 Sep 2021 11:43:56 -0400 Subject: [PATCH 1/3] Fix NPE in SystemCapabilityManager --- .../lifecycle/BaseSystemCapabilityManager.java | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/base/src/main/java/com/smartdevicelink/managers/lifecycle/BaseSystemCapabilityManager.java b/base/src/main/java/com/smartdevicelink/managers/lifecycle/BaseSystemCapabilityManager.java index 54a3430433..e76189632a 100644 --- a/base/src/main/java/com/smartdevicelink/managers/lifecycle/BaseSystemCapabilityManager.java +++ b/base/src/main/java/com/smartdevicelink/managers/lifecycle/BaseSystemCapabilityManager.java @@ -135,12 +135,15 @@ private List createDisplayCapabilityList(DisplayCapabilities // HAX: Issue #1705, Ford Sync bug returning incorrect template name for "NON-MEDIA" (https://github.com/smartdevicelink/sdl_java_suite/issues/1705). List templatesAvailable = display.getTemplatesAvailable(); - for (int i = 0; i < templatesAvailable.size(); i++) { - if (templatesAvailable.get(i).equals("NON_MEDIA")) { - templatesAvailable.set(i, "NON-MEDIA"); - break; + if (templatesAvailable != null) { + for (int i = 0; i < templatesAvailable.size(); i++) { + if (templatesAvailable.get(i).equals("NON_MEDIA")) { + templatesAvailable.set(i, "NON-MEDIA"); + break; + } } } + // copy all available display capabilities defaultWindowCapability.setTemplatesAvailable(templatesAvailable); defaultWindowCapability.setNumCustomPresetsAvailable(display.getNumCustomPresetsAvailable()); From 43eb7cea3f7b1cc56945e93bcd0f647833f633f9 Mon Sep 17 00:00:00 2001 From: Julian Kast Date: Thu, 23 Sep 2021 09:51:26 -0400 Subject: [PATCH 2/3] Fix NPE --- .../managers/lifecycle/BaseSystemCapabilityManager.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/base/src/main/java/com/smartdevicelink/managers/lifecycle/BaseSystemCapabilityManager.java b/base/src/main/java/com/smartdevicelink/managers/lifecycle/BaseSystemCapabilityManager.java index e76189632a..33a778e58f 100644 --- a/base/src/main/java/com/smartdevicelink/managers/lifecycle/BaseSystemCapabilityManager.java +++ b/base/src/main/java/com/smartdevicelink/managers/lifecycle/BaseSystemCapabilityManager.java @@ -137,7 +137,7 @@ private List createDisplayCapabilityList(DisplayCapabilities List templatesAvailable = display.getTemplatesAvailable(); if (templatesAvailable != null) { for (int i = 0; i < templatesAvailable.size(); i++) { - if (templatesAvailable.get(i).equals("NON_MEDIA")) { + if ("NON_MEDIA".equals(templatesAvailable.get(i))) { templatesAvailable.set(i, "NON-MEDIA"); break; } From 196d96e9ddf9575777df736139ce397d1c8d8607 Mon Sep 17 00:00:00 2001 From: Julian Kast Date: Mon, 27 Sep 2021 11:26:56 -0400 Subject: [PATCH 3/3] Add unit test --- .../SystemCapabilityManagerTests.java | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/android/sdl_android/src/androidTest/java/com/smartdevicelink/managers/lifecycle/SystemCapabilityManagerTests.java b/android/sdl_android/src/androidTest/java/com/smartdevicelink/managers/lifecycle/SystemCapabilityManagerTests.java index 65f9c7bb44..03854d3ee7 100644 --- a/android/sdl_android/src/androidTest/java/com/smartdevicelink/managers/lifecycle/SystemCapabilityManagerTests.java +++ b/android/sdl_android/src/androidTest/java/com/smartdevicelink/managers/lifecycle/SystemCapabilityManagerTests.java @@ -32,6 +32,7 @@ import com.smartdevicelink.proxy.rpc.SetDisplayLayoutResponse; import com.smartdevicelink.proxy.rpc.SoftButtonCapabilities; import com.smartdevicelink.proxy.rpc.SystemCapability; +import com.smartdevicelink.proxy.rpc.TextField; import com.smartdevicelink.proxy.rpc.VideoStreamingCapability; import com.smartdevicelink.proxy.rpc.WindowCapability; import com.smartdevicelink.proxy.rpc.WindowTypeCapabilities; @@ -52,6 +53,7 @@ import com.smartdevicelink.proxy.rpc.enums.SpeechCapabilities; import com.smartdevicelink.proxy.rpc.enums.SystemCapabilityType; import com.smartdevicelink.proxy.rpc.enums.SystemContext; +import com.smartdevicelink.proxy.rpc.enums.TextFieldName; import com.smartdevicelink.proxy.rpc.enums.WindowType; import com.smartdevicelink.proxy.rpc.listeners.OnMultipleRequestListener; import com.smartdevicelink.proxy.rpc.listeners.OnRPCListener; @@ -689,6 +691,35 @@ public void testGetAndAddListenerForDisplaysCapability() { verify(internalInterface, times(0)).sendRPC(any(GetSystemCapability.class)); } + /** + * Test to verify that we can get null for templatesAvailable without hitting an NPE and + * test media field conversion for NON_MEDIA to NON-MEDIA for Sync bug. + */ + @Test + public void testMediaFieldConversion() { + SystemCapabilityManager systemCapabilityManager = new SystemCapabilityManager(new InternalSDLInterface()); + + RegisterAppInterfaceResponse raiResponse = new RegisterAppInterfaceResponse(); + DisplayCapabilities displayCapabilities = new DisplayCapabilities(); + displayCapabilities.setGraphicSupported(false); + TextField textField = new TextField(); + textField.setName(TextFieldName.mainField1); + displayCapabilities.setTextFields(Collections.singletonList(textField)); + raiResponse.setDisplayCapabilities(displayCapabilities); + raiResponse.setSuccess(true); + systemCapabilityManager.parseRAIResponse(raiResponse); + + WindowCapability windowCapability = systemCapabilityManager.getDefaultMainWindowCapability(); + assertNull(windowCapability.getTemplatesAvailable()); + + List templates = new ArrayList<>(); + templates.add("NON_MEDIA"); + displayCapabilities.setTemplatesAvailable(templates); + systemCapabilityManager.parseRAIResponse(raiResponse); + windowCapability = systemCapabilityManager.getDefaultMainWindowCapability(); + assertTrue(windowCapability.getTemplatesAvailable().contains("NON-MEDIA")); + } + @Test public void testListConversion() { SystemCapabilityManager systemCapabilityManager = createSampleManager();