Skip to content

Commit 1d4d523

Browse files
Merge pull request #1307 from smartdevicelink/bugfix/issue_1257_2.0
Add Override Equals method to softButtonObject
2 parents f8a9b32 + 6ff36ac commit 1d4d523

8 files changed

Lines changed: 284 additions & 3 deletions

File tree

android/sdl_android/src/androidTest/java/com/smartdevicelink/managers/file/FileManagerTests.java

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -925,4 +925,60 @@ public void onComplete(Map<String, String> errors) {
925925
});
926926
verify(internalInterface, times(1)).sendRequests(any(List.class), any(OnMultipleRequestListener.class));
927927
}
928+
929+
/**
930+
* Test custom overridden SdlFile equals method
931+
*/
932+
public void testSdlFileEquals() {
933+
// Case 1: object is null, assertFalse
934+
SdlFile artwork1 = new SdlFile("image1", FileType.GRAPHIC_PNG, 1, true);
935+
SdlFile artwork2 = null;
936+
assertFalse(artwork1.equals(artwork2));
937+
938+
// Case 2 SoftButtonObjects are the same, assertTrue
939+
assertTrue(artwork1.equals(artwork1));
940+
941+
// Case 3: object is not an instance of SoftButtonObject, assertFalse
942+
assertFalse(artwork1.equals("Test"));
943+
944+
// Case 4: different StaticIcon status, assertFalse
945+
artwork1.setStaticIcon(true);
946+
artwork2 = new SdlFile("image1", FileType.GRAPHIC_PNG, 1, true);
947+
artwork2.setStaticIcon(false);
948+
assertFalse(artwork1.equals(artwork2));
949+
950+
// Case 5: different Persistent status, assertFalse
951+
artwork1 = new SdlFile("image1", FileType.GRAPHIC_PNG, 1, false);
952+
artwork2 = new SdlFile("image1", FileType.GRAPHIC_PNG, 1, true);
953+
assertFalse(artwork1.equals(artwork2));
954+
955+
// Case 6: different name, assertFalse
956+
artwork2 = new SdlFile("image2", FileType.GRAPHIC_PNG, 1, false);
957+
assertFalse(artwork1.equals(artwork2));
958+
959+
// Case 7: different Uri
960+
Uri uri1 = Uri.parse("testUri1");
961+
Uri uri2 = Uri.parse("testUri2");
962+
artwork1 = new SdlFile("image1", FileType.GRAPHIC_PNG, uri1, false);
963+
artwork2 = new SdlFile("image1", FileType.GRAPHIC_PNG, uri2, false);
964+
assertFalse(artwork1.equals(artwork2));
965+
966+
// Case 8: different FileData
967+
artwork1 = new SdlFile("image1", FileType.GRAPHIC_PNG, 1, false);
968+
artwork2 = new SdlFile("image1", FileType.GRAPHIC_PNG, 1, false);
969+
byte[] GENERAL_BYTE_ARRAY2 = new byte[2];
970+
artwork1.setFileData(Test.GENERAL_BYTE_ARRAY);
971+
artwork2.setFileData(GENERAL_BYTE_ARRAY2);
972+
assertFalse(artwork1.equals(artwork2));
973+
974+
// Case 9 different FileType, assertFalse
975+
artwork1 = new SdlFile("image1", FileType.GRAPHIC_PNG, 1, false);
976+
artwork2 = new SdlFile("image1", FileType.AUDIO_WAVE, 1, false);
977+
assertFalse(artwork1.equals(artwork2));
978+
979+
// Case 10: they are equal, assertTrue
980+
artwork1 = new SdlFile("image1", FileType.GRAPHIC_PNG, 1, false);
981+
artwork2 = new SdlFile("image1", FileType.GRAPHIC_PNG, 1, false);
982+
assertTrue(artwork1.equals(artwork2));
983+
}
928984
}

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

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
import com.smartdevicelink.protocol.enums.FunctionID;
1010
import com.smartdevicelink.proxy.interfaces.ISdl;
1111
import com.smartdevicelink.proxy.rpc.Image;
12+
import com.smartdevicelink.proxy.rpc.OnButtonEvent;
13+
import com.smartdevicelink.proxy.rpc.OnButtonPress;
1214
import com.smartdevicelink.proxy.rpc.OnHMIStatus;
1315
import com.smartdevicelink.proxy.rpc.Show;
1416
import com.smartdevicelink.proxy.rpc.SoftButton;
@@ -25,6 +27,7 @@
2527
import org.mockito.invocation.InvocationOnMock;
2628
import org.mockito.stubbing.Answer;
2729

