Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kaishin committed Jan 19, 2025
1 parent 6a6157e commit cdac876
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 128 deletions.
9 changes: 5 additions & 4 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ on:
- master

jobs:
# Device list: https://github.com/actions/runner-images/blob/main/images/macos/macos-15-Readme.md
build-xcode-15:
runs-on: macos-latest
env:
Expand All @@ -20,21 +21,21 @@ jobs:
with:
xcode-version: 15.4
- name: Run iOS tests
run: make test-ios DEVICE="iPhone 15"
run: make test-ios PLATFORM="iOS Simulator,name=iPhone 15"
- name: Run tvOS tests
run: make test-tvos

build-xcode-16:
runs-on: macos-latest
env:
DEVELOPER_DIR: /Applications/Xcode_16.2.app/Contents/Developer
DEVELOPER_DIR: /Applications/Xcode_16.app/Contents/Developer

steps:
- uses: actions/checkout@v1
- uses: maxim-lobanov/setup-xcode@v1
with:
xcode-version: 16.2
xcode-version: 16.0
- name: Run iOS tests
run: make test-ios DEVICE="iPhone 16"
run: make test-ios PLATFORM="iOS Simulator,name=iPhone 16"
- name: Run tvOS tests
run: make test-tvos
8 changes: 4 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
DEVICE ?= "iPhone 15"
PLATFORM ?= "iOS,name=iPhone 16"

test-ios:
set -o pipefail && \
xcodebuild test \
-project Gifu.xcodeproj \
-scheme Gifu \
-destination platform="iOS Simulator,name=$(DEVICE)" \
| xcpretty
-destination platform=$(PLATFORM) \
| xcbeautify

test-tvos:
set -o pipefail && \
xcodebuild test \
-project Gifu.xcodeproj \
-scheme Gifu \
-destination platform="tvOS Simulator,name=Apple TV" \
| xcpretty
| xcbeautify
240 changes: 120 additions & 120 deletions Tests/GifuTests.swift
Original file line number Diff line number Diff line change
@@ -1,157 +1,157 @@
#if os(iOS)
import XCTest
import ImageIO
@testable import Gifu

private let imageData = testImageDataNamed("mugen.gif")
private let staticImage = UIImage(data: imageData)!
private let preloadFrameCount = 20

