|
| 1 | +/* |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + */ |
| 7 | + |
| 8 | + |
| 9 | +package com.facebook.react.animated |
| 10 | + |
| 11 | +import org.assertj.core.api.Assertions.assertThat |
| 12 | +import org.junit.Test |
| 13 | +import org.junit.runner.RunWith |
| 14 | +import org.robolectric.RobolectricTestRunner |
| 15 | + |
| 16 | +/** Tests method used by [InterpolationAnimatedNode] to interpolate value of the input nodes. */ |
| 17 | +@RunWith(RobolectricTestRunner::class) |
| 18 | +class NativeAnimatedInterpolationTest { |
| 19 | + |
| 20 | + @Test |
| 21 | + fun testSimpleOneToOneMapping() { |
| 22 | + val input = doubleArrayOf(0.0, 1.0) |
| 23 | + val output = doubleArrayOf(0.0, 1.0) |
| 24 | + assertThat(simpleInterpolation(0.0, input, output)).isEqualTo(0.0) |
| 25 | + assertThat(simpleInterpolation(0.5, input, output)).isEqualTo(0.5) |
| 26 | + assertThat(simpleInterpolation(0.8, input, output)).isEqualTo(0.8) |
| 27 | + assertThat(simpleInterpolation(1.0, input, output)).isEqualTo(1.0) |
| 28 | + } |
| 29 | + |
| 30 | + @Test |
| 31 | + fun testWiderOutputRange() { |
| 32 | + val input = doubleArrayOf(0.0, 1.0) |
| 33 | + val output = doubleArrayOf(100.0, 200.0) |
| 34 | + assertThat(simpleInterpolation(0.0, input, output)).isEqualTo(100.0) |
| 35 | + assertThat(simpleInterpolation(0.5, input, output)).isEqualTo(150.0) |
| 36 | + assertThat(simpleInterpolation(0.8, input, output)).isEqualTo(180.0) |
| 37 | + assertThat(simpleInterpolation(1.0, input, output)).isEqualTo(200.0) |
| 38 | + } |
| 39 | + |
| 40 | + @Test |
| 41 | + fun testWiderInputRange() { |
| 42 | + val input = doubleArrayOf(2000.0, 3000.0) |
| 43 | + val output = doubleArrayOf(1.0, 2.0) |
| 44 | + assertThat(simpleInterpolation(2000.0, input, output)).isEqualTo(1.0) |
| 45 | + assertThat(simpleInterpolation(2250.0, input, output)).isEqualTo(1.25) |
| 46 | + assertThat(simpleInterpolation(2800.0, input, output)).isEqualTo(1.8) |
| 47 | + assertThat(simpleInterpolation(3000.0, input, output)).isEqualTo(2.0) |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + fun testManySegments() { |
| 52 | + val input = doubleArrayOf(-1.0, 1.0, 5.0) |
| 53 | + val output = doubleArrayOf(0.0, 10.0, 20.0) |
| 54 | + assertThat(simpleInterpolation(-1.0, input, output)).isEqualTo(0.0) |
| 55 | + assertThat(simpleInterpolation(0.0, input, output)).isEqualTo(5.0) |
| 56 | + assertThat(simpleInterpolation(1.0, input, output)).isEqualTo(10.0) |
| 57 | + assertThat(simpleInterpolation(2.0, input, output)).isEqualTo(12.5) |
| 58 | + assertThat(simpleInterpolation(5.0, input, output)).isEqualTo(20.0) |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + fun testExtendExtrapolate() { |
| 63 | + val input = doubleArrayOf(10.0, 20.0) |
| 64 | + val output = doubleArrayOf(0.0, 1.0) |
| 65 | + assertThat(simpleInterpolation(30.0, input, output)).isEqualTo(2.0) |
| 66 | + assertThat(simpleInterpolation(5.0, input, output)).isEqualTo(-0.5) |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + fun testClampExtrapolate() { |
| 71 | + val input = doubleArrayOf(10.0, 20.0) |
| 72 | + val output = doubleArrayOf(0.0, 1.0) |
| 73 | + assertThat( |
| 74 | + InterpolationAnimatedNode.interpolate( |
| 75 | + 30.0, |
| 76 | + input, |
| 77 | + output, |
| 78 | + InterpolationAnimatedNode.EXTRAPOLATE_TYPE_CLAMP, |
| 79 | + InterpolationAnimatedNode.EXTRAPOLATE_TYPE_CLAMP)) |
| 80 | + .isEqualTo(1.0) |
| 81 | + assertThat( |
| 82 | + InterpolationAnimatedNode.interpolate( |
| 83 | + 5.0, |
| 84 | + input, |
| 85 | + output, |
| 86 | + InterpolationAnimatedNode.EXTRAPOLATE_TYPE_CLAMP, |
| 87 | + InterpolationAnimatedNode.EXTRAPOLATE_TYPE_CLAMP)) |
| 88 | + .isEqualTo(0.0) |
| 89 | + } |
| 90 | + |
| 91 | + @Test |
| 92 | + fun testIdentityExtrapolate() { |
| 93 | + val input = doubleArrayOf(10.0, 20.0) |
| 94 | + val output = doubleArrayOf(0.0, 1.0) |
| 95 | + assertThat( |
| 96 | + InterpolationAnimatedNode.interpolate( |
| 97 | + 30.0, |
| 98 | + input, |
| 99 | + output, |
| 100 | + InterpolationAnimatedNode.EXTRAPOLATE_TYPE_IDENTITY, |
| 101 | + InterpolationAnimatedNode.EXTRAPOLATE_TYPE_IDENTITY)) |
| 102 | + .isEqualTo(30.0) |
| 103 | + assertThat( |
| 104 | + InterpolationAnimatedNode.interpolate( |
| 105 | + 5.0, |
| 106 | + input, |
| 107 | + output, |
| 108 | + InterpolationAnimatedNode.EXTRAPOLATE_TYPE_IDENTITY, |
| 109 | + InterpolationAnimatedNode.EXTRAPOLATE_TYPE_IDENTITY)) |
| 110 | + .isEqualTo(5.0) |
| 111 | + } |
| 112 | + |
| 113 | + @Test |
| 114 | + fun testInterpolateColor() { |
| 115 | + val input = doubleArrayOf(0.0, 1.0) |
| 116 | + val output = intArrayOf(0xFF000000.toInt(), 0xFFFF0000.toInt()) |
| 117 | + assertThat(InterpolationAnimatedNode.interpolateColor(0.0, input, output)) |
| 118 | + .isEqualTo(0xFF000000.toInt()) |
| 119 | + assertThat(InterpolationAnimatedNode.interpolateColor(0.5, input, output)) |
| 120 | + .isEqualTo(0xFF7F0000.toInt()) |
| 121 | + } |
| 122 | + |
| 123 | + @Test |
| 124 | + fun testInterpolateString() { |
| 125 | + val input = doubleArrayOf(0.0, 1.0) |
| 126 | + val output = |
| 127 | + arrayOf( |
| 128 | + doubleArrayOf(20.0, 20.0, 20.0, 80.0, 80.0, 80.0, 80.0, 20.0), |
| 129 | + doubleArrayOf(40.0, 40.0, 33.0, 60.0, 60.0, 60.0, 65.0, 40.0)) |
| 130 | + val pattern = "M20,20L20,80L80,80L80,20Z" |
| 131 | + assertThat( |
| 132 | + InterpolationAnimatedNode.interpolateString( |
| 133 | + pattern, |
| 134 | + 0.0, |
| 135 | + input, |
| 136 | + output, |
| 137 | + InterpolationAnimatedNode.EXTRAPOLATE_TYPE_IDENTITY, |
| 138 | + InterpolationAnimatedNode.EXTRAPOLATE_TYPE_IDENTITY)) |
| 139 | + .isEqualTo("M20,20L20,80L80,80L80,20Z") |
| 140 | + assertThat( |
| 141 | + InterpolationAnimatedNode.interpolateString( |
| 142 | + pattern, |
| 143 | + 0.5, |
| 144 | + input, |
| 145 | + output, |
| 146 | + InterpolationAnimatedNode.EXTRAPOLATE_TYPE_IDENTITY, |
| 147 | + InterpolationAnimatedNode.EXTRAPOLATE_TYPE_IDENTITY)) |
| 148 | + .isEqualTo("M30,30L26.5,70L70,70L72.5,30Z") |
| 149 | + } |
| 150 | + |
| 151 | + private fun simpleInterpolation(value: Double, input: DoubleArray, output: DoubleArray): Double = |
| 152 | + InterpolationAnimatedNode.interpolate( |
| 153 | + value, |
| 154 | + input, |
| 155 | + output, |
| 156 | + InterpolationAnimatedNode.EXTRAPOLATE_TYPE_EXTEND, |
| 157 | + InterpolationAnimatedNode.EXTRAPOLATE_TYPE_EXTEND) |
| 158 | +} |
0 commit comments