Skip to content

Commit 9d47512

Browse files
committed
Add AssertionErrors as per iOS alignment
Make constructor check for valid initialStateName in state list Adjust tests to reflect new behavior
1 parent 053ac1b commit 9d47512

2 files changed

Lines changed: 43 additions & 4 deletions

File tree

android/sdl_android/src/androidTest/java/com/smartdevicelink/managers/screen/SoftButtonManagerTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,7 @@ public void testAssignNonEmptyStateListToSoftButtonObject() {
455455
SoftButtonState softButtonState2 = new SoftButtonState("general_kenobi", "General Kenobi", null);
456456
stateList2.add(softButtonState2);
457457

458-
SoftButtonObject softButtonObject = new SoftButtonObject("general_kenobi", stateList1, "general_kenobi", null);
458+
SoftButtonObject softButtonObject = new SoftButtonObject("general_kenobi", stateList1, "hello_there", null);
459459

460460
softButtonObject.setStates(stateList2);
461461

@@ -477,7 +477,7 @@ public void testAssignSameNameStateListToSoftButtonObject() {
477477
SoftButtonState softButtonState3 = new SoftButtonState("general_kenobi", "General Kenobi Again", null);
478478
stateListDuplicateNames.add(softButtonState3);
479479

480-
SoftButtonObject softButtonObject = new SoftButtonObject("general_kenobi", stateListUnique, "general_kenobi", null);
480+
SoftButtonObject softButtonObject = new SoftButtonObject("general_kenobi", stateListUnique, "hello_there", null);
481481

482482
softButtonObject.setStates(stateListDuplicateNames);
483483

base/src/main/java/com/smartdevicelink/managers/screen/SoftButtonObject.java

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
package com.smartdevicelink.managers.screen;
3333

3434
import androidx.annotation.NonNull;
35+
36+
import com.livio.BuildConfig;
3537
import com.smartdevicelink.managers.file.filetypes.SdlArtwork;
3638
import com.smartdevicelink.proxy.rpc.OnButtonEvent;
3739
import com.smartdevicelink.proxy.rpc.OnButtonPress;
@@ -71,7 +73,7 @@ public class SoftButtonObject implements Cloneable{
7173
*/
7274
public SoftButtonObject(@NonNull String name, @NonNull List<SoftButtonState> states, @NonNull String initialStateName, OnEventListener onEventListener) {
7375

74-
// If the list of states is empty, throw an error with DebugTool and return
76+
/*// If the list of states is empty, throw an error with DebugTool and return
7577
if (states.isEmpty()) {
7678
DebugTool.logError(TAG,"The state list is empty");
7779
return;
@@ -80,8 +82,30 @@ public SoftButtonObject(@NonNull String name, @NonNull List<SoftButtonState> sta
8082
if (hasTwoStatesOfSameName(states)) {
8183
DebugTool.logError(TAG, "Two states have the same name in states list for soft button object");
8284
return;
85+
}*/
86+
87+
boolean repeatedStateNames = hasTwoStatesOfSameName(states);
88+
89+
boolean hasStateWithInitialName = false;
90+
for (SoftButtonState state : states) {
91+
if(state.getName().equals(initialStateName)) {
92+
hasStateWithInitialName = true;
93+
break;
94+
}
8395
}
8496

97+
if (repeatedStateNames) {
98+
DebugTool.logError(TAG, "A SoftButtonObject must have states with different names.");
99+
if (BuildConfig.DEBUG && repeatedStateNames)
100+
throw new AssertionError("A SoftButtonObject must have states with different names.");
101+
return;
102+
}
103+
if (!hasStateWithInitialName) {
104+
DebugTool.logError(TAG, "A SoftButtonObject must have a state with initialStateName.");
105+
if (BuildConfig.DEBUG && !hasStateWithInitialName)
106+
throw new AssertionError("A SoftButtonObject must have a state with initialStateName.");
107+
return;
108+
}
85109
this.name = name;
86110
this.states = states;
87111
this.currentStateName = initialStateName;
@@ -267,7 +291,7 @@ public List<SoftButtonState> getStates() {
267291
* @param states a list of the object's soft button states. <strong>states should be unique for every SoftButtonObject. A SoftButtonState instance cannot be reused for multiple SoftButtonObjects.</strong>
268292
*/
269293
public void setStates(@NonNull List<SoftButtonState> states) {
270-
// If the list of states is empty, throw an error with DebugTool and return
294+
/*// If the list of states is empty, throw an error with DebugTool and return
271295
if (states.isEmpty()) {
272296
DebugTool.logError(TAG,"The state list is empty");
273297
return;
@@ -276,6 +300,21 @@ public void setStates(@NonNull List<SoftButtonState> states) {
276300
if (hasTwoStatesOfSameName(states)) {
277301
DebugTool.logError(TAG, "Two states have the same name in states list for soft button object");
278302
return;
303+
}*/
304+
305+
boolean repeatedStateNames = hasTwoStatesOfSameName(states);
306+
307+
if (repeatedStateNames) {
308+
DebugTool.logError(TAG, "A SoftButtonObject must have states with different names.");
309+
if (BuildConfig.DEBUG && repeatedStateNames)
310+
throw new AssertionError("A SoftButtonObject must have states with different names.");
311+
return;
312+
}
313+
if (states.isEmpty()) {
314+
DebugTool.logError(TAG, "A SoftButtonState list must contain at least one state");
315+
if (BuildConfig.DEBUG && states.isEmpty())
316+
throw new AssertionError("A SoftButtonState list must contain at least one state");
317+
return;
279318
}
280319

281320
this.states = states;

0 commit comments

Comments
 (0)