Back to Repositories

Testing Dimension Resource Binding Implementation in ButterKnife

This test suite validates the BindDimen functionality in ButterKnife, ensuring proper dimension resource binding for both integer and float values. It verifies the annotation’s ability to correctly handle dimension resources and maintain bindings through the component lifecycle.

Test Coverage Overview

The test suite provides comprehensive coverage of BindDimen annotation functionality.

  • Tests dimension resource binding for both integer and float values
  • Verifies correct resource resolution from R.dimen
  • Validates binding persistence through unbind operations
  • Ensures accurate pixel conversion for dimension resources

Implementation Analysis

The testing approach employs JUnit with AndroidX test instrumentation to validate dimension resource binding.

Tests utilize ViewTree creation for binding context and implement separate target classes for int and float scenarios. The implementation leverages Truth assertions for precise value comparison and follows ButterKnife’s binding lifecycle patterns.

Technical Details

  • JUnit test framework with AndroidX Instrumentation
  • ButterKnife annotation processing
  • Truth assertion library for precise comparisons
  • Custom ViewTree test utility
  • Context-based resource resolution
  • Dimension resource binding via @BindDimen annotation

Best Practices Demonstrated

The test suite exemplifies clean test organization and thorough validation practices.

  • Separate test cases for different data types
  • Proper resource cleanup with unbinder
  • Clear test method naming conventions
  • Effective use of static inner classes for test targets
  • Comprehensive assertion coverage

jakewharton/butterknife

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

            
package com.example.butterknife.functional;

import android.content.Context;
import android.view.View;
import androidx.test.InstrumentationRegistry;
import butterknife.BindDimen;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import com.example.butterknife.test.R;
import org.junit.Test;

import static com.google.common.truth.Truth.assertThat;

public final class BindDimenTest {
  private final Context context = InstrumentationRegistry.getContext();
  private final View tree = ViewTree.create(1);

  static class IntTarget {
    @BindDimen(R.dimen.twelve_point_two_dp) int actual;
  }

  @Test public void asInt() {
    IntTarget target = new IntTarget();
    int expected = context.getResources().getDimensionPixelSize(R.dimen.twelve_point_two_dp);

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

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

  static class FloatTarget {
    @BindDimen(R.dimen.twelve_point_two_dp) float actual;
  }

  @Test public void asFloat() {
    FloatTarget target = new FloatTarget();
    float expected = context.getResources().getDimension(R.dimen.twelve_point_two_dp);

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

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