|
| 1 | +package com.facebook.react.views.text; |
| 2 | + |
| 3 | +import android.graphics.Paint; |
| 4 | + |
| 5 | +import static org.fest.assertions.api.Assertions.assertThat; |
| 6 | +import org.junit.Test; |
| 7 | +import org.junit.runner.RunWith; |
| 8 | +import org.powermock.core.classloader.annotations.PowerMockIgnore; |
| 9 | +import org.robolectric.RobolectricTestRunner; |
| 10 | + |
| 11 | +@RunWith(RobolectricTestRunner.class) |
| 12 | +@PowerMockIgnore({"org.mockito.*", "org.robolectric.*", "android.*"}) |
| 13 | +public class CustomLineHeightSpanTest { |
| 14 | + |
| 15 | + @Test |
| 16 | + public void shouldIncreaseAllMetricsProportionally() { |
| 17 | + CustomLineHeightSpan customLineHeightSpan = new CustomLineHeightSpan(22); |
| 18 | + Paint.FontMetricsInt fm = new Paint.FontMetricsInt(); |
| 19 | + fm.top = -10; |
| 20 | + fm.ascent = -5; |
| 21 | + fm.descent = 5; |
| 22 | + fm.bottom = 10; |
| 23 | + customLineHeightSpan.chooseHeight("Hi", 0, 2, 0, 0, fm); |
| 24 | + assertThat(fm.top).isEqualTo(-11); |
| 25 | + assertThat(fm.ascent).isEqualTo(-6); |
| 26 | + assertThat(fm.descent).isEqualTo(6); |
| 27 | + assertThat(fm.bottom).isEqualTo(11); |
| 28 | + } |
| 29 | + |
| 30 | + @Test |
| 31 | + public void shouldReduceTopFirst() { |
| 32 | + CustomLineHeightSpan customLineHeightSpan = new CustomLineHeightSpan(19); |
| 33 | + Paint.FontMetricsInt fm = new Paint.FontMetricsInt(); |
| 34 | + fm.top = -10; |
| 35 | + fm.ascent = -5; |
| 36 | + fm.descent = 5; |
| 37 | + fm.bottom = 10; |
| 38 | + customLineHeightSpan.chooseHeight("Hi", 0, 2, 0, 0, fm); |
| 39 | + assertThat(fm.top).isEqualTo(-9); |
| 40 | + assertThat(fm.ascent).isEqualTo(-5); |
| 41 | + assertThat(fm.descent).isEqualTo(5); |
| 42 | + assertThat(fm.bottom).isEqualTo(10); |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + public void shouldReduceBottomSecond() { |
| 47 | + CustomLineHeightSpan customLineHeightSpan = new CustomLineHeightSpan(14); |
| 48 | + Paint.FontMetricsInt fm = new Paint.FontMetricsInt(); |
| 49 | + fm.top = -10; |
| 50 | + fm.ascent = -5; |
| 51 | + fm.descent = 5; |
| 52 | + fm.bottom = 10; |
| 53 | + customLineHeightSpan.chooseHeight("Hi", 0, 2, 0, 0, fm); |
| 54 | + assertThat(fm.top).isEqualTo(-5); |
| 55 | + assertThat(fm.ascent).isEqualTo(-5); |
| 56 | + assertThat(fm.descent).isEqualTo(5); |
| 57 | + assertThat(fm.bottom).isEqualTo(9); |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + public void shouldReduceAscentThird() { |
| 62 | + CustomLineHeightSpan customLineHeightSpan = new CustomLineHeightSpan(9); |
| 63 | + Paint.FontMetricsInt fm = new Paint.FontMetricsInt(); |
| 64 | + fm.top = -10; |
| 65 | + fm.ascent = -5; |
| 66 | + fm.descent = 5; |
| 67 | + fm.bottom = 10; |
| 68 | + customLineHeightSpan.chooseHeight("Hi", 0, 2, 0, 0, fm); |
| 69 | + assertThat(fm.top).isEqualTo(-4); |
| 70 | + assertThat(fm.ascent).isEqualTo(-4); |
| 71 | + assertThat(fm.descent).isEqualTo(5); |
| 72 | + assertThat(fm.bottom).isEqualTo(5); |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + public void shouldReduceDescentLast() { |
| 77 | + CustomLineHeightSpan customLineHeightSpan = new CustomLineHeightSpan(4); |
| 78 | + Paint.FontMetricsInt fm = new Paint.FontMetricsInt(); |
| 79 | + fm.top = -10; |
| 80 | + fm.ascent = -5; |
| 81 | + fm.descent = 5; |
| 82 | + fm.bottom = 10; |
| 83 | + customLineHeightSpan.chooseHeight("Hi", 0, 2, 0, 0, fm); |
| 84 | + assertThat(fm.top).isEqualTo(0); |
| 85 | + assertThat(fm.ascent).isEqualTo(0); |
| 86 | + assertThat(fm.descent).isEqualTo(4); |
| 87 | + assertThat(fm.bottom).isEqualTo(4); |
| 88 | + } |
| 89 | +} |
0 commit comments