Skip to content

Commit 08b37cd

Browse files
committed
Add the test case to ensure lottie bundle works
1 parent 6b7b76b commit 08b37cd

File tree

5 files changed

+53
-1
lines changed

5 files changed

+53
-1
lines changed

Example/SDWebImageLottiePlugin.xcodeproj/project.pbxproj

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
/* Begin PBXBuildFile section */
1010
216B8040AC5A1CEBB8A1AADD /* Pods_SDWebImageLottiePlugin_Example_macOS.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 9122A01A53A3D3143D4B6F21 /* Pods_SDWebImageLottiePlugin_Example_macOS.framework */; };
11+
320F7B26240D0C8900272A7A /* Assets.json in Resources */ = {isa = PBXBuildFile; fileRef = 320F7B25240D0C8900272A7A /* Assets.json */; };
12+
320F7B28240D0D2100272A7A /* img_0.png in Resources */ = {isa = PBXBuildFile; fileRef = 320F7B27240D0D2100272A7A /* img_0.png */; };
1113
3249A542240A5ECB00AB1888 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3249A541240A5ECB00AB1888 /* AppDelegate.swift */; };
1214
3249A544240A5ECB00AB1888 /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3249A543240A5ECB00AB1888 /* ViewController.swift */; };
1315
3249A546240A5ECE00AB1888 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 3249A545240A5ECE00AB1888 /* Assets.xcassets */; };
@@ -35,6 +37,8 @@
3537
/* Begin PBXFileReference section */
3638
20A0C01111066EB216549706 /* README.md */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = net.daringfireball.markdown; name = README.md; path = ../README.md; sourceTree = "<group>"; };
3739
2D65A2027DF1B719B440F0EA /* Pods-SDWebImageLottiePlugin_Example.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-SDWebImageLottiePlugin_Example.release.xcconfig"; path = "Target Support Files/Pods-SDWebImageLottiePlugin_Example/Pods-SDWebImageLottiePlugin_Example.release.xcconfig"; sourceTree = "<group>"; };
40+
320F7B25240D0C8900272A7A /* Assets.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = Assets.json; sourceTree = "<group>"; };
41+
320F7B27240D0D2100272A7A /* img_0.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = img_0.png; sourceTree = "<group>"; };
3842
3249A53F240A5ECB00AB1888 /* SDWebImageLottiePlugin_Example macOS.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = "SDWebImageLottiePlugin_Example macOS.app"; sourceTree = BUILT_PRODUCTS_DIR; };
3943
3249A541240A5ECB00AB1888 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
4044
3249A543240A5ECB00AB1888 /* ViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = "<group>"; };
@@ -92,6 +96,22 @@
9296
/* End PBXFrameworksBuildPhase section */
9397

