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.Ignore; 40import org.junit.Test; 41import org.junit.runner.RunWith; 42import org.mockito.Mock; 43import org.mockito.MockitoAnnotations; 44 45import java.io.File; 46 47/** 48 * Test class for {@link CoreSettingsObserver}. 49 * 50 * To run the tests, use 51 * 52 * runtest -c com.android.server.am.CoreSettingsObserverTest frameworks-services 53 * 54 * or the following steps: 55 * 56 * Build: m FrameworksServicesTests 57 * Install: adb install -r \ 58 * ${ANDROID_PRODUCT_OUT}/data/app/FrameworksServicesTests/FrameworksServicesTests.apk 59 * Run: adb shell am instrument -e class com.android.server.am.CoreSettingsObserverTest -w \ 60 * com.android.frameworks.servicestests/android.support.test.runner.AndroidJUnitRunner 61 */ 62@Ignore 63@SmallTest 64@RunWith(AndroidJUnit4.class) 65public class CoreSettingsObserverTest { 66 private static final String TEST_SETTING_SECURE_INT = "secureInt"; 67 private static final String TEST_SETTING_GLOBAL_FLOAT = "globalFloat"; 68 private static final String TEST_SETTING_SYSTEM_STRING = "systemString"; 69 70 private static final int TEST_INT = 111; 71 private static final float TEST_FLOAT = 3.14f; 72 private static final String TEST_STRING = "testString"; 73 74 private ActivityManagerService mAms; 75 @Mock private Context mContext; 76 77 private MockContentResolver mContentResolver; 78 private CoreSettingsObserver mCoreSettingsObserver; 79 80 @BeforeClass 81 public static void setupOnce() { 82 CoreSettingsObserver.sSecureSettingToTypeMap.put(TEST_SETTING_SECURE_INT, int.class); 83 CoreSettingsObserver.sGlobalSettingToTypeMap.put(TEST_SETTING_GLOBAL_FLOAT, float.class); 84 CoreSettingsObserver.sSystemSettingToTypeMap.put(TEST_SETTING_SYSTEM_STRING, String.class); 85 } 86 87 @Before 88 public void setUp() { 89 MockitoAnnotations.initMocks(this); 90 91 final Context originalContext = InstrumentationRegistry.getContext(); 92 when(mContext.getApplicationInfo()).thenReturn(originalContext.getApplicationInfo()); 93 mContentResolver = new MockContentResolver(mContext); 94 mContentResolver.addProvider(Settings.AUTHORITY, new FakeSettingsProvider()); 95 when(mContext.getContentResolver()).thenReturn(mContentResolver); 96 97 mAms = new ActivityManagerService(new TestInjector()); 98 mCoreSettingsObserver = new CoreSettingsObserver(mAms); 99 } 100 101 @Test 102 public void testPopulateSettings() { 103 Settings.Secure.putInt(mContentResolver, TEST_SETTING_SECURE_INT, TEST_INT); 104 Settings.Global.putFloat(mContentResolver, TEST_SETTING_GLOBAL_FLOAT, TEST_FLOAT); 105 Settings.System.putString(mContentResolver, TEST_SETTING_SYSTEM_STRING, TEST_STRING); 106 107 final Bundle settingsBundle = getPopulatedBundle(); 108 109 assertEquals("Unexpected value of " + TEST_SETTING_SECURE_INT, 110 TEST_INT, settingsBundle.getInt(TEST_SETTING_SECURE_INT)); 111 assertEquals("Unexpected value of " + TEST_SETTING_GLOBAL_FLOAT, 112 TEST_FLOAT, settingsBundle.getFloat(TEST_SETTING_GLOBAL_FLOAT), 0); 113 assertEquals("Unexpected value of " + TEST_SETTING_SYSTEM_STRING, 114 TEST_STRING, settingsBundle.getString(TEST_SETTING_SYSTEM_STRING)); 115 } 116 117 @Test 118 public void testPopulateSettings_settingNotSet() { 119 final Bundle settingsBundle = getPopulatedBundle(); 120 121 assertFalse("Bundle should not contain " + TEST_SETTING_SECURE_INT, 122 settingsBundle.containsKey(TEST_SETTING_SECURE_INT)); 123 assertFalse("Bundle should not contain " + TEST_SETTING_GLOBAL_FLOAT, 124 settingsBundle.containsKey(TEST_SETTING_GLOBAL_FLOAT)); 125 assertFalse("Bundle should not contain " + TEST_SETTING_SYSTEM_STRING, 126 settingsBundle.containsKey(TEST_SETTING_SYSTEM_STRING)); 127 } 128 129 private Bundle getPopulatedBundle() { 130 final Bundle settingsBundle = new Bundle(); 131 mCoreSettingsObserver.populateSettings(settingsBundle, 132 CoreSettingsObserver.sGlobalSettingToTypeMap); 133 mCoreSettingsObserver.populateSettings(settingsBundle, 134 CoreSettingsObserver.sSecureSettingToTypeMap); 135 mCoreSettingsObserver.populateSettings(settingsBundle, 136 CoreSettingsObserver.sSystemSettingToTypeMap); 137 return settingsBundle; 138 } 139 140 private class TestInjector extends Injector { 141 @Override 142 public Context getContext() { 143 return mContext; 144 } 145 146 public AppOpsService getAppOpsService(File file, Handler handler) { 147 return null; 148 } 149 150 @Override 151 public Handler getUiHandler(ActivityManagerService service) { 152 return null; 153 } 154 } 155} 156