| 1 |
import unittest |
| 2 |
|
| 3 |
from pyglet.image import * |
| 4 |
from pyglet.window import * |
| 5 |
|
| 6 |
|
| 7 |
def colorbyte(color): |
| 8 |
return bytes((color,)) |
| 9 |
|
| 10 |
|
| 11 |
class ImageGridTestCase(unittest.TestCase): |
| 12 |
"""Test the ImageGrid for textures.""" |
| 13 |
|
| 14 |
def set_grid_image(self, itemwidth, itemheight, rows, cols, rowpad, colpad): |
| 15 |
data = b'' |
| 16 |
color = 1 |
| 17 |
width = itemwidth * cols + colpad * (cols - 1) |
| 18 |
height = itemheight * rows + rowpad * (rows - 1) |
| 19 |
for row in range(rows): |
| 20 |
rowdata = b'' |
| 21 |
for col in range(cols): |
| 22 |
rowdata += colorbyte(color) * itemwidth |
| 23 |
if col < cols - 1: |
| 24 |
rowdata += b'\0' * colpad |
| 25 |
color += 1 |
| 26 |
|
| 27 |
data += rowdata * itemheight |
| 28 |
if row < rows - 1: |
| 29 |
data += (width * b'\0') * rowpad |
| 30 |
assert len(data) == width * height |
| 31 |
self.image = ImageData(width, height, 'R', data) |
| 32 |
self.grid = ImageGrid(self.image, rows, cols, |
| 33 |
itemwidth, itemheight, rowpad, colpad).get_texture_sequence() |
| 34 |
|
| 35 |
def check_cell(self, cellimage, cellindex): |
| 36 |
self.assertTrue(cellimage.width == self.grid.item_width) |
| 37 |
self.assertTrue(cellimage.height == self.grid.item_height) |
| 38 |
|
| 39 |
color = colorbyte(cellindex + 1) |
| 40 |
cellimage = cellimage.get_image_data() |
| 41 |
data = cellimage.get_data('R', cellimage.width) |
| 42 |
self.assertTrue(data == color * len(data)) |
| 43 |
|
| 44 |
def setUp(self): |
| 45 |
self.w = Window(visible=False) |
| 46 |
|
| 47 |
def tearDown(self) -> None: |
| 48 |
self.w.close() |
| 49 |
|
| 50 |
def testSquare(self): |
| 51 |
|
| 52 |
rows = cols = 3 |
| 53 |
self.set_grid_image(4, 4, rows, cols, 0, 0) |
| 54 |
for i in range(rows * cols): |
| 55 |
self.check_cell(self.grid[i], i) |
| 56 |
|
| 57 |
def testRect(self): |
| 58 |
|
| 59 |
rows, cols = 2, 5 |
| 60 |
self.set_grid_image(3, 8, rows, cols, 0, 0) |
| 61 |
for i in range(rows * cols): |
| 62 |
self.check_cell(self.grid[i], i) |
| 63 |
|
| 64 |
def testPad(self): |
| 65 |
|
| 66 |
rows, cols = 5, 3 |
| 67 |
self.set_grid_image(10, 9, rows, cols, 3, 7) |
| 68 |
for i in range(rows * cols): |
| 69 |
self.check_cell(self.grid[i], i) |
| 70 |
|
| 71 |
def testTuple(self): |
| 72 |
|
| 73 |
rows, cols = 3, 4 |
| 74 |
self.set_grid_image(5, 5, rows, cols, 0, 0) |
| 75 |
for row in range(rows): |
| 76 |
for col in range(cols): |
| 77 |
self.check_cell(self.grid[(row, col)], row * cols + col) |
| 78 |
|
| 79 |
def testRange(self): |
| 80 |
|
| 81 |
rows, cols = 4, 3 |
| 82 |
self.set_grid_image(10, 1, rows, cols, 0, 0) |
| 83 |
images = self.grid[4:8] |
| 84 |
for i, image in enumerate(images): |
| 85 |
self.check_cell(image, i + 4) |
| 86 |
|
| 87 |
def testTupleRange(self): |
| 88 |
|
| 89 |
rows, cols = 10, 10 |
| 90 |
self.set_grid_image(4, 4, rows, cols, 0, 0) |
| 91 |
images = self.grid[(3, 2):(6, 5)] |
| 92 |
i = 0 |
| 93 |
for row in range(3, 6): |
| 94 |
for col in range(2, 5): |
| 95 |
self.check_cell(images[i], row * cols + col) |
| 96 |
i += 1 |