Back to Repositories

Testing ImageEditor Component Implementation in gradio-app/gradio

This test suite validates the functionality of the ImageEditor component within the Gradio framework, focusing on preprocessing, postprocessing, and configuration handling. The tests ensure proper handling of image data, file paths, and editor-specific features.

Test Coverage Overview

The test suite provides comprehensive coverage of the ImageEditor component’s core functionality:

  • Component data preprocessing and postprocessing validation
  • File path handling and conversion
  • Configuration parameter verification
  • Example processing capabilities

Implementation Analysis

The testing approach employs pytest fixtures to validate the ImageEditor component’s behavior. It uses a structured testing pattern that verifies data transformations between EditorData objects and raw file paths, ensuring proper handling of image processing operations.

Technical Details

  • Testing Framework: pytest
  • Key Classes: TestImageEditor, EditorData, EditorValue
  • Test Data: Sample image file (bus.png)
  • Component Configuration: Extensive validation of default settings and parameters

Best Practices Demonstrated

The test suite demonstrates several testing best practices:

  • Isolated component testing
  • Comprehensive configuration validation
  • Edge case handling for different input types
  • Clear test method organization and naming

gradio-app/gradio

test/components/test_image_editor.py

            
import gradio as gr
from gradio.components.image_editor import EditorData, EditorValue
from gradio.data_classes import FileData


class TestImageEditor:
    def test_component_functions(self):
        test_image_path = "test/test_files/bus.png"
        image_data = FileData(path=test_image_path)
        image_editor_data = EditorData(
            background=image_data, layers=[image_data, image_data], composite=image_data
        )
        payload: EditorValue = {
            "background": test_image_path,
            "layers": [test_image_path, test_image_path],
            "composite": test_image_path,
        }

        image_editor_component = gr.ImageEditor()

        assert isinstance(image_editor_component.preprocess(image_editor_data), dict)
        assert image_editor_component.postprocess(payload) == image_editor_data

        # Test that ImageEditor can accept just a filepath as well
        simpler_data = EditorData(
            background=image_data, layers=[], composite=image_data
        )
        assert image_editor_component.postprocess(test_image_path) == simpler_data

        assert image_editor_component.get_config() == {
            "value": None,
            "height": None,
            "width": None,
            "image_mode": "RGBA",
            "sources": ("upload", "webcam", "clipboard"),
            "type": "numpy",
            "label": None,
            "show_label": True,
            "show_download_button": True,
            "container": True,
            "scale": None,
            "min_width": 160,
            "interactive": None,
            "visible": True,
            "elem_id": None,
            "elem_classes": [],
            "mirror_webcam": True,
            "show_share_button": False,
            "_selectable": False,
            "key": None,
            "crop_size": None,
            "transforms": ("crop",),
            "eraser": {"default_size": "auto"},
            "brush": {
                "default_size": "auto",
                "colors": [
                    "rgb(204, 50, 50)",
                    "rgb(173, 204, 50)",
                    "rgb(50, 204, 112)",
                    "rgb(50, 112, 204)",
                    "rgb(173, 50, 204)",
                ],
                "default_color": "auto",
                "color_mode": "defaults",
            },
            "proxy_url": None,
            "name": "imageeditor",
            "server_fns": ["accept_blobs"],
            "format": "webp",
            "layers": True,
            "canvas_size": None,
            "placeholder": None,
            "show_fullscreen_button": True,
        }

    def test_process_example(self):
        test_image_path = "test/test_files/bus.png"
        image_editor = gr.ImageEditor()
        example_value = image_editor.process_example(test_image_path)
        assert isinstance(example_value, EditorData)
        assert example_value.background and example_value.background.path