Skip to content

Commit 7ae9429

Browse files
committed
Update the test case which fix the compatible on macOS 10.14's simulator
1 parent f193c61 commit 7ae9429

File tree

2 files changed

+16
-18
lines changed

2 files changed

+16
-18
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# SDWebImageSwiftUI
22

3-
[![CI Status](https://travis-ci.org/SDWebImage/SDWebImageSwiftUI.svg?branch=master)](https://travis-ci.com/SDWebImage/SDWebImageSwiftUI)
3+
[![CI Status](https://travis-ci.org/SDWebImage/SDWebImageSwiftUI.svg?branch=master)](https://travis-ci.org/SDWebImage/SDWebImageSwiftUI)
44
[![Version](https://img.shields.io/cocoapods/v/SDWebImageSwiftUI.svg?style=flat)](https://cocoapods.org/pods/SDWebImageSwiftUI)
55
[![License](https://img.shields.io/cocoapods/l/SDWebImageSwiftUI.svg?style=flat)](https://cocoapods.org/pods/SDWebImageSwiftUI)
66
[![Platform](https://img.shields.io/cocoapods/p/SDWebImageSwiftUI.svg?style=flat)](https://cocoapods.org/pods/SDWebImageSwiftUI)

Tests/AnimatedImageTests.swift

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,9 @@ class AnimatedImageTests: XCTestCase {
9494

9595
func testAnimatedImageBinding() throws {
9696
let expectation = self.expectation(description: "AnimatedImage binding control")
97-
var viewUpdateCount = 0
97+
var isStopped = false
9898
// Use wrapper to make the @Binding works
9999
let wrapperView = AnimatedImage.WrapperView(name: "TestLoopCount.gif", bundle: TestUtils.testImageBundle(), isAnimating: true) { wrapperView, view, context in
100-
viewUpdateCount += 1
101100
guard let animatedImageView = view as? SDAnimatedImageView else {
102101
XCTFail("AnimatedImage's view should be SDAnimatedImageView")
103102
return
@@ -108,33 +107,32 @@ class AnimatedImageTests: XCTestCase {
108107
} else {
109108
XCTFail("AnimatedImage's image should be SDAnimatedImage")
110109
}
111-
// View update times before stable from SwiftUI, different behavior for AppKit/UIKit
112-
#if os(macOS)
113-
let stableViewUpdateCount = 1
114-
#else
115-
let stableViewUpdateCount = 2
116-
#endif
117-
switch viewUpdateCount {
118-
case stableViewUpdateCount:
119-
// #1 SwiftUI's own updateUIView call
110+
// Wait 1 second for SwiftUI's own `updateUIView` callback finished.
111+
// It's suck that the actual callback behavior is different on different iOS version or Simulator version, so I can assume which is the last callback using the callback count.
112+
if !isStopped {
113+
// # SwiftUI's own updateUIView call
120114
#if os(iOS) || os(tvOS)
121115
XCTAssertTrue(animatedImageView.isAnimating)
122116
#else
123117
XCTAssertTrue(animatedImageView.animates)
124118
#endif
125-
DispatchQueue.main.async {
126-
wrapperView.isAnimating = false
119+
DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
120+
if !isStopped {
121+
isStopped = true
122+
wrapperView.isAnimating = false
123+
} else {
124+
// Extra `updateUIView` from SwiftUI, ignore
125+
// During my test, iOS 13.3 (on macOS 10.15 simulator) called 2 times, iOS 13.0.0 (on macOS 10.15 simulator) called 1 time. iOS 13.3.3 (on macOS 10.14 simulator) called 2 times. Unregulated at all. Thanks Apple :)
126+
}
127127
}
128-
case stableViewUpdateCount + 1:
129-
// #3 AnimatedImage's isAnimating @Binding trigger update (from above)
128+
} else {
129+
// # AnimatedImage's isAnimating @Binding trigger update (from above)
130130
#if os(iOS) || os(tvOS)
131131
XCTAssertFalse(animatedImageView.isAnimating)
132132
#else
133133
XCTAssertFalse(animatedImageView.animates)
134134
#endif
135135
expectation.fulfill()
136-
default: break
137-
// do not care
138136
}
139137
}
140138
ViewHosting.host(view: wrapperView)

0 commit comments

Comments
 (0)