diff options
author | Elliot Sisteron <elliotsisteron@google.com> | 2023-06-09 12:28:54 +0000 |
---|---|---|
committer | Elliot Sisteron <elliotsisteron@google.com> | 2023-06-13 14:28:36 +0000 |
commit | e647f2e32eed00d27ffee869b3cddb1c5ebfe6c5 (patch) | |
tree | 4fe84a11c6108e58222d6569d074f7ada462f86a /SafetyCenter | |
parent | 76e26acf61b2df5797062e30abf093809e00afef (diff) | |
download | Permission-e647f2e32eed00d27ffee869b3cddb1c5ebfe6c5.tar.gz |
Rename SafetyCenterResourcesContext to SafetyCenterResourcesApk.
It's not really a Context anymore.
Bug: 283100177
Test: atest CtsSafetyCenterTestCases
Relnote: N/A
Change-Id: Iab9d28440f10c02a5e1a90fd0fbb95c53ab6b383
Diffstat (limited to 'SafetyCenter')
-rw-r--r-- | SafetyCenter/ResourcesLib/java/com/android/safetycenter/resources/SafetyCenterResourcesApk.java (renamed from SafetyCenter/ResourcesLib/java/com/android/safetycenter/resources/SafetyCenterResourcesContext.java) | 38 | ||||
-rw-r--r-- | SafetyCenter/ResourcesLib/tests/java/com/android/safetycenter/resources/SafetyCenterResourcesApkTest.kt | 440 | ||||
-rw-r--r-- | SafetyCenter/ResourcesLib/tests/java/com/android/safetycenter/resources/SafetyCenterResourcesContextTest.kt | 442 |
3 files changed, 459 insertions, 461 deletions
diff --git a/SafetyCenter/ResourcesLib/java/com/android/safetycenter/resources/SafetyCenterResourcesContext.java b/SafetyCenter/ResourcesLib/java/com/android/safetycenter/resources/SafetyCenterResourcesApk.java index 25455dc7a..b0063d051 100644 --- a/SafetyCenter/ResourcesLib/java/com/android/safetycenter/resources/SafetyCenterResourcesContext.java +++ b/SafetyCenter/ResourcesLib/java/com/android/safetycenter/resources/SafetyCenterResourcesApk.java @@ -46,9 +46,9 @@ import java.util.List; * <p>This class isn't thread safe. Thread safety must be handled by the caller, or this may cause * the resources APK {@link Context} to be initialized multiple times. */ -public final class SafetyCenterResourcesContext { +public final class SafetyCenterResourcesApk { - private static final String TAG = "SafetyCenterResContext"; + private static final String TAG = "SafetyCenterResApk"; /** Intent action that is used to identify the Safety Center resources APK */ private static final String RESOURCES_APK_ACTION = @@ -86,11 +86,11 @@ public final class SafetyCenterResourcesContext { // Cached context from the resources APK. @Nullable private Context mResourcesApkContext; - public SafetyCenterResourcesContext(Context context) { + public SafetyCenterResourcesApk(Context context) { this(context, /* shouldFallbackIfNamedResourceNotFound */ true); } - private SafetyCenterResourcesContext( + private SafetyCenterResourcesApk( Context context, boolean shouldFallbackIfNamedResourceNotFound) { this( context, @@ -101,7 +101,7 @@ public final class SafetyCenterResourcesContext { } @VisibleForTesting - SafetyCenterResourcesContext( + SafetyCenterResourcesApk( Context context, String resourcesApkAction, String resourcesApkPath, @@ -114,11 +114,11 @@ public final class SafetyCenterResourcesContext { mShouldFallbackIfNamedResourceNotFound = shouldFallbackIfNamedResourceNotFound; } - /** Creates a new {@link SafetyCenterResourcesContext} for testing. */ + /** Creates a new {@link SafetyCenterResourcesApk} for testing. */ @VisibleForTesting - public static SafetyCenterResourcesContext forTests(Context contextBase) { - return new SafetyCenterResourcesContext( - contextBase, /* shouldFallbackIfNamedResourceNotFound */ false); + public static SafetyCenterResourcesApk forTests(Context context) { + return new SafetyCenterResourcesApk( + context, /* shouldFallbackIfNamedResourceNotFound */ false); } /** @@ -140,7 +140,7 @@ public final class SafetyCenterResourcesContext { * * <p>Throws an {@link IllegalStateException} if the resources APK is not available */ - public Context getResourcesApkContext() { + public Context getContext() { if (mResourcesApkContext != null) { return mResourcesApkContext; } @@ -213,7 +213,7 @@ public final class SafetyCenterResourcesContext { /** Calls {@link Context#getResources()} for the resources APK {@link Context}. */ public Resources getResources() { - return getResourcesApkContext().getResources(); + return getContext().getResources(); } /** @@ -228,21 +228,21 @@ public final class SafetyCenterResourcesContext { @VisibleForTesting @Nullable InputStream getSafetyCenterConfig(String configName) { - int id = getResIdAndMaybeThrowIfNull(configName, "raw"); - if (id == Resources.ID_NULL) { + int resId = getResIdAndMaybeThrowIfNull(configName, "raw"); + if (resId == Resources.ID_NULL) { return null; } - return getResources().openRawResource(id); + return getResources().openRawResource(resId); } /** Calls {@link Context#getString(int)} for the resources APK {@link Context}. */ public String getString(@StringRes int stringId) { - return getResourcesApkContext().getString(stringId); + return getContext().getString(stringId); } /** Same as {@link #getString(int)} but with the given {@code formatArgs}. */ public String getString(@StringRes int stringId, Object... formatArgs) { - return getResourcesApkContext().getString(stringId, formatArgs); + return getContext().getString(stringId, formatArgs); } /** @@ -317,7 +317,7 @@ public final class SafetyCenterResourcesContext { if (resId == Resources.ID_NULL) { return null; } - return Icon.createWithResource(getResourcesApkContext().getPackageName(), resId); + return Icon.createWithResource(getContext().getPackageName(), resId); } /** @@ -333,7 +333,7 @@ public final class SafetyCenterResourcesContext { if (resId == Resources.ID_NULL) { return null; } - return getResources().getColor(resId, getResourcesApkContext().getTheme()); + return getResources().getColor(resId, getContext().getTheme()); } private int getResIdAndMaybeThrowIfNull(String name, String type) { @@ -351,6 +351,6 @@ public final class SafetyCenterResourcesContext { private int getResId(String name, String type) { // TODO(b/227738283): profile the performance of this operation and consider adding caching // or finding some alternative solution. - return getResources().getIdentifier(name, type, getResourcesApkContext().getPackageName()); + return getResources().getIdentifier(name, type, getContext().getPackageName()); } } diff --git a/SafetyCenter/ResourcesLib/tests/java/com/android/safetycenter/resources/SafetyCenterResourcesApkTest.kt b/SafetyCenter/ResourcesLib/tests/java/com/android/safetycenter/resources/SafetyCenterResourcesApkTest.kt new file mode 100644 index 000000000..b52595b13 --- /dev/null +++ b/SafetyCenter/ResourcesLib/tests/java/com/android/safetycenter/resources/SafetyCenterResourcesApkTest.kt @@ -0,0 +1,440 @@ +/* + * Copyright (C) 2022 The Android Open Source 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 com.android.safetycenter.resources + +import android.content.Context +import android.content.pm.PackageManager +import android.content.res.Resources +import androidx.test.core.app.ApplicationProvider.getApplicationContext +import androidx.test.ext.junit.runners.AndroidJUnit4 +import com.google.common.truth.Truth.assertThat +import java.lang.IllegalStateException +import kotlin.test.assertFailsWith +import org.junit.Test +import org.junit.runner.RunWith + +@RunWith(AndroidJUnit4::class) +class SafetyCenterResourcesApkTest { + private val context: Context = getApplicationContext() + + @Test + fun init_withValidInputs_returnsTrue() { + val resourcesApk = newSafetyCenterResourcesApk() + + val initialized = resourcesApk.init() + + assertThat(initialized).isTrue() + } + + @Test + fun init_withWrongAction_returnsFalse() { + val resourcesApk = newSafetyCenterResourcesApk(resourcesApkAction = "wrong") + + val initialized = resourcesApk.init() + + assertThat(initialized).isFalse() + } + + @Test + fun init_withWrongPath_returnsFalse() { + val resourcesApk = + newSafetyCenterResourcesApk(resourcesApkPath = "/apex/com.android.permission") + + val initialized = resourcesApk.init() + + assertThat(initialized).isFalse() + } + + @Test + fun init_withWrongFlags_returnsFalse() { + val resourcesApk = newSafetyCenterResourcesApk(flags = PackageManager.MATCH_SYSTEM_ONLY) + + val initialized = resourcesApk.init() + + assertThat(initialized).isFalse() + } + + @Test + fun getContext_withValidInputs_returnsResourcesApkContext() { + val resourcesApk = newSafetyCenterResourcesApk() + + val resourcesApkContext = resourcesApk.context + + assertThat(resourcesApkContext.packageName).isEqualTo(RESOURCES_APK_PKG_NAME) + } + + @Test + fun getContext_withWrongAction_throws() { + val resourcesApk = newSafetyCenterResourcesApk(resourcesApkAction = "wrong") + + assertFailsWith(IllegalStateException::class) { resourcesApk.context } + } + + @Test + fun getContext_withWrongPath_throws() { + val resourcesApk = + newSafetyCenterResourcesApk(resourcesApkPath = "/apex/com.android.permission") + + assertFailsWith(IllegalStateException::class) { resourcesApk.context } + } + + @Test + fun getContext_withWrongFlags_throws() { + val resourcesApk = newSafetyCenterResourcesApk(flags = PackageManager.MATCH_SYSTEM_ONLY) + + assertFailsWith(IllegalStateException::class) { resourcesApk.context } + } + + @Test + fun getResources_withValidInputs_returnsResourcesApkContextResources() { + val resourcesApk = newSafetyCenterResourcesApk() + + val resources = resourcesApk.resources + + assertThat(resources).isEqualTo(resourcesApk.context.resources) + } + + @Test + fun getResources_nullContext_throwsRegardlessOfFallback() { + val resourcesApk = + newSafetyCenterResourcesApk(resourcesApkAction = "wrong", fallback = true) + + assertFailsWith(IllegalStateException::class) { resourcesApk.resources } + } + + @Test + fun getSafetyCenterConfig_withValidInputs_returnsConfigContent() { + val resourcesApk = newSafetyCenterResourcesApk() + + val config = resourcesApk.safetyCenterConfig + val configContent = config?.bufferedReader().use { it?.readText() } + + assertThat(config).isNotNull() + assertThat(configContent).isEqualTo(CONFIG_CONTENT) + } + + @Test + fun getSafetyCenterConfig_anotherValidConfigName_returnsConfigContent() { + val resourcesApk = newSafetyCenterResourcesApk() + + val config = resourcesApk.getSafetyCenterConfig(CONFIG_NAME) + val configContent = config?.bufferedReader().use { it?.readText() } + + assertThat(config).isNotNull() + assertThat(configContent).isEqualTo(CONFIG_CONTENT) + } + + @Test + fun getSafetyCenterConfig_invalidConfigNameWithFallback_returnsNull() { + val resourcesApk = newSafetyCenterResourcesApk(fallback = true) + + assertThat(resourcesApk.getSafetyCenterConfig("wrong")).isNull() + } + + @Test + fun getSafetyCenterConfig_invalidConfigNameWithoutFallback_throws() { + val resourcesApk = newSafetyCenterResourcesApk(fallback = false) + + assertFailsWith(Resources.NotFoundException::class) { + resourcesApk.getSafetyCenterConfig("wrong") + } + } + + @Test + fun getSafetyCenterConfig_nullContext_throwsRegardlessOfFallback() { + val resourcesApk = + newSafetyCenterResourcesApk(resourcesApkAction = "wrong", fallback = true) + + assertFailsWith(IllegalStateException::class) { resourcesApk.safetyCenterConfig } + } + + @Test + fun getString_validString_returnsString() { + val resourcesApk = newSafetyCenterResourcesApk() + + val ok = resourcesApk.getString(android.R.string.ok) + + assertThat(ok).isEqualTo("OK") + } + + @Test + fun getString_nullContext_throwsRegardlessOfFallback() { + val resourcesApk = + newSafetyCenterResourcesApk(resourcesApkAction = "wrong", fallback = true) + + assertFailsWith(IllegalStateException::class) { + resourcesApk.getString(android.R.string.ok) + } + } + + @Test + fun getStringWithFormatArgs_validString_returnsString() { + val resourcesApk = newSafetyCenterResourcesApk() + + val ok = resourcesApk.getString(android.R.string.ok, "") + + assertThat(ok).isEqualTo("OK") + } + + @Test + fun getStringWithFormatArgs_nullContext_throwsRegardlessOfFallback() { + val resourcesApk = + newSafetyCenterResourcesApk(resourcesApkAction = "wrong", fallback = true) + + assertFailsWith(IllegalStateException::class) { + resourcesApk.getString(android.R.string.ok, "") + } + } + + @Test + fun getStringByName_validString_returnsString() { + val resourcesApk = newSafetyCenterResourcesApk() + + assertThat(resourcesApk.getStringByName("valid_string")).isEqualTo("I exist!") + } + + @Test + fun getStringByName_invalidStringWithFallback_returnsEmptyString() { + val resourcesApk = newSafetyCenterResourcesApk(fallback = true) + + assertThat(resourcesApk.getStringByName("invalid_string")).isEqualTo("") + } + + @Test + fun getStringByName_invalidStringWithoutFallback_throws() { + val resourcesApk = newSafetyCenterResourcesApk(fallback = false) + + assertFailsWith(Resources.NotFoundException::class) { + resourcesApk.getStringByName("invalid_string") + } + } + + @Test + fun getStringByName_nullContext_throwsRegardlessOfFallback() { + val resourcesApk = + newSafetyCenterResourcesApk(resourcesApkAction = "wrong", fallback = true) + + assertFailsWith(IllegalStateException::class) { + resourcesApk.getStringByName("valid_string") + } + } + + @Test + fun getStringByNameWithFormatArgs_validString_returnsString() { + val resourcesApk = newSafetyCenterResourcesApk() + + assertThat(resourcesApk.getStringByName("valid_string", "")).isEqualTo("I exist!") + } + + @Test + fun getStringByNameWithFormatArgs_invalidStringWithFallback_returnsEmptyString() { + val resourcesApk = newSafetyCenterResourcesApk(fallback = true) + + assertThat(resourcesApk.getStringByName("invalid_string", "")).isEqualTo("") + } + + @Test + fun getStringByNameWithFormatArgs_invalidStringWithoutFallback_throws() { + val resourcesApk = newSafetyCenterResourcesApk(fallback = false) + + assertFailsWith(Resources.NotFoundException::class) { + resourcesApk.getStringByName("invalid_string", "") + } + } + + @Test + fun getStringByNameWithFormatArgs_nullContext_throwsRegardlessOfFallback() { + val resourcesApk = + newSafetyCenterResourcesApk(resourcesApkAction = "wrong", fallback = true) + + assertFailsWith(IllegalStateException::class) { + resourcesApk.getStringByName("valid_string", "") + } + } + + @Test + fun getOptionalString_validString_returnsString() { + val resourcesApk = newSafetyCenterResourcesApk() + + val ok = resourcesApk.getOptionalString(android.R.string.ok) + + assertThat(ok).isEqualTo("OK") + } + + @Test + fun getOptionalString_resourceIdNull_returnsNull() { + val resourcesApk = newSafetyCenterResourcesApk() + + val string = resourcesApk.getOptionalString(Resources.ID_NULL) + + assertThat(string).isNull() + } + + @Test + fun getOptionalString_nullContext_throwsRegardlessOfFallback() { + val resourcesApk = + newSafetyCenterResourcesApk(resourcesApkAction = "wrong", fallback = true) + + assertFailsWith(IllegalStateException::class) { + resourcesApk.getOptionalString(android.R.string.ok) + } + } + + @Test + fun getOptionalStringByName_validString_returnsString() { + val resourcesApk = newSafetyCenterResourcesApk() + + assertThat(resourcesApk.getOptionalStringByName("valid_string")).isEqualTo("I exist!") + } + + @Test + fun getOptionalStringByName_invalidStringWithFallback_returnsNull() { + val resourcesApk = newSafetyCenterResourcesApk(fallback = true) + + assertThat(resourcesApk.getOptionalStringByName("invalid_string")).isNull() + } + + @Test + fun getOptionalStringByName_invalidStringWithoutFallback_returnsNull() { + val resourcesApk = newSafetyCenterResourcesApk(fallback = false) + + assertThat(resourcesApk.getOptionalStringByName("invalid_string")).isNull() + } + + @Test + fun getOptionalStringByName_nullContext_throwsRegardlessOfFallback() { + val resourcesApk = + newSafetyCenterResourcesApk(resourcesApkAction = "wrong", fallback = true) + + assertFailsWith(IllegalStateException::class) { + resourcesApk.getOptionalStringByName("valid_string") + } + } + + @Test + fun getDrawableByName_validDrawable_returnsDrawable() { + val resourcesApk = newSafetyCenterResourcesApk() + + assertThat(resourcesApk.getDrawableByName("valid_drawable", context.theme)).isNotNull() + } + + @Test + fun getDrawableByName_invalidDrawableWithFallback_returnsNull() { + val resourcesApk = newSafetyCenterResourcesApk(fallback = true) + + assertThat(resourcesApk.getDrawableByName("invalid_drawable", context.theme)).isNull() + } + + @Test + fun getDrawableByName_invalidDrawableWithoutFallback_throws() { + val resourcesApk = newSafetyCenterResourcesApk(fallback = false) + + assertFailsWith(Resources.NotFoundException::class) { + resourcesApk.getDrawableByName("invalid_drawable", context.theme) + } + } + + @Test + fun getDrawableByName_nullContext_throwsRegardlessOfFallback() { + val resourcesApk = + newSafetyCenterResourcesApk(resourcesApkAction = "wrong", fallback = true) + + assertFailsWith(IllegalStateException::class) { + resourcesApk.getDrawableByName("valid_drawable", context.theme) + } + } + + @Test + fun getIconByDrawableName_validDrawable_returnsIcon() { + val resourcesApk = newSafetyCenterResourcesApk() + + assertThat(resourcesApk.getIconByDrawableName("valid_drawable")).isNotNull() + } + + @Test + fun getIconByDrawableName_invalidDrawableWithFallback_returnsNull() { + val resourcesApk = newSafetyCenterResourcesApk(fallback = true) + + assertThat(resourcesApk.getIconByDrawableName("invalid_drawable")).isNull() + } + + @Test + fun getIconByDrawableName_invalidDrawableWithoutFallback_throws() { + val resourcesApk = newSafetyCenterResourcesApk(fallback = false) + + assertFailsWith(Resources.NotFoundException::class) { + resourcesApk.getIconByDrawableName("invalid_drawable") + } + } + + @Test + fun getIconByDrawableByName_nullContext_throwsRegardlessOfFallback() { + val resourcesApk = + newSafetyCenterResourcesApk(resourcesApkAction = "wrong", fallback = true) + + assertFailsWith(IllegalStateException::class) { + resourcesApk.getIconByDrawableName("valid_drawable") + } + } + + @Test + fun getColorByName_validColor_returnsColor() { + val resourcesApk = newSafetyCenterResourcesApk() + + assertThat(resourcesApk.getColorByName("valid_color")).isNotNull() + } + + @Test + fun getColorByName_invalidColorWithFallback_returnsNull() { + val resourcesApk = newSafetyCenterResourcesApk(fallback = true) + + assertThat(resourcesApk.getColorByName("invalid_color")).isNull() + } + + @Test + fun getColorByName_invalidColorWithoutFallback_throws() { + val resourcesApk = newSafetyCenterResourcesApk(fallback = false) + + assertFailsWith(Resources.NotFoundException::class) { + resourcesApk.getColorByName("invalid_color") + } + } + + @Test + fun getColorByName_nullContext_throwsRegardlessOfFallback() { + val resourcesApk = + newSafetyCenterResourcesApk(resourcesApkAction = "wrong", fallback = true) + + assertFailsWith(IllegalStateException::class) { resourcesApk.getColorByName("valid_color") } + } + + private fun newSafetyCenterResourcesApk( + resourcesApkAction: String = RESOURCES_APK_ACTION, + resourcesApkPath: String = "", + flags: Int = 0, + fallback: Boolean = false + ) = SafetyCenterResourcesApk(context, resourcesApkAction, resourcesApkPath, flags, fallback) + + companion object { + const val RESOURCES_APK_ACTION = + "com.android.safetycenter.tests.intent.action.SAFETY_CENTER_TEST_RESOURCES_APK" + const val RESOURCES_APK_PKG_NAME = + "com.android.safetycenter.tests.config.safetycenterresourceslibtestresources" + const val CONFIG_NAME = "test" + const val CONFIG_CONTENT = "TEST" + } +} diff --git a/SafetyCenter/ResourcesLib/tests/java/com/android/safetycenter/resources/SafetyCenterResourcesContextTest.kt b/SafetyCenter/ResourcesLib/tests/java/com/android/safetycenter/resources/SafetyCenterResourcesContextTest.kt deleted file mode 100644 index 33ff7543e..000000000 --- a/SafetyCenter/ResourcesLib/tests/java/com/android/safetycenter/resources/SafetyCenterResourcesContextTest.kt +++ /dev/null @@ -1,442 +0,0 @@ -/* - * Copyright (C) 2022 The Android Open Source 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 com.android.safetycenter.resources - -import android.content.Context -import android.content.pm.PackageManager -import android.content.res.Resources -import androidx.test.core.app.ApplicationProvider.getApplicationContext -import androidx.test.ext.junit.runners.AndroidJUnit4 -import com.google.common.truth.Truth.assertThat -import java.lang.IllegalStateException -import kotlin.test.assertFailsWith -import org.junit.Test -import org.junit.runner.RunWith - -@RunWith(AndroidJUnit4::class) -class SafetyCenterResourcesContextTest { - private val context: Context = getApplicationContext() - - @Test - fun init_withValidInputs_returnsTrue() { - val resourcesContext = createNewResourcesContext() - - val initialized = resourcesContext.init() - - assertThat(initialized).isTrue() - } - - @Test - fun init_withWrongAction_returnsFalse() { - val resourcesContext = createNewResourcesContext(resourcesApkAction = "wrong") - - val initialized = resourcesContext.init() - - assertThat(initialized).isFalse() - } - - @Test - fun init_withWrongPath_returnsFalse() { - val resourcesContext = - createNewResourcesContext(resourcesApkPath = "/apex/com.android.permission") - - val initialized = resourcesContext.init() - - assertThat(initialized).isFalse() - } - - @Test - fun init_withWrongFlags_returnsFalse() { - val resourcesContext = createNewResourcesContext(flags = PackageManager.MATCH_SYSTEM_ONLY) - - val initialized = resourcesContext.init() - - assertThat(initialized).isFalse() - } - - @Test - fun getResourcesApkContext_withValidInputs_returnsResourcesApkContext() { - val resourcesContext = createNewResourcesContext() - - val resourcesApkContext = resourcesContext.resourcesApkContext - - assertThat(resourcesApkContext.packageName).isEqualTo(RESOURCES_APK_PKG_NAME) - } - - @Test - fun getResourcesApkContext_withWrongAction_throws() { - val resourcesContext = createNewResourcesContext(resourcesApkAction = "wrong") - - assertFailsWith(IllegalStateException::class) { resourcesContext.resourcesApkContext } - } - - @Test - fun getResourcesApkContext_withWrongPath_throws() { - val resourcesContext = - createNewResourcesContext(resourcesApkPath = "/apex/com.android.permission") - - assertFailsWith(IllegalStateException::class) { resourcesContext.resourcesApkContext } - } - - @Test - fun getResourcesApkContext_withWrongFlags_throws() { - val resourcesContext = createNewResourcesContext(flags = PackageManager.MATCH_SYSTEM_ONLY) - - assertFailsWith(IllegalStateException::class) { resourcesContext.resourcesApkContext } - } - - @Test - fun getResources_withValidInputs_returnsResourcesApkContextResources() { - val resourcesContext = createNewResourcesContext() - - val resources = resourcesContext.resources - - assertThat(resources).isEqualTo(resourcesContext.resourcesApkContext.resources) - } - - @Test - fun getResources_nullContext_throwsRegardlessOfFallback() { - val resourcesContext = - createNewResourcesContext(resourcesApkAction = "wrong", fallback = true) - - assertFailsWith(IllegalStateException::class) { resourcesContext.resources } - } - - @Test - fun getSafetyCenterConfig_withValidInputs_returnsConfigContent() { - val resourcesContext = createNewResourcesContext() - - val config = resourcesContext.safetyCenterConfig - val configContent = config?.bufferedReader().use { it?.readText() } - - assertThat(config).isNotNull() - assertThat(configContent).isEqualTo(CONFIG_CONTENT) - } - - @Test - fun getSafetyCenterConfig_anotherValidConfigName_returnsConfigContent() { - val resourcesContext = createNewResourcesContext() - - val config = resourcesContext.getSafetyCenterConfig(CONFIG_NAME) - val configContent = config?.bufferedReader().use { it?.readText() } - - assertThat(config).isNotNull() - assertThat(configContent).isEqualTo(CONFIG_CONTENT) - } - - @Test - fun getSafetyCenterConfig_invalidConfigNameWithFallback_returnsNull() { - val resourcesContext = createNewResourcesContext(fallback = true) - - assertThat(resourcesContext.getSafetyCenterConfig("wrong")).isNull() - } - - @Test - fun getSafetyCenterConfig_invalidConfigNameWithoutFallback_throws() { - val resourcesContext = createNewResourcesContext(fallback = false) - - assertFailsWith(Resources.NotFoundException::class) { - resourcesContext.getSafetyCenterConfig("wrong") - } - } - - @Test - fun getSafetyCenterConfig_nullContext_throwsRegardlessOfFallback() { - val resourcesContext = - createNewResourcesContext(resourcesApkAction = "wrong", fallback = true) - - assertFailsWith(IllegalStateException::class) { resourcesContext.safetyCenterConfig } - } - - @Test - fun getString_validString_returnsString() { - val resourcesContext = createNewResourcesContext() - - val ok = resourcesContext.getString(android.R.string.ok) - - assertThat(ok).isEqualTo("OK") - } - - @Test - fun getString_nullContext_throwsRegardlessOfFallback() { - val resourcesContext = - createNewResourcesContext(resourcesApkAction = "wrong", fallback = true) - - assertFailsWith(IllegalStateException::class) { - resourcesContext.getString(android.R.string.ok) - } - } - - @Test - fun getStringWithFormatArgs_validString_returnsString() { - val resourcesContext = createNewResourcesContext() - - val ok = resourcesContext.getString(android.R.string.ok, "") - - assertThat(ok).isEqualTo("OK") - } - - @Test - fun getStringWithFormatArgs_nullContext_throwsRegardlessOfFallback() { - val resourcesContext = - createNewResourcesContext(resourcesApkAction = "wrong", fallback = true) - - assertFailsWith(IllegalStateException::class) { - resourcesContext.getString(android.R.string.ok, "") - } - } - - @Test - fun getStringByName_validString_returnsString() { - val resourcesContext = createNewResourcesContext() - - assertThat(resourcesContext.getStringByName("valid_string")).isEqualTo("I exist!") - } - - @Test - fun getStringByName_invalidStringWithFallback_returnsEmptyString() { - val resourcesContext = createNewResourcesContext(fallback = true) - - assertThat(resourcesContext.getStringByName("invalid_string")).isEqualTo("") - } - - @Test - fun getStringByName_invalidStringWithoutFallback_throws() { - val resourcesContext = createNewResourcesContext(fallback = false) - - assertFailsWith(Resources.NotFoundException::class) { - resourcesContext.getStringByName("invalid_string") - } - } - - @Test - fun getStringByName_nullContext_throwsRegardlessOfFallback() { - val resourcesContext = - createNewResourcesContext(resourcesApkAction = "wrong", fallback = true) - - assertFailsWith(IllegalStateException::class) { - resourcesContext.getStringByName("valid_string") - } - } - - @Test - fun getStringByNameWithFormatArgs_validString_returnsString() { - val resourcesContext = createNewResourcesContext() - - assertThat(resourcesContext.getStringByName("valid_string", "")).isEqualTo("I exist!") - } - - @Test - fun getStringByNameWithFormatArgs_invalidStringWithFallback_returnsEmptyString() { - val resourcesContext = createNewResourcesContext(fallback = true) - - assertThat(resourcesContext.getStringByName("invalid_string", "")).isEqualTo("") - } - - @Test - fun getStringByNameWithFormatArgs_invalidStringWithoutFallback_throws() { - val resourcesContext = createNewResourcesContext(fallback = false) - - assertFailsWith(Resources.NotFoundException::class) { - resourcesContext.getStringByName("invalid_string", "") - } - } - - @Test - fun getStringByNameWithFormatArgs_nullContext_throwsRegardlessOfFallback() { - val resourcesContext = - createNewResourcesContext(resourcesApkAction = "wrong", fallback = true) - - assertFailsWith(IllegalStateException::class) { - resourcesContext.getStringByName("valid_string", "") - } - } - - @Test - fun getOptionalString_validString_returnsString() { - val resourcesContext = createNewResourcesContext() - - val ok = resourcesContext.getOptionalString(android.R.string.ok) - - assertThat(ok).isEqualTo("OK") - } - - @Test - fun getOptionalString_resourceIdNull_returnsNull() { - val resourcesContext = createNewResourcesContext() - - val string = resourcesContext.getOptionalString(Resources.ID_NULL) - - assertThat(string).isNull() - } - - @Test - fun getOptionalString_nullContext_throwsRegardlessOfFallback() { - val resourcesContext = - createNewResourcesContext(resourcesApkAction = "wrong", fallback = true) - - assertFailsWith(IllegalStateException::class) { - resourcesContext.getOptionalString(android.R.string.ok) - } - } - - @Test - fun getOptionalStringByName_validString_returnsString() { - val resourcesContext = createNewResourcesContext() - - assertThat(resourcesContext.getOptionalStringByName("valid_string")).isEqualTo("I exist!") - } - - @Test - fun getOptionalStringByName_invalidStringWithFallback_returnsNull() { - val resourcesContext = createNewResourcesContext(fallback = true) - - assertThat(resourcesContext.getOptionalStringByName("invalid_string")).isNull() - } - - @Test - fun getOptionalStringByName_invalidStringWithoutFallback_returnsNull() { - val resourcesContext = createNewResourcesContext(fallback = false) - - assertThat(resourcesContext.getOptionalStringByName("invalid_string")).isNull() - } - - @Test - fun getOptionalStringByName_nullContext_throwsRegardlessOfFallback() { - val resourcesContext = - createNewResourcesContext(resourcesApkAction = "wrong", fallback = true) - - assertFailsWith(IllegalStateException::class) { - resourcesContext.getOptionalStringByName("valid_string") - } - } - - @Test - fun getDrawableByName_validDrawable_returnsDrawable() { - val resourcesContext = createNewResourcesContext() - - assertThat(resourcesContext.getDrawableByName("valid_drawable", context.theme)).isNotNull() - } - - @Test - fun getDrawableByName_invalidDrawableWithFallback_returnsNull() { - val resourcesContext = createNewResourcesContext(fallback = true) - - assertThat(resourcesContext.getDrawableByName("invalid_drawable", context.theme)).isNull() - } - - @Test - fun getDrawableByName_invalidDrawableWithoutFallback_throws() { - val resourcesContext = createNewResourcesContext(fallback = false) - - assertFailsWith(Resources.NotFoundException::class) { - resourcesContext.getDrawableByName("invalid_drawable", context.theme) - } - } - - @Test - fun getDrawableByName_nullContext_throwsRegardlessOfFallback() { - val resourcesContext = - createNewResourcesContext(resourcesApkAction = "wrong", fallback = true) - - assertFailsWith(IllegalStateException::class) { - resourcesContext.getDrawableByName("valid_drawable", context.theme) - } - } - - @Test - fun getIconByDrawableName_validDrawable_returnsIcon() { - val resourcesContext = createNewResourcesContext() - - assertThat(resourcesContext.getIconByDrawableName("valid_drawable")).isNotNull() - } - - @Test - fun getIconByDrawableName_invalidDrawableWithFallback_returnsNull() { - val resourcesContext = createNewResourcesContext(fallback = true) - - assertThat(resourcesContext.getIconByDrawableName("invalid_drawable")).isNull() - } - - @Test - fun getIconByDrawableName_invalidDrawableWithoutFallback_throws() { - val resourcesContext = createNewResourcesContext(fallback = false) - - assertFailsWith(Resources.NotFoundException::class) { - resourcesContext.getIconByDrawableName("invalid_drawable") - } - } - - @Test - fun getIconByDrawableByName_nullContext_throwsRegardlessOfFallback() { - val resourcesContext = - createNewResourcesContext(resourcesApkAction = "wrong", fallback = true) - - assertFailsWith(IllegalStateException::class) { - resourcesContext.getIconByDrawableName("valid_drawable") - } - } - - @Test - fun getColorByName_validColor_returnsColor() { - val resourcesContext = createNewResourcesContext() - - assertThat(resourcesContext.getColorByName("valid_color")).isNotNull() - } - - @Test - fun getColorByName_invalidColorWithFallback_returnsNull() { - val resourcesContext = createNewResourcesContext(fallback = true) - - assertThat(resourcesContext.getColorByName("invalid_color")).isNull() - } - - @Test - fun getColorByName_invalidColorWithoutFallback_throws() { - val resourcesContext = createNewResourcesContext(fallback = false) - - assertFailsWith(Resources.NotFoundException::class) { - resourcesContext.getColorByName("invalid_color") - } - } - - @Test - fun getColorByName_nullContext_throwsRegardlessOfFallback() { - val resourcesContext = - createNewResourcesContext(resourcesApkAction = "wrong", fallback = true) - - assertFailsWith(IllegalStateException::class) { - resourcesContext.getColorByName("valid_color") - } - } - - private fun createNewResourcesContext( - resourcesApkAction: String = RESOURCES_APK_ACTION, - resourcesApkPath: String = "", - flags: Int = 0, - fallback: Boolean = false - ) = SafetyCenterResourcesContext(context, resourcesApkAction, resourcesApkPath, flags, fallback) - - companion object { - const val RESOURCES_APK_ACTION = - "com.android.safetycenter.tests.intent.action.SAFETY_CENTER_TEST_RESOURCES_APK" - const val RESOURCES_APK_PKG_NAME = - "com.android.safetycenter.tests.config.safetycenterresourceslibtestresources" - const val CONFIG_NAME = "test" - const val CONFIG_CONTENT = "TEST" - } -} |