blob: da30c11be81a3f36daf5aec05a722bcbc16547b4 [file] [log] [blame]
Sudheer Shankabce47222017-04-03 10:51:42 -07001/*
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
Sudheer Shankaaa3c30d2017-05-23 15:19:10 -070037import org.junit.AfterClass;
Sudheer Shankabce47222017-04-03 10:51:42 -070038import org.junit.Before;
39import org.junit.BeforeClass;
Sudheer Shankacb2b2ef2017-05-18 17:07:16 -070040import org.junit.Ignore;
Sudheer Shankabce47222017-04-03 10:51:42 -070041import org.junit.Test;
42import org.junit.runner.RunWith;
43import org.mockito.Mock;
44import org.mockito.MockitoAnnotations;
45
46import 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 Shankabce47222017-04-03 10:51:42 -070063@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() {
Sudheer Shankaaa3c30d2017-05-23 15:19:10 -070082 FakeSettingsProvider.clearSettingsProvider();
Sudheer Shankabce47222017-04-03 10:51:42 -070083 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 Shankaaa3c30d2017-05-23 15:19:10 -070088 @AfterClass
89 public static void tearDownOnce() {
90 FakeSettingsProvider.clearSettingsProvider();
91 }
92
Sudheer Shankabce47222017-04-03 10:51:42 -070093 @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}