Sudheer Shanka | bce4722 | 2017-04-03 10:51:42 -0700 | [diff] [blame] | 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 | |
| 17 | package com.android.server.am; |
| 18 | |
| 19 | import static com.android.server.am.ActivityManagerService.Injector; |
| 20 | |
| 21 | import static org.junit.Assert.assertEquals; |
| 22 | import static org.junit.Assert.assertFalse; |
| 23 | import static org.mockito.Mockito.when; |
| 24 | |
| 25 | import android.content.Context; |
| 26 | import android.os.Bundle; |
| 27 | import android.os.Handler; |
| 28 | import android.provider.Settings; |
| 29 | import android.support.test.InstrumentationRegistry; |
| 30 | import android.support.test.filters.SmallTest; |
| 31 | import android.support.test.runner.AndroidJUnit4; |
| 32 | import android.test.mock.MockContentResolver; |
| 33 | |
| 34 | import com.android.internal.util.test.FakeSettingsProvider; |
| 35 | import com.android.server.AppOpsService; |
| 36 | |
Sudheer Shanka | aa3c30d | 2017-05-23 15:19:10 -0700 | [diff] [blame] | 37 | import org.junit.AfterClass; |
Sudheer Shanka | bce4722 | 2017-04-03 10:51:42 -0700 | [diff] [blame] | 38 | import org.junit.Before; |
| 39 | import org.junit.BeforeClass; |
Sudheer Shanka | cb2b2ef | 2017-05-18 17:07:16 -0700 | [diff] [blame] | 40 | import org.junit.Ignore; |
Sudheer Shanka | bce4722 | 2017-04-03 10:51:42 -0700 | [diff] [blame] | 41 | import org.junit.Test; |
| 42 | import org.junit.runner.RunWith; |
| 43 | import org.mockito.Mock; |
| 44 | import org.mockito.MockitoAnnotations; |
| 45 | |
| 46 | import java.io.File; |
| 47 | |
| 48 | /** |
| 49 | * Test class for {@link CoreSettingsObserver}. |
| 50 | * |
| 51 | * To run the tests, use |
| 52 | * |
| 53 | * runtest -c com.android.server.am.CoreSettingsObserverTest frameworks-services |
| 54 | * |
| 55 | * or the following steps: |
| 56 | * |
| 57 | * Build: m FrameworksServicesTests |
| 58 | * Install: adb install -r \ |
| 59 | * ${ANDROID_PRODUCT_OUT}/data/app/FrameworksServicesTests/FrameworksServicesTests.apk |
| 60 | * Run: adb shell am instrument -e class com.android.server.am.CoreSettingsObserverTest -w \ |
| 61 | * com.android.frameworks.servicestests/android.support.test.runner.AndroidJUnitRunner |
| 62 | */ |
Sudheer Shanka | bce4722 | 2017-04-03 10:51:42 -0700 | [diff] [blame] | 63 | @SmallTest |
| 64 | @RunWith(AndroidJUnit4.class) |
| 65 | public 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() { |
Sudheer Shanka | aa3c30d | 2017-05-23 15:19:10 -0700 | [diff] [blame] | 82 | FakeSettingsProvider.clearSettingsProvider(); |
Sudheer Shanka | bce4722 | 2017-04-03 10:51:42 -0700 | [diff] [blame] | 83 | CoreSettingsObserver.sSecureSettingToTypeMap.put(TEST_SETTING_SECURE_INT, int.class); |
| 84 | CoreSettingsObserver.sGlobalSettingToTypeMap.put(TEST_SETTING_GLOBAL_FLOAT, float.class); |
| 85 | CoreSettingsObserver.sSystemSettingToTypeMap.put(TEST_SETTING_SYSTEM_STRING, String.class); |
| 86 | } |
| 87 | |
Sudheer Shanka | aa3c30d | 2017-05-23 15:19:10 -0700 | [diff] [blame] | 88 | @AfterClass |
| 89 | public static void tearDownOnce() { |
| 90 | FakeSettingsProvider.clearSettingsProvider(); |
| 91 | } |
| 92 | |
Sudheer Shanka | bce4722 | 2017-04-03 10:51:42 -0700 | [diff] [blame] | 93 | @Before |
| 94 | public void setUp() { |
| 95 | MockitoAnnotations.initMocks(this); |
| 96 | |
| 97 | final Context originalContext = InstrumentationRegistry.getContext(); |
| 98 | when(mContext.getApplicationInfo()).thenReturn(originalContext.getApplicationInfo()); |
| 99 | mContentResolver = new MockContentResolver(mContext); |
| 100 | mContentResolver.addProvider(Settings.AUTHORITY, new FakeSettingsProvider()); |
| 101 | when(mContext.getContentResolver()).thenReturn(mContentResolver); |
| 102 | |
| 103 | mAms = new ActivityManagerService(new TestInjector()); |
| 104 | mCoreSettingsObserver = new CoreSettingsObserver(mAms); |
| 105 | } |
| 106 | |
| 107 | @Test |
| 108 | public void testPopulateSettings() { |
| 109 | Settings.Secure.putInt(mContentResolver, TEST_SETTING_SECURE_INT, TEST_INT); |
| 110 | Settings.Global.putFloat(mContentResolver, TEST_SETTING_GLOBAL_FLOAT, TEST_FLOAT); |
| 111 | Settings.System.putString(mContentResolver, TEST_SETTING_SYSTEM_STRING, TEST_STRING); |
| 112 | |
| 113 | final Bundle settingsBundle = getPopulatedBundle(); |
| 114 | |
| 115 | assertEquals("Unexpected value of " + TEST_SETTING_SECURE_INT, |
| 116 | TEST_INT, settingsBundle.getInt(TEST_SETTING_SECURE_INT)); |
| 117 | assertEquals("Unexpected value of " + TEST_SETTING_GLOBAL_FLOAT, |
| 118 | TEST_FLOAT, settingsBundle.getFloat(TEST_SETTING_GLOBAL_FLOAT), 0); |
| 119 | assertEquals("Unexpected value of " + TEST_SETTING_SYSTEM_STRING, |
| 120 | TEST_STRING, settingsBundle.getString(TEST_SETTING_SYSTEM_STRING)); |
| 121 | } |
| 122 | |
| 123 | @Test |
| 124 | public void testPopulateSettings_settingNotSet() { |
| 125 | final Bundle settingsBundle = getPopulatedBundle(); |
| 126 | |
| 127 | assertFalse("Bundle should not contain " + TEST_SETTING_SECURE_INT, |
| 128 | settingsBundle.containsKey(TEST_SETTING_SECURE_INT)); |
| 129 | assertFalse("Bundle should not contain " + TEST_SETTING_GLOBAL_FLOAT, |
| 130 | settingsBundle.containsKey(TEST_SETTING_GLOBAL_FLOAT)); |
| 131 | assertFalse("Bundle should not contain " + TEST_SETTING_SYSTEM_STRING, |
| 132 | settingsBundle.containsKey(TEST_SETTING_SYSTEM_STRING)); |
| 133 | } |
| 134 | |
| 135 | private Bundle getPopulatedBundle() { |
| 136 | final Bundle settingsBundle = new Bundle(); |
| 137 | mCoreSettingsObserver.populateSettings(settingsBundle, |
| 138 | CoreSettingsObserver.sGlobalSettingToTypeMap); |
| 139 | mCoreSettingsObserver.populateSettings(settingsBundle, |
| 140 | CoreSettingsObserver.sSecureSettingToTypeMap); |
| 141 | mCoreSettingsObserver.populateSettings(settingsBundle, |
| 142 | CoreSettingsObserver.sSystemSettingToTypeMap); |
| 143 | return settingsBundle; |
| 144 | } |
| 145 | |
| 146 | private class TestInjector extends Injector { |
| 147 | @Override |
| 148 | public Context getContext() { |
| 149 | return mContext; |
| 150 | } |
| 151 | |
| 152 | public AppOpsService getAppOpsService(File file, Handler handler) { |
| 153 | return null; |
| 154 | } |
| 155 | |
| 156 | @Override |
| 157 | public Handler getUiHandler(ActivityManagerService service) { |
| 158 | return null; |
| 159 | } |
| 160 | } |
| 161 | } |