Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add first rendering tests #445

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions Macaw.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,8 @@
5B7E79CF20CBE69700C50BCF /* masking-path-02-b-manual.svg in Resources */ = {isa = PBXBuildFile; fileRef = 5B7E79CD20CBE69700C50BCF /* masking-path-02-b-manual.svg */; };
5B7E79DE20D2781A00C50BCF /* masking-intro-01-f-manual.reference in Resources */ = {isa = PBXBuildFile; fileRef = 5B7E79DC20D2781A00C50BCF /* masking-intro-01-f-manual.reference */; };
5B7E79DF20D2781A00C50BCF /* masking-intro-01-f-manual.svg in Resources */ = {isa = PBXBuildFile; fileRef = 5B7E79DD20D2781A00C50BCF /* masking-intro-01-f-manual.svg */; };
5B8C8E7D20EA0568002AEA29 /* paths-data-04-t-manual.jpg in Resources */ = {isa = PBXBuildFile; fileRef = 5B8C8E7C20EA0568002AEA29 /* paths-data-04-t-manual.jpg */; };
5B8C8E8120EA192F002AEA29 /* color-prop-01-b-manual.jpg in Resources */ = {isa = PBXBuildFile; fileRef = 5B8C8E7F20EA192F002AEA29 /* color-prop-01-b-manual.jpg */; };
5BAE201F208E1211006BF277 /* SVGCanvas.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5BAE201E208E1211006BF277 /* SVGCanvas.swift */; };
5BAE2038208E163D006BF277 /* polyline.reference in Resources */ = {isa = PBXBuildFile; fileRef = 5BAE2022208E1637006BF277 /* polyline.reference */; };
5BAE2039208E163D006BF277 /* polygon.reference in Resources */ = {isa = PBXBuildFile; fileRef = 5BAE2023208E1637006BF277 /* polygon.reference */; };
Expand Down Expand Up @@ -854,6 +856,8 @@
5B7E79CD20CBE69700C50BCF /* masking-path-02-b-manual.svg */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = "masking-path-02-b-manual.svg"; sourceTree = "<group>"; };
5B7E79DC20D2781A00C50BCF /* masking-intro-01-f-manual.reference */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = "masking-intro-01-f-manual.reference"; sourceTree = "<group>"; };
5B7E79DD20D2781A00C50BCF /* masking-intro-01-f-manual.svg */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = "masking-intro-01-f-manual.svg"; sourceTree = "<group>"; };
5B8C8E7C20EA0568002AEA29 /* paths-data-04-t-manual.jpg */ = {isa = PBXFileReference; lastKnownFileType = image.jpeg; path = "paths-data-04-t-manual.jpg"; sourceTree = "<group>"; };
5B8C8E7F20EA192F002AEA29 /* color-prop-01-b-manual.jpg */ = {isa = PBXFileReference; lastKnownFileType = image.jpeg; path = "color-prop-01-b-manual.jpg"; sourceTree = "<group>"; };
5BAE201E208E1211006BF277 /* SVGCanvas.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SVGCanvas.swift; sourceTree = "<group>"; };
5BAE2022208E1637006BF277 /* polyline.reference */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = polyline.reference; sourceTree = "<group>"; };
5BAE2023208E1637006BF277 /* polygon.reference */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = polygon.reference; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1313,6 +1317,8 @@
5B1AE18320B6A669007EECCB /* w3cSVGTests */ = {
isa = PBXGroup;
children = (
5B8C8E7F20EA192F002AEA29 /* color-prop-01-b-manual.jpg */,
5B8C8E7C20EA0568002AEA29 /* paths-data-04-t-manual.jpg */,
5B1AE22D20B6A669007EECCB /* color-prop-01-b-manual.reference */,
5B1AE18A20B6A669007EECCB /* color-prop-01-b-manual.svg */,
5B1AE22C20B6A669007EECCB /* color-prop-02-f-manual.reference */,
Expand Down Expand Up @@ -1746,6 +1752,7 @@
5B1AE2B620B6A669007EECCB /* paths-data-04-t-manual.svg in Resources */,
5B1AE25620B6A669007EECCB /* paths-data-05-t-manual.reference in Resources */,
5B1AE2D020B6A669007EECCB /* struct-defs-01-t-manual.reference in Resources */,
5B8C8E8120EA192F002AEA29 /* color-prop-01-b-manual.jpg in Resources */,
5B1AE25020B6A669007EECCB /* render-elems-01-t-manual.reference in Resources */,
5B1AE24520B6A669007EECCB /* struct-frag-01-t-manual.reference in Resources */,
5B1AE25B20B6A669007EECCB /* painting-stroke-01-t-manual.svg in Resources */,
Expand All @@ -1766,6 +1773,7 @@
5B37139B20BE95D7004BB6EE /* pservers-grad-07-b-manual.svg in Resources */,
5B1AE2B920B6A669007EECCB /* painting-control-02-f-manual.svg in Resources */,
5B7E79DE20D2781A00C50BCF /* masking-intro-01-f-manual.reference in Resources */,
5B8C8E7D20EA0568002AEA29 /* paths-data-04-t-manual.jpg in Resources */,
5B1AE28A20B6A669007EECCB /* shapes-polygon-01-t-manual.reference in Resources */,
5B1AE27920B6A669007EECCB /* shapes-circle-01-t-manual.svg in Resources */,
5B1AE25E20B6A669007EECCB /* paths-data-17-f-manual.reference in Resources */,
Expand Down
80 changes: 78 additions & 2 deletions MacawTests/MacawSVGTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ class MacawSVGTests: XCTestCase {
}

func testColorProp01() {
validateJSON("color-prop-01-b-manual")
validateJSONAndImage("color-prop-01-b-manual")
}

func testShapesEllipse01() {
Expand Down Expand Up @@ -534,7 +534,7 @@ class MacawSVGTests: XCTestCase {
}

func testPathsData04() {
validateJSON("paths-data-04-t-manual")
validateJSONAndImage("paths-data-04-t-manual")
}

func testPaintingStroke04() {
Expand Down Expand Up @@ -572,4 +572,80 @@ class MacawSVGTests: XCTestCase {
func testMaskingIntro01() {
validateJSON("masking-intro-01-f-manual")
}

func saveImage(image: UIImage, fileName: String) -> URL? {
guard let data = UIImageJPEGRepresentation(image, 0.6) else {
return .none
}
guard let directory = try? FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false) as NSURL else {
return .none
}
do {
let path = directory.appendingPathComponent("\(fileName).jpg")!
try data.write(to: path)
return path
} catch {
print(error.localizedDescription)
return .none
}
}

func imageForTest(_ test: String) -> UIImage {
let bundle = Bundle(for: type(of: TestUtils()))
do {
let node = try SVGParser.parse(bundle: bundle, path: test)
var frame = node.bounds
if frame == nil, let group = node as? Group {
frame = Group(contents: group.contents).bounds
}
let view = MacawView(node: node, frame: frame!.toCG())
let renderer = RenderUtils.createNodeRenderer(node, view: view, animationCache: view.animationCache)
let image = renderer.renderToImage(bounds: frame!)
return UIImage(cgImage: image.cgImage!, scale: 1.0, orientation: UIImageOrientation.downMirrored)
} catch {
XCTFail(error.localizedDescription)
}
XCTFail()
return UIImage()
}

func testA() {
createImage("paths-data-04-t-manual")
}

func createImage(_ test: String) {
if saveImage(image: imageForTest(test), fileName: test) == nil {
XCTFail()
}
}

func validateImage(_ test: String) {
let image = imageForTest(test)
let tempPath = saveImage(image: image, fileName: "temp")
let nodeImage = UIImage(contentsOfFile: tempPath!.path)!
validateImage(nodeImage: nodeImage, referenceFile: test)
}

func validateImage(nodeImage: UIImage, referenceFile: String) {
let bundle = Bundle(for: type(of: TestUtils()))
guard let path = bundle.path(forResource: referenceFile, ofType: "jpg") else {
XCTFail("No reference file \(referenceFile)")
return
}

let referenceImage = UIImage(contentsOfFile: path)
let referenceContent = UIImagePNGRepresentation(referenceImage!)!
let nodeContent = UIImagePNGRepresentation(nodeImage)!

if referenceContent != nodeContent {
let referencePath = saveImage(image: referenceImage!, fileName: referenceFile + "_reference")
let _ = saveImage(image: nodeImage, fileName: referenceFile + "_incorrect")
XCTFail("Not equal, see both pictures in \(String(describing: referencePath!.deletingLastPathComponent().path))")
}
}

func validateJSONAndImage(_ test: String) {
validateJSON(test)
validateImage(test)
}
}
Binary file added MacawTests/w3cSVGTests/color-prop-01-b-manual.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added MacawTests/w3cSVGTests/paths-data-04-t-manual.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.