func testBackgroundReplacementVideoFrameProcessor()

in AmazonChimeSDK/AmazonChimeSDKTests/audiovideo/video/backgroundfilter/BackgroundFilterTest.swift [131:207]


    func testBackgroundReplacementVideoFrameProcessor() {
        #if canImport(AmazonChimeSDKMachineLearning)
        guard let frame = videoFrameGenerator.generateVideoFrame(image:self.testImage!) else {
            return
        }

        let videoSinkMock = mock(VideoSink.self)
        var processedImage: UIImage?
        var videoFrameReceivedExpectation: XCTestExpectation?

        given(videoSinkMock.onVideoFrameReceived(frame: any())) ~> { videoFrame in
            let result = self.processImage(frame: videoFrame)
            processedImage = result.image
            videoFrameReceivedExpectation!.fulfill()
        }

        let replacementImageColors = [UIColor.red]
        var backgroundReplacementImage = generateBackgroundImage(width: frame.width,
                                                                 height: frame.height,
                                                                 color: replacementImageColors[0])
        let logger = ConsoleLogger(name: "testBackgroundReplacementVideoFrameProcessor")
        let backgroundReplacementConfigurations = BackgroundReplacementConfiguration(
            logger: logger,
            backgroundReplacementImage: backgroundReplacementImage
        )

        let processor = BackgroundReplacementVideoFrameProcessor(
            backgroundReplacementConfiguration: backgroundReplacementConfigurations)

        processor.addVideoSink(sink: videoSinkMock)

        // Loop through the different generated backgrounds images and verify checksum.
        for index in 0...replacementImageColors.count - 1 {
            processedImage = nil
            videoFrameReceivedExpectation = expectation(description: "Video frame is received for index \(index)")

            if index > 0 {
                backgroundReplacementImage = generateBackgroundImage(width: frame.width,
                                                                     height: frame.height,
                                                                     color: replacementImageColors[index])
                processor.setBackgroundImage(newBackgroundReplacementImage: backgroundReplacementImage)
            }
            processor.onVideoFrameReceived(frame: frame)

            // Wait for the image to generate before proceeding to avoid non-determinism.
            wait(for: [videoFrameReceivedExpectation!], timeout: 1)
            XCTAssert(processedImage != nil)

            guard let gotCgImage = self.resize(image: processedImage!, to: downscaledSize).cgImage,
                let gotCgImageData = gotCgImage.dataProvider?.data,
                let gotCgImageBytes = CFDataGetBytePtr(gotCgImageData) else {
                XCTFail("Couldn't access CGImage data")
                return
            }
            guard let expectedCgImage = self.resize(image: self.expectedReplacementImage!, to: downscaledSize).cgImage,
                let expectedCgImageData = expectedCgImage.dataProvider?.data,
                let expectedCgImageBytes = CFDataGetBytePtr(expectedCgImageData) else {
                XCTFail("Couldn't access CGImage data")
                return
            }
            XCTAssert(gotCgImage.colorSpace?.model == .rgb)
            XCTAssert(expectedCgImage.colorSpace?.model == .rgb)
            XCTAssertEqual(expectedCgImage.bitsPerPixel, gotCgImage.bitsPerPixel)
            XCTAssertEqual(expectedCgImage.bitsPerComponent, gotCgImage.bitsPerComponent)
            XCTAssertEqual(expectedCgImage.height, gotCgImage.height)
            XCTAssertEqual(expectedCgImage.width, gotCgImage.width)

            let matchPercentage = self.getCGImageMatchPercentage(
                expectedCgImage: expectedCgImage, gotCgImage: gotCgImage,
                expectedCgImageBytes: expectedCgImageBytes, gotCgImageBytes: gotCgImageBytes)
            XCTAssert(matchPercentage >= expectedMatchPercentage,
                      "Expected match percentage \(matchPercentage) to be >= \(expectedMatchPercentage)")
        }
        #else
        XCTFail("AmazonChimeSDKMachineLearning could not be imported.")
        #endif
    }