9498
/* Begin PBXGroup section */
99+
320F7B23240D0C1000272A7A /* images */ = {
100+
isa = PBXGroup;
101+
children = (
102+
320F7B27240D0D2100272A7A /* img_0.png */,
103+
);
104+
path = images;
105+
sourceTree = "<group>";
106+
};
107+
320F7B24240D0C1700272A7A /* lotties */ = {
108+
isa = PBXGroup;
109+
children = (
110+
320F7B25240D0C8900272A7A /* Assets.json */,
111+
);
112+
path = lotties;
113+
sourceTree = "<group>";
114+
};
95115
3249A540240A5ECB00AB1888 /* SDWebImageLottiePlugin Example macOS */ = {
96116
isa = PBXGroup;
97117
children = (
@@ -163,6 +183,8 @@
163183
607FACE81AFB9204008FA782 /* Tests */ = {
164184
isa = PBXGroup;
165185
children = (
186+
320F7B24240D0C1700272A7A /* lotties */,
187+
320F7B23240D0C1000272A7A /* images */,
166188
607FACEB1AFB9204008FA782 /* Tests.swift */,
167189
607FACE91AFB9204008FA782 /* Supporting Files */,
168190
);
@@ -331,6 +353,8 @@
331353
isa = PBXResourcesBuildPhase;
332354
buildActionMask = 2147483647;
333355
files = (
356+
320F7B28240D0D2100272A7A /* img_0.png in Resources */,
357+
320F7B26240D0C8900272A7A /* Assets.json in Resources */,
334358
);
335359
runOnlyForDeploymentPostprocessing = 0;
336360
};

Example/Tests/Tests.swift

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,21 @@ import Lottie
33
import SDWebImage
44
@testable import SDWebImageLottiePlugin
55

6+
extension UIColor {
7+
func toHexString() -> String {
8+
var r: CGFloat = 0
9+
var g: CGFloat = 0
10+
var b: CGFloat = 0
11+
var a: CGFloat = 0
12+
13+
getRed(&r, green: &g, blue: &b, alpha: &a)
14+
15+
let rgb: Int = (Int)(r*255)<<16 | (Int)(g*255)<<8 | (Int)(b*255)<<0
16+
17+
return String(format: "#%06x", rgb)
18+
}
19+
}
20+
621
class Tests: XCTestCase {
722

823
override func setUp() {
@@ -35,4 +50,16 @@ class Tests: XCTestCase {
3550
self.waitForExpectations(timeout: 5, handler: nil)
3651
}
3752

53+
func testLottieImageWithBundle() throws {
54+
let bundle = Bundle(for: type(of: self))
55+
let fileURL = bundle.url(forResource: "Assets", withExtension: "json")!
56+
let lottieData = try Data(contentsOf: fileURL)
57+
let context = [SDWebImageContextOption.lottieBundle : bundle]
58+
let lottieImage = LOTAnimatedImage(data: lottieData, scale: 1, options: [.webImageContext: context])!
59+
let posterFrame = try XCTUnwrap(lottieImage.animatedImageFrame(at: 0))
60+
// Pick the color to check
61+
let color = try XCTUnwrap(posterFrame.sd_color(at: CGPoint(x: 150, y: 150)))
62+
XCTAssertEqual(color.toHexString(), "#00d1c1");
63+
}
64+
3865
}

Example/Tests/images/img_0.png

18.2 KB
Loading

Example/Tests/lotties/Assets.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"v":"5.1.7","fr":24,"ip":0,"op":120,"w":200,"h":200,"nm":"Assets","ddd":0,"assets":[{"id":"image_0","w":128,"h":126,"u":"images/","p":"img_0.png"},{"id":"comp_0","layers":[]}],"layers":[{"ddd":0,"ind":1,"ty":0,"nm":"PrecompComp","refId":"comp_0","sr":1,"ks":{"o":{"a":0,"k":100,"ix":11},"r":{"a":0,"k":0,"ix":10},"p":{"a":0,"k":[100,100,0],"ix":2},"a":{"a":0,"k":[100,100,0],"ix":1},"s":{"a":0,"k":[100,100,100],"ix":6}},"ao":0,"w":200,"h":200,"ip":0,"op":120,"st":0,"bm":0},{"ddd":0,"ind":2,"ty":2,"nm":"Lottie.png","cl":"png","refId":"image_0","sr":1,"ks":{"o":{"a":0,"k":100,"ix":11},"r":{"a":0,"k":0,"ix":10},"p":{"a":0,"k":[100,100,0],"ix":2},"a":{"a":0,"k":[64,63,0],"ix":1},"s":{"a":0,"k":[100,100,100],"ix":6}},"ao":0,"ip":0,"op":120,"st":0,"bm":0}],"markers":[]}

SDWebImageLottiePlugin/Classes/LOTAnimatedImage.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
FOUNDATION_EXPORT SDWebImageContextOption _Nonnull const SDWebImageContextLottieBundle;
1717

1818
/**
19-
A wrapper class to allow `LOTComposition` to be compatible for SDWebImage loading/cache/rendering system. The `GIF` image loading from `LOTCompositionView+WebCache` category, will use this subclass instead of `UIImage`.
19+
A wrapper class to allow `LOTComposition` to be compatible for SDWebImage loading/cache/rendering system. The lottie json loading from `LOTCompositionView+WebCache` category, will use this subclass instead of `UIImage`.
2020
2121
@note Though this class conforms to `SDAnimatedImage` protocol, so it's compatible to be used for `SDAnimatedImageView`. But it's normally discouraged to do so. Because it does not provide optimization for animation rendering. Instead, use `SDAnimatedImage` class with `SDAnimatedImageView`.
2222
@note If you want to get the bitmap version of Lottie animation at specify frame, call `animatedImageFrameAtIndex:` will result a snapshot for frame.

0 commit comments

Comments
 (0)