forked from WeTransfer/WeScan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVisionRectangleDetectorTests.swift
103 lines (74 loc) · 2.92 KB
/
VisionRectangleDetectorTests.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
//
// VisionRectangleDetectorTests.swift
// WeScanTests
//
// Created by James Campbell on 8/8/18.
// Copyright © 2018 WeTransfer. All rights reserved.
//
import FBSnapshotTestCase
import XCTest
@testable import WeScan
final class VisionRectangleDetectorTests: FBSnapshotTestCase {
private var containerLayer: CALayer!
private var image: UIImage!
override func setUp() {
super.setUp()
recordMode = false
// Setting up containerLayer and creating the image to be tested on both tests in this class.
containerLayer = CALayer()
let targetSize = CGSize(width: 150, height: 150)
containerLayer.backgroundColor = UIColor.white.cgColor
containerLayer.frame = CGRect(origin: .zero, size: targetSize)
containerLayer.masksToBounds = true
let targetLayer = CALayer()
targetLayer.backgroundColor = UIColor.black.cgColor
targetLayer.frame = containerLayer.frame.insetBy(dx: 5, dy: 5)
containerLayer.addSublayer(targetLayer)
UIGraphicsBeginImageContextWithOptions(targetSize, true, 0.0)
containerLayer.render(in: UIGraphicsGetCurrentContext()!)
self.image = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()
}
override func tearDown() {
super.tearDown()
containerLayer = nil
image = nil
}
func testCorrectlyDetectsAndReturnsQuadilateral() {
let ciImage = CIImage(cgImage: image.cgImage!)
let expectation = XCTestExpectation(description: "Detect rectangle on CIImage")
VisionRectangleDetector.rectangle(forImage: ciImage) { (quad) in
DispatchQueue.main.async {
let resultView = UIView(frame: self.containerLayer.frame)
resultView.layer.addSublayer(self.containerLayer)
let quadView = QuadrilateralView(frame: resultView.bounds)
quadView.drawQuadrilateral(quad: quad!, animated: false)
quadView.backgroundColor = UIColor.red
resultView.addSubview(quadView)
self.FBSnapshotVerifyView(resultView)
expectation.fulfill()
}
}
wait(for: [expectation], timeout: 3.0)
}
func testCorrectlyDetectsAndReturnsQuadilateralPixelBuffer() {
let expectation = XCTestExpectation(description: "Detect rectangle on CVPixelBuffer")
if let pixelBuffer = image.pixelBuffer() {
VisionRectangleDetector.rectangle(forPixelBuffer: pixelBuffer) { (quad) in
DispatchQueue.main.async {
let resultView = UIView(frame: self.containerLayer.frame)
resultView.layer.addSublayer(self.containerLayer)
let quadView = QuadrilateralView(frame: resultView.bounds)
quadView.drawQuadrilateral(quad: quad!, animated: false)
quadView.backgroundColor = UIColor.red
resultView.addSubview(quadView)
self.FBSnapshotVerifyView(resultView)
expectation.fulfill()
}
}
} else {
XCTFail("could not convert image to pixelBuffer")
}
wait(for: [expectation], timeout: 3.0)
}
}