2
2
layout : post
3
3
author : rafa
4
4
title : " Testing the camera on the simulator"
5
- date : 2019-04-02 22:36:49 +0200
5
+ date : 2019-04-08 22:36:49 +0200
6
6
comments : false
7
7
categories :
8
8
---
@@ -35,17 +35,17 @@ final class Camera: NSObject {
35
35
private let session: AVCaptureSession
36
36
private let metadataOutput: AVCaptureMetadataOutput
37
37
private weak var delegate: CameraOutputDelegate?
38
-
38
+
39
39
public init (
40
40
session : AVCaptureSession = AVCaptureSession (),
41
41
metadataOutput : AVCaptureMetadataOutput = AVCaptureMetadataOutput (),
42
42
delegate : CameraOutputDelegate?
43
43
) {
44
44
self .session = session
45
45
self .metadataOutput = metadataOutput
46
-
46
+
47
47
super .init ()
48
-
48
+
49
49
self .metadataOutput .setMetadataObjectsDelegate (self , queue : .main )
50
50
}
51
51
}
@@ -61,7 +61,7 @@ extension Camera: AVCaptureMetadataOutputObjectsDelegate {
61
61
delegate? .qrCode (failed : .invalidMetadata )
62
62
return
63
63
}
64
-
64
+
65
65
delegate? .qrCode (read : code)
66
66
}
67
67
}
@@ -77,7 +77,7 @@ final class CameraOutputSpy: CameraOutputDelegate {
77
77
var qrCodePassed: String ?
78
78
var qrCodeFailedCalled: Bool ?
79
79
var qrCodeErrorPassed: CameraError?
80
-
80
+
81
81
func qrCode (read code : String ) {
82
82
qrCodeReadCalled = true
83
83
qrCodePassed = code
@@ -114,20 +114,20 @@ I'm not going to lay down the nitty-gritty details about how it works, but the m
114
114
``` swift
115
115
struct Swizzler {
116
116
private let `class`: AnyClass
117
-
117
+
118
118
init (_ class : AnyClass ) {
119
119
self .`class` = `class`
120
120
}
121
-
121
+
122
122
func injectNSObjectInit (into selector : Selector) {
123
123
let original = [
124
124
class_getInstanceMethod (`class`, selector)
125
125
].compactMap { $0 }
126
-
126
+
127
127
let swizzled = [
128
128
class_getInstanceMethod (`class`, #selector (NSObject.init ))
129
129
].compactMap { $0 }
130
-
130
+
131
131
zip (original, swizzled)
132
132
.forEach {
133
133
method_setImplementation ($0 .0, method_getImplementation ($0 .1))
@@ -168,7 +168,7 @@ final class FakeMachineReadableCodeObject: AVMetadataMachineReadableCodeObject {
168
168
169
169
// 2
170
170
static func createFake (code : String , type : AVMetadataObject.ObjectType) -> FakeMachineReadableCodeObject? {
171
- // 3
171
+ // 3
172
172
Swizzler (self ).injectNSObjectInit (into : #selector (FakeMachineReadableCodeObject.init (fake: )))
173
173
return fake (fake : code, type : type)
174
174
}
@@ -207,4 +207,4 @@ XCTAssertNil(delegate.qrCodeErrorPassed)
207
207
208
208
As you can see, you can also check if your [ ` sut ` ] ( https://en.wikipedia.org/wiki/System_under_test ) handles just QR code.
209
209
210
- P.S: You can use this technique along side with other collaborators, like ` AVCaptureDevice ` , ` AVCaptureInput ` and ` AVCaptureOutput ` .
210
+ You can use this technique along side with other collaborators, like ` AVCaptureDevice ` , ` AVCaptureInput ` and ` AVCaptureOutput ` .
0 commit comments