aboutsummaryrefslogtreecommitdiff
path: root/integration_tests/nativegraphics/src/test/java/org/robolectric/integrationtests/nativegraphics/ShadowNativeRuntimeShaderTest.java
blob: 9f06cf59a82993a2c589879bfcedaa165aaaf8bb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package org.robolectric.integrationtests.nativegraphics;

import static android.os.Build.VERSION_CODES.S;
import static android.os.Build.VERSION_CODES.S_V2;
import static android.os.Build.VERSION_CODES.TIRAMISU;
import static com.google.common.base.StandardSystemProperty.OS_NAME;
import static com.google.common.truth.TruthJUnit.assume;

import android.graphics.RuntimeShader;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import java.util.Locale;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.annotation.Config;
import org.robolectric.util.ReflectionHelpers;
import org.robolectric.util.ReflectionHelpers.ClassParameter;

@Config(minSdk = S)
@RunWith(AndroidJUnit4.class)
public class ShadowNativeRuntimeShaderTest {
  static final String SKSL =
      ""
          + "uniform float2 in_origin;"
          + "uniform float in_progress;\n"
          + "uniform float in_maxRadius;\n"
          + "uniform shader in_paintColor;\n"
          + "float dist2(float2 p0, float2 pf) { return sqrt((pf.x - p0.x) * (pf.x - p0.x) + "
          + "(pf.y - p0.y) * (pf.y - p0.y)); }\n"
          + "float mod2(float a, float b) { return a - (b * floor(a / b)); }\n"
          + "float rand(float2 src) { return fract(sin(dot(src.xy, float2(12.9898, 78.233)))"
          + " * 43758.5453123); }\n"
          + "float4 main(float2 p)\n"
          + "{\n"
          + "    float fraction = in_progress;\n"
          + "    float2 fragCoord = p;//sk_FragCoord.xy;\n"
          + "    float maxDist = in_maxRadius;\n"
          + "    float fragDist = dist2(in_origin, fragCoord.xy);\n"
          + "    float circleRadius = maxDist * fraction;\n"
          + "    float colorVal = (fragDist - circleRadius) / maxDist;\n"
          + "    float d = fragDist < circleRadius \n"
          + "        ? 1. - abs(colorVal * 2. * smoothstep(0., 1., fraction)) \n"
          + "        : 1. - abs(colorVal * 3.);\n"
          + "    d = smoothstep(0., 1., d);\n"
          + "    float divider = 2.;\n"
          + "    float x = floor(fragCoord.x / divider);\n"
          + "    float y = floor(fragCoord.y / divider);\n"
          + "    float density = .95;\n"
          + "    d = rand(float2(x, y)) > density ? d : d * .2;\n"
          + "    d = d * rand(float2(fraction, x * y));\n"
          + "    float alpha = 1. - pow(fraction, 3.);\n"
          + "    return float4(sample(in_paintColor, p).rgb, d * alpha);\n"
          + "}";

  @Before
  public void setup() {
    // The native code behind RuntimeShader is currently not supported on Mac.
    assume().that(OS_NAME.value().toLowerCase(Locale.US)).doesNotContain("mac");
  }

  @Config(minSdk = S, maxSdk = S_V2)
  @Test
  public void testConstructor() {
    var unused =
        ReflectionHelpers.callConstructor(
            RuntimeShader.class,
            ClassParameter.from(String.class, SKSL),
            ClassParameter.from(boolean.class, false));
  }

  @Config(minSdk = TIRAMISU)
  @Test
  public void testConstructorT() {
    var unused = new RuntimeShader(SKSL);
  }

  @Test
  public void rippleShader_ctor() throws Exception {
    ReflectionHelpers.callConstructor(Class.forName("android.graphics.drawable.RippleShader"));
  }
}