1package com.android.settings.search;
2
3import android.content.Context;
4import android.content.ContentResolver;
5import android.content.Intent;
6import android.os.Parcel;
7import android.provider.Settings;
8import com.android.settings.TestConfig;
9import com.android.settings.search.ResultPayload.SettingsSource;
10import com.android.settings.testutils.SettingsRobolectricTestRunner;
11import org.junit.Before;
12import org.junit.Test;
13import org.junit.runner.RunWith;
14import org.robolectric.RuntimeEnvironment;
15import org.robolectric.annotation.Config;
16
17import static android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE;
18import static com.google.common.truth.Truth.assertThat;
19
20@RunWith(SettingsRobolectricTestRunner.class)
21@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
22public class InlinePayloadTest {
23
24    private Context mContext;
25
26    private final String KEY = "key";
27
28    @Before
29    public void setUp() {
30        mContext = RuntimeEnvironment.application;
31    }
32
33    @Test
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
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
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
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
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
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
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
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
118        public int getType() {
119            return 0;
120        }
121
122        @Override
123        protected int standardizeInput(int input) throws IllegalArgumentException {
124            return input;
125        }
126    }
127}
128