blob: 4c4aeaf498553f0b0d92a45d740c3f047a676394 [file] [log] [blame]
Michal Karpinski2c37b082018-01-18 16:14:27 +00001/*
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 android.provider;
18
19import static org.junit.Assert.fail;
20
21import android.platform.test.annotations.Presubmit;
Michal Karpinski5db1e432018-01-18 20:10:24 +000022import android.provider.SettingsValidators.Validator;
Michal Karpinski2c37b082018-01-18 16:14:27 +000023import android.support.test.filters.SmallTest;
24import android.support.test.runner.AndroidJUnit4;
25
26import org.junit.Test;
27import org.junit.runner.RunWith;
28
Michal Karpinski5db1e432018-01-18 20:10:24 +000029import java.util.Map;
30
Michal Karpinski2c37b082018-01-18 16:14:27 +000031/** Tests that ensure all backed up settings have non-null validators. */
32@Presubmit
33@RunWith(AndroidJUnit4.class)
34@SmallTest
35public class SettingsValidatorsTest {
36
37 @Test
38 public void ensureAllBackedUpSystemSettingsHaveValidators() {
Michal Karpinski964943a2018-01-19 16:28:26 +000039 String offenders = getOffenders(concat(Settings.System.SETTINGS_TO_BACKUP,
40 Settings.System.LEGACY_RESTORE_SETTINGS), Settings.System.VALIDATORS);
Michal Karpinski5db1e432018-01-18 20:10:24 +000041
42 failIfOffendersPresent(offenders, "Settings.System");
43 }
44
45 @Test
46 public void ensureAllBackedUpGlobalSettingsHaveValidators() {
Michal Karpinski964943a2018-01-19 16:28:26 +000047 String offenders = getOffenders(concat(Settings.Global.SETTINGS_TO_BACKUP,
48 Settings.Global.LEGACY_RESTORE_SETTINGS), Settings.Global.VALIDATORS);
Michal Karpinski5db1e432018-01-18 20:10:24 +000049
50 failIfOffendersPresent(offenders, "Settings.Global");
51 }
52
Michal Karpinski964943a2018-01-19 16:28:26 +000053 @Test
54 public void ensureAllBackedUpSecureSettingsHaveValidators() {
55 String offenders = getOffenders(concat(Settings.Secure.SETTINGS_TO_BACKUP,
56 Settings.Secure.LEGACY_RESTORE_SETTINGS), Settings.Secure.VALIDATORS);
57
58 failIfOffendersPresent(offenders, "Settings.Secure");
59 }
60
Michal Karpinski5db1e432018-01-18 20:10:24 +000061 private void failIfOffendersPresent(String offenders, String settingsType) {
62 if (offenders.length() > 0) {
63 fail("All " + settingsType + " settings that are backed up have to have a non-null"
64 + " validator, but those don't: " + offenders);
65 }
66 }
67
68 private String getOffenders(String[] settingsToBackup, Map<String, Validator> validators) {
Michal Karpinski2c37b082018-01-18 16:14:27 +000069 StringBuilder offenders = new StringBuilder();
Michal Karpinski5db1e432018-01-18 20:10:24 +000070 for (String setting : settingsToBackup) {
71 if (validators.get(setting) == null) {
Michal Karpinski2c37b082018-01-18 16:14:27 +000072 offenders.append(setting).append(" ");
73 }
74 }
Michal Karpinski5db1e432018-01-18 20:10:24 +000075 return offenders.toString();
Michal Karpinski2c37b082018-01-18 16:14:27 +000076 }
Michal Karpinski964943a2018-01-19 16:28:26 +000077
78 private String[] concat(String[] first, String[] second) {
79 if (second == null || second.length == 0) {
80 return first;
81 }
82 final int firstLen = first.length;
83 final int secondLen = second.length;
84 String[] both = new String[firstLen + secondLen];
85 System.arraycopy(first, 0, both, 0, firstLen);
86 System.arraycopy(second, 0, both, firstLen, secondLen);
87 return both;
88 }
Michal Karpinski2c37b082018-01-18 16:14:27 +000089}