Skip to content

Commit

Permalink
Fix unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
thisisabhash committed Apr 17, 2024
1 parent 2bc1bb7 commit cef241a
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 10 deletions.
26 changes: 24 additions & 2 deletions Tests/FaceLivenessTests/DetectedFaceTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import XCTest
@testable import FaceLiveness

@_spi(PredictionsFaceLiveness) import AWSPredictionsPlugin

final class DetectedFaceTests: XCTestCase {
var detectedFace: DetectedFace!
Expand Down Expand Up @@ -104,7 +104,29 @@ final class DetectedFaceTests: XCTestCase {
width: 0.6240418540649166,
height: 0.8144985824018897
)
let boundingBox = detectedFace.boundingBoxFromLandmarks(ovalRect: ovalRect)

let face = FaceLivenessSession.OvalMatchChallenge.Face(
distanceThreshold: 0.1,
distanceThresholdMax: 0.1,
distanceThresholdMin: 0.1,
iouWidthThreshold: 0.1,
iouHeightThreshold: 0.1
)

let oval = FaceLivenessSession.OvalMatchChallenge.Oval(boundingBox: .init(x: 0.1,
y: 0.1,
width: 0.1,
height: 0.1),
heightWidthRatio: 1.618,
iouThreshold: 0.1,
iouWidthThreshold: 0.1,
iouHeightThreshold: 0.1,
ovalFitTimeout: 1)

let boundingBox = detectedFace.boundingBoxFromLandmarks(ovalRect: ovalRect,
ovalMatchChallenge: .init(faceDetectionThreshold: 0.7,
face: face,
oval: oval))
XCTAssertEqual(boundingBox.origin.x, expectedBoundingBox.origin.x)
XCTAssertEqual(boundingBox.origin.y, expectedBoundingBox.origin.y)
XCTAssertEqual(boundingBox.width, expectedBoundingBox.width)
Expand Down
29 changes: 24 additions & 5 deletions Tests/FaceLivenessTests/LivenessTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -104,15 +104,37 @@ final class FaceLivenessDetectionViewModelTestCase: XCTestCase {
"setResultHandler(detectionResultHandler:) (FaceLivenessDetectionViewModel)"
])
XCTAssertEqual(livenessService.interactions, [
"initializeLivenessStream(withSessionID:userAgent:)"
"initializeLivenessStream(withSessionID:userAgent:challenges:)"
])
}

/// Given: A `FaceLivenessDetectionViewModel`
/// When: The viewModel is processes a single face result with a face distance less than the inital face distance
/// Then: The end state of this flow is `.recording(ovalDisplayed: false)` and initializeLivenessStream(withSessionID:userAgent:) is called
/// Then: The end state of this flow is `.recording(ovalDisplayed: false)`
func testTransitionToRecordingState() async throws {
viewModel.livenessService = self.livenessService

let face = FaceLivenessSession.OvalMatchChallenge.Face(
distanceThreshold: 0.32,
distanceThresholdMax: 0.1,
distanceThresholdMin: 0.1,
iouWidthThreshold: 0.1,
iouHeightThreshold: 0.1
)

let oval = FaceLivenessSession.OvalMatchChallenge.Oval(boundingBox: .init(x: 0.1,
y: 0.1,
width: 0.1,
height: 0.1),
heightWidthRatio: 1.618,
iouThreshold: 0.1,
iouWidthThreshold: 0.1,
iouHeightThreshold: 0.1,
ovalFitTimeout: 1)

viewModel.sessionConfiguration = .init(ovalMatchChallenge: .init(faceDetectionThreshold: 0.7,
face: face,
oval: oval))

viewModel.livenessState.checkIsFacePrepared()
XCTAssertEqual(viewModel.livenessState.state, .pendingFacePreparedConfirmation(.pendingCheck))
Expand All @@ -136,9 +158,6 @@ final class FaceLivenessDetectionViewModelTestCase: XCTestCase {
XCTAssertEqual(faceDetector.interactions, [
"setResultHandler(detectionResultHandler:) (FaceLivenessDetectionViewModel)"
])
XCTAssertEqual(livenessService.interactions, [
"initializeLivenessStream(withSessionID:userAgent:)"
])
}

/// Given: A `FaceLivenessDetectionViewModel`
Expand Down
12 changes: 9 additions & 3 deletions Tests/FaceLivenessTests/MockLivenessService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class MockLivenessService {
var onFinalClientEvent: (LivenessEvent<FinalClientEvent>, Date) -> Void = { _, _ in }
var onFreshnessEvent: (LivenessEvent<FreshnessEvent>, Date) -> Void = { _, _ in }
var onVideoEvent: (LivenessEvent<VideoEvent>, Date) -> Void = { _, _ in }
var onInitializeLivenessStream: (String, String) -> Void = { _, _ in }
var onInitializeLivenessStream: (String, String,[Challenge]?) -> Void = { _, _, _ in }
var onServiceException: (FaceLivenessSessionError) -> Void = { _ in }
var onCloseSocket: (URLSessionWebSocketTask.CloseCode) -> Void = { _ in }
}
Expand All @@ -44,10 +44,12 @@ extension MockLivenessService: LivenessService {
}

func initializeLivenessStream(
withSessionID sessionID: String, userAgent: String
withSessionID sessionID: String,
userAgent: String,
challenges: [Challenge]?
) throws {
interactions.append(#function)
onInitializeLivenessStream(sessionID, userAgent)
onInitializeLivenessStream(sessionID, userAgent, challenges)
}

func register(
Expand All @@ -62,6 +64,10 @@ extension MockLivenessService: LivenessService {
) {
interactions.append(#function)
}

func register(listener: @escaping (Challenge) -> Void, on event: LivenessEventKind.Server) {
interactions.append(#function)
}

func closeSocket(with code: URLSessionWebSocketTask.CloseCode) {
interactions.append(#function)
Expand Down

0 comments on commit cef241a

Please sign in to comment.