aboutsummaryrefslogtreecommitdiffstats
path: root/packages/CMSettingsProvider/tests/src/org/cyanogenmod/cmsettings/tests/CMSettingsProviderDefaultsTest.java
blob: d2ed19342a447d82c7b5f59755ad47ce440f168f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/**
 * Copyright (c) 2016, The CyanogenMod Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.cyanogenmod.cmsettings.tests;

import android.content.ContentResolver;
import android.content.Context;
import android.content.pm.PackageManager;
import android.content.res.Resources;
import android.os.UserHandle;
import android.test.AndroidTestCase;
import android.test.suitebuilder.annotation.SmallTest;

import java.util.ArrayList;

import android.text.TextUtils;
import android.util.Log;
import android.util.TypedValue;
import cyanogenmod.providers.CMSettings;
import org.cyanogenmod.cmsettings.CMDatabaseHelper;
import org.cyanogenmod.cmsettings.CMSettingsProvider;
import org.cyanogenmod.cmsettings.R;

/**
 * Created by adnan on 1/25/16.
 */
public class CMSettingsProviderDefaultsTest extends AndroidTestCase {
    private ContentResolver mContentResolver;
    private boolean mHasMigratedSettings;
    private Resources mRemoteResources;

    // These data structures are set up in a way that is easier for manual input of new defaults
    private static ArrayList<Setting> SYSTEM_SETTINGS_DEFAULTS = new ArrayList<Setting>();
    private static ArrayList<Setting> SECURE_SETTINGS_DEFAULTS = new ArrayList<Setting>();
    private static ArrayList<Setting> GLOBAL_SETTINGS_DEFAULTS = new ArrayList<Setting>();

    //SYSTEM
    static {
        SYSTEM_SETTINGS_DEFAULTS.add(new Setting(
                CMSettings.System.STATUS_BAR_QUICK_QS_PULLDOWN,
                "R.integer.def_qs_quick_pulldown"));
        SYSTEM_SETTINGS_DEFAULTS.add(new Setting(
                CMSettings.System.NOTIFICATION_LIGHT_BRIGHTNESS_LEVEL,
                "R.integer.def_notification_brightness_level"));
        SYSTEM_SETTINGS_DEFAULTS.add(new Setting(
                CMSettings.System.ENABLE_FORWARD_LOOKUP,
                "R.integer.def_forward_lookup"));
        SYSTEM_SETTINGS_DEFAULTS.add(new Setting(
                CMSettings.System.ENABLE_PEOPLE_LOOKUP,
                "R.integer.def_people_lookup"));
        SYSTEM_SETTINGS_DEFAULTS.add(new Setting(
                CMSettings.System.ENABLE_REVERSE_LOOKUP,
                "R.integer.def_reverse_lookup"));
        SYSTEM_SETTINGS_DEFAULTS.add(new Setting(
                CMSettings.System.NOTIFICATION_LIGHT_MULTIPLE_LEDS_ENABLE,
                "R.bool.def_notification_multiple_leds"));
        SYSTEM_SETTINGS_DEFAULTS.add(new Setting(
                CMSettings.System.SYSTEM_PROFILES_ENABLED,
                "R.bool.def_profiles_enabled"));
        SYSTEM_SETTINGS_DEFAULTS.add(new Setting(
                CMSettings.System.NOTIFICATION_LIGHT_PULSE_CUSTOM_ENABLE,
                "R.bool.def_notification_pulse_custom_enable"));
        SYSTEM_SETTINGS_DEFAULTS.add(new Setting(
                CMSettings.System.SWAP_VOLUME_KEYS_ON_ROTATION,
                "R.bool.def_swap_volume_keys_on_rotation"));
        SYSTEM_SETTINGS_DEFAULTS.add(new Setting(
                CMSettings.System.NOTIFICATION_LIGHT_PULSE_CUSTOM_VALUES,
                "R.string.def_notification_pulse_custom_value"));
        SYSTEM_SETTINGS_DEFAULTS.add(new Setting(
                CMSettings.System.STATUS_BAR_BATTERY_STYLE,
                "R.integer.def_battery_style"));
    }

