Skip to content

Commit bf4ff56

Browse files
committed
chore(test): Asserting interactions on MockFaceDetector and MockLivenessService
1 parent 701a44e commit bf4ff56

File tree

4 files changed

+75
-10
lines changed

4 files changed

+75
-10
lines changed

Tests/FaceLivenessTests/LivenessTests.swift

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,12 @@ import Combine
77
final class FaceLivenessDetectionViewModelTestCase: XCTestCase {
88
var videoChunker: VideoChunker!
99
var viewModel: FaceLivenessDetectionViewModel!
10+
var faceDetector: MockFaceDetector!
11+
var livenessService: MockLivenessService!
1012

1113
override func setUp() {
12-
let faceDetector = MockFaceDetector()
14+
faceDetector = MockFaceDetector()
15+
livenessService = MockLivenessService()
1316
let videoChunker = VideoChunker(
1417
assetWriter: LivenessAVAssetWriter(),
1518
assetWriterDelegate: VideoChunker.AssetWriterDelegate(),
@@ -40,18 +43,32 @@ final class FaceLivenessDetectionViewModelTestCase: XCTestCase {
4043
/// When: The viewModel is first initialized
4144
/// Then: The state is `.intitial`
4245
func testInitialState() {
43-
viewModel.livenessService = MockLivenessService()
46+
// This first call comes from the FaceLivenessDetectionViewModel's initializer
47+
XCTAssertEqual(faceDetector.interactions, [
48+
"setResultHandler(detectionResultHandler:) (FaceLivenessDetectionViewModel)"
49+
])
50+
XCTAssertEqual(livenessService.interactions, [])
51+
52+
viewModel.livenessService = self.livenessService
4453
XCTAssertEqual(viewModel.livenessState.state, .initial)
54+
XCTAssertEqual(faceDetector.interactions, [
55+
"setResultHandler(detectionResultHandler:) (FaceLivenessDetectionViewModel)"
56+
])
57+
XCTAssertEqual(livenessService.interactions, [])
4558
}
4659

4760
/// Given: A `FaceLivenessDetectionViewModel`
4861
/// When: The viewModel is processes the happy path events
4962
/// Then: The end state of this flow is `.faceMatched`
5063
func testHappyPathToMatchedFace() async throws {
51-
viewModel.livenessService = MockLivenessService()
64+
viewModel.livenessService = self.livenessService
5265

5366
viewModel.livenessState.checkIsFacePrepared()
5467
XCTAssertEqual(viewModel.livenessState.state, .pendingFacePreparedConfirmation(.pendingCheck))
68+
XCTAssertEqual(faceDetector.interactions, [
69+
"setResultHandler(detectionResultHandler:) (FaceLivenessDetectionViewModel)"
70+
])
71+
XCTAssertEqual(livenessService.interactions, [])
5572

5673
viewModel.initializeLivenessStream()
5774
viewModel.process(newResult: .noFace)
@@ -76,5 +93,11 @@ final class FaceLivenessDetectionViewModelTestCase: XCTestCase {
7693

7794
viewModel.livenessState.faceMatched()
7895
XCTAssertEqual(viewModel.livenessState.state, .faceMatched)
96+
XCTAssertEqual(faceDetector.interactions, [
97+
"setResultHandler(detectionResultHandler:) (FaceLivenessDetectionViewModel)"
98+
])
99+
XCTAssertEqual(livenessService.interactions, [
100+
"initializeLivenessStream(withSessionID:userAgent:)"
101+
])
79102
}
80103
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//
2+
// Copyright Amazon.com Inc. or its affiliates.
3+
// All Rights Reserved.
4+
//
5+
// SPDX-License-Identifier: Apache-2.0
6+
//
7+
8+
@testable import FaceLiveness
9+
10+
final class MockFaceDetectionResultHandler {
11+
var intereactions: [String] = []
12+
}
13+
14+
extension MockFaceDetectionResultHandler: FaceDetectionResultHandler {
15+
func process(newResult: FaceLiveness.FaceDetectionResult) {
16+
intereactions.append(#function)
17+
}
18+
}

Tests/FaceLivenessTests/MockFaceDetector.swift

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,19 @@ import AVFoundation
99
@testable import FaceLiveness
1010
@_spi(PredictionsFaceLiveness) import AWSPredictionsPlugin
1111

12-
class MockFaceDetector: FaceDetector {
13-
func detectFaces(from buffer: CVPixelBuffer) {}
14-
func setResultHandler(detectionResultHandler: FaceLiveness.FaceDetectionResultHandler) {}
15-
init() {}
12+
final class MockFaceDetector {
13+
var interactions: [String] = []
14+
var detectionResultHandler: FaceDetectionResultHandler = MockFaceDetectionResultHandler()
15+
}
16+
17+
extension MockFaceDetector: FaceDetector {
18+
19+
func detectFaces(from buffer: CVPixelBuffer) {
20+
interactions.append(#function)
21+
}
22+
23+
func setResultHandler(detectionResultHandler: FaceDetectionResultHandler) {
24+
interactions.append("\(#function) (\(type(of: detectionResultHandler)))")
25+
self.detectionResultHandler = detectionResultHandler
26+
}
1627
}

Tests/FaceLivenessTests/MockLivenessService.swift

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,23 @@ import Foundation
99
@testable import FaceLiveness
1010
@_spi(PredictionsFaceLiveness) import AWSPredictionsPlugin
1111

12-
class MockLivenessService: LivenessService {
12+
class MockLivenessService {
13+
14+
var interactions: [String] = []
15+
1316
var onInitialClientEvent: (LivenessEvent<InitialClientEvent>, Date) -> Void = { _, _ in }
1417
var onFaceDetectionEvent: (LivenessEvent<FaceDetection>, Date) -> Void = { _, _ in }
1518
var onFinalClientEvent: (LivenessEvent<FinalClientEvent>, Date) -> Void = { _, _ in }
1619
var onFreshnessEvent: (LivenessEvent<FreshnessEvent>, Date) -> Void = { _, _ in }
1720
var onVideoEvent: (LivenessEvent<VideoEvent>, Date) -> Void = { _, _ in }
1821
var onInitializeLivenessStream: (String, String) -> Void = { _, _ in }
22+
}
23+
24+
extension MockLivenessService: LivenessService {
1925

2026
func send<T>(_ event: LivenessEvent<T>, eventDate: () -> Date) {
27+
interactions.append(#function)
28+
2129
switch event {
2230
case let initialClient as LivenessEvent<InitialClientEvent>:
2331
onInitialClientEvent(initialClient, eventDate())
@@ -36,15 +44,20 @@ class MockLivenessService: LivenessService {
3644
func initializeLivenessStream(
3745
withSessionID sessionID: String, userAgent: String
3846
) throws {
47+
interactions.append(#function)
3948
onInitializeLivenessStream(sessionID, userAgent)
4049
}
4150

4251
func register(
4352
onComplete: @escaping (ServerDisconnection) -> Void
44-
) {}
53+
) {
54+
interactions.append(#function)
55+
}
4556

4657
func register(
4758
listener: @escaping (FaceLivenessSession.SessionConfiguration) -> Void,
4859
on event: LivenessEventKind.Server
49-
) {}
60+
) {
61+
interactions.append(#function)
62+
}
5063
}

0 commit comments

Comments
 (0)