CoreSettingsObserverTest.java revision bce47223cb6e178fe3a2547974c93f32548c12ed
1/* 2 * Copyright (C) 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License 15 */ 16 17package com.android.server.am; 18 19import static com.android.server.am.ActivityManagerService.Injector; 20 21import static org.junit.Assert.assertEquals; 22import static org.junit.Assert.assertFalse; 23import static org.mockito.Mockito.when; 24 25import android.content.Context; 26import android.os.Bundle; 27import android.os.Handler; 28import android.provider.Settings; 29import android.support.test.InstrumentationRegistry; 30import android.support.test.filters.SmallTest; 31import android.support.test.runner.AndroidJUnit4; 32import android.test.mock.MockContentResolver; 33 34import com.android.internal.util.test.FakeSettingsProvider; 35import com.android.server.AppOpsService; 36 37import org.junit.Before; 38import org.junit.BeforeClass; 39import org.junit.Test; 40import org.junit.runner.RunWith; 41import org.mockito.Mock; 42import org.mockito.MockitoAnnotations; 43 44import java.io.File; 45 46/** 47 * Test class for {@link CoreSettingsObserver}. 48 * 49 * To run the tests, use 50 * 51 * runtest -c com.android.server.am.CoreSettingsObserverTest frameworks-services 52 * 53 * or the following steps: 54 * 55 * Build: m FrameworksServicesTests 56 * Install: adb install -r \ 57 * ${ANDROID_PRODUCT_OUT}/data/app/FrameworksServicesTests/FrameworksServicesTests.apk 58 * Run: adb shell am instrument -e class com.android.server.am.CoreSettingsObserverTest -w \ 59 * com.android.frameworks.servicestests/android.support.test.runner.AndroidJUnitRunner 60 */ 61@SmallTest 62@RunWith(AndroidJUnit4.class) 63public class CoreSettingsObserverTest { 64 private static final String TEST_SETTING_SECURE_INT = "secureInt"; 65 private static final String TEST_SETTING_GLOBAL_FLOAT = "globalFloat"; 66 private static final String TEST_SETTING_SYSTEM_STRING = "systemString"; 67 68 private static final int TEST_INT = 111; 69 private static final float TEST_FLOAT = 3.14f; 70 private static final String TEST_STRING = "testString"; 71 72 private ActivityManagerService mAms; 73 @Mock private Context mContext; 74 75 private MockContentResolver mContentResolver; 76 private CoreSettingsObserver mCoreSettingsObserver; 77 78 @BeforeClass 79 public static void setupOnce() { 80 CoreSettingsObserver.sSecureSettingToTypeMap.put(TEST_SETTING_SECURE_INT, int.class); 81 CoreSettingsObserver.sGlobalSettingToTypeMap.put(TEST_SETTING_GLOBAL_FLOAT, float.class); 82 CoreSettingsObserver.sSystemSettingToTypeMap.put(TEST_SETTING_SYSTEM_STRING, String.class); 83 } 84 85 @Before 86 public void setUp() { 87 MockitoAnnotations.initMocks(this); 88 89 final Context originalContext = InstrumentationRegistry.getContext(); 90 when(mContext.getApplicationInfo()).thenReturn(originalContext.getApplicationInfo()); 91 mContentResolver = new MockContentResolver(mContext); 92 mContentResolver.addProvider(Settings.AUTHORITY, new FakeSettingsProvider()); 93 when(mContext.getContentResolver()).thenReturn(mContentResolver); 94 95 mAms = new ActivityManagerService(new TestInjector()); 96 mCoreSettingsObserver = new CoreSettingsObserver(mAms); 97 } 98 99 @Test 100 public void testPopulateSettings() { 101 Settings.Secure.putInt(mContentResolver, TEST_SETTING_SECURE_INT, TEST_INT); 102 Settings.Global.putFloat(mContentResolver, TEST_SETTING_GLOBAL_FLOAT, TEST_FLOAT); 103 Settings.System.putString(mContentResolver, TEST_SETTING_SYSTEM_STRING, TEST_STRING); 104 105 final Bundle settingsBundle = getPopulatedBundle(); 106 107 assertEquals("Unexpected value of " + TEST_SETTING_SECURE_INT, 108 TEST_INT, settingsBundle.getInt(TEST_SETTING_SECURE_INT)); 109 assertEquals("Unexpected value of " + TEST_SETTING_GLOBAL_FLOAT, 110 TEST_FLOAT, settingsBundle.getFloat(TEST_SETTING_GLOBAL_FLOAT), 0); 111 assertEquals("Unexpected value of " + TEST_SETTING_SYSTEM_STRING, 112 TEST_STRING, settingsBundle.getString(TEST_SETTING_SYSTEM_STRING)); 113 } 114 115 @Test 116 public void testPopulateSettings_settingNotSet() { 117 final Bundle settingsBundle = getPopulatedBundle(); 118 119 assertFalse("Bundle should not contain " + TEST_SETTING_SECURE_INT, 120 settingsBundle.containsKey(TEST_SETTING_SECURE_INT)); 121 assertFalse("Bundle should not contain " + TEST_SETTING_GLOBAL_FLOAT, 122 settingsBundle.containsKey(TEST_SETTING_GLOBAL_FLOAT)); 123 assertFalse("Bundle should not contain " + TEST_SETTING_SYSTEM_STRING, 124 settingsBundle.containsKey(TEST_SETTING_SYSTEM_STRING)); 125 } 126 127 private Bundle getPopulatedBundle() { 128 final Bundle settingsBundle = new Bundle(); 129 mCoreSettingsObserver.populateSettings(settingsBundle, 130 CoreSettingsObserver.sGlobalSettingToTypeMap); 131 mCoreSettingsObserver.populateSettings(settingsBundle, 132 CoreSettingsObserver.sSecureSettingToTypeMap); 133 mCoreSettingsObserver.populateSettings(settingsBundle, 134 CoreSettingsObserver.sSystemSettingToTypeMap); 135 return settingsBundle; 136 } 137 138 private class TestInjector extends Injector { 139 @Override 140 public Context getContext() { 141 return mContext; 142 } 143 144 public AppOpsService getAppOpsService(File file, Handler handler) { 145 return null; 146 } 147 148 @Override 149 public Handler getUiHandler(ActivityManagerService service) { 150 return null; 151 } 152 } 153} 154