From e060aeb1e12eb3a6750767d0f52f267565122e2c Mon Sep 17 00:00:00 2001 From: Sunny Shao Date: Wed, 18 Sep 2024 09:25:38 +0800 Subject: [PATCH] Migrate the Legal Info page to Catalyst Test: atest LegalPreferenceTest Test: atest LegalSettingsScreenTest Test: atest WallpaperAttributionsPreferenceTest Bug: 360061554 Flag: com.android.settings.flags.catalyst_legal_information Change-Id: Ic15384e853584b72a847e1a10713a0c3c29273a7 --- src/com/android/settings/LegalSettings.java | 11 +++ .../legal/CopyrightPreferenceController.java | 2 + .../deviceinfo/legal/LegalPreference.kt | 58 ++++++++++++ .../legal/LegalPreferenceController.java | 2 + .../deviceinfo/legal/LegalSettingsScreen.kt | 55 +++++++++++ .../legal/LicensePreferenceController.java | 2 + ...oduleLicensesListPreferenceController.java | 2 + .../deviceinfo/legal/ModuleLicensesScreen.kt | 61 ++++++++++++ .../legal/TermsPreferenceController.java | 2 + .../legal/WallpaperAttributionsPreference.kt | 49 ++++++++++ ...paperAttributionsPreferenceController.java | 2 + .../WebViewLicensePreferenceController.java | 2 + .../CopyrightPreferenceControllerTest.java | 2 + .../deviceinfo/legal/LegalPreferenceTest.kt | 92 +++++++++++++++++++ .../legal/LegalSettingsScreenTest.kt | 53 +++++++++++ .../LicensePreferenceControllerTest.java | 2 + .../legal/TermsPreferenceControllerTest.java | 2 + ...rAttributionsPreferenceControllerTest.java | 4 +- .../WallpaperAttributionsPreferenceTest.kt | 57 ++++++++++++ ...ebViewLicensePreferenceControllerTest.java | 2 + 20 files changed, 461 insertions(+), 1 deletion(-) create mode 100644 src/com/android/settings/deviceinfo/legal/LegalPreference.kt create mode 100644 src/com/android/settings/deviceinfo/legal/LegalSettingsScreen.kt create mode 100644 src/com/android/settings/deviceinfo/legal/ModuleLicensesScreen.kt create mode 100644 src/com/android/settings/deviceinfo/legal/WallpaperAttributionsPreference.kt create mode 100644 tests/robotests/src/com/android/settings/deviceinfo/legal/LegalPreferenceTest.kt create mode 100644 tests/robotests/src/com/android/settings/deviceinfo/legal/LegalSettingsScreenTest.kt create mode 100644 tests/robotests/src/com/android/settings/deviceinfo/legal/WallpaperAttributionsPreferenceTest.kt diff --git a/src/com/android/settings/LegalSettings.java b/src/com/android/settings/LegalSettings.java index 9cb3bf4ef84..e48da2663b9 100644 --- a/src/com/android/settings/LegalSettings.java +++ b/src/com/android/settings/LegalSettings.java @@ -17,8 +17,13 @@ package com.android.settings; import android.app.settings.SettingsEnums; +import android.content.Context; + +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; import com.android.settings.dashboard.DashboardFragment; +import com.android.settings.deviceinfo.legal.LegalSettingsScreen; import com.android.settings.search.BaseSearchIndexProvider; import com.android.settingslib.search.SearchIndexable; @@ -44,4 +49,10 @@ public class LegalSettings extends DashboardFragment { public static final BaseSearchIndexProvider SEARCH_INDEX_DATA_PROVIDER = new BaseSearchIndexProvider(R.xml.about_legal); + + @Nullable + @Override + public String getPreferenceScreenBindingKey(@NonNull Context context) { + return LegalSettingsScreen.KEY; + } } diff --git a/src/com/android/settings/deviceinfo/legal/CopyrightPreferenceController.java b/src/com/android/settings/deviceinfo/legal/CopyrightPreferenceController.java index 68e51f40df7..4c2413a8c23 100644 --- a/src/com/android/settings/deviceinfo/legal/CopyrightPreferenceController.java +++ b/src/com/android/settings/deviceinfo/legal/CopyrightPreferenceController.java @@ -17,6 +17,7 @@ package com.android.settings.deviceinfo.legal; import android.content.Context; import android.content.Intent; +// LINT.IfChange public class CopyrightPreferenceController extends LegalPreferenceController { private static final Intent INTENT = new Intent("android.settings.COPYRIGHT"); @@ -30,3 +31,4 @@ public class CopyrightPreferenceController extends LegalPreferenceController { return INTENT; } } +// LINT.ThenChange(LegalPreference.kt) diff --git a/src/com/android/settings/deviceinfo/legal/LegalPreference.kt b/src/com/android/settings/deviceinfo/legal/LegalPreference.kt new file mode 100644 index 00000000000..af3537bebb0 --- /dev/null +++ b/src/com/android/settings/deviceinfo/legal/LegalPreference.kt @@ -0,0 +1,58 @@ +/* + * Copyright (C) 2024 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.settings.deviceinfo.legal + +import android.content.Context +import android.content.Intent +import android.content.pm.ApplicationInfo +import android.content.pm.ResolveInfo +import androidx.annotation.StringRes +import com.android.settingslib.metadata.PreferenceAvailabilityProvider +import com.android.settingslib.metadata.PreferenceMetadata +import com.android.settingslib.metadata.PreferenceTitleProvider + +// LINT.IfChange +class LegalPreference( + override val key: String, + @StringRes val defaultTitle: Int = 0, + val intentAction: String, +) : PreferenceMetadata, PreferenceTitleProvider, PreferenceAvailabilityProvider { + + override fun getTitle(context: Context): CharSequence? { + val resolveInfo = + findMatchingSpecificActivity(context) ?: return context.getText(defaultTitle) + return resolveInfo.loadLabel(context.packageManager) + } + + override fun isAvailable(context: Context) = (findMatchingSpecificActivity(context) != null) + + override fun intent(context: Context) = + findMatchingSpecificActivity(context)?.let { + Intent() + .setClassName(it.activityInfo.packageName, it.activityInfo.name) + .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) + } + + private fun findMatchingSpecificActivity(context: Context): ResolveInfo? { + val intent = Intent(intentAction) + // Find the activity that is in the system image + val list: List = context.packageManager.queryIntentActivities(intent, 0) + return list.firstOrNull { + (it.activityInfo.applicationInfo.flags and ApplicationInfo.FLAG_SYSTEM) != 0 + } + } +} +// LINT.ThenChange(LegalPreferenceController.java) diff --git a/src/com/android/settings/deviceinfo/legal/LegalPreferenceController.java b/src/com/android/settings/deviceinfo/legal/LegalPreferenceController.java index fe45923d60b..adbc2d75a3c 100644 --- a/src/com/android/settings/deviceinfo/legal/LegalPreferenceController.java +++ b/src/com/android/settings/deviceinfo/legal/LegalPreferenceController.java @@ -27,6 +27,7 @@ import com.android.settings.core.BasePreferenceController; import java.util.List; +// LINT.IfChange public abstract class LegalPreferenceController extends BasePreferenceController { private final PackageManager mPackageManager; private Preference mPreference; @@ -94,3 +95,4 @@ public abstract class LegalPreferenceController extends BasePreferenceController mPreference.setTitle(resolveInfo.loadLabel(mPackageManager)); } } +// LINT.ThenChange(LegalPreference.kt) diff --git a/src/com/android/settings/deviceinfo/legal/LegalSettingsScreen.kt b/src/com/android/settings/deviceinfo/legal/LegalSettingsScreen.kt new file mode 100644 index 00000000000..f990b5dbeb0 --- /dev/null +++ b/src/com/android/settings/deviceinfo/legal/LegalSettingsScreen.kt @@ -0,0 +1,55 @@ +/* + * Copyright (C) 2024 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.settings.deviceinfo.legal + +import android.content.Context +import com.android.settings.LegalSettings +import com.android.settings.R +import com.android.settings.flags.Flags +import com.android.settingslib.metadata.ProvidePreferenceScreen +import com.android.settingslib.metadata.preferenceHierarchy +import com.android.settingslib.preference.PreferenceScreenCreator + +@ProvidePreferenceScreen +open class LegalSettingsScreen : PreferenceScreenCreator { + override val key: String + get() = KEY + + override val title: Int + get() = R.string.legal_information + + override fun isFlagEnabled(context: Context) = Flags.catalystLegalInformation() + + override fun fragmentClass() = LegalSettings::class.java + + override fun getPreferenceHierarchy(context: Context) = + preferenceHierarchy(this) { + +LegalPreference("copyright", R.string.copyright_title, "android.settings.COPYRIGHT") + +LegalPreference("license", R.string.license_title, "android.settings.LICENSE") + +LegalPreference("terms", R.string.terms_title, "android.settings.TERMS") + +ModuleLicensesScreen.KEY // Use screen key in case it is overlaid. + +LegalPreference( + "webview_license", + R.string.webview_license_title, + "android.settings.WEBVIEW_LICENSE", + ) + +WallpaperAttributionsPreference() + } + + companion object { + const val KEY = "legal_information" + } +} diff --git a/src/com/android/settings/deviceinfo/legal/LicensePreferenceController.java b/src/com/android/settings/deviceinfo/legal/LicensePreferenceController.java index 67af15b4fc6..9bd74fd3a8d 100644 --- a/src/com/android/settings/deviceinfo/legal/LicensePreferenceController.java +++ b/src/com/android/settings/deviceinfo/legal/LicensePreferenceController.java @@ -17,6 +17,7 @@ package com.android.settings.deviceinfo.legal; import android.content.Context; import android.content.Intent; +// LINT.IfChange public class LicensePreferenceController extends LegalPreferenceController { private static final Intent INTENT = new Intent("android.settings.LICENSE"); @@ -30,3 +31,4 @@ public class LicensePreferenceController extends LegalPreferenceController { return INTENT; } } +// LINT.ThenChange(LegalPreference.kt) diff --git a/src/com/android/settings/deviceinfo/legal/ModuleLicensesListPreferenceController.java b/src/com/android/settings/deviceinfo/legal/ModuleLicensesListPreferenceController.java index 9faff856074..7b6ca460cbe 100644 --- a/src/com/android/settings/deviceinfo/legal/ModuleLicensesListPreferenceController.java +++ b/src/com/android/settings/deviceinfo/legal/ModuleLicensesListPreferenceController.java @@ -24,6 +24,7 @@ import com.android.settings.core.BasePreferenceController; import java.util.List; +// LINT.IfChange public class ModuleLicensesListPreferenceController extends BasePreferenceController { public ModuleLicensesListPreferenceController(Context context, String preferenceKey) { @@ -39,3 +40,4 @@ public class ModuleLicensesListPreferenceController extends BasePreferenceContro : CONDITIONALLY_UNAVAILABLE; } } +// LINT.ThenChange(ModuleLicensesScreen.kt) diff --git a/src/com/android/settings/deviceinfo/legal/ModuleLicensesScreen.kt b/src/com/android/settings/deviceinfo/legal/ModuleLicensesScreen.kt new file mode 100644 index 00000000000..c7f5e9c05d6 --- /dev/null +++ b/src/com/android/settings/deviceinfo/legal/ModuleLicensesScreen.kt @@ -0,0 +1,61 @@ +/* + * Copyright (C) 2024 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.settings.deviceinfo.legal + +import android.content.Context +import com.android.settings.R +import com.android.settingslib.metadata.PreferenceAvailabilityProvider +import com.android.settingslib.metadata.ProvidePreferenceScreen +import com.android.settingslib.metadata.preferenceHierarchy +import com.android.settingslib.preference.PreferenceScreenCreator + +// LINT.IfChange +@ProvidePreferenceScreen +class ModuleLicensesScreen : PreferenceScreenCreator, PreferenceAvailabilityProvider { + override val key: String + get() = KEY + + override val title: Int + get() = R.string.module_license_title + + // We need to avoid directly assign fragment attribute in the bind() API. So we need to create + // a screen and provide it to its parent screen LegalSettingsScreen. + // By the way, we also need to set the isFlagEnabled() as false. Let system render the legacy + // UI. The hierarchy will be added while migrating this page. + override fun isFlagEnabled(context: Context) = false + + override fun fragmentClass() = ModuleLicensesDashboard::class.java + + override fun getPreferenceHierarchy(context: Context) = preferenceHierarchy(this) {} + + override fun isAvailable(context: Context): Boolean { + val modules = context.packageManager.getInstalledModules(/* flags= */ 0) + return modules.any { + try { + ModuleLicenseProvider.getPackageAssetManager(context.packageManager, it.packageName) + .list("") + ?.contains(ModuleLicenseProvider.GZIPPED_LICENSE_FILE_NAME) == true + } catch (e: Exception) { + false + } + } + } + + companion object { + const val KEY = "module_license" + } +} +// LINT.ThenChange(ModuleLicensesListPreferenceController.java) diff --git a/src/com/android/settings/deviceinfo/legal/TermsPreferenceController.java b/src/com/android/settings/deviceinfo/legal/TermsPreferenceController.java index bccc44579b9..764bde44a04 100644 --- a/src/com/android/settings/deviceinfo/legal/TermsPreferenceController.java +++ b/src/com/android/settings/deviceinfo/legal/TermsPreferenceController.java @@ -17,6 +17,7 @@ package com.android.settings.deviceinfo.legal; import android.content.Context; import android.content.Intent; +// LINT.IfChange public class TermsPreferenceController extends LegalPreferenceController { private static final Intent INTENT = new Intent("android.settings.TERMS"); @@ -30,3 +31,4 @@ public class TermsPreferenceController extends LegalPreferenceController { return INTENT; } } +// LINT.ThenChange(LegalPreference.kt) diff --git a/src/com/android/settings/deviceinfo/legal/WallpaperAttributionsPreference.kt b/src/com/android/settings/deviceinfo/legal/WallpaperAttributionsPreference.kt new file mode 100644 index 00000000000..7a934c29058 --- /dev/null +++ b/src/com/android/settings/deviceinfo/legal/WallpaperAttributionsPreference.kt @@ -0,0 +1,49 @@ +/* + * Copyright (C) 2024 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.settings.deviceinfo.legal + +import android.content.Context +import androidx.preference.Preference +import com.android.settings.R +import com.android.settingslib.metadata.PreferenceAvailabilityProvider +import com.android.settingslib.metadata.PreferenceMetadata +import com.android.settingslib.preference.PreferenceBinding + +// LINT.IfChange +class WallpaperAttributionsPreference : + PreferenceMetadata, PreferenceBinding, PreferenceAvailabilityProvider { + override val key: String + get() = KEY + + override val title: Int + get() = R.string.wallpaper_attributions + + override val summary: Int + get() = R.string.wallpaper_attributions_values + + override fun bind(preference: Preference, metadata: PreferenceMetadata) { + super.bind(preference, metadata) + preference.isSelectable = false + } + + override fun isAvailable(context: Context) = + context.resources.getBoolean(R.bool.config_show_wallpaper_attribution) + + companion object { + const val KEY = "wallpaper_attributions" + } +} +// LINT.ThenChange(WallpaperAttributionsPreferenceController.java) diff --git a/src/com/android/settings/deviceinfo/legal/WallpaperAttributionsPreferenceController.java b/src/com/android/settings/deviceinfo/legal/WallpaperAttributionsPreferenceController.java index caa5afcdc58..cc9c0928df9 100644 --- a/src/com/android/settings/deviceinfo/legal/WallpaperAttributionsPreferenceController.java +++ b/src/com/android/settings/deviceinfo/legal/WallpaperAttributionsPreferenceController.java @@ -20,6 +20,7 @@ import android.content.Context; import com.android.settings.R; import com.android.settings.core.BasePreferenceController; +// LINT.IfChange public class WallpaperAttributionsPreferenceController extends BasePreferenceController { public WallpaperAttributionsPreferenceController(Context context, String key) { @@ -33,3 +34,4 @@ public class WallpaperAttributionsPreferenceController extends BasePreferenceCon : UNSUPPORTED_ON_DEVICE; } } +// LINT.ThenChange(WallpaperAttributionsPreference.kt) diff --git a/src/com/android/settings/deviceinfo/legal/WebViewLicensePreferenceController.java b/src/com/android/settings/deviceinfo/legal/WebViewLicensePreferenceController.java index 9d8b3f95027..ebb9152d80a 100644 --- a/src/com/android/settings/deviceinfo/legal/WebViewLicensePreferenceController.java +++ b/src/com/android/settings/deviceinfo/legal/WebViewLicensePreferenceController.java @@ -17,6 +17,7 @@ package com.android.settings.deviceinfo.legal; import android.content.Context; import android.content.Intent; +// LINT.IfChange public class WebViewLicensePreferenceController extends LegalPreferenceController { private static final Intent INTENT = new Intent("android.settings.WEBVIEW_LICENSE"); @@ -30,3 +31,4 @@ public class WebViewLicensePreferenceController extends LegalPreferenceControlle return INTENT; } } +// LINT.ThenChange(LegalPreference.kt) diff --git a/tests/robotests/src/com/android/settings/deviceinfo/legal/CopyrightPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/deviceinfo/legal/CopyrightPreferenceControllerTest.java index 3a6e494ff04..28772984fd0 100644 --- a/tests/robotests/src/com/android/settings/deviceinfo/legal/CopyrightPreferenceControllerTest.java +++ b/tests/robotests/src/com/android/settings/deviceinfo/legal/CopyrightPreferenceControllerTest.java @@ -45,6 +45,7 @@ import org.robolectric.RuntimeEnvironment; import java.util.ArrayList; import java.util.List; +// LINT.IfChange @RunWith(RobolectricTestRunner.class) public class CopyrightPreferenceControllerTest { @@ -119,3 +120,4 @@ public class CopyrightPreferenceControllerTest { return testResolveInfo; } } +// LINT.ThenChange(LegalPreferenceTest.kt) diff --git a/tests/robotests/src/com/android/settings/deviceinfo/legal/LegalPreferenceTest.kt b/tests/robotests/src/com/android/settings/deviceinfo/legal/LegalPreferenceTest.kt new file mode 100644 index 00000000000..5b65b164f82 --- /dev/null +++ b/tests/robotests/src/com/android/settings/deviceinfo/legal/LegalPreferenceTest.kt @@ -0,0 +1,92 @@ +/* + * Copyright (C) 2024 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.settings.deviceinfo.legal + +import android.content.Context +import android.content.ContextWrapper +import android.content.Intent +import android.content.pm.ActivityInfo +import android.content.pm.ApplicationInfo +import android.content.pm.PackageManager +import android.content.pm.ResolveInfo +import androidx.test.core.app.ApplicationProvider +import com.android.settings.R +import com.google.common.truth.Truth.assertThat +import org.junit.Test +import org.junit.runner.RunWith +import org.mockito.ArgumentMatchers.any +import org.mockito.ArgumentMatchers.anyInt +import org.mockito.kotlin.doReturn +import org.mockito.kotlin.mock +import org.mockito.kotlin.stub +import org.robolectric.RobolectricTestRunner + +// LINT.IfChange +@RunWith(RobolectricTestRunner::class) +class LegalPreferenceTest { + private val pkgManager = mock() + + private val context: Context = + object : ContextWrapper(ApplicationProvider.getApplicationContext()) { + override fun getPackageManager(): PackageManager = pkgManager + } + + private val copyrightPreference = + LegalPreference("copyright", R.string.copyright_title, "android.settings.COPYRIGHT") + + @Test + fun isAvailable_systemApp_shouldReturnTrue() { + val testResolveInfos: MutableList = ArrayList() + testResolveInfos.add(getTestResolveInfo(/* isSystemApp= */ true)) + + pkgManager.stub { + on { queryIntentActivities(any(Intent::class.java), anyInt()) } doReturn + testResolveInfos + } + + assertThat(copyrightPreference.isAvailable(context)).isTrue() + } + + @Test + fun isAvailable_nonSystemApp_shouldReturnFalse() { + val testResolveInfos: MutableList = ArrayList() + testResolveInfos.add(getTestResolveInfo(/* isSystemApp= */ false)) + + pkgManager.stub { + on { queryIntentActivities(any(Intent::class.java), anyInt()) } doReturn + testResolveInfos + } + + assertThat(copyrightPreference.isAvailable(context)).isFalse() + } + + private fun getTestResolveInfo(isSystemApp: Boolean): ResolveInfo { + val testResolveInfo = ResolveInfo() + val testAppInfo = ApplicationInfo() + if (isSystemApp) { + testAppInfo.flags = testAppInfo.flags or ApplicationInfo.FLAG_SYSTEM + } + + testResolveInfo.activityInfo = + ActivityInfo().apply { + name = "TestActivityName" + packageName = "TestPackageName" + applicationInfo = testAppInfo + } + return testResolveInfo + } +} +// LINT.ThenChange(CopyrightPreferenceControllerTest.java) diff --git a/tests/robotests/src/com/android/settings/deviceinfo/legal/LegalSettingsScreenTest.kt b/tests/robotests/src/com/android/settings/deviceinfo/legal/LegalSettingsScreenTest.kt new file mode 100644 index 00000000000..48e21b47dcc --- /dev/null +++ b/tests/robotests/src/com/android/settings/deviceinfo/legal/LegalSettingsScreenTest.kt @@ -0,0 +1,53 @@ +/* + * Copyright (C) 2024 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.settings.deviceinfo.legal + +import android.content.Context +import android.platform.test.annotations.DisableFlags +import android.platform.test.annotations.EnableFlags +import android.platform.test.flag.junit.SetFlagsRule +import android.text.TextUtils +import androidx.test.core.app.ApplicationProvider +import com.android.settings.flags.Flags +import com.google.common.truth.Truth.assertThat +import org.junit.Rule +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner + +@RunWith(RobolectricTestRunner::class) +class LegalSettingsScreenTest { + @get:Rule val setFlagsRule = SetFlagsRule() + private val context: Context = ApplicationProvider.getApplicationContext() + private val legalSettingsScreen = LegalSettingsScreen() + + @Test + fun screenKey_exist() { + assertThat(TextUtils.equals(legalSettingsScreen.key, LegalSettingsScreen.KEY)).isTrue() + } + + @Test + @EnableFlags(Flags.FLAG_CATALYST_LEGAL_INFORMATION) + fun isFlagEnabled_returnTrue() { + assertThat(legalSettingsScreen.isFlagEnabled(context)).isTrue() + } + + @Test + @DisableFlags(Flags.FLAG_CATALYST_LEGAL_INFORMATION) + fun isFlagDisabled_returnTrue() { + assertThat(legalSettingsScreen.isFlagEnabled(context)).isFalse() + } +} diff --git a/tests/robotests/src/com/android/settings/deviceinfo/legal/LicensePreferenceControllerTest.java b/tests/robotests/src/com/android/settings/deviceinfo/legal/LicensePreferenceControllerTest.java index f51c7ad0e43..d6fdf73b3a2 100644 --- a/tests/robotests/src/com/android/settings/deviceinfo/legal/LicensePreferenceControllerTest.java +++ b/tests/robotests/src/com/android/settings/deviceinfo/legal/LicensePreferenceControllerTest.java @@ -45,6 +45,7 @@ import org.robolectric.RuntimeEnvironment; import java.util.ArrayList; import java.util.List; +// LINT.IfChange @RunWith(RobolectricTestRunner.class) public class LicensePreferenceControllerTest { @@ -119,3 +120,4 @@ public class LicensePreferenceControllerTest { return testResolveInfo; } } +// LINT.ThenChange(LegalPreferenceTest.kt) diff --git a/tests/robotests/src/com/android/settings/deviceinfo/legal/TermsPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/deviceinfo/legal/TermsPreferenceControllerTest.java index 2a91fe1ee65..f8ccd0e432c 100644 --- a/tests/robotests/src/com/android/settings/deviceinfo/legal/TermsPreferenceControllerTest.java +++ b/tests/robotests/src/com/android/settings/deviceinfo/legal/TermsPreferenceControllerTest.java @@ -45,6 +45,7 @@ import org.robolectric.RuntimeEnvironment; import java.util.ArrayList; import java.util.List; +// LINT.IfChange @RunWith(RobolectricTestRunner.class) public class TermsPreferenceControllerTest { @@ -119,3 +120,4 @@ public class TermsPreferenceControllerTest { return testResolveInfo; } } +// LINT.ThenChange(LegalPreferenceTest.kt) diff --git a/tests/robotests/src/com/android/settings/deviceinfo/legal/WallpaperAttributionsPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/deviceinfo/legal/WallpaperAttributionsPreferenceControllerTest.java index c06f069e2d5..2f8a8d44c2f 100644 --- a/tests/robotests/src/com/android/settings/deviceinfo/legal/WallpaperAttributionsPreferenceControllerTest.java +++ b/tests/robotests/src/com/android/settings/deviceinfo/legal/WallpaperAttributionsPreferenceControllerTest.java @@ -29,6 +29,7 @@ import org.robolectric.RobolectricTestRunner; import org.robolectric.RuntimeEnvironment; import org.robolectric.annotation.Config; +// LINT.IfChange @RunWith(RobolectricTestRunner.class) public class WallpaperAttributionsPreferenceControllerTest { @@ -54,4 +55,5 @@ public class WallpaperAttributionsPreferenceControllerTest { assertThat(mController.getAvailabilityStatus()) .isEqualTo(BasePreferenceController.UNSUPPORTED_ON_DEVICE); } -} \ No newline at end of file +} +// LINT.ThenChange(WallpaperAttributionsPreferenceTest.kt) diff --git a/tests/robotests/src/com/android/settings/deviceinfo/legal/WallpaperAttributionsPreferenceTest.kt b/tests/robotests/src/com/android/settings/deviceinfo/legal/WallpaperAttributionsPreferenceTest.kt new file mode 100644 index 00000000000..00e0b0150f3 --- /dev/null +++ b/tests/robotests/src/com/android/settings/deviceinfo/legal/WallpaperAttributionsPreferenceTest.kt @@ -0,0 +1,57 @@ +/* + * Copyright (C) 2024 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.settings.deviceinfo.legal + +import android.content.Context +import android.content.ContextWrapper +import android.content.res.Resources +import androidx.test.core.app.ApplicationProvider +import com.google.common.truth.Truth.assertThat +import org.junit.Test +import org.junit.runner.RunWith +import org.mockito.ArgumentMatchers.anyInt +import org.mockito.kotlin.doReturn +import org.mockito.kotlin.mock +import org.mockito.kotlin.stub +import org.robolectric.RobolectricTestRunner + +// LINT.IfChange +@RunWith(RobolectricTestRunner::class) +class WallpaperAttributionsPreferenceTest { + private val mockResources = mock() + + private val context: Context = + object : ContextWrapper(ApplicationProvider.getApplicationContext()) { + override fun getResources(): Resources = mockResources + } + + private val wallpaperAttributionsPreference = WallpaperAttributionsPreference() + + @Test + fun isAvailable_configTrue_shouldReturnTrue() { + mockResources.stub { on { getBoolean(anyInt()) } doReturn true } + + assertThat(wallpaperAttributionsPreference.isAvailable(context)).isTrue() + } + + @Test + fun isAvailable_configFalse_shouldReturnFalse() { + mockResources.stub { on { getBoolean(anyInt()) } doReturn false } + + assertThat(wallpaperAttributionsPreference.isAvailable(context)).isFalse() + } +} +// LINT.ThenChange(WallpaperAttributionsPreferenceControllerTest.java) diff --git a/tests/robotests/src/com/android/settings/deviceinfo/legal/WebViewLicensePreferenceControllerTest.java b/tests/robotests/src/com/android/settings/deviceinfo/legal/WebViewLicensePreferenceControllerTest.java index 55604c33373..0836839516c 100644 --- a/tests/robotests/src/com/android/settings/deviceinfo/legal/WebViewLicensePreferenceControllerTest.java +++ b/tests/robotests/src/com/android/settings/deviceinfo/legal/WebViewLicensePreferenceControllerTest.java @@ -45,6 +45,7 @@ import org.robolectric.RuntimeEnvironment; import java.util.ArrayList; import java.util.List; +// LINT.IfChange @RunWith(RobolectricTestRunner.class) public class WebViewLicensePreferenceControllerTest { @@ -119,3 +120,4 @@ public class WebViewLicensePreferenceControllerTest { return testResolveInfo; } } +// LINT.ThenChange(LegalPreferenceTest.kt)