Skip to content

Commit

Permalink
convert CustomLineHeightSpanTest to kotlin
Browse files Browse the repository at this point in the history
  • Loading branch information
tarunrajput committed Aug 8, 2023
1 parent 798811c commit d5ce7b4
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 115 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

package com.facebook.react.views.text

import android.graphics.Paint
import org.assertj.core.api.Assertions.assertThat
import org.junit.Test
import org.junit.runner.RunWith
import org.powermock.core.classloader.annotations.PowerMockIgnore
import org.robolectric.RobolectricTestRunner

@RunWith(RobolectricTestRunner::class)
@PowerMockIgnore("org.mockito.*", "org.robolectric.*", "androidx.*", "android.*")
class CustomLineHeightSpanTest {

@Test
fun evenLineHeightShouldIncreaseAllMetricsProportionally() {
val customLineHeightSpan = CustomLineHeightSpan(22f)
val fm =
Paint.FontMetricsInt().apply {
top = -10
ascent = -5
descent = 5
bottom = 10
}
customLineHeightSpan.chooseHeight("Hi", 0, 2, 0, 0, fm)
assertThat(fm.top).isEqualTo(-11)
assertThat(fm.ascent).isEqualTo(-11)
assertThat(fm.descent).isEqualTo(11)
assertThat(fm.bottom).isEqualTo(11)
assertThat(fm.bottom - fm.top).isEqualTo(22)
}

@Test
fun oddLineHeightShouldAlsoWork() {
val customLineHeightSpan = CustomLineHeightSpan(23f)
val fm =
Paint.FontMetricsInt().apply {
top = -10
ascent = -5
descent = 5
bottom = 10
}
customLineHeightSpan.chooseHeight("Hi", 0, 2, 0, 0, fm)
assertThat(fm.descent - fm.ascent).isEqualTo(23)
assertThat(fm.bottom - fm.top).isEqualTo(23)
}

@Test
fun shouldReduceTopFirst() {
val customLineHeightSpan = CustomLineHeightSpan(19f)
val fm =
Paint.FontMetricsInt().apply {
top = -10
ascent = -5
descent = 5
bottom = 10
}
customLineHeightSpan.chooseHeight("Hi", 0, 2, 0, 0, fm)
assertThat(fm.top).isEqualTo(-9)
assertThat(fm.ascent).isEqualTo(-5)
assertThat(fm.descent).isEqualTo(5)
assertThat(fm.bottom).isEqualTo(10)
}

@Test
fun shouldReduceBottomSecond() {
val customLineHeightSpan = CustomLineHeightSpan(14f)
val fm =
Paint.FontMetricsInt().apply {
top = -10
ascent = -5
descent = 5
bottom = 10
}
customLineHeightSpan.chooseHeight("Hi", 0, 2, 0, 0, fm)
assertThat(fm.top).isEqualTo(-5)
assertThat(fm.ascent).isEqualTo(-5)
assertThat(fm.descent).isEqualTo(5)
assertThat(fm.bottom).isEqualTo(9)
}

@Test
fun shouldReduceAscentThird() {
val customLineHeightSpan = CustomLineHeightSpan(9f)
val fm =
Paint.FontMetricsInt().apply {
top = -10
ascent = -5
descent = 5
bottom = 10
}
customLineHeightSpan.chooseHeight("Hi", 0, 2, 0, 0, fm)
assertThat(fm.top).isEqualTo(-4)
assertThat(fm.ascent).isEqualTo(-4)
assertThat(fm.descent).isEqualTo(5)
assertThat(fm.bottom).isEqualTo(5)
}

@Test
fun shouldReduceDescentLast() {
val customLineHeightSpan = CustomLineHeightSpan(4f)
val fm =
Paint.FontMetricsInt().apply {
top = -10
ascent = -5
descent = 5
bottom = 10
}
customLineHeightSpan.chooseHeight("Hi", 0, 2, 0, 0, fm)
assertThat(fm.top).isEqualTo(0)
assertThat(fm.ascent).isEqualTo(0)
assertThat(fm.descent).isEqualTo(4)
assertThat(fm.bottom).isEqualTo(4)
}
}

0 comments on commit d5ce7b4

Please sign in to comment.