    //SECURE
    static {
        SECURE_SETTINGS_DEFAULTS.add(new Setting(
                CMSettings.Secure.ADVANCED_MODE,
                "R.bool.def_advanced_mode"));
        SECURE_SETTINGS_DEFAULTS.add(new Setting(
                CMSettings.Secure.QS_USE_MAIN_TILES,
                "R.bool.def_sysui_qs_main_tiles"));
        SECURE_SETTINGS_DEFAULTS.add(new Setting(
                CMSettings.Secure.LOCKSCREEN_VISUALIZER_ENABLED,
                "R.bool.def_lockscreen_visualizer"));
        SECURE_SETTINGS_DEFAULTS.add(new Setting(
                CMSettings.Secure.DEFAULT_THEME_COMPONENTS,
                "R.string.def_theme_components"));
        SECURE_SETTINGS_DEFAULTS.add(new Setting(
                CMSettings.Secure.DEFAULT_THEME_PACKAGE,
                "R.string.def_theme_package"));
        SECURE_SETTINGS_DEFAULTS.add(new Setting(
                CMSettings.Secure.PROTECTED_COMPONENT_MANAGERS,
                "R.string.def_protected_component_managers"));
    }

    //GLOBAL
    static {
        GLOBAL_SETTINGS_DEFAULTS.add(new Setting(
                CMSettings.Global.POWER_NOTIFICATIONS_ENABLED,
                "R.bool.def_power_notifications_enabled"));
        GLOBAL_SETTINGS_DEFAULTS.add(new Setting(
                CMSettings.Global.POWER_NOTIFICATIONS_VIBRATE,
                "R.bool.def_power_notifications_vibrate"));
        GLOBAL_SETTINGS_DEFAULTS.add(new Setting(
                CMSettings.Global.POWER_NOTIFICATIONS_RINGTONE,
                "R.string.def_power_notifications_ringtone"));
        GLOBAL_SETTINGS_DEFAULTS.add(new Setting(
                CMSettings.Global.WEATHER_TEMPERATURE_UNIT,
                "R.integer.def_temperature_unit"));
        GLOBAL_SETTINGS_DEFAULTS.add(new Setting(
                CMSettings.Global.DEV_FORCE_SHOW_NAVBAR,
                "R.integer.def_force_show_navbar"));
    }

    @Override
    protected void setUp() throws Exception {
        super.setUp();
        mContentResolver = getContext().getContentResolver();
        mHasMigratedSettings = getContext().getSharedPreferences(CMSettingsProvider.TAG,
                Context.MODE_PRIVATE).getBoolean(CMSettingsProvider.PREF_HAS_MIGRATED_CM_SETTINGS,
                false);
        mRemoteResources = getRemoteResources("org.cyanogenmod.cmsettings");
    }

    @SmallTest
    public void testVerifySystemSettingsDefault() {
        if (verifyNotMigratedSettings()) {
            for (Setting setting : SYSTEM_SETTINGS_DEFAULTS) {
                verifyDefaultSettingForTable(setting, CMDatabaseHelper.CMTableNames.TABLE_SYSTEM);
            }
        }
    }

    @SmallTest
    public void testVerifySecureSettingsDefaults() {
        if (verifyNotMigratedSettings()) {
            for (Setting setting : SECURE_SETTINGS_DEFAULTS) {
                verifyDefaultSettingForTable(setting, CMDatabaseHelper.CMTableNames.TABLE_SECURE);
            }
        }
    }

    @SmallTest
    public void testVerifyGlobalSettingsDefaults() {
        if (verifyNotMigratedSettings()) {
            for (Setting setting : GLOBAL_SETTINGS_DEFAULTS) {
                verifyDefaultSettingForTable(setting, CMDatabaseHelper.CMTableNames.TABLE_GLOBAL);
            }
        }
    }

    private boolean verifyNotMigratedSettings() {
        return !mHasMigratedSettings;
    }

