[image] Add define and core data (#13058)

This commit is contained in:
Clyde Stubbs
2026-01-08 10:20:37 +10:00
committed by GitHub
parent 0ce3ac438b
commit 738678e87b
2 changed files with 161 additions and 7 deletions

View File

@@ -9,8 +9,14 @@ from typing import Any
import pytest
from esphome import config_validation as cv
from esphome.components.image import CONF_TRANSPARENCY, CONFIG_SCHEMA
from esphome.components.image import (
CONF_TRANSPARENCY,
CONFIG_SCHEMA,
get_all_image_metadata,
get_image_metadata,
)
from esphome.const import CONF_ID, CONF_RAW_DATA_ID, CONF_TYPE
from esphome.core import CORE
@pytest.mark.parametrize(
@@ -235,3 +241,112 @@ def test_image_generation(
"cat_img = new image::Image(uint8_t_id, 32, 24, image::IMAGE_TYPE_RGB565, image::TRANSPARENCY_OPAQUE);"
in main_cpp
)
def test_image_to_code_defines_and_core_data(
generate_main: Callable[[str | Path], str],
component_config_path: Callable[[str], Path],
) -> None:
"""Test that to_code() sets USE_IMAGE define and stores image metadata."""
# Generate the main cpp which will call to_code
generate_main(component_config_path("image_test.yaml"))
# Verify USE_IMAGE define was added
assert any(d.name == "USE_IMAGE" for d in CORE.defines), (
"USE_IMAGE define should be set when images are configured"
)
# Use the public API to get image metadata
# The test config has an image with id 'cat_img'
cat_img_metadata = get_image_metadata("cat_img")
assert cat_img_metadata is not None, (
"Image metadata should be retrievable via get_image_metadata()"
)
# Verify the metadata has the expected attributes
assert hasattr(cat_img_metadata, "width"), "Metadata should have width attribute"
assert hasattr(cat_img_metadata, "height"), "Metadata should have height attribute"
assert hasattr(cat_img_metadata, "image_type"), (
"Metadata should have image_type attribute"
)
assert hasattr(cat_img_metadata, "transparency"), (
"Metadata should have transparency attribute"
)
# Verify the values are correct (from the test image)
assert cat_img_metadata.width == 32, "Width should be 32"
assert cat_img_metadata.height == 24, "Height should be 24"
assert cat_img_metadata.image_type == "RGB565", "Type should be RGB565"
assert cat_img_metadata.transparency == "opaque", "Transparency should be opaque"
def test_image_to_code_multiple_images(
generate_main: Callable[[str | Path], str],
component_config_path: Callable[[str], Path],
) -> None:
"""Test that to_code() stores metadata for multiple images."""
generate_main(component_config_path("image_test.yaml"))
# Use the public API to get all image metadata
all_metadata = get_all_image_metadata()
assert isinstance(all_metadata, dict), (
"get_all_image_metadata() should return a dictionary"
)
# Verify that at least one image is present
assert len(all_metadata) > 0, "Should have at least one image metadata entry"
# Each image ID should map to an ImageMetaData object
for image_id, metadata in all_metadata.items():
assert isinstance(image_id, str), "Image IDs should be strings"
# Verify it's an ImageMetaData object with all required attributes
assert hasattr(metadata, "width"), (
f"Metadata for '{image_id}' should have width"
)
assert hasattr(metadata, "height"), (
f"Metadata for '{image_id}' should have height"
)
assert hasattr(metadata, "image_type"), (
f"Metadata for '{image_id}' should have image_type"
)
assert hasattr(metadata, "transparency"), (
f"Metadata for '{image_id}' should have transparency"
)
# Verify values are valid
assert isinstance(metadata.width, int), (
f"Width for '{image_id}' should be an integer"
)
assert isinstance(metadata.height, int), (
f"Height for '{image_id}' should be an integer"
)
assert isinstance(metadata.image_type, str), (
f"Type for '{image_id}' should be a string"
)
assert isinstance(metadata.transparency, str), (
f"Transparency for '{image_id}' should be a string"
)
assert metadata.width > 0, f"Width for '{image_id}' should be positive"
assert metadata.height > 0, f"Height for '{image_id}' should be positive"
def test_get_image_metadata_nonexistent() -> None:
"""Test that get_image_metadata returns None for non-existent image IDs."""
# This should return None when no images are configured or ID doesn't exist
metadata = get_image_metadata("nonexistent_image_id")
assert metadata is None, (
"get_image_metadata should return None for non-existent IDs"
)
def test_get_all_image_metadata_empty() -> None:
"""Test that get_all_image_metadata returns empty dict when no images configured."""
# When CORE hasn't been initialized with images, should return empty dict
all_metadata = get_all_image_metadata()
assert isinstance(all_metadata, dict), (
"get_all_image_metadata should always return a dict"
)
# Length could be 0 or more depending on what's in CORE at test time