Add parceling test to DocumentStack.
Bug: 32344918
Change-Id: I47390c17829e675f870aabc1ddfb6b2b56a558dc
diff --git a/tests/common/com/android/documentsui/testing/Parcelables.java b/tests/common/com/android/documentsui/testing/Parcelables.java
index cd44a9a..5267065 100644
--- a/tests/common/com/android/documentsui/testing/Parcelables.java
+++ b/tests/common/com/android/documentsui/testing/Parcelables.java
@@ -17,26 +17,28 @@
package com.android.documentsui.testing;
import static junit.framework.Assert.assertEquals;
+import static junit.framework.Assert.assertTrue;
import android.os.Parcel;
import android.os.Parcelable;
-import java.util.Comparator;
+import java.util.function.BiPredicate;
public class Parcelables {
private Parcelables() {}
- public static <T extends Parcelable> void testParceling(T p, int flags) {
+ public static <T extends Parcelable> void assertParcelable(T p, int flags) {
final T restored = parcel(p, flags);
assertEquals(p, restored);
}
- public static <T extends Parcelable> void testParceling(T p, int flags, Comparator<T> comp) {
+ public static <T extends Parcelable> void assertParcelable(
+ T p, int flags, BiPredicate<T, T> pred) {
T restored = parcel(p, flags);
- assertEquals(0, comp.compare(p, restored));
+ assertTrue(pred.test(p, restored));
}
private static <T extends Parcelable> T parcel(T p, int flags) {
diff --git a/tests/unit/com/android/documentsui/base/DocumentStackTest.java b/tests/unit/com/android/documentsui/base/DocumentStackTest.java
index 4c8bc2d..b007583 100644
--- a/tests/unit/com/android/documentsui/base/DocumentStackTest.java
+++ b/tests/unit/com/android/documentsui/base/DocumentStackTest.java
@@ -21,14 +21,18 @@
import static junit.framework.TestCase.assertNull;
import static junit.framework.TestCase.assertTrue;
-import android.net.Uri;
+import android.provider.DocumentsContract;
import android.support.test.filters.SmallTest;
import android.support.test.runner.AndroidJUnit4;
+import com.android.documentsui.testing.Parcelables;
+
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
+import java.util.Objects;
+
@RunWith(AndroidJUnit4.class)
@SmallTest
public class DocumentStackTest {
@@ -46,12 +50,18 @@
ROOT_2 = new RootInfo();
ROOT_2.rootId = "downloads";
- DIR_1 = new DocumentInfo();
- DIR_1.derivedUri = Uri.parse("content://authority/document/firstId");
- DIR_1.displayName = "firstDirectory";
- DIR_2 = new DocumentInfo();
- DIR_2.derivedUri = Uri.parse("content://authority/document/secondId");
- DIR_2.displayName = "secondDirectory";
+ DIR_1 = createDir("first");
+ DIR_2 = createDir("second");
+ }
+
+ private static DocumentInfo createDir(String docId) {
+ DocumentInfo info = new DocumentInfo();
+ info.authority = "authority";
+ info.documentId = docId;
+ info.displayName = docId;
+ info.mimeType = DocumentsContract.Document.MIME_TYPE_DIR;
+ info.deriveFields();
+ return info;
}
@Before
@@ -173,4 +183,25 @@
assertTrue(mStack.hasInitialLocationChanged());
}
+
+ @Test
+ public void testParceling() {
+ mStack.changeRoot(ROOT_1);
+ mStack.push(DIR_1);
+ mStack.push(DIR_2);
+
+ Parcelables.assertParcelable(mStack, 0, (DocumentStack left, DocumentStack right) -> {
+ if (!Objects.equals(left.getRoot(), right.getRoot()) || left.size() != right.size()) {
+ return false;
+ }
+
+ for (int i = 0; i < left.size(); ++i) {
+ if (!Objects.equals(left.get(i), right.get(i))) {
+ return false;
+ }
+ }
+
+ return true;
+ });
+ }
}
diff --git a/tests/unit/com/android/documentsui/sorting/SortDimensionTest.java b/tests/unit/com/android/documentsui/sorting/SortDimensionTest.java
index 9468b84..c7abb9a 100644
--- a/tests/unit/com/android/documentsui/sorting/SortDimensionTest.java
+++ b/tests/unit/com/android/documentsui/sorting/SortDimensionTest.java
@@ -84,6 +84,6 @@
}
@Test
public void testParceling() {
- Parcelables.testParceling(mDimension, 0);
+ Parcelables.assertParcelable(mDimension, 0);
}
}
diff --git a/tests/unit/com/android/documentsui/sorting/SortModelTest.java b/tests/unit/com/android/documentsui/sorting/SortModelTest.java
index aa50fb0..c0acdb6 100644
--- a/tests/unit/com/android/documentsui/sorting/SortModelTest.java
+++ b/tests/unit/com/android/documentsui/sorting/SortModelTest.java
@@ -211,12 +211,12 @@
mModel.sortByUser(DIMENSION_2.getId(), SortDimension.SORT_DIRECTION_DESCENDING);
mModel.setDimensionVisibility(DIMENSION_3.getId(), View.GONE);
- Parcelables.testParceling(mModel, 0);
+ Parcelables.assertParcelable(mModel, 0);
}
@Test
public void testParceling_NoSortedDimension() {
- Parcelables.testParceling(mModel, 0);
+ Parcelables.assertParcelable(mModel, 0);
}
private @Nullable SortDimension getSortedDimension() {