1
1
using Microsoft . VisualStudio . TestTools . UnitTesting ;
2
2
using System ;
3
+ using System . Collections . Generic ;
3
4
using System . Drawing ;
4
5
using System . Drawing . Drawing2D ;
5
6
using System . Drawing . Imaging ;
6
7
using System . IO ;
8
+ using System . Linq ;
7
9
8
10
namespace Svg . UnitTests
9
11
{
@@ -13,6 +15,32 @@ namespace Svg.UnitTests
13
15
public class ImageComparisonTest
14
16
{
15
17
public TestContext TestContext { get ; set ; }
18
+ #if NETCORE
19
+ private static string _basePath = null ;
20
+ private static string GetSuiteTestsFolder
21
+ {
22
+ get
23
+ {
24
+ if ( _basePath != null ) return _basePath ;
25
+ var basePath = Environment . CurrentDirectory ;
26
+ while ( ! basePath . ToLower ( ) . EndsWith ( "svg" ) )
27
+ {
28
+ basePath = Path . GetDirectoryName ( basePath ) ;
29
+ }
30
+
31
+ _basePath = Path . Combine ( basePath , "Tests" ) ;
32
+ return _basePath ;
33
+ }
34
+ }
35
+
36
+ private static IEnumerable < object [ ] > GetData ( )
37
+ {
38
+ var basePath = GetSuiteTestsFolder ;
39
+ var testSuite = Path . Combine ( basePath , "W3CTestSuite" ) ;
40
+ var rows = File . ReadAllLines ( Path . Combine ( basePath , "Svg.UnitTests" , "PassingTests.csv" ) ) . Skip ( 1 ) ;
41
+ foreach ( var row in rows )
42
+ yield return new [ ] { ( object ) testSuite , ( object ) row , } ;
43
+ }
16
44
17
45
/// <summary>
18
46
/// Compares SVG images against reference PNG images from the W3C SVG 1.1 test suite.
@@ -21,9 +49,26 @@ public class ImageComparisonTest
21
49
/// Note that with the current test there are still a lot of false positives,
22
50
/// so this is not a definitive test for image equality yet.
23
51
/// </summary>
52
+ [ TestMethod ]
53
+ [ DynamicData ( nameof ( GetData ) , DynamicDataSourceType . Method ) ]
54
+ // [DataSource("Microsoft.VisualStudio.TestTools.DataSource.CSV",@"|DataDirectory|\..\..\PassingTests.csv","PassingTests#csv", DataAccessMethod.Sequential)]
55
+ //public void CompareSvgImageWithReference()
56
+ public void CompareSvgImageWithReference ( string basePath , string baseName )
57
+ {
58
+ var svgPath = Path . Combine ( basePath , "svg" , baseName + ".svg" ) ;
59
+ var pngPath = Path . Combine ( basePath , "png" , baseName + ".png" ) ;
60
+ var pngImage = Image . FromFile ( pngPath ) ;
61
+ var svgImage = LoadSvgImage ( baseName , svgPath ) ;
62
+ Assert . AreNotEqual ( null , pngImage , "Failed to load " + pngPath ) ;
63
+ Assert . AreNotEqual ( null , svgImage , "Failed to load " + svgPath ) ;
64
+ var difference = svgImage . PercentageDifference ( pngImage ) ;
65
+ Assert . IsTrue ( difference < 0.05 ,
66
+ baseName + ": Difference is " + ( difference * 100.0 ) . ToString ( ) + "%" ) ;
67
+ }
68
+ #else
24
69
[ TestMethod ]
25
70
[ DataSource ( "Microsoft.VisualStudio.TestTools.DataSource.CSV" ,
26
- @"|DataDirectory|\..\..\PassingTests.csv" ,
71
+ @"|DataDirectory|\..\..\..\ PassingTests.csv" ,
27
72
"PassingTests#csv" , DataAccessMethod . Sequential ) ]
28
73
public void CompareSvgImageWithReference ( )
29
74
{
@@ -32,16 +77,16 @@ public void CompareSvgImageWithReference()
32
77
{
33
78
basePath = Path . GetDirectoryName ( basePath ) ;
34
79
}
35
- basePath = Path . Combine ( basePath , "Tests" , "W3CTestSuite" ) ;
80
+ basePath = Path . Combine ( Path . Combine ( basePath , "Tests" ) , "W3CTestSuite" ) ;
36
81
var svgBasePath = Path . Combine ( basePath , "svg" ) ;
37
82
var baseName = TestContext . DataRow [ 0 ] as string ;
38
83
bool testSaveLoad = ! baseName . StartsWith ( "#" ) ;
39
84
if ( ! testSaveLoad )
40
85
{
41
86
baseName = baseName . Substring ( 1 ) ;
42
87
}
43
- var svgPath = Path . Combine ( basePath , "svg" , baseName + ".svg" ) ;
44
- var pngPath = Path . Combine ( basePath , "png" , baseName + ".png" ) ;
88
+ var svgPath = Path . Combine ( Path . Combine ( basePath , "svg" ) , baseName + ".svg" ) ;
89
+ var pngPath = Path . Combine ( Path . Combine ( basePath , "png" ) , baseName + ".png" ) ;
45
90
var pngImage = Image . FromFile ( pngPath ) ;
46
91
var svgDoc = LoadSvgDocument ( svgPath ) ;
47
92
Assert . IsNotNull ( svgDoc ) ;
@@ -50,7 +95,7 @@ public void CompareSvgImageWithReference()
50
95
Assert . AreNotEqual ( null , pngImage , "Failed to load " + pngPath ) ;
51
96
Assert . AreNotEqual ( null , svgImage , "Failed to load " + svgPath ) ;
52
97
var difference = svgImage . PercentageDifference ( pngImage ) ;
53
- Assert . IsTrue ( difference < 0.05 ,
98
+ Assert . IsTrue ( difference < 0.05 ,
54
99
baseName + ": Difference is " + ( difference * 100.0 ) . ToString ( ) + "%" ) ;
55
100
if ( ! testSaveLoad )
56
101
{
@@ -76,6 +121,7 @@ public void CompareSvgImageWithReference()
76
121
baseName + ": Difference is " + ( difference * 100.0 ) . ToString ( ) + "%" ) ;
77
122
}
78
123
}
124
+ #endif
79
125
80
126
/// <summary>
81
127
/// Enable this test to output the calculate percentage difference
@@ -85,15 +131,18 @@ public void CompareSvgImageWithReference()
85
131
// [TestClass]
86
132
public void RecordDiffForAllSvgImagesWithReference ( )
87
133
{
88
- var basePath = Path . GetDirectoryName ( Path . GetDirectoryName ( Path . GetDirectoryName (
89
- TestContext . TestRunDirectory ) ) ) ;
90
- basePath = Path . Combine ( basePath , "Tests" , "W3CTestSuite" ) ;
91
- var svgBasePath = Path . Combine ( basePath , "svg" ) ;
134
+ #if NETCORE
135
+ var basePath = Path . Combine ( GetSuiteTestsFolder , "W3CTestSuite" ) ;
136
+ #else
137
+ var basePath = Path . GetDirectoryName ( Path . GetDirectoryName ( Path . GetDirectoryName ( TestContext . TestRunDirectory ) ) ) ;
138
+ basePath = Path . Combine ( Path . Combine ( basePath , "Tests" ) , "W3CTestSuite" ) ;
139
+ #endif
140
+ // var svgBasePath = Path.Combine(basePath, "svg");
92
141
string [ ] lines = File . ReadAllLines ( @"..\..\..\..\Tests\Svg.UnitTests\all.csv" ) ;
93
142
foreach ( var baseName in lines )
94
143
{
95
- var svgPath = Path . Combine ( basePath , "svg" , baseName + ".svg" ) ;
96
- var pngPath = Path . Combine ( basePath , "png" , baseName + ".png" ) ;
144
+ var svgPath = Path . Combine ( Path . Combine ( basePath , "svg" ) , baseName + ".svg" ) ;
145
+ var pngPath = Path . Combine ( Path . Combine ( basePath , "png" ) , baseName + ".png" ) ;
97
146
if ( File . Exists ( pngPath ) && File . Exists ( svgPath ) )
98
147
{
99
148
var pngImage = Image . FromFile ( pngPath ) ;
0 commit comments