| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
from unittest.mock import MagicMock, patch |
| 4 |
|
| 5 |
import pytest |
| 6 |
|
| 7 |
import pyglet |
| 8 |
|
| 9 |
|
| 10 |
@pytest.fixture(autouse=True) |
| 11 |
def monkeypatch_default_sprite_shader(monkeypatch, get_dummy_shader_program): |
| 12 |
"""Use a dummy shader when testing non-drawing functionality""" |
| 13 |
monkeypatch.setattr('pyglet.sprite.get_default_shader', get_dummy_shader_program) |
| 14 |
|
| 15 |
|
| 16 |
@pytest.fixture() |
| 17 |
def sprite(): |
| 18 |
"""A sprite with no image data. |
| 19 |
|
| 20 |
It is created at a non-zero position so that the update method can |
| 21 |
be verified as working when passed zero positions (Issue #673). |
| 22 |
Scale values do not need to be set to 0 in the fixture as they are |
| 23 |
all 1.0 by default. |
| 24 |
|
| 25 |
The lack of image data doesn't matter because these tests never touch |
| 26 |
a real GL context which would require it. |
| 27 |
""" |
| 28 |
sprite = pyglet.sprite.Sprite(MagicMock(), x=1, y=2, z=3) |
| 29 |
sprite.rotation = 90 |
| 30 |
|
| 31 |
return sprite |
| 32 |
|
| 33 |
|
| 34 |
@pytest.fixture() |
| 35 |
def batched_sprite(): |
| 36 |
"""A sprite with no image data, but is initially added to a batch. |
| 37 |
|
| 38 |
It is created at a non-zero position so that the update method can |
| 39 |
be verified as working when passed zero positions (Issue #673). |
| 40 |
Scale values do not need to be set to 0 in the fixture as they are |
| 41 |
all 1.0 by default. |
| 42 |
|
| 43 |
The lack of image data doesn't matter because these tests never touch |
| 44 |
a real GL context which would require it. |
| 45 |
""" |
| 46 |
from pyglet.graphics import Batch |
| 47 |
sprite = pyglet.sprite.Sprite(MagicMock(), x=1, y=2, z=3, batch=Batch()) |
| 48 |
sprite.rotation = 90 |
| 49 |
|
| 50 |
return sprite |
| 51 |
|
| 52 |
|
| 53 |
def _new_or_none(new_value: float = 0.0) -> tuple[float, None]: |
| 54 |
return new_value, None |
| 55 |
|
| 56 |
|
| 57 |
@pytest.fixture(params=_new_or_none()) |
| 58 |
def x(request): |
| 59 |
return request.param |
| 60 |
|
| 61 |
|
| 62 |
@pytest.fixture(params=_new_or_none()) |
| 63 |
def y(request): |
| 64 |
return request.param |
| 65 |
|
| 66 |
|
| 67 |
@pytest.fixture(params=_new_or_none()) |
| 68 |
def z(request): |
| 69 |
return request.param |
| 70 |
|
| 71 |
|
| 72 |
@pytest.fixture(params=_new_or_none()) |
| 73 |
def scale(request): |
| 74 |
return request.param |
| 75 |
|
| 76 |
|
| 77 |
@pytest.fixture(params=_new_or_none()) |
| 78 |
def scale_x(request): |
| 79 |
return request.param |
| 80 |
|
| 81 |
|
| 82 |
@pytest.fixture(params=_new_or_none()) |
| 83 |
def scale_y(request): |
| 84 |
return request.param |
| 85 |
|
| 86 |
|
| 87 |
def test_color_initially_rgba_white(sprite): |
| 88 |
assert sprite.color == (255, 255, 255, 255) |
| 89 |
|
| 90 |
|
| 91 |
def test_opacity_initially_255(sprite): |
| 92 |
assert sprite.opacity == 255 |
| 93 |
|
| 94 |
def test_color_changes_opacity_if_set_to_rgba(sprite, new_rgba_color): |
| 95 |
sprite.color = new_rgba_color |
| 96 |
assert sprite.opacity == new_rgba_color[3] |
| 97 |
|
| 98 |
|
| 99 |
def test_color_leaves_opacity_unchanged_if_set_to_rgb(sprite, new_rgb_color): |
| 100 |
sprite.color = new_rgb_color |
| 101 |
assert sprite.opacity == 255 |
| 102 |
|
| 103 |
|
| 104 |
def test_color_changes_rgb_channels(sprite, new_rgb_or_rgba_color): |
| 105 |
sprite.color = new_rgb_or_rgba_color |
| 106 |
assert sprite.color[:3] == new_rgb_or_rgba_color[:3] |
| 107 |
|
| 108 |
|
| 109 |
def test_opacity_setter_changes_opacity_channel_in_color(sprite, new_opacity_set_opacity_alone): |
| 110 |
sprite.opacity = new_opacity_set_opacity_alone |
| 111 |
assert sprite.color[3] == new_opacity_set_opacity_alone |
| 112 |
|
| 113 |
|
| 114 |
def test_update_sets_passed_positions(sprite, x, y, z): |
| 115 |
sprite.update(x=x, y=y, z=z) |
| 116 |
|
| 117 |
if x is not None: |
| 118 |
assert sprite.x == x |
| 119 |
|
| 120 |
if y is not None: |
| 121 |
assert sprite.y == y |
| 122 |
|
| 123 |
if z is not None: |
| 124 |
assert sprite.z == z |
| 125 |
|
| 126 |
|
| 127 |
def test_update_leaves_none_translations_alone(sprite, x, y, z): |
| 128 |
o_x, o_y, o_z = sprite.x, sprite.y, sprite.z |
| 129 |
|
| 130 |
sprite.update(x=x, y=y, z=z) |
| 131 |
|
| 132 |
if x is None: |
| 133 |
assert sprite.x == o_x |
| 134 |
|
| 135 |
if y is None: |
| 136 |
assert sprite.y == o_y |
| 137 |
|
| 138 |
if z is None: |
| 139 |
assert sprite.z == o_z |
| 140 |
|
| 141 |
|
| 142 |
def test_update_sets_passed_scales(sprite, scale, scale_x, scale_y): |
| 143 |
sprite.update(scale=scale, scale_x=scale_x, scale_y=scale_y) |
| 144 |
|
| 145 |
if scale is not None: |
| 146 |
assert sprite.scale == scale |
| 147 |
|
| 148 |
if scale_x is not None: |
| 149 |
assert sprite.scale_x == scale_x |
| 150 |
|
| 151 |
if scale_y is not None: |
| 152 |
assert sprite.scale_y == scale_y |
| 153 |
|
| 154 |
|
| 155 |
def test_update_leaves_none_scales_alone(sprite, scale, scale_x, scale_y): |
| 156 |
o_scale, o_scale_x, o_scale_y = sprite.scale, sprite.scale_x, sprite.scale_y |
| 157 |
|
| 158 |
sprite.update(scale=scale, scale_x=scale_x, scale_y=scale_y) |
| 159 |
|
| 160 |
if scale is None: |
| 161 |
assert sprite.scale == o_scale |
| 162 |
|
| 163 |
if scale_x is None: |
| 164 |
assert sprite.scale_x == o_scale_x |
| 165 |
|
| 166 |
if scale_y is None: |
| 167 |
assert sprite.scale_y == o_scale_y |
| 168 |
|
| 169 |
|
| 170 |
def test_update_sets_rotation_when_passed(sprite): |
| 171 |
sprite.update(rotation=0.0) |
| 172 |
assert sprite.rotation == 0.0 |
| 173 |
|
| 174 |
|
| 175 |
def test_update_leaves_rotation_alone_when_none(sprite): |
| 176 |
sprite.update() |
| 177 |
assert sprite.rotation == 90 |
| 178 |
|
| 179 |
|
| 180 |
@pytest.mark.parametrize('fixture', ['sprite', 'batched_sprite']) |
| 181 |
def test_group_setter(request, fixture): |
| 182 |
_sprite = request.getfixturevalue(fixture) |
| 183 |
from pyglet.graphics import Group |
| 184 |
new_group = Group() |
| 185 |
|
| 186 |
with patch.multiple(_sprite._vertex_list, indexed=True, instanced=False): |
| 187 |
_sprite.group = new_group |
| 188 |
|
| 189 |
assert _sprite.group is new_group |
| 190 |
|
| 191 |
|
| 192 |
@pytest.mark.parametrize('fixture', ['sprite', 'batched_sprite']) |
| 193 |
def test_batch_setter(request, fixture): |
| 194 |
_sprite = request.getfixturevalue(fixture) |
| 195 |
from pyglet.graphics import Batch |
| 196 |
new_batch = Batch() |
| 197 |
with patch.multiple(_sprite._vertex_list, indexed=True, instanced=False): |
| 198 |
_sprite.batch = new_batch |
| 199 |
assert _sprite.batch is new_batch |
| 200 |
|
| 201 |
|
| 202 |
@pytest.mark.parametrize('fixture', ['sprite', 'batched_sprite']) |
| 203 |
def test_program_setter(request, fixture): |
| 204 |
_sprite = request.getfixturevalue(fixture) |
| 205 |
|
| 206 |
program = MagicMock() |
| 207 |
with patch.multiple(_sprite._vertex_list, indexed=True, instanced=False): |
| 208 |
_sprite.program = program |
| 209 |
assert _sprite.program == program |
| 210 |
|
| 211 |
|
| 212 |
@pytest.mark.parametrize('fixture', ['sprite', 'batched_sprite']) |
| 213 |
def test_blend_setter(request, fixture): |
| 214 |
_sprite = request.getfixturevalue(fixture) |
| 215 |
|
| 216 |
blend_mode = (1, 1) |
| 217 |
with patch.multiple(_sprite._vertex_list, indexed=True, instanced=False): |
| 218 |
_sprite.blend_mode = blend_mode |
| 219 |
assert _sprite._group.blend_src == 1 |
| 220 |
assert _sprite._group.blend_dest == 1 |