1 package com.android.settings.search; 2 3 import android.content.Context; 4 import android.content.ContentResolver; 5 import android.content.Intent; 6 import android.os.Parcel; 7 import android.provider.Settings; 8 import com.android.settings.TestConfig; 9 import com.android.settings.search.ResultPayload.SettingsSource; 10 import com.android.settings.testutils.SettingsRobolectricTestRunner; 11 import org.junit.Before; 12 import org.junit.Test; 13 import org.junit.runner.RunWith; 14 import org.robolectric.RuntimeEnvironment; 15 import org.robolectric.annotation.Config; 16 17 import static android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE; 18 import static com.google.common.truth.Truth.assertThat; 19 20 @RunWith(SettingsRobolectricTestRunner.class) 21 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) 22 public class InlinePayloadTest { 23 24 private Context mContext; 25 26 private final String KEY = "key"; 27 28 @Before setUp()29 public void setUp() { 30 mContext = RuntimeEnvironment.application; 31 } 32 33 @Test testGetSecure_returnsSecureSetting()34 public void testGetSecure_returnsSecureSetting() { 35 InlinePayload payload = getDummyPayload(SettingsSource.SECURE); 36 int currentValue = 2; 37 Settings.Secure.putInt(mContext.getContentResolver(), KEY, currentValue); 38 39 int newValue = payload.getValue(mContext); 40 41 assertThat(newValue).isEqualTo(currentValue); 42 } 43 44 @Test testGetGlobal_returnsGlobalSetting()45 public void testGetGlobal_returnsGlobalSetting() { 46 InlinePayload payload = getDummyPayload(SettingsSource.GLOBAL); 47 int currentValue = 2; 48 Settings.Global.putInt(mContext.getContentResolver(), KEY, currentValue); 49 50 int newValue = payload.getValue(mContext); 51 52 assertThat(newValue).isEqualTo(currentValue); 53 } 54 55 @Test testGetSystem_returnsSystemSetting()56 public void testGetSystem_returnsSystemSetting() { 57 InlinePayload payload = getDummyPayload(SettingsSource.SYSTEM); 58 int currentValue = 2; 59 Settings.System.putInt(mContext.getContentResolver(), KEY, currentValue); 60 61 int newValue = payload.getValue(mContext); 62 63 assertThat(newValue).isEqualTo(currentValue); 64 } 65 66 @Test testSetSecure_updatesSecureSetting()67 public void testSetSecure_updatesSecureSetting() { 68 InlinePayload payload = getDummyPayload(SettingsSource.SECURE); 69 int newValue = 1; 70 ContentResolver resolver = mContext.getContentResolver(); 71 Settings.Secure.putInt(resolver, KEY, 0); 72 73 payload.setValue(mContext, newValue); 74 int updatedValue = Settings.System.getInt(resolver, KEY, -1); 75 76 assertThat(updatedValue).isEqualTo(newValue); 77 } 78 79 @Test testSetGlobal_updatesGlobalSetting()80 public void testSetGlobal_updatesGlobalSetting() { 81 InlinePayload payload = getDummyPayload(SettingsSource.GLOBAL); 82 int newValue = 1; 83 ContentResolver resolver = mContext.getContentResolver(); 84 Settings.Global.putInt(resolver, KEY, 0); 85 86 payload.setValue(mContext, newValue); 87 int updatedValue = Settings.Global.getInt(resolver, KEY, -1); 88 89 assertThat(updatedValue).isEqualTo(newValue); 90 } 91 92 @Test testSetSystem_updatesSystemSetting()93 public void testSetSystem_updatesSystemSetting() { 94 InlinePayload payload = getDummyPayload(SettingsSource.SECURE); 95 int newValue = 1; 96 ContentResolver resolver = mContext.getContentResolver(); 97 Settings.System.putInt(resolver, SCREEN_BRIGHTNESS_MODE, 0); 98 99 payload.setValue(mContext, newValue); 100 int updatedValue = Settings.System.getInt(resolver, KEY, -1); 101 102 assertThat(updatedValue).isEqualTo(newValue); 103 } 104 getDummyPayload(int source)105 private InlinePayload getDummyPayload(int source) { 106 return new ConcreteInlinePayload(KEY, source, null /* intent */, 107 true /* isDeviceSupported */); 108 } 109 110 class ConcreteInlinePayload extends InlinePayload { 111 ConcreteInlinePayload(String key, @SettingsSource int source, Intent intent, boolean isDeviceSupported)112 public ConcreteInlinePayload(String key, @SettingsSource int source, Intent intent, 113 boolean isDeviceSupported) { 114 super(key, source, intent, isDeviceSupported, 0 /* defaultValue */); 115 } 116 117 @Override getType()118 public int getType() { 119 return 0; 120 } 121 122 @Override standardizeInput(int input)123 protected int standardizeInput(int input) throws IllegalArgumentException { 124 return input; 125 } 126 } 127 } 128