Simplify InlineSwitchPayloads and generalize get/set method

InlineSwitchPayload now assumes that all switches will be
stored as 1 or 0, which simplifies the code.

Moves Availability into ResultPayload, so that custom
payloads can be created to set/get values which are more
complicated than stotring ints (like bluetooth or DnD),
and give more expressive reasons when unavailable.

Bug: 62022517
Test: make RunSettingsRoboTests
Change-Id: I87e6fc041bfd398e7daf6e6e479d502930d36f0f
This commit is contained in:
Matthew Fritze
2017-05-23 09:42:33 -07:00
parent 63b013ea60
commit 2b1a88da3d
13 changed files with 378 additions and 190 deletions

View File

@@ -17,76 +17,57 @@
package com.android.settings.search;
import android.content.ContentResolver;
import android.content.Intent;
import android.os.Parcel;
import android.util.ArrayMap;
import android.provider.Settings;
import android.content.Context;
import com.android.settings.SettingsRobolectricTestRunner;
import com.android.settings.TestConfig;
import com.android.settings.search.ResultPayload.Availability;
import com.android.settings.search.ResultPayload.SettingsSource;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
import org.robolectric.shadows.ShadowApplication;
import static android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE;
import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.fail;
@RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
public class InlineSwitchPayloadTest {
@Test
public void testGetSwitch_EmptyMap_ExceptionThrown() {
final String uri = "test.com";
final int source = ResultPayload.SettingsSource.SECURE;
private static final String DUMMY_SETTING = "inline_test";
private static final int STANDARD_ON = 1;
private static final int FLIPPED_ON = 0;
final Context context = ShadowApplication.getInstance().getApplicationContext();
InlineSwitchPayload payload = new InlineSwitchPayload(uri, source, null, null);
try {
payload.getSwitchValue(context);
fail("Should have thrown exception for null map");
} catch (IllegalStateException e) {
assertThat(e).isNotNull();
}
}
private Context mContext;
@Test
public void testGetSwitch_BadMap_ExceptionThrown() {
final String uri = "test.com";
final int source = ResultPayload.SettingsSource.SECURE;
final ArrayMap<Integer, Boolean> map = new ArrayMap<>();
final Context context = ShadowApplication.getInstance().getApplicationContext();
InlineSwitchPayload payload = new InlineSwitchPayload(uri, source, map, null);
try {
payload.getSwitchValue(context);
fail("Should have thrown exception for bad map");
} catch (IllegalStateException e) {
assertThat(e).isNotNull();
}
@Before
public void setUp() {
mContext = RuntimeEnvironment.application;
}
@Test
public void testConstructor_DataRetained() {
final String uri = "test.com";
final int type = ResultPayload.PayloadType.INLINE_SWITCH;
final int source = ResultPayload.SettingsSource.SECURE;
final ArrayMap<Integer, Boolean> map = new ArrayMap<>();
map.put(1, true);
map.put(0, false);
final int source = SettingsSource.SECURE;
final String intentKey = "key";
final String intentVal = "value";
final Intent intent = new Intent();
intent.putExtra(intentKey, intentVal);
InlineSwitchPayload payload = new InlineSwitchPayload(uri, source, map, intent);
InlineSwitchPayload payload = new InlineSwitchPayload(uri, source, 1, intent, true);
final Intent retainedIntent = payload.getIntent();
assertThat(payload.settingsUri).isEqualTo(uri);
assertThat(payload.inlineType).isEqualTo(type);
assertThat(payload.settingSource).isEqualTo(source);
assertThat(payload.valueMap.get(1)).isTrue();
assertThat(payload.valueMap.get(0)).isFalse();
assertThat(payload.mSettingKey).isEqualTo(uri);
assertThat(payload.mInlineType).isEqualTo(type);
assertThat(payload.mSettingSource).isEqualTo(source);
assertThat(payload.isStandard()).isTrue();
assertThat(payload.getAvailability()).isEqualTo(ResultPayload.Availability.AVAILABLE);
assertThat(retainedIntent.getStringExtra(intentKey)).isEqualTo(intentVal);
}
@@ -94,32 +75,150 @@ public class InlineSwitchPayloadTest {
public void testParcelConstructor_DataRetained() {
String uri = "test.com";
int type = ResultPayload.PayloadType.INLINE_SWITCH;
int source = ResultPayload.SettingsSource.SECURE;
final ArrayMap<Integer, Boolean> map = new ArrayMap<>();
map.put(1, true);
map.put(0, false);
int source = SettingsSource.SECURE;
final String intentKey = "key";
final String intentVal = "value";
final Intent intent = new Intent();
intent.putExtra(intentKey, intentVal);
Parcel parcel = Parcel.obtain();
parcel.writeParcelable(intent, 0);
parcel.writeString(uri);
parcel.writeInt(type);
parcel.writeInt(source);
parcel.writeParcelable(intent, 0);
parcel.writeMap(map);
parcel.writeInt(InlineSwitchPayload.TRUE);
parcel.writeInt(InlineSwitchPayload.TRUE);
parcel.setDataPosition(0);
InlineSwitchPayload payload = InlineSwitchPayload.CREATOR.createFromParcel(parcel);
final Intent builtIntent = payload.getIntent();
assertThat(payload.settingsUri).isEqualTo(uri);
assertThat(payload.inlineType).isEqualTo(type);
assertThat(payload.settingSource).isEqualTo(source);
assertThat(payload.valueMap.get(1)).isTrue();
assertThat(payload.valueMap.get(0)).isFalse();
assertThat(payload.mSettingKey).isEqualTo(uri);
assertThat(payload.mInlineType).isEqualTo(type);
assertThat(payload.mSettingSource).isEqualTo(source);
assertThat(payload.isStandard()).isTrue();
assertThat(payload.getAvailability()).isEqualTo(Availability.AVAILABLE);
assertThat(builtIntent.getStringExtra(intentKey)).isEqualTo(intentVal);
}
@Test
public void testGetSecure_returnsSecureSetting() {
InlineSwitchPayload payload = new InlineSwitchPayload(DUMMY_SETTING, SettingsSource.SECURE,
STANDARD_ON, null /* intent */, true);
int currentValue = 1;
Settings.Secure.putInt(mContext.getContentResolver(), DUMMY_SETTING, currentValue);
int newValue = payload.getValue(mContext);
assertThat(newValue).isEqualTo(currentValue);
}
@Test
public void testGetGlobal_returnsGlobalSetting() {
InlineSwitchPayload payload = new InlineSwitchPayload(DUMMY_SETTING, SettingsSource.GLOBAL,
STANDARD_ON, null /* intent */, true);
int currentValue = 1;
Settings.Global.putInt(mContext.getContentResolver(), DUMMY_SETTING, currentValue);
int newValue = payload.getValue(mContext);
assertThat(newValue).isEqualTo(currentValue);
}
@Test
public void testGetSystem_returnsSystemSetting() {
InlineSwitchPayload payload = new InlineSwitchPayload(DUMMY_SETTING, SettingsSource.SYSTEM,
STANDARD_ON, null /* intent */, true);
int currentValue = 1;
Settings.System.putInt(mContext.getContentResolver(), DUMMY_SETTING, currentValue);
int newValue = payload.getValue(mContext);
assertThat(newValue).isEqualTo(currentValue);
}
@Test
public void testSetSecure_updatesSecureSetting() {
InlineSwitchPayload payload = new InlineSwitchPayload(DUMMY_SETTING, SettingsSource.SECURE,
STANDARD_ON, null /* intent */, true);
int newValue = 1;
ContentResolver resolver = mContext.getContentResolver();
Settings.Secure.putInt(resolver, SCREEN_BRIGHTNESS_MODE, 0);
payload.setValue(mContext, newValue);
int updatedValue = Settings.System.getInt(resolver, DUMMY_SETTING, -1);
assertThat(updatedValue).isEqualTo(newValue);
}
@Test
public void testSetGlobal_updatesGlobalSetting() {
InlineSwitchPayload payload = new InlineSwitchPayload(DUMMY_SETTING, SettingsSource.GLOBAL,
STANDARD_ON, null /* intent */, true);
int newValue = 1;
ContentResolver resolver = mContext.getContentResolver();
Settings.Global.putInt(resolver, SCREEN_BRIGHTNESS_MODE, 0);
payload.setValue(mContext, newValue);
int updatedValue = Settings.Global.getInt(resolver, DUMMY_SETTING, -1);
assertThat(updatedValue).isEqualTo(newValue);
}
@Test
public void testSetSystem_updatesSystemSetting() {
InlineSwitchPayload payload = new InlineSwitchPayload(DUMMY_SETTING, SettingsSource.SYSTEM,
STANDARD_ON, null /* intent */, true);
int newValue = 1;
ContentResolver resolver = mContext.getContentResolver();
Settings.System.putInt(resolver, SCREEN_BRIGHTNESS_MODE, 0);
payload.setValue(mContext, newValue);
int updatedValue = Settings.System.getInt(resolver, DUMMY_SETTING, -1);
assertThat(updatedValue).isEqualTo(newValue);
}
@Test
public void testGetSystem_flippedSetting_returnsFlippedValue() {
// Stores 1s as 0s, and vis versa
InlineSwitchPayload payload = new InlineSwitchPayload(DUMMY_SETTING, SettingsSource.SYSTEM,
FLIPPED_ON, null /* intent */, true);
int currentValue = 1;
Settings.System.putInt(mContext.getContentResolver(), DUMMY_SETTING, currentValue);
int newValue = payload.getValue(mContext);
assertThat(newValue).isEqualTo(1 - currentValue);
}
@Test
public void testSetSystem_flippedSetting_updatesToFlippedValue() {
// Stores 1s as 0s, and vis versa
InlineSwitchPayload payload = new InlineSwitchPayload(DUMMY_SETTING, SettingsSource.SYSTEM,
FLIPPED_ON, null /* intent */, true);
int newValue = 1;
ContentResolver resolver = mContext.getContentResolver();
Settings.System.putInt(resolver, SCREEN_BRIGHTNESS_MODE, newValue);
payload.setValue(mContext, newValue);
int updatedValue = Settings.System.getInt(resolver, DUMMY_SETTING, -1);
assertThat(updatedValue).isEqualTo(1 - newValue);
}
@Test(expected = IllegalArgumentException.class)
public void testSetSystem_negativeValue_ThrowsError() {
InlineSwitchPayload payload = new InlineSwitchPayload(DUMMY_SETTING, SettingsSource.SYSTEM,
STANDARD_ON, null /* intent */, true);
payload.setValue(mContext, -1);
}
@Test(expected = IllegalArgumentException.class)
public void testSetSystem_highValue_ThrowsError() {
InlineSwitchPayload payload = new InlineSwitchPayload(DUMMY_SETTING, SettingsSource.SYSTEM,
STANDARD_ON, null /* intent */, true);
payload.setValue(mContext, 2);
}
}