GlobalSettingsToPropertiesMapperTest.java revision 77bf72078e9eaa4bd0f9c7962e88febba9447287
1/*
2 * Copyright (C) 2018 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 android.content.ContentResolver;
20import android.provider.Settings;
21import android.support.test.InstrumentationRegistry;
22import android.support.test.filters.SmallTest;
23import android.support.test.runner.AndroidJUnit4;
24import android.test.mock.MockContentResolver;
25
26import com.android.internal.util.Preconditions;
27import com.android.internal.util.test.FakeSettingsProvider;
28
29import org.junit.Assert;
30import org.junit.Before;
31import org.junit.Test;
32import org.junit.runner.RunWith;
33
34import java.util.HashMap;
35import java.util.Map;
36
37/**
38 * Tests for {@link GlobalSettingsToPropertiesMapper}
39 */
40@RunWith(AndroidJUnit4.class)
41@SmallTest
42public class GlobalSettingsToPropertiesMapperTest {
43    private static final String[][] TEST_MAPPING = new String[][] {
44        {Settings.Global.SQLITE_COMPATIBILITY_WAL_FLAGS, "TestProperty"}
45    };
46
47    private TestMapper mTestMapper;
48    private MockContentResolver mMockContentResolver;
49
50    @Before
51    public void setup() {
52        // Use FakeSettingsProvider to not affect global state
53        mMockContentResolver = new MockContentResolver(InstrumentationRegistry.getContext());
54        mMockContentResolver.addProvider(Settings.AUTHORITY, new FakeSettingsProvider());
55        mTestMapper = new TestMapper(mMockContentResolver);
56    }
57
58    @Test
59    public void testUpdatePropertiesFromGlobalSettings() {
60        Settings.Global.putString(mMockContentResolver,
61                Settings.Global.SQLITE_COMPATIBILITY_WAL_FLAGS, "testValue");
62
63        mTestMapper.updatePropertiesFromGlobalSettings();
64        String propValue = mTestMapper.systemPropertiesGet("TestProperty");
65        Assert.assertEquals("testValue", propValue);
66
67        Settings.Global.putString(mMockContentResolver,
68                Settings.Global.SQLITE_COMPATIBILITY_WAL_FLAGS, "testValue2");
69        mTestMapper.updatePropertyFromSetting(Settings.Global.SQLITE_COMPATIBILITY_WAL_FLAGS,
70                "TestProperty");
71        propValue = mTestMapper.systemPropertiesGet("TestProperty");
72        Assert.assertEquals("testValue2", propValue);
73
74        Settings.Global.putString(mMockContentResolver,
75                Settings.Global.SQLITE_COMPATIBILITY_WAL_FLAGS, null);
76        mTestMapper.updatePropertyFromSetting(Settings.Global.SQLITE_COMPATIBILITY_WAL_FLAGS,
77                "TestProperty");
78        propValue = mTestMapper.systemPropertiesGet("TestProperty");
79        Assert.assertEquals("", propValue);
80    }
81
82    @Test
83    public void testUpdatePropertiesFromGlobalSettings_PropertyAndSettingNotPresent() {
84        // Test that empty property will not not be set if setting is not set
85        mTestMapper.updatePropertiesFromGlobalSettings();
86        String propValue = mTestMapper.systemPropertiesGet("TestProperty");
87        Assert.assertNull("Property should not be set if setting is null", propValue);
88    }
89
90    private static class TestMapper extends GlobalSettingsToPropertiesMapper {
91        private final Map<String, String> mProps = new HashMap<>();
92
93        TestMapper(ContentResolver contentResolver) {
94            super(contentResolver, TEST_MAPPING);
95        }
96
97        @Override
98        protected String systemPropertiesGet(String key) {
99            Preconditions.checkNotNull(key);
100            return mProps.get(key);
101        }
102
103        @Override
104        protected void systemPropertiesSet(String key, String value) {
105            Preconditions.checkNotNull(value);
106            Preconditions.checkNotNull(key);
107            mProps.put(key, value);
108        }
109    }
110
111}
112
113