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

Liveness check manager tests #255

Open
wants to merge 37 commits into
base: main
Choose a base branch
from
Open

Conversation

tobitech
Copy link
Contributor

Story: https://app.shortcut.com/smileid/stories/space/11439

Summary

  • Add test cases to cover liveness check manager functionalities.

Known Issues

N/A.

Test Instructions

N/A.

Screenshot

N/A

tobitech added 30 commits July 30, 2024 12:02
* implement design for selfie capture camera frame.

* add lottie animation, product cells for selfie enrollment and selfie authentication with strict mode.

* control visibility of smile emblem with showAttribution flag

* make selfie frame border themable.
* implement design for selfie capture camera frame.

* add lottie animation, product cells for selfie enrollment and selfie authentication with strict mode.

* update new selfie view model with existing implementation

* add some image utility functions, import selfie quality model and add selfie quality check to the image buffer from camera feed.

* some code formatting

* more code formatting

* compile coreml model to generate .mlmodelc file.

* automatically generate the model class file.

* remove uncompiled model file.

* disable lint from generated file.

* make a separate class that will handle all the image processing and classification tasks for selfie quality check.

* add some documentation and code formatting

* fix issue with crop to face function.

* Replace the selfie quality model with a new version that takes an image as input.

* remove call to check selfie quality in the v2 view model.

* disable linting for model generated file.

* code formattting.

* remove resources folder and the lottie file.

* code formatting.

