懒得勤快 1 mese fa
parent
commit
6e1c9edafe

+ 6 - 6
Masuit.Tools.sln

@@ -33,7 +33,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BenchmarkTest", "BenchmarkT
 EndProject
 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Masuit.Tools.DigtalWatermarker", "Masuit.Tools.DigtalWatermarker\Masuit.Tools.DigtalWatermarker.csproj", "{2CB82B76-8F91-4826-A40B-B30FBB9A5108}"
 EndProject
-Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Masuit.Tools.DigitalWatermarker.Test", "Masuit.Tools.DigitalWatermarker.Test\Masuit.Tools.DigitalWatermarker.Test.csproj", "{C4224795-2CA3-439D-9B52-A0C76764F6CB}"
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Masuit.Tools.DigitalWatermarker.Test", "Test\Masuit.Tools.DigitalWatermarker.Test\Masuit.Tools.DigitalWatermarker.Test.csproj", "{4AA0AE41-A460-832A-A001-E440E294BF54}"
 EndProject
 Global
 	GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -97,10 +97,10 @@ Global
 		{2CB82B76-8F91-4826-A40B-B30FBB9A5108}.Debug|Any CPU.Build.0 = Debug|Any CPU
 		{2CB82B76-8F91-4826-A40B-B30FBB9A5108}.Release|Any CPU.ActiveCfg = Release|Any CPU
 		{2CB82B76-8F91-4826-A40B-B30FBB9A5108}.Release|Any CPU.Build.0 = Release|Any CPU
-		{C4224795-2CA3-439D-9B52-A0C76764F6CB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
-		{C4224795-2CA3-439D-9B52-A0C76764F6CB}.Debug|Any CPU.Build.0 = Debug|Any CPU
-		{C4224795-2CA3-439D-9B52-A0C76764F6CB}.Release|Any CPU.ActiveCfg = Release|Any CPU
-		{C4224795-2CA3-439D-9B52-A0C76764F6CB}.Release|Any CPU.Build.0 = Release|Any CPU
+		{4AA0AE41-A460-832A-A001-E440E294BF54}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+		{4AA0AE41-A460-832A-A001-E440E294BF54}.Debug|Any CPU.Build.0 = Debug|Any CPU
+		{4AA0AE41-A460-832A-A001-E440E294BF54}.Release|Any CPU.ActiveCfg = Release|Any CPU
+		{4AA0AE41-A460-832A-A001-E440E294BF54}.Release|Any CPU.Build.0 = Release|Any CPU
 	EndGlobalSection
 	GlobalSection(SolutionProperties) = preSolution
 		HideSolutionNode = FALSE
@@ -111,7 +111,7 @@ Global
 		{99185F12-DFAD-4DB8-A2C4-C7E9700C0A31} = {E0B8FBD1-A28A-4420-9DE2-6BD06035CBAC}
 		{AB9F4635-0ACB-4E5E-8E1B-431E0818CF60} = {E0B8FBD1-A28A-4420-9DE2-6BD06035CBAC}
 		{0599ACF0-8495-4E72-AB5E-B7446A5C413A} = {E0B8FBD1-A28A-4420-9DE2-6BD06035CBAC}
-		{C4224795-2CA3-439D-9B52-A0C76764F6CB} = {E0B8FBD1-A28A-4420-9DE2-6BD06035CBAC}
+		{4AA0AE41-A460-832A-A001-E440E294BF54} = {E0B8FBD1-A28A-4420-9DE2-6BD06035CBAC}
 	EndGlobalSection
 	GlobalSection(ExtensibilityGlobals) = postSolution
 		SolutionGuid = {B57FDA8F-95CF-478B-A0A8-7FF0F01CCFAB}

+ 34 - 38
Masuit.Tools.DigitalWatermarker.Test/DigitalWatermarkerTests.cs → Test/Masuit.Tools.DigitalWatermarker.Test/DigitalWatermarkerTests.cs

@@ -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 辅助方法
+}

+ 1 - 2
Masuit.Tools.DigitalWatermarker.Test/Masuit.Tools.DigitalWatermarker.Test.csproj → Test/Masuit.Tools.DigitalWatermarker.Test/Masuit.Tools.DigitalWatermarker.Test.csproj

@@ -12,14 +12,13 @@
   <ItemGroup>
     <PackageReference Include="coverlet.collector" Version="6.0.0" />
     <PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
-    <PackageReference Include="OpenCvSharp4" Version="4.11.0.20250507" />
     <PackageReference Include="OpenCvSharp4.runtime.win" Version="4.11.0.20250507" />
     <PackageReference Include="xunit" Version="2.5.3" />
     <PackageReference Include="xunit.runner.visualstudio" Version="2.5.3" />
   </ItemGroup>
 
   <ItemGroup>
-    <ProjectReference Include="..\Masuit.Tools.DigtalWatermarker\Masuit.Tools.DigtalWatermarker.csproj" />
+    <ProjectReference Include="..\..\Masuit.Tools.DigtalWatermarker\Masuit.Tools.DigtalWatermarker.csproj" />
   </ItemGroup>
 
   <ItemGroup>