Add test and add string template in strings.xml for RTL purposes

Change-Id: I1bce9ac7534f376a9626cac2c50e1bcc7620cf61
pull/476/head
Caren 2 years ago
parent f563862df3
commit 3712ba8c2f

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

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

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

@ -20,4 +20,5 @@
<string name="back">Back</string>
<string name="card_tap_action">Open Resource Link</string>
<string name="card_meta_data_text">%1$s • %2$s</string>
</resources>

@ -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()
}
}
Loading…
Cancel
Save