Back to Repositories

Testing Color Manipulation Utilities in AndroidUtilCode

A comprehensive unit test suite for the ColorUtils class in AndroidUtilCode, validating color manipulation and conversion functionalities. This test file ensures reliable color handling operations across alpha, RGB components, and string-integer conversions.

Test Coverage Overview

The test suite provides thorough coverage of color manipulation utilities, including:
  • Alpha component modification tests with both hex and float values
  • Individual RGB component adjustment validations
  • Color format conversion between strings and integers
  • Edge cases for white color and different format inputs
Integration points include the base ColorUtils implementation and standard color format specifications.

Implementation Analysis

The testing approach utilizes JUnit’s assertion framework for precise color value validations. Each test method focuses on a specific color manipulation function, employing both hexadecimal and floating-point inputs to ensure comprehensive coverage.

The implementation follows a consistent pattern of testing both numeric and percentage-based color modifications, with particular attention to alpha channel handling.

Technical Details

Testing tools and configuration:
  • JUnit 4 testing framework
  • Assert.assertEquals for precise hex value comparisons
  • BaseTest extension for common testing utilities
  • Hexadecimal color representation (0xAARRGGBB format)
  • String-based color notation (#RRGGBB and #AARRGGBB)

Best Practices Demonstrated

The test suite exemplifies several testing best practices:
  • Isolated test methods for each color component operation
  • Consistent validation of both integer and float input types
  • Clear test method naming reflecting functionality under test
  • Comprehensive coverage of color format conversions
  • Validation of both successful and edge case scenarios

blankj/androidutilcode

lib/utilcode/src/test/java/com/blankj/utilcode/util/ColorUtilsTest.java

            
package com.blankj.utilcode.util;

import org.junit.Test;

import static org.junit.Assert.assertEquals;

/**
 * <pre>
 *     author: Blankj
 *     blog  : http://blankj.com
 *     time  : 2019/01/15
 *     desc  : test ColorUtils
 * </pre>
 */
public class ColorUtilsTest extends BaseTest {

    @Test
    public void setAlphaComponent() {
        assertEquals(0x80ffffff, ColorUtils.setAlphaComponent(0xffffffff, 0x80));
        assertEquals(0x80ffffff, ColorUtils.setAlphaComponent(0xffffffff, 0.5f));
    }

    @Test
    public void setRedComponent() {
        assertEquals(0xff80ffff, ColorUtils.setRedComponent(0xffffffff, 0x80));
        assertEquals(0xff80ffff, ColorUtils.setRedComponent(0xffffffff, 0.5f));
    }

    @Test
    public void setGreenComponent() {
        assertEquals(0xffff80ff, ColorUtils.setGreenComponent(0xffffffff, 0x80));
        assertEquals(0xffff80ff, ColorUtils.setGreenComponent(0xffffffff, 0.5f));
    }

    @Test
    public void setBlueComponent() {
        assertEquals(0xffffff80, ColorUtils.setBlueComponent(0xffffffff, 0x80));
        assertEquals(0xffffff80, ColorUtils.setBlueComponent(0xffffffff, 0.5f));
    }

    @Test
    public void string2Int() {
        assertEquals(0xffffffff, ColorUtils.string2Int("#ffffff"));
        assertEquals(0xffffffff, ColorUtils.string2Int("#ffffffff"));
        assertEquals(0xffffffff, ColorUtils.string2Int("white"));
    }

    @Test
    public void int2RgbString() {
        assertEquals("#000001", ColorUtils.int2RgbString(1));
        assertEquals("#ffffff", ColorUtils.int2RgbString(0xffffff));
    }

    @Test
    public void int2ArgbString() {
        assertEquals("#ff000001", ColorUtils.int2ArgbString(1));
        assertEquals("#ffffffff", ColorUtils.int2ArgbString(0xffffff));
    }
}