    private void verifyDefaultSettingForTable(Setting setting, String table) {
        TypedValue value = new TypedValue();
        try {
            int identifier = mRemoteResources.getIdentifier(
                    setting.mDefResName, setting.mType, "org.cyanogenmod.cmsettings");
            mRemoteResources.getValue(identifier, value, true);
        } catch (Resources.NotFoundException e) {
            // Resource not found, can't verify because it probably wasn't loaded in
            throw new AssertionError("Unable to find resource for " + setting.mKey);
        }

        try {
            switch (value.type) {
                case TypedValue.TYPE_INT_DEC:
                    int actualValue = getIntForTable(setting, table);
                    try {
                        assertEquals(value.data, actualValue);
                    } catch (AssertionError e) {
                        throw new AssertionError("Compared value of " + setting.mKey + " expected "
                                + value.data + " got " + actualValue);
                    }
                    break;
                case TypedValue.TYPE_INT_BOOLEAN:
                    int actualBooleanValue = getIntForTable(setting, table);
                    try {
                        //This is gross
                        //Boolean can be "true" as long as it isn't 0
                        if (value.data != 0) {
                            value.data = 1;
                        }
                        assertEquals(value.data, actualBooleanValue);
                    } catch (AssertionError e) {
                        throw new AssertionError("Compared value of " + setting.mKey + " expected "
                                + value.data + " got " + actualBooleanValue);
                    }
                    break;
                case TypedValue.TYPE_STRING:
                    if (!TextUtils.isEmpty(value.string)) {
                        //This should really be done as a parameterized test
                        String actualStringValue = getStringForTable(setting, table);
                        try {
                            assertEquals(value.string, actualStringValue);
                        } catch (AssertionError e) {
                            throw new AssertionError("Compared value of " + setting.mKey
                                    + " expected " + value.string + " got " + actualStringValue);
                        }
                    }
                    break;
                case TypedValue.TYPE_NULL:
                    break;
            }
        } catch (CMSettings.CMSettingNotFoundException e) {
            e.printStackTrace();
            throw new AssertionError("Setting " + setting.mKey + " not found!");
        }
    }

    private int getIntForTable(Setting setting, String table)
            throws CMSettings.CMSettingNotFoundException {
        switch (table) {
            case CMDatabaseHelper.CMTableNames.TABLE_SYSTEM:
                return CMSettings.System.getIntForUser(mContentResolver, setting.mKey,
                        UserHandle.USER_OWNER);
            case CMDatabaseHelper.CMTableNames.TABLE_SECURE:
                return CMSettings.Secure.getIntForUser(mContentResolver, setting.mKey,
                        UserHandle.USER_OWNER);
            case CMDatabaseHelper.CMTableNames.TABLE_GLOBAL:
                return CMSettings.Global.getIntForUser(mContentResolver, setting.mKey,
                        UserHandle.USER_OWNER);
            default:
                throw new AssertionError("Invalid or empty table!");
        }
    }

    private String getStringForTable(Setting setting, String table)
            throws CMSettings.CMSettingNotFoundException {
        switch (table) {
            case CMDatabaseHelper.CMTableNames.TABLE_SYSTEM:
                return CMSettings.System.getStringForUser(mContentResolver, setting.mKey,
                        UserHandle.USER_OWNER);
            case CMDatabaseHelper.CMTableNames.TABLE_SECURE:
                return CMSettings.Secure.getStringForUser(mContentResolver, setting.mKey,
                        UserHandle.USER_OWNER);
            case CMDatabaseHelper.CMTableNames.TABLE_GLOBAL:
                return CMSettings.Global.getStringForUser(mContentResolver, setting.mKey,
                        UserHandle.USER_OWNER);
            default:
                throw new AssertionError("Invalid or empty table!");
        }
    }

    private static class Setting {
        public String mKey;
        public String mDefResName;
        public String mType;

        public Setting(String key, String defResId) {
            mKey = key;
            String[] parts = defResId.split("\\.");
            mType = parts[1];
            mDefResName = parts[2];
        }
    }

    private Resources getRemoteResources(String packageName)
            throws PackageManager.NameNotFoundException {
        PackageManager packageManager = mContext.getPackageManager();
        return packageManager.getResourcesForApplication(packageName);
    }
}