Add test for NewsResourceCard

caren/newsresourcecardtest
Caren Chang 2 years ago
parent e0687e9aae
commit f20f49b96f

@ -20,6 +20,12 @@ plugins {
id("nowinandroid.spotless")
}
android {
defaultConfig {
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
}
}
dependencies {
implementation(project(":core-designsystem"))
implementation(project(":core-model"))
@ -47,4 +53,6 @@ dependencies {
api(libs.androidx.compose.runtime.livedata)
api(libs.androidx.metrics)
api(libs.androidx.tracing.ktx)
androidTestImplementation(project(":core-testing"))
}

@ -0,0 +1,80 @@
/*
* 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.assertHasClickAction
import androidx.compose.ui.test.assertIsDisplayed
import androidx.compose.ui.test.junit4.createComposeRule
import androidx.compose.ui.test.onNodeWithTag
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 testNewsResourceCard_displaysNewsResource() {
val newsResource = previewNewsResources[0]
var dateFormatted = ""
composeTestRule.setContent {
NewsResourceCardExpanded(
newsResource = newsResource,
isBookmarked = false,
onToggleBookmark = {},
onClick = {}
)
dateFormatted = dateFormatted(publishDate = newsResource.publishDate)
}
composeTestRule
.onNodeWithText(newsResource.title)
.assertIsDisplayed()
composeTestRule
.onNodeWithText(newsResource.authors[0].name.uppercase())
.assertIsDisplayed()
composeTestRule
.onNodeWithText(dateFormatted)
.assertIsDisplayed()
composeTestRule
.onNodeWithText(newsResource.content)
.assertIsDisplayed()
}
@Test
fun testNewsResourceCard_hasClickAction() {
val newsResource = previewNewsResources[0]
composeTestRule.setContent {
NewsResourceCardExpanded(
newsResource = newsResource,
isBookmarked = false,
onToggleBookmark = {},
onClick = { }
)
}
composeTestRule.onNodeWithTag(NewsResourceCardTestTag).assertHasClickAction()
}
}

@ -16,6 +16,7 @@
package com.google.samples.apps.nowinandroid.core.ui
import androidx.annotation.VisibleForTesting
import androidx.compose.foundation.background
import androidx.compose.foundation.horizontalScroll
import androidx.compose.foundation.layout.Arrangement
@ -50,6 +51,7 @@ import androidx.compose.ui.draw.clip
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalInspectionMode
import androidx.compose.ui.platform.testTag
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.semantics.onClick
@ -75,6 +77,8 @@ import kotlinx.datetime.toJavaInstant
* [NewsResource] card used on the following screens: For You, Episodes, Saved
*/
const val NewsResourceCardTestTag = "NewsResourceCardTestTag"
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun NewsResourceCardExpanded(
@ -91,9 +95,11 @@ fun NewsResourceCardExpanded(
colors = CardDefaults.cardColors(containerColor = MaterialTheme.colorScheme.surface),
// Use custom label for accessibility services to communicate button's action to user.
// Pass null for action to only override the label and not the actual action.
modifier = modifier.semantics {
onClick(label = clickActionLabel, action = null)
}
modifier = modifier
.semantics {
onClick(label = clickActionLabel, action = null)
}
.testTag(NewsResourceCardTestTag)
) {
Column {
if (!newsResource.headerImageUrl.isNullOrEmpty()) {
@ -223,7 +229,8 @@ fun BookmarkButton(
}
@Composable
private fun dateFormatted(publishDate: Instant): String {
@VisibleForTesting
fun dateFormatted(publishDate: Instant): String {
var zoneId by remember { mutableStateOf(ZoneId.systemDefault()) }
val context = LocalContext.current

Loading…
Cancel
Save