-
-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
4 changed files
with
254 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
128 changes: 128 additions & 0 deletions
128
lottie/src/test/java/com/airbnb/lottie/LottieInitializeTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
package com.airbnb.lottie; | ||
|
||
import static org.junit.Assert.assertNotNull; | ||
|
||
import android.content.Context; | ||
|
||
import androidx.annotation.NonNull; | ||
import androidx.annotation.Nullable; | ||
import androidx.test.platform.app.InstrumentationRegistry; | ||
|
||
import com.airbnb.lottie.network.LottieFetchResult; | ||
|
||
import org.junit.Before; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.junit.rules.TemporaryFolder; | ||
|
||
import java.io.InputStream; | ||
import java.util.Objects; | ||
|
||
public class LottieInitializeTest extends BaseTest { | ||
|
||
@Rule | ||
public final TemporaryFolder temporaryFolder1 = new TemporaryFolder(); | ||
|
||
@Rule | ||
public final TemporaryFolder temporaryFolder2 = new TemporaryFolder(); | ||
|
||
private final Context context = InstrumentationRegistry.getInstrumentation().getTargetContext(); | ||
|
||
@Before | ||
public void setExecutor() { | ||
LottieTask.EXECUTOR = Runnable::run; | ||
} | ||
|
||
@Test | ||
public void fetchAfterSecondInitialize() { | ||
initializeLottie(temporaryFolder1); | ||
// Fetching here causes the resource to be cached in temporaryFolder1: | ||
LottieResult<LottieComposition> result1 = LottieCompositionFactory.fromUrlSync(context, "resources://test1.json"); | ||
assertNotNull(result1.getValue()); | ||
|
||
// Manually delete to simulate the end of a test: | ||
temporaryFolder1.delete(); | ||
|
||
initializeLottie(temporaryFolder2); | ||
// Fetching here fails if L.setCacheProvider doesn't reset both its internal networkFetcher and its internal networkCache, because | ||
// temporaryFolder1 has been deleted: | ||
LottieResult<LottieComposition> result2 = LottieCompositionFactory.fromUrlSync(context, "resources://test1.json"); | ||
assertNotNull(result2.getValue()); | ||
} | ||
|
||
private void initializeLottie(TemporaryFolder temporaryFolder) { | ||
LottieConfig lottieConfig = new LottieConfig.Builder() | ||
.setNetworkCacheDir(temporaryFolder.getRoot()) | ||
.setNetworkFetcher(url -> { | ||
if (url.startsWith("resources://")) { | ||
InputStream stream = Objects.requireNonNull(getClass().getClassLoader()) | ||
.getResourceAsStream(url.substring("resources://".length())); | ||
if (stream != null) { | ||
return new LottieFetchSuccess(stream); | ||
} | ||
} | ||
|
||
return new LottieFetchFailure("Could not load <$url>"); | ||
}) | ||
.build(); | ||
Lottie.initialize(lottieConfig); | ||
} | ||
|
||
private static class LottieFetchSuccess implements LottieFetchResult { | ||
|
||
@NonNull private final InputStream jsonStream; | ||
|
||
LottieFetchSuccess(@NonNull InputStream jsonStream) { | ||
this.jsonStream = jsonStream; | ||
} | ||
|
||
@Override public boolean isSuccessful() { | ||
return true; | ||
} | ||
|
||
@Override @NonNull public InputStream bodyByteStream() { | ||
return jsonStream; | ||
} | ||
|
||
@Override public String contentType() { | ||
return "application/json"; | ||
} | ||
|
||
@Override @Nullable public String error() { | ||
return null; | ||
} | ||
|
||
@Override public void close() { | ||
// No-op | ||
} | ||
} | ||
|
||
private static class LottieFetchFailure implements LottieFetchResult { | ||
|
||
@NonNull private final String errorMessage; | ||
|
||
LottieFetchFailure(@NonNull String errorMessage) { | ||
this.errorMessage = errorMessage; | ||
} | ||
|
||
@Override public boolean isSuccessful() { | ||
return false; | ||
} | ||
|
||
@Override @NonNull public InputStream bodyByteStream() { | ||
throw new RuntimeException("LottieFetchFailure has no body"); | ||
} | ||
|
||
@Override @Nullable public String contentType() { | ||
return null; | ||
} | ||
|
||
@Override public String error() { | ||
return errorMessage; | ||
} | ||
|
||
@Override public void close() { | ||
// No-op | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
{ | ||
"v": "4.11.1", | ||
"fr": 60, | ||
"ip": 0, | ||
"op": 180, | ||
"w": 300, | ||
"h": 300, | ||
"nm": "Comp 1", | ||
"ddd": 0, | ||
"assets": [], | ||
"layers": [ | ||
{ | ||
"ddd": 0, | ||
"ind": 1, | ||
"ty": 4, | ||
"nm": "Shape Layer 1", | ||
"sr": 1, | ||
"ks": { | ||
"o": { | ||
"a": 0, | ||
"k": 100, | ||
"ix": 11 | ||
}, | ||
"r": { | ||
"a": 0, | ||
"k": 0, | ||
"ix": 10 | ||
}, | ||
"p": { | ||
"a": 0, | ||
"k": [ | ||
150, | ||
150, | ||
0 | ||
], | ||
"ix": 2 | ||
}, | ||
"a": { | ||
"a": 0, | ||
"k": [ | ||
0, | ||
0, | ||
0 | ||
], | ||
"ix": 1 | ||
}, | ||
"s": { | ||
"a": 0, | ||
"k": [ | ||
100, | ||
100, | ||
100 | ||
], | ||
"ix": 6 | ||
} | ||
}, | ||
"ao": 0, | ||
"shapes": [ | ||
{ | ||
"ty": "rc", | ||
"d": 1, | ||
"s": { | ||
"a": 0, | ||
"k": [ | ||
100, | ||
100 | ||
], | ||
"ix": 2 | ||
}, | ||
"p": { | ||
"a": 0, | ||
"k": [ | ||
0, | ||
0 | ||
], | ||
"ix": 3 | ||
}, | ||
"r": { | ||
"a": 0, | ||
"k": 0, | ||
"ix": 4 | ||
}, | ||
"nm": "Rectangle Path 1", | ||
"mn": "ADBE Vector Shape - Rect", | ||
"hd": false | ||
}, | ||
{ | ||
"ty": "fl", | ||
"c": { | ||
"a": 0, | ||
"k": [ | ||
0.928262987324, | ||
0, | ||
0, | ||
1 | ||
], | ||
"ix": 4 | ||
}, | ||
"o": { | ||
"a": 0, | ||
"k": 100, | ||
"ix": 5 | ||
}, | ||
"r": 1, | ||
"nm": "Fill 1", | ||
"mn": "ADBE Vector Graphic - Fill", | ||
"hd": false | ||
} | ||
], | ||
"ip": 0, | ||
"op": 180, | ||
"st": 0, | ||
"bm": 0 | ||
} | ||
] | ||
} |