30+
import java.util.ArrayList;
2831
import java.util.Arrays;
2932
import java.util.Collections;
3033
import java.util.List;
@@ -305,4 +308,101 @@ public void testAssigningIdsToSoftButtonObjects() {
305308
assertEquals("SoftButtonObject id doesn't match the expected value", 100, sbo4.getButtonId());
306309
assertEquals("SoftButtonObject id doesn't match the expected value", 103, sbo5.getButtonId());
307310
}
311+
312+
/**
313+
* Test custom overridden softButtonObject equals method
314+
*/
315+
public void testSoftButtonObjectEquals() {
316+
SoftButtonObject softButtonObject1;
317+
SoftButtonObject softButtonObject2;
318+
319+
SoftButtonObject.OnEventListener testOnEventList1 = new SoftButtonObject.OnEventListener() {
320+
@Override
321+
public void onPress(SoftButtonObject softButtonObject, OnButtonPress onButtonPress) {
322+
}
323+
324+
@Override
325+
public void onEvent(SoftButtonObject softButtonObject, OnButtonEvent onButtonEvent) {
326+
}
327+
};
328+
329+
SoftButtonObject.OnEventListener testOnEventList2 = new SoftButtonObject.OnEventListener() {
330+
@Override
331+
public void onPress(SoftButtonObject softButtonObject, OnButtonPress onButtonPress) {
332+
}
333+
334+
@Override
335+
public void onEvent(SoftButtonObject softButtonObject, OnButtonEvent onButtonEvent) {
336+
}
337+
};
338+
339+
// Case 1: object is null, assertFalse
340+
softButtonObject1 = new SoftButtonObject("test", softButtonState1, null);
341+
softButtonObject2 = null;
342+
assertFalse(softButtonObject1.equals(softButtonObject2));
343+
344+
// Case 2 SoftButtonObjects are the same, assertTrue
345+
assertTrue(softButtonObject1.equals(softButtonObject1));
346+
347+
// Case 3: object is not an instance of SoftButtonObject assertFalse
348+
SdlArtwork artwork = new SdlArtwork("image1", FileType.GRAPHIC_PNG, 1, true);
349+
assertFalse(softButtonObject1.equals(artwork));
350+
351+
// Case 4: SoftButtonObjectState List are not same size, assertFalse
352+
List<SoftButtonState> softButtonStateList = new ArrayList<>();
353+
List<SoftButtonState> softButtonStateList2 = new ArrayList<>();
354+
softButtonStateList.add(softButtonState1);
355+
softButtonStateList2.add(softButtonState1);
356+
softButtonStateList2.add(softButtonState2);
357+
softButtonObject1 = new SoftButtonObject("hi", softButtonStateList, "Hi", null);
358+
softButtonObject2 = new SoftButtonObject("hi", softButtonStateList2, "Hi", null);
359+
assertFalse(softButtonObject1.equals(softButtonObject2));
360+
361+
// Case 5: SoftButtonStates are not the same, assertFalse
362+
softButtonObject1 = new SoftButtonObject("test", softButtonState1, null);
363+
softButtonObject2 = new SoftButtonObject("test", softButtonState2, null);
364+
assertFalse(softButtonObject1.equals(softButtonObject2));
365+
366+
// Case 6: SoftButtonObject names are not same, assertFalse
367+
softButtonObject1 = new SoftButtonObject("test", softButtonState1, null);
368+
softButtonObject2 = new SoftButtonObject("test23123", softButtonState1, null);
369+
assertFalse(softButtonObject1.equals(softButtonObject2));
370+
371+
// Case 7: SoftButtonObject currentStateName not same, assertFalse
372+
softButtonObject1 = new SoftButtonObject("hi", softButtonStateList, "Hi", null);
373+
softButtonObject2 = new SoftButtonObject("hi", softButtonStateList, "Hi2", null);
374+
assertFalse(softButtonObject1.equals(softButtonObject2));
375+
}
376+
377+
/**
378+
* Test custom overridden softButtonState equals method
379+
*/
380+
public void testSoftButtonStateEquals() {
381+
assertFalse(softButtonState1.equals(softButtonState2));
382+
SdlArtwork artwork1 = new SdlArtwork("image1", FileType.GRAPHIC_PNG, 1, true);
383+
SdlArtwork artwork2 = new SdlArtwork("image2", FileType.GRAPHIC_PNG, 1, true);
384+
385+
// Case 1: object is null, assertFalse
386+
softButtonState1 = new SoftButtonState("object1-state1", "o1s1", artwork1);
387+
softButtonState2 = null;
388+
assertFalse(softButtonState1.equals(softButtonState2));
389+
390+
// Case 2 SoftButtonObjects are the same, assertTrue
391+
assertTrue(softButtonState1.equals(softButtonState1));
392+
393+
// Case 3: object is not an instance of SoftButtonState, assertFalse
394+
assertFalse(softButtonState1.equals(artwork1));
395+
396+
// Case 4: different artwork, assertFalse
397+
softButtonState2 = new SoftButtonState("object1-state1", "o1s1", artwork2);
398+
assertFalse(softButtonState1.equals(softButtonState2));
399+
400+
// Case 5: different name, assertFalse
401+
softButtonState2 = new SoftButtonState("object1-state1 different name", "o1s1", artwork1);
402+
assertFalse(softButtonState1.equals(softButtonState2));
403+
404+
// Case 6 they are equal, assertTrue
405+
softButtonState2 = new SoftButtonState("object1-state1", "o1s1", artwork1);
406+
assertTrue(softButtonState1.equals(softButtonState2));
407+
}
308408
}

