summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author Felipe Leme <felipeal@google.com> 2022-11-10 23:12:45 +0000
committer Felipe Leme <felipeal@google.com> 2022-11-18 22:15:41 +0000
commit1f4ba2ddc0fe7fa5bbae80f488d0a0b4b46c7ae4 (patch)
tree0ff104ab3fb4256ff0013dce4dbeaaaa9111af54
parent9edf890b7d1bb8e0f588fa683eaf7f2541367662 (diff)
Implemented IntArray.toString()
Also refactored IntArrayTest to use Truth. Test: atest IntArrayTest Bug: 257497221 Bug: 30839080 Change-Id: I1f31fe1ae7736554ab1b32b57773709452813bf3
-rw-r--r--core/java/android/util/IntArray.java19
-rw-r--r--core/tests/utiltests/src/android/util/IntArrayTest.java58
-rw-r--r--services/core/java/com/android/server/pm/UserVisibilityMediator.java4
3 files changed, 57 insertions, 24 deletions
diff --git a/core/java/android/util/IntArray.java b/core/java/android/util/IntArray.java
index 7b28b8a607de..bc0e35da37c3 100644
--- a/core/java/android/util/IntArray.java
+++ b/core/java/android/util/IntArray.java
@@ -234,4 +234,23 @@ public class IntArray implements Cloneable {
public int[] toArray() {
return Arrays.copyOf(mValues, mSize);
}
+
+ @Override
+ public String toString() {
+ // Code below is copied from Arrays.toString(), but uses mSize in the lopp (it cannot call
+ // Arrays.toString() directly as it would return the unused elements as well)
+ int iMax = mSize - 1;
+ if (iMax == -1) {
+ return "[]";
+ }
+ StringBuilder b = new StringBuilder();
+ b.append('[');
+ for (int i = 0;; i++) {
+ b.append(mValues[i]);
+ if (i == iMax) {
+ return b.append(']').toString();
+ }
+ b.append(", ");
+ }
+ }
}
diff --git a/core/tests/utiltests/src/android/util/IntArrayTest.java b/core/tests/utiltests/src/android/util/IntArrayTest.java
index a76c640db74a..caa7312a4475 100644
--- a/core/tests/utiltests/src/android/util/IntArrayTest.java
+++ b/core/tests/utiltests/src/android/util/IntArrayTest.java
@@ -16,8 +16,8 @@
package android.util;
-import static org.junit.Assert.assertArrayEquals;
-import static org.junit.Assert.assertEquals;
+import static com.google.common.truth.Truth.assertThat;
+import static com.google.common.truth.Truth.assertWithMessage;
import androidx.test.filters.SmallTest;
import androidx.test.runner.AndroidJUnit4;
@@ -25,6 +25,8 @@ import androidx.test.runner.AndroidJUnit4;
import org.junit.Test;
import org.junit.runner.RunWith;
+import java.util.Arrays;
+
@RunWith(AndroidJUnit4.class)
@SmallTest
public class IntArrayTest {
@@ -35,51 +37,65 @@ public class IntArrayTest {
a.add(1);
a.add(2);
a.add(3);
- verify(new int[]{1, 2, 3}, a);
+ verify(a, 1, 2, 3);
IntArray b = IntArray.fromArray(new int[]{4, 5, 6, 7, 8}, 3);
a.addAll(b);
- verify(new int[]{1, 2, 3, 4, 5, 6}, a);
+ verify(a, 1, 2, 3, 4, 5, 6);
a.resize(2);
- verify(new int[]{1, 2}, a);
+ verify(a, 1, 2);
a.resize(8);
- verify(new int[]{1, 2, 0, 0, 0, 0, 0, 0}, a);
+ verify(a, 1, 2, 0, 0, 0, 0, 0, 0);
a.set(5, 10);
- verify(new int[]{1, 2, 0, 0, 0, 10, 0, 0}, a);
+ verify(a, 1, 2, 0, 0, 0, 10, 0, 0);
a.add(5, 20);
- assertEquals(20, a.get(5));
- assertEquals(5, a.indexOf(20));
- verify(new int[]{1, 2, 0, 0, 0, 20, 10, 0, 0}, a);
+ assertThat(a.get(5)).isEqualTo(20);
+ assertThat(a.indexOf(20)).isEqualTo(5);
+ verify(a, 1, 2, 0, 0, 0, 20, 10, 0, 0);
- assertEquals(-1, a.indexOf(99));
+ assertThat(a.indexOf(99)).isEqualTo(-1);
a.resize(15);
a.set(14, 30);
- verify(new int[]{1, 2, 0, 0, 0, 20, 10, 0, 0, 0, 0, 0, 0, 0, 30}, a);
+ verify(a, 1, 2, 0, 0, 0, 20, 10, 0, 0, 0, 0, 0, 0, 0, 30);
int[] backingArray = new int[]{1, 2, 3, 4};
a = IntArray.wrap(backingArray);
a.set(0, 10);
- assertEquals(10, backingArray[0]);
+ assertThat(backingArray[0]).isEqualTo(10);
backingArray[1] = 20;
backingArray[2] = 30;
- verify(backingArray, a);
- assertEquals(2, a.indexOf(30));
+ verify(a, backingArray);
+ assertThat(a.indexOf(30)).isEqualTo(2);
a.resize(2);
- assertEquals(0, backingArray[2]);
- assertEquals(0, backingArray[3]);
+ assertThat(backingArray[2]).isEqualTo(0);
+ assertThat(backingArray[3]).isEqualTo(0);
a.add(50);
- verify(new int[]{10, 20, 50}, a);
+ verify(a, 10, 20, 50);
+ }
+
+ @Test
+ public void testToString() {
+ IntArray a = new IntArray(10);
+ a.add(4);
+ a.add(8);
+ a.add(15);
+ a.add(16);
+ a.add(23);
+ a.add(42);
+
+ assertWithMessage("toString()").that(a.toString()).contains("4, 8, 15, 16, 23, 42");
+ assertWithMessage("toString()").that(a.toString()).doesNotContain("0");
}
- public void verify(int[] expected, IntArray intArray) {
- assertEquals(expected.length, intArray.size());
- assertArrayEquals(expected, intArray.toArray());
+ public void verify(IntArray intArray, int... expected) {
+ assertWithMessage("contents of %s", intArray).that(intArray.toArray()).asList()
+ .containsExactlyElementsIn(Arrays.stream(expected).boxed().toList());
}
}
diff --git a/services/core/java/com/android/server/pm/UserVisibilityMediator.java b/services/core/java/com/android/server/pm/UserVisibilityMediator.java
index 9c4187b35e93..387b9ef28a86 100644
--- a/services/core/java/com/android/server/pm/UserVisibilityMediator.java
+++ b/services/core/java/com/android/server/pm/UserVisibilityMediator.java
@@ -604,9 +604,7 @@ public final class UserVisibilityMediator implements Dumpable {
ipw.println(mCurrentUserId);
ipw.print("Visible users: ");
- // TODO: merge 2 lines below if/when IntArray implements toString()...
- IntArray visibleUsers = getVisibleUsers();
- ipw.println(java.util.Arrays.toString(visibleUsers.toArray()));
+ ipw.println(getVisibleUsers());
dumpSparseIntArray(ipw, mStartedProfileGroupIds, "started user / profile group",
"u", "pg");