Skip to content

Commit

Permalink
Fix flakiness in Snapshot tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ipavlidakis committed Oct 9, 2023
1 parent 024c552 commit ea44ec6
Show file tree
Hide file tree
Showing 172 changed files with 80 additions and 18 deletions.
4 changes: 4 additions & 0 deletions StreamVideo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
4093861A2AA09E4A00FF5AF4 /* MemoryLogDestination.swift in Sources */ = {isa = PBXBuildFile; fileRef = 409386192AA09E4A00FF5AF4 /* MemoryLogDestination.swift */; };
4093861C2AA0A11500FF5AF4 /* LogQueue.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4093861B2AA0A11500FF5AF4 /* LogQueue.swift */; };
4093861F2AA0A21800FF5AF4 /* MemoryLogViewer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4093861E2AA0A21800FF5AF4 /* MemoryLogViewer.swift */; };
40942B5D2AD4110A00CE4EDD /* CALayer+IntelWorkaround.swift in Sources */ = {isa = PBXBuildFile; fileRef = 40942B5C2AD4110A00CE4EDD /* CALayer+IntelWorkaround.swift */; };
409BFA402A9F79D2003341EF /* View+OptionalPublisher.swift in Sources */ = {isa = PBXBuildFile; fileRef = 409BFA3F2A9F79D2003341EF /* View+OptionalPublisher.swift */; };
409BFA432A9F7BBB003341EF /* ReadableContentGuide.swift in Sources */ = {isa = PBXBuildFile; fileRef = 409BFA422A9F7BBB003341EF /* ReadableContentGuide.swift */; };
40AB31262A49838000C270E1 /* EventTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 40AB31252A49838000C270E1 /* EventTests.swift */; };
Expand Down Expand Up @@ -903,6 +904,7 @@
409386192AA09E4A00FF5AF4 /* MemoryLogDestination.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MemoryLogDestination.swift; sourceTree = "<group>"; };
4093861B2AA0A11500FF5AF4 /* LogQueue.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LogQueue.swift; sourceTree = "<group>"; };
4093861E2AA0A21800FF5AF4 /* MemoryLogViewer.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MemoryLogViewer.swift; sourceTree = "<group>"; };
40942B5C2AD4110A00CE4EDD /* CALayer+IntelWorkaround.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "CALayer+IntelWorkaround.swift"; sourceTree = "<group>"; };
409BFA3F2A9F79D2003341EF /* View+OptionalPublisher.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "View+OptionalPublisher.swift"; sourceTree = "<group>"; };
409BFA422A9F7BBB003341EF /* ReadableContentGuide.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ReadableContentGuide.swift; sourceTree = "<group>"; };
40AB31252A49838000C270E1 /* EventTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EventTests.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1617,6 +1619,7 @@
841FF5142A5E99B400809BBB /* CallEventsHandler_Tests.swift */,
843BD8F82AA0DF95002B3DAC /* PiPTrackSelectionUtils_Tests.swift */,
843BD8FA2AA0E660002B3DAC /* PiPUtils_Tests.swift */,
40942B5C2AD4110A00CE4EDD /* CALayer+IntelWorkaround.swift */,
);
path = Utils;
sourceTree = "<group>";
Expand Down Expand Up @@ -4373,6 +4376,7 @@
82FF40C02A17C73C00B4D95E /* CallingGroupView_Tests.swift in Sources */,
82E3BA3C2A0BADCA001AB93E /* MockResponseBuilder.swift in Sources */,
82E3BA502A0BAEAA001AB93E /* MockFunc.swift in Sources */,
40942B5D2AD4110A00CE4EDD /* CALayer+IntelWorkaround.swift in Sources */,
82FF40CA2A17C76400B4D95E /* MicrophoneCheckView_Tests.swift in Sources */,
8251E6302A17BEEF00E7257A /* ImageFactory.swift in Sources */,
8251E62C2A17BEB400E7257A /* StreamVideoTestResources.swift in Sources */,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import XCTest

