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