Skip to content

Commit

Permalink
Migrate CustomLineHeightSpanTest to kotlin (#38847)
Browse files Browse the repository at this point in the history
Summary:
Migrate `CustomLineHeightSpanTest` to kotlin as part of ☂️ #38825

## Changelog:

<!-- Help reviewers and the release process by writing your own changelog entry.

Pick one each for the category and type tags:

[ANDROID|GENERAL|IOS|INTERNAL] [BREAKING|ADDED|CHANGED|DEPRECATED|REMOVED|FIXED|SECURITY] - Message

For more details, see:
https://reactnative.dev/contributing/changelogs-in-pull-requests
-->

[Internal] [Changed] - Migrate CustomLineHeightSpanTest to kotlin

Pull Request resolved: #38847

Test Plan:
```
./gradlew :packages:react-native:ReactAndroid:test
```

Reviewed By: rshest

Differential Revision: D48155397

Pulled By: cortinico

fbshipit-source-id: bbaa7d08a84c609bc64b48a08e1b91078ce6ef23
  • Loading branch information
tarunrajput authored and facebook-github-bot committed Aug 8, 2023
1 parent 798811c commit 4343144
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 4343144

Please sign in to comment.