Add SettingsPrefController for Slices

Adds a new abstraction layer between preference controllers
and the AbstractPreferenceController. The layer is used to
consolidate the logic for each the setting type for getting
and setting values. This will be extended to support UI
information for Slices.

For reference how this fits into Slices, look at the
like-named classes added in this prototype:
ag/3221891

The changes in Search are as a transition into deprecation.
The code for Search in Settings is out-of-date from the
unbundled counterpart, and this change is made so that the
current code behaves as normal.

Test: robotests
Bug: 67996707
Change-Id: Ib1faab706485039edad66119a27a3fd5cabe6009
This commit is contained in:
Matthew Fritze
2017-11-15 15:12:52 -08:00
parent c61ed6356d
commit 3a4168360b
11 changed files with 502 additions and 119 deletions

View File

@@ -0,0 +1,120 @@
/*
* Copyright (C) 2017 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.settings.core;
import static com.android.settings.core.BasePreferenceController.AVAILABLE;
import static com.android.settings.core.BasePreferenceController
.DISABLED_DEPENDENT_SETTING;
import static com.android.settings.core.BasePreferenceController.DISABLED_FOR_USER;
import static com.android.settings.core.BasePreferenceController.DISABLED_UNSUPPORTED;
import static com.android.settings.core.BasePreferenceController.UNAVAILABLE_UNKNOWN;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.when;
import com.android.settings.TestConfig;
import com.android.settings.testutils.SettingsRobolectricTestRunner;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.annotation.Config;
@RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION_O)
public class BasePreferenceControllerTest {
@Mock
BasePreferenceController mPreferenceController;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
}
@Test
public void isAvailable_availableStatusAvailable_returnsTrue() {
when(mPreferenceController.getAvailabilityStatus()).thenReturn(AVAILABLE);
assertThat(mPreferenceController.isAvailable()).isTrue();
}
@Test
public void isAvailable_availableStatusUnsupported_returnsFalse() {
when(mPreferenceController.getAvailabilityStatus()).thenReturn(DISABLED_UNSUPPORTED);
assertThat(mPreferenceController.isAvailable()).isFalse();
}
@Test
public void isAvailable_availableStatusDisabled_returnsFalse() {
when(mPreferenceController.getAvailabilityStatus()).thenReturn(DISABLED_FOR_USER);
assertThat(mPreferenceController.isAvailable()).isFalse();
}
@Test
public void isAvailable_availableStatusBlockedDependent_returnsFalse() {
when(mPreferenceController.getAvailabilityStatus()).thenReturn(DISABLED_DEPENDENT_SETTING);
assertThat(mPreferenceController.isAvailable()).isFalse();
}
@Test
public void isAvailable_availableStatusUnavailable_returnsFalse() {
when(mPreferenceController.getAvailabilityStatus()).thenReturn(UNAVAILABLE_UNKNOWN);
assertThat(mPreferenceController.isAvailable()).isFalse();
}
@Test
public void isSupported_availableStatusAvailable_returnsTrue() {
when(mPreferenceController.getAvailabilityStatus()).thenReturn(AVAILABLE);
assertThat(mPreferenceController.isSupported()).isTrue();
}
@Test
public void isSupported_availableStatusUnsupported_returnsFalse() {
when(mPreferenceController.getAvailabilityStatus()).thenReturn(DISABLED_UNSUPPORTED);
assertThat(mPreferenceController.isSupported()).isFalse();
}
@Test
public void isSupported_availableStatusDisabled_returnsTrue() {
when(mPreferenceController.getAvailabilityStatus()).thenReturn(DISABLED_FOR_USER);
assertThat(mPreferenceController.isSupported()).isTrue();
}
@Test
public void isSupported_availableStatusDependentSetting_returnsTrue() {
when(mPreferenceController.getAvailabilityStatus()).thenReturn(DISABLED_DEPENDENT_SETTING);
assertThat(mPreferenceController.isSupported()).isTrue();
}
@Test
public void isSupported_availableStatusUnavailable_returnsTrue() {
when(mPreferenceController.getAvailabilityStatus()).thenReturn(UNAVAILABLE_UNKNOWN);
assertThat(mPreferenceController.isSupported()).isTrue();
}
}

View File

@@ -0,0 +1,92 @@
/*
* Copyright (C) 2017 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.settings.core;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.content.Context;
import android.support.v14.preference.SwitchPreference;
import com.android.settings.TestConfig;
import com.android.settings.core.TogglePreferenceController;
import com.android.settings.testutils.SettingsRobolectricTestRunner;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
@RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION_O)
public class TogglePreferenceControllerTest {
@Mock
TogglePreferenceController mTogglePreferenceController;
Context mContext;
SwitchPreference mPreference;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = RuntimeEnvironment.application;
mPreference = new SwitchPreference(mContext);
}
@Test
public void testSetsPreferenceValue_setsChecked() {
when(mTogglePreferenceController.isChecked()).thenReturn(true);
mPreference.setChecked(false);
mTogglePreferenceController.updateState(mPreference);
assertThat(mPreference.isChecked()).isTrue();
}
@Test
public void testSetsPreferenceValue_setsNotChecked() {
when(mTogglePreferenceController.isChecked()).thenReturn(false);
mPreference.setChecked(true);
mTogglePreferenceController.updateState(mPreference);
assertThat(mPreference.isChecked()).isFalse();
}
@Test
public void testUpdatesPreferenceOnChange_turnsOn() {
boolean newValue = true;
mTogglePreferenceController.onPreferenceChange(mPreference, newValue);
verify(mTogglePreferenceController).setChecked(newValue);
}
@Test
public void testUpdatesPreferenceOnChange_turnsOff() {
boolean newValue = false;
mTogglePreferenceController.onPreferenceChange(mPreference, newValue);
verify(mTogglePreferenceController).setChecked(newValue);
}
}

View File

@@ -19,6 +19,7 @@ package com.android.settings.display;
import static android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE;
import static android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC;
import static android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL;
import static com.google.common.truth.Truth.assertThat;
import android.content.ContentResolver;
@@ -26,9 +27,6 @@ import android.content.Context;
import android.provider.Settings;
import com.android.settings.TestConfig;
import com.android.settings.search.InlinePayload;
import com.android.settings.search.InlineSwitchPayload;
import com.android.settings.search.ResultPayload;
import com.android.settings.testutils.SettingsRobolectricTestRunner;
import org.junit.Before;
@@ -38,10 +36,9 @@ import org.mockito.Answers;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.annotation.Config;
import org.robolectric.shadows.ShadowApplication;
@RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION_O)
public class AutoBrightnessPreferenceControllerTest {
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
private Context mContext;
@@ -73,34 +70,28 @@ public class AutoBrightnessPreferenceControllerTest {
assertThat(mode).isEqualTo(SCREEN_BRIGHTNESS_MODE_MANUAL);
}
@Test
public void testPreferenceController_ProperResultPayloadType() {
final Context context = ShadowApplication.getInstance().getApplicationContext();
mController = new AutoBrightnessPreferenceController(context, PREFERENCE_KEY);
ResultPayload payload = mController.getResultPayload();
assertThat(payload).isInstanceOf(InlineSwitchPayload.class);
}
@Test
public void testSetValue_updatesCorrectly() {
int newValue = 1;
boolean newValue = true;
ContentResolver resolver = mContext.getContentResolver();
Settings.System.putInt(resolver, SCREEN_BRIGHTNESS_MODE, 0);
Settings.System.putInt(resolver, SCREEN_BRIGHTNESS_MODE, SCREEN_BRIGHTNESS_MODE_MANUAL);
((InlinePayload) mController.getResultPayload()).setValue(mContext, newValue);
int updatedValue = Settings.System.getInt(resolver, SCREEN_BRIGHTNESS_MODE, -1);
mController.setChecked(newValue);
boolean updatedValue = Settings.System.getInt(resolver, SCREEN_BRIGHTNESS_MODE, -1)
!= SCREEN_BRIGHTNESS_MODE_MANUAL;
assertThat(updatedValue).isEqualTo(newValue);
}
@Test
public void testGetValue_correctValueReturned() {
int currentValue = 1;
ContentResolver resolver = mContext.getContentResolver();
Settings.System.putInt(resolver, SCREEN_BRIGHTNESS_MODE, currentValue);
Settings.System.putInt(resolver, SCREEN_BRIGHTNESS_MODE, SCREEN_BRIGHTNESS_MODE_AUTOMATIC);
int newValue = ((InlinePayload) mController.getResultPayload()).getValue(mContext);
int newValue = mController.isChecked() ?
SCREEN_BRIGHTNESS_MODE_AUTOMATIC
: SCREEN_BRIGHTNESS_MODE_MANUAL;
assertThat(newValue).isEqualTo(currentValue);
assertThat(newValue).isEqualTo(SCREEN_BRIGHTNESS_MODE_AUTOMATIC);
}
}

View File

@@ -20,18 +20,14 @@ package com.android.settings.search;
import static com.google.common.truth.Truth.assertThat;
import android.content.Context;
import android.util.ArrayMap;
import com.android.internal.hardware.AmbientDisplayConfiguration;
import com.android.settings.TestConfig;
import com.android.settings.core.PreferenceControllerMixin;
import com.android.settings.deviceinfo.SystemUpdatePreferenceController;
import com.android.settings.testutils.SettingsRobolectricTestRunner;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
@@ -43,8 +39,6 @@ import java.util.Map;
public class DatabaseIndexingUtilsTest {
private Context mContext;
@Mock
private AmbientDisplayConfiguration mAmbientDisplayConfiguration;
@Before
public void setUp() {
@@ -54,44 +48,22 @@ public class DatabaseIndexingUtilsTest {
@Test
public void testGetPreferenceControllerUriMap_BadClassName_ReturnsNull() {
Map map = DatabaseIndexingUtils.getPreferenceControllerUriMap("dummy", mContext);
assertThat(map).isNull();
Map map = DatabaseIndexingUtils.getPayloadKeyMap("dummy", mContext);
assertThat(map).isEmpty();
}
@Test
public void testGetPreferenceControllerUriMap_NullContext_ReturnsNull() {
Map map = DatabaseIndexingUtils.getPreferenceControllerUriMap("dummy", null);
assertThat(map).isNull();
}
@Test
public void testGetPreferenceControllerUriMap_CompatibleClass_ReturnsValidMap() {
final String className = "com.android.settings.system.SystemDashboardFragment";
final Map<String, PreferenceControllerMixin> map =
DatabaseIndexingUtils.getPreferenceControllerUriMap(className, mContext);
assertThat(map.get("system_update_settings"))
.isInstanceOf(SystemUpdatePreferenceController.class);
Map map = DatabaseIndexingUtils.getPayloadKeyMap("dummy", null);
assertThat(map).isEmpty();
}
@Test
public void testGetPayloadFromMap_NullMap_ReturnsNull() {
ResultPayload payload = DatabaseIndexingUtils.getPayloadFromUriMap(null, "");
final String className = "com.android.settings.system.SystemDashboardFragment";
final Map<String, ResultPayload> map =
DatabaseIndexingUtils.getPayloadKeyMap(className, mContext);
ResultPayload payload = map.get(null);
assertThat(payload).isNull();
}
@Test
public void testGetPayloadFromMap_MatchingKey_ReturnsPayload() {
final String key = "key";
PreferenceControllerMixin prefController = new PreferenceControllerMixin() {
@Override
public ResultPayload getResultPayload() {
return new ResultPayload(null);
}
};
ArrayMap<String, PreferenceControllerMixin> map = new ArrayMap<>();
map.put(key, prefController);
ResultPayload payload = DatabaseIndexingUtils.getPayloadFromUriMap(map, key);
assertThat(payload).isInstanceOf(ResultPayload.class);
}
}