From 26db8ec2bf63dc747b6f12f775c22ca061a22437 Mon Sep 17 00:00:00 2001 From: "Leo.Qin" Date: Thu, 22 May 2025 19:04:44 +0800 Subject: [PATCH] =?UTF-8?q?REPORT-152615=20=E6=B7=BB=E5=8A=A0=E5=8D=95?= =?UTF-8?q?=E6=B5=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../theme/panel/ImageUtilsTest.java | 93 +++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 designer-base/src/test/java/com/fr/widgettheme/theme/panel/ImageUtilsTest.java diff --git a/designer-base/src/test/java/com/fr/widgettheme/theme/panel/ImageUtilsTest.java b/designer-base/src/test/java/com/fr/widgettheme/theme/panel/ImageUtilsTest.java new file mode 100644 index 0000000000..a1d2c875a4 --- /dev/null +++ b/designer-base/src/test/java/com/fr/widgettheme/theme/panel/ImageUtilsTest.java @@ -0,0 +1,93 @@ +package com.fr.widgettheme.theme.panel; + +import org.junit.Test; + +import javax.swing.ImageIcon; +import java.awt.Color; +import java.awt.Graphics2D; +import java.awt.image.BufferedImage; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + + +/** + * @author Leo.Qin + * @since 11.0 + * Created on 2025/5/22 + */ +public class ImageUtilsTest { + + @Test + public void testColorImage() { + // 创建一个测试用的BufferedImage + BufferedImage image = new BufferedImage(10, 10, BufferedImage.TYPE_INT_ARGB); + Color testColor = new Color(123, 45, 67); + + // 执行测试方法 + BufferedImage result = ImageUtils.colorImage(image, testColor); + + // 验证结果 + assertNotNull(result); + assertEquals(10, result.getWidth()); + assertEquals(10, result.getHeight()); + + // 检查图像中的像素颜色是否符合预期 + int[] pixel = new int[4]; + result.getRaster().getPixel(5, 5, pixel); + assertEquals(testColor.getRed(), pixel[0]); + assertEquals(testColor.getGreen(), pixel[1]); + assertEquals(testColor.getBlue(), pixel[2]); + } + + @Test + public void testImageIconToBufferedImage() { + // 创建一个简单的测试ImageIcon + BufferedImage srcImage = new BufferedImage(20, 20, BufferedImage.TYPE_INT_ARGB); + ImageIcon icon = new ImageIcon(srcImage); + + // 执行测试方法 + BufferedImage result = ImageUtils.imageIconToBufferedImage(icon); + + // 验证结果 + assertNotNull(result); + assertEquals(icon.getIconWidth(), result.getWidth()); + assertEquals(icon.getIconHeight(), result.getHeight()); + assertEquals(BufferedImage.TYPE_INT_ARGB, result.getType()); + } + + @Test + public void testPixelProcessing() { + // 测试边界情况下的像素处理 + BufferedImage image = new BufferedImage(2, 2, BufferedImage.TYPE_INT_ARGB); + Graphics2D g2d = image.createGraphics(); + + // 设置像素值为边界值 + g2d.setColor(new Color(0, 0, 0, 255)); + g2d.fillRect(0, 0, 1, 1); + + g2d.setColor(new Color(255, 255, 255, 255)); + g2d.fillRect(1, 1, 1, 1); + + g2d.dispose(); + + // 使用自定义颜色处理图像 + Color customColor = new Color(100, 150, 200); + BufferedImage result = ImageUtils.colorImage(image, customColor); + + // 验证处理后的像素值 + int[] pixel1 = new int[4]; + int[] pixel2 = new int[4]; + result.getRaster().getPixel(0, 0, pixel1); + result.getRaster().getPixel(1, 1, pixel2); + + // 两个像素点应该都被处理成自定义颜色 + assertEquals(customColor.getRed(), pixel1[0]); + assertEquals(customColor.getGreen(), pixel1[1]); + assertEquals(customColor.getBlue(), pixel1[2]); + + assertEquals(customColor.getRed(), pixel2[0]); + assertEquals(customColor.getGreen(), pixel2[1]); + assertEquals(customColor.getBlue(), pixel2[2]); + } +} \ No newline at end of file