diff --git a/packages/SettingsLib/Spa/settings.gradle b/packages/SettingsLib/Spa/settings.gradle index b627a7036f661..1c5a1ceda34a3 100644 --- a/packages/SettingsLib/Spa/settings.gradle +++ b/packages/SettingsLib/Spa/settings.gradle @@ -33,4 +33,3 @@ rootProject.name = "SpaLib" include ':spa' include ':gallery' include ':testutils' -include ':tests' diff --git a/packages/SettingsLib/Spa/spa/build.gradle b/packages/SettingsLib/Spa/spa/build.gradle index b1d8d0dadfbd6..19963fbeed827 100644 --- a/packages/SettingsLib/Spa/spa/build.gradle +++ b/packages/SettingsLib/Spa/spa/build.gradle @@ -27,6 +27,8 @@ android { defaultConfig { minSdk MIN_SDK targetSdk TARGET_SDK + + testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" } sourceSets { @@ -37,6 +39,13 @@ android { res.srcDirs = ["res"] manifest.srcFile "AndroidManifest.xml" } + androidTest { + kotlin { + srcDir "../tests/src" + } + res.srcDirs = ["../tests/res"] + manifest.srcFile "../tests/AndroidManifest.xml" + } } compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 @@ -52,6 +61,11 @@ android { composeOptions { kotlinCompilerExtensionVersion jetpack_compose_compiler_version } + buildTypes { + debug { + testCoverageEnabled = true + } + } } dependencies { @@ -72,4 +86,29 @@ dependencies { api "com.google.android.material:material:1.7.0-alpha03" debugApi "androidx.compose.ui:ui-tooling:$jetpack_compose_version" implementation "com.airbnb.android:lottie-compose:5.2.0" + + androidTestImplementation project(":testutils") + androidTestImplementation "com.linkedin.dexmaker:dexmaker-mockito:2.28.1" +} + +task coverageReport(type: JacocoReport, dependsOn: "connectedDebugAndroidTest") { + group = "Reporting" + description = "Generate Jacoco coverage reports after running tests." + + sourceDirectories.from = files("src") + classDirectories.from = fileTree( + dir: "$buildDir/tmp/kotlin-classes/debug", + excludes: [ + "com/android/settingslib/spa/debug/**", + + // Excludes files forked from AndroidX. + "com/android/settingslib/spa/widget/scaffold/CustomizedAppBar*", + "com/android/settingslib/spa/widget/scaffold/TopAppBarColors*", + + // Excludes files forked from Accompanist. + "com/android/settingslib/spa/framework/compose/DrawablePainter*", + "com/android/settingslib/spa/framework/compose/Pager*", + ], + ) + executionData.from = fileTree(dir: "$buildDir/outputs/code_coverage/debugAndroidTest/connected") } diff --git a/packages/SettingsLib/Spa/spa/src/com/android/settingslib/spa/framework/util/MessageFormats.kt b/packages/SettingsLib/Spa/spa/src/com/android/settingslib/spa/framework/util/MessageFormats.kt new file mode 100644 index 0000000000000..2adfcca0bf70e --- /dev/null +++ b/packages/SettingsLib/Spa/spa/src/com/android/settingslib/spa/framework/util/MessageFormats.kt @@ -0,0 +1,34 @@ +/* + * 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.settingslib.spa.framework.util + +import android.content.Context +import android.content.res.Resources +import android.icu.text.MessageFormat +import android.os.Build +import androidx.annotation.RequiresApi +import androidx.annotation.StringRes +import java.util.Locale + +@RequiresApi(Build.VERSION_CODES.N) +fun Context.formatString(@StringRes resId: Int, vararg arguments: Pair): String = + resources.formatString(resId, *arguments) + +@RequiresApi(Build.VERSION_CODES.N) +fun Resources.formatString(@StringRes resId: Int, vararg arguments: Pair): String = + MessageFormat(getString(resId), Locale.getDefault(Locale.Category.FORMAT)) + .format(mapOf(*arguments)) diff --git a/packages/SettingsLib/Spa/tests/AndroidManifest.xml b/packages/SettingsLib/Spa/tests/AndroidManifest.xml index e2db5943ae535..1fda4e0bd24e1 100644 --- a/packages/SettingsLib/Spa/tests/AndroidManifest.xml +++ b/packages/SettingsLib/Spa/tests/AndroidManifest.xml @@ -15,7 +15,7 @@ --> + package="com.android.settingslib.spa.test"> @@ -26,6 +26,6 @@ + android:targetPackage="com.android.settingslib.spa.test"> diff --git a/packages/SettingsLib/Spa/tests/build.gradle b/packages/SettingsLib/Spa/tests/build.gradle deleted file mode 100644 index 45f9b23e11d68..0000000000000 --- a/packages/SettingsLib/Spa/tests/build.gradle +++ /dev/null @@ -1,88 +0,0 @@ -/* - * Copyright 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. - */ - -plugins { - id 'com.android.library' - id 'kotlin-android' -} - -android { - namespace 'com.android.settingslib.spa.tests' - compileSdk TARGET_SDK - buildToolsVersion = BUILD_TOOLS_VERSION - - defaultConfig { - minSdk MIN_SDK - targetSdk TARGET_SDK - - testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" - } - - sourceSets { - main { - res.srcDirs = ["res"] - } - androidTest { - kotlin { - srcDir "src" - } - manifest.srcFile "AndroidManifest.xml" - } - } - compileOptions { - sourceCompatibility JavaVersion.VERSION_1_8 - targetCompatibility JavaVersion.VERSION_1_8 - } - kotlinOptions { - jvmTarget = '1.8' - freeCompilerArgs = ["-Xjvm-default=all"] - } - buildFeatures { - compose true - } - composeOptions { - kotlinCompilerExtensionVersion jetpack_compose_compiler_version - } - buildTypes { - debug { - testCoverageEnabled = true - } - } -} - -dependencies { - androidTestImplementation project(":spa") - androidTestImplementation project(":testutils") - androidTestImplementation "com.linkedin.dexmaker:dexmaker-mockito:2.28.1" -} - -task coverageReport(type: JacocoReport, dependsOn: "connectedDebugAndroidTest") { - group = "Reporting" - description = "Generate Jacoco coverage reports after running tests." - - sourceDirectories.from = files("../spa/src") - classDirectories.from = fileTree( - dir: "../spa/build/tmp/kotlin-classes/debug", - excludes: [ - "com/android/settingslib/spa/debug/**", - - // Excludes files forked from Accompanist. - "com/android/settingslib/spa/framework/compose/DrawablePainter*", - "com/android/settingslib/spa/framework/compose/Pager*", - ], - ) - executionData.from = fileTree(dir: "$buildDir/outputs/code_coverage/debugAndroidTest/connected") -} diff --git a/packages/SettingsLib/Spa/tests/res/values/strings.xml b/packages/SettingsLib/Spa/tests/res/values/strings.xml new file mode 100644 index 0000000000000..1ca425c26f0a7 --- /dev/null +++ b/packages/SettingsLib/Spa/tests/res/values/strings.xml @@ -0,0 +1,28 @@ + + + + + {count, plural, + =1 {There is one song found.} + other {There are # songs found.} + } + + {count, plural, + =1 {There is one song found in {place}.} + other {There are # songs found in {place}.} + } + diff --git a/packages/SettingsLib/Spa/tests/src/com/android/settingslib/spa/framework/util/MessageFormatsTest.kt b/packages/SettingsLib/Spa/tests/src/com/android/settingslib/spa/framework/util/MessageFormatsTest.kt new file mode 100644 index 0000000000000..2017ad1df5177 --- /dev/null +++ b/packages/SettingsLib/Spa/tests/src/com/android/settingslib/spa/framework/util/MessageFormatsTest.kt @@ -0,0 +1,66 @@ +/* + * 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.settingslib.spa.framework.util + +import android.content.Context +import androidx.test.core.app.ApplicationProvider +import androidx.test.ext.junit.runners.AndroidJUnit4 +import com.android.settingslib.spa.test.R +import com.google.common.truth.Truth.assertThat +import org.junit.Test +import org.junit.runner.RunWith + +@RunWith(AndroidJUnit4::class) +class MessageFormatsTest { + private val context: Context = ApplicationProvider.getApplicationContext() + + @Test + fun formatString_one() { + val message = context.formatString(R.string.test_quantity_strings, "count" to 1) + + assertThat(message).isEqualTo("There is one song found.") + } + + @Test + fun formatString_other() { + val message = context.formatString(R.string.test_quantity_strings, "count" to 2) + + assertThat(message).isEqualTo("There are 2 songs found.") + } + + @Test + fun formatString_withParam_one() { + val message = context.formatString( + R.string.test_quantity_strings_with_param, + "count" to 1, + "place" to "phone", + ) + + assertThat(message).isEqualTo("There is one song found in phone.") + } + + @Test + fun formatString_withParam_other() { + val message = context.formatString( + R.string.test_quantity_strings_with_param, + "count" to 2, + "place" to "phone", + ) + + assertThat(message).isEqualTo("There are 2 songs found in phone.") + } +} diff --git a/packages/SettingsLib/Spa/tests/src/com/android/settingslib/spa/widget/illustration/IllustrationTest.kt b/packages/SettingsLib/Spa/tests/src/com/android/settingslib/spa/widget/illustration/IllustrationTest.kt index 77c505dae8a92..105fdc8c69e09 100644 --- a/packages/SettingsLib/Spa/tests/src/com/android/settingslib/spa/widget/illustration/IllustrationTest.kt +++ b/packages/SettingsLib/Spa/tests/src/com/android/settingslib/spa/widget/illustration/IllustrationTest.kt @@ -29,7 +29,7 @@ import androidx.compose.ui.test.filterToOne import androidx.compose.ui.test.hasAnyAncestor import androidx.compose.ui.test.junit4.createComposeRule import androidx.test.ext.junit.runners.AndroidJUnit4 -import com.android.settingslib.spa.tests.R +import com.android.settingslib.spa.test.R import org.junit.Rule import org.junit.Test import org.junit.runner.RunWith diff --git a/packages/SettingsLib/Spa/testutils/build.gradle b/packages/SettingsLib/Spa/testutils/build.gradle index e31eb023b8306..dd7058db3f56d 100644 --- a/packages/SettingsLib/Spa/testutils/build.gradle +++ b/packages/SettingsLib/Spa/testutils/build.gradle @@ -53,7 +53,7 @@ android { } dependencies { - api project(":SpaLib") + api project(":spa") api "androidx.arch.core:core-testing:2.1.0" api "androidx.compose.ui:ui-test-junit4:$jetpack_compose_version"