Add MessageFormats to Spa

Also merge the test build.gradle into the main one.

Bug: 260660819
Test: Unit test
Change-Id: Id4bbb3f2d19360d964a5e1ce8de9107715543e92
This commit is contained in:
Chaohui Wang
2022-12-06 15:54:49 +08:00
parent 3b1489ea55
commit e0724be67b
9 changed files with 171 additions and 93 deletions

View File

@@ -33,4 +33,3 @@ rootProject.name = "SpaLib"
include ':spa'
include ':gallery'
include ':testutils'
include ':tests'

View File

@@ -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")
}

View File

@@ -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, Any>): String =
resources.formatString(resId, *arguments)
@RequiresApi(Build.VERSION_CODES.N)
fun Resources.formatString(@StringRes resId: Int, vararg arguments: Pair<String, Any>): String =
MessageFormat(getString(resId), Locale.getDefault(Locale.Category.FORMAT))
.format(mapOf(*arguments))

View File

@@ -15,7 +15,7 @@
-->
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.android.settingslib.spa.tests">
package="com.android.settingslib.spa.test">
<uses-sdk android:minSdkVersion="21"/>
@@ -26,6 +26,6 @@
<instrumentation
android:name="androidx.test.runner.AndroidJUnitRunner"
android:label="Tests for SpaLib"
android:targetPackage="com.android.settingslib.spa.tests">
android:targetPackage="com.android.settingslib.spa.test">
</instrumentation>
</manifest>

View File

@@ -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")
}

View File

@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
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.
-->
<resources>
<string name="test_quantity_strings">{count, plural,
=1 {There is one song found.}
other {There are # songs found.}
}</string>
<string name="test_quantity_strings_with_param">{count, plural,
=1 {There is one song found in {place}.}
other {There are # songs found in {place}.}
}</string>
</resources>

View File

@@ -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.")
}
}

View File

@@ -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

View File

@@ -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"