Home | History | Annotate | Download | only in testing
      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 android.content.ContentResolver;
     18 import android.provider.Settings;
     19 import android.provider.Settings.Global;
     20 import android.provider.Settings.Secure;
     21 import android.support.test.InstrumentationRegistry;
     22 import android.support.test.runner.AndroidJUnit4;
     23 import android.test.suitebuilder.annotation.SmallTest;
     24 import org.junit.Before;
     25 import org.junit.Rule;
     26 import org.junit.Test;
     27 import org.junit.runner.RunWith;
     28 
     29 import static org.junit.Assert.*;
     30 
     31 @SmallTest
     32 @RunWith(AndroidJUnit4.class)
     33 public class TestableSettingsProviderTest {
     34 
     35     public static final String NONEXISTENT_SETTING = "nonexistent_setting";
     36     private static final String TAG = "TestableSettingsProviderTest";
     37     private ContentResolver mContentResolver;
     38     @Rule
     39     public final TestableContext mContext =
     40             new TestableContext(InstrumentationRegistry.getContext());
     41 
     42     @Before
     43     public void setup() {
     44         mContentResolver = mContext.getContentResolver();
     45         Settings.Secure.putString(mContentResolver, NONEXISTENT_SETTING, null);
     46         Settings.Global.putString(mContentResolver, NONEXISTENT_SETTING, "initial value");
     47         Settings.Global.putString(mContentResolver, Global.DEVICE_PROVISIONED, null);
     48     }
     49 
     50     @Test
     51     public void testInitialValueSecure() {
     52         String value = Secure.getString(mContentResolver, NONEXISTENT_SETTING);
     53         assertNull(value);
     54     }
     55 
     56     @Test
     57     public void testInitialValueGlobal() {
     58         String value = Global.getString(mContentResolver, NONEXISTENT_SETTING);
     59         assertEquals("initial value", value);
     60     }
     61 
     62     @Test
     63     public void testSeparateTables() {
     64         Secure.putString(mContentResolver, NONEXISTENT_SETTING, "something");
     65         Global.putString(mContentResolver, NONEXISTENT_SETTING, "else");
     66         assertEquals("something", Secure.getString(mContentResolver, NONEXISTENT_SETTING));
     67         assertEquals("else", Global.getString(mContentResolver, NONEXISTENT_SETTING));
     68     }
     69 
     70     @Test
     71     public void testSeparateUsers() {
     72         Secure.putStringForUser(mContentResolver, NONEXISTENT_SETTING, "something", 0);
     73         Secure.putStringForUser(mContentResolver, NONEXISTENT_SETTING, "else", 1);
     74         assertEquals("something",
     75                 Secure.getStringForUser(mContentResolver, NONEXISTENT_SETTING, 0));
     76         assertEquals("else",
     77                 Secure.getStringForUser(mContentResolver, NONEXISTENT_SETTING, 1));
     78     }
     79 
     80     @Test
     81     public void testPassThrough() {
     82         // Grab the value of a setting that is not overridden.
     83         assertTrue(Secure.getInt(mContentResolver, Secure.USER_SETUP_COMPLETE, 0) != 0);
     84     }
     85 
     86     @Test
     87     public void testOverrideExisting() {
     88         // Grab the value of a setting that is overridden and will be different than the actual
     89         // value.
     90         assertNull(Global.getString(mContentResolver, Global.DEVICE_PROVISIONED));
     91     }
     92 
     93     @Test
     94     public void testRelease() {
     95         // Verify different value.
     96         assertNull(Global.getString(mContentResolver, Global.DEVICE_PROVISIONED));
     97         mContext.getSettingsProvider().clearValuesAndCheck(mContext);
     98         // Verify actual value after release.
     99         assertEquals("1", Global.getString(mContentResolver, Global.DEVICE_PROVISIONED));
    100     }
    101 }
    102