Back to Repositories

Validating Font Resource Binding Integration in ButterKnife

This test suite validates the font binding functionality in ButterKnife’s Android library, specifically focusing on the @BindFont annotation. It verifies both basic typeface binding and styled font applications with custom attributes.

Test Coverage Overview

The test suite provides comprehensive coverage of font binding capabilities:

  • Basic typeface binding validation using @BindFont annotation
  • Custom font style application testing with BOLD attribute
  • Resource compatibility testing with AndroidX
  • Unbinding behavior verification

Implementation Analysis

The testing approach employs JUnit with AndroidX test infrastructure, utilizing instrumentation for context-dependent font resource access. The implementation leverages ResourcesCompat for font loading and comparison, ensuring compatibility across Android versions.

Key patterns include:
  • Context-based resource loading
  • View tree simulation
  • Resource binding verification
  • Memory management testing through unbinding

Technical Details

Testing tools and configuration:
  • JUnit test framework
  • AndroidX test utilities
  • SdkSuppress annotation for version compatibility
  • ResourcesCompat for font resource handling
  • Truth assertion library for precise comparisons
  • Custom ViewTree simulation

Best Practices Demonstrated

The test suite exemplifies several testing best practices:

  • Isolated test cases for different font binding scenarios
  • Proper resource cleanup through unbinding verification
  • SDK version compatibility management
  • Clear test case organization with descriptive method names
  • Effective use of assertion libraries for precise validation

jakewharton/butterknife

butterknife-integration-test/src/androidTest/java/com/example/butterknife/functional/BindFontTest.java

            
package com.example.butterknife.functional;

import android.content.Context;
import android.graphics.Typeface;
import android.view.View;
import androidx.core.content.res.ResourcesCompat;
import androidx.test.InstrumentationRegistry;
import androidx.test.filters.SdkSuppress;
import butterknife.BindFont;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import com.example.butterknife.test.R;
import org.junit.Test;

import static android.graphics.Typeface.BOLD;
import static com.google.common.truth.Truth.assertThat;

@SdkSuppress(minSdkVersion = 24) // AndroidX problems on earlier versions
public final class BindFontTest {
  private final Context context = InstrumentationRegistry.getContext();
  private final View tree = ViewTree.create(1);

  static class TargetTypeface {
    @BindFont(R.font.inconsolata_regular) Typeface actual;
  }

  @Test public void typeface() {
    TargetTypeface target = new TargetTypeface();
    Typeface expected = ResourcesCompat.getFont(context, R.font.inconsolata_regular);

    Unbinder unbinder = ButterKnife.bind(target, tree);
    assertThat(target.actual).isSameAs(expected);

    unbinder.unbind();
    assertThat(target.actual).isSameAs(expected);
  }

  static class TargetStyle {
    @BindFont(value = R.font.inconsolata_regular, style = BOLD) Typeface actual;
  }

  @Test public void style() {
    TargetStyle target = new TargetStyle();
    Typeface expected =
        Typeface.create(ResourcesCompat.getFont(context, R.font.inconsolata_regular), BOLD);

    Unbinder unbinder = ButterKnife.bind(target, tree);
    assertThat(target.actual).isSameAs(expected);

    unbinder.unbind();
    assertThat(target.actual).isSameAs(expected);
  }
}