接口开始5%
This commit is contained in:
@@ -3,7 +3,7 @@ import tempfile
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
|
||||
from PIL import Image
|
||||
from PIL import Image, PngImagePlugin
|
||||
|
||||
SCRIPTS = Path(__file__).resolve().parents[1] / "scripts"
|
||||
sys.path.insert(0, str(SCRIPTS))
|
||||
@@ -37,19 +37,23 @@ class AssetQualityTests(unittest.TestCase):
|
||||
image.putpixel((x, y), (150, 20, 12, 255))
|
||||
return image
|
||||
|
||||
def save(self, image, name="asset.png"):
|
||||
def save(self, image, name="asset.png", include_srgb=True):
|
||||
path = self.root / name
|
||||
image.save(path, format="PNG")
|
||||
png_info = PngImagePlugin.PngInfo()
|
||||
if include_srgb:
|
||||
png_info.add(b"sRGB", b"\x00")
|
||||
image.save(path, format="PNG", pnginfo=png_info)
|
||||
return path
|
||||
|
||||
def error_text(self, report):
|
||||
return " ".join(report["errors"])
|
||||
|
||||
def test_accepts_clean_rgba_asset(self):
|
||||
report = analyze_asset(self.save(self.clean_image()), self.spec())
|
||||
report = analyze_asset(self.save(self.clean_image()), self.spec(), self.root)
|
||||
self.assertEqual([], report["errors"])
|
||||
self.assertEqual(32, report["width"])
|
||||
self.assertEqual(16, report["height"])
|
||||
self.assertEqual("asset.png", report["path"])
|
||||
|
||||
def test_accepts_indexed_png_with_real_transparency(self):
|
||||
indexed = self.clean_image().quantize(
|
||||
@@ -57,44 +61,49 @@ class AssetQualityTests(unittest.TestCase):
|
||||
method=Image.Quantize.FASTOCTREE,
|
||||
dither=Image.Dither.NONE,
|
||||
)
|
||||
report = analyze_asset(self.save(indexed), self.spec())
|
||||
report = analyze_asset(self.save(indexed), self.spec(), self.root)
|
||||
self.assertEqual([], report["errors"])
|
||||
self.assertEqual("P", report["mode"])
|
||||
|
||||
def test_rejects_wrong_dimensions(self):
|
||||
report = analyze_asset(self.save(Image.new("RGBA", (31, 16), (0, 0, 0, 0))), self.spec())
|
||||
report = analyze_asset(self.save(Image.new("RGBA", (31, 16), (0, 0, 0, 0))), self.spec(), self.root)
|
||||
self.assertIn("dimensions", self.error_text(report))
|
||||
|
||||
def test_rejects_opaque_outer_padding(self):
|
||||
image = self.clean_image()
|
||||
image.putpixel((0, 0), (120, 30, 20, 255))
|
||||
report = analyze_asset(self.save(image), self.spec())
|
||||
report = analyze_asset(self.save(image), self.spec(), self.root)
|
||||
self.assertIn("outer padding", self.error_text(report))
|
||||
|
||||
def test_rejects_visible_green_residue(self):
|
||||
image = self.clean_image()
|
||||
image.putpixel((16, 8), (0, 255, 0, 255))
|
||||
report = analyze_asset(self.save(image), self.spec())
|
||||
report = analyze_asset(self.save(image), self.spec(), self.root)
|
||||
self.assertIn("chroma residue", self.error_text(report))
|
||||
|
||||
def test_rejects_light_partially_transparent_fringe(self):
|
||||
image = self.clean_image()
|
||||
image.putpixel((2, 8), (250, 250, 250, 128))
|
||||
report = analyze_asset(self.save(image), self.spec())
|
||||
report = analyze_asset(self.save(image), self.spec(), self.root)
|
||||
self.assertIn("light fringe", self.error_text(report))
|
||||
|
||||
def test_rejects_hidden_rgb_in_fully_transparent_pixels(self):
|
||||
image = self.clean_image()
|
||||
image.putpixel((0, 0), (255, 255, 255, 0))
|
||||
report = analyze_asset(self.save(image), self.spec())
|
||||
report = analyze_asset(self.save(image), self.spec(), self.root)
|
||||
self.assertIn("transparent RGB", self.error_text(report))
|
||||
|
||||
def test_rejects_file_over_max_bytes(self):
|
||||
spec = self.spec()
|
||||
spec["quality"]["maxBytes"] = 8
|
||||
report = analyze_asset(self.save(self.clean_image()), spec)
|
||||
report = analyze_asset(self.save(self.clean_image()), spec, self.root)
|
||||
self.assertIn("maxBytes", self.error_text(report))
|
||||
|
||||
def test_rejects_missing_declared_srgb_metadata(self):
|
||||
path = self.save(self.clean_image(), include_srgb=False)
|
||||
report = analyze_asset(path, self.spec(), self.root)
|
||||
self.assertIn("sRGB", self.error_text(report))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
||||
Reference in New Issue
Block a user