1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
5  * except in compliance with the License. You may obtain a copy of the License at
6  *
7  *      http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software distributed under the
10  * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
11  * KIND, either express or implied. See the License for the specific language governing
12  * permissions and limitations under the License.
13  */
14 
15 package android.testing;
16 
17 import static org.junit.Assert.*;
18 
19 import android.content.ContentResolver;
20 import android.provider.Settings;
21 import android.provider.Settings.Global;
22 import android.provider.Settings.Secure;
23 import android.test.suitebuilder.annotation.SmallTest;
24 
25 import androidx.test.InstrumentationRegistry;
26 import androidx.test.runner.AndroidJUnit4;
27 
28 import org.junit.Before;
29 import org.junit.Rule;
30 import org.junit.Test;
31 import org.junit.runner.RunWith;
32 
33 @SmallTest
34 @RunWith(AndroidJUnit4.class)
35 public class TestableSettingsProviderTest {
36 
37     public static final String NONEXISTENT_SETTING = "nonexistent_setting";
38     private static final String TAG = "TestableSettingsProviderTest";
39     private ContentResolver mContentResolver;
40     @Rule
41     public final TestableContext mContext =
42             new TestableContext(InstrumentationRegistry.getContext());
43 
44     @Before
setup()45     public void setup() {
46         mContentResolver = mContext.getContentResolver();
47         Settings.Secure.putString(mContentResolver, NONEXISTENT_SETTING, null);
48         Settings.Global.putString(mContentResolver, NONEXISTENT_SETTING, "initial value");
49         Settings.Global.putString(mContentResolver, Global.DEVICE_PROVISIONED, null);
50     }
51 
52     @Test
testInitialValueSecure()53     public void testInitialValueSecure() {
54         String value = Secure.getString(mContentResolver, NONEXISTENT_SETTING);
55         assertNull(value);
56     }
57 
58     @Test
testInitialValueGlobal()59     public void testInitialValueGlobal() {
60         String value = Global.getString(mContentResolver, NONEXISTENT_SETTING);
61         assertEquals("initial value", value);
62     }
63 
64     @Test
testSeparateTables()65     public void testSeparateTables() {
66         Secure.putString(mContentResolver, NONEXISTENT_SETTING, "something");
67         Global.putString(mContentResolver, NONEXISTENT_SETTING, "else");
68         assertEquals("something", Secure.getString(mContentResolver, NONEXISTENT_SETTING));
69         assertEquals("else", Global.getString(mContentResolver, NONEXISTENT_SETTING));
70     }
71 
72     @Test
testSeparateUsers()73     public void testSeparateUsers() {
74         Secure.putStringForUser(mContentResolver, NONEXISTENT_SETTING, "something", 0);
75         Secure.putStringForUser(mContentResolver, NONEXISTENT_SETTING, "else", 1);
76         assertEquals("something",
77                 Secure.getStringForUser(mContentResolver, NONEXISTENT_SETTING, 0));
78         assertEquals("else",
79                 Secure.getStringForUser(mContentResolver, NONEXISTENT_SETTING, 1));
80     }
81 
82     @Test
testPassThrough()83     public void testPassThrough() {
84         // Grab the value of a setting that is not overridden.
85         assertTrue(Secure.getInt(mContentResolver, Secure.USER_SETUP_COMPLETE, 0) != 0);
86     }
87 
88     @Test
testOverrideExisting()89     public void testOverrideExisting() {
90         // Grab the value of a setting that is overridden and will be different than the actual
91         // value.
92         assertNull(Global.getString(mContentResolver, Global.DEVICE_PROVISIONED));
93     }
94 
95     @Test
testRelease()96     public void testRelease() {
97         // Verify different value.
98         assertNull(Global.getString(mContentResolver, Global.DEVICE_PROVISIONED));
99         mContext.getSettingsProvider().clearValuesAndCheck(mContext);
100         // Verify actual value after release.
101         assertEquals("1", Global.getString(mContentResolver, Global.DEVICE_PROVISIONED));
102     }
103 }
104