From 9b69f4acd475aa685bfe3ac6935ebc8eb73e0dac Mon Sep 17 00:00:00 2001 From: tarunrajput Date: Tue, 8 Aug 2023 16:46:53 +0530 Subject: [PATCH] convert CustomLineHeightSpanTest to kotlin --- .../views/text/CustomLineHeightSpanTest.java | 115 ------------------ .../views/text/CustomLineHeightSpanTest.kt | 109 +++++++++++++++++ 2 files changed, 109 insertions(+), 115 deletions(-) delete mode 100644 packages/react-native/ReactAndroid/src/test/java/com/facebook/react/views/text/CustomLineHeightSpanTest.java create mode 100644 packages/react-native/ReactAndroid/src/test/java/com/facebook/react/views/text/CustomLineHeightSpanTest.kt diff --git a/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/views/text/CustomLineHeightSpanTest.java b/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/views/text/CustomLineHeightSpanTest.java deleted file mode 100644 index c8a5839e4ace43..00000000000000 --- a/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/views/text/CustomLineHeightSpanTest.java +++ /dev/null @@ -1,115 +0,0 @@ -/* - * 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 static org.assertj.core.api.Assertions.assertThat; - -import android.graphics.Paint; -import org.junit.Ignore; -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.*"}) -@Ignore // TODO T110934492 -public class CustomLineHeightSpanTest { - - @Test - public void evenLineHeightShouldIncreaseAllMetricsProportionally() { - CustomLineHeightSpan customLineHeightSpan = new CustomLineHeightSpan(22); - Paint.FontMetricsInt fm = new Paint.FontMetricsInt(); - fm.top = -10; - fm.ascent = -5; - fm.descent = 5; - fm.bottom = 10; - customLineHeightSpan.chooseHeight("Hi", 0, 2, 0, 0, fm); - // Since line height is even it should be equally added to top and bottom. - 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 - public void oddLineHeightShouldAlsoWork() { - CustomLineHeightSpan customLineHeightSpan = new CustomLineHeightSpan(23); - Paint.FontMetricsInt fm = new Paint.FontMetricsInt(); - fm.top = -10; - fm.ascent = -5; - fm.descent = 5; - fm.bottom = 10; - customLineHeightSpan.chooseHeight("Hi", 0, 2, 0, 0, fm); - // Only test that the sum is correct so the implementation - // is free to add the odd value either on top or bottom. - assertThat(fm.descent - fm.ascent).isEqualTo(23); - assertThat(fm.bottom - fm.top).isEqualTo(23); - } - - @Test - public void shouldReduceTopFirst() { - CustomLineHeightSpan customLineHeightSpan = new CustomLineHeightSpan(19); - Paint.FontMetricsInt fm = new 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 - public void shouldReduceBottomSecond() { - CustomLineHeightSpan customLineHeightSpan = new CustomLineHeightSpan(14); - Paint.FontMetricsInt fm = new 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 - public void shouldReduceAscentThird() { - CustomLineHeightSpan customLineHeightSpan = new CustomLineHeightSpan(9); - Paint.FontMetricsInt fm = new 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 - public void shouldReduceDescentLast() { - CustomLineHeightSpan customLineHeightSpan = new CustomLineHeightSpan(4); - Paint.FontMetricsInt fm = new 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); - } -} diff --git a/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/views/text/CustomLineHeightSpanTest.kt b/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/views/text/CustomLineHeightSpanTest.kt new file mode 100644 index 00000000000000..a8d245ee6a9694 --- /dev/null +++ b/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/views/text/CustomLineHeightSpanTest.kt @@ -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) + } +}