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