Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit bb58879d authored by Amin Shaikh's avatar Amin Shaikh
Browse files

Change theme setting to JSONObject format.

Add JSON_OBJECT_VALIDATOR to Settings and add unit tests.

Fixes: 129543539
Test: atest coretests
Change-Id: I4fa7a1357d442c1fa04a7eee815d5c3a48cc5115
parent ed194a33
Loading
Loading
Loading
Loading
+5 −4
Original line number Diff line number Diff line
@@ -8654,9 +8654,10 @@ public final class Settings {
                "location_permissions_upgrade_to_q_mode";
        /**
         * Comma separated list of enabled overlay packages for all android.theme.customization.*
         * categories. If there is no corresponding package included for a category, then all
         * overlay packages in that category must be disabled.
         * Map of android.theme.customization.* categories to the enabled overlay package for that
         * category, formatted as a serialized {@link org.json.JSONObject}. If there is no
         * corresponding package included for a category, then all overlay packages in that
         * category must be disabled.
         * @hide
         */
        @SystemApi
@@ -8664,7 +8665,7 @@ public final class Settings {
                "theme_customization_overlay_packages";
        private static final Validator THEME_CUSTOMIZATION_OVERLAY_PACKAGES_VALIDATOR =
                new SettingsValidators.PackageNameListValidator(",");
                SettingsValidators.JSON_OBJECT_VALIDATOR;
        /**
         * Controls whether aware is enabled.
+17 −0
Original line number Diff line number Diff line
@@ -19,9 +19,13 @@ package android.provider;
import android.annotation.Nullable;
import android.content.ComponentName;
import android.net.Uri;
import android.text.TextUtils;

import com.android.internal.util.ArrayUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.Locale;

/**
@@ -162,6 +166,19 @@ public class SettingsValidators {
        }
    };

    /** {@link Validator} that checks whether a value is a valid {@link JSONObject}. */
    public static final Validator JSON_OBJECT_VALIDATOR = (value) -> {
        if (TextUtils.isEmpty(value)) {
            return false;
        }
        try {
            new JSONObject(value);
            return true;
        } catch (JSONException e) {
            return false;
        }
    };

    public interface Validator {
        /**
         * Returns whether the input value is valid. Subclasses should handle the case where the
+29 −0
Original line number Diff line number Diff line
@@ -16,6 +16,8 @@

package android.provider;

import static com.google.common.truth.Truth.assertThat;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
@@ -26,6 +28,8 @@ import android.provider.SettingsValidators.Validator;
import androidx.test.filters.SmallTest;
import androidx.test.runner.AndroidJUnit4;

import org.json.JSONException;
import org.json.JSONObject;
import org.junit.Test;
import org.junit.runner.RunWith;

@@ -217,6 +221,31 @@ public class SettingsValidatorsTest {
        assertFalse(Settings.System.DATE_FORMAT_VALIDATOR.validate(null));
    }

    @Test
    public void testJSONObjectValidator() throws JSONException {
        Validator v = SettingsValidators.JSON_OBJECT_VALIDATOR;

        assertThat(v.validate(new JSONObject().toString())).isTrue();
        assertThat(v.validate("{}")).isTrue();
        assertThat(v.validate(new JSONObject().put("foo", "bar").toString()))
                .isTrue();
        assertThat(v.validate("{\"foo\": \"bar\"}")).isTrue();

        assertThat(v.validate("random string")).isFalse();
        assertThat(v.validate("random: string")).isFalse();
        assertThat(v.validate("{random: }")).isFalse();
    }

    @Test
    public void testJSONObjectValidator_onNullValue_returnsFalse() {
        assertThat(SettingsValidators.JSON_OBJECT_VALIDATOR.validate(null)).isFalse();
    }

    @Test
    public void testJSONObjectValidator_onEmptyString_returnsFalse() {
        assertThat(SettingsValidators.JSON_OBJECT_VALIDATOR.validate("")).isFalse();
    }

    @Test
    public void ensureAllBackedUpSystemSettingsHaveValidators() {
        String offenders = getOffenders(concat(Settings.System.SETTINGS_TO_BACKUP,