- Added parameterized tests for image edit replacement to ensure alpha mode preservation. - Refactored existing tests to utilize new image editing methods and improve clarity. - Introduced new tests for image target functionality, ensuring proper handling of shared textures and context. - Updated various tests to validate alpha mode handling across different scenarios. - Improved undo/redo functionality in image operators to maintain state consistency. - Added support for texture node actions, enhancing integration with Blender's node system. - Ensured that image properties are preserved during operations and that invalid contexts are handled gracefully.
125 lines
4.3 KiB
Python
125 lines
4.3 KiB
Python
from pathlib import Path
|
|
from types import SimpleNamespace
|
|
from unittest.mock import Mock, patch
|
|
from tests.support.blender import BlenderTestCase
|
|
|
|
|
|
class AiImageOperatorsTest(BlenderTestCase):
|
|
def test_upscale_operator_defaults_to_wdn_and_sends_selected_model(self):
|
|
operator_type = self.upscale.UpscaleImage
|
|
self.assertIn(operator_type, self.anyimage.CLASSES)
|
|
self.assertEqual(
|
|
operator_type.__annotations__["model"]["default"],
|
|
"REALESRGAN_GENERAL_WDN_X4V3",
|
|
)
|
|
self.assertEqual(
|
|
operator_type.__annotations__["max_input_size"]["default"],
|
|
2048,
|
|
)
|
|
|
|
self.assertIn(self.runtime.JobOperatorBase, operator_type.__mro__)
|
|
self.assertEqual(operator_type.bl_options, {"UNDO"})
|
|
operator = operator_type()
|
|
operator.input_path = "input.png"
|
|
operator._image_target = SimpleNamespace(start_frame=4)
|
|
operator.model = "SAFMN_REAL_X4"
|
|
operator.max_input_size = 1024
|
|
context = SimpleNamespace(
|
|
scene=SimpleNamespace(anyimage_settings=SimpleNamespace(video_frames=7))
|
|
)
|
|
with (
|
|
patch.object(self.upscale, "require_environment"),
|
|
patch.object(self.upscale, "require_model") as require_model,
|
|
patch.object(
|
|
self.upscale, "require_input_path", return_value=Path("input.png")
|
|
),
|
|
patch.object(self.upscale, "production_device", return_value="CUDA"),
|
|
):
|
|
request = operator.request(context)
|
|
|
|
require_model.assert_called_once_with("SAFMN_REAL_X4")
|
|
self.assertEqual(
|
|
request,
|
|
{
|
|
"input": "input.png",
|
|
"model": "SAFMN_REAL_X4",
|
|
"device": "cuda",
|
|
"video_frames": 7,
|
|
"start_frame": 4,
|
|
"max_input_size": 1024,
|
|
},
|
|
)
|
|
|
|
|
|
def test_upscale_operator_opens_setup_when_required_ai_is_missing(self):
|
|
expected = {"CANCELLED"}
|
|
with patch.object(
|
|
self.upscale,
|
|
"invoke_ai_setup_if_needed",
|
|
return_value=expected,
|
|
) as setup:
|
|
result = self.upscale.UpscaleImage().invoke(None, None)
|
|
|
|
setup.assert_called_once_with()
|
|
self.assertIs(result, expected)
|
|
|
|
|
|
def test_upscale_operator_runs_immediately_when_ai_is_ready(self):
|
|
operator = self.upscale.UpscaleImage()
|
|
context = object()
|
|
expected = {"FINISHED"}
|
|
with (
|
|
patch.object(
|
|
self.upscale,
|
|
"invoke_ai_setup_if_needed",
|
|
return_value=None,
|
|
),
|
|
patch.object(
|
|
operator,
|
|
"execute",
|
|
return_value=expected,
|
|
) as execute,
|
|
):
|
|
result = operator.invoke(context, None)
|
|
|
|
execute.assert_called_once_with(context)
|
|
self.assertIs(result, expected)
|
|
self.assertNotIn("REGISTER", operator.bl_options)
|
|
|
|
|
|
def test_remove_background_loads_a_plain_image_edit_result(self):
|
|
result = SimpleNamespace(directory=Path("result"))
|
|
operator = self.remove_background.RunBackgroundRemoval()
|
|
operator._image_target = Mock()
|
|
ops = SimpleNamespace(
|
|
ed=SimpleNamespace(undo_push=Mock(return_value={"FINISHED"}))
|
|
)
|
|
with (
|
|
patch.object(self.remove_background.bpy, "ops", ops, create=True),
|
|
):
|
|
operator.response(None, result)
|
|
|
|
operator._image_target.apply.assert_called_once_with(Path("result"))
|
|
ops.ed.undo_push.assert_called_once_with(message="Remove Background")
|
|
|
|
|
|
def test_upscale_loads_a_plain_image_edit_result(self):
|
|
result = SimpleNamespace(directory=Path("result"))
|
|
operator = self.upscale.UpscaleImage()
|
|
operator._image_target = Mock()
|
|
operator.model = "SAFMN_REAL_X4"
|
|
with (
|
|
patch.object(
|
|
self.upscale,
|
|
"upscale_model_label",
|
|
return_value="SAFMN Real x4",
|
|
),
|
|
):
|
|
message = operator.response(None, result)
|
|
|
|
operator._image_target.apply.assert_called_once_with(
|
|
Path("result"),
|
|
frame_prefix="upscale.",
|
|
)
|
|
self.assertEqual(message, "Image is upscaled with SAFMN Real x4")
|