android/sdl_android/src/main/java/com/smartdevicelink/managers/file/filetypes/SdlFile.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,4 +237,37 @@ public boolean getOverwrite() {
237237
public void setOverwrite(boolean overwrite) {
238238
this.overwrite = overwrite;
239239
}
240+
241+
/**
242+
* Used to compile hashcode for SdlFile for use to compare in overridden equals method
243+
* @return Custom hashcode of SdlFile variables
244+
*/
245+
@Override
246+
public int hashCode() {
247+
int result = 1;
248+
result += ((getName() == null) ? 0 : Integer.rotateLeft(getName().hashCode(), 1));
249+
result += ((getUri() == null) ? 0 : Integer.rotateLeft(getUri().hashCode(), 2));
250+
result += ((getFileData() == null) ? 0 : Integer.rotateLeft(getFileData().hashCode(), 3));
251+
result += ((getType() == null) ? 0 : Integer.rotateLeft(getType().hashCode(), 4));
252+
result += Integer.rotateLeft(Boolean.valueOf(isStaticIcon()).hashCode(), 5);
253+
result += Integer.rotateLeft(Boolean.valueOf(isPersistent()).hashCode(), 6);
254+
result += Integer.rotateLeft(Integer.valueOf(getResourceId()).hashCode(), 7);
255+
return result;
256+
}
257+
258+
/**
259+
* Uses our custom hashCode for SdlFile objects
260+
* @param o - The object to compare
261+
* @return boolean of whether the objects are the same or not
262+
*/
263+
@Override
264+
public boolean equals(Object o) {
265+
if (o == null) return false;
266+
// if this is the same memory address, it's the same
267+
if (this == o) return true;
268+
// if this is not an instance of SdlFile, not the same
269+
if (!(o instanceof SdlFile)) return false;
270+
// return comparison
271+
return hashCode() == o.hashCode();
272+
}
240273
}

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,4 +309,36 @@ interface UpdateListener{
309309
*/
310310
void onUpdate();
311311
}
312+
313+
/**
314+
* Used to compile hashcode for SoftButtonsObjects for use to compare in equals method
315+
* @return Custom hashcode of SoftButtonObjects variables
316+
*/
317+
@Override
318+
public int hashCode() {
319+
int result = 1;
320+
result += ((getName() == null) ? 0 : Integer.rotateLeft(getName().hashCode(), 1));
321+
result += ((getCurrentStateName() == null) ? 0 : Integer.rotateLeft(getCurrentStateName().hashCode(), 2));
322+
result += Integer.rotateLeft(Integer.valueOf(getButtonId()).hashCode(), 3);
323+
for (int i = 0; i < this.states.size(); i++) {
324+
result += ((getStates().get(i) == null) ? 0 : Integer.rotateLeft(getStates().get(i).hashCode(), i + 4));
325+
}
326+
return result;
327+
}
328+
329+
/**
330+
* Uses our custom hashCode for SoftButtonObject objects
331+
* @param o - The object to compare
332+
* @return boolean of whether the objects are the same or not
333+
*/
334+
@Override
335+
public boolean equals(Object o) {
336+
if (o == null) return false;
337+
// if this is the same memory address, it's the same
338+
if (this == o) return true;
339+
// if this is not an instance of SoftButtonObject, not the same
340+
if (!(o instanceof SoftButtonObject)) return false;
341+
// return comparison
342+
return hashCode() == o.hashCode();
343+
}
312344
}

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,4 +158,32 @@ public SoftButton getSoftButton() {
158158
public SdlArtwork getArtwork() {
159159
return artwork;
160160
}
161+
162+
/**
163+
* Used to compile hashcode for SoftButtonState for use to compare in equals method
164+
* @return Custom hashcode of SoftButtonState variables
165+
*/
166+
@Override
167+
public int hashCode() {
168+
int result = 1;
169+
result += ((getName() == null) ? 0 : Integer.rotateLeft(getName().hashCode(), 1));
170+
result += ((getArtwork() == null) ? 0 : Integer.rotateLeft(getArtwork().hashCode(),2));
171+
return result;
172+
}
173+
174+
/**
175+
* Uses our custom hashCode for SoftButtonState objects
176+
* @param o - The object to compare
177+
* @return boolean of whether the objects are the same or not
178+
*/
179+
@Override
180+
public boolean equals(Object o) {
181+
if (o == null) return false;
182+
// if this is the same memory address, it's the same
183+
if (this == o) return true;
184+
// if this is not an instance of SoftButtonState, not the same
185+
if (!(o instanceof SoftButtonState)) return false;
186+
// return comparison
187+
return hashCode() == o.hashCode();
188+
}
161189
}

