From f150516b3cf13f12ad72de63ad5185f95d8f379f Mon Sep 17 00:00:00 2001 From: Caren Date: Wed, 7 Dec 2022 10:47:02 -0800 Subject: [PATCH 1/6] Display resource type for each news resource Change-Id: Ib33ffd0e9b87ec91d2ee4316a4e3dbf70bb5cfc9 --- .../nowinandroid/core/ui/NewsResourceCard.kt | 21 +++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) 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 2d51b3f79..1fca09cde 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 @@ -57,6 +57,7 @@ import com.google.samples.apps.nowinandroid.core.designsystem.component.NiaTopic import com.google.samples.apps.nowinandroid.core.designsystem.icon.NiaIcons import com.google.samples.apps.nowinandroid.core.designsystem.theme.NiaTheme import com.google.samples.apps.nowinandroid.core.model.data.NewsResource +import com.google.samples.apps.nowinandroid.core.model.data.NewsResourceType import com.google.samples.apps.nowinandroid.core.model.data.Topic import com.google.samples.apps.nowinandroid.core.model.data.previewNewsResources import java.time.ZoneId @@ -109,7 +110,7 @@ fun NewsResourceCardExpanded( BookmarkButton(isBookmarked, onToggleBookmark) } Spacer(modifier = Modifier.height(12.dp)) - NewsResourceDate(newsResource.publishDate) + NewsResourceDateAndNewsType(newsResource.publishDate, newsResource.type) Spacer(modifier = Modifier.height(12.dp)) NewsResourceShortDescription(newsResource.content) Spacer(modifier = Modifier.height(12.dp)) @@ -195,10 +196,22 @@ private fun dateFormatted(publishDate: Instant): String { } @Composable -fun NewsResourceDate( - publishDate: Instant +fun NewsResourceDateAndNewsType( + publishDate: Instant, + resourceType: NewsResourceType + ) { - Text(dateFormatted(publishDate), style = MaterialTheme.typography.labelSmall) + val date = dateFormatted(publishDate) + + if (resourceType != NewsResourceType.Unknown) { + Text( + "$date • ${resourceType.displayText}", style = MaterialTheme.typography.labelSmall + ) + } else { + Text( + date, style = MaterialTheme.typography.labelSmall + ) + } } @Composable From f563862df33344be813e9f0a194177a295bd4280 Mon Sep 17 00:00:00 2001 From: Caren Date: Wed, 7 Dec 2022 11:05:43 -0800 Subject: [PATCH 2/6] Rename NewsResourceDateAndNewsType -> NewsResourceDateAndType Change-Id: I535c62f3317d6ada882a08235c0c4465d9963201 --- .../samples/apps/nowinandroid/core/ui/NewsResourceCard.kt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 1fca09cde..a7503273b 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 @@ -110,7 +110,7 @@ fun NewsResourceCardExpanded( BookmarkButton(isBookmarked, onToggleBookmark) } Spacer(modifier = Modifier.height(12.dp)) - NewsResourceDateAndNewsType(newsResource.publishDate, newsResource.type) + NewsResourceDateAndType(newsResource.publishDate, newsResource.type) Spacer(modifier = Modifier.height(12.dp)) NewsResourceShortDescription(newsResource.content) Spacer(modifier = Modifier.height(12.dp)) @@ -196,7 +196,7 @@ private fun dateFormatted(publishDate: Instant): String { } @Composable -fun NewsResourceDateAndNewsType( +fun NewsResourceDateAndType( publishDate: Instant, resourceType: NewsResourceType From 3712ba8c2fa0828d44fef471821a03ed1988e624 Mon Sep 17 00:00:00 2001 From: Caren Date: Wed, 7 Dec 2022 14:38:21 -0800 Subject: [PATCH 3/6] 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 From 352bac94fbeadf3962bc2cc74143babaabf5e22d Mon Sep 17 00:00:00 2001 From: Caren Date: Wed, 7 Dec 2022 15:00:30 -0800 Subject: [PATCH 4/6] run spotless Change-Id: Ieb751d960ade646dfe35ed7d2ca039e3b8f38ba7 --- .../samples/apps/nowinandroid/core/ui/NewsResourceCardTest.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 index f7c375409..20d93b41e 100644 --- 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 @@ -66,4 +66,4 @@ class NewsResourceCardTest { .onNodeWithText(dateFormatted) .assertIsDisplayed() } -} \ No newline at end of file +} From b05302fdddbffc55db095584542287a7882b2e6b Mon Sep 17 00:00:00 2001 From: Caren Date: Mon, 12 Dec 2022 13:29:09 -0800 Subject: [PATCH 5/6] Fix tests Change-Id: Ie1e2408ea523e6ddc0941fe261fce7f2502e3416 --- core/ui/build.gradle.kts | 1 - .../core/ui/NewsResourceCardTest.kt | 35 ++++++++++++------- 2 files changed, 22 insertions(+), 14 deletions(-) rename core/ui/src/{test => androidTest}/java/com/google/samples/apps/nowinandroid/core/ui/NewsResourceCardTest.kt (59%) diff --git a/core/ui/build.gradle.kts b/core/ui/build.gradle.kts index 7c39f1eda..6b37494e2 100644 --- a/core/ui/build.gradle.kts +++ b/core/ui/build.gradle.kts @@ -48,6 +48,5 @@ dependencies { 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/test/java/com/google/samples/apps/nowinandroid/core/ui/NewsResourceCardTest.kt b/core/ui/src/androidTest/java/com/google/samples/apps/nowinandroid/core/ui/NewsResourceCardTest.kt similarity index 59% rename from core/ui/src/test/java/com/google/samples/apps/nowinandroid/core/ui/NewsResourceCardTest.kt rename to core/ui/src/androidTest/java/com/google/samples/apps/nowinandroid/core/ui/NewsResourceCardTest.kt index 20d93b41e..69f919a62 100644 --- a/core/ui/src/test/java/com/google/samples/apps/nowinandroid/core/ui/NewsResourceCardTest.kt +++ b/core/ui/src/androidTest/java/com/google/samples/apps/nowinandroid/core/ui/NewsResourceCardTest.kt @@ -16,8 +16,9 @@ package com.google.samples.apps.nowinandroid.core.ui +import androidx.activity.ComponentActivity import androidx.compose.ui.test.assertIsDisplayed -import androidx.compose.ui.test.junit4.createComposeRule +import androidx.compose.ui.test.junit4.createAndroidComposeRule import androidx.compose.ui.test.onNodeWithText import com.google.samples.apps.nowinandroid.core.model.data.previewNewsResources import org.junit.Rule @@ -25,41 +26,49 @@ import org.junit.Test class NewsResourceCardTest { @get:Rule - val composeTestRule = createComposeRule() + val composeTestRule = createAndroidComposeRule() @Test - fun testMetaDataDisplay() { - // Resource with known resource type - val newsResource = previewNewsResources[0] + fun testMetaDataDisplay_withCodelabResource() { + val newsWithKnownResourceType = previewNewsResources[0] var dateFormatted = "" composeTestRule.setContent { NewsResourceCardExpanded( - newsResource = newsResource, + newsResource = newsWithKnownResourceType, isBookmarked = false, onToggleBookmark = {}, onClick = {} ) - dateFormatted = dateFormatted(publishDate = newsResource.publishDate) + dateFormatted = dateFormatted(publishDate = newsWithKnownResourceType.publishDate) } composeTestRule - .onNodeWithText(dateFormatted + " • " + newsResource.type.displayText) - .assertIsDisplayed() + .onNodeWithText( + composeTestRule.activity.getString( + R.string.card_meta_data_text, + dateFormatted, + newsWithKnownResourceType.type.displayText + ) + ) + .assertExists() + } - // Resource with Unknown resource type - val newsResource2 = previewNewsResources[4] + @Test + fun testMetaDataDisplay_withUnknownResource() { + val newsWithUnknownResourceType = previewNewsResources[3] + var dateFormatted = "" composeTestRule.setContent { NewsResourceCardExpanded( - newsResource = newsResource2, + newsResource = newsWithUnknownResourceType, isBookmarked = false, onToggleBookmark = {}, onClick = {} ) - dateFormatted = dateFormatted(publishDate = newsResource.publishDate) + dateFormatted = dateFormatted(publishDate = newsWithUnknownResourceType.publishDate) } composeTestRule From 4ff9c039b36fc3166f54a3d27703951115fa0068 Mon Sep 17 00:00:00 2001 From: Caren Date: Wed, 14 Dec 2022 15:07:20 -0800 Subject: [PATCH 6/6] Rename NewsResourceDataAndType -> NewsResourceMetaData Change-Id: Ide67bb9adf740d9cf0bba58f369688f91e0eb85f --- .../samples/apps/nowinandroid/core/ui/NewsResourceCard.kt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 47b1118aa..c7d04680c 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 @@ -110,7 +110,7 @@ fun NewsResourceCardExpanded( BookmarkButton(isBookmarked, onToggleBookmark) } Spacer(modifier = Modifier.height(12.dp)) - NewsResourceDateAndType(newsResource.publishDate, newsResource.type) + NewsResourceMetaData(newsResource.publishDate, newsResource.type) Spacer(modifier = Modifier.height(12.dp)) NewsResourceShortDescription(newsResource.content) Spacer(modifier = Modifier.height(12.dp)) @@ -196,7 +196,7 @@ fun dateFormatted(publishDate: Instant): String { } @Composable -fun NewsResourceDateAndType( +fun NewsResourceMetaData( publishDate: Instant, resourceType: NewsResourceType ) {