feat: 功能优化
This commit is contained in:
@@ -0,0 +1,110 @@
|
||||
package cn.iocoder.yudao.module.tik.media;
|
||||
|
||||
import cn.iocoder.yudao.module.tik.file.service.TikOssInitService;
|
||||
import cn.iocoder.yudao.module.tik.mix.config.IceProperties;
|
||||
import com.alibaba.fastjson.JSONArray;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* BatchProduceAlignment 单元测试
|
||||
*/
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class BatchProduceAlignmentTest {
|
||||
|
||||
@Mock
|
||||
private IceProperties iceProperties;
|
||||
|
||||
@Mock
|
||||
private TikOssInitService ossInitService;
|
||||
|
||||
private BatchProduceAlignment batchProduceAlignment;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
batchProduceAlignment = new BatchProduceAlignment(iceProperties, null, ossInitService);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testCalculateCropParams_centerMode() {
|
||||
// 16:9横屏素材 (1920x1080) -> 9:16竖屏裁剪
|
||||
Map<String, Integer> cropParams = callCalculateCropParams(1920, 1080, "center");
|
||||
|
||||
// 验证居中裁剪参数
|
||||
assertThat(cropParams.get("X")).isEqualTo(656); // (1920 - 608) / 2
|
||||
assertThat(cropParams.get("Y")).isZero();
|
||||
assertThat(cropParams.get("Width")).isEqualTo(608); // 1080 * (9/16)
|
||||
assertThat(cropParams.get("Height")).isEqualTo(1080);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testCalculateCropParams_smartMode() {
|
||||
// smart模式目前实现与center相同
|
||||
Map<String, Integer> cropParams = callCalculateCropParams(1920, 1080, "smart");
|
||||
|
||||
assertThat(cropParams.get("X")).isEqualTo(656);
|
||||
assertThat(cropParams.get("Y")).isZero();
|
||||
assertThat(cropParams.get("Width")).isEqualTo(608);
|
||||
assertThat(cropParams.get("Height")).isEqualTo(1080);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testCalculateCropParams_fillMode() {
|
||||
// fill模式不裁剪,保留原尺寸
|
||||
Map<String, Integer> cropParams = callCalculateCropParams(1920, 1080, "fill");
|
||||
|
||||
assertThat(cropParams.get("X")).isZero();
|
||||
assertThat(cropParams.get("Y")).isZero();
|
||||
assertThat(cropParams.get("Width")).isEqualTo(1920);
|
||||
assertThat(cropParams.get("Height")).isEqualTo(1080);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testCalculateCropParams_differentAspectRatios() {
|
||||
// 测试不同分辨率的横屏素材
|
||||
Map<String, Integer> cropParams1 = callCalculateCropParams(1280, 720, "center");
|
||||
assertThat(cropParams1.get("Width")).isEqualTo(405); // 720 * (9/16)
|
||||
assertThat(cropParams1.get("Height")).isEqualTo(720);
|
||||
|
||||
// 测试正方形素材
|
||||
Map<String, Integer> cropParams2 = callCalculateCropParams(1080, 1080, "center");
|
||||
assertThat(cropParams2.get("Width")).isEqualTo(608); // 1080 * (9/16)
|
||||
assertThat(cropParams2.get("Height")).isEqualTo(1080);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testCalculateCropParams_defaultToCenter() {
|
||||
// 默认应该是center模式
|
||||
Map<String, Integer> cropParams = callCalculateCropParams(1920, 1080, null);
|
||||
|
||||
assertThat(cropParams.get("X")).isEqualTo(656);
|
||||
assertThat(cropParams.get("Y")).isZero();
|
||||
assertThat(cropParams.get("Width")).isEqualTo(608);
|
||||
assertThat(cropParams.get("Height")).isEqualTo(1080);
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过反射调用私有方法 calculateCropParams
|
||||
*/
|
||||
private Map<String, Integer> callCalculateCropParams(int sourceWidth, int sourceHeight, String cropMode) {
|
||||
try {
|
||||
java.lang.reflect.Method method = BatchProduceAlignment.class.getDeclaredMethod(
|
||||
"calculateCropParams", int.class, int.class, String.class);
|
||||
method.setAccessible(true);
|
||||
@SuppressWarnings("unchecked")
|
||||
Map<String, Integer> result = (Map<String, Integer>) method.invoke(
|
||||
batchProduceAlignment, sourceWidth, sourceHeight, cropMode);
|
||||
return result;
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("Failed to invoke calculateCropParams via reflection", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user