diff options
author | 2018-01-09 16:09:35 +0000 | |
---|---|---|
committer | 2018-01-09 16:58:45 +0000 | |
commit | eb279461738d86c7613158473af1294898cc353b (patch) | |
tree | f03339f7f07d9b583686709dc8c03492e87ae3b8 | |
parent | 0d64cd33ba31d77f255c2240ea8c69b1a1b05144 (diff) |
WorkSource: Fix corner-case in equals method.
We should treat WorkSource objects that have a null mChains List
as being equal to one that has an empty non-null mChains List.
This bug was uncovered by the tests added to WifiController in
commit 0068bf953ad25e in frameworks/opt/net/wifi.
Bug: 62390666
Test: WorkSourceTest
Change-Id: Icd1f86aa5178a7beb436621362cfe771a3440398
-rw-r--r-- | core/java/android/os/WorkSource.java | 19 | ||||
-rw-r--r-- | core/tests/coretests/src/android/os/WorkSourceTest.java | 17 |
2 files changed, 32 insertions, 4 deletions
diff --git a/core/java/android/os/WorkSource.java b/core/java/android/os/WorkSource.java index bb043b0109d5..d0c2870bfefb 100644 --- a/core/java/android/os/WorkSource.java +++ b/core/java/android/os/WorkSource.java @@ -7,7 +7,6 @@ import android.util.proto.ProtoOutputStream; import java.util.ArrayList; import java.util.Arrays; -import java.util.Objects; /** * Describes the source of some work that may be done by someone else. @@ -162,9 +161,21 @@ public class WorkSource implements Parcelable { @Override public boolean equals(Object o) { - return o instanceof WorkSource - && !diff((WorkSource) o) - && Objects.equals(mChains, ((WorkSource) o).mChains); + if (o instanceof WorkSource) { + WorkSource other = (WorkSource) o; + + if (diff(other)) { + return false; + } + + if (mChains != null && !mChains.isEmpty()) { + return mChains.equals(other.mChains); + } else { + return other.mChains == null || other.mChains.isEmpty(); + } + } + + return false; } @Override diff --git a/core/tests/coretests/src/android/os/WorkSourceTest.java b/core/tests/coretests/src/android/os/WorkSourceTest.java index 566ac4daf950..a427a2fe8f66 100644 --- a/core/tests/coretests/src/android/os/WorkSourceTest.java +++ b/core/tests/coretests/src/android/os/WorkSourceTest.java @@ -128,6 +128,23 @@ public class WorkSourceTest extends TestCase { assertFalse(ws3.equals(ws1)); } + public void testEquals_workChains_nullEmptyAreEquivalent() { + // Construct a WorkSource that has no WorkChains, but whose workChains list + // is non-null. + WorkSource ws1 = new WorkSource(); + ws1.add(100); + ws1.createWorkChain().addNode(100, null); + ws1.getWorkChains().clear(); + + WorkSource ws2 = new WorkSource(); + ws2.add(100); + + assertEquals(ws1, ws2); + + ws2.createWorkChain().addNode(100, null); + assertFalse(ws1.equals(ws2)); + } + public void testWorkSourceParcelling() { WorkSource ws = new WorkSource(); |