class DummyAnimatable: GIFAnimatable {
init() {}
var animator: Animator? = nil
var image: UIImage? = nil
var layer = CALayer()
var frame: CGRect = .zero
var contentMode: UIView.ContentMode = .scaleToFill
func animatorHasNewFrame() {}
import ImageIO
import XCTest

@testable import Gifu

private let imageData = testImageDataNamed("mugen.gif")
private let staticImage = UIImage(data: imageData)!
private let preloadFrameCount = 20

class DummyAnimatable: GIFAnimatable {
init() {}
var animator: Animator? = nil
var image: UIImage? = nil
var layer = CALayer()
var frame: CGRect = .zero
var contentMode: UIView.ContentMode = .scaleToFill
func animatorHasNewFrame() {}
}

@MainActor
class GifuTests: XCTestCase {
var animator: Animator!
var originalFrameCount: Int!
let delegate = DummyAnimatable()

override func setUp() {
super.setUp()
animator = Animator(withDelegate: delegate)
animator.prepareForAnimation(
withGIFData: imageData, size: staticImage.size, contentMode: .scaleToFill)
originalFrameCount = 44
}

class GifuTests: XCTestCase {
var animator: Animator!
var originalFrameCount: Int!
let delegate = DummyAnimatable()

override func setUp() {
super.setUp()
animator = Animator(withDelegate: delegate)
animator.prepareForAnimation(
withGIFData: imageData, size: staticImage.size, contentMode: .scaleToFill)
originalFrameCount = 44
}

func testIsAnimatable() {
XCTAssertNotNil(animator.frameStore)
guard let store = animator.frameStore else { return }
XCTAssertTrue(store.isAnimatable)
}
func testIsAnimatable() {
XCTAssertNotNil(animator.frameStore)
guard let store = animator.frameStore else { return }
XCTAssertTrue(store.isAnimatable)
}

func testCurrentFrame() {
XCTAssertNotNil(animator.frameStore)
guard let store = animator.frameStore else { return }
XCTAssertEqual(store.currentFrameIndex, 0)
}
func testCurrentFrame() {
XCTAssertNotNil(animator.frameStore)
guard let store = animator.frameStore else { return }
XCTAssertEqual(store.currentFrameIndex, 0)
}

func testFramePreload() {
XCTAssertNotNil(animator.frameStore)
guard let store = animator.frameStore else { return }
func testFramePreload() {
XCTAssertNotNil(animator.frameStore)
guard let store = animator.frameStore else { return }

let expectation = self.expectation(description: "frameDuration")
let expectation = self.expectation(description: "frameDuration")

store.prepareFrames {
let animatedFrameCount = store.animatedFrames.count
XCTAssertEqual(animatedFrameCount, self.originalFrameCount)
XCTAssertNotNil(store.frame(at: preloadFrameCount - 1))
XCTAssertNil(store.frame(at: preloadFrameCount + 1)?.images)
XCTAssertEqual(store.currentFrameIndex, 0)
store.prepareFrames { [originalFrameCount] in
let animatedFrameCount = store.animatedFrames.count
XCTAssertEqual(animatedFrameCount, originalFrameCount)
XCTAssertNotNil(store.frame(at: preloadFrameCount - 1))
XCTAssertNil(store.frame(at: preloadFrameCount + 1)?.images)
XCTAssertEqual(store.currentFrameIndex, 0)

store.shouldChangeFrame(with: 1.0) { hasNewFrame in
XCTAssertTrue(hasNewFrame)
XCTAssertEqual(store.currentFrameIndex, 1)
expectation.fulfill()
}
store.shouldChangeFrame(with: 1.0) { hasNewFrame in
XCTAssertTrue(hasNewFrame)
XCTAssertEqual(store.currentFrameIndex, 1)
expectation.fulfill()
}
}

waitForExpectations(timeout: 1.0) { error in
if let error = error {
print("Error: \(error.localizedDescription)")
}
waitForExpectations(timeout: 1.0) { error in
if let error = error {
print("Error: \(error.localizedDescription)")
}
}
}

func testFrameInfo() {
XCTAssertNotNil(animator.frameStore)
guard let store = animator.frameStore else { return }
func testFrameInfo() {
XCTAssertNotNil(animator.frameStore)
guard let store = animator.frameStore else { return }

let expectation = self.expectation(description: "testFrameInfoIsAccurate")
let expectation = self.expectation(description: "testFrameInfoIsAccurate")

store.prepareFrames {
let frameDuration = store.frame(at: 5)?.duration ?? 0
XCTAssertTrue((frameDuration - 0.05) < 0.00001)
store.prepareFrames {
let frameDuration = store.frame(at: 5)?.duration ?? 0
XCTAssertTrue((frameDuration - 0.05) < 0.00001)

let imageSize = store.frame(at: 5)?.size ?? CGSize.zero
XCTAssertEqual(imageSize, staticImage.size)
let imageSize = store.frame(at: 5)?.size ?? CGSize.zero
XCTAssertEqual(imageSize, staticImage.size)

expectation.fulfill()
}
expectation.fulfill()
}

waitForExpectations(timeout: 1.0) { error in
if let error = error {
print("Error: \(error.localizedDescription)")
}
waitForExpectations(timeout: 1.0) { error in
if let error = error {
print("Error: \(error.localizedDescription)")
}
}
}

func testFinishedStates() {
animator = Animator(withDelegate: delegate)
animator.prepareForAnimation(
withGIFData: imageData, size: staticImage.size, contentMode: .scaleToFill, loopCount: 2)

XCTAssertNotNil(animator.frameStore)
guard let store = animator.frameStore else { return }
func testFinishedStates() {
animator = Animator(withDelegate: delegate)
animator.prepareForAnimation(
withGIFData: imageData, size: staticImage.size, contentMode: .scaleToFill, loopCount: 2)

let expectation = self.expectation(description: "testFinishedStatesAreSetCorrectly")
XCTAssertNotNil(animator.frameStore)
guard let store = animator.frameStore else { return }

store.prepareFrames {
let animatedFrameCount = store.animatedFrames.count
XCTAssertEqual(store.currentFrameIndex, 0)
let expectation = self.expectation(description: "testFinishedStatesAreSetCorrectly")

// Animate through all the frames (first loop)
for frame in 1..<animatedFrameCount {
XCTAssertFalse(store.isLoopFinished)
XCTAssertFalse(store.isFinished)
store.shouldChangeFrame(with: 1.0) { hasNewFrame in
XCTAssertTrue(hasNewFrame)
XCTAssertEqual(store.currentFrameIndex, frame)
}
}

XCTAssertTrue(store.isLoopFinished, "First loop should be finished")
XCTAssertFalse(store.isFinished, "Animation should not be finished yet")
store.prepareFrames {
let animatedFrameCount = store.animatedFrames.count
XCTAssertEqual(store.currentFrameIndex, 0)

// Animate through all the frames (first loop)
for frame in 1..<animatedFrameCount {
XCTAssertFalse(store.isLoopFinished)
XCTAssertFalse(store.isFinished)
store.shouldChangeFrame(with: 1.0) { hasNewFrame in
XCTAssertTrue(hasNewFrame)
XCTAssertEqual(store.currentFrameIndex, frame)
}
}

XCTAssertEqual(store.currentFrameIndex, 0)
XCTAssertTrue(store.isLoopFinished, "First loop should be finished")
XCTAssertFalse(store.isFinished, "Animation should not be finished yet")

// Animate through all the frames (second loop)
for frame in 1..<animatedFrameCount {
XCTAssertFalse(store.isLoopFinished)
XCTAssertFalse(store.isFinished)
store.shouldChangeFrame(with: 1.0) { hasNewFrame in
XCTAssertTrue(hasNewFrame)
XCTAssertEqual(store.currentFrameIndex, frame)
}
}
store.shouldChangeFrame(with: 1.0) { hasNewFrame in
XCTAssertTrue(hasNewFrame)
}

XCTAssertTrue(store.isLoopFinished, "Second loop should be finished")
XCTAssertTrue(store.isFinished, "Animation should be finished (loopCount: 2)")
XCTAssertEqual(store.currentFrameIndex, 0)

expectation.fulfill()
// Animate through all the frames (second loop)
for frame in 1..<animatedFrameCount {
XCTAssertFalse(store.isLoopFinished)
XCTAssertFalse(store.isFinished)
store.shouldChangeFrame(with: 1.0) { hasNewFrame in
XCTAssertTrue(hasNewFrame)
XCTAssertEqual(store.currentFrameIndex, frame)
}
}

waitForExpectations(timeout: 1.0) { error in
if let error = error {
print("Error: \(error.localizedDescription)")
}
XCTAssertTrue(store.isLoopFinished, "Second loop should be finished")
XCTAssertTrue(store.isFinished, "Animation should be finished (loopCount: 2)")

expectation.fulfill()
}

waitForExpectations(timeout: 1.0) { error in
if let error = error {
print("Error: \(error.localizedDescription)")
}
}
}
}

private func testImageDataNamed(_ name: String) -> Data {
let testBundle = Bundle(for: GifuTests.self)
let imagePath = testBundle.bundleURL.appendingPathComponent(name)
return (try! Data(contentsOf: imagePath))
}
#endif
private func testImageDataNamed(_ name: String) -> Data {
let testBundle = Bundle(for: GifuTests.self)
let imagePath = testBundle.bundleURL.appendingPathComponent(name)
return (try! Data(contentsOf: imagePath))
}

0 comments on commit cdac876

Please sign in to comment.