Making lists immutable
Test: Added new unit tests
Bug: 116224101
Change-Id: I57e02ad129dad8a79fe98d6bbd232a205fc2c2d1
diff --git a/car/cluster/src/androidTest/java/androidx/car/cluster/navigation/LaneTest.java b/car/cluster/src/androidTest/java/androidx/car/cluster/navigation/LaneTest.java
new file mode 100644
index 0000000..cd9c8e2
--- /dev/null
+++ b/car/cluster/src/androidTest/java/androidx/car/cluster/navigation/LaneTest.java
@@ -0,0 +1,39 @@
+/*
+ * Copyright 2018 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package androidx.car.cluster.navigation;
+
+import androidx.test.filters.SmallTest;
+import androidx.test.runner.AndroidJUnit4;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+/**
+ * Tests for {@link Lane} serialization
+ */
+@RunWith(AndroidJUnit4.class)
+@SmallTest
+public class LaneTest {
+ /**
+ * Tests that lists returned by {@link Lane} are immutable.
+ */
+ @Test(expected = UnsupportedOperationException.class)
+ public void immutableLists() {
+ Lane lane = new Lane.Builder().build();
+ lane.getDirections().add(new LaneDirection.Builder().build());
+ }
+}
diff --git a/car/cluster/src/androidTest/java/androidx/car/cluster/navigation/NavigationStateTest.java b/car/cluster/src/androidTest/java/androidx/car/cluster/navigation/NavigationStateTest.java
index 60935a0..3549a41 100644
--- a/car/cluster/src/androidTest/java/androidx/car/cluster/navigation/NavigationStateTest.java
+++ b/car/cluster/src/androidTest/java/androidx/car/cluster/navigation/NavigationStateTest.java
@@ -133,7 +133,8 @@
NavigationState state = createSampleState();
// Setting a list item to null (even though this is not allowed by the API)
- state.mDestinations.set(0, null);
+ state.mDestinations = new ArrayList<>();
+ state.mDestinations.add(0, null);
// Ignoring the serialization step as Parcelable doesn't allow lists with null items. This
// test is just to make sure that even if the serialization protocol is changed, the API
@@ -142,6 +143,24 @@
assertEquals(new ArrayList(), state.getDestinations());
}
+ /**
+ * Tests that {@link NavigationState#mDestinations} is immutable.
+ */
+ @Test(expected = UnsupportedOperationException.class)
+ public void immutableDestionationsLists() {
+ NavigationState state = createEmptyState();
+ state.getDestinations().add(new Destination.Builder().build());
+ }
+
+ /**
+ * Tests that {@link NavigationState#mSteps} is immutable.
+ */
+ @Test(expected = UnsupportedOperationException.class)
+ public void immutableStepsLists() {
+ NavigationState state = createEmptyState();
+ state.getSteps().add(new Step.Builder().build());
+ }
+
private NavigationState createEmptyState() {
return new NavigationState.Builder().build();
}
diff --git a/car/cluster/src/androidTest/java/androidx/car/cluster/navigation/StepTest.java b/car/cluster/src/androidTest/java/androidx/car/cluster/navigation/StepTest.java
new file mode 100644
index 0000000..12e6ce6
--- /dev/null
+++ b/car/cluster/src/androidTest/java/androidx/car/cluster/navigation/StepTest.java
@@ -0,0 +1,39 @@
+/*
+ * Copyright 2018 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package androidx.car.cluster.navigation;
+
+import androidx.test.filters.SmallTest;
+import androidx.test.runner.AndroidJUnit4;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+/**
+ * Tests for {@link Step} serialization
+ */
+@RunWith(AndroidJUnit4.class)
+@SmallTest
+public class StepTest {
+ /**
+ * Tests that lists returned by {@link Step} are immutable.
+ */
+ @Test(expected = UnsupportedOperationException.class)
+ public void immutableLists() {
+ Step step = new Step.Builder().build();
+ step.getLanes().add(new Lane.Builder().build());
+ }
+}
diff --git a/car/cluster/src/main/java/androidx/car/cluster/navigation/Lane.java b/car/cluster/src/main/java/androidx/car/cluster/navigation/Lane.java
index ba82144..509a7bd 100644
--- a/car/cluster/src/main/java/androidx/car/cluster/navigation/Lane.java
+++ b/car/cluster/src/main/java/androidx/car/cluster/navigation/Lane.java
@@ -26,6 +26,7 @@
import androidx.versionedparcelable.VersionedParcelize;
import java.util.ArrayList;
+import java.util.Collections;
import java.util.List;
import java.util.Objects;
@@ -53,7 +54,7 @@
*/
@RestrictTo(LIBRARY_GROUP)
Lane(@NonNull List<LaneDirection> directions) {
- mDirections = new ArrayList<>(directions);
+ mDirections = Collections.unmodifiableList(new ArrayList<>(directions));
}
/**
@@ -81,7 +82,7 @@
}
/**
- * Returns all possible directions a driver can take from this lane.
+ * Returns an unmodifiable list of possible directions a driver can take from this lane.
*/
@NonNull
public List<LaneDirection> getDirections() {
diff --git a/car/cluster/src/main/java/androidx/car/cluster/navigation/NavigationState.java b/car/cluster/src/main/java/androidx/car/cluster/navigation/NavigationState.java
index a4608ab..16214c7 100644
--- a/car/cluster/src/main/java/androidx/car/cluster/navigation/NavigationState.java
+++ b/car/cluster/src/main/java/androidx/car/cluster/navigation/NavigationState.java
@@ -31,6 +31,7 @@
import androidx.versionedparcelable.VersionedParcelize;
import java.util.ArrayList;
+import java.util.Collections;
import java.util.List;
import java.util.Objects;
@@ -64,8 +65,8 @@
*/
@RestrictTo(LIBRARY_GROUP)
NavigationState(@NonNull List<Step> steps, @NonNull List<Destination> destinations) {
- mSteps = new ArrayList<>(steps);
- mDestinations = new ArrayList<>(destinations);
+ mSteps = Collections.unmodifiableList(new ArrayList<>(steps));
+ mDestinations = Collections.unmodifiableList(new ArrayList<>(destinations));
}
/**
@@ -109,8 +110,8 @@
}
/**
- * Returns the navigation steps, in order of execution. It is up to the producer to decide how
- * many steps in advance will be provided. This list should not be mutated.
+ * Returns an unmodifiable list of navigation steps, in order of execution. It is up to the
+ * producer to decide how many steps in advance will be provided.
*/
@NonNull
public List<Step> getSteps() {
@@ -118,8 +119,8 @@
}
/**
- * Returns the destination and intermediate stops in the navigation, sorted from nearest to
- * furthest. This list should not be mutated.
+ * Returns an unmodifiable list of destinations and intermediate stops in the navigation, sorted
+ * from nearest to furthest.
*/
@NonNull
public List<Destination> getDestinations() {
diff --git a/car/cluster/src/main/java/androidx/car/cluster/navigation/Step.java b/car/cluster/src/main/java/androidx/car/cluster/navigation/Step.java
index fad36b5..78c89b7 100644
--- a/car/cluster/src/main/java/androidx/car/cluster/navigation/Step.java
+++ b/car/cluster/src/main/java/androidx/car/cluster/navigation/Step.java
@@ -27,6 +27,7 @@
import androidx.versionedparcelable.VersionedParcelize;
import java.util.ArrayList;
+import java.util.Collections;
import java.util.List;
import java.util.Objects;
@@ -60,7 +61,7 @@
Step(@Nullable Distance distance, @Nullable Maneuver maneuver, @NonNull List<Lane> lanes) {
mDistance = distance;
mManeuver = maneuver;
- mLanes = new ArrayList<>(lanes);
+ mLanes = Collections.unmodifiableList(new ArrayList<>(lanes));
}
/**
@@ -134,8 +135,8 @@
}
/**
- * Returns the configuration of all road lanes at the point where the driver should execute this
- * step. Lane configurations are listed from left to right.
+ * Returns an unmodifiable list containing the configuration of road lanes at the point where
+ * the driver should execute this step. Lane configurations are listed from left to right.
*/
@NonNull
public List<Lane> getLanes() {