2015-06-09 21:28:11 +00:00
|
|
|
import XCTest
|
|
|
|
import ImageIO
|
|
|
|
@testable import Gifu
|
|
|
|
|
|
|
|
private let imageData = testImageDataNamed("mugen.gif")
|
2016-04-24 09:28:09 +00:00
|
|
|
private let staticImage = UIImage(data: imageData)!
|
|
|
|
private let preloadFrameCount = 20
|
2015-06-09 21:28:11 +00:00
|
|
|
|
|
|
|
class GifuTests: XCTestCase {
|
2016-04-24 09:28:09 +00:00
|
|
|
var animator: Animator!
|
|
|
|
var originalFrameCount: Int!
|
2015-06-09 21:28:11 +00:00
|
|
|
|
|
|
|
override func setUp() {
|
|
|
|
super.setUp()
|
2016-06-18 22:56:52 +00:00
|
|
|
animator = Animator(data: imageData, size: CGSize.zero, contentMode: .scaleToFill, framePreloadCount: preloadFrameCount)
|
2016-04-24 09:28:09 +00:00
|
|
|
originalFrameCount = Int(CGImageSourceGetCount(animator.imageSource))
|
2015-06-09 21:28:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func testIsAnimatable() {
|
2016-04-24 09:28:09 +00:00
|
|
|
XCTAssertTrue(animator.isAnimatable)
|
2015-06-09 21:28:11 +00:00
|
|
|
}
|
|
|
|
|
2016-05-27 03:07:25 +00:00
|
|
|
func testCurrentFrame() {
|
|
|
|
XCTAssertEqual(animator.currentFrameIndex, 0)
|
2016-06-18 22:56:52 +00:00
|
|
|
XCTAssertEqual(animator.currentFrameDuration, TimeInterval.infinity)
|
2016-05-27 03:07:25 +00:00
|
|
|
XCTAssertNil(animator.currentFrameImage)
|
|
|
|
}
|
|
|
|
|
2015-06-09 21:28:11 +00:00
|
|
|
func testFramePreload() {
|
2016-06-18 22:56:52 +00:00
|
|
|
let expectation = self.expectation(description: "frameDuration")
|
2015-06-09 21:28:11 +00:00
|
|
|
|
2016-04-24 09:28:09 +00:00
|
|
|
animator.prepareFrames {
|
|
|
|
let animatedFrameCount = self.animator.animatedFrames.count
|
|
|
|
XCTAssertEqual(animatedFrameCount, self.originalFrameCount)
|
2016-06-18 22:56:52 +00:00
|
|
|
XCTAssertNotNil(self.animator.frame(at: preloadFrameCount - 1))
|
|
|
|
XCTAssertNil(self.animator.frame(at: preloadFrameCount + 1)?.images)
|
2016-04-24 09:28:09 +00:00
|
|
|
XCTAssertEqual(self.animator.currentFrameIndex, 0)
|
2015-06-09 21:28:11 +00:00
|
|
|
|
2016-06-18 22:56:52 +00:00
|
|
|
self.animator.shouldChangeFrame(with: 1.0) { hasNewFrame in
|
2016-04-24 09:28:09 +00:00
|
|
|
XCTAssertTrue(hasNewFrame)
|
|
|
|
XCTAssertEqual(self.animator.currentFrameIndex, 1)
|
|
|
|
expectation.fulfill()
|
|
|
|
}
|
|
|
|
}
|
2015-06-09 21:28:11 +00:00
|
|
|
|
2016-06-18 22:56:52 +00:00
|
|
|
waitForExpectations(timeout: 1.0) { error in
|
2016-04-24 09:28:09 +00:00
|
|
|
if let error = error {
|
|
|
|
print("Error: \(error.localizedDescription)")
|
|
|
|
}
|
|
|
|
}
|
2015-06-09 21:28:11 +00:00
|
|
|
}
|
|
|
|
|
2016-04-24 09:28:09 +00:00
|
|
|
func testFrameInfo() {
|
2016-06-18 22:56:52 +00:00
|
|
|
let expectation = self.expectation(description: "testFrameInfoIsAccurate")
|
2016-04-24 09:28:09 +00:00
|
|
|
|
|
|
|
animator.prepareFrames {
|
2016-06-18 22:56:52 +00:00
|
|
|
let frameDuration = self.animator.frame(at: 5)?.duration ?? 0
|
2016-04-24 09:28:09 +00:00
|
|
|
XCTAssertTrue((frameDuration - 0.05) < 0.00001)
|
|
|
|
|
2016-06-18 22:56:52 +00:00
|
|
|
let imageSize = self.animator.frame(at: 5)?.size ?? CGSize.zero
|
2016-04-24 09:28:09 +00:00
|
|
|
XCTAssertEqual(imageSize, staticImage.size)
|
|
|
|
|
|
|
|
expectation.fulfill()
|
|
|
|
}
|
2015-06-09 21:28:11 +00:00
|
|
|
|
2016-06-18 22:56:52 +00:00
|
|
|
waitForExpectations(timeout: 1.0) { error in
|
2016-04-24 09:28:09 +00:00
|
|
|
if let error = error {
|
|
|
|
print("Error: \(error.localizedDescription)")
|
|
|
|
}
|
2015-06-09 21:28:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-18 22:56:52 +00:00
|
|
|
private func testImageDataNamed(_ name: String) -> Data {
|
|
|
|
let testBundle = Bundle(for: GifuTests.self)
|
|
|
|
let imagePath = testBundle.bundleURL.appendingPathComponent(name)
|
|
|
|
return (try! Data(contentsOf: imagePath))
|
2015-06-09 21:28:11 +00:00
|
|
|
}
|