1package com.xtremelabs.robolectric.shadows;
2
3import android.app.Activity;
4import android.app.Application;
5import android.content.ContentResolver;
6import android.provider.Settings;
7import com.xtremelabs.robolectric.Robolectric;
8import com.xtremelabs.robolectric.WithTestDefaultsRunner;
9import org.junit.Before;
10import org.junit.Test;
11import org.junit.runner.RunWith;
12
13import static org.hamcrest.CoreMatchers.equalTo;
14import static org.hamcrest.CoreMatchers.nullValue;
15import static org.junit.Assert.assertThat;
16
17@RunWith(WithTestDefaultsRunner.class)
18public class SettingsTest {
19    private Activity activity;
20    private ContentResolver contentResolver;
21
22    @Before
23    public void setUp() throws Exception {
24        activity = new Activity();
25        contentResolver = activity.getContentResolver();
26    }
27
28    @Test
29    public void whileApplicationStaysSame_shouldRememberOldSettings() throws Exception {
30        Settings.System.putInt(contentResolver, "property", 1);
31        assertThat(Settings.System.getInt(contentResolver, "property", 0), equalTo(1));
32
33        activity = new Activity();
34        contentResolver = activity.getContentResolver();
35        assertThat(Settings.System.getInt(contentResolver, "property", 0), equalTo(1));
36    }
37
38    @Test
39    public void whenApplicationChanges_shouldStartWithNewSettings() throws Exception {
40        Settings.System.putInt(contentResolver, "property", 1);
41        assertThat(Settings.System.getInt(contentResolver, "property", 0), equalTo(1));
42
43        Robolectric.application = new Application();
44        activity = new Activity();
45        contentResolver = activity.getContentResolver();
46        assertThat(Settings.System.getInt(contentResolver, "property", 0), equalTo(0));
47    }
48
49    @Test
50    public void testSystemGetInt() throws Exception {
51        assertThat(Settings.System.getInt(contentResolver, "property", 0), equalTo(0));
52        assertThat(Settings.System.getInt(contentResolver, "property", 2), equalTo(2));
53
54        Settings.System.putInt(contentResolver, "property", 1);
55        assertThat(Settings.System.getInt(contentResolver, "property", 0), equalTo(1));
56    }
57
58    @Test
59    public void testSecureGetInt() throws Exception {
60        assertThat(Settings.Secure.getInt(contentResolver, "property", 0), equalTo(0));
61        assertThat(Settings.Secure.getInt(contentResolver, "property", 2), equalTo(2));
62
63        Settings.Secure.putInt(contentResolver, "property", 1);
64        assertThat(Settings.Secure.getInt(contentResolver, "property", 0), equalTo(1));
65    }
66
67    @Test
68    public void testSystemGetString() throws Exception {
69        assertThat(Settings.System.getString(contentResolver, "property"), nullValue());
70
71        Settings.System.putString(contentResolver, "property", "value");
72        assertThat(Settings.System.getString(contentResolver, "property"), equalTo("value"));
73    }
74
75    @Test
76    public void testSystemGetLong() throws Exception {
77        assertThat(Settings.System.getLong(contentResolver, "property", 10L), equalTo(10L));
78        Settings.System.putLong(contentResolver, "property", 42L);
79        assertThat(Settings.System.getLong(contentResolver, "property"), equalTo(42L));
80        assertThat(Settings.System.getLong(contentResolver, "property", 10L), equalTo(42L));
81    }
82
83    @Test
84    public void testSystemGetFloat() throws Exception {
85        assertThat(Settings.System.getFloat(contentResolver, "property", 23.23f), equalTo(23.23f));
86        Settings.System.putFloat(contentResolver, "property", 42.42f);
87        assertThat(Settings.System.getFloat(contentResolver, "property", 10L), equalTo(42.42f));
88    }
89
90    @Test(expected = Settings.SettingNotFoundException.class)
91    public void testSystemGetLong_exception() throws Exception {
92        Settings.System.getLong(contentResolver, "property");
93    }
94
95    @Test(expected = Settings.SettingNotFoundException.class)
96    public void testSystemGetInt_exception() throws Exception {
97        Settings.System.getInt(contentResolver, "property");
98    }
99
100    @Test(expected = Settings.SettingNotFoundException.class)
101    public void testSystemGetFloat_exception() throws Exception {
102        Settings.System.getFloat(contentResolver, "property");
103    }
104}
105