Skip to content

Commit

Permalink
Add support for 2-arg Typeface constructor in legacy ShadowTypeface
Browse files Browse the repository at this point in the history
Android U instroduced a new 2-arg constructor for Typeface that is used
by Typeface.CustomFallbackBuilder. Add support for this so that NPE's don't
result.

Fixes #8553

PiperOrigin-RevId: 576219278
  • Loading branch information
hoisie committed Oct 27, 2023
1 parent 1341c57 commit d068642
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -167,4 +167,16 @@ public void typeface_customFallbackBuilder_afterReset() throws IOException {
new Typeface.CustomFallbackBuilder(family).setStyle(font.getStyle()).build();
assertThat(typeface).isNotNull();
}

@Test
@Config(minSdk = Q)
public void createTypeface_withCustomFallbackBuilder() throws IOException {
Font font = new Font.Builder(fontFile).build();
FontFamily family = new FontFamily.Builder(font).build();
Typeface typeface =
new Typeface.CustomFallbackBuilder(family).setStyle(font.getStyle()).build();
Typeface typeface2 = Typeface.create(typeface, Typeface.BOLD);
assertThat(typeface2).isNotNull();
assertThat(typeface2.toString()).isNotEmpty();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
import org.robolectric.shadow.api.Shadow;
import org.robolectric.util.ReflectionHelpers;
import org.robolectric.util.ReflectionHelpers.ClassParameter;
import org.robolectric.versioning.AndroidVersions.T;
import org.robolectric.versioning.AndroidVersions.U;

/** Shadow for {@link Typeface}. */
@Implements(value = Typeface.class, looseSignatures = true, isInAndroidSdk = false)
Expand All @@ -46,18 +48,22 @@ public class ShadowLegacyTypeface extends ShadowTypeface {
private static final AtomicLong nextFontId = new AtomicLong(1);
private FontDesc description;

@HiddenApi
@Implementation(maxSdk = KITKAT)
protected void __constructor__(int fontId) {
description = findById((long) fontId);
description = findById(fontId);
}

@HiddenApi
@Implementation(minSdk = LOLLIPOP)
/** Starting in U, this constructor calls {@link #__constructor__(long, String )} below. */
@Implementation(minSdk = LOLLIPOP, maxSdk = T.SDK_INT)
protected void __constructor__(long fontId) {
description = findById(fontId);
}

@Implementation(minSdk = U.SDK_INT)
protected void __constructor__(long fontId, String familyName) {
description = findById(fontId);
}

@Implementation
protected static void __staticInitializer__() {
Shadow.directInitialize(Typeface.class);
Expand Down

0 comments on commit d068642

Please sign in to comment.