From 4b8ff0573e8b912591d02062e1240c2726e09323 Mon Sep 17 00:00:00 2001 From: rdb Date: Tue, 31 Jul 2018 14:57:55 +0200 Subject: [PATCH] tests: add TexturePool unit tests --- tests/gobj/test_texture_pool.py | 196 ++++++++++++++++++++++++++++++++ 1 file changed, 196 insertions(+) create mode 100644 tests/gobj/test_texture_pool.py diff --git a/tests/gobj/test_texture_pool.py b/tests/gobj/test_texture_pool.py new file mode 100644 index 0000000000..b07e8a0cb9 --- /dev/null +++ b/tests/gobj/test_texture_pool.py @@ -0,0 +1,196 @@ +from panda3d import core +import pytest +import tempfile + +@pytest.fixture(scope='function') +def pool(): + "This fixture ensures the pool is properly emptied" + pool = core.TexturePool + pool.release_all_textures() + yield pool + pool.release_all_textures() + + +def write_image(filename, channels): + img = core.PNMImage(1, 1, channels) + img.set_xel_a(0, 0, (0.0, 0.25, 0.5, 0.75)) + assert img.write(filename) + + +@pytest.fixture(scope='session') +def image_rgb_path(): + "Generates an RGB image." + + file = tempfile.NamedTemporaryFile(suffix='-rgb.png') + write_image(file.name, 3) + yield file.name + file.close() + + +@pytest.fixture(scope='session') +def image_rgba_path(): + "Generates an RGBA image." + + file = tempfile.NamedTemporaryFile(suffix='-rgba.png') + write_image(file.name, 4) + yield file.name + file.close() + + +@pytest.fixture(scope='session') +def image_gray_path(): + "Generates a grayscale image." + + file = tempfile.NamedTemporaryFile(suffix='-gray.png') + write_image(file.name, 1) + yield file.name + file.close() + + +def test_load_texture_rgba(pool, image_rgba_path): + tex = pool.load_texture(image_rgba_path) + assert pool.has_texture(image_rgba_path) + assert tex.num_components == 4 + + +def test_load_texture_rgba4(pool, image_rgba_path): + tex = pool.load_texture(image_rgba_path, 4) + assert pool.has_texture(image_rgba_path) + assert tex.num_components == 4 + + +def test_load_texture_rgba3(pool, image_rgba_path): + tex = pool.load_texture(image_rgba_path, 3) + assert pool.has_texture(image_rgba_path) + assert tex.num_components == 3 + + +def test_load_texture_rgba2(pool, image_rgba_path): + tex = pool.load_texture(image_rgba_path, 2) + assert pool.has_texture(image_rgba_path) + assert tex.num_components == 2 + + +def test_load_texture_rgba1(pool, image_rgba_path): + tex = pool.load_texture(image_rgba_path, 1) + assert pool.has_texture(image_rgba_path) + assert tex.num_components == 1 + + +def test_load_texture_rgb(pool, image_rgb_path): + tex = pool.load_texture(image_rgb_path) + assert pool.has_texture(image_rgb_path) + assert tex.num_components == 3 + + +def test_load_texture_rgb4(pool, image_rgb_path): + # Will not increase this + tex = pool.load_texture(image_rgb_path, 4) + assert pool.has_texture(image_rgb_path) + assert tex.num_components == 3 + + +def test_load_texture_rgb3(pool, image_rgb_path): + tex = pool.load_texture(image_rgb_path, 3) + assert pool.has_texture(image_rgb_path) + assert tex.num_components == 3 + + +def test_load_texture_rgb2(pool, image_rgb_path): + # Cannot reduce this, since it would add an alpha channel + tex = pool.load_texture(image_rgb_path, 2) + assert pool.has_texture(image_rgb_path) + assert tex.num_components == 3 + + +def test_load_texture_rgb1(pool, image_rgb_path): + tex = pool.load_texture(image_rgb_path, 1) + assert pool.has_texture(image_rgb_path) + assert tex.num_components == 1 + + +def test_load_texture_rgba_alpha(pool, image_rgba_path, image_gray_path): + tex = pool.load_texture(image_rgba_path, image_gray_path) + assert tex.num_components == 4 + + +def test_load_texture_rgba4_alpha(pool, image_rgba_path, image_gray_path): + tex = pool.load_texture(image_rgba_path, image_gray_path, 4) + assert tex.num_components == 4 + + +def test_load_texture_rgba3_alpha(pool, image_rgba_path, image_gray_path): + tex = pool.load_texture(image_rgba_path, image_gray_path, 3) + assert tex.num_components == 4 + + +def test_load_texture_rgba2_alpha(pool, image_rgba_path, image_gray_path): + #FIXME: why is this not consistent with test_load_texture_rgb2_alpha? + tex = pool.load_texture(image_rgba_path, image_gray_path, 2) + assert tex.num_components == 2 + + +def test_load_texture_rgba1_alpha(pool, image_rgba_path, image_gray_path): + tex = pool.load_texture(image_rgba_path, image_gray_path, 1) + assert tex.num_components == 2 + + +def test_load_texture_rgb_alpha(pool, image_rgb_path, image_gray_path): + tex = pool.load_texture(image_rgb_path, image_gray_path) + assert tex.num_components == 4 + + +def test_load_texture_rgb4_alpha(pool, image_rgb_path, image_gray_path): + tex = pool.load_texture(image_rgb_path, image_gray_path, 4) + assert tex.num_components == 4 + + +def test_load_texture_rgb3_alpha(pool, image_rgb_path, image_gray_path): + tex = pool.load_texture(image_rgb_path, image_gray_path, 3) + assert tex.num_components == 4 + + +def test_load_texture_rgb2_alpha(pool, image_rgb_path, image_gray_path): + #FIXME: why is this not consistent with test_load_texture_rgba2_alpha? + tex = pool.load_texture(image_rgb_path, image_gray_path, 2) + assert tex.num_components == 4 + + +def test_load_texture_rgb1_alpha(pool, image_rgb_path, image_gray_path): + tex = pool.load_texture(image_rgb_path, image_gray_path, 1) + assert tex.num_components == 2 + + +def test_reload_texture_fewer_channels(pool, image_rgba_path): + tex = pool.load_texture(image_rgba_path) + assert pool.has_texture(image_rgba_path) + assert tex.num_components == 4 + + tex = pool.load_texture(image_rgba_path, 3) + assert tex.num_components == 3 + + +def test_reload_texture_more_channels(pool, image_rgba_path): + tex = pool.load_texture(image_rgba_path, 3) + assert pool.has_texture(image_rgba_path) + assert tex.num_components == 3 + + tex = pool.load_texture(image_rgba_path) + assert tex.num_components == 4 + + +def test_reload_texture_with_alpha(pool, image_rgb_path, image_gray_path): + tex = pool.load_texture(image_rgb_path) + assert pool.has_texture(image_rgb_path) + assert tex.num_components == 3 + + tex = pool.load_texture(image_rgb_path, image_gray_path) + assert tex.num_components == 4 + + +def test_reload_texture_without_alpha(pool, image_rgb_path, image_gray_path): + tex = pool.load_texture(image_rgb_path, image_gray_path) + assert tex.num_components == 4 + + tex = pool.load_texture(image_rgb_path) + assert tex.num_components == 3