TravelContentCreator/examples/test_resource_loader.py

197 lines
6.8 KiB
Python
Raw Normal View History

2025-04-22 21:57:06 +08:00
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
测试ResourceLoader组件
该脚本演示如何独立使用resource_loader模块加载资源文件
"""
import os
import sys
import tempfile
import json
from pathlib import Path
# 将项目根目录添加到PATH
project_root = str(Path(__file__).parent.parent.absolute())
if project_root not in sys.path:
sys.path.append(project_root)
from utils.resource_loader import ResourceLoader
def create_test_files():
"""创建测试用的资源文件结构"""
# 创建临时目录
temp_dir = tempfile.mkdtemp()
# 创建资源目录结构
resources_dir = os.path.join(temp_dir, "resources")
os.makedirs(resources_dir, exist_ok=True)
# 创建各类资源子目录
image_dir = os.path.join(resources_dir, "images")
font_dir = os.path.join(resources_dir, "fonts")
sticker_dir = os.path.join(resources_dir, "stickers")
background_dir = os.path.join(resources_dir, "backgrounds")
os.makedirs(image_dir, exist_ok=True)
os.makedirs(font_dir, exist_ok=True)
os.makedirs(sticker_dir, exist_ok=True)
os.makedirs(os.path.join(background_dir, "portrait"), exist_ok=True)
os.makedirs(os.path.join(background_dir, "landscape"), exist_ok=True)
# 创建测试图片文件(创建文本文件代替)
img1_path = os.path.join(image_dir, "test1.jpg")
img2_path = os.path.join(image_dir, "test2.png")
with open(img1_path, 'w') as f:
f.write("Dummy JPG image file")
with open(img2_path, 'w') as f:
f.write("Dummy PNG image file")
# 创建字体文件
font1_path = os.path.join(font_dir, "test_font1.ttf")
font2_path = os.path.join(font_dir, "test_font2.ttf")
with open(font1_path, 'w') as f:
f.write("Dummy TTF font file 1")
with open(font2_path, 'w') as f:
f.write("Dummy TTF font file 2")
# 创建贴纸文件
sticker1_path = os.path.join(sticker_dir, "sticker1.png")
sticker2_path = os.path.join(sticker_dir, "sticker2.png")
with open(sticker1_path, 'w') as f:
f.write("Dummy sticker file 1")
with open(sticker2_path, 'w') as f:
f.write("Dummy sticker file 2")
# 创建背景文件
bg_portrait_path = os.path.join(background_dir, "portrait", "bg1.jpg")
bg_landscape_path = os.path.join(background_dir, "landscape", "bg1.jpg")
with open(bg_portrait_path, 'w') as f:
f.write("Dummy portrait background")
with open(bg_landscape_path, 'w') as f:
f.write("Dummy landscape background")
# 创建配置文件
config_path = os.path.join(temp_dir, "config.json")
config = {
"resource_dir": resources_dir,
"font_dir": font_dir,
"sticker_dir": sticker_dir,
"bg_portrait_dir": os.path.join(background_dir, "portrait"),
"bg_landscape_dir": os.path.join(background_dir, "landscape"),
"output_dir": os.path.join(temp_dir, "output")
}
with open(config_path, 'w') as f:
json.dump(config, f, indent=2)
# 返回创建的文件路径
return {
"temp_dir": temp_dir,
"resources_dir": resources_dir,
"config_path": config_path,
"image_dir": image_dir,
"font_dir": font_dir,
"sticker_dir": sticker_dir,
"bg_portrait_dir": os.path.join(background_dir, "portrait"),
"bg_landscape_dir": os.path.join(background_dir, "landscape"),
}
def main():
"""主函数"""
print("=== 测试 ResourceLoader 组件 ===")
# 创建测试文件
test_files = create_test_files()
print(f"创建临时测试文件夹: {test_files['temp_dir']}")
try:
# 加载配置
with open(test_files["config_path"], 'r') as f:
config = json.load(f)
# 初始化ResourceLoader
resource_loader = ResourceLoader(config)
# 测试1: 加载字体
print("\n测试1: 加载字体文件")
fonts = resource_loader.load_fonts()
print(f"加载字体数量: {len(fonts)}")
if len(fonts) == 2:
print("测试通过: 成功加载了2个字体文件")
else:
print(f"测试失败: 应加载2个字体文件实际加载了{len(fonts)}")
# 测试2: 加载贴纸
print("\n测试2: 加载贴纸文件")
stickers = resource_loader.load_stickers()
print(f"加载贴纸数量: {len(stickers)}")
if len(stickers) == 2:
print("测试通过: 成功加载了2个贴纸文件")
else:
print(f"测试失败: 应加载2个贴纸文件实际加载了{len(stickers)}")
# 测试3: 加载背景图
print("\n测试3: 加载背景图文件")
portrait_bgs = resource_loader.load_backgrounds("portrait")
landscape_bgs = resource_loader.load_backgrounds("landscape")
print(f"加载竖版背景数量: {len(portrait_bgs)}")
print(f"加载横版背景数量: {len(landscape_bgs)}")
if len(portrait_bgs) == 1:
print("测试通过: 成功加载了1个竖版背景文件")
else:
print(f"测试失败: 应加载1个竖版背景文件实际加载了{len(portrait_bgs)}")
if len(landscape_bgs) == 1:
print("测试通过: 成功加载了1个横版背景文件")
else:
print(f"测试失败: 应加载1个横版背景文件实际加载了{len(landscape_bgs)}")
# 测试4: 创建输出目录
print("\n测试4: 创建输出目录")
output_dir = resource_loader.get_output_dir()
if os.path.exists(output_dir):
print(f"测试通过: 成功创建输出目录 {output_dir}")
else:
print(f"测试失败: 未能成功创建输出目录 {output_dir}")
# 测试5: 异常处理 - 非法方向
print("\n测试5: 异常处理 - 加载非法方向背景")
try:
invalid_bgs = resource_loader.load_backgrounds("invalid_direction")
print("测试失败: 应该抛出异常但未抛出")
except ValueError as e:
print(f"测试通过: 成功捕获到期望的ValueError异常: {e}")
except Exception as e:
print(f"测试失败: 捕获到意外的异常类型: {type(e).__name__}")
except Exception as e:
print(f"测试过程中出错: {e}")
import traceback
traceback.print_exc()
finally:
# 清理临时文件
import shutil
try:
shutil.rmtree(test_files["temp_dir"])
print(f"\n清理临时文件: {test_files['temp_dir']}")
except Exception as e:
print(f"清理临时文件失败: {e}")
print("\n=== 测试完成 ===")
if __name__ == "__main__":
main()