From 4343144c81955f50aa1bfc573dffa7d7a6ac1805 Mon Sep 17 00:00:00 2001 From: tarunrajput Date: Tue, 8 Aug 2023 10:49:15 -0700 Subject: [PATCH] Migrate CustomLineHeightSpanTest to kotlin (#38847) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Summary: Migrate `CustomLineHeightSpanTest` to kotlin as part of ☂️ https://github.com/facebook/react-native/issues/38825 ## Changelog: [Internal] [Changed] - Migrate CustomLineHeightSpanTest to kotlin Pull Request resolved: https://github.com/facebook/react-native/pull/38847 Test Plan: ``` ./gradlew :packages:react-native:ReactAndroid:test ``` Reviewed By: rshest Differential Revision: D48155397 Pulled By: cortinico fbshipit-source-id: bbaa7d08a84c609bc64b48a08e1b91078ce6ef23 --- .../views/text/CustomLineHeightSpanTest.java | 115 ----------------- .../views/text/CustomLineHeightSpanTest.kt | 121 ++++++++++++++++++ 2 files changed, 121 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..0721e232ac715c --- /dev/null +++ b/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/views/text/CustomLineHeightSpanTest.kt @@ -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) + } +}