Use SP for fontSize
Test: ./gradlew ui-text:text
Test: ./gradlew ui-text:connectedAndroidTest
Test: ./gradlew ui-framework:test
Test: ./gradlew ui-framework:connectedAndroidTest
Test: ./gradlew ui-material:test
Test: ./gradlew ui-material:connectedAndroidTest
Test: ./gradlew ui-animation:test
Test: ./gradlew ui-animation:connectedAndroidTest
Test: ./gradlew ui-layout:test
Test: ./gradlew ui-layout:connectedAndroidTest
Test: Manual on demo app
Bug: 127345041
Change-Id: Iac6c9f5915fd557f1e80c7a3577b42db42539baa
diff --git a/ui/text/src/androidTest/java/androidx/ui/engine/text/ParagraphIntegrationTest.kt b/ui/text/src/androidTest/java/androidx/ui/engine/text/ParagraphIntegrationTest.kt
index 66d5701..7bdb728 100644
--- a/ui/text/src/androidTest/java/androidx/ui/engine/text/ParagraphIntegrationTest.kt
+++ b/ui/text/src/androidTest/java/androidx/ui/engine/text/ParagraphIntegrationTest.kt
@@ -19,7 +19,11 @@
import androidx.test.filters.SdkSuppress
import androidx.test.filters.SmallTest
import androidx.test.platform.app.InstrumentationRegistry
+import androidx.ui.core.Density
+import androidx.ui.core.Sp
import androidx.ui.core.px
+import androidx.ui.core.sp
+import androidx.ui.core.withDensity
import androidx.ui.engine.geometry.Offset
import androidx.ui.engine.geometry.Rect
import androidx.ui.engine.text.FontTestData.Companion.BASIC_KERN_FONT
@@ -53,6 +57,7 @@
private lateinit var fontFamilyKernFont: FontFamily
private lateinit var fontFamilyCustom100: FontFamily
private lateinit var fontFamilyCustom200: FontFamily
+ private val defaultDensity = Density(density = 1f)
@Before
fun setup() {
@@ -76,324 +81,408 @@
@Test
fun empty_string() {
- val fontSize = 50.0f
- val text = ""
- val paragraph = simpleParagraph(text = text, fontSize = fontSize)
+ withDensity(defaultDensity) {
+ val fontSize = 50.sp
+ val fontSizeInPx = fontSize.toPx().value
+ val text = ""
+ val paragraph = simpleParagraph(text = text, fontSize = fontSize)
- paragraph.layout(ParagraphConstraints(width = 100.0f))
+ paragraph.layout(ParagraphConstraints(width = 100.0f))
- assertThat(paragraph.width, equalTo(100.0f))
- assertThat(paragraph.height, equalTo(fontSize))
- // defined in sample_font
- assertThat(paragraph.baseline, equalTo(fontSize * 0.8f))
- assertThat(paragraph.maxIntrinsicWidth, equalTo(0.0f))
- assertThat(paragraph.minIntrinsicWidth, equalTo(0.0f))
- // TODO(Migration/siyamed): no baseline query per line?
- // TODO(Migration/siyamed): no line count?
+ assertThat(paragraph.width, equalTo(100.0f))
+
+ assertThat(paragraph.height, equalTo(fontSizeInPx))
+ // defined in sample_font
+ assertThat(paragraph.baseline, equalTo(fontSizeInPx * 0.8f))
+ assertThat(paragraph.maxIntrinsicWidth, equalTo(0.0f))
+ assertThat(paragraph.minIntrinsicWidth, equalTo(0.0f))
+ // TODO(Migration/siyamed): no baseline query per line?
+ // TODO(Migration/siyamed): no line count?
+ }
}
@Test
fun single_line_default_values() {
- val fontSize = 50.0f
- for (text in arrayOf("xyz", "\u05D0\u05D1\u05D2")) {
- val paragraph = simpleParagraph(text = text, fontSize = fontSize)
+ withDensity(defaultDensity) {
+ val fontSize = 50.sp
+ val fontSizeInpx = fontSize.toPx().value
- // width greater than text width - 150
- paragraph.layout(ParagraphConstraints(width = 200.0f))
+ for (text in arrayOf("xyz", "\u05D0\u05D1\u05D2")) {
+ val paragraph = simpleParagraph(text = text, fontSize = fontSize)
- assertThat(text, paragraph.width, equalTo(200.0f))
- assertThat(text, paragraph.height, equalTo(fontSize))
- // defined in sample_font
- assertThat(text, paragraph.baseline, equalTo(fontSize * 0.8f))
- assertThat(text, paragraph.maxIntrinsicWidth, equalTo(fontSize * text.length))
- assertThat(text, paragraph.minIntrinsicWidth, equalTo(0.0f))
+ // width greater than text width - 150
+ paragraph.layout(ParagraphConstraints(width = 200.0f))
+
+ assertThat(text, paragraph.width, equalTo(200.0f))
+ assertThat(text, paragraph.height, equalTo(fontSizeInpx))
+ // defined in sample_font
+ assertThat(text, paragraph.baseline, equalTo(fontSizeInpx * 0.8f))
+ assertThat(
+ text,
+ paragraph.maxIntrinsicWidth,
+ equalTo(fontSizeInpx * text.length)
+ )
+ assertThat(text, paragraph.minIntrinsicWidth, equalTo(0.0f))
+ }
}
}
@Test
fun line_break_default_values() {
- val fontSize = 50.0f
- for (text in arrayOf("abcdef", "\u05D0\u05D1\u05D2\u05D3\u05D4\u05D5")) {
- val paragraph = simpleParagraph(text = text, fontSize = fontSize)
+ withDensity(defaultDensity) {
+ val fontSize = 50.sp
+ val fontSizeInPx = fontSize.toPx().value
- // 3 chars width
- paragraph.layout(ParagraphConstraints(width = 3 * fontSize))
+ for (text in arrayOf("abcdef", "\u05D0\u05D1\u05D2\u05D3\u05D4\u05D5")) {
+ val paragraph = simpleParagraph(text = text, fontSize = fontSize)
- // 3 chars
- assertThat(text, paragraph.width, equalTo(3 * fontSize))
- // 2 lines, 1 line gap
- assertThat(text, paragraph.height, equalTo(2 * fontSize + fontSize / 5.0f))
- // defined in sample_font
- assertThat(text, paragraph.baseline, equalTo(fontSize * 0.8f))
- assertThat(text, paragraph.maxIntrinsicWidth, equalTo(fontSize * text.length))
- assertThat(text, paragraph.minIntrinsicWidth, equalTo(0.0f))
+ // 3 chars width
+ paragraph.layout(ParagraphConstraints(width = 3 * fontSizeInPx))
+
+ // 3 chars
+ assertThat(text, paragraph.width, equalTo(3 * fontSizeInPx))
+ // 2 lines, 1 line gap
+ assertThat(
+ text,
+ paragraph.height,
+ equalTo(2 * fontSizeInPx + fontSizeInPx / 5.0f)
+ )
+ // defined in sample_font
+ assertThat(text, paragraph.baseline, equalTo(fontSizeInPx * 0.8f))
+ assertThat(
+ text,
+ paragraph.maxIntrinsicWidth,
+ equalTo(fontSizeInPx * text.length)
+ )
+ assertThat(text, paragraph.minIntrinsicWidth, equalTo(0.0f))
+ }
}
}
@Test
fun newline_default_values() {
- val fontSize = 50.0f
- for (text in arrayOf("abc\ndef", "\u05D0\u05D1\u05D2\n\u05D3\u05D4\u05D5")) {
- val paragraph = simpleParagraph(text = text, fontSize = fontSize)
+ withDensity(defaultDensity) {
+ val fontSize = 50.sp
+ val fontSizeInpx = fontSize.toPx().value
- // 3 chars width
- paragraph.layout(ParagraphConstraints(width = 3 * fontSize))
+ for (text in arrayOf("abc\ndef", "\u05D0\u05D1\u05D2\n\u05D3\u05D4\u05D5")) {
+ val paragraph = simpleParagraph(text = text, fontSize = fontSize)
- // 3 chars
- assertThat(text, paragraph.width, equalTo(3 * fontSize))
- // 2 lines, 1 line gap
- assertThat(text, paragraph.height, equalTo(2 * fontSize + fontSize / 5.0f))
- // defined in sample_font
- assertThat(text, paragraph.baseline, equalTo(fontSize * 0.8f))
- assertThat(text, paragraph.maxIntrinsicWidth, equalTo(fontSize * text.indexOf("\n")))
- assertThat(text, paragraph.minIntrinsicWidth, equalTo(0.0f))
+ // 3 chars width
+ paragraph.layout(ParagraphConstraints(width = 3 * fontSizeInpx))
+
+ // 3 chars
+
+ assertThat(text, paragraph.width, equalTo(3 * fontSizeInpx))
+ // 2 lines, 1 line gap
+ assertThat(
+ text,
+ paragraph.height,
+ equalTo(2 * fontSizeInpx + fontSizeInpx / 5.0f)
+ )
+ // defined in sample_font
+ assertThat(text, paragraph.baseline, equalTo(fontSizeInpx * 0.8f))
+ assertThat(
+ text,
+ paragraph.maxIntrinsicWidth,
+ equalTo(fontSizeInpx * text.indexOf("\n"))
+ )
+ assertThat(text, paragraph.minIntrinsicWidth, equalTo(0.0f))
+ }
}
}
@Test
fun newline_and_line_break_default_values() {
- val fontSize = 50.0f
- for (text in arrayOf("abc\ndef", "\u05D0\u05D1\u05D2\n\u05D3\u05D4\u05D5")) {
- val paragraph = simpleParagraph(text = text, fontSize = fontSize)
+ withDensity(defaultDensity) {
+ val fontSize = 50.sp
+ val fontSizeInPx = fontSize.toPx().value
- // 2 chars width
- paragraph.layout(ParagraphConstraints(width = 2 * fontSize))
+ for (text in arrayOf("abc\ndef", "\u05D0\u05D1\u05D2\n\u05D3\u05D4\u05D5")) {
+ val paragraph = simpleParagraph(text = text, fontSize = fontSize)
- // 2 chars
- assertThat(text, paragraph.width, equalTo(2 * fontSize))
- // 4 lines, 3 line gaps
- assertThat(text, paragraph.height, equalTo(4 * fontSize + 3 * fontSize / 5.0f))
- // defined in sample_font
- assertThat(text, paragraph.baseline, equalTo(fontSize * 0.8f))
- assertThat(text, paragraph.maxIntrinsicWidth, equalTo(fontSize * text.indexOf("\n")))
- assertThat(text, paragraph.minIntrinsicWidth, equalTo(0.0f))
+ // 2 chars width
+
+ paragraph.layout(ParagraphConstraints(width = 2 * fontSizeInPx))
+
+ // 2 chars
+ assertThat(text, paragraph.width, equalTo(2 * fontSizeInPx))
+ // 4 lines, 3 line gaps
+ assertThat(
+ text,
+ paragraph.height,
+ equalTo(4 * fontSizeInPx + 3 * fontSizeInPx / 5.0f)
+ )
+ // defined in sample_font
+ assertThat(text, paragraph.baseline, equalTo(fontSizeInPx * 0.8f))
+ assertThat(
+ text,
+ paragraph.maxIntrinsicWidth,
+ equalTo(fontSizeInPx * text.indexOf("\n"))
+ )
+ assertThat(text, paragraph.minIntrinsicWidth, equalTo(0.0f))
+ }
}
}
@Test
fun getPositionForOffset_ltr() {
- val text = "abc"
- val fontSize = 50.0f
- val paragraph = simpleParagraph(text = text, fontSize = fontSize)
+ withDensity(defaultDensity) {
+ val text = "abc"
+ val fontSize = 50.sp
+ val fontSizeInPx = fontSize.toPx().value
+ val paragraph = simpleParagraph(text = text, fontSize = fontSize)
- paragraph.layout(ParagraphConstraints(width = text.length * fontSize))
- // test positions that are 1, fontSize+1, 2fontSize+1 which maps to chars 0, 1, 2 ...
- for (i in 0..text.length) {
- val offset = Offset(i * fontSize + 1, fontSize / 2)
- val position = paragraph.getPositionForOffset(offset)
- assertThat(
- "position at index $i, offset $offset does not match",
- position,
- equalTo(i)
- )
+ paragraph.layout(ParagraphConstraints(width = text.length * fontSizeInPx))
+ // test positions that are 1, fontSize+1, 2fontSize+1 which maps to chars 0, 1, 2 ...
+ for (i in 0..text.length) {
+ val offset = Offset(i * fontSizeInPx + 1, fontSizeInPx / 2)
+ val position = paragraph.getPositionForOffset(offset)
+ assertThat(
+ "position at index $i, offset $offset does not match",
+ position,
+ equalTo(i)
+ )
+ }
}
}
@Test
fun getPositionForOffset_rtl() {
- val text = "\u05D0\u05D1\u05D2"
- val fontSize = 50.0f
- val paragraph = simpleParagraph(text = text, fontSize = fontSize)
+ withDensity(defaultDensity) {
+ val text = "\u05D0\u05D1\u05D2"
+ val fontSize = 50.sp
+ val fontSizeInPx = fontSize.toPx().value
+ val paragraph = simpleParagraph(text = text, fontSize = fontSize)
- paragraph.layout(ParagraphConstraints(width = text.length * fontSize))
+ paragraph.layout(ParagraphConstraints(width = text.length * fontSizeInPx))
- // test positions that are 1, fontSize+1, 2fontSize+1 which maps to chars .., 2, 1, 0
- for (i in 0..text.length) {
- val offset = Offset(i * fontSize + 1, fontSize / 2)
- val position = paragraph.getPositionForOffset(offset)
- assertThat(
- "position at index $i, offset $offset does not match",
- position,
- equalTo(text.length - i)
- )
+ // test positions that are 1, fontSize+1, 2fontSize+1 which maps to chars .., 2, 1, 0
+ for (i in 0..text.length) {
+ val offset = Offset(i * fontSizeInPx + 1, fontSizeInPx / 2)
+ val position = paragraph.getPositionForOffset(offset)
+ assertThat(
+ "position at index $i, offset $offset does not match",
+ position,
+ equalTo(text.length - i)
+ )
+ }
}
}
@Test
fun getPositionForOffset_ltr_multiline() {
- val firstLine = "abc"
- val secondLine = "def"
- val text = firstLine + secondLine
- val fontSize = 50.0f
- val paragraph = simpleParagraph(text = text, fontSize = fontSize)
+ withDensity(defaultDensity) {
+ val firstLine = "abc"
+ val secondLine = "def"
+ val text = firstLine + secondLine
+ val fontSize = 50.sp
+ val fontSizeInPx = fontSize.toPx().value
+ val paragraph = simpleParagraph(text = text, fontSize = fontSize)
- paragraph.layout(ParagraphConstraints(width = firstLine.length * fontSize))
+ paragraph.layout(ParagraphConstraints(width = firstLine.length * fontSizeInPx))
- // test positions are 1, fontSize+1, 2fontSize+1 and always on the second line
- // which maps to chars 3, 4, 5
- for (i in 0..secondLine.length) {
- val offset = Offset(i * fontSize + 1, fontSize * 1.5f)
- val position = paragraph.getPositionForOffset(offset)
- assertThat(
- "position at index $i, offset $offset, second line does not match",
- position,
- equalTo(i + firstLine.length)
- )
+ // test positions are 1, fontSize+1, 2fontSize+1 and always on the second line
+ // which maps to chars 3, 4, 5
+ for (i in 0..secondLine.length) {
+ val offset = Offset(i * fontSizeInPx + 1, fontSizeInPx * 1.5f)
+ val position = paragraph.getPositionForOffset(offset)
+ assertThat(
+ "position at index $i, offset $offset, second line does not match",
+ position,
+ equalTo(i + firstLine.length)
+ )
+ }
}
}
@Test
fun getPositionForOffset_rtl_multiline() {
- val firstLine = "\u05D0\u05D1\u05D2"
- val secondLine = "\u05D3\u05D4\u05D5"
- val text = firstLine + secondLine
- val fontSize = 50.0f
- val paragraph = simpleParagraph(text = text, fontSize = fontSize)
+ withDensity(defaultDensity) {
+ val firstLine = "\u05D0\u05D1\u05D2"
+ val secondLine = "\u05D3\u05D4\u05D5"
+ val text = firstLine + secondLine
+ val fontSize = 50.sp
+ val fontSizeInPx = fontSize.toPx().value
+ val paragraph = simpleParagraph(text = text, fontSize = fontSize)
- paragraph.layout(ParagraphConstraints(width = firstLine.length * fontSize))
+ paragraph.layout(ParagraphConstraints(width = firstLine.length * fontSizeInPx))
- // test positions are 1, fontSize+1, 2fontSize+1 and always on the second line
- // which maps to chars 5, 4, 3
- for (i in 0..secondLine.length) {
- val offset = Offset(i * fontSize + 1, fontSize * 1.5f)
- val position = paragraph.getPositionForOffset(offset)
- assertThat(
- "position at index $i, offset $offset, second line does not match",
- position,
- equalTo(text.length - i)
- )
+ // test positions are 1, fontSize+1, 2fontSize+1 and always on the second line
+ // which maps to chars 5, 4, 3
+ for (i in 0..secondLine.length) {
+ val offset = Offset(i * fontSizeInPx + 1, fontSizeInPx * 1.5f)
+ val position = paragraph.getPositionForOffset(offset)
+ assertThat(
+ "position at index $i, offset $offset, second line does not match",
+ position,
+ equalTo(text.length - i)
+ )
+ }
}
}
@Test
fun getPositionForOffset_ltr_width_outOfBounds() {
- val text = "abc"
- val fontSize = 50.0f
- val paragraph = simpleParagraph(text = text, fontSize = fontSize)
+ withDensity(defaultDensity) {
+ val text = "abc"
+ val fontSize = 50.sp
+ val fontSizeInPx = fontSize.toPx().value
+ val paragraph = simpleParagraph(text = text, fontSize = fontSize)
- paragraph.layout(ParagraphConstraints(width = text.length * fontSize))
+ paragraph.layout(ParagraphConstraints(width = text.length * fontSizeInPx))
- // greater than width
- var offset = Offset(fontSize * text.length * 2, fontSize / 2)
- var position = paragraph.getPositionForOffset(offset)
- assertThat(position, equalTo(text.length))
+ // greater than width
+ var offset = Offset(fontSizeInPx * text.length * 2, fontSizeInPx / 2)
+ var position = paragraph.getPositionForOffset(offset)
+ assertThat(position, equalTo(text.length))
- // negative
- offset = Offset(-1 * fontSize, fontSize / 2)
- position = paragraph.getPositionForOffset(offset)
- assertThat(position, equalTo(0))
+ // negative
+ offset = Offset(-1 * fontSizeInPx, fontSizeInPx / 2)
+ position = paragraph.getPositionForOffset(offset)
+ assertThat(position, equalTo(0))
+ }
}
@Test
fun getPositionForOffset_ltr_height_outOfBounds() {
- val text = "abc"
- val fontSize = 50.0f
- val paragraph = simpleParagraph(text = text, fontSize = fontSize)
+ withDensity(defaultDensity) {
+ val text = "abc"
+ val fontSize = 50.sp
+ val fontSizeInPx = fontSize.toPx().value
+ val paragraph = simpleParagraph(text = text, fontSize = fontSize)
- paragraph.layout(ParagraphConstraints(width = text.length * fontSize))
+ paragraph.layout(ParagraphConstraints(width = text.length * fontSizeInPx))
- // greater than height
- var offset = Offset(fontSize / 2, fontSize * text.length * 2)
- var position = paragraph.getPositionForOffset(offset)
- assertThat(position, equalTo(0))
+ // greater than height
+ var offset = Offset(fontSizeInPx / 2, fontSizeInPx * text.length * 2)
+ var position = paragraph.getPositionForOffset(offset)
+ assertThat(position, equalTo(0))
- // negative
- offset = Offset(fontSize / 2, -1 * fontSize)
- position = paragraph.getPositionForOffset(offset)
- assertThat(position, equalTo(0))
+ // negative
+ offset = Offset(fontSizeInPx / 2, -1 * fontSizeInPx)
+ position = paragraph.getPositionForOffset(offset)
+ assertThat(position, equalTo(0))
+ }
}
@Test
fun getBoundingBoxForTextPosition_ltr_singleLine() {
- val text = "abc"
- val fontSize = 50.0f
- val paragraph = simpleParagraph(text = text, fontSize = fontSize)
+ withDensity(defaultDensity) {
+ val text = "abc"
+ val fontSize = 50.sp
+ val fontSizeInPx = fontSize.toPx().value
+ val paragraph = simpleParagraph(text = text, fontSize = fontSize)
- paragraph.layout(ParagraphConstraints(width = text.length * fontSize))
- // test positions that are 0, 1, 2 ... which maps to chars 0, 1, 2 ...
- for (i in 0..text.length - 1) {
- val box = paragraph.getBoundingBoxForTextPosition(i)
- assertThat(box.left, equalTo(i * fontSize))
- assertThat(box.right, equalTo((i + 1) * fontSize))
- assertThat(box.top, equalTo(0f))
- assertThat(box.bottom, equalTo(fontSize))
+ paragraph.layout(ParagraphConstraints(width = text.length * fontSizeInPx))
+ // test positions that are 0, 1, 2 ... which maps to chars 0, 1, 2 ...
+ for (i in 0..text.length - 1) {
+ val box = paragraph.getBoundingBoxForTextPosition(i)
+ assertThat(box.left, equalTo(i * fontSizeInPx))
+ assertThat(box.right, equalTo((i + 1) * fontSizeInPx))
+ assertThat(box.top, equalTo(0f))
+ assertThat(box.bottom, equalTo(fontSizeInPx))
+ }
}
}
@Test
fun getBoundingBoxForTextPosition_ltr_multiLines() {
- val firstLine = "abc"
- val secondLine = "def"
- val text = firstLine + secondLine
- val fontSize = 50.0f
- val paragraph = simpleParagraph(text = text, fontSize = fontSize)
+ withDensity(defaultDensity) {
+ val firstLine = "abc"
+ val secondLine = "def"
+ val text = firstLine + secondLine
+ val fontSize = 50.sp
+ val fontSizeInPx = fontSize.toPx().value
+ val paragraph = simpleParagraph(text = text, fontSize = fontSize)
- paragraph.layout(ParagraphConstraints(width = firstLine.length * fontSize))
+ paragraph.layout(ParagraphConstraints(width = firstLine.length * fontSizeInPx))
- // test positions are 3, 4, 5 and always on the second line
- // which maps to chars 3, 4, 5
- for (i in 0..secondLine.length - 1) {
- val textPosition = i + firstLine.length
- val box = paragraph.getBoundingBoxForTextPosition(textPosition)
- assertThat(box.left, equalTo(i * fontSize))
- assertThat(box.right, equalTo((i + 1) * fontSize))
- assertThat(box.top, equalTo(fontSize))
- assertThat(box.bottom, equalTo((2f + 1 / 5f) * fontSize))
+ // test positions are 3, 4, 5 and always on the second line
+ // which maps to chars 3, 4, 5
+ for (i in 0..secondLine.length - 1) {
+ val textPosition = i + firstLine.length
+ val box = paragraph.getBoundingBoxForTextPosition(textPosition)
+ assertThat(box.left, equalTo(i * fontSizeInPx))
+ assertThat(box.right, equalTo((i + 1) * fontSizeInPx))
+ assertThat(box.top, equalTo(fontSizeInPx))
+ assertThat(box.bottom, equalTo((2f + 1 / 5f) * fontSizeInPx))
+ }
}
}
@Test
fun getBoundingBoxForTextPosition_ltr_textPosition_negative() {
- val text = "abc"
- val fontSize = 50.0f
- val paragraph = simpleParagraph(text = text, fontSize = fontSize)
+ withDensity(defaultDensity) {
+ val text = "abc"
+ val fontSize = 50.sp
+ val fontSizeInPx = fontSize.toPx().value
+ val paragraph = simpleParagraph(text = text, fontSize = fontSize)
- paragraph.layout(ParagraphConstraints(width = text.length * fontSize))
+ paragraph.layout(ParagraphConstraints(width = text.length * fontSizeInPx))
- val textPosition = -1
- val box = paragraph.getBoundingBoxForTextPosition(textPosition)
- assertThat(box.left, equalTo(0f))
- assertThat(box.right, equalTo(0f))
- assertThat(box.top, equalTo(0f))
- assertThat(box.bottom, equalTo(fontSize))
+ val textPosition = -1
+ val box = paragraph.getBoundingBoxForTextPosition(textPosition)
+ assertThat(box.left, equalTo(0f))
+ assertThat(box.right, equalTo(0f))
+ assertThat(box.top, equalTo(0f))
+ assertThat(box.bottom, equalTo(fontSizeInPx))
+ }
}
@Test(expected = java.lang.IndexOutOfBoundsException::class)
fun getBoundingBoxForTextPosition_ltr_textPosition_larger_than_length_throw_exception() {
- val text = "abc"
- val fontSize = 50.0f
- val paragraph = simpleParagraph(text = text, fontSize = fontSize)
+ withDensity(defaultDensity) {
+ val text = "abc"
+ val fontSize = 50.sp
+ val fontSizeInPx = fontSize.toPx().value
+ val paragraph = simpleParagraph(text = text, fontSize = fontSize)
- paragraph.layout(ParagraphConstraints(width = text.length * fontSize))
+ paragraph.layout(ParagraphConstraints(width = text.length * fontSizeInPx))
- val textPosition = text.length + 1
- paragraph.getBoundingBoxForTextPosition(textPosition)
+ val textPosition = text.length + 1
+ paragraph.getBoundingBoxForTextPosition(textPosition)
+ }
}
@Test
fun locale_withCJK_shouldNotDrawSame() {
- val text = "\u82B1"
- val fontSize = 10.0f
- val locales = arrayOf(
- // duplicate ja is on purpose
- Locale(_languageCode = "ja"),
- Locale(_languageCode = "ja"),
- Locale(_languageCode = "zh", _countryCode = "CN"),
- Locale(_languageCode = "zh", _countryCode = "TW")
- )
-
- val bitmaps = locales.map { locale ->
- val paragraph = Paragraph(
- text = text,
- textStyles = listOf(),
- style = TextStyle(
- fontSize = fontSize,
- locale = locale
- ),
- paragraphStyle = ParagraphStyle()
+ withDensity(defaultDensity) {
+ val text = "\u82B1"
+ val fontSize = 10.sp
+ val fontSizeInPx = fontSize.toPx().value
+ val locales = arrayOf(
+ // duplicate ja is on purpose
+ Locale(_languageCode = "ja"),
+ Locale(_languageCode = "ja"),
+ Locale(_languageCode = "zh", _countryCode = "CN"),
+ Locale(_languageCode = "zh", _countryCode = "TW")
)
- // just have 10x font size to have a bitmap
- paragraph.layout(ParagraphConstraints(width = fontSize * 10))
+ val bitmaps = locales.map { locale ->
+ val paragraph = Paragraph(
+ text = text,
+ textStyles = listOf(),
+ style = TextStyle(
+ fontSize = fontSize,
+ locale = locale
+ ),
+ paragraphStyle = ParagraphStyle(),
+ density = defaultDensity
+ )
- paragraph.bitmap()
+ // just have 10x font size to have a bitmap
+ paragraph.layout(ParagraphConstraints(width = fontSizeInPx * 10))
+
+ paragraph.bitmap()
+ }
+
+ assertThat(bitmaps[0], equalToBitmap(bitmaps[1]))
+ assertThat(bitmaps[1], not(equalToBitmap(bitmaps[2])))
+ assertThat(bitmaps[1], not(equalToBitmap(bitmaps[3])))
+ // this does not work on API 21
+ // assertThat(bitmaps[2], not(equalToBitmap(bitmaps[3])))
}
-
- assertThat(bitmaps[0], equalToBitmap(bitmaps[1]))
- assertThat(bitmaps[1], not(equalToBitmap(bitmaps[2])))
- assertThat(bitmaps[1], not(equalToBitmap(bitmaps[3])))
- // this does not work on API 21
- // assertThat(bitmaps[2], not(equalToBitmap(bitmaps[3])))
}
@Test
@@ -445,11 +534,10 @@
@Test
fun maxLines_withMaxLineEqualsZero() {
val text = "a\na\na"
- val fontSize = 100.0f
val maxLines = 0
val paragraph = simpleParagraph(
text = text,
- fontSize = fontSize,
+ fontSize = 100.sp,
maxLines = maxLines
)
paragraph.layout(ParagraphConstraints(width = Float.MAX_VALUE))
@@ -459,11 +547,10 @@
@Test(expected = java.lang.IllegalArgumentException::class)
fun maxLines_withMaxLineNegative_throwsException() {
val text = "a\na\na"
- val fontSize = 100.0f
val maxLines = -1
val paragraph = simpleParagraph(
text = text,
- fontSize = fontSize,
+ fontSize = 100.sp,
maxLines = maxLines
)
paragraph.layout(ParagraphConstraints(width = Float.MAX_VALUE))
@@ -471,49 +558,58 @@
@Test
fun maxLines_withMaxLineSmallerThanTextLines_clipHeight() {
- val text = "a\na\na"
- val fontSize = 100.0f
- val lineCount = text.lines().size
- val maxLines = lineCount
- val paragraph = simpleParagraph(
- text = text,
- fontSize = fontSize,
- maxLines = maxLines
- )
- paragraph.layout(ParagraphConstraints(width = Float.MAX_VALUE))
- val expectHeight = (lineCount + (lineCount - 1) * 0.2f) * fontSize
- assertThat(paragraph.height, equalTo(expectHeight))
+ withDensity(defaultDensity) {
+ val text = "a\na\na"
+ val fontSize = 100.sp
+ val fontSizeInPx = fontSize.toPx().value
+ val lineCount = text.lines().size
+ val maxLines = lineCount
+ val paragraph = simpleParagraph(
+ text = text,
+ fontSize = fontSize,
+ maxLines = maxLines
+ )
+ paragraph.layout(ParagraphConstraints(width = Float.MAX_VALUE))
+ val expectHeight = (lineCount + (lineCount - 1) * 0.2f) * fontSizeInPx
+ assertThat(paragraph.height, equalTo(expectHeight))
+ }
}
@Test
fun maxLines_withMaxLineEqualsTextLine() {
- val text = "a\na\na"
- val fontSize = 100.0f
- val maxLines = text.lines().size
- val paragraph = simpleParagraph(
- text = text,
- fontSize = fontSize,
- maxLines = maxLines
- )
- paragraph.layout(ParagraphConstraints(width = Float.MAX_VALUE))
- val expectHeight = (maxLines + (maxLines - 1) * 0.2f) * fontSize
- assertThat(paragraph.height, equalTo(expectHeight))
+ withDensity(defaultDensity) {
+ val text = "a\na\na"
+ val fontSize = 100.sp
+ val fontSizeInPx = fontSize.toPx().value
+ val maxLines = text.lines().size
+ val paragraph = simpleParagraph(
+ text = text,
+ fontSize = fontSize,
+ maxLines = maxLines
+ )
+ paragraph.layout(ParagraphConstraints(width = Float.MAX_VALUE))
+ val expectHeight = (maxLines + (maxLines - 1) * 0.2f) * fontSizeInPx
+ assertThat(paragraph.height, equalTo(expectHeight))
+ }
}
@Test
fun maxLines_withMaxLineGreaterThanTextLines() {
- val text = "a\na\na"
- val fontSize = 100.0f
- val lineCount = text.lines().size
- val maxLines = lineCount + 1
- val paragraph = simpleParagraph(
- text = text,
- fontSize = fontSize,
- maxLines = maxLines
- )
- paragraph.layout(ParagraphConstraints(width = 200f))
- val expectHeight = (lineCount + (lineCount - 1) * 0.2f) * fontSize
- assertThat(paragraph.height, equalTo(expectHeight))
+ withDensity(defaultDensity) {
+ val text = "a\na\na"
+ val fontSize = 100.sp
+ val fontSizeInPx = fontSize.toPx().value
+ val lineCount = text.lines().size
+ val maxLines = lineCount + 1
+ val paragraph = simpleParagraph(
+ text = text,
+ fontSize = fontSize,
+ maxLines = maxLines
+ )
+ paragraph.layout(ParagraphConstraints(width = 200f))
+ val expectHeight = (lineCount + (lineCount - 1) * 0.2f) * fontSizeInPx
+ assertThat(paragraph.height, equalTo(expectHeight))
+ }
}
@Test
@@ -548,22 +644,24 @@
@Test
fun didExceedMaxLines_withMaxLinesSmallerThanTextLines_withLineWrap_returnsTrue() {
- val text = "aa"
- val fontSize = 50.0f
- val maxLines = 1
- val paragraph = simpleParagraph(text = text, fontSize = fontSize, maxLines = maxLines)
+ withDensity(defaultDensity) {
+ val text = "aa"
+ val fontSize = 50.sp
+ val fontSizeInPx = fontSize.toPx().value
+ val maxLines = 1
+ val paragraph = simpleParagraph(text = text, fontSize = fontSize, maxLines = maxLines)
- // One line can only contain 1 character
- paragraph.layout(ParagraphConstraints(width = fontSize))
- assertThat(paragraph.didExceedMaxLines, equalTo(true))
+ // One line can only contain 1 character
+ paragraph.layout(ParagraphConstraints(width = fontSizeInPx))
+ assertThat(paragraph.didExceedMaxLines, equalTo(true))
+ }
}
@Test
fun didExceedMaxLines_withMaxLinesEqualToTextLines_withLineWrap_returnsFalse() {
val text = "a"
- val fontSize = 50.0f
val maxLines = text.lines().size
- val paragraph = simpleParagraph(text = text, fontSize = fontSize, maxLines = maxLines)
+ val paragraph = simpleParagraph(text = text, fontSize = 50.sp, maxLines = maxLines)
paragraph.layout(ParagraphConstraints(width = Float.MAX_VALUE))
assertThat(paragraph.didExceedMaxLines, equalTo(false))
@@ -571,53 +669,131 @@
@Test
fun didExceedMaxLines_withMaxLinesGreaterThanTextLines_withLineWrap_returnsFalse() {
- val text = "aa"
- val maxLines = 3
- val fontSize = 50.0f
- val paragraph = simpleParagraph(text = text, fontSize = fontSize, maxLines = maxLines)
+ withDensity(defaultDensity) {
+ val text = "aa"
+ val maxLines = 3
+ val fontSize = 50.sp
+ val fontSizeInPx = fontSize.toPx().value
+ val paragraph = simpleParagraph(text = text, fontSize = fontSize, maxLines = maxLines)
- // One line can only contain 1 character
- paragraph.layout(ParagraphConstraints(width = fontSize))
- assertThat(paragraph.didExceedMaxLines, equalTo(false))
+ // One line can only contain 1 character
+ paragraph.layout(ParagraphConstraints(width = fontSizeInPx))
+ assertThat(paragraph.didExceedMaxLines, equalTo(false))
+ }
}
@Test
fun textAlign_defaultValue_alignsStart() {
- val textLTR = "aa"
- val textRTL = "\u05D0\u05D0"
- val fontSize = 20.0f
+ withDensity(defaultDensity) {
+ val textLTR = "aa"
+ val textRTL = "\u05D0\u05D0"
+ val fontSize = 20.sp
+ val fontSizeInPx = fontSize.toPx().value
- val paragraphLTR = simpleParagraph(
- text = textLTR,
- fontSize = fontSize
- )
- val layoutLTRWidth = (textLTR.length + 2) * fontSize
- paragraphLTR.layout(ParagraphConstraints(width = layoutLTRWidth))
+ val paragraphLTR = simpleParagraph(
+ text = textLTR,
+ fontSize = fontSize
+ )
+ val layoutLTRWidth = (textLTR.length + 2) * fontSizeInPx
+ paragraphLTR.layout(ParagraphConstraints(width = layoutLTRWidth))
- val paragraphRTL = simpleParagraph(
- text = textRTL,
- fontSize = fontSize
- )
- val layoutRTLWidth = (textRTL.length + 2) * fontSize
- paragraphRTL.layout(ParagraphConstraints(width = layoutRTLWidth))
+ val paragraphRTL = simpleParagraph(
+ text = textRTL,
+ fontSize = fontSize
+ )
+ val layoutRTLWidth = (textRTL.length + 2) * fontSizeInPx
+ paragraphRTL.layout(ParagraphConstraints(width = layoutRTLWidth))
- // When textAlign is TextAlign.start, LTR aligns to left, RTL aligns to right.
- assertThat(paragraphLTR.paragraphImpl.getLineLeft(0), equalTo(0.0f))
- assertThat(paragraphRTL.paragraphImpl.getLineRight(0), equalTo(layoutRTLWidth))
+ // When textAlign is TextAlign.start, LTR aligns to left, RTL aligns to right.
+ assertThat(paragraphLTR.paragraphImpl.getLineLeft(0), equalTo(0.0f))
+ assertThat(paragraphRTL.paragraphImpl.getLineRight(0), equalTo(layoutRTLWidth))
+ }
}
@Test
fun textAlign_whenAlignLeft_returnsZeroForGetLineLeft() {
- val texts = listOf("aa", "\u05D0\u05D0")
- val fontSize = 20.0f
+ withDensity(defaultDensity) {
+ val texts = listOf("aa", "\u05D0\u05D0")
+ val fontSize = 20.sp
+ val fontSizeInPx = fontSize.toPx().value
- texts.map { text ->
+ texts.map { text ->
+ val paragraph = simpleParagraph(
+ text = text,
+ textAlign = TextAlign.Left,
+ fontSize = fontSize
+ )
+ val layoutWidth = (text.length + 2) * fontSizeInPx
+ paragraph.layout(ParagraphConstraints(width = layoutWidth))
+ val paragraphImpl = paragraph.paragraphImpl
+ assertThat(paragraphImpl.getLineLeft(0), equalTo(0.0f))
+ }
+ }
+ }
+
+ @Test
+ fun textAlign_whenAlignRight_returnsLayoutWidthForGetLineRight() {
+ withDensity(defaultDensity) {
+ val texts = listOf("aa", "\u05D0\u05D0")
+ val fontSize = 20.sp
+ val fontSizeInPx = fontSize.toPx().value
+
+ texts.map { text ->
+ val paragraph = simpleParagraph(
+ text = text,
+ textAlign = TextAlign.Right,
+ fontSize = fontSize
+ )
+ val layoutWidth = (text.length + 2) * fontSizeInPx
+ paragraph.layout(ParagraphConstraints(width = layoutWidth))
+ val paragraphImpl = paragraph.paragraphImpl
+ assertThat(paragraphImpl.getLineRight(0), equalTo(layoutWidth))
+ }
+ }
+ }
+
+ @Test
+ fun textAlign_whenAlignCenter_textIsCentered() {
+ withDensity(defaultDensity) {
+ val texts = listOf("aa", "\u05D0\u05D0")
+ val fontSize = 20.sp
+ val fontSizeInPx = fontSize.toPx().value
+
+ texts.map { text ->
+ val paragraph = simpleParagraph(
+ text = text,
+ textAlign = TextAlign.Center,
+ fontSize = fontSize
+ )
+ val layoutWidth = (text.length + 2) * fontSizeInPx
+ paragraph.layout(ParagraphConstraints(width = layoutWidth))
+ val textWidth = text.length * fontSizeInPx
+ val paragraphImpl = paragraph.paragraphImpl
+ assertThat(
+ paragraphImpl.getLineLeft(0),
+ equalTo(layoutWidth / 2 - textWidth / 2)
+ )
+ assertThat(
+ paragraphImpl.getLineRight(0),
+ equalTo(layoutWidth / 2 + textWidth / 2)
+ )
+ }
+ }
+ }
+
+ @Test
+ fun textAlign_whenAlignStart_withLTR_returnsZeroForGetLineLeft() {
+ withDensity(defaultDensity) {
+ val text = "aa"
+ val fontSize = 20.sp
+ val fontSizeInPx = fontSize.toPx().value
+ val layoutWidth = (text.length + 2) * fontSizeInPx
+
val paragraph = simpleParagraph(
text = text,
- textAlign = TextAlign.Left,
+ textAlign = TextAlign.Start,
fontSize = fontSize
)
- val layoutWidth = (text.length + 2) * fontSize
paragraph.layout(ParagraphConstraints(width = layoutWidth))
val paragraphImpl = paragraph.paragraphImpl
assertThat(paragraphImpl.getLineLeft(0), equalTo(0.0f))
@@ -625,17 +801,18 @@
}
@Test
- fun textAlign_whenAlignRight_returnsLayoutWidthForGetLineRight() {
- val texts = listOf("aa", "\u05D0\u05D0")
- val fontSize = 20.0f
+ fun textAlign_whenAlignEnd_withLTR_returnsLayoutWidthForGetLineRight() {
+ withDensity(defaultDensity) {
+ val text = "aa"
+ val fontSize = 20.sp
+ val fontSizeInPx = fontSize.toPx().value
+ val layoutWidth = (text.length + 2) * fontSizeInPx
- texts.map { text ->
val paragraph = simpleParagraph(
text = text,
- textAlign = TextAlign.Right,
+ textAlign = TextAlign.End,
fontSize = fontSize
)
- val layoutWidth = (text.length + 2) * fontSize
paragraph.layout(ParagraphConstraints(width = layoutWidth))
val paragraphImpl = paragraph.paragraphImpl
assertThat(paragraphImpl.getLineRight(0), equalTo(layoutWidth))
@@ -643,89 +820,41 @@
}
@Test
- fun textAlign_whenAlignCenter_textIsCentered() {
- val texts = listOf("aa", "\u05D0\u05D0")
- val fontSize = 20.0f
+ fun textAlign_whenAlignStart_withRTL_returnsLayoutWidthForGetLineRight() {
+ withDensity(defaultDensity) {
+ val text = "\u05D0\u05D0"
+ val fontSize = 20.sp
+ val fontSizeInPx = fontSize.toPx().value
+ val layoutWidth = (text.length + 2) * fontSizeInPx
- texts.map { text ->
val paragraph = simpleParagraph(
text = text,
- textAlign = TextAlign.Center,
+ textAlign = TextAlign.Start,
fontSize = fontSize
)
- val layoutWidth = (text.length + 2) * fontSize
paragraph.layout(ParagraphConstraints(width = layoutWidth))
- val textWidth = text.length * fontSize
val paragraphImpl = paragraph.paragraphImpl
- assertThat(paragraphImpl.getLineLeft(0),
- equalTo(layoutWidth / 2 - textWidth / 2))
- assertThat(paragraphImpl.getLineRight(0),
- equalTo(layoutWidth / 2 + textWidth / 2))
+ assertThat(paragraphImpl.getLineRight(0), equalTo(layoutWidth))
}
}
@Test
- fun textAlign_whenAlignStart_withLTR_returnsZeroForGetLineLeft() {
- val text = "aa"
- val fontSize = 20.0f
- val layoutWidth = (text.length + 2) * fontSize
-
- val paragraph = simpleParagraph(
- text = text,
- textAlign = TextAlign.Start,
- fontSize = fontSize
- )
- paragraph.layout(ParagraphConstraints(width = layoutWidth))
- val paragraphImpl = paragraph.paragraphImpl
- assertThat(paragraphImpl.getLineLeft(0), equalTo(0.0f))
- }
-
- @Test
- fun textAlign_whenAlignEnd_withLTR_returnsLayoutWidthForGetLineRight() {
- val text = "aa"
- val fontSize = 20.0f
- val layoutWidth = (text.length + 2) * fontSize
-
- val paragraph = simpleParagraph(
- text = text,
- textAlign = TextAlign.End,
- fontSize = fontSize
- )
- paragraph.layout(ParagraphConstraints(width = layoutWidth))
- val paragraphImpl = paragraph.paragraphImpl
- assertThat(paragraphImpl.getLineRight(0), equalTo(layoutWidth))
- }
-
- @Test
- fun textAlign_whenAlignStart_withRTL_returnsLayoutWidthForGetLineRight() {
- val text = "\u05D0\u05D0"
- val fontSize = 20.0f
- val layoutWidth = (text.length + 2) * fontSize
-
- val paragraph = simpleParagraph(
- text = text,
- textAlign = TextAlign.Start,
- fontSize = fontSize
- )
- paragraph.layout(ParagraphConstraints(width = layoutWidth))
- val paragraphImpl = paragraph.paragraphImpl
- assertThat(paragraphImpl.getLineRight(0), equalTo(layoutWidth))
- }
-
- @Test
fun textAlign_whenAlignEnd_withRTL_returnsZeroForGetLineLeft() {
- val text = "\u05D0\u05D0"
- val fontSize = 20.0f
- val layoutWidth = (text.length + 2) * fontSize
+ withDensity(defaultDensity) {
+ val text = "\u05D0\u05D0"
+ val fontSize = 20.sp
+ val fontSizeInPx = fontSize.toPx().value
+ val layoutWidth = (text.length + 2) * fontSizeInPx
- val paragraph = simpleParagraph(
- text = text,
- textAlign = TextAlign.End,
- fontSize = fontSize
- )
- paragraph.layout(ParagraphConstraints(width = layoutWidth))
- val paragraphImpl = paragraph.paragraphImpl
- assertThat(paragraphImpl.getLineLeft(0), equalTo(0.0f))
+ val paragraph = simpleParagraph(
+ text = text,
+ textAlign = TextAlign.End,
+ fontSize = fontSize
+ )
+ paragraph.layout(ParagraphConstraints(width = layoutWidth))
+ val paragraphImpl = paragraph.paragraphImpl
+ assertThat(paragraphImpl.getLineLeft(0), equalTo(0.0f))
+ }
}
@Test
@@ -733,160 +862,188 @@
// We have to test justification above API 28 because of this bug b/68009059, where devices
// before API 28 may have an extra space at the end of line.
fun textAlign_whenAlignJustify_justifies() {
- val text = "a a a"
- val fontSize = 20.0f
- val layoutWidth = ("a a".length + 1) * fontSize
+ withDensity(defaultDensity) {
+ val text = "a a a"
+ val fontSize = 20.sp
+ val fontSizeInPx = fontSize.toPx().value
+ val layoutWidth = ("a a".length + 1) * fontSizeInPx
- val paragraph = simpleParagraph(
- text = text,
- textAlign = TextAlign.Justify,
- fontSize = fontSize
- )
- paragraph.layout(ParagraphConstraints(width = layoutWidth))
- val paragraphImpl = paragraph.paragraphImpl
- assertThat(paragraphImpl.getLineLeft(0), equalTo(0.0f))
- assertThat(paragraphImpl.getLineRight(0), equalTo(layoutWidth))
- // Last line should align start
- assertThat(paragraphImpl.getLineLeft(1), equalTo(0.0f))
+ val paragraph = simpleParagraph(
+ text = text,
+ textAlign = TextAlign.Justify,
+ fontSize = fontSize
+ )
+ paragraph.layout(ParagraphConstraints(width = layoutWidth))
+ val paragraphImpl = paragraph.paragraphImpl
+ assertThat(paragraphImpl.getLineLeft(0), equalTo(0.0f))
+ assertThat(paragraphImpl.getLineRight(0), equalTo(layoutWidth))
+ // Last line should align start
+ assertThat(paragraphImpl.getLineLeft(1), equalTo(0.0f))
+ }
}
@Test
fun textDirection_whenLTR_dotIsOnRight() {
- val text = "a.."
- val fontSize = 20.0f
- val layoutWidth = text.length * fontSize
+ withDensity(defaultDensity) {
+ val text = "a.."
+ val fontSize = 20.sp
+ val fontSizeInPx = fontSize.toPx().value
+ val layoutWidth = text.length * fontSizeInPx
- val paragraph = simpleParagraph(
- text = text,
- textDirection = TextDirection.Ltr,
- fontSize = fontSize
- )
- paragraph.layout(ParagraphConstraints(width = layoutWidth))
- // The offset of the last character in display order.
- val offset = Offset("a.".length * fontSize + 1, fontSize / 2)
- val charIndex = paragraph.getPositionForOffset(offset = offset)
- assertThat(charIndex, equalTo(2))
+ val paragraph = simpleParagraph(
+ text = text,
+ textDirection = TextDirection.Ltr,
+ fontSize = fontSize
+ )
+ paragraph.layout(ParagraphConstraints(width = layoutWidth))
+ // The offset of the last character in display order.
+ val offset = Offset("a.".length * fontSizeInPx + 1, fontSizeInPx / 2)
+ val charIndex = paragraph.getPositionForOffset(offset = offset)
+ assertThat(charIndex, equalTo(2))
+ }
}
@Test
fun textDirection_whenRTL_dotIsOnLeft() {
- val text = "a.."
- val fontSize = 20.0f
- val layoutWidth = text.length * fontSize
+ withDensity(defaultDensity) {
+ val text = "a.."
+ val fontSize = 20.sp
+ val fontSizeInPx = fontSize.toPx().value
+ val layoutWidth = text.length * fontSizeInPx
- val paragraph = simpleParagraph(
- text = text,
- textDirection = TextDirection.Rtl,
- fontSize = fontSize
- )
- paragraph.layout(ParagraphConstraints(width = layoutWidth))
- // The offset of the first character in display order.
- val offset = Offset(fontSize / 2 + 1, fontSize / 2)
- val charIndex = paragraph.getPositionForOffset(offset = offset)
- assertThat(charIndex, equalTo(2))
+ val paragraph = simpleParagraph(
+ text = text,
+ textDirection = TextDirection.Rtl,
+ fontSize = fontSize
+ )
+ paragraph.layout(ParagraphConstraints(width = layoutWidth))
+ // The offset of the first character in display order.
+ val offset = Offset(fontSizeInPx / 2 + 1, fontSizeInPx / 2)
+ val charIndex = paragraph.getPositionForOffset(offset = offset)
+ assertThat(charIndex, equalTo(2))
+ }
}
@Test
fun textDirection_whenDefault_withoutStrongChar_directionIsLTR() {
- val text = "..."
- val fontSize = 20.0f
- val layoutWidth = text.length * fontSize
+ withDensity(defaultDensity) {
+ val text = "..."
+ val fontSize = 20.sp
+ val fontSizeInPx = fontSize.toPx().value
+ val layoutWidth = text.length * fontSizeInPx
- val paragraph = simpleParagraph(
- text = text,
- fontSize = fontSize
- )
- paragraph.layout(ParagraphConstraints(width = layoutWidth))
- for (i in 0..text.length) {
- // The offset of the i-th character in display order.
- val offset = Offset(i * fontSize + 1, fontSize / 2)
- val charIndex = paragraph.getPositionForOffset(offset = offset)
- assertThat(charIndex, equalTo(i))
+ val paragraph = simpleParagraph(
+ text = text,
+ fontSize = fontSize
+ )
+ paragraph.layout(ParagraphConstraints(width = layoutWidth))
+ for (i in 0..text.length) {
+ // The offset of the i-th character in display order.
+ val offset = Offset(i * fontSizeInPx + 1, fontSizeInPx / 2)
+ val charIndex = paragraph.getPositionForOffset(offset = offset)
+ assertThat(charIndex, equalTo(i))
+ }
}
}
@Test
fun textDirection_whenDefault_withFirstStrongCharLTR_directionIsLTR() {
- val text = "a\u05D0."
- val fontSize = 20.0f
- val layoutWidth = text.length * fontSize
+ withDensity(defaultDensity) {
+ val text = "a\u05D0."
+ val fontSize = 20.sp
+ val fontSizeInPx = fontSize.toPx().value
+ val layoutWidth = text.length * fontSizeInPx
- val paragraph = simpleParagraph(
- text = text,
- fontSize = fontSize
- )
- paragraph.layout(ParagraphConstraints(width = layoutWidth))
- for (i in 0 until text.length) {
- // The offset of the i-th character in display order.
- val offset = Offset(i * fontSize + 1, fontSize / 2)
- val charIndex = paragraph.getPositionForOffset(offset = offset)
- assertThat(charIndex, equalTo(i))
+ val paragraph = simpleParagraph(
+ text = text,
+ fontSize = fontSize
+ )
+ paragraph.layout(ParagraphConstraints(width = layoutWidth))
+ for (i in 0 until text.length) {
+ // The offset of the i-th character in display order.
+ val offset = Offset(i * fontSizeInPx + 1, fontSizeInPx / 2)
+ val charIndex = paragraph.getPositionForOffset(offset = offset)
+ assertThat(charIndex, equalTo(i))
+ }
}
}
@Test
fun textDirection_whenDefault_withFirstStrongCharRTL_directionIsRTL() {
- val text = "\u05D0a."
- val fontSize = 20.0f
- val layoutWidth = text.length * fontSize
+ withDensity(defaultDensity) {
+ val text = "\u05D0a."
+ val fontSize = 20.sp
+ val fontSizeInPx = fontSize.toPx().value
+ val layoutWidth = text.length * fontSizeInPx
- val paragraph = simpleParagraph(
- text = text,
- fontSize = fontSize
- )
- paragraph.layout(ParagraphConstraints(width = layoutWidth))
- // The first character in display order should be '.'
- val offset = Offset(fontSize / 2 + 1, fontSize / 2)
- val index = paragraph.getPositionForOffset(offset = offset)
- assertThat(index, equalTo(2))
+ val paragraph = simpleParagraph(
+ text = text,
+ fontSize = fontSize
+ )
+ paragraph.layout(ParagraphConstraints(width = layoutWidth))
+ // The first character in display order should be '.'
+ val offset = Offset(fontSizeInPx / 2 + 1, fontSizeInPx / 2)
+ val index = paragraph.getPositionForOffset(offset = offset)
+ assertThat(index, equalTo(2))
+ }
}
@Test
fun lineHeight_returnsSameAsGiven() {
- val text = "abcdefgh"
- val fontSize = 20.0f
- // Make the layout 4 lines
- val layoutWidth = text.length * fontSize / 4
- val lineHeight = 1.5f
+ withDensity(defaultDensity) {
+ val text = "abcdefgh"
+ val fontSize = 20.sp
+ val fontSizeInPx = fontSize.toPx().value
+ // Make the layout 4 lines
+ val layoutWidth = text.length * fontSizeInPx / 4
+ val lineHeight = 1.5f
- val paragraph = simpleParagraph(
- text = text,
- fontSize = fontSize,
- lineHeight = lineHeight
- )
- paragraph.layout(ParagraphConstraints(width = layoutWidth))
- val paragraphImpl = paragraph.paragraphImpl
+ val paragraph = simpleParagraph(
+ text = text,
+ fontSize = fontSize,
+ lineHeight = lineHeight
+ )
+ paragraph.layout(ParagraphConstraints(width = layoutWidth))
+ val paragraphImpl = paragraph.paragraphImpl
- assertThat(paragraphImpl.lineCount, equalTo(4))
- // TODO(Migration/haoyuchang): Due to bug b/120530738, the height of the first line is
- // wrong in the framework. Will fix it when the lineHeight in TextSpan is implemented.
- for (i in 1 until paragraphImpl.lineCount - 1) {
- val actualHeight = paragraphImpl.getLineHeight(i)
- // In the sample_font.ttf, the height of the line should be
- // fontSize + 0.2f * fontSize(line gap)
- assertThat("line number $i", actualHeight, equalTo(1.2f * fontSize * lineHeight))
+ assertThat(paragraphImpl.lineCount, equalTo(4))
+ // TODO(Migration/haoyuchang): Due to bug b/120530738, the height of the first line is
+ // wrong in the framework. Will fix it when the lineHeight in TextSpan is implemented.
+ for (i in 1 until paragraphImpl.lineCount - 1) {
+ val actualHeight = paragraphImpl.getLineHeight(i)
+ // In the sample_font.ttf, the height of the line should be
+ // fontSize + 0.2f * fontSize(line gap)
+ assertThat(
+ "line number $i",
+ actualHeight,
+ equalTo(1.2f * fontSizeInPx * lineHeight)
+ )
+ }
}
}
@Test
fun lineHeight_hasNoEffectOnLastLine() {
- val text = "abc"
- val fontSize = 20.0f
- val layoutWidth = (text.length - 1) * fontSize
- val lineHeight = 1.5f
+ withDensity(defaultDensity) {
+ val text = "abc"
+ val fontSize = 20.sp
+ val fontSizeInPx = fontSize.toPx().value
+ val layoutWidth = (text.length - 1) * fontSizeInPx
+ val lineHeight = 1.5f
- val paragraph = simpleParagraph(
- text = text,
- fontSize = fontSize,
- lineHeight = lineHeight
- )
- paragraph.layout(ParagraphConstraints(width = layoutWidth))
- val paragraphImpl = paragraph.paragraphImpl
+ val paragraph = simpleParagraph(
+ text = text,
+ fontSize = fontSize,
+ lineHeight = lineHeight
+ )
+ paragraph.layout(ParagraphConstraints(width = layoutWidth))
+ val paragraphImpl = paragraph.paragraphImpl
- val lastLine = paragraphImpl.lineCount - 1
- // In the sample_font.ttf, the height of the line should be
- // fontSize + 0.2 * fontSize(line gap)
- assertThat(paragraphImpl.getLineHeight(lastLine), equalTo(1.2f * fontSize))
+ val lastLine = paragraphImpl.lineCount - 1
+ // In the sample_font.ttf, the height of the line should be
+ // fontSize + 0.2 * fontSize(line gap)
+ assertThat(paragraphImpl.getLineHeight(lastLine), equalTo(1.2f * fontSizeInPx))
+ }
}
@Test(expected = IllegalArgumentException::class)
@@ -897,680 +1054,774 @@
style = TextStyle(),
paragraphStyle = ParagraphStyle(
lineHeight = -1.0f
- )
+ ),
+ density = defaultDensity
)
}
@Test
fun testAnnotatedString_setFontSizeOnWholeText() {
- val text = "abcde"
- val fontSize = 20.0f
- val textStyle = TextStyle(fontSize = fontSize)
- val paragraphWidth = fontSize * text.length
+ withDensity(defaultDensity) {
+ val text = "abcde"
+ val fontSize = 20.sp
+ val fontSizeInPx = fontSize.toPx().value
+ val textStyle = TextStyle(fontSize = fontSize)
+ val paragraphWidth = fontSizeInPx * text.length
- val paragraph = simpleParagraph(
- text = text,
- textStyles = listOf(AnnotatedString.Item(textStyle, 0, text.length))
- )
- paragraph.layout(ParagraphConstraints(width = paragraphWidth))
- val paragraphImpl = paragraph.paragraphImpl
+ val paragraph = simpleParagraph(
+ text = text,
+ textStyles = listOf(AnnotatedString.Item(textStyle, 0, text.length))
+ )
+ paragraph.layout(ParagraphConstraints(width = paragraphWidth))
+ val paragraphImpl = paragraph.paragraphImpl
- // Make sure there is only one line, so that we can use getLineRight to test fontSize.
- assertThat(paragraphImpl.lineCount, equalTo(1))
- // Notice that in this test font, the width of character equals to fontSize.
- assertThat(paragraphImpl.getLineWidth(0), equalTo(fontSize * text.length))
+ // Make sure there is only one line, so that we can use getLineRight to test fontSize.
+ assertThat(paragraphImpl.lineCount, equalTo(1))
+ // Notice that in this test font, the width of character equals to fontSize.
+ assertThat(paragraphImpl.getLineWidth(0), equalTo(fontSizeInPx * text.length))
+ }
}
@Test
fun testAnnotatedString_setFontSizeOnPartOfText() {
- val text = "abcde"
- val fontSize = 20.0f
- val textStyleFontSize = 30.0f
- val textStyle = TextStyle(fontSize = textStyleFontSize)
- val paragraphWidth = textStyleFontSize * text.length
+ withDensity(defaultDensity) {
+ val text = "abcde"
+ val fontSize = 20.sp
+ val fontSizeInPx = fontSize.toPx().value
+ val textStyleFontSize = 30.sp
+ val textStyleFontSizeInPx = textStyleFontSize.toPx().value
+ val textStyle = TextStyle(fontSize = textStyleFontSize)
+ val paragraphWidth = textStyleFontSizeInPx * text.length
- val paragraph = simpleParagraph(
- text = text,
- textStyles = listOf(AnnotatedString.Item(textStyle, 0, "abc".length)),
- fontSize = fontSize
- )
- paragraph.layout(ParagraphConstraints(width = paragraphWidth))
- val paragraphImpl = paragraph.paragraphImpl
+ val paragraph = simpleParagraph(
+ text = text,
+ textStyles = listOf(AnnotatedString.Item(textStyle, 0, "abc".length)),
+ fontSize = fontSize
+ )
+ paragraph.layout(ParagraphConstraints(width = paragraphWidth))
+ val paragraphImpl = paragraph.paragraphImpl
- // Make sure there is only one line, so that we can use getLineRight to test fontSize.
- assertThat(paragraphImpl.lineCount, equalTo(1))
- // Notice that in this test font, the width of character equals to fontSize.
- val expectedLineRight = "abc".length * textStyleFontSize + "de".length * fontSize
- assertThat(paragraphImpl.getLineWidth(0), equalTo(expectedLineRight))
+ // Make sure there is only one line, so that we can use getLineRight to test fontSize.
+ assertThat(paragraphImpl.lineCount, equalTo(1))
+ // Notice that in this test font, the width of character equals to fontSize.
+ val expectedLineRight =
+ "abc".length * textStyleFontSizeInPx + "de".length * fontSizeInPx
+ assertThat(paragraphImpl.getLineWidth(0), equalTo(expectedLineRight))
+ }
}
@Test
fun testAnnotatedString_seFontSizeTwice_lastOneOverwrite() {
- val text = "abcde"
- val fontSize = 20.0f
- val textStyle = TextStyle(fontSize = fontSize)
+ withDensity(defaultDensity) {
+ val text = "abcde"
+ val fontSize = 20.sp
+ val fontSizeInPx = fontSize.toPx().value
+ val textStyle = TextStyle(fontSize = fontSize)
- val fontSizeOverwrite = 30.0f
- val textStyleOverwrite = TextStyle(fontSize = fontSizeOverwrite)
- val paragraphWidth = fontSizeOverwrite * text.length
+ val fontSizeOverwrite = 30.sp
+ val fontSizeOverwriteInPx = fontSizeOverwrite.toPx().value
+ val textStyleOverwrite = TextStyle(fontSize = fontSizeOverwrite)
+ val paragraphWidth = fontSizeOverwriteInPx * text.length
- val paragraph = simpleParagraph(
- text = text,
- textStyles = listOf(
- AnnotatedString.Item(textStyle, 0, text.length),
- AnnotatedString.Item(textStyleOverwrite, 0, "abc".length)
+ val paragraph = simpleParagraph(
+ text = text,
+ textStyles = listOf(
+ AnnotatedString.Item(textStyle, 0, text.length),
+ AnnotatedString.Item(textStyleOverwrite, 0, "abc".length)
+ )
)
- )
- paragraph.layout(ParagraphConstraints(width = paragraphWidth))
- val paragraphImpl = paragraph.paragraphImpl
+ paragraph.layout(ParagraphConstraints(width = paragraphWidth))
+ val paragraphImpl = paragraph.paragraphImpl
- // Make sure there is only one line, so that we can use getLineRight to test fontSize.
- assertThat(paragraphImpl.lineCount, equalTo(1))
- // Notice that in this test font, the width of character equals to fontSize.
- val expectedWidth = "abc".length * fontSizeOverwrite + "de".length * fontSize
- assertThat(paragraphImpl.getLineWidth(0), equalTo(expectedWidth))
+ // Make sure there is only one line, so that we can use getLineRight to test fontSize.
+ assertThat(paragraphImpl.lineCount, equalTo(1))
+ // Notice that in this test font, the width of character equals to fontSize.
+ val expectedWidth = "abc".length * fontSizeOverwriteInPx + "de".length * fontSizeInPx
+ assertThat(paragraphImpl.getLineWidth(0), equalTo(expectedWidth))
+ }
}
@Test
fun testAnnotatedString_fontSizeScale() {
- val text = "abcde"
- val fontSize = 20f
- val fontSizeScale = 0.5f
- val textStyle = TextStyle(fontSizeScale = fontSizeScale)
+ withDensity(defaultDensity) {
+ val text = "abcde"
+ val fontSize = 20.sp
+ val fontSizeInPx = fontSize.toPx().value
+ val fontSizeScale = 0.5f
+ val textStyle = TextStyle(fontSizeScale = fontSizeScale)
- val paragraph = simpleParagraph(
- text = text,
- textStyles = listOf(AnnotatedString.Item(textStyle, 0, text.length)),
- fontSize = fontSize
- )
- paragraph.layout(ParagraphConstraints(width = Float.MAX_VALUE))
- val paragraphImpl = paragraph.paragraphImpl
+ val paragraph = simpleParagraph(
+ text = text,
+ textStyles = listOf(AnnotatedString.Item(textStyle, 0, text.length)),
+ fontSize = fontSize
+ )
+ paragraph.layout(ParagraphConstraints(width = Float.MAX_VALUE))
+ val paragraphImpl = paragraph.paragraphImpl
- assertThat(
- paragraphImpl.getLineRight(0),
- equalTo(text.length * fontSize * fontSizeScale)
- )
+ assertThat(
+ paragraphImpl.getLineRight(0),
+ equalTo(text.length * fontSizeInPx * fontSizeScale)
+ )
+ }
}
@Test
fun testAnnotatedString_fontSizeScaleNested() {
- val text = "abcde"
- val fontSize = 20f
- val fontSizeScale = 0.5f
- val textStyle = TextStyle(fontSizeScale = fontSizeScale)
+ withDensity(defaultDensity) {
+ val text = "abcde"
+ val fontSize = 20.sp
+ val fontSizeInPx = fontSize.toPx().value
+ val fontSizeScale = 0.5f
+ val textStyle = TextStyle(fontSizeScale = fontSizeScale)
- val fontSizeScaleNested = 2f
- val textStyleNested = TextStyle(fontSizeScale = fontSizeScaleNested)
+ val fontSizeScaleNested = 2f
+ val textStyleNested = TextStyle(fontSizeScale = fontSizeScaleNested)
- val paragraph = simpleParagraph(
- text = text,
- textStyles = listOf(
- AnnotatedString.Item(textStyle, 0, text.length),
- AnnotatedString.Item(textStyleNested, 0, text.length)
- ),
- fontSize = fontSize
- )
- paragraph.layout(ParagraphConstraints(width = Float.MAX_VALUE))
- val paragraphImpl = paragraph.paragraphImpl
+ val paragraph = simpleParagraph(
+ text = text,
+ textStyles = listOf(
+ AnnotatedString.Item(textStyle, 0, text.length),
+ AnnotatedString.Item(textStyleNested, 0, text.length)
+ ),
+ fontSize = fontSize
+ )
+ paragraph.layout(ParagraphConstraints(width = Float.MAX_VALUE))
+ val paragraphImpl = paragraph.paragraphImpl
- assertThat(
- paragraphImpl.getLineRight(0),
- equalTo(text.length * fontSize * fontSizeScale * fontSizeScaleNested)
- )
+ assertThat(
+ paragraphImpl.getLineRight(0),
+ equalTo(text.length * fontSizeInPx * fontSizeScale * fontSizeScaleNested)
+ )
+ }
}
@Test
fun testAnnotatedString_fontSizeScaleWithFontSizeFirst() {
- val text = "abcde"
- val paragraphFontSize = 20f
+ withDensity(defaultDensity) {
+ val text = "abcde"
+ val paragraphFontSize = 20.sp
- val fontSize = 30f
- val fontSizeStyle = TextStyle(fontSize = fontSize)
+ val fontSize = 30.sp
+ val fontSizeInPx = fontSize.toPx().value
+ val fontSizeStyle = TextStyle(fontSize = fontSize)
- val fontSizeScale = 0.5f
- val fontSizeScaleStyle = TextStyle(fontSizeScale = fontSizeScale)
+ val fontSizeScale = 0.5f
+ val fontSizeScaleStyle = TextStyle(fontSizeScale = fontSizeScale)
- val paragraph = simpleParagraph(
- text = text,
- textStyles = listOf(
- AnnotatedString.Item(fontSizeStyle, 0, text.length),
- AnnotatedString.Item(fontSizeScaleStyle, 0, text.length)
- ),
- fontSize = paragraphFontSize
- )
- paragraph.layout(ParagraphConstraints(width = Float.MAX_VALUE))
- val paragraphImpl = paragraph.paragraphImpl
+ val paragraph = simpleParagraph(
+ text = text,
+ textStyles = listOf(
+ AnnotatedString.Item(fontSizeStyle, 0, text.length),
+ AnnotatedString.Item(fontSizeScaleStyle, 0, text.length)
+ ),
+ fontSize = paragraphFontSize
+ )
+ paragraph.layout(ParagraphConstraints(width = Float.MAX_VALUE))
+ val paragraphImpl = paragraph.paragraphImpl
- assertThat(
- paragraphImpl.getLineRight(0),
- equalTo(text.length * fontSize * fontSizeScale)
- )
+ assertThat(
+ paragraphImpl.getLineRight(0),
+ equalTo(text.length * fontSizeInPx * fontSizeScale)
+ )
+ }
}
@Test
fun testAnnotatedString_fontSizeScaleWithFontSizeSecond() {
- val text = "abcde"
- val paragraphFontSize = 20f
+ withDensity(defaultDensity) {
+ val text = "abcde"
+ val paragraphFontSize = 20.sp
- val fontSize = 30f
- val fontSizeStyle = TextStyle(fontSize = fontSize)
+ val fontSize = 30.sp
+ val fontSizeInPx = fontSize.toPx().value
+ val fontSizeStyle = TextStyle(fontSize = fontSize)
- val fontSizeScale = 0.5f
- val fontSizeScaleStyle = TextStyle(fontSizeScale = fontSizeScale)
+ val fontSizeScale = 0.5f
+ val fontSizeScaleStyle = TextStyle(fontSizeScale = fontSizeScale)
- val paragraph = simpleParagraph(
- text = text,
- textStyles = listOf(
- AnnotatedString.Item(fontSizeScaleStyle, 0, text.length),
- AnnotatedString.Item(fontSizeStyle, 0, text.length)
- ),
- fontSize = paragraphFontSize
- )
- paragraph.layout(ParagraphConstraints(width = Float.MAX_VALUE))
- val paragraphImpl = paragraph.paragraphImpl
+ val paragraph = simpleParagraph(
+ text = text,
+ textStyles = listOf(
+ AnnotatedString.Item(fontSizeScaleStyle, 0, text.length),
+ AnnotatedString.Item(fontSizeStyle, 0, text.length)
+ ),
+ fontSize = paragraphFontSize
+ )
+ paragraph.layout(ParagraphConstraints(width = Float.MAX_VALUE))
+ val paragraphImpl = paragraph.paragraphImpl
- assertThat(
- paragraphImpl.getLineRight(0),
- equalTo(text.length * fontSize)
- )
+ assertThat(
+ paragraphImpl.getLineRight(0),
+ equalTo(text.length * fontSizeInPx)
+ )
+ }
}
@Test
fun testAnnotatedString_fontSizeScaleWithFontSizeNested() {
- val text = "abcde"
- val paragraphFontSize = 20f
+ withDensity(defaultDensity) {
+ val text = "abcde"
+ val paragraphFontSize = 20.sp
- val fontSize = 30f
- val fontSizeStyle = TextStyle(fontSize = fontSize)
+ val fontSize = 30.sp
+ val fontSizeInPx = fontSize.toPx().value
+ val fontSizeStyle = TextStyle(fontSize = fontSize)
- val fontSizeScale1 = 0.5f
- val fontSizeScaleStyle1 = TextStyle(fontSizeScale = fontSizeScale1)
+ val fontSizeScale1 = 0.5f
+ val fontSizeScaleStyle1 = TextStyle(fontSizeScale = fontSizeScale1)
- val fontSizeScale2 = 2f
- val fontSizeScaleStyle2 = TextStyle(fontSizeScale = fontSizeScale2)
+ val fontSizeScale2 = 2f
+ val fontSizeScaleStyle2 = TextStyle(fontSizeScale = fontSizeScale2)
- val paragraph = simpleParagraph(
- text = text,
- textStyles = listOf(
- AnnotatedString.Item(fontSizeScaleStyle1, 0, text.length),
- AnnotatedString.Item(fontSizeStyle, 0, text.length),
- AnnotatedString.Item(fontSizeScaleStyle2, 0, text.length)
- ),
- fontSize = paragraphFontSize
- )
- paragraph.layout(ParagraphConstraints(width = Float.MAX_VALUE))
- val paragraphImpl = paragraph.paragraphImpl
+ val paragraph = simpleParagraph(
+ text = text,
+ textStyles = listOf(
+ AnnotatedString.Item(fontSizeScaleStyle1, 0, text.length),
+ AnnotatedString.Item(fontSizeStyle, 0, text.length),
+ AnnotatedString.Item(fontSizeScaleStyle2, 0, text.length)
+ ),
+ fontSize = paragraphFontSize
+ )
+ paragraph.layout(ParagraphConstraints(width = Float.MAX_VALUE))
+ val paragraphImpl = paragraph.paragraphImpl
- assertThat(
- paragraphImpl.getLineRight(0),
- equalTo(text.length * fontSize * fontSizeScale2)
- )
+ assertThat(
+ paragraphImpl.getLineRight(0),
+ equalTo(text.length * fontSizeInPx * fontSizeScale2)
+ )
+ }
}
@Test
fun testAnnotatedString_setLetterSpacingOnWholeText() {
- val text = "abcde"
- val fontSize = 20.0f
- val letterSpacing = 5.0f
- val textStyle = TextStyle(letterSpacing = letterSpacing)
- val paragraphWidth = fontSize * (1 + letterSpacing) * text.length
+ withDensity(defaultDensity) {
+ val text = "abcde"
+ val fontSize = 20.sp
+ val fontSizeInPx = fontSize.toPx().value
+ val letterSpacing = 5.0f
+ val textStyle = TextStyle(letterSpacing = letterSpacing)
+ val paragraphWidth = fontSizeInPx * (1 + letterSpacing) * text.length
- val paragraph = simpleParagraph(
- text = text,
- textStyles = listOf(AnnotatedString.Item(textStyle, 0, text.length)),
- fontSize = fontSize
- )
- paragraph.layout(ParagraphConstraints(width = paragraphWidth))
- val paragraphImpl = paragraph.paragraphImpl
+ val paragraph = simpleParagraph(
+ text = text,
+ textStyles = listOf(AnnotatedString.Item(textStyle, 0, text.length)),
+ fontSize = fontSize
+ )
+ paragraph.layout(ParagraphConstraints(width = paragraphWidth))
+ val paragraphImpl = paragraph.paragraphImpl
- // Make sure there is only one line, so that we can use getLineRight to test fontSize.
- assertThat(paragraphImpl.lineCount, equalTo(1))
- // Notice that in this test font, the width of character equals to fontSize.
- assertThat(
- paragraphImpl.getLineWidth(0),
- equalTo(fontSize * text.length * (1 + letterSpacing))
- )
+ // Make sure there is only one line, so that we can use getLineRight to test fontSize.
+ assertThat(paragraphImpl.lineCount, equalTo(1))
+ // Notice that in this test font, the width of character equals to fontSize.
+ assertThat(
+ paragraphImpl.getLineWidth(0),
+ equalTo(fontSizeInPx * text.length * (1 + letterSpacing))
+ )
+ }
}
@Test
fun testAnnotatedString_setLetterSpacingOnPartText() {
- val text = "abcde"
- val fontSize = 20.0f
- val letterSpacing = 5.0f
- val textStyle = TextStyle(letterSpacing = letterSpacing)
- val paragraphWidth = fontSize * (1 + letterSpacing) * text.length
+ withDensity(defaultDensity) {
+ val text = "abcde"
+ val fontSize = 20.sp
+ val fontSizeInPx = fontSize.toPx().value
+ val letterSpacing = 5.0f
+ val textStyle = TextStyle(letterSpacing = letterSpacing)
+ val paragraphWidth = fontSizeInPx * (1 + letterSpacing) * text.length
- val paragraph = simpleParagraph(
- text = text,
- textStyles = listOf(AnnotatedString.Item(textStyle, 0, "abc".length)),
- fontSize = fontSize
- )
- paragraph.layout(ParagraphConstraints(width = paragraphWidth))
- val paragraphImpl = paragraph.paragraphImpl
+ val paragraph = simpleParagraph(
+ text = text,
+ textStyles = listOf(AnnotatedString.Item(textStyle, 0, "abc".length)),
+ fontSize = fontSize
+ )
+ paragraph.layout(ParagraphConstraints(width = paragraphWidth))
+ val paragraphImpl = paragraph.paragraphImpl
- // Make sure there is only one line, so that we can use getLineRight to test fontSize.
- assertThat(paragraphImpl.lineCount, equalTo(1))
- // Notice that in this test font, the width of character equals to fontSize.
- val expectedWidth = ("abc".length * letterSpacing + text.length) * fontSize
- assertThat(paragraphImpl.getLineWidth(0), equalTo(expectedWidth))
+ // Make sure there is only one line, so that we can use getLineRight to test fontSize.
+ assertThat(paragraphImpl.lineCount, equalTo(1))
+ // Notice that in this test font, the width of character equals to fontSize.
+ val expectedWidth = ("abc".length * letterSpacing + text.length) * fontSizeInPx
+ assertThat(paragraphImpl.getLineWidth(0), equalTo(expectedWidth))
+ }
}
@Test
fun testAnnotatedString_setLetterSpacingTwice_lastOneOverwrite() {
- val text = "abcde"
- val fontSize = 20.0f
+ withDensity(defaultDensity) {
+ val text = "abcde"
+ val fontSize = 20.sp
+ val fontSizeInPx = fontSize.toPx().value
+ val letterSpacing = 5.0f
+ val textStyle = TextStyle(letterSpacing = letterSpacing)
- val letterSpacing = 5.0f
- val textStyle = TextStyle(letterSpacing = letterSpacing)
+ val letterSpacingOverwrite = 10.0f
+ val textStyleOverwrite = TextStyle(letterSpacing = letterSpacingOverwrite)
+ val paragraphWidth = fontSizeInPx * (1 + letterSpacingOverwrite) * text.length
- val letterSpacingOverwrite = 10.0f
- val textStyleOverwrite = TextStyle(letterSpacing = letterSpacingOverwrite)
- val paragraphWidth = fontSize * (1 + letterSpacingOverwrite) * text.length
+ val paragraph = simpleParagraph(
+ text = text,
+ textStyles = listOf(
+ AnnotatedString.Item(textStyle, 0, text.length),
+ AnnotatedString.Item(textStyleOverwrite, 0, "abc".length)
+ ),
+ fontSize = fontSize
+ )
+ paragraph.layout(ParagraphConstraints(width = paragraphWidth))
+ val paragraphImpl = paragraph.paragraphImpl
- val paragraph = simpleParagraph(
- text = text,
- textStyles = listOf(
- AnnotatedString.Item(textStyle, 0, text.length),
- AnnotatedString.Item(textStyleOverwrite, 0, "abc".length)
- ),
- fontSize = fontSize
- )
- paragraph.layout(ParagraphConstraints(width = paragraphWidth))
- val paragraphImpl = paragraph.paragraphImpl
-
- // Make sure there is only one line, so that we can use getLineRight to test fontSize.
- assertThat(paragraphImpl.lineCount, equalTo(1))
- // Notice that in this test font, the width of character equals to fontSize.
- val expectedWidth = "abc".length * (1 + letterSpacingOverwrite) * fontSize +
- "de".length * (1 + letterSpacing) * fontSize
- assertThat(paragraphImpl.getLineWidth(0), equalTo(expectedWidth))
+ // Make sure there is only one line, so that we can use getLineRight to test fontSize.
+ assertThat(paragraphImpl.lineCount, equalTo(1))
+ // Notice that in this test font, the width of character equals to fontSize.
+ val expectedWidth = "abc".length * (1 + letterSpacingOverwrite) * fontSizeInPx +
+ "de".length * (1 + letterSpacing) * fontSizeInPx
+ assertThat(paragraphImpl.getLineWidth(0), equalTo(expectedWidth))
+ }
}
@SdkSuppress(minSdkVersion = 29)
@Test
fun testAnnotatedString_setWordSpacingOnWholeText() {
if (!BuildCompat.isAtLeastQ()) return
- val text = "ab cd"
- val fontSize = 20.0f
- val wordSpacing = 5.0f
- val textStyle = TextStyle(wordSpacing = wordSpacing)
- val paragraphWidth = fontSize * (1 + text.length)
+ withDensity(defaultDensity) {
+ val text = "ab cd"
+ val fontSize = 20.sp
+ val fontSizeInPx = fontSize.toPx().value
+ val wordSpacing = 5.0f
+ val textStyle = TextStyle(wordSpacing = wordSpacing)
+ val paragraphWidth = fontSizeInPx * (1 + text.length)
- val paragraph = simpleParagraph(
- text = text,
- textStyles = listOf(AnnotatedString.Item(textStyle, 0, text.length)),
- fontSize = fontSize
- )
- paragraph.layout(ParagraphConstraints(width = paragraphWidth))
- val paragraphImpl = paragraph.paragraphImpl
+ val paragraph = simpleParagraph(
+ text = text,
+ textStyles = listOf(AnnotatedString.Item(textStyle, 0, text.length)),
+ fontSize = fontSize
+ )
+ paragraph.layout(ParagraphConstraints(width = paragraphWidth))
+ val paragraphImpl = paragraph.paragraphImpl
- // Make sure there is only one line, so that we can use getLineWidth to test fontSize.
- assertThat(paragraphImpl.lineCount, equalTo(1))
- // Notice that in this test font, the width of character equals to fontSize.
- assertThat(
- paragraphImpl.getLineWidth(0),
- equalTo(fontSize * text.length + wordSpacing)
- )
+ // Make sure there is only one line, so that we can use getLineWidth to test fontSize.
+ assertThat(paragraphImpl.lineCount, equalTo(1))
+ // Notice that in this test font, the width of character equals to fontSize.
+ assertThat(
+ paragraphImpl.getLineWidth(0),
+ equalTo(fontSizeInPx * text.length + wordSpacing)
+ )
+ }
}
@SdkSuppress(minSdkVersion = 29)
@Test
fun testAnnotatedString_setWordSpacingOnPartText() {
if (!BuildCompat.isAtLeastQ()) return
- val text = "a b c"
- val fontSize = 20.0f
- val wordSpacing = 5.0f
- val textStyle = TextStyle(wordSpacing = wordSpacing)
- val paragraphWidth = fontSize * (1 + text.length)
+ withDensity(defaultDensity) {
+ val text = "a b c"
+ val fontSize = 20.sp
+ val fontSizeInPx = fontSize.toPx().value
+ val wordSpacing = 5.0f
+ val textStyle = TextStyle(wordSpacing = wordSpacing)
+ val paragraphWidth = fontSizeInPx * (1 + text.length)
- val paragraph = simpleParagraph(
- text = text,
- textStyles = listOf(AnnotatedString.Item(textStyle, 0, "a b".length)),
- fontSize = fontSize
- )
- paragraph.layout(ParagraphConstraints(width = paragraphWidth))
- val paragraphImpl = paragraph.paragraphImpl
+ val paragraph = simpleParagraph(
+ text = text,
+ textStyles = listOf(AnnotatedString.Item(textStyle, 0, "a b".length)),
+ fontSize = fontSize
+ )
+ paragraph.layout(ParagraphConstraints(width = paragraphWidth))
+ val paragraphImpl = paragraph.paragraphImpl
- // Make sure there is only one line, so that we can use getLineWidth to test fontSize.
- assertThat(paragraphImpl.lineCount, equalTo(1))
- // Notice that in this test font, the width of character equals to fontSize.
- assertThat(
- paragraphImpl.getLineWidth(0),
- equalTo(fontSize * text.length + wordSpacing)
- )
+ // Make sure there is only one line, so that we can use getLineWidth to test fontSize.
+ assertThat(paragraphImpl.lineCount, equalTo(1))
+ // Notice that in this test font, the width of character equals to fontSize.
+ assertThat(
+ paragraphImpl.getLineWidth(0),
+ equalTo(fontSizeInPx * text.length + wordSpacing)
+ )
+ }
}
@SdkSuppress(minSdkVersion = 29)
@Test
fun testAnnotatedString_setWordSpacingTwice_lastOneOverwrite() {
if (!BuildCompat.isAtLeastQ()) return
- val text = "a b c"
- val fontSize = 20.0f
+ withDensity(defaultDensity) {
+ val text = "a b c"
+ val fontSize = 20.sp
+ val fontSizeInPx = fontSize.toPx().value
+ val wordSpacing = 2.0f
+ val textStyle = TextStyle(wordSpacing = wordSpacing)
- val wordSpacing = 2.0f
- val textStyle = TextStyle(wordSpacing = wordSpacing)
+ val wordSpacingOverwrite = 5.0f
+ val textStyleOverwrite = TextStyle(wordSpacing = wordSpacingOverwrite)
+ val paragraphWidth = fontSizeInPx * (1 + text.length)
- val wordSpacingOverwrite = 5.0f
- val textStyleOverwrite = TextStyle(wordSpacing = wordSpacingOverwrite)
- val paragraphWidth = fontSize * (1 + text.length)
+ val paragraph = simpleParagraph(
+ text = text,
+ textStyles = listOf(
+ AnnotatedString.Item(textStyle, 0, text.length),
+ AnnotatedString.Item(textStyleOverwrite, 0, "a b".length)
+ ),
+ fontSize = fontSize
+ )
+ paragraph.layout(ParagraphConstraints(width = paragraphWidth))
+ val paragraphImpl = paragraph.paragraphImpl
- val paragraph = simpleParagraph(
- text = text,
- textStyles = listOf(
- AnnotatedString.Item(textStyle, 0, text.length),
- AnnotatedString.Item(textStyleOverwrite, 0, "a b".length)
- ),
- fontSize = fontSize
- )
- paragraph.layout(ParagraphConstraints(width = paragraphWidth))
- val paragraphImpl = paragraph.paragraphImpl
-
- // Make sure there is only one line, so that we can use getLineWidth to test fontSize.
- assertThat(paragraphImpl.lineCount, equalTo(1))
- // Notice that in this test font, the width of character equals to fontSize.
- assertThat(
- paragraphImpl.getLineWidth(0),
- equalTo(fontSize * text.length + wordSpacing + wordSpacingOverwrite)
- )
+ // Make sure there is only one line, so that we can use getLineWidth to test fontSize.
+ assertThat(paragraphImpl.lineCount, equalTo(1))
+ // Notice that in this test font, the width of character equals to fontSize.
+ assertThat(
+ paragraphImpl.getLineWidth(0),
+ equalTo(fontSizeInPx * text.length + wordSpacing + wordSpacingOverwrite)
+ )
+ }
}
@Test
fun textIndent_onSingleLine() {
- val text = "abc"
- val fontSize = 20.0f
- val indent = 20.0f
+ withDensity(defaultDensity) {
+ val text = "abc"
+ val fontSize = 20.sp
+ val fontSizeInPx = fontSize.toPx().value
+ val indent = 20.0f
- val paragraph = simpleParagraph(
- text = text,
- textIndent = TextIndent(firstLine = indent.px),
- fontSize = fontSize,
- fontFamily = fontFamilyMeasureFont
- )
- paragraph.layout(ParagraphConstraints(width = Float.MAX_VALUE))
- val paragraphImpl = paragraph.paragraphImpl
+ val paragraph = simpleParagraph(
+ text = text,
+ textIndent = TextIndent(firstLine = indent.px),
+ fontSize = fontSize,
+ fontFamily = fontFamilyMeasureFont
+ )
+ paragraph.layout(ParagraphConstraints(width = Float.MAX_VALUE))
+ val paragraphImpl = paragraph.paragraphImpl
- // This offset should point to the first character 'a' if indent is applied.
- // Otherwise this offset will point to the second character 'b'.
- val offset = Offset(indent + 1, fontSize / 2)
- // The position corresponding to the offset should be the first char 'a'.
- assertThat(paragraphImpl.getPositionForOffset(offset), equalTo(0))
+ // This offset should point to the first character 'a' if indent is applied.
+ // Otherwise this offset will point to the second character 'b'.
+ val offset = Offset(indent + 1, fontSizeInPx / 2)
+ // The position corresponding to the offset should be the first char 'a'.
+ assertThat(paragraphImpl.getPositionForOffset(offset), equalTo(0))
+ }
}
@Test
fun textIndent_onFirstLine() {
- val text = "abcdef"
- val fontSize = 20.0f
- val indent = 20.0f
- val paragraphWidth = "abcd".length * fontSize
+ withDensity(defaultDensity) {
+ val text = "abcdef"
+ val fontSize = 20.sp
+ val fontSizeInPx = fontSize.toPx().value
+ val indent = 20.0f
+ val paragraphWidth = "abcd".length * fontSizeInPx
- val paragraph = simpleParagraph(
- text = text,
- textIndent = TextIndent(firstLine = indent.px),
- fontSize = fontSize,
- fontFamily = fontFamilyMeasureFont
- )
- paragraph.layout(ParagraphConstraints(width = paragraphWidth))
- val paragraphImpl = paragraph.paragraphImpl
+ val paragraph = simpleParagraph(
+ text = text,
+ textIndent = TextIndent(firstLine = indent.px),
+ fontSize = fontSize,
+ fontFamily = fontFamilyMeasureFont
+ )
+ paragraph.layout(ParagraphConstraints(width = paragraphWidth))
+ val paragraphImpl = paragraph.paragraphImpl
- assertThat(paragraphImpl.lineCount, equalTo(2))
- // This offset should point to the first character of the first line if indent is applied.
- // Otherwise this offset will point to the second character of the second line.
- val offset = Offset(indent + 1, fontSize / 2)
- // The position corresponding to the offset should be the first char 'a'.
- assertThat(paragraphImpl.getPositionForOffset(offset), equalTo(0))
+ assertThat(paragraphImpl.lineCount, equalTo(2))
+ // This offset should point to the first character of the first line if indent is
+ // applied. Otherwise this offset will point to the second character of the second line.
+ val offset = Offset(indent + 1, fontSizeInPx / 2)
+ // The position corresponding to the offset should be the first char 'a'.
+ assertThat(paragraphImpl.getPositionForOffset(offset), equalTo(0))
+ }
}
@Test
fun textIndent_onRestLine() {
- val text = "abcde"
- val fontSize = 20.0f
- val indent = 20.0f
- val paragraphWidth = "abc".length * fontSize
+ withDensity(defaultDensity) {
+ val text = "abcde"
+ val fontSize = 20.sp
+ val fontSizeInPx = fontSize.toPx().value
+ val indent = 20.0f
+ val paragraphWidth = "abc".length * fontSizeInPx
- val paragraph = simpleParagraph(
- text = text,
- textIndent = TextIndent(firstLine = 0.px, restLine = indent.px),
- fontSize = fontSize,
- fontFamily = fontFamilyMeasureFont
- )
- paragraph.layout(ParagraphConstraints(width = paragraphWidth))
- val paragraphImpl = paragraph.paragraphImpl
- // This offset should point to the first character of the second line if indent is applied.
- // Otherwise this offset will point to the second character of the second line.
- val offset = Offset(indent + 1, fontSize / 2 + fontSize)
- // The position corresponding to the offset should be the 'd' in the second line.
- assertThat(
- paragraphImpl.getPositionForOffset(offset),
- equalTo("abcd".length - 1)
- )
+ val paragraph = simpleParagraph(
+ text = text,
+ textIndent = TextIndent(firstLine = 0.px, restLine = indent.px),
+ fontSize = fontSize,
+ fontFamily = fontFamilyMeasureFont
+ )
+ paragraph.layout(ParagraphConstraints(width = paragraphWidth))
+ val paragraphImpl = paragraph.paragraphImpl
+ // This offset should point to the first character of the second line if indent is
+ // applied. Otherwise this offset will point to the second character of the second line.
+ val offset = Offset(indent + 1, fontSizeInPx / 2 + fontSizeInPx)
+ // The position corresponding to the offset should be the 'd' in the second line.
+ assertThat(
+ paragraphImpl.getPositionForOffset(offset),
+ equalTo("abcd".length - 1)
+ )
+ }
}
@Test
fun testAnnotatedString_fontFamily_changesMeasurement() {
- val text = "ad"
- val fontSize = 20.0f
- // custom 100 regular font has b as the wide glyph
- // custom 200 regular font has d as the wide glyph
- val textStyle = TextStyle(fontFamily = fontFamilyCustom200)
- // a is rendered in paragraphStyle font (custom 100), it will not have wide glyph
- // d is rendered in defaultTextStyle font (custom 200), and it will be wide glyph
- val expectedWidth = fontSize + fontSize * 3
+ withDensity(defaultDensity) {
+ val text = "ad"
+ val fontSize = 20.sp
+ val fontSizeInPx = fontSize.toPx().value
+ // custom 100 regular font has b as the wide glyph
+ // custom 200 regular font has d as the wide glyph
+ val textStyle = TextStyle(fontFamily = fontFamilyCustom200)
+ // a is rendered in paragraphStyle font (custom 100), it will not have wide glyph
+ // d is rendered in defaultTextStyle font (custom 200), and it will be wide glyph
+ val expectedWidth = fontSizeInPx + fontSizeInPx * 3
- val paragraph = simpleParagraph(
- text = text,
- textStyles = listOf(
- AnnotatedString.Item(textStyle, "a".length, text.length)
- ),
- fontSize = fontSize,
- fontFamily = fontFamilyCustom100
- )
- paragraph.layout(ParagraphConstraints(width = Float.MAX_VALUE))
- val paragraphImpl = paragraph.paragraphImpl
+ val paragraph = simpleParagraph(
+ text = text,
+ textStyles = listOf(
+ AnnotatedString.Item(textStyle, "a".length, text.length)
+ ),
+ fontSize = fontSize,
+ fontFamily = fontFamilyCustom100
+ )
+ paragraph.layout(ParagraphConstraints(width = Float.MAX_VALUE))
+ val paragraphImpl = paragraph.paragraphImpl
- assertThat(paragraphImpl.lineCount, equalTo(1))
- assertThat(paragraphImpl.getLineWidth(0), equalTo(expectedWidth))
+ assertThat(paragraphImpl.lineCount, equalTo(1))
+ assertThat(paragraphImpl.getLineWidth(0), equalTo(expectedWidth))
+ }
}
@Test
fun testAnnotatedString_fontFeature_turnOffKern() {
- val text = "AaAa"
- val fontSize = 20.0f
- // This fontFeatureSetting turns off the kerning
- val textStyle = TextStyle(fontFeatureSettings = "\"kern\" 0")
+ withDensity(defaultDensity) {
+ val text = "AaAa"
+ val fontSize = 20.sp
+ val fontSizeInPx = fontSize.toPx().value
+ // This fontFeatureSetting turns off the kerning
+ val textStyle = TextStyle(fontFeatureSettings = "\"kern\" 0")
- val paragraph = simpleParagraph(
- text = text,
- textStyles = listOf(
- AnnotatedString.Item(textStyle, 0, "aA".length)
- ),
- fontSize = fontSize,
- fontFamily = fontFamilyKernFont
- )
- paragraph.layout(ParagraphConstraints(width = Float.MAX_VALUE))
- val paragraphImpl = paragraph.paragraphImpl
+ val paragraph = simpleParagraph(
+ text = text,
+ textStyles = listOf(
+ AnnotatedString.Item(textStyle, 0, "aA".length)
+ ),
+ fontSize = fontSize,
+ fontFamily = fontFamilyKernFont
+ )
+ paragraph.layout(ParagraphConstraints(width = Float.MAX_VALUE))
+ val paragraphImpl = paragraph.paragraphImpl
- // Two characters are kerning, so minus 0.4 * fontSize
- val expectedWidth = text.length * fontSize - 0.4f * fontSize
- assertThat(paragraphImpl.lineCount, equalTo(1))
- assertThat(paragraphImpl.getLineWidth(0), equalTo(expectedWidth))
+ // Two characters are kerning, so minus 0.4 * fontSize
+ val expectedWidth = text.length * fontSizeInPx - 0.4f * fontSizeInPx
+ assertThat(paragraphImpl.lineCount, equalTo(1))
+ assertThat(paragraphImpl.getLineWidth(0), equalTo(expectedWidth))
+ }
}
@Test
fun testAnnotatedString_shadow() {
- val text = "abcde"
- val fontSize = 20f
- val paragraphWidth = fontSize * text.length
+ withDensity(defaultDensity) {
+ val text = "abcde"
+ val fontSize = 20.sp
+ val fontSizeInPx = fontSize.toPx().value
+ val paragraphWidth = fontSizeInPx * text.length
- val textStyle = TextStyle(shadow = Shadow(Color(0xFF00FF00.toInt()), Offset(1f, 2f), 3.px))
- val paragraphShadow = simpleParagraph(
- text = text,
- textStyles = listOf(
- AnnotatedString.Item(textStyle, 0, text.length)
+ val textStyle =
+ TextStyle(shadow = Shadow(Color(0xFF00FF00.toInt()), Offset(1f, 2f), 3.px))
+ val paragraphShadow = simpleParagraph(
+ text = text,
+ textStyles = listOf(
+ AnnotatedString.Item(textStyle, 0, text.length)
+ )
)
- )
- paragraphShadow.layout(ParagraphConstraints(width = paragraphWidth))
+ paragraphShadow.layout(ParagraphConstraints(width = paragraphWidth))
- val paragraph = simpleParagraph(text = text)
- paragraph.layout(ParagraphConstraints(width = paragraphWidth))
+ val paragraph = simpleParagraph(text = text)
+ paragraph.layout(ParagraphConstraints(width = paragraphWidth))
- assertThat(paragraphShadow.bitmap(), not(equalToBitmap(paragraph.bitmap())))
+ assertThat(paragraphShadow.bitmap(), not(equalToBitmap(paragraph.bitmap())))
+ }
}
@Test
fun testDefaultTextStyle_setColor() {
- val text = "abc"
- // FontSize doesn't matter here, but it should be big enough for bitmap comparison.
- val fontSize = 100f
- val paragraphWidth = fontSize * text.length
- val textStyle = TextStyle(color = Color.Red)
+ withDensity(defaultDensity) {
+ val text = "abc"
+ // FontSize doesn't matter here, but it should be big enough for bitmap comparison.
+ val fontSize = 100.sp
+ val fontSizeInPx = fontSize.toPx().value
+ val paragraphWidth = fontSizeInPx * text.length
+ val textStyle = TextStyle(color = Color.Red)
- val paragraphWithoutColor = simpleParagraph(
- text = text,
- fontSize = fontSize
- )
- paragraphWithoutColor.layout(ParagraphConstraints(paragraphWidth))
+ val paragraphWithoutColor = simpleParagraph(
+ text = text,
+ fontSize = fontSize
+ )
+ paragraphWithoutColor.layout(ParagraphConstraints(paragraphWidth))
- val paragraphWithColor = simpleParagraph(
- text = text,
- textStyle = textStyle,
- fontSize = fontSize
- )
- paragraphWithColor.layout(ParagraphConstraints(paragraphWidth))
+ val paragraphWithColor = simpleParagraph(
+ text = text,
+ textStyle = textStyle,
+ fontSize = fontSize
+ )
+ paragraphWithColor.layout(ParagraphConstraints(paragraphWidth))
- assertThat(
- paragraphWithColor.bitmap(),
- not(equalToBitmap(paragraphWithoutColor.bitmap()))
- )
+ assertThat(
+ paragraphWithColor.bitmap(),
+ not(equalToBitmap(paragraphWithoutColor.bitmap()))
+ )
+ }
}
@Test
fun testDefaultTextStyle_setLetterSpacing() {
- val text = "abc"
- // FontSize doesn't matter here, but it should be big enough for bitmap comparison.
- val fontSize = 100f
- val letterSpacing = 1f
- val textStyle = TextStyle(letterSpacing = letterSpacing)
+ withDensity(defaultDensity) {
+ val text = "abc"
+ // FontSize doesn't matter here, but it should be big enough for bitmap comparison.
+ val fontSize = 100.sp
+ val fontSizeInPx = fontSize.toPx().value
+ val letterSpacing = 1f
+ val textStyle = TextStyle(letterSpacing = letterSpacing)
- val paragraph = simpleParagraph(
- text = text,
- textStyle = textStyle,
- fontSize = fontSize
- )
- paragraph.layout(ParagraphConstraints(Float.MAX_VALUE))
+ val paragraph = simpleParagraph(
+ text = text,
+ textStyle = textStyle,
+ fontSize = fontSize
+ )
+ paragraph.layout(ParagraphConstraints(Float.MAX_VALUE))
- assertThat(
- paragraph.getLineRight(0),
- equalTo(fontSize * (1 + letterSpacing) * text.length)
- )
+ assertThat(
+ paragraph.getLineRight(0),
+ equalTo(fontSizeInPx * (1 + letterSpacing) * text.length)
+ )
+ }
}
@Test
fun testGetPathForRange_singleLine() {
- // Setup test.
- val text = "abc"
- val fontSize = 20f
- val paragraph = simpleParagraph(
+ withDensity(defaultDensity) {
+ val text = "abc"
+ val fontSize = 20.sp
+ val fontSizeInPx = fontSize.toPx().value
+ val paragraph = simpleParagraph(
text = text,
fontFamily = fontFamilyMeasureFont,
fontSize = fontSize
- )
- paragraph.layout(ParagraphConstraints(width = Float.MAX_VALUE))
+ )
+ paragraph.layout(ParagraphConstraints(width = Float.MAX_VALUE))
- val paragraphImpl = paragraph.paragraphImpl
- val expectedPath = Path()
- val lineLeft = paragraphImpl.getLineLeft(0)
- val lineRight = paragraphImpl.getLineRight(0)
- expectedPath.addRect(Rect(lineLeft, 0f, lineRight - fontSize, fontSize))
+ val paragraphImpl = paragraph.paragraphImpl
+ val expectedPath = Path()
+ val lineLeft = paragraphImpl.getLineLeft(0)
+ val lineRight = paragraphImpl.getLineRight(0)
+ expectedPath.addRect(
+ Rect(
+ lineLeft,
+ 0f,
+ lineRight - fontSizeInPx,
+ fontSizeInPx
+ )
+ )
- // Run.
- // Select "ab"
- val actualPath = paragraph.getPathForRange(0, 2)
+ // Select "ab"
+ val actualPath = paragraph.getPathForRange(0, 2)
- // Assert.
- val diff = Path.combine(PathOperation.difference, expectedPath, actualPath).getBounds()
- assertThat(diff, equalTo(Rect.zero))
+ val diff = Path.combine(PathOperation.difference, expectedPath, actualPath).getBounds()
+ assertThat(diff, equalTo(Rect.zero))
+ }
}
@Test
fun testGetPathForRange_multiLines() {
- // Setup test.
- val text = "abc\nabc"
- val fontSize = 20f
- val paragraph = simpleParagraph(
+ withDensity(defaultDensity) {
+ val text = "abc\nabc"
+ val fontSize = 20.sp
+ val fontSizeInPx = fontSize.toPx().value
+ val paragraph = simpleParagraph(
text = text,
fontFamily = fontFamilyMeasureFont,
fontSize = fontSize
- )
- paragraph.layout(ParagraphConstraints(width = Float.MAX_VALUE))
+ )
+ paragraph.layout(ParagraphConstraints(width = Float.MAX_VALUE))
- val paragraphImpl = paragraph.paragraphImpl
- val expectedPath = Path()
- val firstLineLeft = paragraphImpl.getLineLeft(0)
- val secondLineLeft = paragraphImpl.getLineLeft(1)
- val firstLineRight = paragraphImpl.getLineRight(0)
- val secondLineRight = paragraphImpl.getLineRight(1)
- expectedPath.addRect(Rect(firstLineLeft + fontSize, 0f, firstLineRight, fontSize))
- expectedPath.addRect(Rect(
- secondLineLeft,
- fontSize,
- secondLineRight - fontSize,
- paragraph.height))
+ val paragraphImpl = paragraph.paragraphImpl
+ val expectedPath = Path()
+ val firstLineLeft = paragraphImpl.getLineLeft(0)
+ val secondLineLeft = paragraphImpl.getLineLeft(1)
+ val firstLineRight = paragraphImpl.getLineRight(0)
+ val secondLineRight = paragraphImpl.getLineRight(1)
+ expectedPath.addRect(
+ Rect(
+ firstLineLeft + fontSizeInPx,
+ 0f,
+ firstLineRight,
+ fontSizeInPx
+ )
+ )
+ expectedPath.addRect(
+ Rect(
+ secondLineLeft,
+ fontSizeInPx,
+ secondLineRight - fontSizeInPx,
+ paragraph.height
+ )
+ )
- // Run.
- // Select "bc\nab"
- val actualPath = paragraph.getPathForRange(1, 6)
+ // Select "bc\nab"
+ val actualPath = paragraph.getPathForRange(1, 6)
- // Assert.
- val diff = Path.combine(PathOperation.difference, expectedPath, actualPath).getBounds()
- assertThat(diff, equalTo(Rect.zero))
+ val diff = Path.combine(PathOperation.difference, expectedPath, actualPath).getBounds()
+ assertThat(diff, equalTo(Rect.zero))
+ }
}
@Test
fun testGetPathForRange_Bidi() {
- // Setup test.
- val textLTR = "Hello"
- val textRTL = "שלום"
- val text = textLTR + textRTL
- val selectionLTRStart = 2
- val selectionRTLEnd = 2
- val fontSize = 20f
- val paragraph = simpleParagraph(
+ withDensity(defaultDensity) {
+ val textLTR = "Hello"
+ val textRTL = "שלום"
+ val text = textLTR + textRTL
+ val selectionLTRStart = 2
+ val selectionRTLEnd = 2
+ val fontSize = 20.sp
+ val fontSizeInPx = fontSize.toPx().value
+ val paragraph = simpleParagraph(
text = text,
fontFamily = fontFamilyMeasureFont,
fontSize = fontSize
- )
- paragraph.layout(ParagraphConstraints(width = Float.MAX_VALUE))
+ )
+ paragraph.layout(ParagraphConstraints(width = Float.MAX_VALUE))
- val paragraphImpl = paragraph.paragraphImpl
- val expectedPath = Path()
- val lineLeft = paragraphImpl.getLineLeft(0)
- val lineRight = paragraphImpl.getLineRight(0)
- expectedPath.addRect(
+ val paragraphImpl = paragraph.paragraphImpl
+ val expectedPath = Path()
+ val lineLeft = paragraphImpl.getLineLeft(0)
+ val lineRight = paragraphImpl.getLineRight(0)
+ expectedPath.addRect(
Rect(
- lineLeft + selectionLTRStart * fontSize,
+ lineLeft + selectionLTRStart * fontSizeInPx,
0f,
- lineLeft + textLTR.length * fontSize,
- fontSize))
- expectedPath.addRect(Rect(lineRight - selectionRTLEnd * fontSize, 0f, lineRight, fontSize))
+ lineLeft + textLTR.length * fontSizeInPx,
+ fontSizeInPx
+ )
+ )
+ expectedPath.addRect(
+ Rect(
+ lineRight - selectionRTLEnd * fontSizeInPx,
+ 0f,
+ lineRight,
+ fontSizeInPx
+ )
+ )
- // Run.
- // Select "llo..של"
- val actualPath =
+ // Select "llo..של"
+ val actualPath =
paragraph.getPathForRange(selectionLTRStart, textLTR.length + selectionRTLEnd)
- // Assert.
- val diff = Path.combine(PathOperation.difference, expectedPath, actualPath).getBounds()
- assertThat(diff, equalTo(Rect.zero))
+ val diff = Path.combine(PathOperation.difference, expectedPath, actualPath).getBounds()
+ assertThat(diff, equalTo(Rect.zero))
+ }
}
@Test
fun testGetPathForRange_Start_Equals_End_Returns_Empty_Path() {
val text = "abc"
- val fontSize = 20f
val paragraph = simpleParagraph(
- text = text,
- fontFamily = fontFamilyMeasureFont,
- fontSize = fontSize
+ text = text,
+ fontFamily = fontFamilyMeasureFont,
+ fontSize = 20.sp
)
paragraph.layout(ParagraphConstraints(width = Float.MAX_VALUE))
@@ -1582,11 +1833,10 @@
@Test
fun testGetPathForRange_Empty_Text() {
val text = ""
- val fontSize = 20f
val paragraph = simpleParagraph(
- text = text,
- fontFamily = fontFamilyMeasureFont,
- fontSize = fontSize
+ text = text,
+ fontFamily = fontFamilyMeasureFont,
+ fontSize = 20.sp
)
paragraph.layout(ParagraphConstraints(width = Float.MAX_VALUE))
@@ -1597,171 +1847,177 @@
@Test
fun testGetPathForRange_Surrogate_Pair_Start_Middle_Second_Character_Selected() {
- // Setup test.
- val text = "\uD834\uDD1E\uD834\uDD1F"
- val fontSize = 20f
- val paragraph = simpleParagraph(
+ withDensity(defaultDensity) {
+ val text = "\uD834\uDD1E\uD834\uDD1F"
+ val fontSize = 20.sp
+ val fontSizeInPx = fontSize.toPx().value
+ val paragraph = simpleParagraph(
text = text,
fontFamily = fontFamilyMeasureFont,
fontSize = fontSize
- )
- paragraph.layout(ParagraphConstraints(width = Float.MAX_VALUE))
+ )
+ paragraph.layout(ParagraphConstraints(width = Float.MAX_VALUE))
- val paragraphImpl = paragraph.paragraphImpl
- val expectedPath = Path()
- val lineRight = paragraphImpl.getLineRight(0)
- expectedPath.addRect(Rect(lineRight / 2, 0f, lineRight, fontSize))
+ val paragraphImpl = paragraph.paragraphImpl
+ val expectedPath = Path()
+ val lineRight = paragraphImpl.getLineRight(0)
+ expectedPath.addRect(Rect(lineRight / 2, 0f, lineRight, fontSizeInPx))
- // Run.
- // Try to select "\uDD1E\uD834\uDD1F", only "\uD834\uDD1F" is selected.
- val actualPath = paragraph.getPathForRange(1, text.length)
+ // Try to select "\uDD1E\uD834\uDD1F", only "\uD834\uDD1F" is selected.
+ val actualPath = paragraph.getPathForRange(1, text.length)
- // Assert.
- val diff = Path.combine(PathOperation.difference, expectedPath, actualPath).getBounds()
- assertThat(diff, equalTo(Rect.zero))
+ val diff = Path.combine(PathOperation.difference, expectedPath, actualPath).getBounds()
+ assertThat(diff, equalTo(Rect.zero))
+ }
}
@Test
fun testGetPathForRange_Surrogate_Pair_End_Middle_Second_Character_Selected() {
- // Setup test.
- val text = "\uD834\uDD1E\uD834\uDD1F"
- val fontSize = 20f
- val paragraph = simpleParagraph(
+ withDensity(defaultDensity) {
+ val text = "\uD834\uDD1E\uD834\uDD1F"
+ val fontSize = 20.sp
+ val fontSizeInPx = fontSize.toPx().value
+ val paragraph = simpleParagraph(
text = text,
fontFamily = fontFamilyMeasureFont,
fontSize = fontSize
- )
- paragraph.layout(ParagraphConstraints(width = Float.MAX_VALUE))
+ )
+ paragraph.layout(ParagraphConstraints(width = Float.MAX_VALUE))
- val paragraphImpl = paragraph.paragraphImpl
- val expectedPath = Path()
- val lineRight = paragraphImpl.getLineRight(0)
- expectedPath.addRect(Rect(lineRight / 2, 0f, lineRight, fontSize))
+ val paragraphImpl = paragraph.paragraphImpl
+ val expectedPath = Path()
+ val lineRight = paragraphImpl.getLineRight(0)
+ expectedPath.addRect(Rect(lineRight / 2, 0f, lineRight, fontSizeInPx))
- // Run.
- // Try to select "\uDD1E\uD834", actually "\uD834\uDD1F" is selected.
- val actualPath = paragraph.getPathForRange(1, text.length - 1)
+ // Try to select "\uDD1E\uD834", actually "\uD834\uDD1F" is selected.
+ val actualPath = paragraph.getPathForRange(1, text.length - 1)
- // Assert.
- val diff = Path.combine(PathOperation.difference, expectedPath, actualPath).getBounds()
- assertThat(diff, equalTo(Rect.zero))
+ val diff = Path.combine(PathOperation.difference, expectedPath, actualPath).getBounds()
+ assertThat(diff, equalTo(Rect.zero))
+ }
}
@Test
fun testGetPathForRange_Surrogate_Pair_Start_Middle_End_Same_Character_Returns_Line_Segment() {
- // Setup test.
- val text = "\uD834\uDD1E\uD834\uDD1F"
- val fontSize = 20f
- val paragraph = simpleParagraph(
+ withDensity(defaultDensity) {
+ val text = "\uD834\uDD1E\uD834\uDD1F"
+ val fontSize = 20.sp
+ val fontSizeInPx = fontSize.toPx().value
+ val paragraph = simpleParagraph(
text = text,
fontFamily = fontFamilyMeasureFont,
fontSize = fontSize
- )
- paragraph.layout(ParagraphConstraints(width = Float.MAX_VALUE))
+ )
+ paragraph.layout(ParagraphConstraints(width = Float.MAX_VALUE))
- val paragraphImpl = paragraph.paragraphImpl
- val expectedPath = Path()
- val lineRight = paragraphImpl.getLineRight(0)
- expectedPath.addRect(Rect(lineRight / 2, 0f, lineRight / 2, fontSize))
+ val paragraphImpl = paragraph.paragraphImpl
+ val expectedPath = Path()
+ val lineRight = paragraphImpl.getLineRight(0)
+ expectedPath.addRect(Rect(lineRight / 2, 0f, lineRight / 2, fontSizeInPx))
- // Run.
- // Try to select "\uDD1E", get vertical line segment after this character.
- val actualPath = paragraph.getPathForRange(1, 2)
+ // Try to select "\uDD1E", get vertical line segment after this character.
+ val actualPath = paragraph.getPathForRange(1, 2)
- // Assert.
- val diff = Path.combine(PathOperation.difference, expectedPath, actualPath).getBounds()
- assertThat(diff, equalTo(Rect.zero))
+ val diff = Path.combine(PathOperation.difference, expectedPath, actualPath).getBounds()
+ assertThat(diff, equalTo(Rect.zero))
+ }
}
@Test
fun testGetPathForRange_Emoji_Sequence() {
- // Setup test.
- val text = "\u1F600\u1F603\u1F604\u1F606"
- val fontSize = 20f
- val paragraph = simpleParagraph(
+ withDensity(defaultDensity) {
+ val text = "\u1F600\u1F603\u1F604\u1F606"
+ val fontSize = 20.sp
+ val fontSizeInPx = fontSize.toPx().value
+ val paragraph = simpleParagraph(
text = text,
fontFamily = fontFamilyMeasureFont,
fontSize = fontSize
- )
- paragraph.layout(ParagraphConstraints(width = Float.MAX_VALUE))
+ )
+ paragraph.layout(ParagraphConstraints(width = Float.MAX_VALUE))
- val paragraphImpl = paragraph.paragraphImpl
- val expectedPath = Path()
- val lineLeft = paragraphImpl.getLineLeft(0)
- val lineRight = paragraphImpl.getLineRight(0)
- expectedPath.addRect(Rect(lineLeft + fontSize, 0f, lineRight - fontSize, fontSize))
+ val paragraphImpl = paragraph.paragraphImpl
+ val expectedPath = Path()
+ val lineLeft = paragraphImpl.getLineLeft(0)
+ val lineRight = paragraphImpl.getLineRight(0)
+ expectedPath.addRect(
+ Rect(
+ lineLeft + fontSizeInPx,
+ 0f,
+ lineRight - fontSizeInPx,
+ fontSizeInPx
+ )
+ )
- // Run.
- // Select "\u1F603\u1F604"
- val actualPath = paragraph.getPathForRange(1, text.length - 1)
+ // Select "\u1F603\u1F604"
+ val actualPath = paragraph.getPathForRange(1, text.length - 1)
- // Assert.
- val diff = Path.combine(PathOperation.difference, expectedPath, actualPath).getBounds()
- assertThat(diff, equalTo(Rect.zero))
+ val diff = Path.combine(PathOperation.difference, expectedPath, actualPath).getBounds()
+ assertThat(diff, equalTo(Rect.zero))
+ }
}
@Test
fun testGetPathForRange_Unicode_200D_Return_Line_Segment() {
- // Setup test.
- val text = "\u200D"
- val fontSize = 20f
- val paragraph = simpleParagraph(
+ withDensity(defaultDensity) {
+ val text = "\u200D"
+ val fontSize = 20.sp
+ val fontSizeInPx = fontSize.toPx().value
+ val paragraph = simpleParagraph(
text = text,
fontFamily = fontFamilyMeasureFont,
fontSize = fontSize
- )
- paragraph.layout(ParagraphConstraints(width = Float.MAX_VALUE))
+ )
+ paragraph.layout(ParagraphConstraints(width = Float.MAX_VALUE))
- val paragraphImpl = paragraph.paragraphImpl
- val expectedPath = Path()
- val lineLeft = paragraphImpl.getLineLeft(0)
- val lineRight = paragraphImpl.getLineRight(0)
- expectedPath.addRect(Rect(lineLeft, 0f, lineRight, fontSize))
+ val paragraphImpl = paragraph.paragraphImpl
+ val expectedPath = Path()
+ val lineLeft = paragraphImpl.getLineLeft(0)
+ val lineRight = paragraphImpl.getLineRight(0)
+ expectedPath.addRect(Rect(lineLeft, 0f, lineRight, fontSizeInPx))
- // Run.
- val actualPath = paragraph.getPathForRange(0, 1)
+ val actualPath = paragraph.getPathForRange(0, 1)
- // Assert.
- assertThat(lineLeft, equalTo(lineRight))
- val diff = Path.combine(PathOperation.difference, expectedPath, actualPath).getBounds()
- assertThat(diff, equalTo(Rect.zero))
+ assertThat(lineLeft, equalTo(lineRight))
+ val diff = Path.combine(PathOperation.difference, expectedPath, actualPath).getBounds()
+ assertThat(diff, equalTo(Rect.zero))
+ }
}
@Test
fun testGetPathForRange_Unicode_2066_Return_Line_Segment() {
- // Setup tests.
- val text = "\u2066"
- val fontSize = 20f
- val paragraph = simpleParagraph(
+ withDensity(defaultDensity) {
+ val text = "\u2066"
+ val fontSize = 20f.sp
+ val fontSizeInPx = fontSize.toPx().value
+ val paragraph = simpleParagraph(
text = text,
fontFamily = fontFamilyMeasureFont,
fontSize = fontSize
- )
- paragraph.layout(ParagraphConstraints(width = Float.MAX_VALUE))
+ )
+ paragraph.layout(ParagraphConstraints(width = Float.MAX_VALUE))
- val paragraphImpl = paragraph.paragraphImpl
- val expectedPath = Path()
- val lineLeft = paragraphImpl.getLineLeft(0)
- val lineRight = paragraphImpl.getLineRight(0)
- expectedPath.addRect(Rect(lineLeft, 0f, lineRight, fontSize))
+ val paragraphImpl = paragraph.paragraphImpl
+ val expectedPath = Path()
+ val lineLeft = paragraphImpl.getLineLeft(0)
+ val lineRight = paragraphImpl.getLineRight(0)
+ expectedPath.addRect(Rect(lineLeft, 0f, lineRight, fontSizeInPx))
- // Run.
- val actualPath = paragraph.getPathForRange(0, 1)
+ val actualPath = paragraph.getPathForRange(0, 1)
- // Assert.
- assertThat(lineLeft, equalTo(lineRight))
- val diff = Path.combine(PathOperation.difference, expectedPath, actualPath).getBounds()
- assertThat(diff, equalTo(Rect.zero))
+ assertThat(lineLeft, equalTo(lineRight))
+ val diff = Path.combine(PathOperation.difference, expectedPath, actualPath).getBounds()
+ assertThat(diff, equalTo(Rect.zero))
+ }
}
@Test
fun testGetWordBoundary() {
val text = "abc def"
- val fontSize = 20f
val paragraph = simpleParagraph(
text = text,
fontFamily = fontFamilyMeasureFont,
- fontSize = fontSize
+ fontSize = 20.sp
)
paragraph.layout(ParagraphConstraints(width = Float.MAX_VALUE))
@@ -1774,11 +2030,10 @@
@Test
fun testGetWordBoundary_Bidi() {
val text = "abc \u05d0\u05d1\u05d2 def"
- val fontSize = 20f
val paragraph = simpleParagraph(
text = text,
fontFamily = fontFamilyMeasureFont,
- fontSize = fontSize
+ fontSize = 20.sp
)
paragraph.layout(ParagraphConstraints(width = Float.MAX_VALUE))
@@ -1791,18 +2046,46 @@
assertThat(resultHebrew.end, equalTo(text.indexOf('\u05d2') + 1))
}
+ @Test
+ fun test_finalFontSizeChangesWithDensity() {
+ val text = "a"
+ val fontSize = 20.sp
+ val densityMultiplier = 2f
+
+ val paragraph = simpleParagraph(
+ text = text,
+ textStyle = TextStyle(fontSize = fontSize),
+ density = Density(density = 1f, fontScale = 1f)
+ )
+ paragraph.layout(ParagraphConstraints(width = Float.MAX_VALUE))
+
+ val doubleFontSizeParagraph = simpleParagraph(
+ text = text,
+ textStyle = TextStyle(fontSize = fontSize),
+ density = Density(density = 1f, fontScale = densityMultiplier)
+ )
+ doubleFontSizeParagraph.layout(ParagraphConstraints(width = Float.MAX_VALUE))
+
+ assertThat(
+ doubleFontSizeParagraph.maxIntrinsicWidth,
+ equalTo(paragraph.maxIntrinsicWidth * densityMultiplier)
+ )
+ assertThat(doubleFontSizeParagraph.height, equalTo(paragraph.height * densityMultiplier))
+ }
+
private fun simpleParagraph(
text: String = "",
textIndent: TextIndent? = null,
textAlign: TextAlign? = null,
textDirection: TextDirection? = null,
- fontSize: Float? = null,
+ fontSize: Sp? = null,
maxLines: Int? = null,
lineHeight: Float? = null,
textStyles: List<AnnotatedString.Item<TextStyle>> = listOf(),
fontFamily: FontFamily = fontFamilyMeasureFont,
locale: Locale? = null,
- textStyle: TextStyle? = null
+ textStyle: TextStyle? = null,
+ density: Density? = null
): Paragraph {
return Paragraph(
text = text,
@@ -1818,7 +2101,8 @@
textDirection = textDirection,
maxLines = maxLines,
lineHeight = lineHeight
- )
+ ),
+ density = density ?: defaultDensity
)
}
}
diff --git a/ui/text/src/androidTest/java/androidx/ui/engine/text/platform/ParagraphAndroidTest.kt b/ui/text/src/androidTest/java/androidx/ui/engine/text/platform/ParagraphAndroidTest.kt
index bf4658e..ef72aa4 100644
--- a/ui/text/src/androidTest/java/androidx/ui/engine/text/platform/ParagraphAndroidTest.kt
+++ b/ui/text/src/androidTest/java/androidx/ui/engine/text/platform/ParagraphAndroidTest.kt
@@ -22,7 +22,10 @@
import androidx.text.style.SkewXSpan
import androidx.text.style.TypefaceSpan
import androidx.text.style.WordSpacingSpan
+import androidx.ui.core.Density
import androidx.ui.core.px
+import androidx.ui.core.sp
+import androidx.ui.core.withDensity
import androidx.ui.engine.geometry.Offset
import androidx.ui.engine.text.BaselineShift
import androidx.ui.engine.text.FontStyle
@@ -67,6 +70,7 @@
@SmallTest
class ParagraphAndroidTest {
private lateinit var fontFamily: FontFamily
+ private val defaultDensity = Density(density = 1f)
@Before
fun setup() {
@@ -80,31 +84,33 @@
@Test
fun draw_with_newline_and_line_break_default_values() {
- val fontSize = 50.0f
- for (text in arrayOf("abc\ndef", "\u05D0\u05D1\u05D2\n\u05D3\u05D4\u05D5")) {
- val paragraphAndroid = simpleParagraph(
- text = text,
- textStyle = TextStyle(
- fontSize = fontSize,
- fontFamily = fontFamily
+ withDensity(defaultDensity) {
+ val fontSize = 50.sp
+ for (text in arrayOf("abc\ndef", "\u05D0\u05D1\u05D2\n\u05D3\u05D4\u05D5")) {
+ val paragraphAndroid = simpleParagraph(
+ text = text,
+ textStyle = TextStyle(
+ fontSize = fontSize,
+ fontFamily = fontFamily
+ )
)
- )
- // 2 chars width
- paragraphAndroid.layout(width = 2 * fontSize)
+ // 2 chars width
+ paragraphAndroid.layout(width = 2 * fontSize.toPx().value)
- val textPaint = TextPaint(Paint.ANTI_ALIAS_FLAG)
- textPaint.textSize = fontSize
- textPaint.typeface = TypefaceAdapter().create(fontFamily)
+ val textPaint = TextPaint(Paint.ANTI_ALIAS_FLAG)
+ textPaint.textSize = fontSize.toPx().value
+ textPaint.typeface = TypefaceAdapter().create(fontFamily)
- val staticLayout = StaticLayoutCompat.Builder(
- text,
- textPaint,
- ceil(paragraphAndroid.width).toInt()
- )
- .setEllipsizedWidth(ceil(paragraphAndroid.width).toInt())
- .build()
- assertThat(paragraphAndroid.bitmap(), equalToBitmap(staticLayout.bitmap()))
+ val staticLayout = StaticLayoutCompat.Builder(
+ text,
+ textPaint,
+ ceil(paragraphAndroid.width).toInt()
+ )
+ .setEllipsizedWidth(ceil(paragraphAndroid.width).toInt())
+ .build()
+ assertThat(paragraphAndroid.bitmap(), equalToBitmap(staticLayout.bitmap()))
+ }
}
}
@@ -241,60 +247,66 @@
@Test
fun testAnnotatedString_setFontSizeOnWholeText() {
- val text = "abcde"
- val fontSize = 20.0f
- val paragraphWidth = text.length * fontSize
- val textStyle = TextStyle(fontSize = fontSize)
+ withDensity(defaultDensity) {
+ val text = "abcde"
+ val fontSize = 20.sp
+ val paragraphWidth = text.length * fontSize.toPx().value
+ val textStyle = TextStyle(fontSize = fontSize)
- val paragraph = simpleParagraph(
- text = text,
- textStyles = listOf(AnnotatedString.Item(textStyle, 0, text.length))
- )
- paragraph.layout(paragraphWidth)
+ val paragraph = simpleParagraph(
+ text = text,
+ textStyles = listOf(AnnotatedString.Item(textStyle, 0, text.length))
+ )
+ paragraph.layout(paragraphWidth)
- assertThat(paragraph.underlyingText, hasSpan(AbsoluteSizeSpan::class, 0, text.length))
+ assertThat(paragraph.underlyingText, hasSpan(AbsoluteSizeSpan::class, 0, text.length))
+ }
}
@Test
fun testAnnotatedString_setFontSizeOnPartText() {
- val text = "abcde"
- val fontSize = 20.0f
- val paragraphWidth = text.length * fontSize
- val textStyle = TextStyle(fontSize = fontSize)
+ withDensity(defaultDensity) {
+ val text = "abcde"
+ val fontSize = 20.sp
+ val paragraphWidth = text.length * fontSize.toPx().value
+ val textStyle = TextStyle(fontSize = fontSize)
- val paragraph = simpleParagraph(
- text = text,
- textStyles = listOf(AnnotatedString.Item(textStyle, 0, "abc".length))
- )
- paragraph.layout(paragraphWidth)
+ val paragraph = simpleParagraph(
+ text = text,
+ textStyles = listOf(AnnotatedString.Item(textStyle, 0, "abc".length))
+ )
+ paragraph.layout(paragraphWidth)
- assertThat(paragraph.underlyingText, hasSpan(AbsoluteSizeSpan::class, 0, "abc".length))
+ assertThat(paragraph.underlyingText, hasSpan(AbsoluteSizeSpan::class, 0, "abc".length))
+ }
}
@Test
fun testAnnotatedString_setFontSizeTwice_lastOneOverwrite() {
- val text = "abcde"
- val fontSize = 20.0f
- val fontSizeOverwrite = 30.0f
- val paragraphWidth = text.length * fontSizeOverwrite
- val textStyle = TextStyle(fontSize = fontSize)
- val textStyleOverwrite = TextStyle(fontSize = fontSizeOverwrite)
+ withDensity(defaultDensity) {
+ val text = "abcde"
+ val fontSize = 20.sp
+ val fontSizeOverwrite = 30.sp
+ val paragraphWidth = text.length * fontSizeOverwrite.toPx().value
+ val textStyle = TextStyle(fontSize = fontSize)
+ val textStyleOverwrite = TextStyle(fontSize = fontSizeOverwrite)
- val paragraph = simpleParagraph(
- text = text,
- textStyles = listOf(
- AnnotatedString.Item(textStyle, 0, text.length),
- AnnotatedString.Item(textStyleOverwrite, 0, "abc".length)
+ val paragraph = simpleParagraph(
+ text = text,
+ textStyles = listOf(
+ AnnotatedString.Item(textStyle, 0, text.length),
+ AnnotatedString.Item(textStyleOverwrite, 0, "abc".length)
+ )
)
- )
- paragraph.layout(paragraphWidth)
+ paragraph.layout(paragraphWidth)
- assertThat(paragraph.underlyingText, hasSpan(AbsoluteSizeSpan::class, 0, text.length))
- assertThat(paragraph.underlyingText, hasSpan(AbsoluteSizeSpan::class, 0, "abc".length))
- assertThat(
- paragraph.underlyingText,
- hasSpanOnTop(AbsoluteSizeSpan::class, 0, "abc".length)
- )
+ assertThat(paragraph.underlyingText, hasSpan(AbsoluteSizeSpan::class, 0, text.length))
+ assertThat(paragraph.underlyingText, hasSpan(AbsoluteSizeSpan::class, 0, "abc".length))
+ assertThat(
+ paragraph.underlyingText,
+ hasSpanOnTop(AbsoluteSizeSpan::class, 0, "abc".length)
+ )
+ }
}
@Test
@@ -1025,91 +1037,101 @@
@Test
fun testEllipsis_withMaxLineEqualsNull_doesNotEllipsis() {
- val text = "abc"
- val fontSize = 20f
- val paragraphWidth = (text.length - 1) * fontSize
- val paragraph = simpleParagraph(
- text = text,
- textStyle = TextStyle(
- fontFamily = fontFamily,
- fontSize = fontSize
- ),
- ellipsis = true
- )
- paragraph.layout(paragraphWidth)
- for (i in 0 until paragraph.lineCount) {
- assertFalse(paragraph.isEllipsisApplied(i))
+ withDensity(defaultDensity) {
+ val text = "abc"
+ val fontSize = 20.sp
+ val paragraphWidth = (text.length - 1) * fontSize.toPx().value
+ val paragraph = simpleParagraph(
+ text = text,
+ textStyle = TextStyle(
+ fontFamily = fontFamily,
+ fontSize = fontSize
+ ),
+ ellipsis = true
+ )
+ paragraph.layout(paragraphWidth)
+ for (i in 0 until paragraph.lineCount) {
+ assertFalse(paragraph.isEllipsisApplied(i))
+ }
}
}
@Test
fun testEllipsis_withMaxLinesLessThanTextLines_doesEllipsis() {
- val text = "abcde"
- val fontSize = 100f
- // Note that on API 21, if the next line only contains 1 character, ellipsis won't work
- val paragraphWidth = (text.length - 1.5f) * fontSize
- val paragraph = simpleParagraph(
- text = text,
- ellipsis = true,
- maxLines = 1,
- textStyle = TextStyle(
- fontFamily = fontFamily,
- fontSize = fontSize
+ withDensity(defaultDensity) {
+ val text = "abcde"
+ val fontSize = 100.sp
+ // Note that on API 21, if the next line only contains 1 character, ellipsis won't work
+ val paragraphWidth = (text.length - 1.5f) * fontSize.toPx().value
+ val paragraph = simpleParagraph(
+ text = text,
+ ellipsis = true,
+ maxLines = 1,
+ textStyle = TextStyle(
+ fontFamily = fontFamily,
+ fontSize = fontSize
+ )
)
- )
- paragraph.layout(paragraphWidth)
+ paragraph.layout(paragraphWidth)
- assertTrue(paragraph.isEllipsisApplied(0))
+ assertTrue(paragraph.isEllipsisApplied(0))
+ }
}
@Test
fun testEllipsis_withMaxLinesMoreThanTextLines_doesNotEllipsis() {
- val text = "abc"
- val fontSize = 100f
- val paragraphWidth = (text.length - 1) * fontSize
- val maxLines = ceil(text.length * fontSize / paragraphWidth).toInt()
- val paragraph = simpleParagraph(
- text = text,
- ellipsis = true,
- maxLines = maxLines,
- textStyle = TextStyle(
- fontFamily = fontFamily,
- fontSize = fontSize
+ withDensity(defaultDensity) {
+ val text = "abc"
+ val fontSize = 100.sp
+ val paragraphWidth = (text.length - 1) * fontSize.toPx().value
+ val maxLines = ceil(text.length * fontSize.toPx().value / paragraphWidth).toInt()
+ val paragraph = simpleParagraph(
+ text = text,
+ ellipsis = true,
+ maxLines = maxLines,
+ textStyle = TextStyle(
+ fontFamily = fontFamily,
+ fontSize = fontSize
+ )
)
- )
- paragraph.layout(paragraphWidth)
+ paragraph.layout(paragraphWidth)
- for (i in 0 until paragraph.lineCount) {
- assertFalse(paragraph.isEllipsisApplied(i))
+ for (i in 0 until paragraph.lineCount) {
+ assertFalse(paragraph.isEllipsisApplied(i))
+ }
}
}
@Test
fun testTextStyle_fontSize_appliedOnTextPaint() {
- val fontSize = 100f
- val paragraph = simpleParagraph(
- text = "",
- textStyle = TextStyle(fontSize = fontSize)
- )
- paragraph.layout(0f)
+ withDensity(defaultDensity) {
+ val fontSize = 100.sp
+ val paragraph = simpleParagraph(
+ text = "",
+ textStyle = TextStyle(fontSize = fontSize)
+ )
+ paragraph.layout(0f)
- assertThat(paragraph.textPaint.textSize, equalTo(fontSize))
+ assertThat(paragraph.textPaint.textSize, equalTo(fontSize.toPx().value))
+ }
}
@Test
fun testTextStyle_fontSizeScale_appliedOnTextPaint() {
- val fontSize = 100f
- val fontSizeScale = 2f
- val paragraph = simpleParagraph(
- text = "",
- textStyle = TextStyle(
- fontSize = fontSize,
- fontSizeScale = fontSizeScale
+ withDensity(defaultDensity) {
+ val fontSize = 100.sp
+ val fontSizeScale = 2f
+ val paragraph = simpleParagraph(
+ text = "",
+ textStyle = TextStyle(
+ fontSize = fontSize,
+ fontSizeScale = fontSizeScale
+ )
)
- )
- paragraph.layout(0f)
+ paragraph.layout(0f)
- assertThat(paragraph.textPaint.textSize, equalTo(fontSize * fontSizeScale))
+ assertThat(paragraph.textPaint.textSize, equalTo(fontSize.toPx().value * fontSizeScale))
+ }
}
@Test
@@ -1288,7 +1310,8 @@
textIndent = textIndent,
ellipsis = ellipsis,
maxLines = maxLines
- )
+ ),
+ density = Density(density = 1f)
)
}
}
\ No newline at end of file
diff --git a/ui/text/src/androidTest/java/androidx/ui/painting/TextPainterIntegrationTest.kt b/ui/text/src/androidTest/java/androidx/ui/painting/TextPainterIntegrationTest.kt
index e228eea..2179b8e 100644
--- a/ui/text/src/androidTest/java/androidx/ui/painting/TextPainterIntegrationTest.kt
+++ b/ui/text/src/androidTest/java/androidx/ui/painting/TextPainterIntegrationTest.kt
@@ -20,7 +20,10 @@
import androidx.test.filters.SmallTest
import androidx.test.platform.app.InstrumentationRegistry
import androidx.ui.core.Constraints
+import androidx.ui.core.Density
import androidx.ui.core.ipx
+import androidx.ui.core.sp
+import androidx.ui.core.withDensity
import androidx.ui.engine.geometry.Offset
import androidx.ui.engine.geometry.Rect
import androidx.ui.engine.geometry.Size
@@ -44,6 +47,7 @@
class TextPainterIntegrationTest {
private lateinit var fontFamily: FontFamily
+ private val density = Density(density = 1f)
@Before
fun setup() {
@@ -53,12 +57,14 @@
@Test
fun preferredLineHeight_style_set() {
- val fontSize = 20.0f
- val textStyle = TextStyle(fontSize = fontSize, fontFamily = fontFamily)
- val textPainter = TextPainter(style = textStyle)
- val preferredHeight = textPainter.preferredLineHeight
+ withDensity(density) {
+ val fontSize = 20.sp
+ val textStyle = TextStyle(fontSize = fontSize, fontFamily = fontFamily)
+ val textPainter = TextPainter(style = textStyle, density = density)
+ val preferredHeight = textPainter.preferredLineHeight
- assertThat(preferredHeight).isEqualTo(fontSize)
+ assertThat(preferredHeight).isEqualTo(fontSize.toPx().value)
+ }
}
// TODO(Migration/qqd): The default font size should be 14.0 but it returns 15.0. Need further
@@ -75,7 +81,7 @@
@Test
fun minIntrinsicWidth_getter() {
- val fontSize = 20.0f
+ val fontSize = 20.sp
val text = "Hello"
val textStyle = TextStyle(fontSize = fontSize, fontFamily = fontFamily)
val annotatedString = AnnotatedString(
@@ -84,7 +90,8 @@
)
val textPainter = TextPainter(
text = annotatedString,
- paragraphStyle = ParagraphStyle(textDirection = TextDirection.Rtl)
+ paragraphStyle = ParagraphStyle(textDirection = TextDirection.Rtl),
+ density = density
)
textPainter.layout(Constraints())
@@ -94,55 +101,61 @@
@Test
fun maxIntrinsicWidth_getter() {
- val fontSize = 20.0f
- val text = "Hello"
- val textStyle = TextStyle(fontSize = fontSize, fontFamily = fontFamily)
- val annotatedString = AnnotatedString(
- text = text,
- textStyles = listOf(AnnotatedString.Item(textStyle, 0, text.length))
- )
- val textPainter = TextPainter(
- text = annotatedString,
- paragraphStyle = ParagraphStyle(textDirection = TextDirection.Rtl)
- )
+ withDensity(density) {
+ val fontSize = 20.sp
+ val text = "Hello"
+ val textStyle = TextStyle(fontSize = fontSize, fontFamily = fontFamily)
+ val annotatedString = AnnotatedString(
+ text = text,
+ textStyles = listOf(AnnotatedString.Item(textStyle, 0, text.length))
+ )
+ val textPainter = TextPainter(
+ text = annotatedString,
+ paragraphStyle = ParagraphStyle(textDirection = TextDirection.Rtl),
+ density = density
+ )
- textPainter.layout(Constraints())
+ textPainter.layout(Constraints())
- assertThat(textPainter.maxIntrinsicWidth).isEqualTo(fontSize * text.length)
+ assertThat(textPainter.maxIntrinsicWidth).isEqualTo(fontSize.toPx().value * text.length)
+ }
}
@Test
fun width_getter() {
- val fontSize = 20.0f
- val text = "Hello"
- val textStyle = TextStyle(fontSize = fontSize, fontFamily = fontFamily)
- val annotatedString = AnnotatedString(
- text = text,
- textStyles = listOf(AnnotatedString.Item(textStyle, 0, text.length))
- )
- val textPainter = TextPainter(
- text = annotatedString,
- paragraphStyle = ParagraphStyle(textDirection = TextDirection.Rtl)
- )
+ withDensity(density) {
+ val fontSize = 20.sp
+ val text = "Hello"
+ val textStyle = TextStyle(fontSize = fontSize, fontFamily = fontFamily)
+ val annotatedString = AnnotatedString(
+ text = text,
+ textStyles = listOf(AnnotatedString.Item(textStyle, 0, text.length))
+ )
+ val textPainter = TextPainter(
+ text = annotatedString,
+ paragraphStyle = ParagraphStyle(textDirection = TextDirection.Rtl),
+ density = density
+ )
- textPainter.layout(Constraints(0.ipx, 200.ipx))
+ textPainter.layout(Constraints(0.ipx, 200.ipx))
- assertThat(textPainter.width).isEqualTo(fontSize * text.length)
+ assertThat(textPainter.width).isEqualTo(fontSize.toPx().value * text.length)
+ }
}
@Test
fun width_getter_with_small_width() {
- val fontSize = 20.0f
val text = "Hello"
val width = 80.ipx
- val textStyle = TextStyle(fontSize = fontSize, fontFamily = fontFamily)
+ val textStyle = TextStyle(fontSize = 20.sp, fontFamily = fontFamily)
val annotatedString = AnnotatedString(
text = text,
textStyles = listOf(AnnotatedString.Item(textStyle, 0, text.length))
)
val textPainter = TextPainter(
text = annotatedString,
- paragraphStyle = ParagraphStyle(textDirection = TextDirection.Rtl)
+ paragraphStyle = ParagraphStyle(textDirection = TextDirection.Rtl),
+ density = density
)
textPainter.layout(Constraints(maxWidth = width))
@@ -152,41 +165,52 @@
@Test
fun height_getter() {
- val fontSize = 20.0f
- val textStyle = TextStyle(fontSize = fontSize, fontFamily = fontFamily)
- val text = "hello"
- val annotatedString = AnnotatedString(
- text = text,
- textStyles = listOf(AnnotatedString.Item(textStyle, 0, text.length))
- )
- val textPainter = TextPainter(
- text = annotatedString,
- paragraphStyle = ParagraphStyle(textDirection = TextDirection.Rtl)
- )
+ withDensity(density) {
+ val fontSize = 20.sp
+ val textStyle = TextStyle(fontSize = fontSize, fontFamily = fontFamily)
+ val text = "hello"
+ val annotatedString = AnnotatedString(
+ text = text,
+ textStyles = listOf(AnnotatedString.Item(textStyle, 0, text.length))
+ )
+ val textPainter = TextPainter(
+ text = annotatedString,
+ paragraphStyle = ParagraphStyle(textDirection = TextDirection.Rtl),
+ density = density
+ )
- textPainter.layout(Constraints())
+ textPainter.layout(Constraints())
- assertThat(textPainter.height).isEqualTo(fontSize)
+ assertThat(textPainter.height).isEqualTo(fontSize.toPx().value)
+ }
}
@Test
fun size_getter() {
- val fontSize = 20.0f
- val text = "Hello"
- val textStyle = TextStyle(fontSize = fontSize, fontFamily = fontFamily)
- val annotatedString = AnnotatedString(
- text = text,
- textStyles = listOf(AnnotatedString.Item(textStyle, 0, text.length))
- )
- val textPainter = TextPainter(
- text = annotatedString,
- paragraphStyle = ParagraphStyle(textDirection = TextDirection.Rtl)
- )
+ withDensity(density) {
+ val fontSize = 20.sp
+ val fontSizeInPx = fontSize.toPx().value
+ val text = "Hello"
+ val textStyle = TextStyle(fontSize = fontSize, fontFamily = fontFamily)
+ val annotatedString = AnnotatedString(
+ text = text,
+ textStyles = listOf(AnnotatedString.Item(textStyle, 0, text.length))
+ )
+ val textPainter = TextPainter(
+ text = annotatedString,
+ paragraphStyle = ParagraphStyle(textDirection = TextDirection.Rtl),
+ density = density
+ )
- textPainter.layout(Constraints())
+ textPainter.layout(Constraints())
- assertThat(textPainter.size)
- .isEqualTo(Size(width = fontSize * text.length, height = fontSize))
+ assertThat(textPainter.size).isEqualTo(
+ Size(
+ width = fontSizeInPx * text.length,
+ height = fontSizeInPx
+ )
+ )
+ }
}
@Test
@@ -197,7 +221,8 @@
val textPainter = TextPainter(
text = annotatedString,
paragraphStyle = ParagraphStyle(textDirection = TextDirection.Rtl),
- maxLines = 2
+ maxLines = 2,
+ density = density
)
textPainter.layout(Constraints(0.ipx, 200.ipx))
@@ -211,7 +236,8 @@
val annotatedString = AnnotatedString(text = text)
val textPainter = TextPainter(text = annotatedString,
paragraphStyle = ParagraphStyle(textDirection = TextDirection.Rtl),
- maxLines = 2
+ maxLines = 2,
+ density = density
)
textPainter.layout(Constraints(0.ipx, 200.ipx))
@@ -223,7 +249,8 @@
fun layout_build_paragraph() {
val textPainter = TextPainter(
text = AnnotatedString(text = "Hello"),
- paragraphStyle = ParagraphStyle(textDirection = TextDirection.Ltr)
+ paragraphStyle = ParagraphStyle(textDirection = TextDirection.Ltr),
+ density = density
)
textPainter.layout(Constraints(0.ipx, 20.ipx))
@@ -233,13 +260,12 @@
@Test
fun getPositionForOffset_First_Character() {
- val fontSize = 20.0f
val text = "Hello"
val annotatedString = AnnotatedString(
text = text,
textStyles = listOf(
AnnotatedString.Item(
- TextStyle(fontSize = fontSize, fontFamily = fontFamily),
+ TextStyle(fontSize = 20.sp, fontFamily = fontFamily),
0,
text.length
)
@@ -247,7 +273,8 @@
)
val textPainter = TextPainter(
text = annotatedString,
- paragraphStyle = ParagraphStyle(textDirection = TextDirection.Ltr)
+ paragraphStyle = ParagraphStyle(textDirection = TextDirection.Ltr),
+ density = density
)
textPainter.layout(Constraints())
@@ -258,44 +285,47 @@
@Test
fun getPositionForOffset_other_Character() {
- val fontSize = 20.0f
- val characterIndex = 2 // Start from 0.
- val text = "Hello"
- val annotatedString = AnnotatedString(
- text = text,
- textStyles = listOf(
- AnnotatedString.Item(
- TextStyle(fontSize = fontSize, fontFamily = fontFamily),
- 0,
- text.length
+ withDensity(density) {
+ val fontSize = 20.sp
+ val characterIndex = 2 // Start from 0.
+ val text = "Hello"
+ val annotatedString = AnnotatedString(
+ text = text,
+ textStyles = listOf(
+ AnnotatedString.Item(
+ TextStyle(fontSize = fontSize, fontFamily = fontFamily),
+ 0,
+ text.length
+ )
)
)
- )
- val textPainter = TextPainter(
- text = annotatedString,
- paragraphStyle = ParagraphStyle(textDirection = TextDirection.Ltr)
- )
- textPainter.layout(Constraints())
+ val textPainter = TextPainter(
+ text = annotatedString,
+ paragraphStyle = ParagraphStyle(textDirection = TextDirection.Ltr),
+ density = density
+ )
+ textPainter.layout(Constraints())
- val selection = textPainter.getPositionForOffset(
- offset = Offset(dx = fontSize * characterIndex + 1f, dy = 0f)
- )
+ val selection = textPainter.getPositionForOffset(
+ offset = Offset(dx = fontSize.toPx().value * characterIndex + 1f, dy = 0f)
+ )
- assertThat(selection).isEqualTo(characterIndex)
+ assertThat(selection).isEqualTo(characterIndex)
+ }
}
@Test
fun hasOverflowShaderFalse() {
- val fontSize = 20.0f
val text = "Hello"
- val textStyle = TextStyle(fontSize = fontSize, fontFamily = fontFamily)
+ val textStyle = TextStyle(fontSize = 20.sp, fontFamily = fontFamily)
val annotatedString = AnnotatedString(
text = text,
textStyles = listOf(AnnotatedString.Item(textStyle, 0, text.length))
)
val textPainter = TextPainter(
text = annotatedString,
- paragraphStyle = ParagraphStyle(textDirection = TextDirection.Ltr)
+ paragraphStyle = ParagraphStyle(textDirection = TextDirection.Ltr),
+ density = density
)
textPainter.layout(Constraints())
@@ -305,12 +335,11 @@
@Test
fun hasOverflowShaderFadeHorizontallyTrue() {
- val fontSize = 20.0f
var text = ""
for (i in 1..15) {
text = text + "Hello World"
}
- val textStyle = TextStyle(fontSize = fontSize, fontFamily = fontFamily)
+ val textStyle = TextStyle(fontSize = 20.sp, fontFamily = fontFamily)
val annotatedString = AnnotatedString(
text = text,
textStyles = listOf(AnnotatedString.Item(textStyle, 0, text.length))
@@ -320,7 +349,8 @@
paragraphStyle = ParagraphStyle(textDirection = TextDirection.Ltr),
overflow = TextOverflow.Fade,
softWrap = false,
- maxLines = 1
+ maxLines = 1,
+ density = density
)
textPainter.layout(Constraints(maxWidth = 100.ipx))
@@ -330,12 +360,11 @@
@Test
fun hasOverflowShaderFadeVerticallyTrue() {
- val fontSize = 20.0f
var text = ""
for (i in 1..30) {
text = text + "Hello World"
}
- val textStyle = TextStyle(fontSize = fontSize, fontFamily = fontFamily)
+ val textStyle = TextStyle(fontSize = 20.sp, fontFamily = fontFamily)
val annotatedString = AnnotatedString(
text = text,
textStyles = listOf(AnnotatedString.Item(textStyle, 0, text.length))
@@ -344,7 +373,8 @@
text = annotatedString,
paragraphStyle = ParagraphStyle(textDirection = TextDirection.Ltr),
overflow = TextOverflow.Fade,
- maxLines = 2
+ maxLines = 2,
+ density = density
)
textPainter.layout(Constraints(maxWidth = 100.ipx))
@@ -354,251 +384,268 @@
@Test
fun testBackgroundPaint_paint_wrap_multiLines() {
- // Setup test.
- val fontSize = 20.0f
- val text = "HelloHello"
- val textStyle = TextStyle(fontSize = fontSize, fontFamily = fontFamily)
- val annotatedString = AnnotatedString(
- text = text,
- textStyles = listOf(AnnotatedString.Item(textStyle, 0, text.length))
- )
- val textPainter = TextPainter(
- text = annotatedString,
- paragraphStyle = ParagraphStyle(textDirection = TextDirection.Ltr)
- )
- textPainter.layout(Constraints(maxWidth = 120.ipx))
+ withDensity(density) {
+ // Setup test.
+ val fontSize = 20.sp
+ val fontSizeInPx = fontSize.toPx().value
+ val text = "HelloHello"
+ val textStyle = TextStyle(fontSize = fontSize, fontFamily = fontFamily)
+ val annotatedString = AnnotatedString(
+ text = text,
+ textStyles = listOf(AnnotatedString.Item(textStyle, 0, text.length))
+ )
+ val textPainter = TextPainter(
+ text = annotatedString,
+ paragraphStyle = ParagraphStyle(textDirection = TextDirection.Ltr),
+ density = density
+ )
+ textPainter.layout(Constraints(maxWidth = 120.ipx))
- val expectedBitmap = Bitmap.createBitmap(
- ceil(textPainter.width).toInt(),
- ceil(textPainter.height).toInt(),
- Bitmap.Config.ARGB_8888
- )
- val expectedCanvas = Canvas(android.graphics.Canvas(expectedBitmap))
- val expectedPaint = Paint()
- val defaultSelectionColor = Color(0x6633B5E5)
- expectedPaint.color = defaultSelectionColor
+ val expectedBitmap = Bitmap.createBitmap(
+ ceil(textPainter.width).toInt(),
+ ceil(textPainter.height).toInt(),
+ Bitmap.Config.ARGB_8888
+ )
+ val expectedCanvas = Canvas(android.graphics.Canvas(expectedBitmap))
+ val expectedPaint = Paint()
+ val defaultSelectionColor = Color(0x6633B5E5)
+ expectedPaint.color = defaultSelectionColor
- val firstLineLeft = textPainter.paragraph?.getLineLeft(0)
- val secondLineLeft = textPainter.paragraph?.getLineLeft(1)
- val firstLineRight = textPainter.paragraph?.getLineRight(0)
- val secondLineRight = textPainter.paragraph?.getLineRight(1)
- expectedCanvas.drawRect(
- Rect(firstLineLeft!!, 0f, firstLineRight!!, fontSize),
- expectedPaint
- )
- expectedCanvas.drawRect(
- Rect(
- secondLineLeft!!,
- fontSize,
- secondLineRight!!,
- textPainter.paragraph!!.height
- ),
- expectedPaint
- )
+ val firstLineLeft = textPainter.paragraph?.getLineLeft(0)
+ val secondLineLeft = textPainter.paragraph?.getLineLeft(1)
+ val firstLineRight = textPainter.paragraph?.getLineRight(0)
+ val secondLineRight = textPainter.paragraph?.getLineRight(1)
+ expectedCanvas.drawRect(
+ Rect(firstLineLeft!!, 0f, firstLineRight!!, fontSizeInPx),
+ expectedPaint
+ )
+ expectedCanvas.drawRect(
+ Rect(
+ secondLineLeft!!,
+ fontSizeInPx,
+ secondLineRight!!,
+ textPainter.paragraph!!.height
+ ),
+ expectedPaint
+ )
- val actualBitmap = Bitmap.createBitmap(
- ceil(textPainter.width).toInt(),
- ceil(textPainter.height).toInt(),
- Bitmap.Config.ARGB_8888
- )
- val actualCanvas = Canvas(android.graphics.Canvas(actualBitmap))
+ val actualBitmap = Bitmap.createBitmap(
+ ceil(textPainter.width).toInt(),
+ ceil(textPainter.height).toInt(),
+ Bitmap.Config.ARGB_8888
+ )
+ val actualCanvas = Canvas(android.graphics.Canvas(actualBitmap))
- // Run.
- // Select all.
- textPainter.paintBackground(
- start = 0,
- end = text.length,
- color = defaultSelectionColor,
- canvas = actualCanvas,
- offset = Offset.zero
- )
+ // Run.
+ // Select all.
+ textPainter.paintBackground(
+ start = 0,
+ end = text.length,
+ color = defaultSelectionColor,
+ canvas = actualCanvas,
+ offset = Offset.zero
+ )
- // Assert.
- Assert.assertThat(actualBitmap, equalToBitmap(expectedBitmap))
+ // Assert.
+ Assert.assertThat(actualBitmap, equalToBitmap(expectedBitmap))
+ }
}
@Test
fun testBackgroundPaint_paint_with_default_color() {
- // Setup test.
- val selectionStart = 0
- val selectionEnd = 3
- val fontSize = 20.0f
- val text = "Hello"
- val textStyle = TextStyle(fontSize = fontSize, fontFamily = fontFamily)
- val annotatedString = AnnotatedString(
- text = text,
- textStyles = listOf(AnnotatedString.Item(textStyle, 0, text.length))
- )
- val textPainter = TextPainter(
- text = annotatedString,
- paragraphStyle = ParagraphStyle(textDirection = TextDirection.Ltr)
- )
- textPainter.layout(Constraints())
+ withDensity(density) {
+ // Setup test.
+ val selectionStart = 0
+ val selectionEnd = 3
+ val fontSize = 20.sp
+ val fontSizeInPx = fontSize.toPx().value
+ val text = "Hello"
+ val textStyle = TextStyle(fontSize = fontSize, fontFamily = fontFamily)
+ val annotatedString = AnnotatedString(
+ text = text,
+ textStyles = listOf(AnnotatedString.Item(textStyle, 0, text.length))
+ )
+ val textPainter = TextPainter(
+ text = annotatedString,
+ paragraphStyle = ParagraphStyle(textDirection = TextDirection.Ltr),
+ density = density
+ )
+ textPainter.layout(Constraints())
- val expectedBitmap = Bitmap.createBitmap(
- ceil(textPainter.width).toInt(),
- ceil(textPainter.height).toInt(),
- Bitmap.Config.ARGB_8888
- )
- val expectedCanvas = Canvas(android.graphics.Canvas(expectedBitmap))
- val expectedPaint = Paint()
- val defaultSelectionColor = Color(0x6633B5E5)
- expectedPaint.color = defaultSelectionColor
- expectedCanvas.drawRect(
- Rect(
- left = 0f,
- top = 0f,
- right = fontSize * (selectionEnd - selectionStart),
- bottom = fontSize
- ),
- expectedPaint
- )
+ val expectedBitmap = Bitmap.createBitmap(
+ ceil(textPainter.width).toInt(),
+ ceil(textPainter.height).toInt(),
+ Bitmap.Config.ARGB_8888
+ )
+ val expectedCanvas = Canvas(android.graphics.Canvas(expectedBitmap))
+ val expectedPaint = Paint()
+ val defaultSelectionColor = Color(0x6633B5E5)
+ expectedPaint.color = defaultSelectionColor
+ expectedCanvas.drawRect(
+ Rect(
+ left = 0f,
+ top = 0f,
+ right = fontSizeInPx * (selectionEnd - selectionStart),
+ bottom = fontSizeInPx
+ ),
+ expectedPaint
+ )
- val actualBitmap = Bitmap.createBitmap(
- ceil(textPainter.width).toInt(),
- ceil(textPainter.height).toInt(),
- Bitmap.Config.ARGB_8888
- )
- val actualCanvas = Canvas(android.graphics.Canvas(actualBitmap))
+ val actualBitmap = Bitmap.createBitmap(
+ ceil(textPainter.width).toInt(),
+ ceil(textPainter.height).toInt(),
+ Bitmap.Config.ARGB_8888
+ )
+ val actualCanvas = Canvas(android.graphics.Canvas(actualBitmap))
- // Run.
- textPainter.paintBackground(
- start = selectionStart,
- end = selectionEnd,
- color = defaultSelectionColor,
- canvas = actualCanvas,
- offset = Offset.zero
- )
+ // Run.
+ textPainter.paintBackground(
+ start = selectionStart,
+ end = selectionEnd,
+ color = defaultSelectionColor,
+ canvas = actualCanvas,
+ offset = Offset.zero
+ )
- // Assert
- Assert.assertThat(actualBitmap, equalToBitmap(expectedBitmap))
+ // Assert
+ Assert.assertThat(actualBitmap, equalToBitmap(expectedBitmap))
+ }
}
@Test
fun testBackgroundPaint_paint_with_default_color_bidi() {
- // Setup test.
- val textLTR = "Hello"
- // From right to left: שלום
- val textRTL = "\u05e9\u05dc\u05d5\u05dd"
- val text = textLTR + textRTL
- val selectionLTRStart = 2
- val selectionRTLEnd = 2
- val fontSize = 20.0f
- val textStyle = TextStyle(fontSize = fontSize, fontFamily = fontFamily)
- val annotatedString = AnnotatedString(
- text = text,
- textStyles = listOf(AnnotatedString.Item(textStyle, 0, text.length))
- )
- val textPainter = TextPainter(text = annotatedString,
- paragraphStyle = ParagraphStyle(textDirection = TextDirection.Ltr)
- )
- textPainter.layout(Constraints())
+ withDensity(density) {
+ // Setup test.
+ val textLTR = "Hello"
+ // From right to left: שלום
+ val textRTL = "\u05e9\u05dc\u05d5\u05dd"
+ val text = textLTR + textRTL
+ val selectionLTRStart = 2
+ val selectionRTLEnd = 2
+ val fontSize = 20.sp
+ val fontSizeInPx = fontSize.toPx().value
+ val textStyle = TextStyle(fontSize = fontSize, fontFamily = fontFamily)
+ val annotatedString = AnnotatedString(
+ text = text,
+ textStyles = listOf(AnnotatedString.Item(textStyle, 0, text.length))
+ )
+ val textPainter = TextPainter(
+ text = annotatedString,
+ paragraphStyle = ParagraphStyle(textDirection = TextDirection.Ltr),
+ density = density
+ )
+ textPainter.layout(Constraints())
- val expectedBitmap = Bitmap.createBitmap(
- ceil(textPainter.width).toInt(),
- ceil(textPainter.height).toInt(),
- Bitmap.Config.ARGB_8888
- )
- val expectedCanvas = Canvas(android.graphics.Canvas(expectedBitmap))
- val expectedPaint = Paint()
- val defaultSelectionColor = Color(0x6633B5E5)
- expectedPaint.color = defaultSelectionColor
- // Select "llo".
- expectedCanvas.drawRect(
- Rect(
- left = fontSize * selectionLTRStart,
- top = 0f,
- right = textLTR.length * fontSize,
- bottom = fontSize
- ),
- expectedPaint
- )
+ val expectedBitmap = Bitmap.createBitmap(
+ ceil(textPainter.width).toInt(),
+ ceil(textPainter.height).toInt(),
+ Bitmap.Config.ARGB_8888
+ )
+ val expectedCanvas = Canvas(android.graphics.Canvas(expectedBitmap))
+ val expectedPaint = Paint()
+ val defaultSelectionColor = Color(0x6633B5E5)
+ expectedPaint.color = defaultSelectionColor
+ // Select "llo".
+ expectedCanvas.drawRect(
+ Rect(
+ left = fontSizeInPx * selectionLTRStart,
+ top = 0f,
+ right = textLTR.length * fontSizeInPx,
+ bottom = fontSizeInPx
+ ),
+ expectedPaint
+ )
- // Select "של"
- expectedCanvas.drawRect(
- Rect(
- left = (textLTR.length + textRTL.length - selectionRTLEnd) * fontSize,
- top = 0f,
- right = (textLTR.length + textRTL.length) * fontSize,
- bottom = fontSize
- ),
- expectedPaint
- )
+ // Select "של"
+ expectedCanvas.drawRect(
+ Rect(
+ left = (textLTR.length + textRTL.length - selectionRTLEnd) * fontSizeInPx,
+ top = 0f,
+ right = (textLTR.length + textRTL.length) * fontSizeInPx,
+ bottom = fontSizeInPx
+ ),
+ expectedPaint
+ )
- val actualBitmap = Bitmap.createBitmap(
- ceil(textPainter.width).toInt(),
- ceil(textPainter.height).toInt(),
- Bitmap.Config.ARGB_8888
- )
- val actualCanvas = Canvas(android.graphics.Canvas(actualBitmap))
+ val actualBitmap = Bitmap.createBitmap(
+ ceil(textPainter.width).toInt(),
+ ceil(textPainter.height).toInt(),
+ Bitmap.Config.ARGB_8888
+ )
+ val actualCanvas = Canvas(android.graphics.Canvas(actualBitmap))
- // Run.
- textPainter.paintBackground(
- start = selectionLTRStart,
- end = textLTR.length + selectionRTLEnd,
- color = defaultSelectionColor,
- canvas = actualCanvas,
- offset = Offset.zero
- )
+ // Run.
+ textPainter.paintBackground(
+ start = selectionLTRStart,
+ end = textLTR.length + selectionRTLEnd,
+ color = defaultSelectionColor,
+ canvas = actualCanvas,
+ offset = Offset.zero
+ )
- // Assert
- Assert.assertThat(actualBitmap, equalToBitmap(expectedBitmap))
+ // Assert
+ Assert.assertThat(actualBitmap, equalToBitmap(expectedBitmap))
+ }
}
@Test
fun testBackgroundPaint_paint_with_customized_color() {
- // Setup test.
- val selectionStart = 0
- val selectionEnd = 3
- val fontSize = 20.0f
- val text = "Hello"
- val textStyle = TextStyle(fontSize = fontSize, fontFamily = fontFamily)
- val annotatedString = AnnotatedString(
- text = text,
- textStyles = listOf(AnnotatedString.Item(textStyle, 0, text.length))
- )
- val selectionColor = Color(0x66AABB33)
- val textPainter = TextPainter(
- text = annotatedString,
- paragraphStyle = ParagraphStyle(textDirection = TextDirection.Ltr)
- )
- textPainter.layout(Constraints())
+ withDensity(density) {
+ // Setup test.
+ val selectionStart = 0
+ val selectionEnd = 3
+ val fontSize = 20.sp
+ val fontSizeInPx = fontSize.toPx().value
+ val text = "Hello"
+ val textStyle = TextStyle(fontSize = fontSize, fontFamily = fontFamily)
+ val annotatedString = AnnotatedString(
+ text = text,
+ textStyles = listOf(AnnotatedString.Item(textStyle, 0, text.length))
+ )
+ val selectionColor = Color(0x66AABB33)
+ val textPainter = TextPainter(
+ text = annotatedString,
+ paragraphStyle = ParagraphStyle(textDirection = TextDirection.Ltr),
+ density = density
+ )
+ textPainter.layout(Constraints())
- val expectedBitmap = Bitmap.createBitmap(
- ceil(textPainter.width).toInt(),
- ceil(textPainter.height).toInt(),
- Bitmap.Config.ARGB_8888
- )
- val expectedCanvas = Canvas(android.graphics.Canvas(expectedBitmap))
- val expectedPaint = Paint()
- expectedPaint.color = selectionColor
- expectedCanvas.drawRect(
- Rect(
- left = 0f,
- top = 0f,
- right = fontSize * (selectionEnd - selectionStart),
- bottom = fontSize
- ),
- expectedPaint
- )
+ val expectedBitmap = Bitmap.createBitmap(
+ ceil(textPainter.width).toInt(),
+ ceil(textPainter.height).toInt(),
+ Bitmap.Config.ARGB_8888
+ )
+ val expectedCanvas = Canvas(android.graphics.Canvas(expectedBitmap))
+ val expectedPaint = Paint()
+ expectedPaint.color = selectionColor
+ expectedCanvas.drawRect(
+ Rect(
+ left = 0f,
+ top = 0f,
+ right = fontSizeInPx * (selectionEnd - selectionStart),
+ bottom = fontSizeInPx
+ ),
+ expectedPaint
+ )
- val actualBitmap = Bitmap.createBitmap(
- ceil(textPainter.width).toInt(),
- ceil(textPainter.height).toInt(),
- Bitmap.Config.ARGB_8888
- )
- val actualCanvas = Canvas(android.graphics.Canvas(actualBitmap))
+ val actualBitmap = Bitmap.createBitmap(
+ ceil(textPainter.width).toInt(),
+ ceil(textPainter.height).toInt(),
+ Bitmap.Config.ARGB_8888
+ )
+ val actualCanvas = Canvas(android.graphics.Canvas(actualBitmap))
- // Run.
- textPainter.paintBackground(
- start = selectionStart,
- end = selectionEnd,
- color = selectionColor,
- canvas = actualCanvas,
- offset = Offset.zero
- )
+ // Run.
+ textPainter.paintBackground(
+ start = selectionStart,
+ end = selectionEnd,
+ color = selectionColor,
+ canvas = actualCanvas,
+ offset = Offset.zero
+ )
- // Assert
- Assert.assertThat(actualBitmap, equalToBitmap(expectedBitmap))
+ // Assert
+ Assert.assertThat(actualBitmap, equalToBitmap(expectedBitmap))
+ }
}
-}
+}
\ No newline at end of file