|
@@ -1,8 +1,4 @@
|
|
|
-using System;
|
|
|
-using System.IO;
|
|
|
-using Masuit.Tools.DigtalWatermarker;
|
|
|
-using OpenCvSharp;
|
|
|
-using Xunit;
|
|
|
+using OpenCvSharp;
|
|
|
|
|
|
namespace Masuit.Tools.DigitalWatermarker.Test;
|
|
|
|
|
@@ -26,7 +22,7 @@ public class DigitalWatermarkerTests : IDisposable
|
|
|
|
|
|
// 创建测试用的源图像 (512x512 彩色图像)
|
|
|
_sourceImage = CreateTestSourceImage();
|
|
|
-
|
|
|
+
|
|
|
// 创建测试用的水印图像 (64x64 二值图像)
|
|
|
_watermarkImage = CreateTestWatermarkImage();
|
|
|
|
|
@@ -39,7 +35,7 @@ public class DigitalWatermarkerTests : IDisposable
|
|
|
{
|
|
|
_sourceImage?.Dispose();
|
|
|
_watermarkImage?.Dispose();
|
|
|
-
|
|
|
+
|
|
|
// 清理测试文件
|
|
|
if (Directory.Exists(_testImageDirectory))
|
|
|
{
|
|
@@ -69,7 +65,7 @@ public class DigitalWatermarkerTests : IDisposable
|
|
|
using var emptyMat = new Mat();
|
|
|
|
|
|
// Act & Assert
|
|
|
- var exception = Assert.Throws<ArgumentException>(() =>
|
|
|
+ var exception = Assert.Throws<ArgumentException>(() =>
|
|
|
Masuit.Tools.DigtalWatermarker.DigitalWatermarker.EmbedWatermark(emptyMat, _watermarkImage));
|
|
|
Assert.Contains("source is empty", exception.Message);
|
|
|
}
|
|
@@ -81,7 +77,7 @@ public class DigitalWatermarkerTests : IDisposable
|
|
|
using var emptyMat = new Mat();
|
|
|
|
|
|
// Act & Assert
|
|
|
- var exception = Assert.Throws<ArgumentException>(() =>
|
|
|
+ var exception = Assert.Throws<ArgumentException>(() =>
|
|
|
Masuit.Tools.DigtalWatermarker.DigitalWatermarker.EmbedWatermark(_sourceImage, emptyMat));
|
|
|
Assert.Contains("watermark is empty", exception.Message);
|
|
|
}
|
|
@@ -108,12 +104,12 @@ public class DigitalWatermarkerTests : IDisposable
|
|
|
using var emptyMat = new Mat();
|
|
|
|
|
|
// Act & Assert
|
|
|
- var exception = Assert.Throws<ArgumentException>(() =>
|
|
|
+ var exception = Assert.Throws<ArgumentException>(() =>
|
|
|
Masuit.Tools.DigtalWatermarker.DigitalWatermarker.ExtractWatermark(emptyMat));
|
|
|
Assert.Contains("image is empty", exception.Message);
|
|
|
}
|
|
|
|
|
|
- #endregion
|
|
|
+ #endregion 测试Mat对象方法
|
|
|
|
|
|
#region 测试文件路径方法
|
|
|
|
|
@@ -134,7 +130,7 @@ public class DigitalWatermarkerTests : IDisposable
|
|
|
public void EmbedWatermark_WithNullSourcePath_ShouldThrowArgumentException()
|
|
|
{
|
|
|
// Act & Assert
|
|
|
- var exception = Assert.Throws<ArgumentException>(() =>
|
|
|
+ var exception = Assert.Throws<ArgumentException>(() =>
|
|
|
Masuit.Tools.DigtalWatermarker.DigitalWatermarker.EmbedWatermark(null, _watermarkImagePath));
|
|
|
Assert.Contains("图片路径不能为空", exception.Message);
|
|
|
}
|
|
@@ -143,7 +139,7 @@ public class DigitalWatermarkerTests : IDisposable
|
|
|
public void EmbedWatermark_WithEmptySourcePath_ShouldThrowArgumentException()
|
|
|
{
|
|
|
// Act & Assert
|
|
|
- var exception = Assert.Throws<ArgumentException>(() =>
|
|
|
+ var exception = Assert.Throws<ArgumentException>(() =>
|
|
|
Masuit.Tools.DigtalWatermarker.DigitalWatermarker.EmbedWatermark("", _watermarkImagePath));
|
|
|
Assert.Contains("图片路径不能为空", exception.Message);
|
|
|
}
|
|
@@ -152,7 +148,7 @@ public class DigitalWatermarkerTests : IDisposable
|
|
|
public void EmbedWatermark_WithNullWatermarkPath_ShouldThrowArgumentException()
|
|
|
{
|
|
|
// Act & Assert
|
|
|
- var exception = Assert.Throws<ArgumentException>(() =>
|
|
|
+ var exception = Assert.Throws<ArgumentException>(() =>
|
|
|
Masuit.Tools.DigtalWatermarker.DigitalWatermarker.EmbedWatermark(_sourceImagePath, null));
|
|
|
Assert.Contains("水印图片路径不能为空", exception.Message);
|
|
|
}
|
|
@@ -161,7 +157,7 @@ public class DigitalWatermarkerTests : IDisposable
|
|
|
public void EmbedWatermark_WithNonExistentSourceFile_ShouldThrowFileNotFoundException()
|
|
|
{
|
|
|
// Act & Assert
|
|
|
- var exception = Assert.Throws<FileNotFoundException>(() =>
|
|
|
+ var exception = Assert.Throws<FileNotFoundException>(() =>
|
|
|
Masuit.Tools.DigtalWatermarker.DigitalWatermarker.EmbedWatermark("nonexistent.jpg", _watermarkImagePath));
|
|
|
Assert.Contains("文件不存在", exception.Message);
|
|
|
}
|
|
@@ -170,7 +166,7 @@ public class DigitalWatermarkerTests : IDisposable
|
|
|
public void EmbedWatermark_WithNonExistentWatermarkFile_ShouldThrowFileNotFoundException()
|
|
|
{
|
|
|
// Act & Assert
|
|
|
- var exception = Assert.Throws<FileNotFoundException>(() =>
|
|
|
+ var exception = Assert.Throws<FileNotFoundException>(() =>
|
|
|
Masuit.Tools.DigtalWatermarker.DigitalWatermarker.EmbedWatermark(_sourceImagePath, "nonexistent.png"));
|
|
|
Assert.Contains("文件不存在", exception.Message);
|
|
|
}
|
|
@@ -195,7 +191,7 @@ public class DigitalWatermarkerTests : IDisposable
|
|
|
public void ExtractWatermark_WithNullPath_ShouldThrowArgumentException()
|
|
|
{
|
|
|
// Act & Assert
|
|
|
- var exception = Assert.Throws<ArgumentException>(() =>
|
|
|
+ var exception = Assert.Throws<ArgumentException>(() =>
|
|
|
Masuit.Tools.DigtalWatermarker.DigitalWatermarker.ExtractWatermark((string)null!));
|
|
|
Assert.Contains("路径不能为空", exception.Message);
|
|
|
}
|
|
@@ -204,12 +200,12 @@ public class DigitalWatermarkerTests : IDisposable
|
|
|
public void ExtractWatermark_WithNonExistentFile_ShouldThrowFileNotFoundException()
|
|
|
{
|
|
|
// Act & Assert
|
|
|
- var exception = Assert.Throws<FileNotFoundException>(() =>
|
|
|
+ var exception = Assert.Throws<FileNotFoundException>(() =>
|
|
|
Masuit.Tools.DigtalWatermarker.DigitalWatermarker.ExtractWatermark("nonexistent.jpg"));
|
|
|
Assert.Contains("文件不存在", exception.Message);
|
|
|
}
|
|
|
|
|
|
- #endregion
|
|
|
+ #endregion 测试文件路径方法
|
|
|
|
|
|
#region 测试Stream方法
|
|
|
|
|
@@ -219,13 +215,13 @@ public class DigitalWatermarkerTests : IDisposable
|
|
|
// Arrange
|
|
|
using var sourceStream = new MemoryStream();
|
|
|
using var watermarkStream = new MemoryStream();
|
|
|
-
|
|
|
+
|
|
|
Cv2.ImEncode(".jpg", _sourceImage, out var sourceBytes);
|
|
|
Cv2.ImEncode(".png", _watermarkImage, out var watermarkBytes);
|
|
|
-
|
|
|
+
|
|
|
sourceStream.Write(sourceBytes);
|
|
|
watermarkStream.Write(watermarkBytes);
|
|
|
-
|
|
|
+
|
|
|
sourceStream.Position = 0;
|
|
|
watermarkStream.Position = 0;
|
|
|
|
|
@@ -246,7 +242,7 @@ public class DigitalWatermarkerTests : IDisposable
|
|
|
using var watermarkStream = new MemoryStream();
|
|
|
|
|
|
// Act & Assert
|
|
|
- Assert.Throws<ArgumentNullException>(() =>
|
|
|
+ Assert.Throws<ArgumentNullException>(() =>
|
|
|
Masuit.Tools.DigtalWatermarker.DigitalWatermarker.EmbedWatermark(null, watermarkStream));
|
|
|
}
|
|
|
|
|
@@ -257,7 +253,7 @@ public class DigitalWatermarkerTests : IDisposable
|
|
|
using var sourceStream = new MemoryStream();
|
|
|
|
|
|
// Act & Assert
|
|
|
- Assert.Throws<ArgumentNullException>(() =>
|
|
|
+ Assert.Throws<ArgumentNullException>(() =>
|
|
|
Masuit.Tools.DigtalWatermarker.DigitalWatermarker.EmbedWatermark(sourceStream, null));
|
|
|
}
|
|
|
|
|
@@ -267,7 +263,7 @@ public class DigitalWatermarkerTests : IDisposable
|
|
|
// Arrange
|
|
|
using var watermarkedImage = Masuit.Tools.DigtalWatermarker.DigitalWatermarker.EmbedWatermark(_sourceImage, _watermarkImage);
|
|
|
using var imageStream = new MemoryStream();
|
|
|
-
|
|
|
+
|
|
|
Cv2.ImEncode(".jpg", watermarkedImage, out var imageBytes);
|
|
|
imageStream.Write(imageBytes);
|
|
|
imageStream.Position = 0;
|
|
@@ -284,7 +280,7 @@ public class DigitalWatermarkerTests : IDisposable
|
|
|
public void ExtractWatermark_WithNullStream_ShouldThrowArgumentNullException()
|
|
|
{
|
|
|
// Act & Assert
|
|
|
- Assert.Throws<ArgumentNullException>(() =>
|
|
|
+ Assert.Throws<ArgumentNullException>(() =>
|
|
|
Masuit.Tools.DigtalWatermarker.DigitalWatermarker.ExtractWatermark((Stream)null!));
|
|
|
}
|
|
|
|
|
@@ -295,12 +291,12 @@ public class DigitalWatermarkerTests : IDisposable
|
|
|
using var invalidStream = new MemoryStream(new byte[] { 1, 2, 3, 4, 5 });
|
|
|
|
|
|
// Act & Assert
|
|
|
- var exception = Assert.Throws<ArgumentException>(() =>
|
|
|
+ var exception = Assert.Throws<ArgumentException>(() =>
|
|
|
Masuit.Tools.DigtalWatermarker.DigitalWatermarker.ExtractWatermark(invalidStream));
|
|
|
Assert.Contains("stream不能解析为图像", exception.Message);
|
|
|
}
|
|
|
|
|
|
- #endregion
|
|
|
+ #endregion 测试Stream方法
|
|
|
|
|
|
#region 集成测试
|
|
|
|
|
@@ -326,7 +322,7 @@ public class DigitalWatermarkerTests : IDisposable
|
|
|
{
|
|
|
// Arrange
|
|
|
using var watermarkedImage = Masuit.Tools.DigtalWatermarker.DigitalWatermarker.EmbedWatermark(_sourceImage, _watermarkImage);
|
|
|
-
|
|
|
+
|
|
|
// 模拟JPEG压缩
|
|
|
var compressionParams = new int[] { (int)ImwriteFlags.JpegQuality, 75 };
|
|
|
Cv2.ImEncode(".jpg", watermarkedImage, out var compressedBytes, compressionParams);
|
|
@@ -340,14 +336,14 @@ public class DigitalWatermarkerTests : IDisposable
|
|
|
Assert.False(extractedWatermark.Empty());
|
|
|
}
|
|
|
|
|
|
- #endregion
|
|
|
+ #endregion 集成测试
|
|
|
|
|
|
#region 辅助方法
|
|
|
|
|
|
private static Mat CreateTestSourceImage()
|
|
|
{
|
|
|
var image = new Mat(512, 512, MatType.CV_8UC3);
|
|
|
-
|
|
|
+
|
|
|
// 创建渐变背景
|
|
|
for (int y = 0; y < image.Rows; y++)
|
|
|
{
|
|
@@ -357,14 +353,14 @@ public class DigitalWatermarkerTests : IDisposable
|
|
|
image.Set(y, x, new Vec3b(intensity, (byte)(255 - intensity), (byte)(intensity / 2)));
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
return image;
|
|
|
}
|
|
|
|
|
|
private static Mat CreateTestWatermarkImage()
|
|
|
{
|
|
|
var watermark = new Mat(64, 64, MatType.CV_8UC1, Scalar.Black);
|
|
|
-
|
|
|
+
|
|
|
// 创建简单的棋盘格模式
|
|
|
for (int y = 0; y < watermark.Rows; y++)
|
|
|
{
|
|
@@ -376,14 +372,14 @@ public class DigitalWatermarkerTests : IDisposable
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
return watermark;
|
|
|
}
|
|
|
|
|
|
private static Mat CreatePatternWatermark()
|
|
|
{
|
|
|
var watermark = new Mat(32, 32, MatType.CV_8UC1, Scalar.Black);
|
|
|
-
|
|
|
+
|
|
|
// 创建十字形模式
|
|
|
int center = 16;
|
|
|
for (int i = 0; i < 32; i++)
|
|
@@ -391,9 +387,9 @@ public class DigitalWatermarkerTests : IDisposable
|
|
|
watermark.Set(center, i, (byte)255); // 水平线
|
|
|
watermark.Set(i, center, (byte)255); // 垂直线
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
return watermark;
|
|
|
}
|
|
|
|
|
|
- #endregion
|
|
|
-}
|
|
|
+ #endregion 辅助方法
|
|
|
+}
|