@MainActor
final class ReconnectionView_Tests: StreamVideoUITestCase {

func test_reconnectionView_snapshot() throws {
let view = ReconnectionView(viewModel: CallViewModel(), viewFactory: TestViewFactory())
AssertSnapshot(view)
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,18 @@ final class LivestreamPlayer_Tests: StreamVideoTestCase {
func test_livestreamPlayer_snapshot() async throws {
// Given
let player = LivestreamPlayer(type: callType, id: callId)
.frame(width: defaultScreenSize.width, height: defaultScreenSize.height)

// Then
assertSnapshot(matching: player, as: .image(perceptualPrecision: 0.98))
AssertSnapshot(player)
}

@MainActor
func test_livestreamPlayer_snapshotHideParticipantCount() async throws {
// Given
let player = LivestreamPlayer(type: callType, id: callId, showParticipantCount: false)
.frame(width: defaultScreenSize.width, height: defaultScreenSize.height)

// Then
assertSnapshot(matching: player, as: .image(perceptualPrecision: 0.98))
AssertSnapshot(player)
}

@MainActor
Expand Down
21 changes: 17 additions & 4 deletions StreamVideoSwiftUITests/StreamVideoUITestCase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
@testable import StreamVideoSwiftUI
@testable import StreamVideo
import XCTest
import UIKit

class StreamVideoUITestCase: XCTestCase {

Expand All @@ -18,14 +19,26 @@ class StreamVideoUITestCase: XCTestCase {
var callCid: String { "\(callType):\(callId)" }
let sizeThatFits = CGSize(width: 100, height: 100)

override func setUpWithError() throws {
try super.setUpWithError()
override func setUp() {
super.setUp()

let streamVideo = StreamVideo.mock(httpClient: httpClient)
streamVideoUI = StreamVideoUI(streamVideo: streamVideo)
CALayer.swizzleShadow()
animations(enabled: false)
}


override func tearDown() {
animations(enabled: true)
CALayer.reverSwizzleShadow()
super.tearDown()
}

override func tearDownWithError() throws {
try super.tearDownWithError()
}

func animations(enabled: Bool) {
UIView.setAnimationsEnabled(enabled)
}
}
36 changes: 36 additions & 0 deletions StreamVideoSwiftUITests/Utils/CALayer+IntelWorkaround.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//
// Copyright © 2023 Stream.io Inc. All rights reserved.
//

import UIKit
import XCTest

extension CALayer {
static func swizzleShadow() {
swizzle(original: #selector(getter: shadowOpacity), modified: #selector(_swizzled_shadowOpacity))
swizzle(original: #selector(getter: shadowRadius), modified: #selector(_swizzled_shadowRadius))
swizzle(original: #selector(getter: shadowColor), modified: #selector(_swizzled_shadowColor))
swizzle(original: #selector(getter: shadowOffset), modified: #selector(_swizzled_shadowOffset))
swizzle(original: #selector(getter: shadowPath), modified: #selector(_swizzled_shadowPath))
}

static func reverSwizzleShadow() {
swizzle(original: #selector(_swizzled_shadowOpacity), modified: #selector(getter: shadowOpacity))
swizzle(original: #selector(_swizzled_shadowRadius), modified: #selector(getter: shadowRadius))
swizzle(original: #selector(_swizzled_shadowColor), modified: #selector(getter: shadowColor))
swizzle(original: #selector(_swizzled_shadowOffset), modified: #selector(getter: shadowOffset))
swizzle(original: #selector(_swizzled_shadowPath), modified: #selector(getter: shadowPath))
}

private static func swizzle(original: Selector, modified: Selector) {
let originalMethod = class_getInstanceMethod(self, original)!
let swizzledMethod = class_getInstanceMethod(self, modified)!
method_exchangeImplementations(originalMethod, swizzledMethod)
}

@objc func _swizzled_shadowOpacity() -> Float { .zero }
@objc func _swizzled_shadowRadius() -> CGFloat { .zero }
@objc func _swizzled_shadowColor() -> CGColor? { nil }
@objc func _swizzled_shadowOffset() -> CGSize { .zero }
@objc func _swizzled_shadowPath() -> CGPath? { nil }
}
14 changes: 6 additions & 8 deletions StreamVideoSwiftUITests/Utils/ToastView_Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ import SwiftUI
import SnapshotTesting
import XCTest

final class ToastView_Tests: XCTestCase {

private let perceptualPrecision: Float = 0.96
final class ToastView_Tests: StreamVideoTestCase {

func test_toastView_errorSnapshot() {
// Given
Expand All @@ -23,7 +21,7 @@ final class ToastView_Tests: XCTestCase {


// Then
AssertSnapshot(view, perceptualPrecision: perceptualPrecision)
AssertSnapshot(view)
}

func test_toastView_successSnapshot() {
Expand All @@ -38,7 +36,7 @@ final class ToastView_Tests: XCTestCase {


// Then
AssertSnapshot(view, perceptualPrecision: perceptualPrecision)
AssertSnapshot(view)
}

func test_toastView_warningSnapshot() {
Expand All @@ -53,7 +51,7 @@ final class ToastView_Tests: XCTestCase {


// Then
AssertSnapshot(view, perceptualPrecision: perceptualPrecision)
AssertSnapshot(view)
}

func test_toastView_infoSnapshot() {
Expand All @@ -68,7 +66,7 @@ final class ToastView_Tests: XCTestCase {


// Then
AssertSnapshot(view, perceptualPrecision: perceptualPrecision)
AssertSnapshot(view)
}

func test_toastView_errorSnapshotBottom() {
Expand All @@ -83,7 +81,7 @@ final class ToastView_Tests: XCTestCase {


// Then
AssertSnapshot(view, perceptualPrecision: perceptualPrecision)
AssertSnapshot(view)
}

}
15 changes: 14 additions & 1 deletion StreamVideoTests/StreamVideoTestCase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

@testable import StreamVideo
import XCTest
import UIKit

open class StreamVideoTestCase: XCTestCase {

Expand All @@ -13,8 +14,20 @@ open class StreamVideoTestCase: XCTestCase {
open override func setUp() {
super.setUp()
streamVideo = StreamVideo.mock(httpClient: httpClient)
CALayer.swizzleShadow()
animations(enabled: false)
}


open override func tearDown() {
animations(enabled: true)
CALayer.reverSwizzleShadow()
super.tearDown()
}

func animations(enabled: Bool) {
UIView.setAnimationsEnabled(enabled)
}

// TODO: replace this with something a bit better
func waitForCallEvent(nanoseconds: UInt64 = 500_000_000) async throws {
try await Task.sleep(nanoseconds: nanoseconds)
Expand Down

0 comments on commit ea44ec6

Please sign in to comment.