Create concrete class for "no setup" ux restrictions behavior.
This allows preferences that do not need a more advanced controller to respond to ux restrictions simply. It also aligns the base behavior with preferences that do not have controllers improving consistency.
Bug: 112931076
Test: RunCarSettingsRoboTests
Change-Id: I5d90ed65249e031db7ff57b7ce7be948c6926d10
diff --git a/src/com/android/car/settings/common/BasePreferenceController.java b/src/com/android/car/settings/common/BasePreferenceController.java
index c28cc58..7161036 100644
--- a/src/com/android/car/settings/common/BasePreferenceController.java
+++ b/src/com/android/car/settings/common/BasePreferenceController.java
@@ -177,10 +177,10 @@
/**
* Returns {@code true} if the preference for this controller can be shown given the {@param
- * restrictionInfo}. Defaults to {@code false} if
- * {@link CarUxRestrictions#UX_RESTRICTIONS_NO_SETUP} is set.
+ * restrictionInfo}. Defaults to {@code true}. Subclasses may override this method to modify
+ * availability based on driving restrictions.
*/
protected boolean canBeShownWithRestrictions(CarUxRestrictions restrictionInfo) {
- return !CarUxRestrictionsHelper.isNoSetup(restrictionInfo);
+ return true;
}
}
diff --git a/src/com/android/car/settings/common/NoSetupPreferenceController.java b/src/com/android/car/settings/common/NoSetupPreferenceController.java
new file mode 100644
index 0000000..70faeff
--- /dev/null
+++ b/src/com/android/car/settings/common/NoSetupPreferenceController.java
@@ -0,0 +1,41 @@
+/*
+ * Copyright (C) 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 com.android.car.settings.common;
+
+import android.car.drivingstate.CarUxRestrictions;
+import android.content.Context;
+
+/**
+ * Controller that is available when {@link CarUxRestrictions#UX_RESTRICTIONS_NO_SETUP} is not
+ * active.
+ */
+public class NoSetupPreferenceController extends BasePreferenceController {
+
+ public NoSetupPreferenceController(Context context, String preferenceKey) {
+ super(context, preferenceKey);
+ }
+
+ @Override
+ public int getAvailabilityStatus() {
+ return AVAILABLE;
+ }
+
+ @Override
+ protected boolean canBeShownWithRestrictions(CarUxRestrictions restrictionInfo) {
+ return !CarUxRestrictionsHelper.isNoSetup(restrictionInfo);
+ }
+}
diff --git a/tests/robotests/src/com/android/car/settings/common/BasePreferenceControllerTest.java b/tests/robotests/src/com/android/car/settings/common/BasePreferenceControllerTest.java
index 2f53d5c..8f685cd 100644
--- a/tests/robotests/src/com/android/car/settings/common/BasePreferenceControllerTest.java
+++ b/tests/robotests/src/com/android/car/settings/common/BasePreferenceControllerTest.java
@@ -25,7 +25,6 @@
import static org.testng.Assert.expectThrows;
-import android.car.drivingstate.CarUxRestrictions;
import android.content.Context;
import com.android.car.settings.CarSettingsRobolectricTestRunner;
@@ -71,40 +70,30 @@
}
@Test
- public void isAvailable_available_noRestrictions_returnsTrue() {
+ public void isAvailable_available_returnsTrue() {
mController.setAvailabilityStatus(AVAILABLE);
assertThat(mController.isAvailable()).isTrue();
}
@Test
- public void isAvailable_conditionallyUnavailable_noRestrictions_returnsFalse() {
+ public void isAvailable_conditionallyUnavailable_returnsFalse() {
mController.setAvailabilityStatus(CONDITIONALLY_UNAVAILABLE);
assertThat(mController.isAvailable()).isFalse();
}
@Test
- public void isAvailable_unsupportedOnDevice_noRestrictions_returnsFalse() {
+ public void isAvailable_unsupportedOnDevice_returnsFalse() {
mController.setAvailabilityStatus(UNSUPPORTED_ON_DEVICE);
assertThat(mController.isAvailable()).isFalse();
}
@Test
- public void isAvailable_disabledForUser_noRestrictions_returnsFalse() {
+ public void isAvailable_disabledForUser_returnsFalse() {
mController.setAvailabilityStatus(DISABLED_FOR_USER);
assertThat(mController.isAvailable()).isFalse();
}
-
- @Test
- public void isAvailable_available_restrictions_returnsFalse() {
- mController.setAvailabilityStatus(AVAILABLE);
- CarUxRestrictions restrictionInfo = new CarUxRestrictions.Builder(/* reqOpt= */ true,
- CarUxRestrictions.UX_RESTRICTIONS_NO_SETUP, /* timestamp= */ 0).build();
- mController.onUxRestrictionsChanged(restrictionInfo);
-
- assertThat(mController.isAvailable()).isFalse();
- }
}
diff --git a/tests/robotests/src/com/android/car/settings/common/NoSetupPreferenceControllerTest.java b/tests/robotests/src/com/android/car/settings/common/NoSetupPreferenceControllerTest.java
new file mode 100644
index 0000000..d28ffb3
--- /dev/null
+++ b/tests/robotests/src/com/android/car/settings/common/NoSetupPreferenceControllerTest.java
@@ -0,0 +1,72 @@
+/*
+ * Copyright (C) 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 com.android.car.settings.common;
+
+import static com.android.car.settings.common.BasePreferenceController.AVAILABLE;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import android.car.drivingstate.CarUxRestrictions;
+import android.content.Context;
+
+import com.android.car.settings.CarSettingsRobolectricTestRunner;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.robolectric.RuntimeEnvironment;
+
+/**
+ * Unit test for {@link BasePreferenceController}.
+ */
+@RunWith(CarSettingsRobolectricTestRunner.class)
+public class NoSetupPreferenceControllerTest {
+
+ private static final String PREFERENCE_KEY = "preference_key";
+
+ private Context mContext;
+ private NoSetupPreferenceController mController;
+
+ @Before
+ public void setUp() {
+ mContext = RuntimeEnvironment.application;
+ mController = new NoSetupPreferenceController(mContext, PREFERENCE_KEY);
+ }
+
+ @Test
+ public void getAvailabilityStatus_available() {
+ assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE);
+ }
+
+ @Test
+ public void isAvailable_baselineRestrictions_returnsTrue() {
+ CarUxRestrictions restrictionInfo = new CarUxRestrictions.Builder(/* reqOpt= */ true,
+ CarUxRestrictions.UX_RESTRICTIONS_BASELINE, /* timestamp= */ 0).build();
+ mController.onUxRestrictionsChanged(restrictionInfo);
+
+ assertThat(mController.isAvailable()).isTrue();
+ }
+
+ @Test
+ public void isAvailable_noSetupRestrictions_returnsFalse() {
+ CarUxRestrictions restrictionInfo = new CarUxRestrictions.Builder(/* reqOpt= */ true,
+ CarUxRestrictions.UX_RESTRICTIONS_NO_SETUP, /* timestamp= */ 0).build();
+ mController.onUxRestrictionsChanged(restrictionInfo);
+
+ assertThat(mController.isAvailable()).isFalse();
+ }
+}