* move model image cropping size info to a constant.
# Conflicts:
#	Example/Podfile.lock
…Liveness Check Manager. (#224)

* implement design for selfie capture camera frame.

* add lottie animation, product cells for selfie enrollment and selfie authentication with strict mode.

* update new selfie view model with existing implementation

* add some image utility functions, import selfie quality model and add selfie quality check to the image buffer from camera feed.

* some code formatting

* more code formatting

* compile coreml model to generate .mlmodelc file.

* automatically generate the model class file.

* remove uncompiled model file.

* disable lint from generated file.

* make a separate class that will handle all the image processing and classification tasks for selfie quality check.

* add some documentation and code formatting

* fix issue with crop to face function.

* introduce a new version for facedetector class and new debug design for selfie capture screen. also refactor state management and action handling for the selfie view model v2

* rename preview view to camera view controller

* hook up new face detector class to the new selfie view model.

* integrate the new model that takes an image input and also display the outputs in the debug view in real time.

* remove lottie resources. code formatting.

* Add new debug views and and also use av preview layer to calculate the bounding box

* Fix issue with face bounding box. implement face size and positioning within the frame.

* add instructions view to the selfie capture screen.

* add image capture to the selfie view model.

* Add a view to show captured images: selfie and liveness.

* add active liveness manager class and move updating directive to the view model.

* some clean up

* remove print statements from facedetectorv2

* move face geometry check for active liveness into the liveness manager

* refactor how current task is assigned and how directive for liveness task is displayed

* update criteria for passing the liveness checks

* fix the length of the captured image view

* code formatting

* add some documentation to liveness check manager class

* more docs

* run pod install

* folder structure changes.

* code formatting.

* import UIkit inside Imageclassifier.

* add the ML folder group into the right target.

* some layout changes.

* update the bundle for the generated ml model class.

* move initialisation for the image classifier into the init function so that it's done only once
# Conflicts:
#	Example/Podfile.lock
# Conflicts:
#	Example/Podfile.lock
* implement design for selfie capture camera frame.

* add lottie animation, product cells for selfie enrollment and selfie authentication with strict mode.

* update new selfie view model with existing implementation

* add some image utility functions, import selfie quality model and add selfie quality check to the image buffer from camera feed.

* some code formatting

* more code formatting

* compile coreml model to generate .mlmodelc file.

* automatically generate the model class file.

* remove uncompiled model file.

* disable lint from generated file.

* make a separate class that will handle all the image processing and classification tasks for selfie quality check.

* add some documentation and code formatting

* fix issue with crop to face function.

* introduce a new version for facedetector class and new debug design for selfie capture screen. also refactor state management and action handling for the selfie view model v2

* rename preview view to camera view controller

* hook up new face detector class to the new selfie view model.

* integrate the new model that takes an image input and also display the outputs in the debug view in real time.

* remove lottie resources. code formatting.

* Add new debug views and and also use av preview layer to calculate the bounding box

* Fix issue with face bounding box. implement face size and positioning within the frame.

* add instructions view to the selfie capture screen.

* add image capture to the selfie view model.

* Add a view to show captured images: selfie and liveness.

* add active liveness manager class and move updating directive to the view model.

* some clean up

* remove print statements from facedetectorv2

* move face geometry check for active liveness into the liveness manager

* refactor how current task is assigned and how directive for liveness task is displayed

* update criteria for passing the liveness checks

* fix the length of the captured image view

* code formatting

* add some documentation to liveness check manager class

* more docs

* run pod install

* folder structure changes.

* code formatting.

* import UIkit inside Imageclassifier.

* add some views for new selfie design

* create a folder group for lottie files in the resources folder and add a new lottie file.

* add lottie animation to liveness instructions screen.

* rename selfie capture instructions view to liveness capture

* Add liveness guides view with the 3 progress arcs

* restore lottie view

* import the liveness guide lottie file into resources.

* Remove comments
# Conflicts:
#	Example/Podfile.lock
#	Example/SmileID.xcodeproj/project.pbxproj
#	SmileID.xcodeproj/project.pbxproj
#	Sources/SmileID/Classes/DocumentVerification/Model/DocumentCaptureViewModel.swift
#	Sources/SmileID/Classes/SelfieCapture/SelfieViewModel.swift
# Conflicts:
#	Example/Podfile.lock
* improve code and file structure

* add the liveness guides

* add some config values to liveness guides view.

* change lottie animation frame

* run pod install

* setup animation progress time for the liiveness guide lottie animation.

* refactor the components of liveness guides view.
# Conflicts:
#	Example/Podfile.lock
* improve code and file structure

* add the liveness guides

* add some config values to liveness guides view.

* change lottie animation frame

* run pod install

* setup animation progress time for the liiveness guide lottie animation.

* fix homeview fore each warnings.

* connect face bounds detection to the the indicator.

* add throttling to camera feed.

* use full screen cover to present home screen products.

* control progress arc visibility based on progress.

* add a dummy submit function.

* setup timers.

* add animation to the progress fill arcs.

* refactor the components of liveness guides view.

* setup delay timer and introduce state for the current animation that should be displayed with instructions after delay.

* update arc shape init

* add new lottie files. define a guide animation enum to hold the animation details.

* some refactoring.

* create a validator class for the face observation data.

* refactor buffer image processing and communication between face detector, face validator and selfie view model. some code formatting.

* remove some debug views. some refactoring and improvements.

* make capture instruction strings localizable

* introduce current liveness task into the face validator to set the right capture instruction.

* fix cropping for selfie quality check.

* show or hide the circular ring and the liveness guides based on whether face is within bounds.

* add processing view to view captured images.

* reset animation as user is completing liveness checks

* localise instruction. use appropriate error for face not found.

* refactor image conversion for use in brightness and selfie quality checks.

* replace ObservedObject with StateObject in HomeView to prevent multiple initialising.
# Conflicts:
#	Example/Podfile.lock
#	Example/SmileID/Home/HomeView.swift
#	Sources/SmileID/Classes/SelfieCapture/SelfieViewModel.swift
# Conflicts:
#	Sources/SmileID/Classes/SelfieCapture/SelfieViewModel.swift
* improve code and file structure

* add the liveness guides

* add some config values to liveness guides view.

* change lottie animation frame

* run pod install

* setup animation progress time for the liiveness guide lottie animation.

* fix homeview fore each warnings.

* connect face bounds detection to the the indicator.

* add throttling to camera feed.

* use full screen cover to present home screen products.

* control progress arc visibility based on progress.

* add a dummy submit function.

* setup timers.

* add animation to the progress fill arcs.

* refactor the components of liveness guides view.

* setup delay timer and introduce state for the current animation that should be displayed with instructions after delay.

* update arc shape init

* add new lottie files. define a guide animation enum to hold the animation details.

* some refactoring.

* create a validator class for the face observation data.

* refactor buffer image processing and communication between face detector, face validator and selfie view model. some code formatting.

* remove some debug views. some refactoring and improvements.

* make capture instruction strings localizable

* introduce current liveness task into the face validator to set the right capture instruction.

* fix cropping for selfie quality check.

* show or hide the circular ring and the liveness guides based on whether face is within bounds.

* add processing view to view captured images.

* reset animation as user is completing liveness checks

* inject current liveness task into liveness guide to control which progress is showing. update reset delay timer on main thread. use appropriate error for face detection during cropping.

* processing screen layout. introduce a backport of stateobject.

* run pod install to import missing lottie files.

* present selfie capture flow in navigation view, programmatically navigate to processing view based on capture status. add a circular progress view with a loader image for the selfie processing view.

* refactor view appear setup and reset selfie capture state variables

* import submit method from selfie viewmodel.

* extract submit selfie functionality into a new class to manage the submission processes.

* some refactoring

* move backport and stateobject to helper folder.

* code formatting.

* restore threshold value

* replace ObservedObject with StateObject in HomeView so that it's initialised once.

* use proxy size instead of frame for window size calculation and face layout guide positioning.

* remove stateobject backport

* use a delegate to communicate selfie submission updates to selfie view model.
add the right title to processing screen.

* update processing changes on main thread.

* inject failure reason into the api call for submitting selfie

* rename selfie submission manager

* reset the threshold for timeout for liveness check.

* remove presentation mode variable.

* introduce an environment key to manage dismissing of the selfie capture flow.

* improve error handling of selfie capture.

* localise strings.

* code formatting.

* pod install

* make loader background color themeable.

* run pod install.

* fix missing files and build errors.

* redesign selfie capture screen to use box and oval for camera area and face bounding area

* add a view to preview selfie image, also add an actions view.

* redesign the the progress arcs for active liveness.

* improvements to validating face bounding box. add a frame to selfie preview image.

* remove processing view, add a view state to control visibility of different items based on capture state.

* adjust face size and position evaluation. fix layout for attribution and retry button.

* change active liveness progress colours

* code formatting.

* update faceboundmultiplier constant.

* refactor task timer.

* improve submission handling.
* add test for valid face.

* add more test cases for face validator class.

* refactor out common functionality for reusability in the validator tests.
# Conflicts:
#	Example/Podfile.lock
@tobitech tobitech self-assigned this Nov 15, 2024
@tobitech tobitech requested a review from a team as a code owner November 15, 2024 15:58
@prfectionist
Copy link

prfectionist bot commented Nov 15, 2024

Title

Liveness check manager tests


User description

Story: https://app.shortcut.com/smileid/stories/space/11439

Summary

  • Add test cases to cover liveness check manager functionalities.

Known Issues

N/A.

Test Instructions

N/A.

Screenshot

N/A


PR Type

Tests, Enhancement


Description

  • Added comprehensive test suite for LivenessCheckManager covering initialization, task sequence, task completion, and timeout scenarios
  • Enhanced testability of LivenessCheckManager through dependency injection and protocol abstractions
  • Introduced TimerProtocol and DispatchQueueType abstractions to enable proper mocking in tests
  • Improved code organization and encapsulation in test files
  • Made LivenessTask conform to CaseIterable for better task management

Changes walkthrough 📝

Relevant files
Enhancement
FaceValidatorTests.swift
Improve encapsulation in FaceValidatorTests                           

Example/Tests/FaceValidatorTests.swift

  • Changed visibility of class properties to private
+2/-2     
LivenessCheckManager.swift
Enhance LivenessCheckManager with better testability         

Sources/SmileID/Classes/FaceDetector/LivenessCheckManager.swift

  • Made LivenessTask conform to CaseIterable
  • Added dependency injection for timer and dispatch queue
  • Improved task management and timer handling
  • +32/-21 
    DispatchQueueType.swift
    Add DispatchQueue abstraction for testing                               

    Sources/SmileID/Classes/Helpers/DispatchQueueType.swift

  • Added protocol for abstracting DispatchQueue functionality
  • Implemented protocol conformance for DispatchQueue
  • +11/-0   
    TimerProtocol.swift
    Add Timer abstraction for testing                                               

    Sources/SmileID/Classes/Helpers/TimerProtocol.swift

  • Added protocol for abstracting Timer functionality
  • Implemented real and mock timer classes
  • +54/-0   
    Tests
    LivenessCheckManagerTests.swift
    Add test coverage for LivenessCheckManager functionality 

    Example/Tests/LivenessCheckManagerTests.swift

  • Added comprehensive test suite for LivenessCheckManager
  • Tests initialization, task sequence, task completion, and timeout
    scenarios
  • Includes mock implementations for testing dependencies
  • +134/-0 

    💡 PR-Agent usage: Comment /help "your question" on any pull request to receive relevant information

    @prfectionist
    Copy link

    prfectionist bot commented Nov 15, 2024

    PR Reviewer Guide 🔍

    Here are some key observations to aid the review process:

    ⏱️ Estimated effort to review: 3 🔵🔵🔵⚪⚪
    🧪 PR contains tests
    🔒 No security concerns identified
    ⚡ Recommended focus areas for review

    Thread Safety
    The timer and task management code should be reviewed for thread safety issues, particularly around the shared state modifications in handleTaskChange and resetTaskTimer methods

    Lock Usage
    The lock usage in RealTimer implementation should be reviewed to ensure there are no deadlock scenarios and proper lock/unlock patterns

    @prfectionist
    Copy link

    prfectionist bot commented Nov 15, 2024

    PR Code Suggestions ✨

    Explore these optional code suggestions:

    CategorySuggestion                                                                                                                                    Score
    Possible issue
    Add proper locking mechanism to prevent potential race conditions when scheduling timers

    Add lock.lock() before accessing timer in scheduledTimer method to ensure thread
    safety, similar to how invalidate() is implemented.

    Sources/SmileID/Classes/Helpers/TimerProtocol.swift [13-17]

     func scheduledTimer(
         withTimeInterval interval: TimeInterval, repeats: Bool, block: @escaping (any TimerProtocol) -> Void
     ) {
    +    lock.lock()
         defer { lock.unlock() }
         timer = Timer.scheduledTimer(
    Suggestion importance[1-10]: 8

    Why: Adding lock.lock() before timer initialization is crucial for thread safety, preventing potential race conditions that could lead to timer-related bugs in a concurrent environment.

    8
    Best practice
    Prevent memory leaks by using weak self in asynchronous closures

    Add weak self capture in the dispatchQueue.async closure to prevent potential retain
    cycles.

    Sources/SmileID/Classes/FaceDetector/LivenessCheckManager.swift [76-77]

    -dispatchQueue.async {
    +dispatchQueue.async { [weak self] in
    +        guard let self = self else { return }
             self.taskTimer.scheduledTimer(withTimeInterval: 1.0, repeats: true) { [weak self] _ in
    Suggestion importance[1-10]: 7

    Why: Using [weak self] in the async closure is important to prevent retain cycles and memory leaks, especially in a class that manages timers and delegates.

    7
    Add guard condition to prevent unnecessary timer scheduling

    Add a check to prevent task timer from being scheduled multiple times if
    resetTaskTimer() is called while a timer is already running.

    Sources/SmileID/Classes/FaceDetector/LivenessCheckManager.swift [72-77]

     private func resetTaskTimer() {
         stopTaskTimer()
         elapsedTime = 0.0
    -
    +    
    +    guard currentTask != nil else { return }
         dispatchQueue.async {
             self.taskTimer.scheduledTimer(withTimeInterval: 1.0, repeats: true) { [weak self] _ in
    Suggestion importance[1-10]: 6

    Why: The guard condition adds a safety check to prevent unnecessary timer scheduling when there's no current task, improving efficiency and preventing potential timing issues.

    6

    Copy link
    Contributor

    @JNdhlovu JNdhlovu left a comment

    Choose a reason for hiding this comment

    The reason will be displayed to describe this comment to others. Learn more.

    Commenting instead of approving but not a show stopper tests are pasing fine, however I know we have tests for the library in ./Tests I see these are in Example/Tests. Do we need to move the other as well?

    Base automatically changed from liveness-timeout-images to new-smart-selfie-capture December 10, 2024 11:42
    Base automatically changed from new-smart-selfie-capture to main December 13, 2024 15:36
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Projects
    None yet
    Development

    Successfully merging this pull request may close these issues.

    2 participants