Skip to content

Commit

Permalink
Fix Robolectric ShadowNativeBitmapTests to work with Android V.
Browse files Browse the repository at this point in the history
A recent change in the ColorSpace class changed the name and type of a static field.
This CL updates the access of this field.

PiperOrigin-RevId: 644496680
  • Loading branch information
Googler authored and Copybara-Service committed Jun 18, 2024
1 parent 6afbcb6 commit f5125ea
Showing 1 changed file with 18 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Implementation;
import org.robolectric.annotation.Implements;
Expand All @@ -35,6 +36,7 @@
import org.robolectric.util.reflector.ForType;
import org.robolectric.util.reflector.Static;
import org.robolectric.versioning.AndroidVersions.U;
import org.robolectric.versioning.AndroidVersions.V;

/** Shadow for {@link Bitmap} that is backed by native code */
@Implements(
Expand Down Expand Up @@ -362,6 +364,9 @@ interface ColorSpaceReflector {

@Accessor("sNamedColorSpaces")
ColorSpace[] getNamedColorSpaces();

@Accessor("sNamedColorSpaceMap")
Map<Integer, ColorSpace> getNamedColorSpaceMap();
}

@Implementation
Expand Down Expand Up @@ -409,7 +414,19 @@ protected static Bitmap nativeCreateFromParcel(Parcel p) {
boolean hasColorSpace = p.readBoolean();
if (hasColorSpace) {
String colorSpaceName = p.readString();
ColorSpace[] namedColorSpaces = reflector(ColorSpaceReflector.class).getNamedColorSpaces();
ColorSpace[] namedColorSpaces;
if (RuntimeEnvironment.getApiLevel() >= V.SDK_INT) {
// Starting Android V, we need to access the color space map to get all supported color
// spaces.
Map<Integer, ColorSpace> namedColorSpaceMap =
reflector(ColorSpaceReflector.class).getNamedColorSpaceMap();
namedColorSpaces =
namedColorSpaceMap.values().toArray(new ColorSpace[namedColorSpaceMap.size()]);
} else {
// Before V, we directly access the color space array.
namedColorSpaces = reflector(ColorSpaceReflector.class).getNamedColorSpaces();
}

for (ColorSpace named : namedColorSpaces) {
if (named.getName().equals(colorSpaceName)) {
colorSpace = named;
Expand Down

0 comments on commit f5125ea

Please sign in to comment.