From 3712ba8c2fa0828d44fef471821a03ed1988e624 Mon Sep 17 00:00:00 2001 From: Caren Date: Wed, 7 Dec 2022 14:38:21 -0800 Subject: [PATCH] Add test and add string template in strings.xml for RTL purposes Change-Id: I1bce9ac7534f376a9626cac2c50e1bcc7620cf61 --- .../core/model/data/NewsResource.kt | 12 ++++ core/ui/build.gradle.kts | 8 ++- .../nowinandroid/core/ui/NewsResourceCard.kt | 23 +++---- core/ui/src/main/res/values/strings.xml | 1 + .../core/ui/NewsResourceCardTest.kt | 69 +++++++++++++++++++ 5 files changed, 99 insertions(+), 14 deletions(-) create mode 100644 core/ui/src/test/java/com/google/samples/apps/nowinandroid/core/ui/NewsResourceCardTest.kt diff --git a/core/model/src/main/java/com/google/samples/apps/nowinandroid/core/model/data/NewsResource.kt b/core/model/src/main/java/com/google/samples/apps/nowinandroid/core/model/data/NewsResource.kt index a22ca920d..4f75d27ad 100644 --- a/core/model/src/main/java/com/google/samples/apps/nowinandroid/core/model/data/NewsResource.kt +++ b/core/model/src/main/java/com/google/samples/apps/nowinandroid/core/model/data/NewsResource.kt @@ -17,6 +17,7 @@ package com.google.samples.apps.nowinandroid.core.model.data import com.google.samples.apps.nowinandroid.core.model.data.NewsResourceType.Codelab +import com.google.samples.apps.nowinandroid.core.model.data.NewsResourceType.Unknown import com.google.samples.apps.nowinandroid.core.model.data.NewsResourceType.Video import kotlinx.datetime.Instant import kotlinx.datetime.LocalDateTime @@ -83,5 +84,16 @@ val previewNewsResources = listOf( publishDate = Instant.parse("2021-11-01T00:00:00.000Z"), type = Video, topics = listOf(previewTopics[2]) + ), + NewsResource( + id = "4", + title = "New Jetpack Release", + content = "New Jetpack release includes updates to libraries such as CameraX, Benchmark, and" + + "more!", + url = "https://developer.android.com/jetpack/androidx/versions/all-channel", + headerImageUrl = "", + publishDate = Instant.parse("2022-10-01T00:00:00.000Z"), + type = Unknown, + topics = listOf(previewTopics[2]) ) ) diff --git a/core/ui/build.gradle.kts b/core/ui/build.gradle.kts index 794b1573b..7c39f1eda 100644 --- a/core/ui/build.gradle.kts +++ b/core/ui/build.gradle.kts @@ -20,6 +20,9 @@ plugins { } android { + defaultConfig { + testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner" + } namespace = "com.google.samples.apps.nowinandroid.core.ui" } @@ -44,4 +47,7 @@ dependencies { api(libs.androidx.compose.runtime.livedata) api(libs.androidx.metrics) api(libs.androidx.tracing.ktx) -} + + testImplementation(project(":core:testing")) + androidTestImplementation(project(":core:testing")) +} \ No newline at end of file diff --git a/core/ui/src/main/java/com/google/samples/apps/nowinandroid/core/ui/NewsResourceCard.kt b/core/ui/src/main/java/com/google/samples/apps/nowinandroid/core/ui/NewsResourceCard.kt index a7503273b..47b1118aa 100644 --- a/core/ui/src/main/java/com/google/samples/apps/nowinandroid/core/ui/NewsResourceCard.kt +++ b/core/ui/src/main/java/com/google/samples/apps/nowinandroid/core/ui/NewsResourceCard.kt @@ -176,7 +176,7 @@ fun BookmarkButton( } @Composable -private fun dateFormatted(publishDate: Instant): String { +fun dateFormatted(publishDate: Instant): String { var zoneId by remember { mutableStateOf(ZoneId.systemDefault()) } val context = LocalContext.current @@ -199,19 +199,16 @@ private fun dateFormatted(publishDate: Instant): String { fun NewsResourceDateAndType( publishDate: Instant, resourceType: NewsResourceType - ) { - val date = dateFormatted(publishDate) - - if (resourceType != NewsResourceType.Unknown) { - Text( - "$date • ${resourceType.displayText}", style = MaterialTheme.typography.labelSmall - ) - } else { - Text( - date, style = MaterialTheme.typography.labelSmall - ) - } + val formattedDate = dateFormatted(publishDate) + Text( + if (resourceType != NewsResourceType.Unknown) { + stringResource(R.string.card_meta_data_text, formattedDate, resourceType.displayText) + } else { + formattedDate + }, + style = MaterialTheme.typography.labelSmall + ) } @Composable diff --git a/core/ui/src/main/res/values/strings.xml b/core/ui/src/main/res/values/strings.xml index 548ea63ef..6d7c98eb1 100644 --- a/core/ui/src/main/res/values/strings.xml +++ b/core/ui/src/main/res/values/strings.xml @@ -20,4 +20,5 @@ Back Open Resource Link + %1$s • %2$s diff --git a/core/ui/src/test/java/com/google/samples/apps/nowinandroid/core/ui/NewsResourceCardTest.kt b/core/ui/src/test/java/com/google/samples/apps/nowinandroid/core/ui/NewsResourceCardTest.kt new file mode 100644 index 000000000..f7c375409 --- /dev/null +++ b/core/ui/src/test/java/com/google/samples/apps/nowinandroid/core/ui/NewsResourceCardTest.kt @@ -0,0 +1,69 @@ +/* + * 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 + * + * https://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.google.samples.apps.nowinandroid.core.ui + +import androidx.compose.ui.test.assertIsDisplayed +import androidx.compose.ui.test.junit4.createComposeRule +import androidx.compose.ui.test.onNodeWithText +import com.google.samples.apps.nowinandroid.core.model.data.previewNewsResources +import org.junit.Rule +import org.junit.Test + +class NewsResourceCardTest { + @get:Rule + val composeTestRule = createComposeRule() + + @Test + fun testMetaDataDisplay() { + // Resource with known resource type + val newsResource = previewNewsResources[0] + var dateFormatted = "" + + composeTestRule.setContent { + NewsResourceCardExpanded( + newsResource = newsResource, + isBookmarked = false, + onToggleBookmark = {}, + onClick = {} + ) + + dateFormatted = dateFormatted(publishDate = newsResource.publishDate) + } + + composeTestRule + .onNodeWithText(dateFormatted + " • " + newsResource.type.displayText) + .assertIsDisplayed() + + // Resource with Unknown resource type + val newsResource2 = previewNewsResources[4] + + composeTestRule.setContent { + NewsResourceCardExpanded( + newsResource = newsResource2, + isBookmarked = false, + onToggleBookmark = {}, + onClick = {} + ) + + dateFormatted = dateFormatted(publishDate = newsResource.publishDate) + } + + composeTestRule + .onNodeWithText(dateFormatted) + .assertIsDisplayed() + } +} \ No newline at end of file