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 9b69f4a
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 115 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/*
* 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()
fm.top = -10
fm.ascent = -5
fm.descent = 5
fm.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()
fm.top = -10
fm.ascent = -5
fm.descent = 5
fm.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()
fm.top = -10
fm.ascent = -5
fm.descent = 5
fm.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()
fm.top = -10
fm.ascent = -5
fm.descent = 5
fm.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()
fm.top = -10
fm.ascent = -5
fm.descent = 5
fm.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()
fm.top = -10
fm.ascent = -5
fm.descent = 5
fm.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 9b69f4a

Please sign in to comment.