base/src/main/java/com/smartdevicelink/managers/screen/choiceset/ChoiceCell.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,8 +224,8 @@ public int hashCode() {
224224
result += ((getText() == null) ? 0 : Integer.rotateLeft(getText().hashCode(), 1));
225225
result += ((getSecondaryText() == null) ? 0 : Integer.rotateLeft(getSecondaryText().hashCode(), 2));
226226
result += ((getTertiaryText() == null) ? 0 : Integer.rotateLeft(getTertiaryText().hashCode(), 3));
227-
result += ((getArtwork() == null || getArtwork().getName() == null) ? 0 : Integer.rotateLeft(getArtwork().getName().hashCode(), 4));
228-
result += ((getSecondaryArtwork() == null || getSecondaryArtwork().getName() == null) ? 0 : Integer.rotateLeft(getSecondaryArtwork().getName().hashCode(), 5));
227+
result += ((getArtwork() == null) ? 0 : Integer.rotateLeft(getArtwork().hashCode(), 4));
228+
result += ((getSecondaryArtwork() == null) ? 0 : Integer.rotateLeft(getSecondaryArtwork().hashCode(), 5));
229229
result += ((getVoiceCommands() == null) ? 0 : Integer.rotateLeft(getVoiceCommands().hashCode(), 6));
230230
return result;
231231
}

base/src/main/java/com/smartdevicelink/managers/screen/menu/MenuCell.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ int getParentCellId() {
299299
public int hashCode() {
300300
int result = 1;
301301
result += ((getTitle() == null) ? 0 : Integer.rotateLeft(getTitle().hashCode(), 1));
302-
result += ((getIcon() == null || getIcon().getName() == null) ? 0 : Integer.rotateLeft(getIcon().getName().hashCode(), 2));
302+
result += ((getIcon() == null) ? 0 : Integer.rotateLeft(getIcon().hashCode(), 2));
303303
result += ((getVoiceCommands() == null) ? 0 : Integer.rotateLeft(getVoiceCommands().hashCode(), 3));
304304
result += ((getSubCells() == null) ? 0 : Integer.rotateLeft(1, 4));
305305
return result;

javaSE/src/main/java/com/smartdevicelink/managers/file/filetypes/SdlFile.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,4 +203,36 @@ public boolean getOverwrite() {
203203
public void setOverwrite(boolean overwrite) {
204204
this.overwrite = overwrite;
205205
}
206+
207+
/**
208+
* Used to compile hashcode for SdlFile for use to compare in equals method
209+
* @return Custom hashcode of SdlFile variables
210+
*/
211+
@Override
212+
public int hashCode() {
213+
int result = 1;
214+
result += ((getName() == null) ? 0 : Integer.rotateLeft(getName().hashCode(), 1));
215+
result += ((getFilePath() == null) ? 0 : Integer.rotateLeft(getFilePath().hashCode(), 2));
216+
result += ((getFileData() == null) ? 0 : Integer.rotateLeft(getFileData().hashCode(), 3));
217+
result += ((getType() == null) ? 0 : Integer.rotateLeft(getType().hashCode(), 4));
218+
result += Integer.rotateLeft(Boolean.valueOf(isStaticIcon()).hashCode(), 5);
219+
result += Integer.rotateLeft(Boolean.valueOf(isPersistent()).hashCode(), 6);
220+
return result;
221+
}
222+
223+
/**
224+
* Uses our custom hashCode for SdlFile objects
225+
* @param o - The object to compare
226+
* @return boolean of whether the objects are the same or not
227+
*/
228+
@Override
229+
public boolean equals(Object o) {
230+
if (o == null) return false;
231+
// if this is the same memory address, it's the same
232+
if (this == o) return true;
233+
// if this is not an instance of SdlFile, not the same
234+
if (!(o instanceof SdlFile)) return false;
235+
// return comparison
236+
return hashCode() == o.hashCode();
237+
}
206238
}

0 commit comments

Comments
 (0)