Skip to content

Commit

Permalink
ANDROID-14597 title view heading (#350)
Browse files Browse the repository at this point in the history
* ANDROID-14597 add isTitleHeading to TitleView

* ANDROID-14597 add isTitleHeading to Title

* ANDROID-14597 add some title headings in catalog

* ANDROID-14597 add BindingMethods for databinding

* ANDROID-14597 fix typo
  • Loading branch information
jeprubio authored Apr 26, 2024
1 parent 314ac81 commit 69ba5f5
Show file tree
Hide file tree
Showing 8 changed files with 151 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ private fun DefaultTitles() {
Title(
modifier = Modifier.padding(bottom = 8.dp),
text = "Short default title",
isTitleHeading = true,
)
Title(
modifier = Modifier.padding(bottom = 8.dp),
Expand Down Expand Up @@ -59,6 +60,7 @@ private fun TitlesWithStyleOverridden(
modifier = Modifier.padding(bottom = 8.dp),
style = style,
text = "Short title $style",
isTitleHeading = true,
)
Title(
modifier = Modifier.padding(bottom = 8.dp),
Expand Down
2 changes: 2 additions & 0 deletions catalog/src/main/res/layout/title_catalog.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
app:isTitleHeading="true"
app:title="Short default Title" />

<com.telefonica.mistica.title.TitleView
Expand Down Expand Up @@ -48,6 +49,7 @@
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
app:titleStyle="title1"
app:isTitleHeading="true"
app:title="Some title 1" />

<com.telefonica.mistica.title.TitleView
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,16 @@ import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.semantics.heading
import androidx.compose.ui.semantics.semantics
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.unit.dp
import com.telefonica.mistica.compose.theme.MisticaTheme

@Composable
fun Title(
modifier: Modifier = Modifier,
isTitleHeading: Boolean = false,
style: TitleStyle = MisticaTheme.values.titleStyle,
text: String,
linkText: String? = null,
Expand All @@ -24,6 +27,13 @@ fun Title(
) {
TitleText(
modifier = Modifier
.then(
if (isTitleHeading) {
Modifier.semantics { heading() }
} else {
Modifier
}
)
.weight(1F)
.alignByBaseline(),
text = text,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ import com.telefonica.mistica.util.setAlpha
attribute = "listRowTitleMaxLines",
method = "setTitleMaxLines"
),
BindingMethod(
type = ListRowView::class,
attribute = "listRowIsTitleHeading",
method = "setTitleHeading"
),
BindingMethod(
type = ListRowView::class,
attribute = "listRowSubtitle",
Expand Down
21 changes: 21 additions & 0 deletions library/src/main/java/com/telefonica/mistica/title/TitleView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import android.widget.TextView
import androidx.annotation.AttrRes
import androidx.annotation.IntDef
import androidx.annotation.StyleRes
import androidx.annotation.VisibleForTesting
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.view.ViewCompat
import androidx.databinding.BindingMethod
import androidx.databinding.BindingMethods
import com.telefonica.mistica.R
Expand All @@ -26,6 +28,11 @@ import com.telefonica.mistica.util.setTextPreset
attribute = "titleStyle",
method = "setTitleStyle"
),
BindingMethod(
type = TitleView::class,
attribute = "isTitleHeading",
method = "setTitleHeading"
),
BindingMethod(
type = TitleView::class,
attribute = "link",
Expand Down Expand Up @@ -71,6 +78,13 @@ class TitleView @JvmOverloads constructor(
titleTextView.setTextAndVisibility(styledAttrs.getText(R.styleable.TitleView_title))
setTitleStyle(styledAttrs.getInt(R.styleable.TitleView_titleStyle, -1))

styledAttrs.getBoolean(
R.styleable.TitleView_isTitleHeading,
false
)
.takeIf { it }
?.let { setTitleHeading() }

linkTextView.setTextAndVisibility(styledAttrs.getText(R.styleable.TitleView_link))

styledAttrs.recycle()
Expand All @@ -79,6 +93,9 @@ class TitleView @JvmOverloads constructor(

fun getTitle(): String = titleTextView.text.toString()

@VisibleForTesting
internal fun getTitleTextView(): TextView = titleTextView

fun setTitle(text: CharSequence?) {
titleTextView.setTextAndVisibility(text)
}
Expand All @@ -95,6 +112,10 @@ class TitleView @JvmOverloads constructor(
titleTextView.isAllCaps = styleConfig.isAllCaps
}

fun setTitleHeading() {
ViewCompat.setAccessibilityHeading(titleTextView, true)
}

fun getLink(): String = linkTextView.text.toString()

fun setLink(text: CharSequence?) {
Expand Down
1 change: 1 addition & 0 deletions library/src/main/res/values/attrs_components.xml
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@

<declare-styleable name="TitleView">
<attr name="title" format="string" />
<attr name="isTitleHeading" format="boolean" />
<attr name="titleStyle" format="enum">
<enum name="title1" value="0" />
<enum name="title2" value="1" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.telefonica.mistica.compose.title

import androidx.compose.ui.test.assert
import androidx.compose.ui.test.isHeading
import androidx.compose.ui.test.junit4.createComposeRule
import androidx.compose.ui.test.onNodeWithText
import com.telefonica.mistica.compose.theme.MisticaTheme
import com.telefonica.mistica.compose.theme.brand.MovistarBrand
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner

@RunWith(RobolectricTestRunner::class)
internal class TitleKtTest {
@get:Rule
val composeTestRule = createComposeRule()

@Test
fun `check the title is not set with accessibility heading by default`() = test {
`given Title`()

composeTestRule.onNodeWithText(textValue).assert(!isHeading())
}

@Test
fun `check the title is set with accessibility heading when isHeading is set`() = test {
`given Title`(isHeading = true)

composeTestRule.onNodeWithText(textValue).assert(isHeading())
}

private fun TestScope.`given Title`(isHeading: Boolean = false) {
`when Title`(isHeading)
}

private fun TestScope.`when Title`(isHeading: Boolean = false) {
composeTestRule.setContent {
MisticaTheme(brand = MovistarBrand) {
Title(
text = textValue,
isTitleHeading = isHeading
)
}
}
}

private fun test(block: TestScope.() -> Unit) {
TestScope().block()
}

private class TestScope {
val textValue = "textValue"
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.telefonica.mistica.title

import android.widget.FrameLayout
import androidx.test.ext.junit.rules.activityScenarioRule
import com.telefonica.mistica.DummyActivity
import com.telefonica.mistica.R
import com.telefonica.mistica.testutils.ScreenshotsTest
import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import org.robolectric.annotation.Config

@RunWith(RobolectricTestRunner::class)
internal class TitleViewTest: ScreenshotsTest() {
@get:Rule
val rule = activityScenarioRule<DummyActivity>()

@Test
fun `check TitleView has accessibility heading when setTitleHeading is invoked`() {
checkTitleView {
setTitle("Title")
setTitleHeading()

assertTrue(this.getTitleTextView().isAccessibilityHeading)
}
}

@Test
fun `check TitleView has NOT accessibility heading when setTitleHeading is not invoked`() {
checkTitleView {
setTitle("Title")

assertFalse(this.getTitleTextView().isAccessibilityHeading)
}
}

@Config(qualifiers = "+night")
@Test
fun `check TextInput dark`() {
checkTitleView()
}

private fun checkTitleView(onTitleView: TitleView.() -> Unit = {}) {
rule.scenario.onActivity { activity ->
val wrapper: FrameLayout = activity.findViewById(R.id.dummy_activity_wrapper)
val titleView = TitleView(activity)
wrapper.addView(titleView)
titleView.onTitleView()
}
}
}

0 comments on commit 69ba5f5

Please sign in to comment.